Exemplo n.º 1
0
        internal virtual bool SetResultException(QueryTalkException ex)
        {
            if (AsyncStatus != AsyncStatus.Canceled)
            {
                _exception = ex;
                if (Result == null)
                {
                    Result = new Result(Connectable, ex);
                }
                else
                {
                    Result.Exception = ex;
                }
                return(true);
            }
            else
            {
                if (Result == null)
                {
                    Result = new Result(Connectable);
                }

                Result.AsyncStatus = AsyncStatus.Canceled;
            }
            return(false);
        }
Exemplo n.º 2
0
        internal ByArgument(System.String arg)
            : base(arg)
        {
            if (String.IsNullOrEmpty(arg))
            {
                arg = null;
            }

            if (CheckNull(Arg(() => arg, arg)))
            {
                if (arg.Split(new char[] { Text.DotChar }).Length > 2)
                {
                    chainException = new QueryTalkException(this,
                                                            QueryTalkExceptionType.InvalidColumnIdentifier,
                                                            String.Format("identifier = {0}", arg),
                                                            Text.Method.Identifier);
                    return;
                }

                var pair = new AliasColumnPair(arg);
                _alias  = pair.Alias;
                _column = pair.Column;

                if (_column != null)
                {
                    JoinType = Wall.ByType.NonMappedColumn;
                }
                else
                {
                    JoinType = Wall.ByType.Alias;
                }
            }

            SetArgType(arg);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Represents a column in the select statement.
        /// </summary>
        /// <param name="arg">Is an aliased column.</param>
        public Column(ColumnAsChainer arg)
            : this(arg as Chainer)
        {
            CheckNullAndThrow(Arg(() => arg, arg));

            SetArgType(arg);

            if (arg.Exception == null)
            {
                if (arg.Prev is Identifier)
                {
                    // check if more than 2-parts
                    if ((arg.Prev as Identifier).NumberOfParts > 2)
                    {
                        chainException = new QueryTalkException(this, QueryTalkExceptionType.InvalidColumnIdentifier,
                                                                String.Format("identifier = {0} AS {1}",
                                                                              arg.Prev.ToString(),
                                                                              Filter.Delimit(arg.Name ?? "")));
                    }
                }
            }
            else
            {
                TryTakeException(arg.Exception);
                if (arg.IsUndefined)
                {
                    chainException.Extra = Text.Free.AliasNullExtra;
                }
            }

            if (arg != null)
            {
                _columnName = ((IColumnName)arg).ColumnName;
            }
        }
Exemplo n.º 4
0
        internal ByArgument(Identifier arg)
            : base(arg)
        {
            CtorBody(arg);
            SetArgType(arg);

            if (chainException == null)
            {
                // identifier should consist of alias and column.
                if (arg.NumberOfParts != 2)
                {
                    chainException = new QueryTalkException(this,
                                                            QueryTalkExceptionType.InvalidByIdentifier,
                                                            String.Format("identifier = {0}", arg),
                                                            Text.Method.Identifier);
                    return;
                }

                _alias  = arg.Part1;
                _column = arg.Part2;

                if (_column != null)
                {
                    JoinType = Wall.ByType.NonMappedColumn;
                }
                else
                {
                    JoinType = Wall.ByType.Alias;
                }
            }
        }
Exemplo n.º 5
0
        internal static string BuildSequence <T>(IEnumerable <T> scalarCollection, out QueryTalkException exception)
        {
            exception = null;

            if (scalarCollection == null || scalarCollection.Count() == 0)
            {
                exception = new QueryTalkException("Expression.BuildSequence", QueryTalkExceptionType.CollectionNullOrEmpty,
                                                   String.Format("element type = {0}", typeof(T)), Text.Method.In);
                return(null);
            }

            Type type;
            QueryTalkException exclr;

            if (Mapping.CheckClrCompliance(typeof(T), out type, out exclr) != Mapping.ClrTypeMatch.ClrMatch)
            {
                throw exclr;
            }

            StringBuilder sql = Text.GenerateSql(200);
            int           i   = 0;

            foreach (T item in scalarCollection)
            {
                if (i++ > 0)
                {
                    sql.Append(Text.Comma);
                }

                sql.Append(Mapping.BuildUnchecked(item));
            }

            return(sql.ToString());
        }
Exemplo n.º 6
0
 // conditionally throws an exception
 internal static void TryThrow(this QueryTalkException exception, string method = null)
 {
     if (exception != null)
     {
         exception.Method = method ?? exception.Method;
         throw exception;
     }
 }
Exemplo n.º 7
0
        internal static string Concatenate(
            GroupingArgument[] arguments,
            BuildContext buildContext,
            BuildArgs buildArgs,
            bool isEnclosed)
        {
            // check
            if (arguments == null || arguments.Length == 0)
            {
                return("()");
            }

            return(String.Join(Text.CommaWithSpace,
                               arguments.Select(argument =>
            {
                if (argument.IsUndefined())
                {
                    var exception = new QueryTalkException(
                        "GroupingArgument.Concatenate",
                        QueryTalkExceptionType.ArgumentNull,
                        String.Format("column = undefined"));
                    buildContext.TryTakeException(exception);
                    return null;
                }

                if (argument.Exception != null)
                {
                    buildContext.TryTakeException(argument.Exception);
                    return null;
                }

                string sql = argument.Build(buildContext, buildArgs);

                if (argument.Exception != null)
                {
                    buildContext.TryTakeException(argument.Exception);
                    return null;
                }

                if (buildContext.Exception != null)
                {
                    return null;
                }

                buildContext.TryTake(argument);

                if (sql == "[]")
                {
                    return "()";
                }
                else
                {
                    return isEnclosed ?
                    String.Format("({0})", sql) : sql;
                }
            }).ToArray()));
        }
Exemplo n.º 8
0
 // conditionally assigns a "from" exception to the build context
 internal static bool TryTakeException(this BuildContext buildContext, QueryTalkException exception, string method = null)
 {
     // exception
     if (buildContext != null &&
         exception != null)
     {
         buildContext.Exception        = exception;
         buildContext.Exception.Method = method ?? buildContext.Exception.Method;
         return(true);
     }
     return(false);
 }
Exemplo n.º 9
0
        // attention:
        //   If root object is accessed through the BuildContext object, use buildContext.TryGetVariable method instead.
        internal Variable TryGetVariable(
            string variableName,
            out QueryTalkException exception,
            Variable.SearchType searchType = Variable.SearchType.Any)
        {
            exception = null;

            if (variableName == null)
            {
                exception = Variable.InvalidVariableException(variableName,
                                                              QueryTalkExceptionType.ArgumentNull);
                return(null);
            }

            Variable param;
            Variable variable;

            // try get param
            param = AllParams.Where(p => p.Name.EqualsCS(variableName))
                    .FirstOrDefault();

            // try get non-param
            variable = _variables.Where(v => v.Name.EqualsCS(variableName))
                       .FirstOrDefault();

            if (searchType == Variable.SearchType.Any)
            {
                return(param ?? variable);
            }

            if (searchType == Variable.SearchType.Inliner &&
                (param == null || param.DT.IsNotInliner()))
            {
                exception = Variable.InvalidVariableException(variableName,
                                                              QueryTalkExceptionType.InlinerNotFound);
                return(null);
            }

            // param/variable should exists
            if (searchType != Variable.SearchType.Any && (param == null && variable == null))
            {
                exception = Variable.InvalidVariableException(variableName,
                                                              QueryTalkExceptionType.ParamOrVariableNotDeclared);
                return(null);
            }

            return(param ?? variable);
        }
Exemplo n.º 10
0
        internal Expression(Operator op, ScalarArgument arg1, string collectionSql, QueryTalkException exception)
            : this(null, true)
        {
            if (exception != null)
            {
                chainException       = exception;
                chainException.Extra = Text.Free.ExpressionNullExtra;
                return;
            }

            arg1 = arg1 ?? Designer.Null;

            Arguments = new[] { arg1 };

            var list = collectionSql;

            CheckNull(Arg(() => list, list));

            TryTake(arg1);
            TryTakeException(exception);

            Build = (buildContext, buildArgs) =>
            {
                if (buildContext.TryTakeException(chainException))
                {
                    return(null);
                }

                if (!CheckConcatenator(buildContext, arg1))
                {
                    return(null);
                }

                arg1.RootSubject = ((ISemantic)this).RootSubject;

                var sql = BuildBinaryExpression(
                    op,
                    (arg1 as Chainer).Build(buildContext, buildArgs),
                    collectionSql);

                buildContext.TryTakeException(chainException);

                return(sql);
            };
        }
Exemplo n.º 11
0
        internal void SetClrException(
            System.Exception ex,
            string exceptionCreator,
            Connectable connectable,
            string method)
        {
            if (connectable.AsyncCanceled)
            {
                AsyncStatus        = AsyncStatus.Canceled;
                Result             = new Result(connectable);
                Result.AsyncStatus = AsyncStatus.Canceled;
                return;
            }

            QueryTalkException exception = new QueryTalkException(exceptionCreator, QueryTalkExceptionType.ClrException, ((IName)connectable).Name,
                                                                  method, null);

            exception.ClrException = ex;
            SetResultException(exception);
        }
Exemplo n.º 12
0
        internal static string Concatenate(
            OrderedColumnArgument[] arguments,
            BuildContext buildContext,
            BuildArgs buildArgs)
        {
            return(String.Join(Text.CommaWithSpace,
                               arguments.Select(argument =>
            {
                if (argument.IsUndefined())
                {
                    var exception = new QueryTalkException("OrderedColumnArgument",
                                                           QueryTalkExceptionType.ArgumentNull,
                                                           "column = undefined");
                    buildContext.TryTakeException(exception);
                    return null;
                }

                if (argument.Exception != null)
                {
                    buildContext.TryTakeException(argument.Exception);
                    return null;
                }

                var sql = argument.Build(buildContext, buildArgs);

                if (argument.Exception != null)
                {
                    buildContext.TryTakeException(argument.Exception);
                    return null;
                }

                if (buildContext.Exception != null)
                {
                    return null;
                }

                buildContext.TryTake(argument);

                return sql;
            }).ToArray()));
        }
Exemplo n.º 13
0
 internal Result(Connectable connectable, QueryTalkException ex)
     : this(connectable, (IEnumerable <dynamic>)null)
 {
     Exception = ex;
 }
Exemplo n.º 14
0
        // returns first cell of the first row
        /// <summary>
        /// Returns the value of the first cell of the resultset table.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        public TValue ToValue <TValue>()
        {
            PropertyInfo property = null;

            try
            {
                if (TableType == typeof(DataTable))
                {
                    if (DataTable.AsEnumerable().Count() > 0)
                    {
                        return((TValue)DataTable.Rows[0][0]);
                    }
                    else
                    {
                        throw new QueryTalkException("ResultSet.ToValue<TValue>", QueryTalkExceptionType.EmptyResultset,
                                                     null, Text.Method.ToValue, "data class = DataTable");
                    }
                }
                else
                {
                    if (Count > 0)
                    {
                        var properties = TableType.GetProperties();
                        if (properties.Length == 0)
                        {
                            throw new QueryTalkException("ResultSet.ToValue<TValue>", QueryTalkExceptionType.EmptyResultset,
                                                         null, Text.Method.ToValue, String.Format("data class = {0}", TableType));
                        }

                        property = properties[0];
                        if (property.GetGetMethod() == null)
                        {
                            throw new QueryTalkException("ResultSet.ToValue<TValue>", QueryTalkExceptionType.NonAccessableProperty,
                                                         null, Text.Method.ToValue, String.Format("data class = {0}{1}   property = {2}",
                                                                                                  TableType, Environment.NewLine, property.Name));
                        }

                        var value = property.GetValue(this.First(), null);
                        return((value != null) ? (TValue)value : default(TValue));
                    }
                    else
                    {
                        throw new QueryTalkException("ResultSet.ToValue<TValue>", QueryTalkExceptionType.EmptyResultset,
                                                     null, Text.Method.ToValue, String.Format("data class = {0}", TableType));
                    }
                }
            }
            catch (QueryTalkException)
            {
                throw;
            }
            catch (System.InvalidCastException)
            {
                throw new QueryTalkException("ResultSet.ToValue<TValue>", QueryTalkExceptionType.InvalidCast,
                                             null, Text.Method.ToValue, String.Format("returning type = {0}{1}   value type = {2}",
                                                                                      typeof(TValue), Environment.NewLine, property.PropertyType));
            }
            catch (System.Exception ex)
            {
                var exception = new QueryTalkException("ResultSet.ToValue<TValue>", QueryTalkExceptionType.ClrException,
                                                       null, Text.Method.ToValue, String.Format("returning type = {0}", typeof(TValue)));
                exception.ClrException = ex;
                throw exception;
            }
        }
Exemplo n.º 15
0
 private static void ThrowNotClrCompliantException(QueryTalkException exception, Type classType)
 {
     exception.Extra = String.Format("A property with a non-compliant type belongs to a class {0}. This class cannot be converted to the DataTable object.", classType);
     throw exception;
 }
Exemplo n.º 16
0
        // Builds argument with critical check if the argument is bound to a certain param.
        internal void BuildArgument(Designer root, string paramName)
        {
            // check if a param with the given param name exists
            Variable param = root.TryGetVariable(paramName, out chainException, Variable.SearchType.Param);

            if (chainException != null)
            {
                chainException.Method = Text.Method.Pass;
                return;
            }

            // check: argument is null/NULL
            if (QueryTalk.Value.IsNull(Value))
            {
                if (IsPassedVariable || !param.ParamNullCheck(out chainException))
                {
                    chainException.Method    = Text.Method.Pass;
                    chainException.Arguments = String.Format("param = {0}", paramName);
                    return;
                }
            }

            // store param data
            ParamName       = paramName;
            _dt             = param.DT;
            DataType        = param.DataType;
            IsParamOutput   = param.IsOutput;
            IsParameterized = param.IsParameterized;

            // clear parameterized value on param
            if (IsParameterized)
            {
                param.ClearParameterizedValue();
            }

            // udt: store compiled SQL
            if (param.DT.IsNameType())
            {
                NameTypeSql = param.NameType.Sql;
            }

            // table variable/temp table/bulk table: cannot be passed as variable
            if (DT.IsVTB())
            {
                // bulk table
                if (DT == Wall.DT.BulkTable)
                {
                    if (Value.GetType() != typeof(DataTable))
                    {
                        chainException = new QueryTalkException("Param.BuildArgument", QueryTalkExceptionType.InvalidTableArgument,
                                                                String.Format(
                                                                    "bulk table = {0}{1}   argument type = {2}{3}   required type = {4}",
                                                                    ParamName, Environment.NewLine, ArgType, Environment.NewLine, typeof(DataTable)),
                                                                Text.Method.Pass);
                        return;
                    }
                    else
                    {
                        return;
                    }
                }

                // table variable/temp table
                if (Value.GetType() != typeof(View))
                {
                    chainException = new QueryTalkException("Param.BuildArgument", QueryTalkExceptionType.InvalidTableArgument,
                                                            String.Format(
                                                                "param = {0}{1}   argument type = {2}{3}   required type = {4}",
                                                                ParamName, Environment.NewLine, ArgType, Environment.NewLine, typeof(View)),
                                                            Text.Method.Pass);
                    return;
                }
            }

            // finish if argument has been passed as variable
            if (IsPassedVariable)
            {
                Sql = Original.ToString();
                return;
            }

            // argument type check
            if (!CheckType(param, Value, out chainException))
            {
                chainException.Method = Text.Method.Pass;
                return;
            }

            // Here the check has passed. Argument is properly bound to its param.
            // Now build the SQL output.

            // value
            if (DT.IsDataType() || DT.IsNameType())
            {
                if (ArgType == typeof(View))
                {
                    Sql = ((View)Original).Sql;
                }
                else if (ArgType == typeof(DataTable))
                {
                    Sql = null;
                }
                else if (ArgType == typeof(OutputVar))
                {
                    Sql = ((OutputVar)Original).Value;
                }
                // common type
                else
                {
                    Sql = Mapping.BuildUnchecked(Value, out chainException);
                }
            }
            // inline
            else
            {
                // table
                if (DT == DT.InTable)
                {
                    if (IsPassedVariable)
                    {
                        Sql = (string)Value;
                    }
                    // Identifier (build by Build method)
                    else
                    {
                    }
                }
                // sql
                else if (DT == DT.InSql)
                {
                    if (IsPassedVariable)
                    {
                        Sql = (string)Value;
                    }
                    else
                    {
                        Sql = _string;
                    }
                }
                // other inline types must be built by the Build method
                else
                {
                }
            }
        }
Exemplo n.º 17
0
        // Check argument type and prepare exception object if the check fails.
        internal bool CheckType(Variable param, object value, out QueryTalkException exception)
        {
            exception = null;

            #region Table-value param

            if (DT.IsTable())
            {
                // check null: #temp table & bulk table are allowed to be passed a null value (in the inner call)
                // Note: table variable param cannot be used in inner calls => raise an exception
                if (QueryTalk.Value.IsNull(value))
                {
                    throw new QueryTalkException("Param.ArgumentCheck",
                                                 QueryTalkExceptionType.ParamArgumentNull,
                                                 String.Format("param = {0}", ParamName),
                                                 Text.Method.Pass);
                }

                // check type match
                if (!Mapping.TableParamMapping[DT].Contains(ArgType))
                {
                    exception = new QueryTalkException("Param.ArgumentCheck",
                                                       QueryTalkExceptionType.ParamArgumentTypeMismatch,
                                                       String.Format(
                                                           "param = {0}{1}   argument type = {2}{3}   required type(s) = {4}",
                                                           ParamName, Environment.NewLine,
                                                           ArgType, Environment.NewLine,
                                                           String.Join(", ", Mapping.TableParamMapping[DT]
                                                                       .Select(a => a.FullName).ToArray())),
                                                       Text.Method.Pass);
                    return(false);
                }

                // check View: should store data values only.
                if (ArgType == typeof(View))
                {
                    // if a view does not origin from a CLR collection
                    if (!((View)value).IsValidDataView)
                    {
                        exception = new QueryTalkException(
                            "Param.ArgumentCheck",
                            QueryTalkExceptionType.InvalidDataView,
                            String.Format("param = {0}", ParamName));
                        return(false);
                    }
                    return(true);
                }
            }

            #endregion

            // it's ok if value is null
            if (QueryTalk.Value.IsNull(value))
            {
                return(true);
            }

            // type check
            if (!Mapping.CheckArgument(param, ArgType, out exception))
            {
                if (DT.IsInliner())
                {
                    exception.Arguments = String.Format(
                        "param = {0}{1}   param type = {2}{3}   argument = {4}{5}   argument type = {6}{7}   required type(s) = {8}",
                        param.Name, Environment.NewLine,
                        Text.Free.InlinerType + "." + param.DT.ToInliner(), Environment.NewLine,
                        value.ToReport(), Environment.NewLine,
                        ArgType, Environment.NewLine,
                        String.Join(", ", Mapping.InlineMapping[param.DT]
                                    .Select(a => a.FullName).ToArray()));
                }
                else
                {
                    exception.Arguments = String.Format(
                        "param = {0}{1}   param type = {2}{3}   argument = {4}{5}   argument type = {6}{7}   required type = {8}",
                        param.Name, Environment.NewLine,
                        param.DT.ToString().ToLower(), Environment.NewLine,
                        value.ToReport(), Environment.NewLine,
                        ArgType, Environment.NewLine,
                        Mapping.SqlMapping[param.DT].ClrType
                        );
                }
                return(false);
            }
            return(true);
        }