Skip to content

Commit

Permalink
feat: support include columns in create index for pg
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Nov 22, 2024
1 parent 9ad28d7 commit 8f8d8b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,16 @@ create_sequence_definition_list
return createList(head, tail, 1)
}

include_column
= k:'INCLUDE'i __ LPAREN __ c:column_list __ RPAREN {
// => { type: 'include', keyword: 'include', columns: column_list }
return {
type: k.toLowerCase(),
keyword: k.toLowerCase(),
columns:c,
}
}

create_index_stmt
= a:KW_CREATE __
kw:KW_UNIQUE? __
Expand All @@ -1068,6 +1078,7 @@ create_index_stmt
ta:table_name __
um:index_type? __
LPAREN __ cols:column_order_list __ RPAREN __
include:include_column? __
wr:(KW_WITH __ LPAREN __ index_options_list __ RPAREN)? __
ts:(KW_TABLESPACE __ ident_name)? __
w:where_clause? __ {
Expand All @@ -1083,6 +1094,7 @@ create_index_stmt
table: table_name;
index_using?: index_type;
index_columns: column_order[];
include?: column_list_items;
with?: index_option[];
with_before_where: true;
tablespace?: {type: 'origin'; value: string; }
Expand All @@ -1104,6 +1116,7 @@ create_index_stmt
table: ta,
index_using: um,
index_columns: cols,
include,
with: wr && wr[4],
with_before_where: true,
tablespace: ts && { type: 'origin', value: ts[2] },
Expand Down
2 changes: 1 addition & 1 deletion src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function createIndexToSQL(stmt) {
with: withExpr, with_before_where: withBeforeWhere,
} = stmt
const withIndexOpt = withExpr && `WITH (${indexOptionListToSQL(withExpr).join(', ')})`
const includeColumns = include && `${toUpper(include.keyword)} (${include.columns.map(col => identifierToSql(col)).join(', ')})`
const includeColumns = include && `${toUpper(include.keyword)} (${include.columns.map(col => (typeof col === 'string' ? identifierToSql(col) : exprToSQL(col))).join(', ')})`
let indexName = index
if (index) {
indexName = typeof index === 'string' ? identifierToSql(index) : [identifierToSql(index.schema), identifierToSql(index.name)].filter(hasVal).join('.')
Expand Down
7 changes: 7 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,13 @@ describe('Postgres', () => {
'CREATE UNIQUE INDEX IF NOT EXISTS "public_i_locations_pkey" ON "public"."i_locations" (id)'
]
},
{
title: 'create index with include clause',
sql: [
'CREATE INDEX ON tableName (supplier, amount) INCLUDE(id);',
'CREATE INDEX ON "tableName" (supplier, amount) INCLUDE (id)'
]
},
]
function neatlyNestTestedSQL(sqlList){
sqlList.forEach(sqlInfo => {
Expand Down

0 comments on commit 8f8d8b2

Please sign in to comment.