Esempio n. 1
0
        /// <inheritDoc />
        public PhpValue fetch(int?fetch_style = default(int?), int cursor_orientation = default(int), int cursor_offet = 0)
        {
            this.m_pdo.ClearError();
            try
            {
                PDO.PDO_FETCH style = PDO.PDO_FETCH.FETCH_BOTH;
                if (fetch_style.HasValue && Enum.IsDefined(typeof(PDO.PDO_FETCH), fetch_style.Value))
                {
                    style = (PDO.PDO_FETCH)fetch_style.Value;
                }
                PDO.PDO_FETCH_ORI ori = PDO.PDO_FETCH_ORI.FETCH_ORI_NEXT;
                if (Enum.IsDefined(typeof(PDO.PDO_FETCH_ORI), cursor_orientation))
                {
                    ori = (PDO.PDO_FETCH_ORI)cursor_orientation;
                }

                switch (ori)
                {
                case PDO.PDO_FETCH_ORI.FETCH_ORI_NEXT:
                    break;

                default:
                    throw new NotSupportedException();
                }

                if (!this.m_dr.Read())
                {
                    return(PhpValue.False);
                }

                switch (style)
                {
                case PDO.PDO_FETCH.FETCH_OBJ:
                    return(this.ReadObj());

                case PDO.PDO_FETCH.FETCH_ASSOC:
                    return(PhpValue.Create(this.ReadArray(true, false)));

                case PDO.PDO_FETCH.FETCH_BOTH:
                case PDO.PDO_FETCH.FETCH_USE_DEFAULT:
                    return(PhpValue.Create(this.ReadArray(true, true)));

                case PDO.PDO_FETCH.FETCH_NUM:
                    return(PhpValue.Create(this.ReadArray(false, true)));

                default:
                    throw new NotImplementedException();
                }
            }
            catch (System.Exception ex)
            {
                this.m_pdo.HandleError(ex);
                return(PhpValue.False);
            }
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        /// <inheritDoc />
        public 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)
        {
            this.m_pdo.ClearError();

            if (storedQueryResult != null)
            {
                return(FetchFromStored());
            }
            else
            {
                if (cursor_orientation != PDO_FETCH_ORI.FETCH_ORI_NEXT) // 0
                {
                    throw new NotSupportedException(nameof(cursor_orientation));
                }

                try
                {
                    // read next row
                    if (!this.m_dr.Read())
                    {
                        return(PhpValue.False);
                    }

                    if (m_dr_names == null)
                    {
                        // Get the column schema, if possible,
                        // for the associative fetch
                        initializeColumnNames();
                    }

                    var style = fetch_style != PDO_FETCH.Default ? fetch_style : m_fetchStyle;
                    var flags = style & PDO_FETCH.Flags;

                    switch (style & ~PDO_FETCH.Flags)
                    {
                    case PDO_FETCH.FETCH_ASSOC:
                        return(this.ReadArray(true, false));

                    case PDO_FETCH.FETCH_NUM:
                        return(this.ReadArray(false, true));

                    case PDO_FETCH.Default:
                    case PDO_FETCH.FETCH_BOTH:
                        return(this.ReadArray(true, true));

                    case PDO_FETCH.FETCH_OBJ:
                        return(this.ReadObj());

                    case PDO_FETCH.FETCH_COLUMN:
                        if (FetchColNo.HasValue)
                        {
                            return(this.ReadColumn(FetchColNo.Value));
                        }
                        else
                        {
                            m_pdo.HandleError(new PDOException("The column number for FETCH_COLUMN mode is not set."));
                            return(PhpValue.False);
                        }

                    //case PDO.PDO_FETCH.FETCH_CLASS:
                    //    if (FetchClassName != null)
                    //    {
                    //        var obj = FetchClassName.Creator(_ctx, FetchClassCtorArgs ?? Array.Empty<PhpValue>());
                    //        // TODO: set properties
                    //        return PhpValue.FromClass(obj);
                    //    }
                    //    else
                    //    {
                    //        m_pdo.HandleError(new PDOException("The className for FETCH_CLASS mode is not set."));
                    //        return PhpValue.False;
                    //    }

                    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 {style}");
                    }
                }
                catch (System.Exception ex)
                {
                    this.m_pdo.HandleError(ex);
                    return(PhpValue.False);
                }
            }
        }
Esempio n. 4
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)
        {
            ClearError();

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

            if (Result == null)
            {
                // statement not executed
                PhpException.Throw(PhpError.Notice, "no results to fetch");
                return(false);
            }

            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)
            {
                HandleError(ex);
                return(PhpValue.False);
            }
        }
Esempio n. 5
0
        /// <inheritDoc />
        public PhpValue fetch(PDO.PDO_FETCH fetch_style = PDO.PDO_FETCH.FETCH_USE_DEFAULT, int cursor_orientation = default(int), int cursor_offet = 0)
        {
            this.m_pdo.ClearError();

            if (storedQueryResult != null)
            {
                return(FetchFromStored());
            }
            else
            {
                try
                {
                    PDO.PDO_FETCH style = this.m_fetchStyle;

                    if ((int)fetch_style != -1 && Enum.IsDefined(typeof(PDO.PDO_FETCH), fetch_style))
                    {
                        style = (PDO.PDO_FETCH)fetch_style;
                    }
                    PDO.PDO_FETCH_ORI ori = PDO.PDO_FETCH_ORI.FETCH_ORI_NEXT;
                    if (Enum.IsDefined(typeof(PDO.PDO_FETCH_ORI), cursor_orientation))
                    {
                        ori = (PDO.PDO_FETCH_ORI)cursor_orientation;
                    }

                    switch (ori)
                    {
                    case PDO.PDO_FETCH_ORI.FETCH_ORI_NEXT:
                        break;

                    default:
                        throw new NotSupportedException();
                    }

                    if (!this.m_dr.Read())
                    {
                        return(PhpValue.False);
                    }

                    // Get the column schema, if possible, for the associative fetch
                    if (this.m_dr_names == null)
                    {
                        this.m_dr_names = new string[m_dr.FieldCount];

                        if (this.m_dr.CanGetColumnSchema())
                        {
                            var columnSchema = this.m_dr.GetColumnSchema();

                            for (int i = 0; i < m_dr.FieldCount; i++)
                            {
                                this.m_dr_names[i] = columnSchema[i].ColumnName;
                            }
                        }
                        else
                        {
                            for (int i = 0; i < m_dr.FieldCount; i++)
                            {
                                this.m_dr_names[i] = this.m_dr.GetName(i);
                            }
                        }
                    }

                    switch (style)
                    {
                    case PDO.PDO_FETCH.FETCH_OBJ:
                        return(this.ReadObj());

                    case PDO.PDO_FETCH.FETCH_ASSOC:
                        return(PhpValue.Create(this.ReadArray(true, false)));

                    case PDO.PDO_FETCH.FETCH_BOTH:
                    case PDO.PDO_FETCH.FETCH_USE_DEFAULT:
                        return(PhpValue.Create(this.ReadArray(true, true)));

                    case PDO.PDO_FETCH.FETCH_NUM:
                        return(PhpValue.Create(this.ReadArray(false, true)));

                    case PDO.PDO_FETCH.FETCH_COLUMN:
                        if (FetchColNo != -1)
                        {
                            m_pdo.HandleError(new PDOException("The column number for FETCH_COLUMN mode is not set."));
                            return(PhpValue.False);
                        }

                        return(this.ReadArray(false, true)[FetchColNo].GetValue());

                    case PDO.PDO_FETCH.FETCH_CLASS:
                        if (FetchClassName == null)
                        {
                            m_pdo.HandleError(new PDOException("The className for FETCH_CLASS mode is not set."));
                            return(PhpValue.False);
                        }

                        var obj = _ctx.Create(FetchClassName, FetchClassCtorArgs ?? Array.Empty <PhpValue>());
                        return(PhpValue.FromClass(obj));

                    default:
                        throw new NotImplementedException();
                    }
                }
                catch (System.Exception ex)
                {
                    this.m_pdo.HandleError(ex);
                    return(PhpValue.False);
                }
            }
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        /// <inheritDoc />
        public PhpValue fetch(int fetch_style = -1, int cursor_orientation = default(int), int cursor_offet = 0)
        {
            this.m_pdo.ClearError();
            try
            {
                PDO.PDO_FETCH style = this.m_fetchStyle;

                if (fetch_style != -1 && Enum.IsDefined(typeof(PDO.PDO_FETCH), fetch_style))
                {
                    style = (PDO.PDO_FETCH)fetch_style;
                }
                PDO.PDO_FETCH_ORI ori = PDO.PDO_FETCH_ORI.FETCH_ORI_NEXT;
                if (Enum.IsDefined(typeof(PDO.PDO_FETCH_ORI), cursor_orientation))
                {
                    ori = (PDO.PDO_FETCH_ORI)cursor_orientation;
                }

                switch (ori)
                {
                case PDO.PDO_FETCH_ORI.FETCH_ORI_NEXT:
                    break;

                default:
                    throw new NotSupportedException();
                }

                if (!this.m_dr.Read())
                {
                    return(PhpValue.False);
                }

                // Get the column schema, if possible, for the associative fetch
                if (this.m_dr_names == null)
                {
                    this.m_dr_names = new string[m_dr.FieldCount];

                    if (this.m_dr.CanGetColumnSchema())
                    {
                        var columnSchema = this.m_dr.GetColumnSchema();

                        for (int i = 0; i < m_dr.FieldCount; i++)
                        {
                            this.m_dr_names[i] = columnSchema[i].ColumnName;
                        }
                    }
                }

                switch (style)
                {
                case PDO.PDO_FETCH.FETCH_OBJ:
                    return(this.ReadObj());

                case PDO.PDO_FETCH.FETCH_ASSOC:
                    return(PhpValue.Create(this.ReadArray(true, false)));

                case PDO.PDO_FETCH.FETCH_BOTH:
                case PDO.PDO_FETCH.FETCH_USE_DEFAULT:
                    return(PhpValue.Create(this.ReadArray(true, true)));

                case PDO.PDO_FETCH.FETCH_NUM:
                    return(PhpValue.Create(this.ReadArray(false, true)));

                case PDO.PDO_FETCH.FETCH_COLUMN:
                    return(this.ReadArray(false, true)[FetchColNo].GetValue());

                case PDO.PDO_FETCH.FETCH_CLASS:
                default:
                    throw new NotImplementedException();
                }
            }
            catch (System.Exception ex)
            {
                this.m_pdo.HandleError(ex);
                return(PhpValue.False);
            }
        }
Esempio n. 8
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);
        }