Exemplo n.º 1
0
        public static List <Table> ResortTables(List <Table> tables, List <TableForeignKey> foreignKeys)
        {
            string[] tableNames = tables.Select(item => item.Name).ToArray();

            List <string> sortedTableNames = TableReferenceHelper.ResortTableNames(tableNames, foreignKeys);

            int i = 1;

            foreach (string tableName in sortedTableNames)
            {
                Table table = tables.FirstOrDefault(item => item.Name == tableName);
                if (table != null)
                {
                    table.Order = i++;
                }
            }

            return(tables.OrderBy(item => item.Order).ToList());
        }
Exemplo n.º 2
0
        public virtual async Task <SchemaInfo> GetSchemaInfoAsync(SelectionInfo selectionInfo)
        {
            SchemaInfo schemaInfo = new SchemaInfo();

            using (DbConnection connection = this.dbConnector.CreateConnection())
            {
                if (this.NeedFetchObjects(selectionInfo.UserDefinedTypeNames))
                {
                    schemaInfo.UserDefinedTypes = await this.GetUserDefinedTypesAsync(connection, selectionInfo.UserDefinedTypeNames);
                }

                if (this.NeedFetchObjects(selectionInfo.FunctionNames))
                {
                    schemaInfo.Functions = await this.GetFunctionsAsync(connection, selectionInfo.FunctionNames);
                }

                if (this.NeedFetchObjects(selectionInfo.TableNames))
                {
                    schemaInfo.Tables = await this.GetTablesAsync(connection, selectionInfo.TableNames);

                    schemaInfo.TableColumns = await this.GetTableColumnsAsync(connection, selectionInfo.TableNames);

                    if (this.Option.GenerateKey)
                    {
                        schemaInfo.TablePrimaryKeys = await this.GetTablePrimaryKeysAsync(connection, selectionInfo.TableNames);

                        schemaInfo.TableForeignKeys = await this.GetTableForeignKeysAsync(connection, selectionInfo.TableNames);
                    }

                    if (this.Option.GenerateIndex)
                    {
                        schemaInfo.TableIndexes = await this.GetTableIndexesAsync(connection, selectionInfo.TableNames);
                    }
                }

                if (this.Option.SortTablesByKeyReference && this.Option.GenerateKey)
                {
                    string[] tableNames = schemaInfo.Tables.Select(item => item.Name).ToArray();

                    List <string> sortedTableNames = TableReferenceHelper.ResortTableNames(tableNames, schemaInfo.TableForeignKeys);

                    int i = 1;
                    foreach (string tableName in sortedTableNames)
                    {
                        Table table = schemaInfo.Tables.FirstOrDefault(item => item.Name == tableName);
                        if (table != null)
                        {
                            table.Order = i++;
                        }
                    }

                    schemaInfo.Tables = schemaInfo.Tables.OrderBy(item => item.Order).ToList();
                }

                if (this.NeedFetchObjects(selectionInfo.ViewNames))
                {
                    schemaInfo.Views = ViewHelper.ResortViews(await this.GetViewsAsync(connection, selectionInfo.ViewNames));
                }

                if (this.NeedFetchObjects(selectionInfo.TriggerNames))
                {
                    schemaInfo.Triggers = await this.GetTriggersAsync(connection, selectionInfo.TriggerNames);
                }

                if (this.NeedFetchObjects(selectionInfo.ProcedureNames))
                {
                    schemaInfo.Procedures = await this.GetProceduresAsync(connection, selectionInfo.ProcedureNames);
                }
            }

            return(schemaInfo);
        }