|
1 | | -import { DataType } from "./data-type"; |
2 | | -import type { Expr, ExprWithAlias } from "./expr"; |
| 1 | +import { ColumnDef, CommentDef, DataType, NullsDistinctOption, SqlOption } from "./data-type"; |
| 2 | +import type { Expr, ExprWithAlias, OneOrManyWithParens } from "./expr"; |
3 | 3 | import { FunctionArg, SQLFunction } from "./function"; |
4 | 4 | import type { Ident, ObjectName } from "./ident"; |
5 | 5 | import type { AttachedToken, Value } from "./token"; |
@@ -42,7 +42,7 @@ export type Statement = { |
42 | 42 | Open?: unknown; |
43 | 43 | Close?: unknown; |
44 | 44 | CreateView?: unknown; |
45 | | - CreateTable?: unknown; |
| 45 | + CreateTable?: CreateTable; |
46 | 46 | CreateVirtualTable?: unknown; |
47 | 47 | CreateIndex?: unknown; |
48 | 48 | CreateRole?: unknown; |
@@ -139,6 +139,303 @@ export type Statement = { |
139 | 139 | Vacuum?: unknown; |
140 | 140 | } |
141 | 141 |
|
| 142 | +/** |
| 143 | + * SQL `CREATE TABLE` statement. |
| 144 | + * |
| 145 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/struct.CreateTable.html |
| 146 | + */ |
| 147 | +export interface CreateTable { |
| 148 | + or_replace: boolean, |
| 149 | + temporary: boolean, |
| 150 | + external: boolean, |
| 151 | + dynamic: boolean, |
| 152 | + global?: boolean, |
| 153 | + if_not_exists: boolean, |
| 154 | + transient: boolean, |
| 155 | + volatile: boolean, |
| 156 | + iceberg: boolean, |
| 157 | + name: ObjectName, |
| 158 | + columns: ColumnDef[], |
| 159 | + constraints: TableConstraint[], |
| 160 | + hive_distribution: unknown, // TODO: Define proper type |
| 161 | + hive_formats?: unknown, // TODO: Define proper type |
| 162 | + table_options: CreateTableOptions, |
| 163 | + file_format?: FileFormat, |
| 164 | + location?: String, |
| 165 | + query?: Query, |
| 166 | + without_rowid: boolean, |
| 167 | + like?: CreateTableLikeKind, |
| 168 | + clone?: ObjectName, |
| 169 | + version?: TableVersion, |
| 170 | + comment?: CommentDef, |
| 171 | + on_commit?: OnCommit, |
| 172 | + on_cluster?: Ident, |
| 173 | + primary_key?: Expr, |
| 174 | + order_by?: OneOrManyWithParens<Expr>, |
| 175 | + partition_by?: Expr, |
| 176 | + cluster_by?: WrappedCollection<Expr>, |
| 177 | + clustered_by?: ClusteredBy, |
| 178 | + inherits?: ObjectName, |
| 179 | + strict: boolean, |
| 180 | + copy_grants: boolean, |
| 181 | + enable_schema_evolution?: boolean, |
| 182 | + change_tracking?: boolean, |
| 183 | + data_retention_time_in_days?: number, |
| 184 | + max_data_extension_time_in_days?: number, |
| 185 | + default_ddl_collation?: String, |
| 186 | + with_aggregation_policy?: ObjectName, |
| 187 | + with_row_access_policy?: RowAccessPolicy, |
| 188 | + with_tags?: Tag, |
| 189 | + external_volume?: String, |
| 190 | + base_location?: String, |
| 191 | + catalog?: String, |
| 192 | + catalog_sync?: String, |
| 193 | + storage_serialization_policy?: StorageSerializationPolicy, |
| 194 | + target_lag?: String, |
| 195 | + warehouse?: Ident, |
| 196 | + refresh_mode?: RefreshModeKind, |
| 197 | + initialize?: InitializeKind, |
| 198 | + require_user: boolean, |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Specifies how to create a new table based on an existing table’s schema. |
| 203 | + * ```sql |
| 204 | + * CREATE TABLE new LIKE old … |
| 205 | + * ``` |
| 206 | + */ |
| 207 | +export type CreateTableLikeKind = { |
| 208 | + /** |
| 209 | + * ```sql |
| 210 | + * CREATE TABLE new (LIKE old …) |
| 211 | + * ``` |
| 212 | + * Redshift |
| 213 | + */ |
| 214 | + Parenthesized?: CreateTableLike, |
| 215 | + |
| 216 | + /** |
| 217 | + * ```sql |
| 218 | + * CREATE TABLE new LIKE old … |
| 219 | + * ``` |
| 220 | + */ |
| 221 | + Plain?: CreateTableLike, |
| 222 | +} |
| 223 | + |
| 224 | +/** |
| 225 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/struct.CreateTableLike.html |
| 226 | + */ |
| 227 | +export interface CreateTableLike { |
| 228 | + name: ObjectName; |
| 229 | + defaults?: unknown; // TODO: Define proper type |
| 230 | +} |
| 231 | + |
| 232 | +/** |
| 233 | + * Sql options of a `CREATE TABLE` statement. |
| 234 | + * |
| 235 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.CreateTableOptions.html |
| 236 | + */ |
| 237 | +export type CreateTableOptions = 'None' | { |
| 238 | + /** |
| 239 | + * Options specified using the `WITH` keyword. e.g. `WITH (description = "123")` |
| 240 | + * |
| 241 | + * @see https://www.postgresql.org/docs/current/sql-createtable.html |
| 242 | + * |
| 243 | + * MSSQL supports more specific options that’s not only key-value pairs. |
| 244 | + * |
| 245 | + * `WITH ( DISTRIBUTION = ROUND_ROBIN, CLUSTERED INDEX (column_a DESC, column_b) )` |
| 246 | + * |
| 247 | + * @see https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-azure-sql-data-warehouse?view=aps-pdw-2016-au7#syntax |
| 248 | + */ |
| 249 | + With?: SqlOption[], |
| 250 | + |
| 251 | + /** |
| 252 | + * Options specified using the `OPTIONS` keyword. e.g. `OPTIONS(description = "123")` |
| 253 | + * |
| 254 | + * @see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list |
| 255 | + */ |
| 256 | + Options?: SqlOption[], |
| 257 | + |
| 258 | + /** |
| 259 | + * Plain options, options which are not part on any declerative statement e.g. `WITH`/`OPTIONS`/… |
| 260 | + * |
| 261 | + * @see https://dev.mysql.com/doc/refman/8.4/en/create-table.html |
| 262 | + */ |
| 263 | + Plain?: SqlOption[], |
| 264 | + TableProperties?: SqlOption[], |
| 265 | +}; |
| 266 | + |
| 267 | +/** |
| 268 | + * A table-level constraint, specified in a `CREATE TABLE` or an `ALTER TABLE ADD <constraint>` statement. |
| 269 | + * |
| 270 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.TableConstraint.html |
| 271 | + */ |
| 272 | +export interface TableConstraint { |
| 273 | + Unique?: { |
| 274 | + name?: Ident, |
| 275 | + index_name?: Ident, |
| 276 | + index_type_display: KeyOrIndexDisplay, |
| 277 | + index_type?: IndexType, |
| 278 | + columns: IndexColumn[], |
| 279 | + index_options: IndexOption[], |
| 280 | + characteristics?: ConstraintCharacteristics, |
| 281 | + nulls_distinct: NullsDistinctOption, |
| 282 | + }, |
| 283 | + PrimaryKey?: { |
| 284 | + name?: Ident, |
| 285 | + index_name?: Ident, |
| 286 | + index_type?: IndexType, |
| 287 | + columns: IndexColumn[], |
| 288 | + index_options: IndexOption[], |
| 289 | + characteristics?: ConstraintCharacteristics, |
| 290 | + }, |
| 291 | + ForeignKey?: { |
| 292 | + name?: Ident, |
| 293 | + index_name?: Ident, |
| 294 | + columns: Ident[], |
| 295 | + foreign_table: ObjectName, |
| 296 | + referred_columns: Ident[], |
| 297 | + on_delete?: ReferentialAction, |
| 298 | + on_update?: ReferentialAction, |
| 299 | + characteristics?: ConstraintCharacteristics, |
| 300 | + }, |
| 301 | + Check?: { |
| 302 | + name?: Ident, |
| 303 | + expr: Expr, |
| 304 | + enforced?: boolean, |
| 305 | + }, |
| 306 | + Index?: { |
| 307 | + display_as_key: boolean, |
| 308 | + name?: Ident, |
| 309 | + index_type?: IndexType, |
| 310 | + columns: IndexColumn[], |
| 311 | + index_options: IndexOption[], |
| 312 | + }, |
| 313 | + FulltextOrSpatial?: { |
| 314 | + fulltext: boolean, |
| 315 | + index_type_display: KeyOrIndexDisplay, |
| 316 | + opt_index_name?: Ident, |
| 317 | + columns: IndexColumn[], |
| 318 | + }, |
| 319 | +} |
| 320 | + |
| 321 | +/** |
| 322 | + * External table’s available file format |
| 323 | + * |
| 324 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.FileFormat.html |
| 325 | + */ |
| 326 | +export type FileFormat = |
| 327 | + | 'TEXTFILE' |
| 328 | + | 'SEQUENCEFILE' |
| 329 | + | 'ORC' |
| 330 | + | 'PARQUET' |
| 331 | + | 'AVRO' |
| 332 | + | 'RCFILE' |
| 333 | + | 'JSONFILE'; |
| 334 | + |
| 335 | +/** |
| 336 | + * `<constraint_characteristics> = [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]` |
| 337 | + * |
| 338 | + * Used in UNIQUE and foreign key constraints. The individual settings may occur in any order. |
| 339 | + * |
| 340 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/struct.ConstraintCharacteristics.html |
| 341 | + */ |
| 342 | +export interface ConstraintCharacteristics { |
| 343 | + /** |
| 344 | + * `[ DEFERRABLE | NOT DEFERRABLE ]` |
| 345 | + */ |
| 346 | + deferrable?: boolean, |
| 347 | + |
| 348 | + /** |
| 349 | + * `[ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]` |
| 350 | + */ |
| 351 | + initially?: DeferrableInitial, |
| 352 | + |
| 353 | + /** |
| 354 | + * `[ ENFORCED | NOT ENFORCED ]` |
| 355 | + */ |
| 356 | + enforced?: boolean, |
| 357 | +} |
| 358 | + |
| 359 | +/** |
| 360 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.DeferrableInitial.html |
| 361 | + */ |
| 362 | +export type DeferrableInitial = 'Immediate' | 'Deferred'; |
| 363 | + |
| 364 | +/** |
| 365 | + * `<referential_action> = { RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }` |
| 366 | + * |
| 367 | + * Used in foreign key constraints in ON UPDATE and ON DELETE options. |
| 368 | + * |
| 369 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.ReferentialAction.html |
| 370 | + */ |
| 371 | +export type ReferentialAction = |
| 372 | + | 'Restrict' |
| 373 | + | 'Cascade' |
| 374 | + | 'SetNull' |
| 375 | + | 'NoAction' |
| 376 | + | 'SetDefault'; |
| 377 | + |
| 378 | +/** |
| 379 | + * Representation whether a definition can can contains the KEY or INDEX keywords with the same meaning. |
| 380 | + * |
| 381 | + * This enum initially is directed to `FULLTEXT`,`SPATIAL`, and `UNIQUE` indexes on create table statements of MySQL [(1)]. |
| 382 | + * |
| 383 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.KeyOrIndexDisplay.html |
| 384 | + */ |
| 385 | +export type KeyOrIndexDisplay = |
| 386 | + | 'None' |
| 387 | + | 'Key' |
| 388 | + | 'Index'; |
| 389 | + |
| 390 | +/** |
| 391 | + * Indexing method used by that index. |
| 392 | + * |
| 393 | + * This structure isn’t present on ANSI, but is found at least in MySQL `CREATE TABLE`, MySQL `CREATE INDEX`, and Postgresql `CREATE INDEX` statements. |
| 394 | + */ |
| 395 | +export type IndexType = |
| 396 | + | 'BTree' |
| 397 | + | 'Hash' |
| 398 | + | 'GIN' |
| 399 | + | 'GiST' |
| 400 | + | 'SPGiST' |
| 401 | + | 'BRIN' |
| 402 | + | 'Bloom' |
| 403 | + | { |
| 404 | + /** |
| 405 | + * Users may define their own index types, which would not be covered by the above variants. |
| 406 | + */ |
| 407 | + Custom: Ident |
| 408 | + }; |
| 409 | + |
| 410 | +/** |
| 411 | + * Index column type. |
| 412 | + * |
| 413 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/struct.IndexColumn.html |
| 414 | + */ |
| 415 | +export interface IndexColumn { |
| 416 | + column: OrderByExpr; |
| 417 | + operator_class?: Ident; |
| 418 | +} |
| 419 | + |
| 420 | +/** |
| 421 | + * MySQL index option, used in `CREATE TABLE`, `CREATE INDEX`, and `ALTER TABLE`. |
| 422 | + * |
| 423 | + * @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.IndexOption.html |
| 424 | + */ |
| 425 | +export type IndexOption = { |
| 426 | + /** |
| 427 | + * `USING { BTREE | HASH }`: Index type to use for the index. |
| 428 | + * Note that we permissively parse non-MySQL index types, like `GIN`. |
| 429 | + */ |
| 430 | + Using?: IndexType; |
| 431 | + |
| 432 | + /** |
| 433 | + * `COMMENT 'string'`: Specifies a comment for the index. |
| 434 | + */ |
| 435 | + Comment?: string; |
| 436 | +} |
| 437 | + |
| 438 | + |
142 | 439 | /** |
143 | 440 | * Target of a `TRUNCATE TABLE` command. |
144 | 441 | * Note this is its own struct because `visit_relation` requires an `ObjectName` (not a `ObjectName[]`) |
|
0 commit comments