/// <summary> /// Parses COMMENT ON INDEX. /// </summary> private static void ParseIndex(Parser parser, PgDatabase database) { var indexName = parser.ParseIdentifier(); var objectName = ParserUtils.GetObjectName(indexName); var schemaName = ParserUtils.GetSchemaName(indexName, database); if (database.SchemaIsIgnored(schemaName)) { return; } PgSchema schema = database.GetSchema(schemaName); PgIndex index = schema.GetIndex(objectName); if (index == null) { PgConstraint primaryKey = schema.GetPrimaryKey(objectName); parser.Expect("IS"); primaryKey.Comment = GetComment(parser); parser.Expect(";"); } else { parser.Expect("IS"); index.Comment = GetComment(parser); parser.Expect(";"); } }
/// <summary> /// Parses CREATE INDEX statement. /// </summary> public static void Parse(PgDatabase database, string statement) { var parser = new Parser(statement); parser.Expect("CREATE"); var unique = parser.ExpectOptional("UNIQUE"); parser.Expect("INDEX"); parser.ExpectOptional("CONCURRENTLY"); var indexName = ParserUtils.GetObjectName(parser.ParseIdentifier()); parser.Expect("ON"); var tableName = parser.ParseIdentifier(); var definition = parser.Rest(); 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) { throw new TeamworkParserException($"CannotFindTable {tableName} from {statement}"); } var index = new PgIndex(indexName); table.AddIndex(index); schema.Add(index); index.Definition = definition.Trim(); index.TableName = table.Name; index.Unique = unique; }
public static void Parse(PgDatabase database, string statement) { var parser = new Parser(statement); parser.Expect("CREATE"); var unique = parser.ExpectOptional("UNIQUE"); parser.Expect("INDEX"); parser.ExpectOptional("CONCURRENTLY"); var indexName = ParserUtils.GetObjectName(parser.ParseIdentifier()); parser.Expect("ON"); var tableName = parser.ParseIdentifier(); var definition = parser.GetRest(); var schemaName = ParserUtils.GetSchemaName(tableName, database); var schema = database.GetSchema(schemaName); if (schema == null) { throw new Exception(string.Format(Resources.CannotFindSchema, schemaName, statement)); } var objectName = ParserUtils.GetObjectName(tableName); var table = schema.GetTable(objectName); if (table == null) { throw new Exception(string.Format(Resources.CannotFindTable, tableName, statement)); } var index = new PgIndex(indexName); table.AddIndex(index); schema.AddIndex(index); index.Definition = definition.Trim(); index.TableName = table.Name; index.Unique = unique; }
/// <summary> /// Outputs statements for index comments that have changed. /// </summary> public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper) { if (oldSchema == null) { return; } foreach (PgIndex oldIndex in oldSchema.Indexes) { PgIndex newIndex = newSchema.GetIndex(oldIndex.Name); if (newIndex == null) { continue; } if ((oldIndex.Comment == null && newIndex.Comment != null) || (oldIndex.Comment != null && newIndex.Comment != null && !oldIndex.Comment.Equals(newIndex.Comment))) { searchPathHelper.OutputSearchPath(writer); writer.WriteLine(); writer.Write("COMMENT ON INDEX "); writer.Write(PgDiffStringExtension.QuoteName(newIndex.Name)); writer.Write(" IS "); writer.Write(newIndex.Comment); writer.WriteLine(';'); } else if (oldIndex.Comment != null && newIndex.Comment == null) { searchPathHelper.OutputSearchPath(writer); writer.WriteLine(); writer.Write("COMMENT ON INDEX "); writer.Write(PgDiffStringExtension.QuoteName(newIndex.Name)); writer.WriteLine(" IS NULL;"); } } }
internal static Collection <PgIndex> GetIndices(PgTable pgTable) { Collection <PgIndex> indices = new Collection <PgIndex>(); string sql = FileHelper.ReadSqlResource("indices.sql"); using (NpgsqlCommand command = new NpgsqlCommand(sql)) { command.Parameters.AddWithValue("@SchemaName", pgTable.SchemaName); command.Parameters.AddWithValue("@TableName", pgTable.Name); using (DataTable table = DbOperation.GetDataTable(command)) { if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { PgIndex index = new PgIndex { SchemaName = pgTable.SchemaName, TableName = pgTable.Name, Name = Conversion.TryCastString(row["index_name"]), Owner = Conversion.TryCastString(row["owner"]), Type = Conversion.TryCastString(row["type"]), AccessMethod = Conversion.TryCastString(row["access_method"]), Definition = Conversion.TryCastString(row["definition"]), IsClustered = Conversion.TryCastBoolean(row["is_clustered"]), IsValid = Conversion.TryCastBoolean(row["is_valid"]), Description = Conversion.TryCastString(row["description"]) }; indices.Add(index); } } } } return(indices); }