Пример #1
0
        public string Refine(SchemaRefineOption option)
        {
            string SQL = $"SELECT * FROM [{tname.Name}]";
            var    dt  = FillDataTable(SQL);

            StringBuilder builder = new StringBuilder();

            foreach (IColumn column in schema.Columns)
            {
                ColumnSchema cs      = (ColumnSchema)column;
                bool         isDirty = false;
                if (option.ChangeNotNull && column.Nullable && !HasNull(dt, column))
                {
                    cs.Nullable = false;
                    isDirty     = true;
                }

                if (option.ConvertInteger && CanConvertToInt32(dt, column))
                {
                    cs.DataType = "int";
                    isDirty     = true;
                }

                if (option.ConvertBoolean && CanConvertBoolean(dt, column))
                {
                    cs.DataType = "bit";
                    isDirty     = true;
                }

                if (option.ShrinkString && ShrinkString(dt, column, out short length))
                {
                    cs.Length = length;
                    isDirty   = true;
                }

                if (isDirty)
                {
                    string field = cs.GetSQLField();
                    SQL = $"ALTER TABLE [{tname.Name}] ALTER COLUMN {field}";

                    builder.AppendLine(SQL);
                }
            }

            return(builder.ToString());
        }