コード例 #1
0
ファイル: DBObject.cs プロジェクト: aquilax1/DBLint
 public static int GetViewColumnHashCode(ViewColumnID vcid)
 {
     if (!vcid._hashCode.HasValue)
     {
         vcid._hashCode = vcid.ColumnName.GetHashCode();
     }
     return(GetViewHashCode(vcid) * 23 + vcid._hashCode.Value);
 }
コード例 #2
0
ファイル: DBObject.cs プロジェクト: aquilax1/DBLint
 public static bool ViewColumnEquals(ViewColumnID vcid, ViewColumnID vcid2)
 {
     if (vcid == null)
     {
         return(false);
     }
     if (GetViewColumnHashCode(vcid2) != GetViewColumnHashCode(vcid))
     {
         return(false);
     }
     return(ViewEquals(vcid, vcid2) && vcid.ColumnName.Equals(vcid2.ColumnName));
 }
コード例 #3
0
        public static Database DatabaseFactory(Extractor extractor, IEnumerable <DBLint.DataAccess.DBObjects.Schema> selectedSchemas, IEnumerable <DBLint.DataAccess.DBObjects.TableID> ignoredTables)
        {
            var database          = new Database(extractor.DatabaseName, extractor.Database);
            var ignoredDictionary = DictionaryFactory.CreateTableID <TableID>();

            foreach (var ignoredTable in ignoredTables)
            {
                var tblid = new TableID(extractor.DatabaseName, ignoredTable.SchemaName, ignoredTable.TableName);
                ignoredDictionary.Add(tblid, tblid);
            }

            using (var db = database)
            {
                var dbSchemas = extractor.Database.GetSchemas();
                foreach (var dbSchema in dbSchemas)
                {
                    using (var schema = new Schema(db.DatabaseName, dbSchema.SchemaName))
                    {
                        if (!selectedSchemas.Any(p => p.Equals(dbSchema)))
                        {
                            continue;
                        }
                        schema.Database = db;
                        db._schemas.Add(schema);

                        #region Table and table columns

                        var dbTables = extractor.Database.GetTables(schema.SchemaName);
                        var tables   = from dbTable in dbTables
                                       orderby dbTable.TableName
                                       select dbTable;

                        foreach (var dbTable in tables)
                        {
                            var table = new DataTable(db.DatabaseName, schema.SchemaName, dbTable.TableName, extractor.Database);

                            if (ignoredDictionary.ContainsKey(table))
                            {
                                database.IgnoredTables.Add(table);
                                continue;
                            }

                            schema._tables.Add(table);
                            db.tableDictionary.Add(table, table);
                            table.Database = db;
                            table.Schema   = schema;
                        }

                        var dbColumns = extractor.Database.GetColumns(schema.SchemaName);

                        var columnID = new ColumnID(extractor.DatabaseName, schema.SchemaName, null, null);
                        foreach (var dbColumn in dbColumns)
                        {
                            columnID.TableName  = dbColumn.TableName;
                            columnID.ColumnName = dbColumn.ColumnName;
                            if (db.tableDictionary.ContainsKey(columnID))
                            {
                                var column = new Column(db.DatabaseName, schema.SchemaName, dbColumn.TableName, dbColumn.ColumnName);
                                var table  = db.tableDictionary[column];
                                table._columns.Add(column);
                                column.DataType           = dbColumn.DataType;
                                column.DefaultValue       = dbColumn.DefaultValue;
                                column.OrdinalPosition    = dbColumn.OrdinalPosition;
                                column.CharacterMaxLength = dbColumn.CharacterMaxLength;
                                column.IsNullable         = dbColumn.IsNullable;
                                column.NumericPrecision   = dbColumn.NumericPrecision;
                                column.NumericScale       = dbColumn.NumericScale;
                                column.Table      = table;
                                column.Schema     = schema;
                                column.Database   = db;
                                column.IsSequence = dbColumn.IsSequence;
                                column.IsDefaultValueAFunction = dbColumn.DefaultIsFunction;
                            }
                        }

                        #endregion

                        #region Views and view columns

                        var dbViews = extractor.Database.GetViews(schema.SchemaName);
                        var views   = from dbView in dbViews
                                      orderby dbView.ViewName
                                      select dbView;

                        foreach (var dbView in views)
                        {
                            var view = new View(db.DatabaseName, schema.SchemaName, dbView.ViewName);

                            schema._views.Add(view);
                            db.viewDictionary.Add(view, view);
                            view.Database = db;
                            view.Schema   = schema;
                        }

                        var dbViewColumns = extractor.Database.GetViewColumns(schema.SchemaName);
                        var viewColumnID  = new ViewColumnID(extractor.DatabaseName, schema.SchemaName, null, null);

                        foreach (var dbViewColumn in dbViewColumns)
                        {
                            viewColumnID.ViewName   = dbViewColumn.ViewName;
                            viewColumnID.ColumnName = dbViewColumn.ColumnName;

                            if (db.viewDictionary.ContainsKey(viewColumnID))
                            {
                                var viewColumn = new ViewColumn(db.DatabaseName, schema.SchemaName, dbViewColumn.ViewName, dbViewColumn.ColumnName);
                                var view       = db.viewDictionary[viewColumn];
                                view._columns.Add(viewColumn);
                                viewColumn.Database           = db;
                                viewColumn.Schema             = schema;
                                viewColumn.View               = view;
                                viewColumn.DataType           = dbViewColumn.DataType;
                                viewColumn.DefaultValue       = dbViewColumn.DefaultValue;
                                viewColumn.OrdinalPosition    = dbViewColumn.OrdinalPosition;
                                viewColumn.CharacterMaxLength = dbViewColumn.CharacterMaxLength;
                                viewColumn.IsNullable         = dbViewColumn.IsNullable;
                                viewColumn.NumericPrecision   = dbViewColumn.NumericPrecision;
                                viewColumn.NumericScale       = dbViewColumn.NumericScale;
                                viewColumn.Privileges         = dbViewColumn.Privileges;
                            }
                        }

                        #endregion

                        // Adding functions
                        var functions = extractor.Database.GetFunctions(schema.SchemaName);
                        foreach (var function in functions)
                        {
                            var f = new Function(
                                database.DatabaseName,
                                function.SchemaName,
                                function.RoutineName);
                            f.Database = database;
                            f.Schema   = schema;

                            schema._functions.Add(f);
                        }

                        var fParameters = extractor.Database.GetFunctionsParameters(schema.SchemaName);
                        foreach (var fParameter in fParameters)
                        {
                            var fp = new Parameter(database.DatabaseName, fParameter.SchemaName, fParameter.RoutineName, fParameter.ParameterName);
                            fp.Database = schema.Database;
                            fp.Schema   = schema;

                            fp.DataType           = fParameter.DataType;
                            fp.Direction          = fParameter.Direction;
                            fp.CharacterMaxLength = fParameter.CharacterMaxLength;
                            fp.NumericPrecision   = fParameter.NumericPrecision;
                            fp.NumericScale       = fParameter.NumericScale;
                            fp.OrdinalPosition    = fParameter.OrdinalPosition;

                            var tmpF = schema._functions.Where(f => f.FunctionName.Equals(fp.RoutineName)).FirstOrDefault();
                            if (tmpF != null)
                            {
                                fp.Routine = tmpF;
                                tmpF._parameters.Add(fp);
                            }
                        }

                        // Adding stored procedures
                        var storedProcedures = extractor.Database.GetStoredProcedures(schema.SchemaName);
                        foreach (var storedProcedure in storedProcedures)
                        {
                            var sp = new StoredProcedure(
                                database.DatabaseName,
                                storedProcedure.SchemaName,
                                storedProcedure.RoutineName);

                            sp.Database = database;
                            sp.Schema   = schema;

                            schema._storedProcedures.Add(sp);
                        }

                        var parameters = extractor.Database.GetStoredProceduresParameters(schema.SchemaName);
                        foreach (var parameter in parameters)
                        {
                            var p = new Parameter(database.DatabaseName, parameter.SchemaName, parameter.RoutineName, parameter.ParameterName);
                            p.Database = schema.Database;
                            p.Schema   = schema;

                            p.DataType           = parameter.DataType;
                            p.Direction          = parameter.Direction;
                            p.CharacterMaxLength = parameter.CharacterMaxLength;
                            p.NumericPrecision   = parameter.NumericPrecision;
                            p.NumericScale       = parameter.NumericScale;
                            p.OrdinalPosition    = parameter.OrdinalPosition;

                            var tmpSp = schema._storedProcedures.Where(sp => sp.StoredProcedureName.Equals(p.RoutineName)).FirstOrDefault();
                            if (tmpSp != null)
                            {
                                p.Routine = tmpSp;
                                tmpSp._parameters.Add(p);
                            }
                        }
                    }
                }
            }
            AddForeignKeys(extractor, database);
            AddPrimaryKeys(extractor, database);
            AddIndices(extractor, database);
            AddUniqueConstraints(extractor, database);

            foreach (var tbl in database.Tables)
            {
                tbl.Dispose();
            }

            foreach (var view in database.Views)
            {
                view.Dispose();
            }

            AddCardinalities(database.tableDictionary, extractor, selectedSchemas);

            database.Escaper = Escaper.GetEscaper(extractor.Database.DBMS);
            database.DBMS    = extractor.Database.DBMS;
            return(database);
        }