/// <summary> /// 读取表空间信息 /// </summary> /// <param name="tableFilterExclude"></param> /// <param name="columnFilterExclude"></param> /// <param name="useCamelCase"></param> /// <param name="prependSchemaName"></param> /// <param name="includeComments"></param> /// <param name="includeExtendedPropertyComments"></param> /// <param name="tableRename"></param> /// <param name="schemaNameFilter"></param> /// <param name="updateColumn"></param> /// <returns></returns> 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); }
private static Column CreateColumn(IDataRecord rdr, Regex rxClean, Table table, bool useCamelCase, Regex columnFilterExclude, Func <Column, Table, Column> updateColumn) { if (rdr == null) { throw new ArgumentNullException("rdr"); } string typename = rdr["TypeName"].ToString().Trim().ToLower(); var scale = (int)rdr["Scale"]; var precision = (int)rdr["Precision"]; var col = new Column { Name = rdr["ColumnName"].ToString().Trim(), TypeName = typename, PropertyType = GetPropertyType(typename, scale, precision), MaxLength = (int)rdr["MaxLength"], Precision = precision, Default = rdr["Default"].ToString().Trim(), DateTimePrecision = (int)rdr["DateTimePrecision"], Scale = scale, Ordinal = (int)rdr["Ordinal"], IsIdentity = rdr["IsIdentity"].ToString().Trim().ToLower() == "true", IsNullable = rdr["IsNullable"].ToString().Trim().ToLower() == "true", IsStoreGenerated = rdr["IsStoreGenerated"].ToString().Trim().ToLower() == "true", IsPrimaryKey = rdr["PrimaryKey"].ToString().Trim().ToLower() == "true", PrimaryKeyOrdinal = (int)rdr["PrimaryKeyOrdinal"], IsForeignKey = rdr["IsForeignKey"].ToString().Trim().ToLower() == "true" }; if (columnFilterExclude != null && !col.IsPrimaryKey && columnFilterExclude.IsMatch(col.Name)) { col.Hidden = true; } col.IsFixedLength = (typename == "char" || typename == "nchar"); col.IsUnicode = !(typename == "char" || typename == "varchar" || typename == "text"); col.IsRowVersion = col.IsStoreGenerated && !col.IsNullable && typename == "timestamp"; if (col.IsRowVersion) { col.MaxLength = 8; } col.CleanUpDefault(); col.PropertyName = CodeFirstTools.CleanUp(col.Name); col.PropertyName = rxClean.Replace(col.PropertyName, "_$1"); // Make sure property name doesn't clash with class name if (col.PropertyName == table.NameHumanCase) { col.PropertyName = col.PropertyName + "_"; } col.PropertyNameHumanCase = (useCamelCase ? Inflector.ToTitleCase(col.PropertyName) : col.PropertyName).Replace(" ", ""); if (col.PropertyNameHumanCase == string.Empty) { col.PropertyNameHumanCase = col.PropertyName; } // Make sure property name doesn't clash with class name if (col.PropertyNameHumanCase == table.NameHumanCase) { col.PropertyNameHumanCase = col.PropertyNameHumanCase + "_"; } if (char.IsDigit(col.PropertyNameHumanCase[0])) { col.PropertyNameHumanCase = "_" + col.PropertyNameHumanCase; } if (CodeFirstTools.CheckNullable(col) != string.Empty) { table.HasNullableColumns = true; } col = updateColumn(col, table); // If PropertyType is empty, return null. Most likely ignoring a column due to legacy (such as OData not supporting spatial types) if (string.IsNullOrEmpty(col.PropertyType)) { return(null); } return(col); }
private static Column CreateColumn(IDataRecord rdr, Regex rxClean, Table table, bool useCamelCase, Regex columnFilterExclude, Func <Column, Table, Column> updateColumn) { try { if (rdr == null) { throw new ArgumentNullException("rdr"); } string typename = rdr["TYPENAME"].ToString().Trim().ToLower(); var scale = decimal.ToInt32(decimal.Parse(rdr["SCALE"].ToString())); var precision = decimal.ToInt32(decimal.Parse(rdr["PRECISION"].ToString())); if (typename.ToLower() == "number" && scale == 0 && precision == 0) { scale = int.MinValue; precision = 38; } var col = new Column { Name = rdr["COLUMNNAME"].ToString().Trim(), TypeName = typename, PropertyType = GetPropertyType(typename, ref scale, precision), MaxLength = decimal.ToInt32(decimal.Parse(rdr["MAXLENGTH"].ToString())), Precision = precision, Default = rdr["Default"].ToString().Trim(), DateTimePrecision = decimal.ToInt32(decimal.Parse(rdr["DATETIMEPRECISION"].ToString())), Scale = scale, Ordinal = decimal.ToInt32(decimal.Parse(rdr["ORDINAL"].ToString())), IsIdentity = rdr["ISIDENTITY"].ToString().Trim().ToLower() == "1", IsNullable = rdr["ISNULLABLE"].ToString().Trim().ToLower() == "1", IsStoreGenerated = rdr["ISSTOREGENERATED"].ToString().Trim().ToLower() == "1", IsPrimaryKey = rdr["PRIMARYKEY"].ToString().Trim().ToLower() == "1", PrimaryKeyOrdinal = decimal.ToInt32(decimal.Parse(rdr["PRIMARYKEYORDINAL"].ToString())), IsForeignKey = rdr["ISFOREIGNKEY"].ToString().Trim().ToLower() == "1" }; Assembly asm = Assembly.Load("EntityFramework"); Version v = new Version("5.0.0.0"); if (typename.ToLower() == "float" && asm.GetName().Version > v) { if (precision == 126) { col.Precision = 38; } else if (precision == 63) { col.Precision = 19; } } if (columnFilterExclude != null && !col.IsPrimaryKey && columnFilterExclude.IsMatch(col.Name)) { col.Hidden = true; } col.IsFixedLength = (typename.ToLower() == "char" || typename.ToLower() == "nchar"); col.IsUnicode = !(typename.ToLower() == "char" || typename.ToLower() == "varchar2" || typename.ToLower() == "clob" || typename.ToLower() == "long"); col.IsRowVersion = col.IsStoreGenerated && !col.IsNullable && typename == "timestamp"; if (col.IsRowVersion) { col.MaxLength = 8; } col.CleanUpDefault(); col.PropertyName = CodeFirstTools.CleanUp(col.Name); col.PropertyName = rxClean.Replace(col.PropertyName, "_$1"); // Make sure property name doesn't clash with class name if (col.PropertyName == table.NameHumanCase) { col.PropertyName = col.PropertyName + "_"; } col.PropertyNameHumanCase = (useCamelCase ? Inflector.ToTitleCase(col.PropertyName) : col.PropertyName).Replace(" ", ""); if (col.PropertyNameHumanCase == string.Empty) { col.PropertyNameHumanCase = col.PropertyName; } // Make sure property name doesn't clash with class name if (col.PropertyNameHumanCase == table.NameHumanCase) { col.PropertyNameHumanCase = col.PropertyNameHumanCase + "_"; } if (char.IsDigit(col.PropertyNameHumanCase[0])) { col.PropertyNameHumanCase = "_" + col.PropertyNameHumanCase; } if (CodeFirstTools.CheckNullable(col) != string.Empty) { table.HasNullableColumns = true; } col = updateColumn(col, table); // If PropertyType is empty, return null. Most likely ignoring a column due to legacy (such as OData not supporting spatial types) if (string.IsNullOrEmpty(col.PropertyType)) { return(null); } return(col); } catch (Exception ee) { throw ee; } }
/// <summary> /// /// </summary> /// <param name="spFilterExclude"></param> /// <param name="useCamelCase"></param> /// <param name="prependSchemaName"></param> /// <param name="StoredProcedureRename"></param> /// <param name="schemaNameFilter"></param> /// <returns></returns> public override List <StoredProcedure> ReadStoredProcs(Regex spFilterExclude, bool useCamelCase, bool prependSchemaName, Func <string, string, string> StoredProcedureRename, string schemaNameFilter) { var result = new List <StoredProcedure>(); if (Cmd == null) { return(result); } Cmd.CommandText = StoredProcedureSQL + IncludeQueryTraceOn9481(); if (Cmd.GetType().Name == "SqlCeCommand") { return(result); } Cmd.CommandTimeout = 600; using (DbDataReader rdr = Cmd.ExecuteReader()) { var lastSp = string.Empty; StoredProcedure sp = null; while (rdr.Read()) { string spName = rdr["SPECIFIC_NAME"].ToString().Trim(); if (spFilterExclude != null && spFilterExclude.IsMatch(spName)) { continue; } string schema = rdr["SPECIFIC_SCHEMA"].ToString().Trim(); if (schemaNameFilter != null && !schema.Equals(schemaNameFilter, StringComparison.CurrentCultureIgnoreCase)) { continue; } if (lastSp != spName || sp == null) { lastSp = spName; sp = new StoredProcedure { Name = spName, NameHumanCase = (useCamelCase ? Inflector.ToTitleCase(spName) : spName).Replace(" ", "").Replace("$", ""), Schema = schema }; if ((string.Compare(schema, "dbo", StringComparison.OrdinalIgnoreCase) != 0) && prependSchemaName) { sp.NameHumanCase = schema + "_" + sp.NameHumanCase; } sp.NameHumanCase = StoredProcedureRename(sp.NameHumanCase, schema); result.Add(sp); } string typename = rdr["DATA_TYPE"].ToString().Trim().ToLower(); var scale = (int)rdr["NUMERIC_SCALE"]; var precision = (int)((byte)rdr["NUMERIC_PRECISION"]); var parameterMode = rdr["PARAMETER_MODE"].ToString().Trim().ToUpper(); var param = new StoredProcedureParameter { Ordinal = (int)rdr["ORDINAL_POSITION"], Mode = (parameterMode == "IN") ? StoredProcedureParameterMode.In : StoredProcedureParameterMode.InOut, Name = rdr["PARAMETER_NAME"].ToString().Trim(), SqlDbType = GetSqlDbType(typename, scale, precision), PropertyType = GetPropertyType(typename, scale, precision), DateTimePrecision = (Int16)rdr["DATETIME_PRECISION"], MaxLength = (int)rdr["CHARACTER_MAXIMUM_LENGTH"], Precision = precision, Scale = scale }; var clean = CodeFirstTools.CleanUp(param.Name.Replace("@", "")); param.NameHumanCase = Inflector.MakeInitialLower((useCamelCase ? Inflector.ToTitleCase(clean) : clean).Replace(" ", "")); if (CodeFirstTools.ReservedKeywords.Contains(param.NameHumanCase)) { param.NameHumanCase = "@" + param.NameHumanCase; } sp.Parameters.Add(param); } } return(result); }