Пример #1
0
        public PhpArray fetchAll(PDO.PDO_FETCH fetch_style = FETCH_USE_DEFAULT, PhpValue fetch_argument = default(PhpValue), PhpArray ctor_args = null)
        {
            if (m_dr == null)
            {
                m_pdo.HandleError(new PDOException("The data reader can not be null."));
                return(null);
            }

            if (fetch_style == PDO.PDO_FETCH.FETCH_COLUMN)
            {
                if (fetch_argument.IsInteger())
                {
                    FetchColNo = (int)fetch_argument;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("The fetch_argument must be an integer for FETCH_COLUMN."));
                    return(null);
                }
            }

            PhpArray returnArray = new PhpArray();

            while (m_dr.HasRows)
            {
                var value = fetch(fetch_style);

                if (value == PhpValue.False)
                {
                    break;
                }

                returnArray.Add(value);
            }

            return(returnArray);
        }
Пример #2
0
        /// <inheritDoc />
        public bool bindValue(PhpValue parameter, PhpValue value, PDO.PARAM data_type = (PDO.PARAM) PDO.PARAM_STR)
        {
            Debug.Assert(this.m_cmd != null);

            IDbDataParameter param = null;

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

                string key = parameter.AsString();
                if (key == null)
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter name must be a string."));
                    return(false);
                }

                if (key.Length > 0 && key[0] == ':')
                {
                    key = key.Substring(1);
                }

                param = m_cmd.Parameters[key];

                //rewrite the bounded params dictionary
                if (HasParamsBound)
                {
                    if (m_boundParams.ContainsKey(key))
                    {
                        m_boundParams.Remove(key);
                    }
                }
            }
            else if (m_positionalAttr)
            {
                if (!parameter.IsInteger())
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter index must be an integer."));
                    return(false);
                }
                int paramIndex = ((int)parameter) - 1;

                if (paramIndex < m_positionalPlaceholders.Count)
                {
                    param = m_cmd.Parameters[paramIndex];
                }

                //rewrite the bounded params dictionary
                if (HasParamsBound)
                {
                    if (m_boundParams.ContainsKey(paramIndex.ToString()))
                    {
                        m_boundParams.Remove(paramIndex.ToString());
                    }
                }
            }
            else
            {
                m_pdo.HandleError(new PDOException("No parameter mode set yet for this Statement. Possibly no parameters required?"));
                return(false);
            }

            if (param == null)
            {
                m_pdo.HandleError(new PDOException("No matching parameter found."));
                return(false);
            }

            switch (data_type)
            {
            case PDO.PARAM.PARAM_INT:
                if (value.IsInteger())
                {
                    param.DbType = DbType.Int32;
                    param.Value  = (int)value;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_STR:
                string str = null;
                if ((str = value.ToStringOrNull()) != null)
                {
                    param.DbType = DbType.String;
                    param.Value  = str;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_BOOL:
                if (value.IsBoolean())
                {
                    param.DbType = DbType.Boolean;
                    param.Value  = value.ToBoolean();
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_LOB:
                byte[] bytes = null;
                if ((bytes = value.ToBytesOrNull()) != null)
                {
                    param.DbType = DbType.Binary;
                    param.Value  = bytes;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            // Currently not supported by any drivers
            case PDO.PARAM.PARAM_NULL:
            case PDO.PARAM.PARAM_STMT:
            default:
                throw new NotImplementedException();
            }

            return(true);
        }
Пример #3
0
        /// <inheritDoc />
        public bool bindParam(PhpValue parameter, PhpAlias variable, PDO.PARAM data_type = PDO.PARAM.PARAM_STR, int length = -1, PhpValue driver_options = default(PhpValue))
        {
            Debug.Assert(this.m_cmd != null);

            // lazy instantization
            if (m_boundParams == null)
            {
                m_boundParams = new Dictionary <string, PhpAlias>();
            }

            IDbDataParameter param = null;

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

                string key = parameter.AsString();
                if (key == null)
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter name must be a string."));
                    return(false);
                }

                if (key.Length > 0 && key[0] == ':')
                {
                    key = key.Substring(1);
                }

                //Store the bounded variable reference in the dictionary
                m_boundParams.Add(key, variable);

                param = m_cmd.Parameters[key];
            }
            else if (m_positionalAttr)
            {
                if (!parameter.IsInteger())
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter index must be an integer."));
                    return(false);
                }
                int paramIndex = (int)parameter;

                //Store the bounded variable.Value reference in the dictionary
                m_boundParams.Add(paramIndex.ToString(), variable);

                if (paramIndex < m_positionalPlaceholders.Count)
                {
                    param = m_cmd.Parameters[paramIndex];
                }
            }
            else
            {
                m_pdo.HandleError(new PDOException("No parameter mode set yet for this Statement. Possibly no parameters required?"));
                return(false);
            }

            if (param == null)
            {
                m_pdo.HandleError(new PDOException("No matching parameter found."));
                return(false);
            }

            switch (data_type)
            {
            case PDO.PARAM.PARAM_INT:
                if (variable.Value.IsInteger())
                {
                    param.DbType = DbType.Int32;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_STR:
                string str = null;
                if ((str = variable.Value.ToStringOrNull()) != null)
                {
                    param.DbType = DbType.String;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_BOOL:
                if (variable.Value.IsBoolean())
                {
                    param.DbType = DbType.Boolean;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_LOB:
                byte[] bytes = null;
                if ((bytes = variable.Value.ToBytesOrNull()) != null)
                {
                    param.DbType = DbType.Binary;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            // Currently not supported by any drivers
            case PDO.PARAM.PARAM_NULL:
            case PDO.PARAM.PARAM_STMT:
                throw new NotImplementedException();
            }

            return(true);
        }
Пример #4
0
 /// <summary>
 /// Checks whether a dereferenced variable is integer.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is integer.</returns>
 public static bool is_int(PhpValue variable) => variable.IsInteger();
Пример #5
0
        /// <inheritDoc />
        public bool setFetchMode(params PhpValue[] args)
        {
            PDO_FETCH fetch = PDO_FETCH.FETCH_USE_DEFAULT;

            if (args.Length > 0)
            {
                PhpValue fetchMode = args[0];

                if (fetchMode.IsInteger())
                {
                    int value = (int)fetchMode.Long;
                    if (Enum.IsDefined(typeof(PDO_FETCH), value))
                    {
                        fetch = (PDO_FETCH)value;

                        this.m_fetchStyle = fetch;
                    }
                    else
                    {
                        m_pdo.HandleError(new PDOException("Given PDO_FETCH constant is not implemented."));
                    }
                }
            }

            if (fetch == PDO_FETCH.FETCH_COLUMN)
            {
                int colNo = -1;
                if (args.Length > 1)
                {
                    if (args[1].IsInteger())
                    {
                        colNo           = (int)args[1].ToLong();
                        this.FetchColNo = colNo;
                    }
                    else
                    {
                        m_pdo.HandleError(new PDOException("General error: colno must be an integer"));
                    }
                }
                else
                {
                    m_pdo.HandleError(new PDOException("General error: fetch mode requires the colno argument"));

                    //TODO what to do if missing parameter ?
                    //fetch = PDO_FETCH.FETCH_USE_DEFAULT;
                }
            }
            string className = null;

            if (fetch == PDO_FETCH.FETCH_CLASS)
            {
                if (args.Length > 1)
                {
                    className           = args[1].ToStringOrNull();
                    this.FetchClassName = className;

                    if (args.Length > 2)
                    {
                        this.FetchClassCtorArgs = args[2].AsArray()?.GetValues();
                    }
                }
                else
                {
                    throw new PDOException("General error: fetch mode requires the classname argument.");
                }
            }

            return(true);
        }
Пример #6
0
        public PDOStatement query(string statement, params PhpValue[] args)
        {
            PDOStatement stmt = new PDOStatement(this, statement, null);

            PDO_FETCH fetch = PDO_FETCH.FETCH_USE_DEFAULT;

            if (args.Length > 0)
            {
                PhpValue fetchMode = args[0];
                if (fetchMode.IsInteger())
                {
                    int value = (int)fetchMode.Long;
                    if (Enum.IsDefined(typeof(PDO_FETCH), value))
                    {
                        fetch = (PDO_FETCH)value;
                    }
                }
            }
            int?colNo = null;

            if (fetch == PDO_FETCH.FETCH_COLUMN)
            {
                if (args.Length > 2)
                {
                    colNo = (int)args[1].ToLong();
                }
                else
                {
                    //TODO what to do if missing parameter ?
                    fetch = PDO_FETCH.FETCH_USE_DEFAULT;
                }
            }
            string   className = null;
            PhpArray ctorArgs  = null;

            if (fetch == PDO_FETCH.FETCH_CLASS)
            {
                if (args.Length > 2)
                {
                    className = args[1].ToStringOrNull();
                    if (args.Length > 3)
                    {
                        ctorArgs = args[2].ArrayOrNull();
                    }
                }
                else
                {
                    //TODO what to do if missing parameter ?
                    fetch = PDO_FETCH.FETCH_USE_DEFAULT;
                }
            }
            PhpValue?fetchObject = null;

            if (fetch == PDO_FETCH.FETCH_OBJ)
            {
                if (args.Length > 2)
                {
                    fetchObject = args[1];
                    if (fetchObject.Value.IsNull)
                    {
                        //TODO passed object is null
                    }
                }
                else
                {
                    //TODO what to do if missing parameter ?
                    fetch = PDO_FETCH.FETCH_USE_DEFAULT;
                }
            }

            if (stmt.execute())
            {
                return(stmt);
            }
            else
            {
                return(null);
            }
        }
Пример #7
0
        /// <inheritDoc />
        public bool setFetchMode(params PhpValue[] args)
        {
            PDO_FETCH fetch = PDO_FETCH.FETCH_USE_DEFAULT;

            PhpValue fetchMode = args[0];

            if (fetchMode.IsInteger())
            {
                int value = (int)fetchMode.Long;
                if (Enum.IsDefined(typeof(PDO_FETCH), value))
                {
                    fetch = (PDO_FETCH)value;

                    this.m_fetchStyle = fetch;
                }
                else
                {
                    throw new PDOException("Given PDO_FETCH constant is not implemented.");
                }
            }

            if (fetch == PDO_FETCH.FETCH_COLUMN)
            {
                int colNo = -1;
                if (args.Length > 1)
                {
                    if (args[1].IsInteger())
                    {
                        colNo           = (int)args[1].ToLong();
                        this.FetchColNo = colNo;
                    }
                    else
                    {
                        throw new PDOException("General error: colno must be an integer");
                    }
                }
                else
                {
                    throw new PDOException("General error: fetch mode requires the colno argument");

                    //TODO what to do if missing parameter ?
                    //fetch = PDO_FETCH.FETCH_USE_DEFAULT;
                }
            }
            string   className = null;
            PhpArray ctorArgs  = null;

            if (fetch == PDO_FETCH.FETCH_CLASS)
            {
                if (args.Length > 2)
                {
                    className           = args[1].ToStringOrNull();
                    this.FetchClassName = className;

                    if (args.Length > 3)
                    {
                        ctorArgs = args[2].ArrayOrNull();
                    }
                }
                else
                {
                    throw new PDOException("General error: fetch mode requires the classname argument.");

                    //TODO what to do if missing parameter ?
                    //fetch = PDO_FETCH.FETCH_USE_DEFAULT;
                }
            }
            //TODO: FETCH_OBJ does not require additional parameters

            /*PhpValue? fetchObject = null;
             * if (fetch == PDO_FETCH.FETCH_OBJ)
             * {
             *  if (args.Length > 2)
             *  {
             *      fetchObject = args[1];
             *      if (fetchObject.Value.IsNull)
             *      {
             *          //TODO passed object is null
             *      }
             *  }
             *  else
             *  {
             *      //TODO what to do if missing parameter ?
             *      fetch = PDO_FETCH.FETCH_USE_DEFAULT;
             *  }
             * }*/

            return(true);
        }