Skip to content

Commit

Permalink
feat: support create table option in sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Oct 13, 2024
1 parent 000c336 commit 0226ab3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
32 changes: 19 additions & 13 deletions pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,12 @@ create_view_stmt

create_table_stmt
= a:KW_CREATE __
tp:KW_TEMPORARY? __
tp:(KW_TEMPORARY / KW_TEMP)? __
KW_TABLE __
ife:if_not_exists_stmt? __
t:table_name __
c:create_table_definition? __
to:table_options? __
ir: (KW_IGNORE / KW_REPLACE)? __
as: KW_AS? __
qe: union_stmt? {
c:create_table_definition __
to:table_options? {
if(t) tableList.add(`create::${t.db}::${t.table}`)
return {
tableList: Array.from(tableList),
Expand All @@ -436,20 +433,18 @@ create_table_stmt
temporary: tp && tp[0].toLowerCase(),
if_not_exists:ife,
table: [t],
ignore_replace: ir && ir[0].toLowerCase(),
as: as && as[0].toLowerCase(),
query_expr: qe && qe.ast,
create_definitions: c,
table_options: to
}
}
}
/ a:KW_CREATE __
tp:KW_TEMPORARY? __
tp:(KW_TEMPORARY / KW_TEMP)? __
KW_TABLE __
ife:if_not_exists_stmt? __
t:table_name __
lt:create_like_table {
as:KW_AS __
qe:select_stmt {
if(t) tableList.add(`create::${t.db}::${t.table}`)
return {
tableList: Array.from(tableList),
Expand All @@ -458,9 +453,10 @@ create_table_stmt
type: a[0].toLowerCase(),
keyword: 'table',
temporary: tp && tp[0].toLowerCase(),
if_not_exists:ife,
if_not_exists: ife,
table: [t],
like: lt
as: 'as',
query_expr: qe,
}
}
}
Expand Down Expand Up @@ -1144,6 +1140,16 @@ table_option
value: c.toUpperCase()
}
}
/ 'WITHOUT'i __ 'ROWID'i {
return {
keyword: 'without rowid'
}
}
/ 'STRICT'i {
return {
keyword: 'strict'
}
}


rename_stmt
Expand Down
7 changes: 6 additions & 1 deletion src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
commentToSQL,
commonTypeValue,
dataTypeToSQL,
getParserOpt,
toUpper,
hasVal,
identifierToSql,
Expand Down Expand Up @@ -85,7 +86,11 @@ function createTableToSQL(stmt) {
}
if (partitionOf) return sql.concat([createTablePartitionOfToSQL(partitionOf)]).filter(hasVal).join(' ')
if (createDefinition) sql.push(`(${createDefinition.map(createDefinitionToSQL).join(', ')})`)
if (tableOptions) sql.push(tableOptions.map(tableOptionToSQL).join(' '))
if (tableOptions) {
const { database } = getParserOpt()
const symbol = database && database.toLowerCase() === 'sqlite' ? ', ' : ' '
sql.push(tableOptions.map(tableOptionToSQL).join(symbol))
}
if (withExpr) {
const withSQL = withExpr.map(withExprItem => [literalToSQL(withExprItem.keyword), toUpper(withExprItem.symbol), literalToSQL(withExprItem.value)].join(' ')).join(', ')
sql.push(`WITH (${withSQL})`)
Expand Down
2 changes: 1 addition & 1 deletion src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function tableOptionToSQL(tableOption) {
break
}
sql.push(val)
return sql.join(' ')
return sql.filter(hasVal).join(' ')
}

export {
Expand Down
8 changes: 4 additions & 4 deletions test/sqlite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ describe('sqlite', () => {
let sql = `CREATE TABLE IF NOT EXISTS posts (
user_id INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
) WITHOUT ROWID;
`
expect(getParsedSql(sql)).to.be.equal('CREATE TABLE IF NOT EXISTS "posts" ("user_id" INTEGER NOT NULL, FOREIGN KEY ("user_id") REFERENCES "users" ("user_id"))')
sql = 'CREATE TABLE users (age INTEGER CHECK(age >= 18));'
expect(getParsedSql(sql)).to.be.equal('CREATE TABLE "users" ("age" INTEGER CHECK ("age" >= 18))')
expect(getParsedSql(sql)).to.be.equal('CREATE TABLE IF NOT EXISTS "posts" ("user_id" INTEGER NOT NULL, FOREIGN KEY ("user_id") REFERENCES "users" ("user_id")) WITHOUT ROWID')
sql = 'CREATE TABLE users (age INTEGER CHECK(age >= 18)) STRICT, WITHOUT ROWID;'
expect(getParsedSql(sql)).to.be.equal('CREATE TABLE "users" ("age" INTEGER CHECK ("age" >= 18)) STRICT, WITHOUT ROWID')
sql = 'ALTER TABLE customers RENAME COLUMN age TO customer_age;'
expect(getParsedSql(sql)).to.be.equal('ALTER TABLE "customers" RENAME COLUMN "age" TO "customer_age"')
})
Expand Down

0 comments on commit 0226ab3

Please sign in to comment.