sql-parser-cst icon indicating copy to clipboard operation
sql-parser-cst copied to clipboard

Postgres support (user defined types)

Open nene opened this issue 1 year ago • 2 comments

Domains

  • [x] CREATE DOMAIN
    • [x] name [AS] data_type
    • [x] COLLATE collation
    • [x] DEFAULT expression
    • [x] domain_constraint [ ... ]:
      • [x] CONSTRAINT constraint_name
      • [x] NOT NULL
      • [x] NULL
      • [x] CHECK (expression)
  • [x] DROP DOMAIN
    • [x] IF EXISTS
    • [x] name, ...
    • [x] CASCADE | RESTRICT
  • [x] ALTER DOMAIN
    • [x] SET DEFAULT expression | DROP DEFAULT
    • [x] { SET | DROP } NOT NULL
    • [x] ADD domain_constraint
      • [x] CONSTRAINT name
      • [x] CHECK (...)
      • [x] NOT NULL
      • [x] [ NOT VALID ]
    • [x] DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
    • [x] RENAME CONSTRAINT constraint_name TO new_constraint_name
    • [x] VALIDATE CONSTRAINT constraint_name
    • [x] OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
    • [x] RENAME TO new_name
    • [x] SET SCHEMA new_schema

Types

  • [ ] CREATE TYPE
    • [x] AS ( attribute_name data_type [ COLLATE collation ] [, ... ] )
    • [x] AS ENUM ( 'label' [, ... ] )
    • [ ] AS RANGE ( ... ):
      • [ ] SUBTYPE = subtype
      • [ ] SUBTYPE_OPCLASS = subtype_operator_class
      • [ ] COLLATION = collation
      • [ ] CANONICAL = canonical_function
      • [ ] SUBTYPE_DIFF = subtype_diff_function
      • [ ] MULTIRANGE_TYPE_NAME = multirange_type_name
    • [ ] ( ... ):
      • [ ] INPUT = input_function
      • [ ] OUTPUT = output_function
      • [ ] RECEIVE = receive_function
      • [ ] SEND = send_function
      • [ ] TYPMOD_IN = type_modifier_input_function
      • [ ] TYPMOD_OUT = type_modifier_output_function
      • [ ] ANALYZE = analyze_function
      • [ ] SUBSCRIPT = subscript_function
      • [ ] INTERNALLENGTH = { internallength | VARIABLE }
      • [ ] PASSEDBYVALUE
      • [ ] ALIGNMENT = alignment
      • [ ] STORAGE = storage
      • [ ] LIKE = like_type
      • [ ] CATEGORY = category
      • [ ] PREFERRED = preferred
      • [ ] ELEMENT = element
      • [ ] DELIMITER = delimiter
      • [ ] COLLATABLE = collatable
    • [x] CREATE TYPE name;
  • [x] DROP TYPE
    • [x] IF EXISTS
    • [x] name, ...
    • [x] CASCADE | RESTRICT
  • [ ] ALTER TYPE
    • [x] OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
    • [x] RENAME TO new_name
    • [x] SET SCHEMA new_schema
    • [x] RENAME ATTRIBUTE attribute_name TO new_attribute_name
      • [x] CASCADE | RESTRICT
    • [x] action, ...:
      • [x] ADD ATTRIBUTE attribute_name data_type
        • [x] COLLATE collation
        • [x] CASCADE | RESTRICT
      • [x] DROP ATTRIBUTE attribute_name
        • [x] IF EXISTS
        • [x] CASCADE | RESTRICT
      • [x] ALTER ATTRIBUTE attribute_name
        • [x] [ SET DATA ] TYPE data_type
        • [x] COLLATE collation
        • [x] CASCADE | RESTRICT
    • [x] ADD VALUE new_enum_value
      • [x] IF NOT EXISTS
      • [x] { BEFORE | AFTER } neighbor_enum_value
    • [x] RENAME VALUE existing_enum_value TO new_enum_value
    • [ ] SET ( property = value [, ... ] )

nene avatar Apr 15 '24 09:04 nene

Thanks for documenting this! I would be glad to help out if I can figure out how. If you have any time and is convenient for you, can you point me to where I can begin?

maheshsundaram avatar Apr 16 '24 06:04 maheshsundaram

I guess you can look at existing commits to see how. This one might be a good start: https://github.com/nene/sql-parser-cst/commit/00adcae78bc799bb6aaa0b146b0d7aba9ff89590

The trickiest bit is figuring out the structure of the resulting syntax tree. For CREATE TYPE I guess it should be something like:

export interface CreateTypeStmt extends BaseNode {
  type: "create_type_stmt";
  createKw: Keyword<"CREATE">;
  typeKw: Keyword<"TYPE">;
  name: EntityName;
  // ENUM definition is probably the simplest, though others should fit here as well
  definition?: AsClause<EnumTypeDefinition>;
}

export interface EnumTypeDefinition extends BaseNode {
  type: "enum_type_definition";
  enumKw: Keyword<"ENUM">;
  values: ParenExpr<ListExpr<StringLiteral>>;
}

nene avatar Apr 16 '24 07:04 nene