Ejemplo n.º 1
0
        public void RevokeParseTest()
        {
            var db = new PgDatabase("Name", new List <string>());

            db.DefaultSchema.Privileges.Count.Should().Be(0);
            PrivilegeParser.Parse(db, "REVOKE ALL ON FUNCTION generate_csharp_pocos() FROM PUBLIC;", PgPrivilegeCommand.Revoke);
            db.DefaultSchema.Privileges.Count.Should().Be(1);
            var priv = db.DefaultSchema.Privileges[0];

            priv.Command.Should().Be(PgPrivilegeCommand.Revoke);
            priv.Privilege.Should().Be(PgPrivilegeKind.All);
            priv.Role.Should().Be("public");
            priv.OnType.Should().Be("FUNCTION");
            priv.OnName.Should().Be("generate_csharp_pocos()");

            PrivilegeParser.Parse(db, "REVOKE ALL ON FUNCTION generate_ids(sequence_name text, count integer) FROM PUBLIC;", PgPrivilegeCommand.Revoke);
            db.DefaultSchema.Privileges.Count.Should().Be(2);
            priv = db.DefaultSchema.Privileges[1];
            priv.Command.Should().Be(PgPrivilegeCommand.Revoke);
            priv.Privilege.Should().Be(PgPrivilegeKind.All);
            priv.Role.Should().Be("public");
            priv.OnType.Should().Be("FUNCTION");
            priv.OnName.Should().Be("generate_ids(sequence_name text, count integer)");

            PrivilegeParser.Parse(db, "REVOKE ALL ON FUNCTION postgrestype_to_csharptype(pg_type text) FROM postgres;", PgPrivilegeCommand.Revoke);
            db.DefaultSchema.Privileges.Count.Should().Be(3);
            priv = db.DefaultSchema.Privileges[2];
            priv.Command.Should().Be(PgPrivilegeCommand.Revoke);
            priv.Privilege.Should().Be(PgPrivilegeKind.All);
            priv.Role.Should().Be("postgres");
            priv.OnType.Should().Be("FUNCTION");
            priv.OnName.Should().Be("postgrestype_to_csharptype(pg_type text)");
        }
        /// <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);
        }