Beispiel #1
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));
        }
Beispiel #2
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));
        }
Beispiel #3
0
        internal static View ToView <T>(T data)
        {
            Type type = typeof(T);
            QueryTalkException exception;

            if (data != null && type == typeof(System.Object))
            {
                type = data.GetType();
            }

            if (data is ResultSet <DataTable> )
            {
                return(ViewConverter.ConvertDataTable(((ResultSet <DataTable>)(object) data).ToDataTable()));
            }

            if (type == typeof(Result <DataTable>) && data == null)
            {
                return(ViewConverter.ConvertDataTable(null));
            }

            if (type == typeof(DataTable))
            {
                return(ViewConverter.ConvertDataTable((DataTable)(object)data));
            }

            if (type.IsIResult(typeof(IResult)))
            {
                var    result = (IResult)(object)data;
                object table  = null;

                if (result != null)
                {
                    if (result.TableCount == 1)
                    {
                        table = data;
                    }
                    else
                    {
                        table = ((dynamic)data).Table1;
                    }
                }

                if (result == null || table == null)
                {
                    throw new QueryTalkException(
                              "ViewConverter.ToView<T>", QueryTalkExceptionType.EmptyDynamicResult,
                              String.Format("type = {0}", typeof(T)), Text.Method.ToView);
                }

                return(ViewConverter.ConvertCollection <T>((IEnumerable)table));
            }

            if (type == typeof(Value))
            {
                return(ViewConverter.ConvertValue((Value)(object)data));
            }

            Type clrType;

            if (QueryTalk.Wall.Mapping.CheckClrCompliance(type, out clrType, out exception)
                == QueryTalk.Wall.Mapping.ClrTypeMatch.ClrMatch)
            {
                return(ViewConverter.ConvertScalar <T>(data, clrType));
            }

            if (type.GetInterfaces().Where(i => i == typeof(IEnumerable)).Any())
            {
                return(ViewConverter.ConvertCollection <T>((IEnumerable)data));
            }

            if (type.IsClass)
            {
                return(ViewConverter.ConvertCollection <IEnumerable <T> >((IEnumerable)(new HashSet <T>(new[] { data }))));
            }

            throw new QueryTalkException("ViewConverter.ToView<T>", QueryTalkExceptionType.InvalidSerializationData,
                                         String.Format("type = {0}", typeof(T)), Text.Method.ToView);
        }
Beispiel #4
0
        private static SubResult ProcessPackage_InsertRows <T>(Assembly client,
                                                               IEnumerable <T> package, NodeMap map, ConnectBy connectBy, Nullable <bool> identityInsert)
            where T : DbRow
        {
            if (package.Count() == 0)
            {
                return(new SubResult());
            }

            PassChainer cpass;
            List <ParameterArgument> args = new List <ParameterArgument>();
            var view    = ViewConverter.ConvertDbRowData(package, false, true);
            var columns = map.GetInsertableColumns(true).Select(a => new Column(a.Name)).ToArray();

            if (map.HasIdentity)
            {
                var identityPK = map.TryGetIdentityPK();
                if (identityInsert == false)
                {
                    var delta = ((IRow)package.First()).RowID;
                    args.Add(view);
                    args.Add(delta);                                                // @Delta
                    args.Add(identityPK.Name.Part1);                                // @Identity
                    args.Add(new Column(Designer.IsNull(                            // ISNULL(MAX(@Identity), 0)
                                            Designer.Max(Designer.Identifier("1", identityPK.Name.Part1)), 0)));
                    args.Add(new Column(String.Format("[1].[{0}] - @Delta + @LastID + 1", Text.Reserved.QtRowIDColumnName).E()));
                    args.Add(map.Name);                                             // @Table
                    args.Add(columns);                                              // @Columns
                    args.Add(map.GetColumns(1));                                    // @AllColumns
                    args.Add(map.BuildSelfRelation(1, 2).E());                      // @On
                    cpass = _insertRowsIntoIdentityProc.Pass(args.ToArray());
                }
                else
                {
                    args.Add(ViewConverter.ConvertDbRowData(package, false, true)); // #Rows
                    args.Add(map.Name);                                             // @Table
                    args.Add(columns);                                              // @Columns
                    args.Add(map.GetColumns(1));                                    // @AllColumns
                    args.Add(map.BuildSelfRelation(1, 2).E());                      // @On
                    cpass = _identityInsertRowsProc.Pass(args.ToArray());
                }
            }
            // non-identity table
            else
            {
                // pass arguments
                args.Add(ViewConverter.ConvertDbRowData(package, false, true));     // #Rows
                args.Add(map.Name);                                                 // @Table
                args.Add(columns);                                                  // @Columns
                args.Add(map.GetColumns(1));                                        // @AllColumns
                args.Add(map.BuildSelfRelation(1, 2).E());                          // @On
                cpass = _insertRowsProc.Pass(args.ToArray());
            }

            // execute
            cpass.SetRootMap(map.ID);
            var connectable = Reader.GetConnectable(client, cpass, connectBy);
            var result      = connectable.Go <T>();
            var data        = result.ToList();

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

            foreach (var row in package)
            {
                PropertyAccessor.SetValues(row, PropertyAccessor.GetValues(data[j++]), computedIndexes);
                row.SetStatus(DbRowStatus.Loaded);
            }

            // all rows inserted successfully or none
            return(new SubResult(true, result.RowCount));
        }