Example #1
0
        /// <summary>
        /// Save into database
        /// </summary>
        /// <param name="entries"></param>
        public void SubmitChanges(List <ResourceEntry> entries, bool deleteRowNotInResource)
        {
            SqlMaker gen = new SqlMaker(tname.FormalName)
            {
                PrimaryKeys = new string[] { name_column }
            };

            StringBuilder builder = new StringBuilder();
            int           count   = 0;
            int           max     = 1000;

            foreach (var entry in entries)
            {
                gen.Clear();
                gen.Add(name_column, entry.Name);
                gen.Add(value_column, entry.NewValue);
                if (order_column != null)
                {
                    gen.Add(order_column, entry.Index);
                }

                switch (entry.Action)
                {
                case DataRowAction.Add:
                    builder.AppendLine(gen.Insert());
                    break;

                case DataRowAction.Change:
                    builder.AppendLine(gen.Update());
                    break;

                case DataRowAction.Delete:
                    if (deleteRowNotInResource)
                    {
                        builder.AppendLine(gen.Delete());
                    }
                    break;
                }

                count = (count + 1) % max;
                if (count == 0)
                {
                    ExecuteNonQuery();
                }
            }

            ExecuteNonQuery();

            void ExecuteNonQuery()
            {
                string SQL = builder.ToString();

                builder.Clear();
                if (!string.IsNullOrEmpty(SQL))
                {
                    SqlCmd cmd = new SqlCmd(tname.Provider, SQL);
                    cmd.ExecuteNonQuery();
                }
            }
        }
        public void Method_CRUD(DataTable dt, Class clss)
        {
            var       provider = ConnectionProviderManager.DefaultProvider;
            TableName tname    = new TableName(provider, dt.TableName);

            SqlMaker gen = new SqlMaker(tname.FormalName)
            {
                PrimaryKeys = dt.PrimaryKey.Select(x => x.ColumnName).ToArray()
            };

            foreach (DataColumn column in dt.Columns)
            {
                string cname = column.ColumnName;
                gen.Add(cname, "{" + cname + "}");
            }

            Method method = new Method("Insert")
            {
                Modifier = Modifier.Public,
                Type     = new TypeInfo(typeof(string)),
            };

            method.Statement.AppendLine("return $\"" + gen.Insert() + "\";");
            clss.Add(method);

            method = new Method("Update")
            {
                Modifier = Modifier.Public,
                Type     = new TypeInfo(typeof(string)),
            };
            method.Statement.AppendLine("return $\"" + gen.Update() + "\";");
            clss.Add(method);

            method = new Method("InsertOrUpdate")
            {
                Modifier = Modifier.Public,
                Type     = new TypeInfo(typeof(string)),
            };
            method.Statement.AppendLine("return $\"" + gen.InsertOrUpdate() + "\";");
            clss.Add(method);

            method = new Method("Delete")
            {
                Modifier = Modifier.Public,
                Type     = new TypeInfo(typeof(string)),
            };
            method.Statement.AppendLine("return $\"" + gen.Delete() + "\";");
            clss.Add(method);
        }
Example #3
0
        private void OperateOnSubmit(RowOperation operation, TEntity entity)
        {
            SqlMaker gen = this.Generator;

            var dict = broker.ToDictionary(entity);

            gen.AddRange(dict);

            string sql = null;

            switch (operation)
            {
            case RowOperation.Insert:
                sql = gen.Insert();
                break;

            case RowOperation.Update:
                sql = gen.Update();
                break;

            case RowOperation.InsertOrUpdate:
                sql = gen.InsertOrUpdate();
                break;

            case RowOperation.Delete:
                sql = gen.Delete();
                break;
            }

            if (sql == null)
            {
                return;
            }

            Context.CodeBlock.AppendLine <TEntity>(sql);

            var evt = new RowEvent
            {
                TypeName  = typeof(TEntity).Name,
                Operation = operation,
                Row       = gen.ToDictionary(),
            };

            Context.RowEvents.Add(evt);
            gen.Clear();
        }