コード例 #1
0
ファイル: TableSchema.cs プロジェクト: AyreYang/Workflow
        internal void FillSaveScript(SQLScriptCollection collection)
        {
            switch (this.ColumnsStatus)
            {
            case STATUS.ASSIGNED:
            case STATUS.RAW:
                var script1 = collection.NewSQLScript(null, SQLTYPE.INSERT);
                var insert  = new InsertItem <TableEntity>(collection.accessor, Entity);
                Columns.ToList().ForEach(col =>
                {
                    var param = collection.NewParameter(col.Name, col.Value);
                    insert.Append(new Clause(script1.accessor, param.CoveredName, new KeyValuePair <string, object>[] { param.ToKeyValuePair() }));
                });
                script1.AddItem(insert);
                break;

            case STATUS.CHANGED:
                var script2 = collection.NewSQLScript(null, SQLTYPE.UPDATE);
                var update  = new UpdateItem <TableEntity>(collection.accessor, Entity);
                Columns.Where(c => c.Status == STATUS.CHANGED).ToList().ForEach(col =>
                {
                    var param = collection.NewParameter(col.Name, col.Value);
                    update.Append(new Clause(script2.accessor, string.Format("{0} = {1}", col.Name, param.CoveredName), new KeyValuePair <string, object>[] { param.ToKeyValuePair() }));
                });
                script2.AddItem(update);
                var where = new WhereItem(script2.accessor, update);
                Columns.Where(c => c.IsPK).ToList().ForEach(col =>
                {
                    var param = collection.NewParameter(col.Name, col.Value);
                    where.And(where.ColumnEquals(col.Name, param));
                });
                script2.AddItem(where);
                break;
            }

            foreach (var foreign in this.Foreigns)
            {
                foreign.Save(collection);
            }
        }
コード例 #2
0
ファイル: TableSchema.cs プロジェクト: AyreYang/Workflow
        internal void FillDeleteScript(SQLScriptCollection collection)
        {
            var script = collection.NewSQLScript(null, SQLTYPE.DELETE);
            var delete = new DeleteItem <TableEntity>(collection.accessor, Entity);

            script.AddItem(delete);
            var where = new WhereItem(script.accessor, delete);
            Columns.Where(c => c.IsPK).ToList().ForEach(col =>
            {
                var param = collection.NewParameter(col.Name, col.Value);
                where.And(where.ColumnEquals(col.Name, param));
            });
            script.AddItem(where);

            if (this.HasForeigns)
            {
                foreach (var foreign in this.Foreigns)
                {
                    foreign.Delete(collection);
                }
            }
        }
コード例 #3
0
ファイル: TableSchema.cs プロジェクト: AyreYang/Workflow
        private void FillSelectScript(string name, int level, SQLScriptCollection collection, WhereItem where = null, Stack <JoinItem <TableEntity> > stack = null)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }

            var script = collection.NewSQLScript(level == 0 ? 0.ToString() : string.Format("{0}-{1}", name.Trim(), level), SQLTYPE.SELECT);
            var alias  = collection.NewTableAlias();
            var from   = new FromItem <TableEntity>(collection.accessor, Entity, alias);
            var select = new SelectItem(collection.accessor, from);

            script.AddItems(select, from);

            //Build Jions
            JoinItem <TableEntity>[] join_items = null;
            if (stack != null)
            {
                if (HasForeigns)
                {
                    join_items = new JoinItem <TableEntity> [stack.Count];
                    stack.CopyTo(join_items, 0);
                }

                TableItem table1 = from;
                JoinItem <TableEntity> table2 = null;
                while (stack.Count > 0)
                {
                    table2 = stack.Pop().Clone() as JoinItem <TableEntity>;
                    table2.ON(table1, table2.Keys.ToArray());
                    table2.Entity.AddJoinFilter(table2, collection.SQLParamCreater);
                    script.AddItems(table2);
                    table1 = table2;
                }
            }

            //Build Where
            if (level == 0)
            {
                var where1 = new WhereItem(collection.accessor, from);
                Columns.Where(c => c.IsPK).ToList().ForEach(col =>
                {
                    var param = collection.NewParameter(col.Value);
                    where1.And(where1.ColumnEquals(col.Name, param));
                });
                where = where == null ? where1 : where + where1;
            }
            if (where != null)
            {
                script.AddItem(where.Clone() as WhereItem);
            }
            var where0 = new WhereItem(collection.accessor, from);

            Entity.AddWhereFilter(where0, collection.SQLParamCreater);
            script.AddItem(where0);

            foreach (var foreign in this.Foreigns)
            {
                var nstack = join_items != null ? new Stack <JoinItem <TableEntity> >(join_items) : new Stack <JoinItem <TableEntity> >();
                nstack.Push(new JoinItem <TableEntity>(collection.accessor, Entity, alias, foreign.Keys.Select(key => key.ToKeyValuePair()).ToArray()));
                foreign.EntityInst.Schema.FillSelectScript(foreign.Name, level + 1, collection, where.Clone() as WhereItem, nstack);
            }
        }