public override List <DatabaseTableForeignKeyColumnsWindows> GetTableForeignKeyColumns() { List <DatabaseTableForeignKeyColumnsWindows> result = new List <DatabaseTableForeignKeyColumnsWindows>(); foreach (SqliteDatabaseTableWindows t in _tables) { DatabaseTableForeignKeyColumnsWindows foreignKeyColumns = new DatabaseTableForeignKeyColumnsWindows(t.TableName); t.GetForeignKeyColumns().ToList().ForEach(c => foreignKeyColumns.ForeignKeys.Add(new ForeignKeyInfoWindows() { ChildTableName = t.TableName, ChildTableForeignKeyName = c.ColumnName, ParentTableName = c.ParentTableName, ParentTablePrimaryKeyName = c.ParentTablePrimaryKeyName, ConstraintName = c.ConstraintName })); result.Add(foreignKeyColumns); } return(result); }
public SqlDatabaseWindows GetSqlDatabase( bool createOrmAssembly, bool saveOrmAssembly, string ormAssemblyOutputDirectory, bool wrapWebException) { string rawOutput = string.Empty; SqlDatabaseWindows result = new SqlDatabaseWindows(); HttpStatusCode statusCode; string statusDescription = null; result.Name = GetSqlSchema <string>(DatabaseSchemaInfoTypeWindows.DatabaseName, null, out rawOutput, out statusCode, out statusDescription, wrapWebException); DataTable tablesSchema = GetSqlSchema <DataTable>(DatabaseSchemaInfoTypeWindows.Tables, null, out rawOutput, out statusCode, out statusDescription, wrapWebException); Dictionary <string, DatabaseTableKeyColumnsWindows> tablesKeyColumns = new Dictionary <string, DatabaseTableKeyColumnsWindows>(); //Primary keys of all the tables. GetSqlSchema <List <DatabaseTableKeyColumnsWindows> >(DatabaseSchemaInfoTypeWindows.TableKeyColumns, null, out rawOutput, out statusCode, out statusDescription, wrapWebException).ForEach(p => tablesKeyColumns.Add(p.TableName, p)); Dictionary <string, DatabaseTableForeignKeyColumnsWindows> tablesForeignKeyColumns = new Dictionary <string, DatabaseTableForeignKeyColumnsWindows>(); //Foreign keys of all tables. GetSqlSchema <List <DatabaseTableForeignKeyColumnsWindows> >(DatabaseSchemaInfoTypeWindows.TableForeignKeyColumns, null, out rawOutput, out statusCode, out statusDescription, wrapWebException).ForEach(p => tablesForeignKeyColumns.Add(p.TableName, p)); foreach (DataRow t in tablesSchema.Rows) { SqlDatabaseTableWindows table = new SqlDatabaseTableWindows(t); if (table.IsSystemTable) { continue; } if (!tablesKeyColumns.ContainsKey(table.TableName)) { throw new UserThrownException(string.Format("Could not find key columns for table {0}.", table.TableName), LoggingLevel.Minimum); } DatabaseTableKeyColumnsWindows tableKeys = tablesKeyColumns[table.TableName]; DatabaseTableForeignKeyColumnsWindows tableForeignKeys = tablesForeignKeyColumns[table.TableName]; if (result.Tables.Exists(table.TableName)) { throw new Exception(string.Format( "{0} with name {1} already added.", typeof(SqlDatabaseTableWindows).FullName, table.TableName)); } result.AddTable(table); DataTable columnsSchema = GetSqlSchema <DataTable>(DatabaseSchemaInfoTypeWindows.Columns, table.TableName, out rawOutput, out statusCode, out statusDescription, wrapWebException); table.PopulateColumnsFromSchema(columnsSchema); foreach (string keyColumn in tableKeys.KeyNames) { if (!table.Columns.Exists(keyColumn)) { throw new UserThrownException(string.Format("Could not find key column {0} on table {1}.", keyColumn, table.TableName), LoggingLevel.Minimum); } table.Columns[keyColumn].IsKey = true; } foreach (ForeignKeyInfoWindows foreignKeyColumn in tableForeignKeys.ForeignKeys) { if (!table.Columns.Exists(foreignKeyColumn.ChildTableForeignKeyName)) { throw new UserThrownException(string.Format("Could not find foreign key column {0} on table {1}.", foreignKeyColumn.ChildTableForeignKeyName, table.TableName), LoggingLevel.Minimum); } DatabaseTableColumnWindows c = table.Columns[foreignKeyColumn.ChildTableForeignKeyName]; c.IsForeignKey = true; c.ParentTableName = foreignKeyColumn.ParentTableName; c.ParentTablePrimaryKeyName = foreignKeyColumn.ParentTablePrimaryKeyName; c.ConstraintName = foreignKeyColumn.ConstraintName; } } PopulateSqlDatabaseChildrenTables(result); if (createOrmAssembly) { result.CreateOrmAssembly(saveOrmAssembly, ormAssemblyOutputDirectory); } return(result); }