Esempio n. 1
0
        /// <summary>
        /// Executes the scalar function returning the value of a specified type.
        /// </summary>
        /// <typeparam name="T">The type of a value to return.</typeparam>
        /// <param name="client">A client assembly.</param>
        /// <param name="arguments">Are the arguments to pass.</param>
        protected T GoFunc <T>(System.Reflection.Assembly client, params ParameterArgument[] arguments)
        {
            return(PublicInvoker.Call <T>(() =>
            {
                var root = Mapper.GetRoot();

                DbMapping.CreateParams(root, this);

                var parameters = String.Join(",", root.AllParams.Select(p => p.Name).ToArray());

                // build
                string sql = Text.GenerateSql(100)
                             .NewLine(Text.Select).S()
                             .Append(Map.Name.Sql)
                             .EncloseLeft()
                             .Append(parameters)
                             .EncloseRight()
                             .Append(Text._As_)
                             .Append(Text.LeftSquareBracket)
                             .Append(Text.SingleColumnName)
                             .Append(Text.RightSquareBracket)
                             .Terminate()
                             .ToString();

                Mapper.SetSql(sql);

                var cpass = new PassChainer(Mapper, arguments);
                var connectable = Reader.GetConnectable(client, cpass);
                return Reader.LoadTable <Row <T> >(connectable, null, true).ToValue <T>();
            }));
        }
Esempio n. 2
0
        private void BodyMethod(IExecutable iexecutable, string returnVariable)
        {
            ReturnVariable = returnVariable;

            _innerObject = iexecutable as Chainer;
            if (iexecutable is Compilable)
            {
                Executable = new Executable((Compilable)iexecutable);
            }
            else if (iexecutable is DbProcedure)
            {
                var dbProc = (DbProcedure)iexecutable;
                dbProc.BuildProc(null);
                var cpass = new PassChainer(dbProc);
                Executable   = cpass.Executable;
                _innerObject = cpass;
            }
            // PassChainer
            else
            {
                Executable = ((Chainer)iexecutable).Executable;
            }

            Executable.Inner = true;    // important!
            Executable.ArgumentsCriticalCheck(false);
            TryThrow(Executable.Exception);
        }
Esempio n. 3
0
 /// <summary>
 /// Executes a stored procedure or SQL batch.
 /// </summary>
 /// <param name="procOrBatch">Is a stored procedure or SQL batch.</param>
 public Result ExecGo(ExecArgument procOrBatch)
 {
     return(PublicInvoker.Call <Result>(Assembly.GetCallingAssembly(), (ca) =>
     {
         var cpass = PassChainer.Create(_root, procOrBatch);
         var connectable = Reader.GetConnectable(ca, cpass, this);
         return Reader.LoadAll(connectable);
     }));
 }
Esempio n. 4
0
        internal static ExecChainer Create(
            Chainer prev,
            ExecArgument executable,
            string returnValueToVariable,
            ParameterArgument[] arguments)
        {
            if (executable.Exception != null)
            {
                executable.Exception.ObjectName = prev.GetRoot().Name;
                executable.Exception.Arguments  = "executable = null";
                executable.Exception.Method     = Text.Method.Exec;
                throw executable.Exception;
            }

            QueryTalkException exception;
            var      root    = prev.GetRoot();
            Variable inliner = null;

            if (executable.Original is System.String)
            {
                inliner = root.TryGetVariable((string)executable.Original,
                                              out exception, Variable.SearchType.Inliner);
            }
            exception = null;

            // stored procedure, SQL batch, mapped stored procedure
            if (inliner == null)
            {
                var cpass = PassChainer.Create(new InternalRoot(), executable);
                return(new ExecChainer((Chainer)prev, cpass, returnValueToVariable));
            }
            // inliner: stored procedure, procedure, SQL
            else
            {
                if (inliner.DT == DT.InProcedure)
                {
                    return(new ExecChainer(prev, inliner, returnValueToVariable, arguments));
                }
                else if (inliner.DT == DT.InStoredProcedure)
                {
                    return(new ExecChainer(prev, inliner, returnValueToVariable, arguments));
                }
                else if (inliner.DT == DT.InSql)
                {
                    return(new ExecChainer(prev, inliner, returnValueToVariable, arguments));
                }
                else
                {
                    exception = inliner.DT.InvalidInlinerException(typeof(ExecChainer).ToString(),
                                                                   inliner.Name, _inliners);
                    exception.ObjectName = root.Name;
                    exception.Method     = Text.Method.Exec;
                    throw exception;
                }
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Executes a stored procedure or SQL batch asynchronously.
 /// </summary>
 /// <param name="procOrBatch">Is a stored procedure or SQL batch.</param>
 /// <param name="onCompleted">A delegate method that is called when the asynchronous operation completes.</param>
 /// <returns>The object of the asynchronous operation.</returns>
 public Async ExecGoAsync(ExecArgument procOrBatch, Action <Result> onCompleted = null)
 {
     return(PublicInvoker.Call <Async>(Assembly.GetCallingAssembly(), (ca) =>
     {
         var cpass = PassChainer.Create(_root, procOrBatch);
         var connectable = Reader.GetConnectable(ca, cpass, this);
         connectable.OnAsyncCompleted = onCompleted;
         return Reader.LoadAllAsync(connectable);
     }));
 }
Esempio n. 6
0
 /// <summary>
 /// Executes the procedure returning a strongly typed result set.
 /// </summary>
 /// <param name="client">A client assembly.</param>
 /// <param name="arguments">Are the arguments to pass.</param>
 protected Result <T> Go <T>(Assembly client, params ParameterArgument[] arguments)
 {
     return(PublicInvoker.Call <Result <T> >(() =>
     {
         BuildProc(arguments);
         var cpass = new PassChainer(Mapper, arguments);
         var connectable = Reader.GetConnectable(client, this, cpass);
         return Reader.LoadTable <T>(connectable, null);
     }));
 }
Esempio n. 7
0
        private static SubResult ProcessPackage_UpdateRows <T>(
            Assembly client,
            IEnumerable <T> package,
            NodeMap map,
            ConnectBy connectBy,
            Nullable <bool> identityInsert)
            where T : DbRow
        {
            List <ParameterArgument> args = new List <ParameterArgument>();

            args.Add(ViewConverter.ConvertDbRowData(package, false, true)); // #Rows
            args.Add(map.Name);                                             // @Table
            args.Add(map.GetColumnsForUpdate(2));                           // @SourceColumns
            args.Add(map.GetColumnsForUpdate(null));                        // @TargetColumns
            args.Add(map.BuildSelfRelationWithOriginalValues(1, 2).E());    // @OnUpdate
            args.Add(map.BuildSelfRelation(1, 2).E());                      // @OnSelect
            args.Add(map.GetColumns(1, 2));                                 // @AllColumns (with RowID)

            PassChainer cpass = _updateRowsProc.Pass(args.ToArray());

            cpass.SetRootMap(map.ID);   // important!
            var connectable = Reader.GetConnectable(client, cpass, connectBy);
            var result      = connectable.Go();
            var data        = result.Table2.ToList();

            var computedIndexes    = map.SortedComputedColumns.Select(a => a.ID.ColumnZ).ToArray();
            var hasComputedColumns = computedIndexes.Length > 0;

            var i       = 0;
            var count   = result.RowCount;
            var updated = result.Table1.ToList();

            foreach (var row in package)
            {
                if (i >= count)
                {
                    break;
                }

                if (((IRow)row).RowID == updated[i].RowID)
                {
                    if (hasComputedColumns)
                    {
                        PropertyAccessor.SetValues(row, PropertyAccessor.GetValues(data[i], Text.Reserved.QtRowIDColumnName),
                                                   computedIndexes);
                    }

                    row.SetStatus(DbRowStatus.Loaded);

                    ++i;
                }
            }

            return(new SubResult(true, result.ReturnValue));
        }
Esempio n. 8
0
        /// <summary>
        /// Executes a stored procedure or SQL batch asynchronously.
        /// </summary>
        /// <typeparam name="T">The type of the result set that is returned by the execution.</typeparam>
        /// <param name="procOrBatch">Is a stored procedure or SQL batch.</param>
        /// <param name="onCompleted">A delegate method that is called when the asynchronous operation completes.</param>
        /// <returns>The object of the asynchronous operation.</returns>
        public Async <Result <T> > ExecGoAsync <T>(ExecArgument procOrBatch, Action <Result <T> > onCompleted = null)

        {
            return(PublicInvoker.Call <Async <Result <T> > >(Assembly.GetCallingAssembly(), (ca) =>
            {
                var cpass = PassChainer.Create(_root, procOrBatch);
                var connectable = Reader.GetConnectable(ca, cpass, this);
                connectable.OnAsyncCompleted = onCompleted;

                if (typeof(T) == typeof(DataTable))
                {
                    return Reader.LoadDataTableAsync <T>(connectable);
                }
                else
                {
                    return Reader.LoadTableAsync <T>(connectable, null);
                }
            }));
        }
Esempio n. 9
0
        private static SubResult ProcessPackage_DeleteRows <T>(
            Assembly client,
            IEnumerable <T> package,
            NodeMap map,
            ConnectBy connectBy,
            Nullable <bool> identityInsert)
            where T : DbRow
        {
            List <ParameterArgument> args = new List <ParameterArgument>();

            args.Add(ViewConverter.ConvertDbRowData(package, false, true));     // #Rows
            args.Add(map.Name);                                                 // @Table
            args.Add(map.BuildSelfRelationWithOriginalValues(1, 2).E());        // @On

            PassChainer cpass = _deleteRowsProc.Pass(args.ToArray());

            cpass.SetRootMap(map.ID);   // important!
            var connectable = Reader.GetConnectable(client, cpass, connectBy);
            var result      = connectable.Go();

            var i       = 0;
            var count   = result.RowCount;
            var deleted = result.ToList();

            foreach (var row in package)
            {
                if (i >= count)
                {
                    break;
                }
                if (((IRow)row).RowID == deleted[i].RowID)
                {
                    row.SetStatus(DbRowStatus.Deleted);
                    ++i;
                }
            }

            return(new SubResult(true, result.ReturnValue));
        }
Esempio n. 10
0
        /// <summary>
        /// Executes a stored procedure or SQL batch.
        /// </summary>
        /// <typeparam name="T">The type of the result set that is returned by the execution.</typeparam>
        /// <param name="procOrBatch">Is a stored procedure or SQL batch.</param>
        public Result <T> ExecGo <T>(ExecArgument procOrBatch)

        {
            return(PublicInvoker.Call <Result <T> >(Assembly.GetCallingAssembly(), (ca) =>
            {
                var cpass = PassChainer.Create(_root, procOrBatch);
                var connectable = Reader.GetConnectable(ca, cpass, this);

                Result <T> result;

                if (typeof(T) == typeof(DataTable))
                {
                    result = Reader.LoadDataTable <T>(connectable);
                }
                else
                {
                    result = Reader.LoadTable <T>(connectable, null);
                }

                return result;
            }));
        }
Esempio n. 11
0
        // inliner: procedure, snippet, stored procedure
        private ExecChainer(
            Chainer prev,
            Variable inliner,
            string returnValueToVariable,
            params ParameterArgument[] arguments) : base(prev)
        {
            Build = (buildContext, buildArgs) =>
            {
                ParameterArgument inlinerArgument = buildArgs.Executable.GetInlinerArgument(inliner.Name);
                if (inlinerArgument.Value == null)
                {
                    buildContext.TryTakeException(new QueryTalkException(this,
                                                                         QueryTalkExceptionType.InlinerArgumentNull,
                                                                         String.Format("{0} = null", inliner.Name)));
                    return(null);
                }

                if (inliner.DT == DT.InProcedure)
                {
                    if (inlinerArgument.DT != DT.InProcedure)
                    {
                        buildContext.TryTakeException(inliner.DT.InvalidInlinerException(GetType().Name,
                                                                                         inliner.Name, _procInliners));
                        return(null);
                    }

                    PassChainer cpass;
                    if (inlinerArgument.ArgType == typeof(PassChainer))
                    {
                        cpass = (PassChainer)inlinerArgument.Original;
                    }
                    // inliner variable
                    else
                    {
                        var proc = (Procedure)inlinerArgument.Original;
                        cpass = new PassChainer(proc, arguments);
                    }

                    BodyMethod(cpass, returnValueToVariable);

                    return(BuildExecProc(buildContext, buildArgs));
                }
                else if (inliner.DT == DT.InStoredProcedure)
                {
                    if (inlinerArgument.DT != DT.InStoredProcedure)
                    {
                        buildContext.TryTakeException(inliner.DT.InvalidInlinerException(GetType().Name,
                                                                                         inliner.Name, _sprocInliners));
                        return(null);
                    }

                    PassChainer cpass;
                    if (inlinerArgument.ArgType == typeof(PassChainer))
                    {
                        cpass = (PassChainer)inlinerArgument.Original;
                    }
                    else if (inlinerArgument.ArgType == typeof(ExecArgument))
                    {
                        cpass = PassChainer.Create(new InternalRoot(), (ExecArgument)inlinerArgument.Original);
                    }
                    // inliner variable
                    else
                    {
                        var execArgument = ((string)inlinerArgument.Original).Pass(arguments);
                        cpass = PassChainer.Create(new InternalRoot(), execArgument);
                    }

                    BodyMethod(cpass, returnValueToVariable);

                    return(BuildExecProc(buildContext, buildArgs));
                }
                else
                {
                    if (inlinerArgument.DT != DT.InSql)
                    {
                        buildContext.TryTakeException(inliner.DT.InvalidInlinerException(GetType().Name,
                                                                                         inliner.Name, _sqlInliners));
                        return(null);
                    }

                    PassChainer cpass;
                    if (inlinerArgument.ArgType == typeof(ExecArgument))
                    {
                        cpass = PassChainer.Create(new InternalRoot(), (ExecArgument)inlinerArgument.Original);
                    }
                    // inliner variable
                    else
                    {
                        var execArgument = ((string)inlinerArgument.Original).Pass(arguments);
                        cpass = PassChainer.Create(new InternalRoot(), execArgument);
                    }

                    BodyMethod(cpass, null);

                    return(BuildExecProc(buildContext, buildArgs));
                }
            };
        }
Esempio n. 12
0
 /// <summary>
 /// Sets the argument's type.
 /// </summary>
 /// <param name="arg">Is an argument.</param>
 protected internal void SetArgType(PassChainer arg)
 {
     ArgType = typeof(PassChainer);
     SetDebugValue(arg, DebugSetter.Arg);
 }