Ejemplo n.º 1
0
        public static PgDatabase LoadDatabaseSchema(TextReader reader, string charsetName, bool outputIgnoredStatements, bool ignoreSlonyTriggers)
        {
            var database = new PgDatabase();

            var statement = GetWholeStatement(reader);

            while (statement != null)
            {
                if (PatternCreateSchema.IsMatch(statement))
                {
                    CreateSchemaParser.Parse(database, statement);
                }
                else if (PatternDefaultSchema.IsMatch(statement))
                {
                    database.SetDefaultSchema(PatternDefaultSchema.Matches(statement)[0].Groups[1].Value);
                }
                else if (PatternCreateTable.IsMatch(statement))
                {
                    CreateTableParser.Parse(database, statement);
                }
                else if (PatternAlterTable.IsMatch(statement))
                {
                    AlterTableParser.Parse(database, statement, outputIgnoredStatements);
                }
                else if (PatternCreateSequence.IsMatch(statement))
                {
                    CreateSequenceParser.Parse(database, statement);
                }
                else if (PatternAlterSequence.IsMatch(statement))
                {
                    AlterSequenceParser.Parse(database, statement, outputIgnoredStatements);
                }
                else if (PatternCreateIndex.IsMatch(statement))
                {
                    CreateIndexParser.Parse(database, statement);
                }
                else if (PatternCreateView.IsMatch(statement))
                {
                    CreateViewParser.Parse(database, statement);
                }
                else if (PatternAlterView.IsMatch(statement))
                {
                    AlterViewParser.Parse(database, statement, outputIgnoredStatements);
                }
                else if (PatternCreateTrigger.IsMatch(statement))
                {
                    CreateTriggerParser.Parse(database, statement, ignoreSlonyTriggers);
                }
                else if (PatternCreateFunction.IsMatch(statement))
                {
                    CreateFunctionParser.Parse(database, statement);
                }
                else if (PatternComment.IsMatch(statement))
                {
                    CommentParser.Parse(database, statement, outputIgnoredStatements);
                }
                else if (PatternSelect.IsMatch(statement) ||
                         PatternInsertInto.IsMatch(statement) ||
                         PatternUpdate.IsMatch(statement) ||
                         PatternDeleteFrom.IsMatch(statement)) /* we just ignore these statements*/ } {
        /// <summary>
        /// Loads database schema from dump file.
        /// </summary>
        /// <param name="file">The path to the file which is loaded.</param>
        /// <param name="database">The database.</param>
        /// <param name="encodingName">Charset that should be used to read the file.</param>
        /// <param name="outputIgnoredStatements">Whether ignored statements should be included in the output.</param>
        /// <param name="ignoreSlonyTriggers">Indicates if slony triggers are ignored.</param>
        /// <returns>Database schema from dump file.</returns>
        public static PgDatabase LoadDatabaseSchema(string file, Database database, bool outputIgnoredStatements, bool ignoreSlonyTriggers, string encodingName = DefaultEncoding)
        {
            var          encoding   = Encoding.GetEncoding(encodingName);
            var          pgDatabase = new PgDatabase(database.Name, database.IgnoredSchemas.ToList());
            StreamReader reader     = null;

            using (reader = new StreamReader(file, encoding))
            {
                var statement = GetWholeStatement(reader);

                while (statement != null)
                {
                    if (PatternCreateSchema.Matches(statement).Count != 0)
                    {
                        CreateSchemaParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternCreateRule.Matches(statement).Count != 0)
                    {
                        CreateRuleParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternDefaultSchema.Matches(statement).Count != 0)
                    {
                        PatternDefaultSchema.Matches(statement);
                        pgDatabase.SetDefaultSchema(PatternDefaultSchema.Matches(statement)[0].Groups[1].ToString());
                    }
                    else if (PatternCreateTable.Matches(statement).Count != 0)
                    {
                        CreateTableParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternAlterTable.Matches(statement).Count != 0)
                    {
                        AlterTableParser.Parse(pgDatabase, statement, outputIgnoredStatements);
                    }
                    else if (PatternCreateSequence.Matches(statement).Count != 0)
                    {
                        CreateSequenceParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternAlterSequence.Matches(statement).Count != 0)
                    {
                        AlterSequenceParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternCreateIndex.Matches(statement).Count != 0)
                    {
                        CreateIndexParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternCreateView.Matches(statement).Count != 0)
                    {
                        CreateViewParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternAlterView.Matches(statement).Count != 0)
                    {
                        AlterViewParser.Parse(pgDatabase, statement, outputIgnoredStatements);
                    }
                    else if (PatternCreateTrigger.Matches(statement).Count != 0)
                    {
                        CreateTriggerParser.Parse(pgDatabase, statement, ignoreSlonyTriggers);
                    }
                    else if (PatternCreateFunction.Matches(statement).Count != 0)
                    {
                        CreateFunctionParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternPrivilegeGrant.Matches(statement).Count != 0)
                    {
                        PrivilegeParser.Parse(pgDatabase, statement, PgPrivilegeCommand.Grant);
                    }
                    else if (PatternPrivilegeRevoke.Matches(statement).Count != 0)
                    {
                        PrivilegeParser.Parse(pgDatabase, statement, PgPrivilegeCommand.Revoke);
                    }
                    else if (PatternCreateAggregate.Matches(statement).Count != 0)
                    {
                        CreateAggregateParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternComment.Matches(statement).Count != 0)
                    {
                        CommentParser.Parse(pgDatabase, statement, outputIgnoredStatements);
                    }
                    else if (PatternCreateType.Matches(statement).Count != 0)
                    {
                        CreateTypeParser.Parse(pgDatabase, statement);
                    }
                    else if (PatternSelect.Matches(statement).Count != 0 ||
                             PatternInsertInto.Matches(statement).Count != 0 ||
                             PatternUpdate.Matches(statement).Count != 0 ||
                             PatternDeleteFrom.Matches(statement).Count != 0)
                    {
                        // these statements are ignored
                    }
                    else if (outputIgnoredStatements)
                    {
                        pgDatabase.AddIgnoredStatement(statement);
                    }
                    else
                    {
                        // these statements are ignored if outputIgnoredStatements is false
                    }

                    statement = GetWholeStatement(reader);
                }
            }

            return(pgDatabase);
        }