Skip to content

Commit 6afe725

Browse files
committed
feat: support comment on in pg
1 parent c98cdf2 commit 6afe725

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

pegjs/postgresql.pegjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ cmd_stmt
239239
/ execute_stmt
240240
/ for_loop_stmt
241241
/ transaction_stmt
242+
/ comment_on_stmt
242243

243244
create_stmt
244245
= create_table_stmt
@@ -3052,6 +3053,46 @@ transaction_stmt
30523053
}
30533054
}
30543055
}
3056+
comment_on_option
3057+
= t:(KW_TABLE / KW_VIEW) __ name:table_name {
3058+
// => { type: string; name: table_name; }
3059+
return {
3060+
type: t.toLowerCase(),
3061+
name: name,
3062+
}
3063+
}
3064+
/ t:(KW_COLUMN) __ name:column_ref {
3065+
// => { type: string; name: column_ref; }
3066+
return {
3067+
type: t.toLowerCase(),
3068+
name: name,
3069+
}
3070+
}
3071+
3072+
comment_on_is
3073+
= 'IS'i __ e:(literal_string / literal_null) {
3074+
// => { keyword: 'is'; expr: literal_string | literal_null; }
3075+
return {
3076+
keyword: 'is',
3077+
expr: e,
3078+
}
3079+
}
3080+
comment_on_stmt
3081+
= 'COMMENT'i __ 'ON'i __ co:comment_on_option __ is:comment_on_is {
3082+
/* export interface comment_on_stmt_t {
3083+
type: 'comment';
3084+
target: comment_on_option;
3085+
expr: comment_on_is;
3086+
}
3087+
=> AstStatement<comment_on_stmt_t>
3088+
*/
3089+
return {
3090+
type: 'comment',
3091+
keyword: 'on',
3092+
target: co,
3093+
expr: is,
3094+
}
3095+
}
30553096
select_stmt
30563097
= KW_SELECT __ ';' {
30573098
// => { type: 'select'; }

src/comment.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { columnRefToSQL } from './column'
2+
import { hasVal, identifierToSql, literalToSQL, toUpper } from './util'
3+
4+
function commentOptionToSQL(stmt) {
5+
const { name, type } = stmt
6+
switch (type) {
7+
case 'table':
8+
case 'view':
9+
const fullTableName = [identifierToSql(name.db), identifierToSql(name.table)].filter(hasVal).join('.')
10+
return `${toUpper(type)} ${fullTableName}`
11+
case 'column':
12+
return `COLUMN ${columnRefToSQL(name)}`
13+
}
14+
}
15+
16+
function commentIsExprToSQL(stmt) {
17+
const { keyword, expr } = stmt
18+
return [toUpper(keyword), literalToSQL(expr)].filter(hasVal).join(' ')
19+
}
20+
21+
function commentOnToSQL(stmt) {
22+
const { expr, keyword, target, type } = stmt
23+
const result = [
24+
toUpper(type),
25+
toUpper(keyword),
26+
commentOptionToSQL(target),
27+
commentIsExprToSQL(expr),
28+
]
29+
return result.filter(hasVal).join(' ')
30+
}
31+
32+
export {
33+
commentOnToSQL,
34+
}

src/sql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { unionToSQL, multipleToSQL } from './union'
22

3-
const supportedTypes = ['analyze', 'attach', 'select', 'deallocate', 'delete', 'exec', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'declare', 'show', 'replace', 'if', 'grant', 'revoke', 'proc', 'raise', 'execute', 'transaction', 'explain']
3+
const supportedTypes = ['analyze', 'attach', 'select', 'deallocate', 'delete', 'exec', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'declare', 'show', 'replace', 'if', 'grant', 'revoke', 'proc', 'raise', 'execute', 'transaction', 'explain', 'comment']
44

55
function checkSupported(expr) {
66
const ast = expr && expr.ast ? expr.ast : expr

src/union.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { alterToSQL } from './alter'
22
import { analyzeToSQL, attachToSQL } from './analyze'
33
import { createToSQL } from './create'
4+
import { commentOnToSQL } from './comment'
45
import { explainToSQL } from './explain'
56
import { selectToSQL } from './select'
67
import { deleteToSQL } from './delete'
@@ -35,6 +36,7 @@ const typeToSQLFn = {
3536
analyze : analyzeToSQL,
3637
attach : attachToSQL,
3738
create : createToSQL,
39+
comment : commentOnToSQL,
3840
select : selectToSQL,
3941
deallocate : deallocateToSQL,
4042
delete : deleteToSQL,

test/postgres.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,20 @@ describe('Postgres', () => {
19431943
'SELECT "meeting".end FROM "meeting"'
19441944
]
19451945
},
1946+
{
1947+
title: 'comment on table',
1948+
sql: [
1949+
"COMMENT ON TABLE users IS 'users table';",
1950+
`COMMENT ON TABLE "users" IS 'users table'`
1951+
]
1952+
},
1953+
{
1954+
title: 'comment on column',
1955+
sql: [
1956+
`COMMENT ON COLUMN "users"."name" IS 'first name and last name';`,
1957+
`COMMENT ON COLUMN "users"."name" IS 'first name and last name'`
1958+
]
1959+
},
19461960
]
19471961
neatlyNestTestedSQL(SQL_LIST)
19481962
})

0 commit comments

Comments
 (0)