public override void ReadExtendedProperties(Tables tables) { if (Cmd == null) { return; } Cmd.CommandText = ExtendedPropertySQL + IncludeQueryTraceOn9481(); if (Cmd.GetType().Name == "SqlCeCommand") { Cmd.CommandText = ExtendedPropertyTableExistsSQLCE; var obj = Cmd.ExecuteScalar(); if (obj == null) { return; } Cmd.CommandText = ExtendedPropertySQLCE; } using (DbDataReader rdr = Cmd.ExecuteReader()) { Table t = null; while (rdr.Read()) { string schema = rdr["schema"].ToString().Trim(); string tableName = rdr["table"].ToString().Trim(); string column = rdr["column"].ToString().Trim(); string extendedProperty = rdr["property"].ToString().Trim(); if (extendedProperty == string.Empty) { continue; } if (t == null || t.Name != tableName || t.Schema != schema) { t = tables.Find(x => x.Name == tableName && x.Schema == schema); } if (t != null) { var col = t.Columns.Find(x => x.Name == column); if (col != null) { extendedProperty = extendedProperty.Replace("\n", " ").Replace("\r", " "); col.ExtendedProperty = extendedProperty; } } } } }
public override void ReadExtendedProperties(Tables tables) { if (Cmd == null) { return; } Cmd.CommandText = ExtendedPropertySQL; using (DbDataReader rdr = Cmd.ExecuteReader()) { Table t = null; while (rdr.Read()) { string schema = rdr["SCHEMA"].ToString().Trim(); string tableName = rdr["TABLE"].ToString().Trim(); string column = rdr["COLUMN"].ToString().Trim(); string extendedProperty = rdr["PROPERTY"].ToString().Trim(); if (extendedProperty == string.Empty) { continue; } if (t == null || t.Name != tableName || t.Schema != schema) { t = tables.Find(x => x.Name == tableName && x.Schema == schema); } if (t != null) { var col = t.Columns.Find(x => x.Name == column); if (col != null) { extendedProperty = extendedProperty.Replace("\n", " ").Replace("\r", " "); col.ExtendedProperty = extendedProperty; } } } } }
public override Tables ReadSchema(Regex tableFilterExclude, Regex columnFilterExclude, bool useCamelCase, bool prependSchemaName, bool includeComments, ExtendedPropertyCommentsStyle includeExtendedPropertyComments, Func <string, string, string> tableRename, string schemaNameFilter, Func <Column, Table, Column> updateColumn) { var result = new Tables(); if (Cmd == null) { return(result); } Cmd.CommandText = TableSQL; if (Cmd.GetType().Name == "SqlCeCommand") { Cmd.CommandText = string.Empty; } else { Cmd.CommandTimeout = 600; } using (DbDataReader rdr = Cmd.ExecuteReader()) { var rxClean = new Regex("^(event|Equals|GetHashCode|GetType|ToString|repo|Save|IsNew|Insert|Update|Delete|Exists|SingleOrDefault|Single|First|FirstOrDefault|Fetch|Page|Query)$"); var lastTable = string.Empty; Table table = null; while (rdr.Read()) { string tableName = rdr["TABLENAME"].ToString().Trim(); if (tableFilterExclude != null && tableFilterExclude.IsMatch(tableName)) { continue; } string schema = rdr["SCHEMANAME"].ToString().Trim(); if (schemaNameFilter != null && !schema.Equals(schemaNameFilter, StringComparison.CurrentCultureIgnoreCase)) { continue; } if (lastTable != tableName || table == null) { // The data from the database is not sorted table = result.Find(x => x.Name == tableName && x.Schema == schema); if (table == null) { table = new Table { Name = tableName, Schema = schema, IsView = String.Compare(rdr["TABLETYPE"].ToString().Trim(), "View", StringComparison.OrdinalIgnoreCase) == 0, // Will be set later HasForeignKey = false, HasNullableColumns = false }; tableName = tableRename(tableName, schema); CodeFirstTools.SchemaName = schema; table.CleanName = CodeFirstTools.CleanUp(tableName); table.ClassName = Inflector.MakeSingular(table.CleanName); string singular = Inflector.MakeSingular(tableName); table.NameHumanCase = (useCamelCase ? Inflector.ToTitleCase(singular) : singular).Replace(" ", "").Replace("$", ""); //if ((string.Compare(table.Schema, "dbo", StringComparison.OrdinalIgnoreCase) != 0) && prependSchemaName) // table.NameHumanCase = table.Schema + "_" + table.NameHumanCase; // Check for table or C# name clashes if (CodeFirstTools.ReservedKeywords.Contains(table.NameHumanCase) || (useCamelCase && result.Find(x => x.NameHumanCase == table.NameHumanCase) != null)) { table.NameHumanCase += "1"; } result.Add(table); } } var col = CreateColumn(rdr, rxClean, table, useCamelCase, columnFilterExclude, updateColumn); if (col != null) { table.Columns.Add(col); } } } // Check for property name clashes in columns foreach (Column c in result.SelectMany(tbl => tbl.Columns.Where(c => tbl.Columns.FindAll(x => x.PropertyNameHumanCase == c.PropertyNameHumanCase).Count > 1))) { c.PropertyNameHumanCase = c.PropertyName; } if (includeExtendedPropertyComments != ExtendedPropertyCommentsStyle.None) { ReadExtendedProperties(result); } ReadUniqueIndexes(result); foreach (Table tbl in result) { tbl.Columns.ForEach(x => x.SetupEntityAndConfig(includeComments, includeExtendedPropertyComments)); } return(result); }
// When a table has no primary keys, all the NOT NULL columns are set as being the primary key. // This function reads the unique indexes for a table, and correctly sets the columns being used as primary keys. public override void ReadUniqueIndexes(Tables tables) { if (Cmd == null) { return; } if (Cmd.GetType().Name == "SqlCeCommand") { return; } Cmd.CommandText = UniqueIndexSQL; var list = new List <UniqueIndex>(); using (DbDataReader rdr = Cmd.ExecuteReader()) { while (rdr.Read()) { var uniqueIndex = new UniqueIndex(); uniqueIndex.Schema = rdr["TABLESCHEMA"].ToString().Trim(); uniqueIndex.TableName = rdr["TABLENAME"].ToString().Trim(); uniqueIndex.IndexName = rdr["INDEXNAME"].ToString().Trim(); uniqueIndex.KeyOrdinal = decimal.ToByte(decimal.Parse(rdr["KEYORDINAL"].ToString())); uniqueIndex.Column = rdr["COLUMNNAME"].ToString().Trim(); uniqueIndex.ColumnCount = decimal.ToInt32(decimal.Parse(rdr["COLUMNCOUNT"].ToString())); list.Add(uniqueIndex); } } Table t = null; var indexes = list .Select(x => new { x.Schema, x.TableName, x.IndexName }) .Distinct() .OrderBy(o => o.Schema) .ThenBy(o => o.TableName) .ThenBy(o => o.IndexName); foreach (var index in indexes) { if (t == null || t.Name != index.TableName || t.Schema != index.Schema) { t = tables.Find(x => x.Name == index.TableName && x.Schema == index.Schema); } if (t != null && !t.PrimaryKeys.Any()) { // Table has no primary keys var uniqueIndexKeys = list.Where(x => x.Schema == index.Schema && x.TableName == index.TableName && x.IndexName == index.IndexName) .Select(x => new { x.IndexName, x.KeyOrdinal, x.Column, x.ColumnCount }) .OrderBy(o => o.ColumnCount) .ThenBy(o => o.IndexName); // Process only the first index with the lowest unique column count string indexName = null; foreach (var key in uniqueIndexKeys) { if (indexName == null) { indexName = key.IndexName; } if (indexName != key.IndexName) { break; // First unique index with lowest column count has been processed, exit. } var col = t.Columns.Find(x => x.Name == key.Column); if (col != null && !col.IsNullable && !col.Hidden) { col.IsPrimaryKey = true; col.IsPrimaryKeyViaUniqueIndex = true; col.UniqueIndexName = indexName; } } } } }