Example #1
0
        public object fetch(ScriptContext context, object fetch_style /*=null*/, object cursor_orientation /*FETCH_ORI_NEXT*/, object cursor_offset /*0*/)
        {
            PDOFetchType ft;

            if (fetch_style == null || fetch_style == Arg.Default)
            {
                fetch_style = this.m_pdo.getAttribute(context, (int)PDOAttributeType.PDO_ATTR_DEFAULT_FETCH_MODE);
            }

            int fetch_style_int = PHP.Core.Convert.ObjectToInteger(fetch_style);

            if (!Enum.IsDefined(typeof(PDOFetchType), fetch_style_int))
            {
                PDOException.Throw(context, "Invalid fetch_style value", null, null, null);
                return(null);
            }
            ft = (PDOFetchType)fetch_style_int;
            var dr = this.CurrentReader;

            switch (ft)
            {
            case PDOFetchType.PDO_FETCH_ASSOC:
                return(Fetch_Assoc(m_pdo.Driver, dr, false) ?? (object)false);

            case PDOFetchType.PDO_FETCH_NUM:
                return(Fetch_Num(m_pdo.Driver, dr) ?? (object)false);

            case PDOFetchType.PDO_FETCH_BOTH:
            case PDOFetchType.PDO_FETCH_USE_DEFAULT:
                return(Fetch_Assoc(m_pdo.Driver, dr, true) ?? (object)false);

            default:
                throw new NotImplementedException();
            }
        }
Example #2
0
        public virtual object __construct(ScriptContext context, object argdsn, [Optional] object argusername, [Optional] object argpassword, [Optional] object argdriver_options)
        {
            string dsn            = PHP.Core.Convert.ObjectToString(argdsn);
            string username       = (argusername == Arg.Default) ? null : PHP.Core.Convert.ObjectToString(argusername);
            string password       = (argpassword == Arg.Default) ? null : PHP.Core.Convert.ObjectToString(argpassword);
            object driver_options = (argdriver_options == Arg.Default) ? null : argdriver_options;

            if (string.IsNullOrEmpty(dsn))
            {
                throw new ArgumentNullException();
            }

            const string uri = "uri:";

            if (dsn.StartsWith(uri))
            {
                Uri url = new Uri(dsn.Substring(uri.Length));
                throw new NotImplementedException("PDO uri handling");
            }
            string[] items = dsn.Split(new char[] { ':' }, 2);
            if (items.Length == 1)
            {
                //TODO : try to search for aliasing
                throw new NotImplementedException("PDO DSN aliasing");
            }
            if (items.Length == 2)
            {
                string drvName = items[0];
                this.m_driver = PDOLibraryDescriptor.GetProvider(drvName);
                if (this.m_driver == null)
                {
                    PDOException.Throw(context, "Driver not found", null, null, null);
                    return(null);
                }
                this.m_con = this.m_driver.OpenConnection(context, items[1], username, password, driver_options);
            }

            if (this.m_driver == null || this.m_con == null)
            {
                PDOException.Throw(context, "Invalid DSN", null, null, null);
                return(null);
            }

            //Defaults
            this.SetAttributeValueNoCheck(ATTR_AUTOCOMMIT, true);
            this.SetAttributeValueNoCheck(ATTR_DEFAULT_FETCH_MODE, FETCH_BOTH);
            this.SetAttributeValueNoCheck(ATTR_DRIVER_NAME, this.m_driver.Scheme);
            this.SetAttributeValueNoCheck(ATTR_ORACLE_NULLS, NULL_NATURAL);
            this.SetAttributeValueNoCheck(ATTR_STRINGIFY_FETCHES, false);
            this.SetAttributeValueNoCheck(ATTR_TIMEOUT, 30000);

            return(null);
        }
Example #3
0
        internal void Prepare(ScriptContext context, string query, Dictionary <int, object> options)
        {
            this.m_prepMode = PreparedMode.None;
            this.m_prepName = new Dictionary <string, string>();
            this.m_prepNum  = new List <string>();
            int           pos         = 0;
            StringBuilder sbRewritten = new StringBuilder();

            while (pos < query.Length)
            {
                char c = query[pos];
                switch (c)
                {
                case '?':
                {
                    if (this.m_prepMode == PreparedMode.None)
                    {
                        this.m_prepMode = PreparedMode.Numbers;
                    }
                    else
                    {
                        if (this.m_prepMode != PreparedMode.Numbers)
                        {
                            PDOException.Throw(context, "Mixed parameter mode : use only '?' or ':name' pattern", null, null, null);
                            return;
                        }
                    }
                    int    paramNum = this.m_prepNum.Count;
                    string pName    = this.m_pdo.Driver.GetParameterName("p" + paramNum);
                    this.m_prepNum.Insert(paramNum, pName);
                    sbRewritten.Append(pName);
                }
                break;

                case ':':
                {
                    if (this.m_prepMode == PreparedMode.None)
                    {
                        this.m_prepMode = PreparedMode.Named;
                    }
                    else
                    {
                        if (this.m_prepMode != PreparedMode.Named)
                        {
                            PDOException.Throw(context, "Mixed parameter mode : use only '?' or ':name' pattern", null, null, null);
                            return;
                        }
                    }
                    Match  m         = regName.Match(query, pos);
                    string paramName = m.Value;
                    string pName     = this.m_pdo.Driver.GetParameterName(paramName);
                    this.m_prepName[paramName] = pName;
                    sbRewritten.Append(pName);
                    pos += paramName.Length;
                }
                break;

                case '"':
                    sbRewritten.Append(c);
                    this.SkipToNext(query, sbRewritten, ref pos, '"');
                    break;

                case '\'':
                    sbRewritten.Append(c);
                    this.SkipToNext(query, sbRewritten, ref pos, '\'');
                    break;

                default:
                    sbRewritten.Append(c);
                    break;
                }
                pos++;
            }

            //this.CurrentCommand.CommandText = sbRewritten.ToString();
            this.Init(sbRewritten.ToString(), options);
            string[] arrParams = null;
            switch (this.m_prepMode)
            {
            case PreparedMode.Named:
                arrParams = this.m_prepName.Values.ToArray();
                break;

            case PreparedMode.Numbers:
                arrParams = this.m_prepNum.ToArray();
                break;

            case PreparedMode.None:
            default:
                break;
            }
            this.CurrentCommand.Parameters.Clear();
            if (arrParams != null)
            {
                foreach (string paramName in arrParams)
                {
                    var param = this.CurrentCommand.CreateParameter();
                    param.ParameterName = paramName;
                    this.CurrentCommand.Parameters.Add(param);
                }
            }
            this.CurrentCommand.Prepare();
        }