示例#1
0
        public string GetDbType(IColumnSchema columnSchema, IColumnTypeSchema columnTypeSchema, IPrimaryKeySchema primaryKeySchema)
        {
            ArgumentUtility.CheckNotNull("columnSchema", columnSchema);
            ArgumentUtility.CheckNotNull("columnTypeSchema", columnTypeSchema);

            return(this._provider.GetDbType(columnSchema, columnTypeSchema, primaryKeySchema));
        }
示例#2
0
        public virtual string GetDbType(IColumnSchema columnSchema, IColumnTypeSchema columnTypeSchema, IPrimaryKeySchema primaryKeySchema)
        {
            var dbType = columnTypeSchema.CreateFormat;

            if (columnTypeSchema.CreateFormat.Contains("{0}"))
            {
                if (columnTypeSchema.CreateFormat.Contains("{1}"))
                {
                    dbType = string.Format(columnTypeSchema.CreateFormat, this.GetPrecision(columnSchema), columnSchema.Scale.Value);
                }
                else
                {
                    if (columnSchema.Precision.HasValue)
                    {
                        dbType = string.Format(columnTypeSchema.CreateFormat, this.GetPrecision(columnSchema));
                    }
                    else
                    {
                        dbType = string.Format(columnTypeSchema.CreateFormat, columnSchema.MaxLength.Value);
                    }
                }
            }

            return(this.GetDbType(dbType, columnSchema, primaryKeySchema));
        }
示例#3
0
        protected override string GetSingleParameterFormat(IColumnSchema columnSchema, IColumnTypeSchema columnTypeSchema)
        {
            if (columnTypeSchema.ColumnType == "TIMESTAMP")
            {
                return(string.Format(columnTypeSchema.CreateFormat, columnSchema.Scale.Value));
            }

            return(base.GetSingleParameterFormat(columnSchema, columnTypeSchema));
        }
示例#4
0
        public override string GetDbType(IColumnSchema columnSchema, IColumnTypeSchema columnTypeSchema, IPrimaryKeySchema primaryKeySchema)
        {
            if (columnTypeSchema.Type == typeof(string) && !columnSchema.MaxLength.HasValue)
            {
                return(this.GetDbType("M", columnSchema, primaryKeySchema));
            }

            return(base.GetDbType(columnSchema, columnTypeSchema, primaryKeySchema));
        }
示例#5
0
        private string GetNotNull(IColumnSchema columnSchema)
        {
            if (!columnSchema.Nullable)
            {
                return(" NOT NULL");
            }

            return(string.Empty);
        }
示例#6
0
        protected override object GetPrecision(IColumnSchema columnSchema)
        {
            if (!columnSchema.Precision.HasValue)
            {
                return("*");
            }

            return(base.GetPrecision(columnSchema));
        }
示例#7
0
 protected virtual string GetSingleParameterFormat(IColumnSchema columnSchema, IColumnTypeSchema columnTypeSchema)
 {
     if (columnSchema.Precision.HasValue)
     {
         return(string.Format(columnTypeSchema.CreateFormat, this.GetPrecision(columnSchema)));
     }
     else
     {
         return(string.Format(columnTypeSchema.CreateFormat, columnSchema.MaxLength.Value));
     }
 }
示例#8
0
        public Column(IColumnSchema schema)
        {
            ArgumentUtility.CheckNotNull("schema", schema);

            this.ColumnName   = schema.ColumnName;
            this.PropertyName = schema.ColumnName.ToSafeClrName(schema.ColumnName.ShouldForceProperCase());
            this.MaxLength    = schema.MaxLength;
            this.Precision    = schema.Precision;
            this.Scale        = schema.Scale;
            this.Nullable     = schema.Nullable;
            this.DefaultValue = schema.DefaultValue;
        }
示例#9
0
        private IColumnTypeSchema GetColumnType(IColumnSchema columnSchema)
        {
            var columnType = this._columnTypes.FirstOrDefault(x => x.ColumnType.Equals(columnSchema.ColumnType, StringComparison.InvariantCultureIgnoreCase));

            if (columnType == null)
            {
                var index = columnSchema.ColumnType.IndexOf("(");

                if (index >= 0)
                {
                    var columnTypeName = columnSchema.ColumnType.Substring(0, index);
                    columnType = this._columnTypes.FirstOrDefault(x => x.ColumnType.Equals(columnTypeName, StringComparison.InvariantCultureIgnoreCase));
                }
            }

            return(columnType);
        }
示例#10
0
 private static PropertyColumn CreatePropertyColumn(IColumnSchema column)
 {
     var col = column as ColumnSchema;
     var pc = new PropertyColumn();
     pc.ColumnName = col.Name;
     pc.Length = col.Length;
     pc.PropertyName = col.Name; // Inflector.Pascalize(col.Name.Replace(" ", ""));
     pc.ArgName = Inflector.Camelize(col.Name.Replace(" ", ""));
     pc.SqlDbType = TypeTranslator.ToSqlDbType(col.SqlTypeName).ToString();
     pc.IsNullable = col.Nullable;
     pc.IsString = col.DataType == typeof(string);
     pc.CSharpAlias = TypeTranslator.ToCSharpAlias(col.SqlTypeName);
     pc.IsAutoIncrement = col.IsIdentity;
     pc.RequiresLengthDefinition = col.Length > 0
         && !(new[] { "image", "text", "ntext" }.Contains(col.SqlTypeName, StringComparer.OrdinalIgnoreCase));
     return pc;
 }
示例#11
0
 protected virtual object GetPrecision(IColumnSchema columnSchema)
 {
     return(columnSchema.Precision.Value);
 }
示例#12
0
 protected string GetDbType(string dbType, IColumnSchema columnSchema, IPrimaryKeySchema primaryKeySchema)
 {
     return(dbType + this.GetPkIdentifier(primaryKeySchema) + this.GetNotNull(columnSchema));
 }
示例#13
0
 public void AddColumn(IColumnSchema column)
 {
     columns.Add(column);
 }
示例#14
0
 public bool IsUnsupportedDbType(IColumnSchema column)
 {
     return column.NativeType.Equals("geography", System.StringComparison.OrdinalIgnoreCase)
            || column.NativeType.Equals("geometry", System.StringComparison.OrdinalIgnoreCase)
            || column.NativeType.Equals("hierarchyid", System.StringComparison.OrdinalIgnoreCase);
 }
示例#15
0
 public void AddColumn(IColumnSchema column)
 {
     columns.Add(column);
 }
示例#16
0
 public bool IsUnsupportedDbType(IColumnSchema column)
 {
     return(column.NativeType.Equals("geography", System.StringComparison.OrdinalIgnoreCase) ||
            column.NativeType.Equals("geometry", System.StringComparison.OrdinalIgnoreCase) ||
            column.NativeType.Equals("hierarchyid", System.StringComparison.OrdinalIgnoreCase));
 }
示例#17
0
        public override IDatabaseSchema Load(DbConfiguration cfg)
        {
            var       databaseSchema = new DatabaseSchema();
            DataTable allColumns     = null;
            DataTable pkTable        = null;
            DataTable fkTable        = null;
            DataTable viewTable      = null;

            using (var conn = new OleDbConnection(cfg.ConnectionString))
            {
                conn.Open();
                allColumns = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null);
                pkTable    = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, null);
                fkTable    = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, null);
                viewTable  = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Views, null);
            }

            Dictionary <string, TableSchema> tables = new Dictionary <string, TableSchema>();

            foreach (var item in allColumns.Rows.Cast <DataRow>())
            {
                var tableName    = item["TABLE_NAME"] as string;
                var tableSchema  = item["TABLE_SCHEMA"] as string;
                var tableCatalog = item["TABLE_CATALOG"] as string;

                var c = new ColumnSchema();
                c.ColumnName = item["COLUMN_NAME"] as string;
                c.Order      = Convert.ToInt32(item["ORDINAL_POSITION"]);
                if (string.IsNullOrEmpty(item["NUMERIC_PRECISION"] as string))
                {
                    c.Precision = 0;
                }
                else
                {
                    c.Precision = (int)item["NUMERIC_PRECISION"];
                }
                if (string.IsNullOrEmpty(item["NUMERIC_SCALE"] as string))
                {
                    c.Scale = 0;
                }
                else
                {
                    c.Scale = (int)(item["NUMERIC_SCALE"]);
                }
                if (string.IsNullOrEmpty(item["CHARACTER_MAXIMUM_LENGTH"] as string))
                {
                    c.Length = 0;
                }
                else
                {
                    c.Length = (int)item["CHARACTER_MAXIMUM_LENGTH"];
                }
                c.DefaultValue = item["COLUMN_DEFAULT"] as string;
                c.Comment      = item["DESCRIPTION"] as string;
                c.DbType       = ParseDbType((int)item["DATA_TYPE"]);
                c.Type         = ParseType(c.DbType);
                foreach (var p in pkTable.Rows.Cast <DataRow>())
                {
                    if (tableName == p["TABLE_NAME"] as string && c.ColumnName == p["COLUMN_NAME"] as string)
                    {
                        c.IsPrimaryKey = true;
                        break;
                    }
                }

                if (!item.IsNull("IS_NULLABLE"))
                {
                    c.IsNullable = (bool)item["IS_NULLABLE"];
                }

                TableSchema table = null;
                string      key   = string.Concat(tableCatalog, tableSchema, tableName);

                if (!tables.TryGetValue(key, out table))
                {
                    table = new TableSchema {
                        TableName = tableName, Schema = tableSchema
                    };
                    tables[tableName] = table;
                }

                table.AddColumn(c);
            }

            foreach (var item in fkTable.Rows.Cast <DataRow>())
            {
                var tableSchema  = item["PK_TABLE_SCHEMA"] as string;
                var tableCatalog = item["PK_TABLE_CATALOG"] as string;

                TableSchema   thisTable  = null;
                TableSchema   otherTable = null;
                IColumnSchema thisKey    = null;
                IColumnSchema otherKey   = null;
                var           key        = string.Concat(tableCatalog, tableSchema, item["FK_TABLE_NAME"] as string);
                if (tables.TryGetValue(key, out thisTable))
                {
                    thisKey = thisTable.AllColumns.FirstOrDefault(p => p.ColumnName == item["FK_COLUMN_NAME"] as string);
                }

                key = string.Concat(tableCatalog, tableSchema, item["PK_TABLE_NAME"] as string);
                if (tables.TryGetValue(key, out otherTable))
                {
                    otherKey = otherTable.AllColumns.FirstOrDefault(p => p.ColumnName == item["PK_COLUMN_NAME"] as string);
                }

                thisTable.AddFK(new ForeignKeySchema
                {
                    ThisTable  = thisTable,
                    Name       = item["FK_NAME"] as string,
                    ThisKey    = thisKey,
                    OtherTable = otherTable,
                    OtherKey   = otherKey
                });
            }

            foreach (var item in viewTable.Rows.Cast <DataRow>())
            {
                var         tableName    = item["TABLE_NAME"] as string;
                var         tableSchema  = item["TABLE_SCHEMA"] as string;
                var         tableCatalog = item["TABLE_CATALOG"] as string;
                TableSchema table        = null;
                var         key          = string.Concat(tableCatalog, tableSchema, tableName);
                if (tables.TryGetValue(key, out table))
                {
                    table.IsView = true;
                }
            }

            databaseSchema.Tables = tables.Values.Where(p => !p.IsView).ToArray();
            databaseSchema.Views  = tables.Values.Where(p => p.IsView).ToArray();

            //allColumns.WriteXmlSchema(Console.Out);
            //pkTable.WriteXmlSchema(Console.Out);
            //fkTable.WriteXmlSchema(Console.Out);
            //viewTable.WriteXmlSchema(Console.Out);
            return(databaseSchema);
        }
示例#18
0
        public override IDatabaseSchema Load(DbConfiguration cfg)
        {
            var databaseSchema = new DatabaseSchema();

            ColumnInfo[] allColumns = null;

            ForeignKeyInfo[] allFks = null;
            using (var ctx = cfg.CreateDbContext())
            {
                InitConnection(ctx.Connection);
                using (var reader = ctx.DbHelper.ExecuteReader(AllColumnsSql))
                    allColumns = reader.ToList <ColumnInfo>().ToArray();

                using (var reader = ctx.DbHelper.ExecuteReader(AllFKsSql))
                    allFks = reader.ToList <ForeignKeyInfo>().ToArray();
            }

            Dictionary <string, TableSchema> tables = new Dictionary <string, TableSchema>();

            foreach (var c in allColumns)
            {
                TableSchema table = null;
                if (!tables.TryGetValue(c.TableName, out table))
                {
                    table = new TableSchema {
                        TableName = c.TableName, IsView = c.IsView
                    };
                    tables[c.TableName] = table;
                }

                var key = allFks.FirstOrDefault(p => p.Type == "P" &&
                                                p.ThisTableName == c.TableName &&
                                                p.ThisKey == c.ColumnName);
                c.IsPrimaryKey = key != null;



                var column = ToColumn(c);
                table.AddColumn(column);
            }

            foreach (var item in allFks.Where(p => p.OtherTableName.HasValue()))
            {
                TableSchema   thisTable  = tables[item.OtherTableName];
                TableSchema   otherTable = tables[item.ThisTableName];
                IColumnSchema thisKey    = thisTable.AllColumns.FirstOrDefault(p => p.ColumnName == item.OtherKey);
                IColumnSchema otherKey   = otherTable.AllColumns.FirstOrDefault(p => p.ColumnName == item.ThisKey);

                thisTable.AddFK(new ForeignKeySchema
                {
                    ThisTable  = thisTable,
                    Name       = item.Name,
                    ThisKey    = thisKey,
                    OtherTable = otherTable,
                    OtherKey   = otherKey
                });
            }

            databaseSchema.Tables = tables.Values.Where(p => !p.IsView).ToArray();
            databaseSchema.Views  = tables.Values.Where(p => p.IsView).ToArray();

            return(databaseSchema);
        }
示例#19
0
        public virtual IDatabaseSchema Load(DbConfiguration cfg)
        {
            var databaseSchema = new DatabaseSchema();

            ColumnInfo[]     allColumns     = null;
            ConstraintInfo[] allConstraints = null;
            ForeignKeyInfo[] allFks         = null;
            using (var ctx = cfg.CreateDbContext())
            {
                InitConnection(ctx.Connection);
                using (var reader = ctx.DbHelper.ExecuteReader(AllColumnsSql))
                    allColumns = reader.ToList <ColumnInfo>().ToArray();
                using (var reader = ctx.DbHelper.ExecuteReader(AllConstraintsSql))
                    allConstraints = reader.ToList <ConstraintInfo>().ToArray();
                using (var reader = ctx.DbHelper.ExecuteReader(AllFKsSql))
                    allFks = reader.ToList <ForeignKeyInfo>().ToArray();
            }

            PopulateConstraints(allColumns, allConstraints);

            Dictionary <string, TableSchema> tables = new Dictionary <string, TableSchema>();

            PopulateTables(allColumns, tables);


            foreach (var item in allFks)
            {
                TableSchema   thisTable  = null;
                TableSchema   otherTable = null;
                IColumnSchema thisKey    = null;
                IColumnSchema otherKey   = null;

                var key = string.Format("{0}.{1}", item.ThisSchema, item.ThisTableName);
                if (tables.TryGetValue(key, out thisTable))
                {
                    thisKey = thisTable.AllColumns.FirstOrDefault(p => p.ColumnName == item.ThisKey);
                }

                key = string.Format("{0}.{1}", item.OtherSchema, item.OtherTableName);
                if (tables.TryGetValue(key, out otherTable))
                {
                    otherKey = otherTable.AllColumns.FirstOrDefault(p => p.ColumnName == item.OtherKey);
                }

                thisTable.AddFK(new ForeignKeySchema
                {
                    ThisTable = thisTable
                    ,
                    Name = item.Name
                    ,
                    ThisKey = thisKey
                    ,
                    OtherTable = otherTable
                    ,
                    OtherKey = otherKey
                });
            }

            databaseSchema.Tables = tables.Values.Where(p => !p.IsView).ToArray();
            databaseSchema.Views  = tables.Values.Where(p => p.IsView).ToArray();

            return(databaseSchema);
        }