protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names) { var constraints = ReadConstraints(conn, schemaName.DbName); //sort tables - parents first (this is moving to SchemaPostprocess) //TableSorter.Sort(tables, constraints); foreach (DataConstraint keyColRow in constraints) { //find my table: string fullKeyDbName = GetFullDbName(keyColRow.TableName, keyColRow.TableSchema); DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => fullKeyDbName == t.Name); if (table == null) { bool ignoreCase = true; table = schema.Tables.FirstOrDefault(t => 0 == string.Compare(fullKeyDbName, t.Name, ignoreCase)); if (table == null) { WriteErrorLine("ERROR L46: Table '" + keyColRow.TableName + "' not found for column " + keyColRow.ColumnName); continue; } } bool isForeignKey = keyColRow.ConstraintName != "PRIMARY" && keyColRow.ReferencedTableName != null; if (isForeignKey) { LoadForeignKey(schema, table, keyColRow.ColumnName, keyColRow.TableName, keyColRow.TableSchema, keyColRow.ReferencedColumnName, keyColRow.ReferencedTableName, keyColRow.ReferencedTableSchema, keyColRow.ConstraintName, nameFormat, names); } } }
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names) { //TableSorter.Sort(tables, constraints); //sort tables - parents first var constraints = ReadConstraints(conn, schemaName.DbName); var allKeys2 = ReadForeignConstraints(conn, schemaName.DbName); var foreignKeys = allKeys2.Where(k => k.ConstraintType == "FOREIGN KEY").ToList(); var primaryKeys = allKeys2.Where(k => k.ConstraintType == "PRIMARY KEY").ToList(); foreach (DataConstraint keyColRow in constraints) { //find my table: string constraintFullDbName = GetFullDbName(keyColRow.TableName, keyColRow.TableSchema); DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => constraintFullDbName == t.Name); if (table == null) { WriteErrorLine("ERROR L138: Table '" + keyColRow.TableName + "' not found for column " + keyColRow.ColumnName); continue; } //todo: must understand better how PKEYs are encoded. //In Sasha's DB, they don't end with "_pkey", you need to rely on ReadForeignConstraints(). //In Northwind, they do end with "_pkey". bool isPrimaryKey = keyColRow.ConstraintName.EndsWith("_pkey") || primaryKeys.Count(k => k.ConstraintName == keyColRow.ConstraintName) == 1; if (isPrimaryKey) { //A) add primary key DbLinq.Schema.Dbml.Column primaryKeyCol = table.Type.Columns.First(c => c.Name == keyColRow.ColumnName); primaryKeyCol.IsPrimaryKey = true; } else { DataForeignConstraint dataForeignConstraint = foreignKeys.FirstOrDefault(f => f.ConstraintName == keyColRow.ConstraintName); if (dataForeignConstraint == null) { string msg = "Missing data from 'constraint_column_usage' for foreign key " + keyColRow.ConstraintName; WriteErrorLine(msg); //throw new ApplicationException(msg); continue; //as per Andrus, do not throw. //putting together an Adnrus_DB test case. } LoadForeignKey(schema, table, keyColRow.ColumnName, keyColRow.TableName, keyColRow.TableSchema, dataForeignConstraint.ColumnName, dataForeignConstraint.ReferencedTableName, dataForeignConstraint.ReferencedTableSchema, keyColRow.ConstraintName, nameFormat, names); } } }
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names) { var constraints = ReadConstraints(conn, schemaName.DbName); foreach (DataConstraint constraint in constraints) { //find my table: string constraintFullDbName = GetFullDbName(constraint.TableName, constraint.TableSchema); DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => constraintFullDbName == t.Name); if (table == null) { WriteErrorLine("ERROR L100: Table '" + constraint.TableName + "' not found for column " + constraint.ColumnNameList); continue; } //if (table.Name.StartsWith("E")) // Logger.Write("---Dbg"); if (constraint.ConstraintType == "P") { //A) add primary key DbLinq.Schema.Dbml.Column pkColumn = table.Type.Columns.Where(c => constraint.ColumnNames.Contains(c.Name)).First(); pkColumn.IsPrimaryKey = true; } else if (constraint.ConstraintType == "R") { //if not PRIMARY, it's a foreign key. (constraint_type=="R") //both parent and child table get an [Association] DataConstraint referencedConstraint = constraints.FirstOrDefault(c => c.ConstraintName == constraint.ReverseConstraintName); if (constraint.ReverseConstraintName == null || referencedConstraint == null) { WriteErrorLine("ERROR L127: given R_contraint_name='" + constraint.ReverseConstraintName + "', unable to find parent constraint"); continue; } LoadForeignKey(schema, table, constraint.ColumnNameList, constraint.TableName, constraint.TableSchema, referencedConstraint.ColumnNameList, referencedConstraint.TableName, referencedConstraint.TableSchema, constraint.ConstraintName, nameFormat, names); } // custom type, this is a trigger else if (constraint.ConstraintType == "T" && constraint.ColumnNames.Count == 1) { var column = table.Type.Columns.Where(c => c.Name == constraint.ColumnNames[0]).First(); column.Expression = constraint.Expression; column.IsDbGenerated = true; } } //GuessSequencePopulatedFields(schema); }
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names) { //TableSorter.Sort(tables, constraints); //sort tables - parents first var foreignKeys = ReadConstraints(conn, schemaName.DbName); foreach (DataConstraint keyColRow in foreignKeys) { //find my table: string constraintFullDbName = GetFullDbName(keyColRow.TableName, keyColRow.TableSchema); DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => constraintFullDbName == t.Name); if (table == null) { WriteErrorLine("ERROR L138: Table '" + keyColRow.TableName + "' not found for column " + keyColRow.ColumnName); continue; } if (keyColRow.ConstraintType.Equals("P")) //'PRIMARY KEY' { //foreach (string pk_name in keyColRow.column_name_primaries) //{ DbLinq.Schema.Dbml.Column primaryKeyCol = table.Type.Columns.First(c => c.Name == keyColRow.ColumnName); primaryKeyCol.IsPrimaryKey = true; //} continue; } if (keyColRow.ConstraintType.Equals("R")) //'FOREIGN KEY' { // This is very bad... if (!names.ColumnsNames[keyColRow.ReferencedTableName].ContainsKey(keyColRow.ReferencedColumnName)) continue; LoadForeignKey(schema, table, keyColRow.ColumnName, keyColRow.TableName, keyColRow.TableSchema, keyColRow.ReferencedColumnName, keyColRow.ReferencedTableName, keyColRow.ReferencedTableSchema, keyColRow.ConstraintName, nameFormat, names); } } }
protected override void LoadStoredProcedures(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat) { var parameters = ReadParameters(conn, schemaName.DbName); foreach (var parameter in parameters) { var procedureName = CreateProcedureName(parameter.ProcedureName, parameter.Schema, nameFormat); Function function = schema.Functions.SingleOrDefault(f => f.Method == procedureName.MethodName); if (function == null) { function = new Function { Name = procedureName.DbName, Method = procedureName.MethodName }; schema.Functions.Add(function); } if (parameter.Name == null) { var returnParameter = new Return(); returnParameter.DbType = parameter.Type.SqlType; returnParameter.Type = MapDbType(parameter.Name, parameter.Type).ToString(); function.IsComposable = true; function.Return = returnParameter; } else { var functionParameter = new Parameter(); functionParameter.DbType = parameter.Type.SqlType; functionParameter.Type = MapDbType(parameter.Name, parameter.Type).ToString(); if (parameter.In) { if (parameter.Out) functionParameter.Direction = DbLinq.Schema.Dbml.ParameterDirection.InOut; else functionParameter.Direction = DbLinq.Schema.Dbml.ParameterDirection.In; } else functionParameter.Direction = DbLinq.Schema.Dbml.ParameterDirection.Out; var parameterName = CreateParameterName(parameter.Name, nameFormat); functionParameter.Name = parameterName.CallName; function.Parameters.Add(functionParameter); } } }
protected override void LoadStoredProcedures(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat) { var procs = ReadProcedures(conn, schemaName.DbName); foreach (DataStoredProcedure proc in procs) { var procedureName = CreateProcedureName(proc.specific_name, proc.db, nameFormat); var func = new Function(); func.Name = procedureName.DbName; func.Method = procedureName.MethodName; func.IsComposable = string.Compare(proc.type, "FUNCTION") == 0; func.BodyContainsSelectStatement = proc.body != null && proc.body.IndexOf("select", StringComparison.OrdinalIgnoreCase) > -1; ParseProcParams(proc, func); schema.Functions.Add(func); } }
protected override void LoadStoredProcedures(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat) { var procs = ReadProcedures(conn, schemaName.DbName); //4a. determine unknown types Dictionary<long, string> typeOidToName = new Dictionary<long, string>(); foreach (DataStoredProcedure proc in procs) { if (proc.proallargtypes == null && !string.IsNullOrEmpty(proc.proargtypes)) proc.proallargtypes = "{" + proc.proargtypes.Replace(' ', ',') + "}"; //work around pgsql weirdness? } foreach (DataStoredProcedure proc in procs) { typeOidToName[proc.prorettype] = proc.formatted_prorettype; if (proc.proallargtypes == null) continue; //no args, no Oids to resolve, skip string[] argTypes1 = parseCsvString(proc.proallargtypes); //eg. {23,24,1043} var argTypes2 = from t in argTypes1 select long.Parse(t); foreach (long argType in argTypes2) { if (!typeOidToName.ContainsKey(argType)) typeOidToName[argType] = null; } } //4b. get names for unknown types GetTypeNames(conn, schemaName.DbName, typeOidToName); //4c. generate dbml objects foreach (DataStoredProcedure proc in procs) { DbLinq.Schema.Dbml.Function dbml_fct = ParseFunction(proc, typeOidToName, nameFormat); if (!SkipProc(dbml_fct.Name)) schema.Functions.Add(dbml_fct); } }
protected override void LoadStoredProcedures(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat) { // TODO: debug stored procedures support return; var procs = ReadProcedures(conn, schemaName.DbName); foreach (DataStoredProcedure proc in procs) { var procedureName = CreateProcedureName(proc.Name, proc.TableSchema, nameFormat); var func = new Function(); func.Name = procedureName.DbName; func.Method = procedureName.MethodName; func.IsComposable = string.Compare(proc.Type, "FUNCTION") == 0; func.BodyContainsSelectStatement = proc.BodyContainsSelectStatement; ParseProcParams(proc, func); schema.Functions.Add(func); } }
/// <summary> /// Loads the columns. /// </summary> /// <param name="schema">The schema.</param> /// <param name="schemaName">Name of the schema.</param> /// <param name="conn">The conn.</param> /// <param name="nameAliases">The name aliases.</param> /// <param name="nameFormat">The name format.</param> /// <param name="names">The names.</param> protected void LoadColumns(Database schema, SchemaName schemaName, IDbConnection conn, INameAliases nameAliases, NameFormat nameFormat, Names names) { var columnRows = ReadColumns(conn, schemaName.DbName); foreach (var columnRow in columnRows) { var columnName = CreateColumnName(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema, nameAliases, nameFormat); names.AddColumn(columnRow.TableName, columnName); //find which table this column belongs to string fullColumnDbName = GetFullDbName(columnRow.TableName, columnRow.TableSchema); DbLinq.Schema.Dbml.Table tableSchema = schema.Tables.FirstOrDefault(tblSchema => fullColumnDbName == tblSchema.Name); if (tableSchema == null) { WriteErrorLine("ERROR L46: Table '" + columnRow.TableName + "' not found for column " + columnRow.ColumnName); continue; } var column = new Column(); column.Name = columnName.DbName; column.Member = columnName.PropertyName; column.DbType = columnRow.FullType; if (columnRow.PrimaryKey.HasValue) column.IsPrimaryKey = columnRow.PrimaryKey.Value; bool? generated = (nameAliases != null) ? nameAliases.GetColumnGenerated(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null; if (!generated.HasValue) generated = columnRow.Generated; if (generated.HasValue) column.IsDbGenerated = generated.Value; AutoSync? autoSync = (nameAliases != null) ? nameAliases.GetColumnAutoSync(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null; if (autoSync.HasValue) column.AutoSync = autoSync.Value; // the Expression can originate from two sources: // 1. DefaultValue // 2. Expression // we use any valid source (we can't have both) if (column.IsDbGenerated && columnRow.DefaultValue != null) column.Expression = columnRow.DefaultValue; column.CanBeNull = columnRow.Nullable; string columnTypeAlias = nameAliases != null ? nameAliases.GetColumnForcedType(columnRow.ColumnName, columnRow.TableName, columnRow.TableSchema) : null; var columnType = MapDbType(columnName.DbName, columnRow); var columnEnumType = columnType as EnumType; if (columnEnumType != null) { var enumType = column.SetExtendedTypeAsEnumType(); enumType.Name = columnEnumType.Name; foreach (KeyValuePair<string, int> enumValue in columnEnumType.EnumValues) { enumType[enumValue.Key] = enumValue.Value; } } else if (columnTypeAlias != null) column.Type = columnTypeAlias; else column.Type = columnType.ToString(); tableSchema.Type.Columns.Add(column); } }
/// <summary> /// Loads the tables in the given schema. /// </summary> /// <param name="schema">The schema.</param> /// <param name="schemaName">Name of the schema.</param> /// <param name="conn">The conn.</param> /// <param name="nameAliases">The name aliases.</param> /// <param name="nameFormat">The name format.</param> /// <param name="names">The names.</param> protected virtual void LoadTables(Database schema, SchemaName schemaName, IDbConnection conn, INameAliases nameAliases, NameFormat nameFormat, Names names) { var tables = ReadTables(conn, schemaName.DbName); foreach (var row in tables) { var tableName = CreateTableName(row.Name, row.Schema, nameAliases, nameFormat); names.TablesNames[tableName.DbName] = tableName; var table = new Table(); table.Name = tableName.DbName; table.Member = tableName.MemberName; table.Type.Name = tableName.ClassName; schema.Tables.Add(table); } }
protected virtual void LoadStoredProcedures(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat) { }
/// <summary> /// Loads the constraints. /// </summary> /// <param name="schema">The schema.</param> /// <param name="schemaName">Name of the schema.</param> /// <param name="conn">The conn.</param> /// <param name="nameFormat">The name format.</param> /// <param name="names">The names.</param> protected abstract void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names);
public SchemaName GetSchemaName(string dbName, WordsExtraction extraction, NameFormat nameFormat) { var words = GetLanguageWords(nameFormat.Culture); var schemaName = new SchemaName { DbName = dbName }; schemaName.NameWords = ExtractWords(words, dbName, extraction); schemaName.ClassName = Format(words, schemaName.NameWords, nameFormat.Case, Singularization.DontChange); return schemaName; }