private static Collection <PgSequence> GetSequences(DataTable table) { Collection <PgSequence> sequences = new Collection <PgSequence>(); if (table == null || table.Rows == null || table.Rows.Count.Equals(0)) { return(sequences); } foreach (DataRow row in table.Rows) { PgSequence sequence = new PgSequence { RowNumber = Conversion.TryCastInteger(row["row_number"]), SchemaName = Conversion.TryCastString(row["sequence_schema"]), Name = Conversion.TryCastString(row["sequence_name"]), DataType = Conversion.TryCastString(row["data_type"]), Increment = Conversion.TryCastString(row["increment"]), Description = Conversion.TryCastString(row["description"]), Owner = Conversion.TryCastString(row["owner"]), StartValue = Conversion.TryCastString(row["start_value"]), MinimumValue = Conversion.TryCastString(row["minimum_value"]), MaximumValue = Conversion.TryCastString(row["maximum_value"]) }; sequences.Add(sequence); } return(sequences); }
internal static void CallAlterTableParserParseSequence(Parser parser, PgSequence sequence, bool outputIgnoredStatements, string sequenceName, PgDatabase database) { var pt = new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType(typeof(AlterTableParser)); try { pt.InvokeStatic("ParseSequence", new object[] { parser, sequence, outputIgnoredStatements, sequenceName, database }); } catch (System.MissingMethodException missingMethodException) { throw new System.NotSupportedException("ParseSequence with requested parameters is not found. Rerun code generation.", missingMethodException); } }
/// <summary> /// Parses ALTER SEQUENCE statement. /// </summary> public static void Parse(PgDatabase database, string statement) { var parser = new Parser(statement); parser.Expect("ALTER", "SEQUENCE"); var sequenceName = parser.ParseIdentifier(); var schemaName = ParserUtils.GetSchemaName(sequenceName, database); if (database.SchemaIsIgnored(schemaName)) { return; } var schema = database.GetSchema(schemaName); if (schema == null) { throw new TeamworkParserException($"CannotFindSchema {schemaName} from statement {statement}"); } var objectName = ParserUtils.GetObjectName(sequenceName); PgSequence sequence = schema.GetSequence(objectName); if (sequence == null) { throw new TeamworkParserException($"CannotFindSequence {sequenceName} from statement {statement}"); } while (!parser.ExpectOptional(";")) { if (parser.ExpectOptional("OWNED", "BY")) { if (parser.ExpectOptional("NONE")) { sequence.Owner = null; } else { sequence.Owner = parser.Expression(); } } else { throw new TeamworkParserException("CannotParseStringUnsupportedCommand"); } } }
/// <summary> /// Parses COMMENT ON SEQUENCE. /// </summary> private static void ParseSequence(Parser parser, PgDatabase database) { var sequenceName = parser.ParseIdentifier(); var objectName = ParserUtils.GetObjectName(sequenceName); var schemaName = ParserUtils.GetSchemaName(sequenceName, database); if (database.SchemaIsIgnored(schemaName)) { return; } PgSequence sequence = database.GetSchema(schemaName).GetSequence(objectName); parser.Expect("IS"); sequence.Comment = GetComment(parser); parser.Expect(";"); }
private static void ParseSequence(Parser parser, PgSequence sequence, bool outputIgnoredStatements, string sequenceName, PgDatabase database) { while (!parser.ExpectOptional(";")) { if (parser.ExpectOptional("OWNER", "TO")) { if (outputIgnoredStatements) { database.IgnoredStatements.Add($"ALTER TABLE {sequenceName} OWNER TO {parser.ParseIdentifier()};"); } else { parser.ParseIdentifier(); } } else { parser.ThrowUnsupportedCommand(); } } }
/// <summary> /// Parses ALTER TABLE sequence. /// </summary> private static void ParseSequence(Parser parser, PgSequence sequence, bool outputIgnoredStatements, string sequenceName, PgDatabase database) { while (!parser.ExpectOptional(";")) { if (parser.ExpectOptional("OWNER", "TO")) { // we do not parse this one so we just consume the identifier if (outputIgnoredStatements) { database.AddIgnoredStatement("ALTER TABLE " + sequenceName + " OWNER TO " + parser.ParseIdentifier() + ';'); } else { parser.ParseIdentifier(); } } else { throw new TeamworkParserException("CannotParseStringUnsupportedCommand"); } } }
/// <summary> /// Outputs statement for modified sequences. /// </summary> public static void Alter(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper, bool ignoreStartWith) { if (oldSchema == null) { return; } var alterSequenceSql = new StringBuilder(100); foreach (PgSequence newSequence in newSchema.Sequences) { PgSequence oldSequence = oldSchema.GetSequence(newSequence.Name); if (oldSequence == null) { continue; } alterSequenceSql.Length = 0; var oldIncrement = oldSequence.Increment; var newIncrement = newSequence.Increment; if (newIncrement != null && !newIncrement.Equals(oldIncrement)) { alterSequenceSql.Append("\n\tINCREMENT BY "); alterSequenceSql.Append(newIncrement); } var oldMinValue = oldSequence.MinValue; var newMinValue = newSequence.MinValue; if (newMinValue == null && oldMinValue != null) { alterSequenceSql.Append("\n\tNO MINVALUE"); } else if (newMinValue != null && !newMinValue.Equals(oldMinValue)) { alterSequenceSql.Append("\n\tMINVALUE "); alterSequenceSql.Append(newMinValue); } var oldMaxValue = oldSequence.MaxValue; var newMaxValue = newSequence.MaxValue; if (newMaxValue == null && oldMaxValue != null) { alterSequenceSql.Append("\n\tNO MAXVALUE"); } else if (newMaxValue != null && !newMaxValue.Equals(oldMaxValue)) { alterSequenceSql.Append("\n\tMAXVALUE "); alterSequenceSql.Append(newMaxValue); } if (!ignoreStartWith) { var oldStart = oldSequence.StartWith; var newStart = newSequence.StartWith; if (newStart != null && !newStart.Equals(oldStart)) { alterSequenceSql.Append("\n\tRESTART WITH "); alterSequenceSql.Append(newStart); } } var oldCache = oldSequence.Cache; var newCache = newSequence.Cache; if (newCache != null && !newCache.Equals(oldCache)) { alterSequenceSql.Append("\n\tCACHE "); alterSequenceSql.Append(newCache); } var oldCycle = oldSequence.Cycle; var newCycle = newSequence.Cycle; if (oldCycle && !newCycle) { alterSequenceSql.Append("\n\tNO CYCLE"); } else if (!oldCycle && newCycle) { alterSequenceSql.Append("\n\tCYCLE"); } var oldOwnedBy = oldSequence.Owner; var newOwnedBy = newSequence.Owner; if (newOwnedBy != null && !newOwnedBy.Equals(oldOwnedBy)) { alterSequenceSql.Append("\n\tOWNED BY "); alterSequenceSql.Append(newOwnedBy); } if (alterSequenceSql.Length > 0) { searchPathHelper.OutputSearchPath(writer); writer.WriteLine(); writer.Write("ALTER SEQUENCE " + PgDiffStringExtension.QuoteName(newSequence.Name)); writer.Write(alterSequenceSql.ToString()); writer.WriteLine(';'); } if ((oldSequence.Comment == null && newSequence.Comment != null) || (oldSequence.Comment != null && newSequence.Comment != null && !oldSequence.Comment.Equals(newSequence.Comment))) { searchPathHelper.OutputSearchPath(writer); writer.WriteLine(); writer.Write("COMMENT ON SEQUENCE "); writer.Write(PgDiffStringExtension.QuoteName(newSequence.Name)); writer.Write(" IS "); writer.Write(newSequence.Comment); writer.WriteLine(';'); } else if (oldSequence.Comment != null && newSequence.Comment == null) { searchPathHelper.OutputSearchPath(writer); writer.WriteLine(); writer.Write("COMMENT ON SEQUENCE "); writer.Write(newSequence.Name); writer.WriteLine(" IS NULL;"); } } }
public static void Parse(PgDatabase database, string statement) { var parser = new Parser(statement); parser.Expect("CREATE", "SEQUENCE"); var sequenceName = parser.ParseIdentifier(); var sequence = new PgSequence(ParserUtils.GetObjectName(sequenceName)); var schemaName = ParserUtils.GetSchemaName(sequenceName, database); var schema = database.GetSchema(schemaName); if (schema == null) { throw new Exception(string.Format(Resources.CannotFindSchema, schemaName, statement)); } schema.AddSequence(sequence); while (!parser.ExpectOptional(";")) { if (parser.ExpectOptional("INCREMENT")) { parser.ExpectOptional("BY"); sequence.Increment = parser.ParseString(); } else if (parser.ExpectOptional("MINVALUE")) { sequence.MinValue = parser.ParseString(); } else if (parser.ExpectOptional("MAXVALUE")) { sequence.MaxValue = parser.ParseString(); } else if (parser.ExpectOptional("START")) { parser.ExpectOptional("WITH"); sequence.StartWith = parser.ParseString(); } else if (parser.ExpectOptional("CACHE")) { sequence.Cache = parser.ParseString(); } else if (parser.ExpectOptional("CYCLE")) { sequence.Cycle = true; } else if (parser.ExpectOptional("OWNED", "BY")) { sequence.OwnedBy = parser.ExpectOptional("NONE") ? null : ParserUtils.GetObjectName(parser.ParseIdentifier()); } else if (parser.ExpectOptional("NO")) { if (parser.ExpectOptional("MINVALUE")) { sequence.MinValue = null; } else if (parser.ExpectOptional("MAXVALUE")) { sequence.MaxValue = null; } else if (parser.ExpectOptional("CYCLE")) { sequence.Cycle = false; } else { parser.ThrowUnsupportedCommand(); } } else { parser.ThrowUnsupportedCommand(); } } }
/// <summary> /// Parses ALTER TABLE statement. /// </summary> public static void Parse(PgDatabase database, string statement, bool outputIgnoredStatements) { var parser = new Parser(statement); parser.Expect("ALTER", "TABLE"); parser.ExpectOptional("ONLY"); var tableName = parser.ParseIdentifier(); var schemaName = ParserUtils.GetSchemaName(tableName, database); if (database.SchemaIsIgnored(schemaName)) { return; } PgSchema schema = database.GetSchema(schemaName); if (schema == null) { throw new TeamworkParserException($"CannotFindSchema {schemaName} from {statement}"); } var objectName = ParserUtils.GetObjectName(tableName); PgTable table = schema.GetTable(objectName); if (table == null) { PgView view = schema.GetView(objectName); if (view != null) { ParseView(parser, view, outputIgnoredStatements, tableName, database); return; } PgSequence sequence = schema.GetSequence(objectName); if (sequence != null) { ParseSequence(parser, sequence, outputIgnoredStatements, tableName, database); return; } throw new TeamworkParserException($"CannotFindObject in {tableName} from {statement}"); } while (!parser.ExpectOptional(";")) { if (parser.ExpectOptional("ALTER")) { ParseAlterColumn(parser, table); } else if (parser.ExpectOptional("CLUSTER", "ON")) { table.ClusterIndexName = ParserUtils.GetObjectName(parser.ParseIdentifier()); } else if (parser.ExpectOptional("OWNER", "TO")) { // we do not parse this one so we just consume the identifier if (outputIgnoredStatements) { database.AddIgnoredStatement("ALTER TABLE " + tableName + " OWNER TO " + parser.ParseIdentifier() + ';'); } else { parser.ParseIdentifier(); } } else if (parser.ExpectOptional("ADD")) { if (parser.ExpectOptional("FOREIGN", "KEY")) { ParseAddForeignKey(parser, table); } else if (parser.ExpectOptional("CONSTRAINT")) { ParseAddConstraint(parser, table, schema); } else { throw new TeamworkParserException("CannotParseStringUnsupportedCommand"); } } else if (parser.ExpectOptional("ENABLE")) { ParseEnable(parser, outputIgnoredStatements, tableName, database); } else if (parser.ExpectOptional("DISABLE")) { ParseDisable(parser, outputIgnoredStatements, tableName, database); } else if (parser.ExpectOptional("REPLICA IDENTITY")) { parser.Expect("NOTHING"); } else { throw new TeamworkParserException("CannotParseStringUnsupportedCommand"); } if (parser.ExpectOptional(";")) { break; } else { parser.Expect(","); } } }
/// <summary> /// Parses CREATE SEQUENCE statement. /// </summary> public static void Parse(PgDatabase database, string statement) { var parser = new Parser(statement); parser.Expect("CREATE", "SEQUENCE"); var sequenceName = parser.ParseIdentifier(); var sequence = new PgSequence(ParserUtils.GetObjectName(sequenceName)); var schemaName = ParserUtils.GetSchemaName(sequenceName, database); if (database.SchemaIsIgnored(schemaName)) { return; } PgSchema schema = database.GetSchema(schemaName); if (schema == null) { throw new TeamworkParserException($"CannotFindSchema {schemaName} from {statement}"); } schema.Add(sequence); while (!parser.ExpectOptional(";")) { if (parser.ExpectOptional("INCREMENT")) { parser.ExpectOptional("BY"); #pragma warning disable CS0618 // Type or member is obsolete sequence.Increment = parser.ParseStringCompat(); #pragma warning restore CS0618 // Type or member is obsolete } else if (parser.ExpectOptional("MINVALUE")) { #pragma warning disable CS0618 // Type or member is obsolete sequence.MinValue = parser.ParseStringCompat(); #pragma warning restore CS0618 // Type or member is obsolete } else if (parser.ExpectOptional("MAXVALUE")) { #pragma warning disable CS0618 // Type or member is obsolete sequence.MaxValue = parser.ParseStringCompat(); #pragma warning restore CS0618 // Type or member is obsolete } else if (parser.ExpectOptional("START")) { parser.ExpectOptional("WITH"); #pragma warning disable CS0618 // Type or member is obsolete sequence.StartWith = parser.ParseStringCompat(); #pragma warning restore CS0618 // Type or member is obsolete } else if (parser.ExpectOptional("CACHE")) { #pragma warning disable CS0618 // Type or member is obsolete sequence.Cache = parser.ParseStringCompat(); #pragma warning restore CS0618 // Type or member is obsolete } else if (parser.ExpectOptional("CYCLE")) { sequence.Cycle = true; } else if (parser.ExpectOptional("OWNED", "BY")) { if (parser.ExpectOptional("NONE")) { sequence.Owner = null; } else { sequence.Owner = ParserUtils.GetObjectName(parser.ParseIdentifier()); } } else if (parser.ExpectOptional("NO")) { if (parser.ExpectOptional("MINVALUE")) { sequence.MinValue = null; } else if (parser.ExpectOptional("MAXVALUE")) { sequence.MaxValue = null; } else if (parser.ExpectOptional("CYCLE")) { sequence.Cycle = false; } else { throw new TeamworkParserException("CannotParseStringUnsupportedCommand"); } } else { throw new TeamworkParserException("CannotParseStringUnsupportedCommand"); } } }