/// <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 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); } }