public static async Task <List <InformationSchemaTableDefinition> > GetInformationSchemaTableDefinitions( ISqlConnectionProvider sqlConnectionProvider) { var tables = GetAllTables(sqlConnectionProvider); var allColumns = GetAllColumns(sqlConnectionProvider); var allConstraints = await GetAllTableConstraints(sqlConnectionProvider); var columnsByTable = allColumns.ToDictionaryMany(z => new { z.TABLE_SCHEMA, z.TABLE_NAME }); var constraintsByTable = allConstraints.ToDictionaryMany(z => new { z.TABLE_SCHEMA, z.TABLE_NAME }); var results = new List <InformationSchemaTableDefinition>(); foreach (var table in tables) { var key = new { table.TABLE_SCHEMA, table.TABLE_NAME }; var columns = columnsByTable.TryGet(key); var constraints = constraintsByTable.TryGet(key); var tableDef = new InformationSchemaTableDefinition() { InformationSchemaTable = table, InformationSchemaColumns = columns.Value ?? new List <SISColumn>(), TableConstraints = constraints.Value ?? new List <TableConstraintInfoDto>() }; results.Add(tableDef); } return(results); }
public static string GenerateCreateTableScript( InformationSchemaTableDefinition tableDefinition, CreateIfExistsModification createIfExistsModification = CreateIfExistsModification.CreateAnyway) { var informationSchemaTable = tableDefinition.InformationSchemaTable; if (informationSchemaTable.TABLE_TYPE != TableTypes.BaseTable && informationSchemaTable.TABLE_TYPE != TableTypes.View) { throw new NotSupportedException( $"Table is of a type other than 'Base Table' or 'View'. Table type is {tableDefinition.InformationSchemaTable.TABLE_TYPE}"); } switch (tableDefinition.InformationSchemaTable.TABLE_TYPE) { case TableTypes.View: throw new NotImplementedException("Have not implemented View creation yet."); case TableTypes.BaseTable: var schema = informationSchemaTable.TABLE_SCHEMA; var tableName = informationSchemaTable.TABLE_NAME; var columns = tableDefinition.InformationSchemaColumns; var script = CreateTableScript(schema, tableName, columns, createIfExistsModification); return(script); default: throw new ArgumentOutOfRangeException(); } }
public static InformationSchemaTableDefinition GetInformationSchemaTableDefinition( SqlTableReference sqlTableReference) { var definition = new InformationSchemaTableDefinition(); definition.InformationSchemaTable = GetInformationSchemaTable(sqlTableReference); definition.InformationSchemaColumns = GetInformationSchemaColumns(sqlTableReference); definition.TableConstraints = GetTableConstraints(sqlTableReference); return(definition); }