コード例 #1
0
        protected override string AddNumberedParameter(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers, StringCollection paramNames, int paramNumber)
        {
            while (paramNames.Count <= paramNumber)
                paramNames.Add(null);

            string paramName = paramNames[paramNumber];
            if (paramName == null)
            {
                paramName = AddParameterFromValue(command, v, modifiers);
                paramNames[paramNumber] = paramName;
            }
            return paramName;
        }
コード例 #2
0
        protected override string AddNumberedParameter(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers, StringCollection paramNames, int paramNumber)
        {
            while (paramNames.Count <= paramNumber)
            {
                paramNames.Add(null);
            }

            string paramName = paramNames[paramNumber];

            if (paramName == null)
            {
                paramName = AddParameterFromValue(command, v, modifiers);
                paramNames[paramNumber] = paramName;
            }
            return(paramName);
        }
コード例 #3
0
        protected virtual string AddParameterFromValue(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers)
        {
            IDbDataParameter p = command.CreateParameter();

            p.Direction = ParameterDirection.Input;

            p.ParameterName = GetNameForParameter(command.Parameters.Count);
            if (modifiers != null)
            {
                FieldHandlerFactory.GetFieldHandler(modifiers.DataTypeOverride).SetupDBParameter(p, v);
            }
            else
            {
                SetDbTypeFromValue(p, v, modifiers);
                p.Value = v;
            }
            command.Parameters.Add(p);
            return(p.ParameterName);
        }
コード例 #4
0
 protected abstract string AddNumberedParameter(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers, StringCollection paramNames, int paramNumber);
コード例 #5
0
        protected virtual string AddParameterFromValue(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers)
        {
            IDbDataParameter p = command.CreateParameter();
            p.Direction = ParameterDirection.Input;

            p.ParameterName = GetNameForParameter(command.Parameters.Count);
            if (modifiers != null)
                FieldHandlerFactory.GetFieldHandler(modifiers.DataTypeOverride).SetupDBParameter(p, v);
            else
            {
                SetDbTypeFromValue(p, v, modifiers);
                p.Value = v;
            }
            command.Parameters.Add(p);
            return p.ParameterName;
        }
コード例 #6
0
 protected virtual bool SetDbTypeFromValue(IDbDataParameter parameter, object value, SoqlLiteralValueModifiers modifiers)
 {
     DbType dbType;
     if (!paramTypes.TryGetValue(value.GetType(), out dbType))
         return false;
     parameter.DbType = dbType;
     return true;
 }
コード例 #7
0
 public SoqlParameterLiteralExpression(int parameterPos, SoqlLiteralValueModifiers modifiers)
 {
     this.ParameterPosition = parameterPos;
     this.Modifiers = modifiers;
 }
コード例 #8
0
 protected override string AddNumberedParameter(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers, StringCollection paramNames, int paramNumber)
 {
     return(AddParameterFromValue(command, v, modifiers));
 }
コード例 #9
0
 public SoqlLiteralExpression(object val, SoqlLiteralValueModifiers modifiers)
 {
     this.LiteralValue = val;
     this.Modifiers = modifiers;
 }
コード例 #10
0
 protected abstract string AddNumberedParameter(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers, StringCollection paramNames, int paramNumber);
コード例 #11
0
        public void BuildCommandWithParameters(System.Data.IDbCommand command, bool append, string query, object[] par, bool isRaw)
        {
            if (append)
            {
                if (command.CommandText == null)
                {
                    command.CommandText = "";
                }
                else if (command.CommandText.Length > 0)
                {
                    command.CommandText += ";\n";
                }
            }
            else
            {
                command.CommandText = "";
                command.Parameters.Clear();
            }

            System.Text.StringBuilder sb         = new System.Text.StringBuilder(query.Length * 2);
            StringCollection          paramNames = new StringCollection();

            for (int i = 0; i < query.Length; ++i)
            {
                char c = query[i];

                if (c == '\'')
                {
                    int j = ++i;
                    for (;; ++j)
                    {
                        if (j >= query.Length)
                        {
                            throw new ArgumentException("Query has unbalanced quotes");
                        }
                        if (query[j] == '\'')
                        {
                            if (j + 1 >= query.Length || query[j + 1] != '\'')
                            {
                                break;
                            }
                            // double apostrophe
                            j++;
                        }
                    }

                    string stringValue = query.Substring(i, j - i);
                    char   modifier    = j + 1 < query.Length ? query[j + 1] : ' ';
                    string paramName;

                    switch (modifier)
                    {
                    case 'V':
                        sb.Append('\'');
                        sb.Append(stringValue);
                        sb.Append('\'');
                        j++;
                        break;

                    case 'D':
                        paramName = AddParameterFromValue(command, DateTime.ParseExact(stringValue, "yyyyMMddHH:mm:ss", CultureInfo.InvariantCulture), null);
                        sb.Append(paramName);
                        j++;
                        break;

                    case 'A':
                        stringValue = stringValue.Replace("''", "'");
                        paramName   = AddParameterFromValue(command, stringValue, SoqlLiteralValueModifiers.AnsiString);
                        sb.Append(paramName);
                        j++;
                        break;

                    default:
                        if (!isRaw && (!UseSafeLiterals || !IsStringSafeForLiteral(stringValue)))
                        {
                            stringValue = stringValue.Replace("''", "'");
                            paramName   = AddParameterFromValue(command, stringValue, null);
                            sb.Append(paramName);
                        }
                        else
                        {
                            sb.Append('\'');
                            sb.Append(stringValue);
                            sb.Append('\'');
                        }
                        break;
                    }
                    i = j;
                }
                else if (c == '{')
                {
                    c = query[i + 1];

                    if (c == 'L')
                    {
                        // {L:fieldDataTypeName:value

                        int startPos = i + 3;
                        int endPos   = query.IndexOf(':', startPos);
                        if (endPos < 0)
                        {
                            throw new ArgumentException("Missing ':' in literal specification");
                        }

                        SoqlLiteralValueModifiers modifier = SoqlParser.ParseLiteralValueModifiers(query.Substring(startPos, endPos - startPos));
                        FieldDataType             fdt      = modifier.DataTypeOverride;

                        int  valueStartPos = endPos + 1;
                        bool anyEscape     = false;

                        for (i = valueStartPos; i < query.Length && query[i] != '}'; ++i)
                        {
                            if (query[i] == '\\')
                            {
                                i++;
                                anyEscape = true;
                            }
                        }

                        string literalValue = query.Substring(valueStartPos, i - valueStartPos);
                        if (anyEscape)
                        {
                            literalValue = literalValue.Replace("\\}", "}");
                            literalValue = literalValue.Replace("\\\\", "\\");
                        }

                        SoodaFieldHandler fieldHandler = FieldHandlerFactory.GetFieldHandler(fdt);
                        object            v            = fieldHandler.RawDeserialize(literalValue);

                        if (v == null)
                        {
                            sb.Append("null");
                        }
                        else if (UseSafeLiterals && v is int)
                        {
                            sb.Append((int)v);
                        }
                        else if (UseSafeLiterals && v is string && IsStringSafeForLiteral((string)v))
                        {
                            sb.Append('\'');
                            sb.Append((string)v);
                            sb.Append('\'');
                        }
                        else
                        {
                            IDbDataParameter p = command.CreateParameter();
                            p.Direction     = ParameterDirection.Input;
                            p.ParameterName = GetNameForParameter(command.Parameters.Count);
                            fieldHandler.SetupDBParameter(p, v);
                            command.Parameters.Add(p);
                            sb.Append(p.ParameterName);
                        }
                    }
                    else if (c >= '0' && c <= '9')
                    {
                        i++;
                        int paramNumber = 0;
                        do
                        {
                            paramNumber = paramNumber * 10 + c - '0';
                            c           = query[++i];
                        } while (c >= '0' && c <= '9');

                        SoqlLiteralValueModifiers modifiers = null;
                        if (c == ':')
                        {
                            int startPos = i + 1;
                            i = query.IndexOf('}', startPos);
                            if (i < 0)
                            {
                                throw new ArgumentException("Missing '}' in parameter specification");
                            }
                            modifiers = SoqlParser.ParseLiteralValueModifiers(query.Substring(startPos, i - startPos));
                        }
                        else if (c != '}')
                        {
                            throw new ArgumentException("Missing '}' in parameter specification");
                        }

                        object v = par[paramNumber];

                        if (v is SoodaObject)
                        {
                            v = ((SoodaObject)v).GetPrimaryKeyValue();
                        }

                        if (v == null)
                        {
                            sb.Append("null");
                        }
                        else if (UseSafeLiterals && v is int)
                        {
                            sb.Append((int)v);
                        }
                        else if (UseSafeLiterals && v is string && IsStringSafeForLiteral((string)v))
                        {
                            sb.Append('\'');
                            sb.Append((string)v);
                            sb.Append('\'');
                        }
                        else
                        {
                            sb.Append(AddNumberedParameter(command, v, modifiers, paramNames, paramNumber));
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Unexpected character in parameter specification");
                    }
                }
                else if (c == '(' || c == ' ' || c == ',' || c == '=' || c == '>' || c == '<' || c == '+' || c == '-' || c == '*' || c == '/')
                {
                    sb.Append(c);
                    if (i < query.Length - 1)
                    {
                        c = query[i + 1];
                        if (c >= '0' && c <= '9' && !UseSafeLiterals)
                        {
                            int    v        = 0;
                            double f        = 0;
                            double dp       = 0;
                            bool   isDouble = false;
                            do
                            {
                                if (c != '.')
                                {
                                    if (!isDouble)
                                    {
                                        v = v * 10 + c - '0';
                                    }
                                    else
                                    {
                                        f  = f + dp * (c - '0');
                                        dp = dp * 0.1;
                                    }
                                }
                                else
                                {
                                    isDouble = true;
                                    f        = v;
                                    dp       = 0.1;
                                }
                                i++;
                                if (i < query.Length - 1)
                                {
                                    c = query[i + 1];
                                }
                            } while (((c >= '0' && c <= '9') || c == '.') && (i < query.Length - 1));
                            if (!isDouble)
                            {
                                string paramName = AddParameterFromValue(command, v, null);
                                sb.Append(paramName);
                            }
                            else
                            {
                                string paramName = AddParameterFromValue(command, f, null);
                                sb.Append(paramName);
                            }
                        }
                    }
                }
                else
                {
                    sb.Append(c);
                }
            }
            command.CommandText += sb.ToString();
        }
コード例 #12
0
        protected virtual bool SetDbTypeFromValue(IDbDataParameter parameter, object value, SoqlLiteralValueModifiers modifiers)
        {
            DbType dbType;

            if (!paramTypes.TryGetValue(value.GetType(), out dbType))
            {
                return(false);
            }
            parameter.DbType = dbType;
            return(true);
        }
コード例 #13
0
 public SoqlLiteralExpression(object val, SoqlLiteralValueModifiers modifiers)
 {
     this.LiteralValue = val;
     this.Modifiers    = modifiers;
 }
コード例 #14
0
 protected override string AddNumberedParameter(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers, StringCollection paramNames, int paramNumber)
 {
     return AddParameterFromValue(command, v, modifiers);
 }
コード例 #15
0
ファイル: SoqlParser.cs プロジェクト: valery-shinkevich/sooda
        private SoqlLiteralValueModifiers ParseLiteralValueModifiers()
        {
            SoqlLiteralValueModifiers retVal = new SoqlLiteralValueModifiers();

            string typeName = tokenizer.EatKeyword();
            FieldDataType typeOverride = (FieldDataType)FieldDataType.Parse(typeof(FieldDataType), typeName);
            retVal.DataTypeOverride = typeOverride;
            return retVal;
        }
コード例 #16
0
 protected override string AddParameterFromValue(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers)
 {
     string paramName = base.AddParameterFromValue(command, v, modifiers);
     OracleParameter param = (OracleParameter)command.Parameters[paramName];
     if (param.DbType == DbType.String && v.ToString().Length > 2000)
         param.OracleType = OracleType.NClob;
     return paramName;
 }
コード例 #17
0
        protected override string AddParameterFromValue(IDbCommand command, object v, SoqlLiteralValueModifiers modifiers)
        {
            string          paramName = base.AddParameterFromValue(command, v, modifiers);
            OracleParameter param     = (OracleParameter)command.Parameters[paramName];

            if (param.DbType == DbType.String && v.ToString().Length > 2000)
            {
                param.OracleType = OracleType.NClob;
            }
            return(paramName);
        }
コード例 #18
0
 public void OutputLiteral(object literalValue, SoqlLiteralValueModifiers modifier)
 {
     if (literalValue is String)
     {
         Output.Write('\'');
         Output.Write(((string)literalValue).Replace("'", "''"));
         Output.Write('\'');
         if (modifier != null && modifier.DataTypeOverride == FieldDataType.AnsiString)
         {
             Output.Write('A');
         }
     }
     else if (literalValue is DateTime)
     {
         Output.Write('\'');
         Output.Write(((DateTime)literalValue).ToString("yyyyMMddHH:mm:ss"));
         Output.Write("'D");
     }
     else if (literalValue == null)
     {
         Output.Write("null");
     }
     else
     {
         // this is to output the decimal point as dot and not comma under Polish locale
         IFormattable formattable = literalValue as IFormattable;
         if (formattable != null)
             Output.Write(formattable.ToString(null, System.Globalization.CultureInfo.InvariantCulture));
         else
             Output.Write(literalValue);
     }
 }