Skip to content

Commit c6e9fc5

Browse files
Merge pull request taozhi8833998#2130 from taozhi8833998/feat-comment-on-pg
feat: support comment on in pg
2 parents 465a891 + e401fe8 commit c6e9fc5

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

pegjs/postgresql.pegjs

Lines changed: 49 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,53 @@ transaction_stmt
30523053
}
30533054
}
30543055
}
3056+
comment_on_option
3057+
= t:(KW_TABLE / KW_VIEW / KW_TABLESPACE) __ name:table_name {
3058+
// => { type: string; name: table_name; }
3059+
return {
3060+
type: t.toLowerCase(),
3061+
name,
3062+
}
3063+
}
3064+
/ t:(KW_COLUMN) __ name:column_ref {
3065+
// => { type: string; name: column_ref; }
3066+
return {
3067+
type: t.toLowerCase(),
3068+
name,
3069+
}
3070+
}
3071+
/ t:(KW_INDEX / KW_COLLATION / KW_TABLESPACE / KW_SCHEMA / 'DOMAIN'i / KW_DATABASE / 'ROLE'i / 'SEQUENCE'i / 'SERVER'i / 'SUBSCRIPTION'i ) __ name:ident_type {
3072+
// => { type: string; name: ident; }
3073+
return {
3074+
type: t.toLowerCase(),
3075+
name,
3076+
}
3077+
}
3078+
3079+
comment_on_is
3080+
= 'IS'i __ e:(literal_string / literal_null) {
3081+
// => { keyword: 'is'; expr: literal_string | literal_null; }
3082+
return {
3083+
keyword: 'is',
3084+
expr: e,
3085+
}
3086+
}
3087+
comment_on_stmt
3088+
= 'COMMENT'i __ 'ON'i __ co:comment_on_option __ is:comment_on_is {
3089+
/* export interface comment_on_stmt_t {
3090+
type: 'comment';
3091+
target: comment_on_option;
3092+
expr: comment_on_is;
3093+
}
3094+
=> AstStatement<comment_on_stmt_t>
3095+
*/
3096+
return {
3097+
type: 'comment',
3098+
keyword: 'on',
3099+
target: co,
3100+
expr: is,
3101+
}
3102+
}
30553103
select_stmt
30563104
= KW_SELECT __ ';' {
30573105
// => { type: 'select'; }
@@ -5249,6 +5297,7 @@ KW_SCHEMA = "SCHEMA"i !ident_start { return 'SCHEMA'; }
52495297
KW_SEQUENCE = "SEQUENCE"i !ident_start { return 'SEQUENCE'; }
52505298
KW_TABLESPACE = "TABLESPACE"i !ident_start { return 'TABLESPACE'; }
52515299
KW_COLLATE = "COLLATE"i !ident_start { return 'COLLATE'; }
5300+
KW_COLLATION = "COLLATION"i !ident_start { return 'COLLATION'; }
52525301
KW_DEALLOCATE = "DEALLOCATE"i !ident_start { return 'DEALLOCATE'; }
52535302

52545303
KW_ON = "ON"i !ident_start

src/comment.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
default:
14+
return `${toUpper(type)} ${literalToSQL(name)}`
15+
}
16+
}
17+
18+
function commentIsExprToSQL(stmt) {
19+
const { keyword, expr } = stmt
20+
return [toUpper(keyword), literalToSQL(expr)].filter(hasVal).join(' ')
21+
}
22+
23+
function commentOnToSQL(stmt) {
24+
const { expr, keyword, target, type } = stmt
25+
const result = [
26+
toUpper(type),
27+
toUpper(keyword),
28+
commentOptionToSQL(target),
29+
commentIsExprToSQL(expr),
30+
]
31+
return result.filter(hasVal).join(' ')
32+
}
33+
34+
export {
35+
commentOnToSQL,
36+
}

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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,10 +1943,31 @@ 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+
},
1960+
{
1961+
title: 'comment on database',
1962+
sql: [
1963+
`COMMENT ON DATABASE my_database IS 'Development Database';`,
1964+
`COMMENT ON DATABASE my_database IS 'Development Database'`,
1965+
],
1966+
},
19461967
]
19471968
neatlyNestTestedSQL(SQL_LIST)
19481969
})
1949-
1970+
19501971
describe('pg ast', () => {
19511972
it('should get correct columns and tables', () => {
19521973
let sql = 'SELECT "Id" FROM "Test";'

0 commit comments

Comments
 (0)