Skip to content

Commit 3b67fae

Browse files
Merge pull request #1968 from taozhi8833998/feat-check-constraint-pg
feat: support check constraints in pg
2 parents 45af80e + d19025b commit 3b67fae

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

pegjs/postgresql.pegjs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,10 @@ column_definition_opt
12681268
// => { reference_definition: reference_definition; }
12691269
return { reference_definition: re }
12701270
}
1271+
/ ck:check_constraint_definition {
1272+
// => { check: check_constraint_definition; }
1273+
return { check: ck }
1274+
}
12711275
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
12721276
// => { character_set: collate_expr }
12731277
return { character_set: { type: t, value: v, symbol: s }}
@@ -1907,7 +1911,7 @@ create_constraint_check
19071911
= kc:constraint_name? __ p:'CHECK'i __ LPAREN __ e:or_and_where_expr __ RPAREN {
19081912
/* => {
19091913
constraint?: constraint_name['constraint'];
1910-
definition: or_and_where_expr;
1914+
definition: [or_and_where_expr];
19111915
keyword?: constraint_name['keyword'];
19121916
constraint_type: 'check';
19131917
resource: 'constraint';
@@ -2001,6 +2005,28 @@ create_constraint_foreign
20012005
}
20022006
}
20032007

2008+
check_constraint_definition
2009+
= kc:constraint_name? __ u:'CHECK'i __ LPAREN __ c:or_and_expr __ RPAREN __ ne:(KW_NOT? __ 'ENFORCED'i)? {
2010+
/* => {
2011+
constraint_type: 'check';
2012+
keyword: constraint_name['keyword'];
2013+
constraint?: constraint_name['constraint'];
2014+
definition: [or_and_expr];
2015+
enforced?: 'enforced' | 'not enforced';
2016+
resource: 'constraint';
2017+
}*/
2018+
const enforced = []
2019+
if (ne) enforced.push(ne[0], ne[2])
2020+
return {
2021+
constraint_type: u.toLowerCase(),
2022+
keyword: kc && kc.keyword,
2023+
constraint: kc && kc.constraint,
2024+
definition: [c],
2025+
enforced: enforced.filter(v => v).join(' ').toLowerCase(),
2026+
resource: 'constraint',
2027+
}
2028+
}
2029+
20042030
reference_definition
20052031
= kc:KW_REFERENCES __
20062032
t: table_name __

pegjs/sqlite.pegjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ column_definition_opt
500500
/ a:('AUTO_INCREMENT'i / 'AUTOINCREMENT'i) {
501501
return { auto_increment: a.toLowerCase() }
502502
}
503-
/ 'UNIQUE'i __ k:('KEY'i)? {
503+
/ 'UNIQUE'i __ k:('KEY'i)? {
504504
const sql = ['unique']
505505
if (k) sql.push(k)
506506
return { unique: sql.join(' ').toLowerCase('') }

test/postgres.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,13 @@ describe('Postgres', () => {
15071507
`SELECT a FROM "b" WHERE a::TEXT ILIKE '%x%'`
15081508
]
15091509
},
1510+
{
1511+
title: 'check constraint',
1512+
sql: [
1513+
'CREATE TABLE Books (price DECIMAL(10, 2) CHECK (Price > 0));',
1514+
'CREATE TABLE "Books" (price DECIMAL(10, 2) CHECK (Price > 0))'
1515+
]
1516+
},
15101517
]
15111518
function neatlyNestTestedSQL(sqlList){
15121519
sqlList.forEach(sqlInfo => {

0 commit comments

Comments
 (0)