Skip to content

Commit

Permalink
Merge pull request #1968 from taozhi8833998/feat-check-constraint-pg
Browse files Browse the repository at this point in the history
feat: support check constraints in pg
  • Loading branch information
taozhi8833998 authored Jun 20, 2024
2 parents 45af80e + d19025b commit 3b67fae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
28 changes: 27 additions & 1 deletion pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,10 @@ column_definition_opt
// => { reference_definition: reference_definition; }
return { reference_definition: re }
}
/ ck:check_constraint_definition {
// => { check: check_constraint_definition; }
return { check: ck }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
// => { character_set: collate_expr }
return { character_set: { type: t, value: v, symbol: s }}
Expand Down Expand Up @@ -1907,7 +1911,7 @@ create_constraint_check
= kc:constraint_name? __ p:'CHECK'i __ LPAREN __ e:or_and_where_expr __ RPAREN {
/* => {
constraint?: constraint_name['constraint'];
definition: or_and_where_expr;
definition: [or_and_where_expr];
keyword?: constraint_name['keyword'];
constraint_type: 'check';
resource: 'constraint';
Expand Down Expand Up @@ -2001,6 +2005,28 @@ create_constraint_foreign
}
}

check_constraint_definition
= kc:constraint_name? __ u:'CHECK'i __ LPAREN __ c:or_and_expr __ RPAREN __ ne:(KW_NOT? __ 'ENFORCED'i)? {
/* => {
constraint_type: 'check';
keyword: constraint_name['keyword'];
constraint?: constraint_name['constraint'];
definition: [or_and_expr];
enforced?: 'enforced' | 'not enforced';
resource: 'constraint';
}*/
const enforced = []
if (ne) enforced.push(ne[0], ne[2])
return {
constraint_type: u.toLowerCase(),
keyword: kc && kc.keyword,
constraint: kc && kc.constraint,
definition: [c],
enforced: enforced.filter(v => v).join(' ').toLowerCase(),
resource: 'constraint',
}
}

reference_definition
= kc:KW_REFERENCES __
t: table_name __
Expand Down
2 changes: 1 addition & 1 deletion pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ column_definition_opt
/ a:('AUTO_INCREMENT'i / 'AUTOINCREMENT'i) {
return { auto_increment: a.toLowerCase() }
}
/ 'UNIQUE'i __ k:('KEY'i)? {
/ 'UNIQUE'i __ k:('KEY'i)? {
const sql = ['unique']
if (k) sql.push(k)
return { unique: sql.join(' ').toLowerCase('') }
Expand Down
7 changes: 7 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,13 @@ describe('Postgres', () => {
`SELECT a FROM "b" WHERE a::TEXT ILIKE '%x%'`
]
},
{
title: 'check constraint',
sql: [
'CREATE TABLE Books (price DECIMAL(10, 2) CHECK (Price > 0));',
'CREATE TABLE "Books" (price DECIMAL(10, 2) CHECK (Price > 0))'
]
},
]
function neatlyNestTestedSQL(sqlList){
sqlList.forEach(sqlInfo => {
Expand Down

0 comments on commit 3b67fae

Please sign in to comment.