Exemple #1
0
        public void CreateClass()
        {
            ClassTableName ctname = new ClassTableName(tableName)
            {
                Option = Option
            };

            ClassName cname = new ClassName(Option.NameSpace, Option.Modifier, ctname);

            ITable schema = new TableSchema(tableName);

            var dpoClass = new DpoClass(schema, cname, Option);

            var sourceCode = dpoClass.Generate(cname.Modifier, ctname);

            string fileName = string.Format("{0}\\{1}.cs", Option.OutputPath, cname.Class);

            if (!Directory.Exists(Option.OutputPath))
            {
                Directory.CreateDirectory(Option.OutputPath);
            }

            StreamWriter sw = new StreamWriter(fileName);
            sw.Write(sourceCode);
            sw.Close();
        }
Exemple #2
0
        /// <summary>
        /// use default locator to save records into database, primary keys must be defined
        /// </summary>
        /// <param name="tableName"></param>
        public TableWriter(TableName tableName)
        {
            this.schema = tableName.GetTableSchema();

            IPrimaryKeys primary = schema.PrimaryKeys;
            if (primary.Length != 0)
                this.locator = new Locator(primary);
        }
Exemple #3
0
        public TableCompare(TableSchema schema1, TableSchema schema2)
        {
            this.SideType = CompareSideType.compare;
            this.ExceptColumns = new string[] { };

            this.schema1 = schema1;
            this.schema2 = schema2;
        }
Exemple #4
0
        public string CompareTable(ActionType actiontype, CompareSideType sidetype, TableName tname1, TableName tname2, Dictionary<string, string[]> pk, string[] exceptColumns)
        {
            TableSchema schema1 = new TableSchema(tname1);
            TableSchema schema2 = new TableSchema(tname2);

            if (!Exists(tname1))
                return string.Empty;

            string sql = string.Empty;

            if (actiontype == ActionType.CompareSchema)
            {
                sql = Compare.TableSchemaDifference(sidetype, tname1, tname2);
                stdio.WriteLine("completed to {0} table schema {1} => {2}", sidetype, tname1, tname2);
            }
            else if (actiontype == ActionType.CompareData)
            {
                if (!Exists(tname2))
                {
                    return string.Empty;
                }

                if (Compare.TableSchemaDifference(sidetype, tname1, tname2) != string.Empty)
                {
                    stdio.WriteLine("failed to {0} becuase of different table schemas", sidetype);
                    return string.Empty;
                }

                bool hasPk = schema1.PrimaryKeys.Length > 0;
                sql = Compare.TableDifference(sidetype, schema1, schema2, schema1.PrimaryKeys.Keys, exceptColumns);

                if (!hasPk)
                {
                    stdio.WriteLine("warning: no primary key found : {0}", tname1);

                    string key = tname1.Name.ToUpper();
                    if (pk.ContainsKey(key))
                    {
                        stdio.WriteLine("use predefine keys defined in ini file: {0}", tname1);
                        sql = Compare.TableDifference(sidetype, schema1, schema2, pk[key], exceptColumns);
                    }
                    else
                    {
                        stdio.WriteLine("use entire row as primary keys:{0}", tname1);
                        var keys = schema1.Columns.Select(row => row.ColumnName).ToArray();
                        sql = Compare.TableDifference(sidetype, schema1, schema2, keys, exceptColumns);
                    }
                }

                stdio.WriteLine("completed to {0} table data {1} => {2}", sidetype, tname1, tname2);
            }

            if (sql != string.Empty && sidetype == CompareSideType.compare)
                stdio.WriteLine(sql);

            return sql;
        }
Exemple #5
0
        protected override void CreateClass()
        {
            TableSchema schema = new TableSchema(tname);
            Func<IColumn, string> COLUMN = column => "_" + column.ColumnName.ToUpper();

            var clss = new Class(cname, OptionalBaseType()) { modifier = Modifier.Public | Modifier.Partial };
            builder.AddClass(clss);

            //Const Field
            Field field;
            foreach (var column in schema.Columns)
            {
                field = new Field(new TypeInfo { type = typeof(string) }, COLUMN(column), column.ColumnName)
                {
                    modifier = Modifier.Public | Modifier.Const
                };
                clss.Add(field);
            }

            clss.AddCopyCloneEqualsFunc(cname, schema.Columns.Select(column => column.ColumnName));
        }
Exemple #6
0
 /// <summary>
 /// use user defined locator to save records into database
 /// </summary>
 /// <param name="tableName"></param>
 /// <param name="locator"></param>
 public TableWriter(TableName tableName, Locator locator)
 {
     this.schema = tableName.GetTableSchema();
     this.locator = locator;
 }
Exemple #7
0
 public TableClause(TableSchema schema)
 {
     this.schema = schema;
     this.tableName = schema.TableName;
 }
Exemple #8
0
 public string CREATE_TABLE()
 {
     TableSchema schema1 = new TableSchema(tableName);
     string format = TableClause.GenerateCREATE_TABLE(schema1);
     string script = string.Format(format, tableName.FormalName);
     return script;
 }
Exemple #9
0
        private string LikeExpr(string wildcard, string[] columns)
        {
            wildcard = wildcard.Replace("*", "%").Replace("?", "_");

            if (columns.Length == 0)
            {
                var schema = new TableSchema(tname);
                List<string> L = new List<string>();
                foreach (var c in schema.Columns)
                {
                    if (c.CType == CType.NVarChar || c.CType == CType.NChar || c.CType == CType.NText)
                        L.Add(c.ColumnName);
                }
                columns = L.ToArray();
            }

            string where = "";
            foreach (string column in columns)
            {
                if (where != "")
                    where += " OR ";
                where += string.Format("[{0}] LIKE '{1}'", column, wildcard);
            }

            return where;
        }
Exemple #10
0
        public void attrib(Command cmd)
        {
            if (cmd.HasHelp)
            {
                stdio.WriteLine("command attrib: update column property");
                stdio.WriteLine("add primary key, foreign key or identity key");
                stdio.WriteLine("columns:");
                stdio.WriteLine("  attrib [table] +c:col1=varchar(2)+null : add column or alter column");
                stdio.WriteLine("  attrib [table] +c:col1=varchar(10)     : add column or alter column");
                stdio.WriteLine("  attrib [table] -c:col1                 : remove column");
                stdio.WriteLine("primary keys:");
                stdio.WriteLine("  attrib [table] +p:col1,col2            : add primary key");
                stdio.WriteLine("  attrib [table] +p:col1,col2            : remove primary key");
                stdio.WriteLine("foreign keys:");
                stdio.WriteLine("  attrib [table] +f:col1=table2.col2     : add foreign key");
                stdio.WriteLine("  attrib [table] -f:col1                 : remove foreign key");
                stdio.WriteLine("identiy key:");
                stdio.WriteLine("  attrib [table] +i:col1                 : add identity");
                stdio.WriteLine("  attrib [table] -i:col1                 : remove identity");
                return;
            }

            if (!Navigate(cmd.Path1))
                return;

            if (!(pt.Item is TableName))
            {
                stdio.ErrorFormat("table is not selected");
                return;
            }

            if (cmd.options.Has("+c"))
            {
                TableName tname = (TableName)pt.Item;
                string expr = cmd.options.GetValue("+c");
                string[] items = expr.Split(new string[] { "=", "+" }, StringSplitOptions.RemoveEmptyEntries);
                if (items.Length != 2 && items.Length != 3)
                {
                    stdio.ErrorFormat("invalid expression:{0}, correct is col1=type or col1=type+null", expr);
                    return;
                }
                string column = items[0];
                string type = items[1];
                string nullable = "NOT NULL";
                if (items.Length == 3 && items[2] == "null")
                    nullable = "NULL";

                string SQL;
                var schema = new TableSchema(tname);

                if (schema.Columns.Where(c => c.ColumnName.ToLower() == column.ToLower()).Count() != 0)
                    SQL = $"ALTER TABLE [{tname.Name}] ALTER COLUMN {column} {type} {nullable}";
                else
                    SQL = $"ALTER TABLE [{tname.Name}] ADD {column} {type} {nullable}";

                ExecuteNonQuery(tname.Provider, SQL);
                return;
            }

            if (cmd.options.Has("-c"))
            {
                TableName tname = (TableName)pt.Item;
                string column = cmd.options.GetValue("-c");
                string SQL = $"ALTER TABLE [{tname.Name}] DROP COLUMN {column}";
                ExecuteNonQuery(tname.Provider, SQL);
                return;
            }

            if (cmd.options.Has("+f"))
            {
                TableName fkName = (TableName)pt.Item;
                string expr = cmd.options.GetValue("+f");
                string[] items = expr.Split(new string[] { "=", "." }, StringSplitOptions.RemoveEmptyEntries);
                if (items.Length != 3)
                {
                    stdio.ErrorFormat("invalid foreign key expression:{0}, correct is col1->pktable.col2", expr);
                    return;
                }

                string fkColumn = items[0];
                string pkName = items[1];
                string pkColumn = items[2];

                //check fkColumn, pkColumn is valid

                string SQL = $"ALTER TABLE [{fkName.Name}] ADD CONSTRAINT [FK_{fkName.Name}_{pkName}] FOREIGN KEY([{fkColumn}]) REFERENCES [{pkName}]([{pkColumn}])";
                ExecuteNonQuery(fkName.Provider, SQL);
                return;
            }

            if (cmd.options.Has("+p"))
            {
                TableName tname = (TableName)pt.Item;
                string expr = cmd.options.GetValue("+p");
                string SQL = $"ALTER TABLE [{tname.Name}] ADD PRIMARY KEY(expr)";
                ExecuteNonQuery(tname.Provider, SQL);
                return;
            }

            if (cmd.options.Has("+i"))
            {
                TableName tname = (TableName)pt.Item;
                string column = cmd.options.GetValue("+i");
                string SQL = @"
            ALTER TABLE {0} ADD {1} INT IDENTITY(1, 1)
            ALTER TABLE {0} DROP COLUMN {2}
            sp_rename '{1}', '{2}', 'COLUMN'";
                string.Format(SQL, tname.Name, $"_{column}_", column);
                ExecuteNonQuery(tname.Provider, SQL);
                return;
            }
        }
Exemple #11
0
        private static void _DisplayColumnNodes(Command cmd, TableName tname)
        {
            TableSchema schema = new TableSchema(tname);
            stdio.WriteLine("TABLE: {0}", tname.Path);

            int i = 0;
            int count = 0;
            int h = 0;
            foreach (IColumn column in schema.Columns)
            {
                if (IsMatch(cmd.wildcard, column.ColumnName))
                {
                    count++;

                    List<string> L = new List<string>();
                    if (column.IsIdentity) L.Add("++");
                    if (column.IsPrimary) L.Add("pk");
                    if ((column as ColumnSchema).FkContraintName != null) L.Add("fk");
                    string keys = string.Join(",", L);

                    stdio.WriteLine("{0,5} {1,26} {2,-16} {3,10} {4,10}",
                       sub(++i),
                       string.Format("[{0}]", column.ColumnName),
                       ColumnSchema.GetSQLType(column),
                       keys,
                       column.Nullable ? "null" : "not null");

                    h = PagePause(cmd, ++h);
                }
            }

            stdio.WriteLine("\t{0} Column(s)", count);
        }