public virtual SchemaInfo GetSchemaInfo(string[] tableNames, string[] userDefinedTypeNames = null, bool getAllIfNotSpecified = true) { List <UserDefinedType> userDefinedTypes = new List <UserDefinedType>(); List <Table> tables = new List <Table>(); List <TableColumn> columns = new List <TableColumn>(); if ((userDefinedTypeNames != null && userDefinedTypeNames.Length > 0) || getAllIfNotSpecified) { userDefinedTypes = this.GetUserDefinedTypes(userDefinedTypeNames); } List <TablePrimaryKey> tablePrimaryKeys = new List <TablePrimaryKey>(); List <TableForeignKey> tableForeignKeys = new List <TableForeignKey>(); List <TableIndex> tableIndices = new List <TableIndex>(); if ((tableNames != null && tableNames.Length > 0) || getAllIfNotSpecified) { tables = this.GetTables(tableNames); columns = this.GetTableColumns(tableNames); if (Option.GenerateKey) { tablePrimaryKeys = this.GetTablePrimaryKeys(tableNames); tableForeignKeys = this.GetTableForeignKeys(tableNames); } if (Option.GenerateIndex) { tableIndices = this.GetTableIndexes(tableNames); } } if (Option.SortTablesByKeyReference && Option.GenerateKey) { List <string> sortedTableNames = TableReferenceHelper.ResortTableNames(tableNames, tableForeignKeys); int i = 1; foreach (string tableName in sortedTableNames) { Table table = tables.FirstOrDefault(item => item.Name == tableName); if (table != null) { table.Order = i++; } } tables = tables.OrderBy(item => item.Order).ToList(); } return(new SchemaInfo() { UserDefinedTypes = userDefinedTypes, Tables = tables, Columns = columns, TablePrimaryKeys = tablePrimaryKeys, TableForeignKeys = tableForeignKeys, TableIndices = tableIndices }); }
private async Task <string> InternalGenerateDataScripts(SchemaInfo schemaInfo, bool async = false) { StringBuilder sb = new StringBuilder(); if (Option.ScriptOutputMode == GenerateScriptOutputMode.WriteToFile) { this.AppendScriptsToFile("", GenerateScriptMode.Data, true); } int i = 0; int pickupIndex = -1; if (schemaInfo.PickupTable != null) { foreach (Table table in schemaInfo.Tables) { if (table.Owner == schemaInfo.PickupTable.Owner && table.Name == schemaInfo.PickupTable.Name) { pickupIndex = i; break; } i++; } } i = 0; using (DbConnection connection = this.GetDbConnector().CreateConnection()) { int tableCount = schemaInfo.Tables.Count - (pickupIndex == -1 ? 0 : pickupIndex + 1); int count = 0; foreach (Table table in schemaInfo.Tables) { if (i < pickupIndex) { i++; continue; } count++; string strTableCount = $"({count}/{tableCount})"; string tableName = table.Name; List <TableColumn> columns = schemaInfo.Columns.Where(item => item.Owner == table.Owner && item.TableName == tableName).OrderBy(item => item.Order).ToList(); bool isSelfReference = TableReferenceHelper.IsSelfReference(tableName, schemaInfo.TableForeignKeys); List <TablePrimaryKey> primaryKeys = schemaInfo.TablePrimaryKeys.Where(item => item.Owner == table.Owner && item.TableName == tableName).ToList(); string primaryKeyColumns = string.Join(",", primaryKeys.OrderBy(item => item.Order).Select(item => GetQuotedString(item.ColumnName))); long total = await this.GetTableRecordCountAsync(connection, table); if (Option.DataGenerateThreshold.HasValue && total > Option.DataGenerateThreshold.Value) { continue; } int pageSize = Option.DataBatchSize; this.FeedbackInfo($"{strTableCount}Begin to read data from table {table.Name}, total rows:{total}."); Dictionary <long, List <Dictionary <string, object> > > dictPagedData; if (isSelfReference) { string parentColumnName = schemaInfo.TableForeignKeys.FirstOrDefault(item => item.Owner == table.Owner && item.TableName == tableName && item.ReferencedTableName == tableName)?.ColumnName; string strWhere = $" WHERE {GetQuotedString(parentColumnName)} IS NULL"; dictPagedData = this.GetSortedPageDatas(connection, table, primaryKeyColumns, parentColumnName, columns, Option, strWhere); } else { dictPagedData = async ? await this.GetPagedDataListAsync(connection, table, columns, primaryKeyColumns, total, pageSize) : this.GetPagedDataList(connection, table, columns, primaryKeyColumns, total, pageSize); } this.FeedbackInfo($"{strTableCount}End read data from table {table.Name}."); this.AppendDataScripts(Option, sb, table, columns, dictPagedData); i++; } } var dataScripts = string.Empty; try { dataScripts = sb.ToString(); } catch (OutOfMemoryException ex) { this.FeedbackError("Exception occurs:" + ex.Message); } finally { sb.Clear(); } return(dataScripts); }
private async Task <SchemaInfo> InternalGetSchemalInfo(SelectionInfo selectionInfo, bool getAllIfNotSpecified = true, bool async = false) { List <UserDefinedType> userDefinedTypes = new List <UserDefinedType>(); string[] userDefinedTypeNames = selectionInfo.UserDefinedTypeNames; string[] tableNames = selectionInfo.TableNames; string[] viewNames = selectionInfo.ViewNames; List <Table> tables = new List <Table>(); List <TableColumn> columns = new List <TableColumn>(); List <View> views = new List <View>(); if ((userDefinedTypeNames != null && userDefinedTypeNames.Length > 0) || getAllIfNotSpecified) { userDefinedTypes = async ? await this.GetUserDefinedTypesAsync(userDefinedTypeNames) : this.GetUserDefinedTypes(userDefinedTypeNames); } List <TablePrimaryKey> tablePrimaryKeys = new List <TablePrimaryKey>(); List <TableForeignKey> tableForeignKeys = new List <TableForeignKey>(); List <TableIndex> tableIndices = new List <TableIndex>(); if ((tableNames != null && tableNames.Length > 0) || getAllIfNotSpecified) { tables = async ? await this.GetTablesAsync(tableNames) : this.GetTables(tableNames); columns = this.GetTableColumns(tableNames); if (Option.GenerateKey) { tablePrimaryKeys = this.GetTablePrimaryKeys(tableNames); tableForeignKeys = this.GetTableForeignKeys(tableNames); } if (Option.GenerateIndex) { tableIndices = this.GetTableIndexes(tableNames); } } if (Option.SortTablesByKeyReference && Option.GenerateKey) { List <string> sortedTableNames = TableReferenceHelper.ResortTableNames(tableNames, tableForeignKeys); int i = 1; foreach (string tableName in sortedTableNames) { Table table = tables.FirstOrDefault(item => item.Name == tableName); if (table != null) { table.Order = i++; } } tables = tables.OrderBy(item => item.Order).ToList(); } if ((viewNames != null && viewNames.Length > 0) || getAllIfNotSpecified) { views = async ? await this.GetViewsAsync(viewNames) : this.GetViews(viewNames); views = ViewHelper.ResortViews(views); } return(new SchemaInfo { UserDefinedTypes = userDefinedTypes, Tables = tables, Columns = columns, TablePrimaryKeys = tablePrimaryKeys, TableForeignKeys = tableForeignKeys, TableIndices = tableIndices, Views = views }); }