コード例 #1
0
        private bool ExecuteInternal(object input_parameters)
        {
            if (input_parameters != null)
            {
                PhpArray arr;
                if ((arr = input_parameters as PhpArray) != null)
                {
                    if (arr.Count != 0)
                    {
                        PreparedMode mode = PreparedMode.None;
                        foreach (var item in arr)
                        {
                            Debug.Assert(item.Key.Object != null);

                            if (item.Key.IsString && (mode == PreparedMode.Named || mode == PreparedMode.None))
                            {
                                mode = PreparedMode.Named;
                            }
                            else if (item.Key.IsInteger && (mode == PreparedMode.Numbers || mode == PreparedMode.None))
                            {
                                mode = PreparedMode.Numbers;
                            }
                            else
                            {
                                PhpException.Throw(PhpError.Warning, "Invalid bind parameter " + item.Key.Object.ToString());
                                return(false);
                            }

                            // bind the parameter
                            if (!this.bindValue(item.Key.Object, item.Value, PDOParamType.PDO_PARAM_STR))
                            {
                                PhpException.Throw(PhpError.Warning, "Can't bind parameter " + item.Key.Object.ToString());
                                return(false);
                            }
                        }
                    }
                }
                else
                {
                    PhpException.InvalidArgumentType("input_parameters", PhpArray.PhpTypeName);
                    return(false);
                }
            }
            return(this.ExecuteStatement());
        }
コード例 #2
0
ファイル: PDOStatement.cs プロジェクト: xmaxmex/Phalanger
        private bool bindValues(ScriptContext context, PhpArray parameters, PDOParamType dt)
        {
            if (parameters == null || parameters.Count == 0)
            {
                return(true);
            }

            PreparedMode mode = PreparedMode.None;

            foreach (var item in parameters)
            {
                Debug.Assert(item.Key.Object != null);

                if (item.Key.IsString && (mode == PreparedMode.Named || mode == PreparedMode.None))
                {
                    mode = PreparedMode.Named;
                }
                else if (item.Key.IsInteger && (mode == PreparedMode.Numbers || mode == PreparedMode.None))
                {
                    mode = PreparedMode.Numbers;
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, "Invalid bind parameter " + item.Key.Object.ToString());
                    return(false);
                }

                // bind the parameter
                var bindresult = this.bindValue(context, item.Key.Object, item.Value, dt);
                if (!Core.Convert.ObjectToBoolean(bindresult))
                {
                    PhpException.Throw(PhpError.Warning, "Can't bind parameter " + item.Key.Object.ToString());
                    return(false);
                }
            }

            //
            return(true);
        }
コード例 #3
0
ファイル: PDOStatement.cs プロジェクト: hansdude/Phalanger
        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();
        }
コード例 #4
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();
        }