Exemple #1
0
        /// <summary>
        /// Executes a prepared statement
        /// </summary>
        /// <param name="input_parameters">An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.</param>
        /// <returns>Returns TRUE on success or FALSE on failure</returns>
        public virtual bool execute(PhpArray input_parameters = null)
        {
            if (Result != null)
            {
                // reusing command
                Connection.ClosePendingReader();
            }

            // parameters
            BindParameters(_cmd.Parameters, input_parameters);

            // execute
            Result = Connection.ExecuteCommand(_cmd, convertTypes: true, parameters: null /*already set*/, skipResults: false);

            // handle error:
            if (Connection.LastException == null)
            {
                // TODO: write-back output parameters from _cmd:
            }
            else
            {
                PDO.HandleError(Connection.LastException);
                return(false);
            }

            return(Result != null);
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PDOStatement" /> class.
        /// </summary>
        /// <param name="ctx">The php context.</param>
        /// <param name="pdo">The PDO statement is created for.</param>
        /// <param name="statement">The statement.</param>
        /// <param name="driver_options">The driver options.</param>
        internal PDOStatement(Context ctx, PDO pdo, string statement, PhpArray driver_options)
        {
            if (pdo.HasExecutedQuery)
            {
                if (!pdo.StoreLastExecutedQuery())
                {
                    pdo.HandleError(new PDOException("Last executed PDOStatement result set could not be saved correctly."));
                }
            }

            this.m_pdo     = pdo;
            this._ctx      = ctx;
            this.m_stmt    = statement;
            this.m_options = driver_options ?? PhpArray.Empty;

            this.m_cmd = pdo.CreateCommand(this.m_stmt);

            PrepareStatement();

            this.SetDefaultAttributes();
        }
Exemple #3
0
 private void RaiseError(string message)
 {
     m_pdo.HandleError(new PDOException(message));
 }
Exemple #4
0
 private protected void HandleError(ErrorInfo error)
 {
     _lastError = error;
     PDO.HandleError(error);
 }
Exemple #5
0
        /// <summary>
        /// Fetches the specified fetch style.
        /// </summary>
        /// <param name="fetch_style">Controls how the next row will be returned to the caller. This value must be one of the PDO::FETCH_* constants.</param>
        /// <param name="cursor_orientation">This value determines which row will be returned to the caller.</param>
        /// <param name="cursor_offet">Relative or absolute position move for the cursor.</param>
        /// <returns>The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure.</returns>
        public virtual PhpValue fetch(PDO.PDO_FETCH fetch_style = PDO_FETCH.Default /*0*/, PDO_FETCH_ORI cursor_orientation = PDO_FETCH_ORI.FETCH_ORI_NEXT /*0*/, int cursor_offet = 0)
        {
            PDO.ClearError();

            if (cursor_orientation != PDO_FETCH_ORI.FETCH_ORI_NEXT) // 0
            {
                throw new NotImplementedException(cursor_orientation.ToString());
            }

            if (Result == null)
            {
                throw new InvalidOperationException();
            }

            try
            {
                var how   = fetch_style != PDO_FETCH.Default ? fetch_style : _default_fetch_type;
                var flags = how & PDO_FETCH.Flags;

                switch (how & ~PDO_FETCH.Flags)
                {
                case PDO_FETCH.Default:
                case PDO_FETCH.FETCH_BOTH:
                    return(Result.FetchArray(true, true) ?? PhpValue.False);

                case PDO_FETCH.FETCH_ASSOC:
                    return(Result.FetchAssocArray() ?? PhpValue.False);

                case PDO_FETCH.FETCH_NUM:
                    return(Result.FetchArray(true, false) ?? PhpValue.False);

                case PDO_FETCH.FETCH_OBJ:
                    return(ObjectOrFalse(Result.FetchStdClass()));

                case PDO_FETCH.FETCH_BOUND:
                    return(FetchBound());

                case PDO_FETCH.FETCH_COLUMN:
                    return(fetchColumn(_fetch_column));

                case PDO.PDO_FETCH.FETCH_CLASS:
                    return(ObjectOrFalse(FetchClass(_default_fetch_class, _default_fetch_class_args)));

                case PDO_FETCH.FETCH_NAMED:
                    return(this.ReadNamed());

                //case PDO_FETCH.FETCH_LAZY:
                //    return new PDORow( ... ) reads columns lazily

                //case PDO_FETCH.FETCH_INTO:

                //case PDO_FETCH.FETCH_FUNC:

                //case PDO_FETCH.FETCH_KEY_PAIR:

                default:
                    throw new NotImplementedException($"fetch {how}");
                }
            }
            catch (System.Exception ex)
            {
                PDO.HandleError(ex);
                return(PhpValue.False);
            }
        }
Exemple #6
0
        /// <summary>
        /// Prepare the PDOStatement command.
        /// Set either positional, named or neither parameters mode.
        /// Create the parameters, add them to the command and prepare the command.
        /// </summary>
        /// <returns></returns>
        private bool PrepareStatement()
        {
            Debug.Assert(m_stmt != null && m_stmt.Length > 0);

            m_namedPlaceholders      = new Dictionary <string, string>();
            m_positionalPlaceholders = new List <string>();
            m_namedAttr      = false;
            m_positionalAttr = false;

            int pos            = 0;
            var rewrittenQuery = new StringBuilder();

            // Go throught the text query and find either positional or named parameters
            while (pos < m_stmt.Length)
            {
                char   currentChar = m_stmt[pos];
                string paramName   = "";

                switch (currentChar)
                {
                case '?':
                    if (m_namedAttr)
                    {
                        throw new PDOException("Mixing positional and named parameters not allowed. Use only '?' or ':name' pattern");
                    }

                    m_positionalAttr = true;

                    paramName = "@p" + m_positionalPlaceholders.Count();
                    m_positionalPlaceholders.Add(paramName);
                    rewrittenQuery.Append(paramName);

                    break;

                case ':':
                    if (m_positionalAttr)
                    {
                        throw new PDOException("Mixing positional and named parameters not allowed.Use only '?' or ':name' pattern");
                    }

                    m_namedAttr = true;

                    var    match = regName.Match(m_stmt, pos);
                    string param = match.Value;

                    paramName = "@" + param;
                    m_namedPlaceholders[param] = paramName;
                    rewrittenQuery.Append(paramName);

                    pos += param.Length;

                    break;

                case '"':
                    rewrittenQuery.Append(currentChar);
                    pos = SkipQuotedWord(m_stmt, rewrittenQuery, pos, '"');
                    break;

                case '\'':
                    rewrittenQuery.Append(currentChar);
                    pos = SkipQuotedWord(m_stmt, rewrittenQuery, pos, '\'');
                    break;

                default:
                    rewrittenQuery.Append(currentChar);
                    break;
                }
                pos++;
            }

            m_cmd.CommandText = rewrittenQuery.ToString();
            m_cmd.Parameters.Clear();

            if (m_positionalAttr)
            {
                // Mixed parameters not allowed
                if (m_namedAttr)
                {
                    m_pdo.HandleError(new PDOException("Mixed parameters mode not allowed. Use either only positional, or only named parameters."));
                    return(false);
                }

                foreach (var paramName in m_positionalPlaceholders.ToArray())
                {
                    var param = m_cmd.CreateParameter();
                    param.ParameterName = paramName;
                    m_cmd.Parameters.Add(param);
                }
            }
            else if (m_namedAttr)
            {
                foreach (var paramPair in m_namedPlaceholders)
                {
                    var param = m_cmd.CreateParameter();
                    param.ParameterName = paramPair.Key;
                    m_cmd.Parameters.Add(param);
                }
            }

            // Finalise the command preparation
            m_cmd.Prepare();

            return(true);
        }