public override void PopulateColumnsFromSchema(DbConnection connection, bool disposeConnectionAfterExecute) { try { if (connection == null) { connection = new SQLiteConnection(_connectionString); } if (connection.State != ConnectionState.Open) { connection.Open(); } _columns.Clear(); DataTable schema = connection.GetSchema("Columns", new string[] { null, null, _tableName, null }); List <DatabaseTableColumnWindows> tempColumns = new List <DatabaseTableColumnWindows>(); foreach (DataRow row in schema.Rows) { tempColumns.Add(new SqliteDatabaseTableColumnWindows(row)); } tempColumns.OrderBy(c => c.OrdinalPosition).ToList().ForEach(c => _columns.Add(c.ColumnName, c)); if (!(connection is SQLiteConnection)) { throw new InvalidCastException(string.Format( "Expected a {0}, and received a {1}.", typeof(SQLiteConnection).FullName, connection.GetType().FullName)); } List <string> keyColumns = GetKeyColummnNames(connection, false); if (keyColumns.Count < 0) { throw new Exception(string.Format("Table {0} has no key columns.", _tableName)); } foreach (string k in keyColumns) { DatabaseTableColumnWindows c = _columns[k]; if (c == null) { throw new NullReferenceException(string.Format( "Could not find key column {0} on table {1}.", k, _tableName)); } c.IsKey = true; } SQLiteConnection sqliteConnection = connection as SQLiteConnection; if (sqliteConnection == null) { throw new InvalidCastException(string.Format("Expected connection to be a {0} in {1}.", typeof(SQLiteConnection).FullName, this.GetType().FullName)); } EntityCacheGeneric <string, ForeignKeyInfoWindows> foreignKeys = sqliteConnection.GetTableForeignKeys(_tableName); foreach (ForeignKeyInfoWindows f in foreignKeys) { if (_columns.Exists(f.ChildTableForeignKeyName)) { DatabaseTableColumnWindows c = _columns[f.ChildTableForeignKeyName]; c.IsForeignKey = true; c.ParentTableName = f.ParentTableName; c.ParentTablePrimaryKeyName = f.ParentTablePrimaryKeyName; c.ConstraintName = f.ConstraintName; } } } finally { if (disposeConnectionAfterExecute && connection != null && connection.State != ConnectionState.Closed) { connection.Dispose(); } } }
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); }