Skip to content

Commit 6cda8cf

Browse files
committed
fix: custom type support schema prefix in pg
1 parent f2e89f7 commit 6cda8cf

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

pegjs/postgresql.pegjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6272,7 +6272,11 @@ network_address_type
62726272
= t:(KW_INET / KW_CIDR / KW_MACADDR8 / KW_MACADDR) {/* => data_type */ return { dataType: t }}
62736273

62746274
custom_types
6275-
= name:ident_name &{ return customTypes.has(name) } {
6275+
= schema:ident_name __ DOT __ name:ident_without_kw &{ return customTypes.has(`${schema}.${name}`) } {
6276+
// => data_type
6277+
return { schema: schema, dataType: name }
6278+
}
6279+
/ name:ident_name &{ return customTypes.has(name) } {
62766280
// => data_type
62776281
return { dataType: name }
62786282
}

src/column.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ function columnRefToSQL(expr) {
6565

6666
function columnDataType(definition) {
6767
if (!definition) return
68-
const { dataType, length, suffix, scale, expr } = definition
68+
const { schema, dataType, length, suffix, scale, expr } = definition
6969
const parentheses = length != null && true || false
70-
let result = dataTypeToSQL({ dataType, length, suffix, scale, parentheses })
70+
let result = dataTypeToSQL({ schema, dataType, length, suffix, scale, parentheses })
7171
if (expr) result += exprToSQL(expr)
7272
if (definition.array) {
7373
const arrayExpr = arrayDimensionToSymbol(definition)

src/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,13 @@ function onPartitionsToSQL(expr) {
292292
}
293293

294294
function dataTypeToSQL(expr) {
295-
const { dataType, length, parentheses, scale, suffix } = expr
295+
const { schema, dataType, length, parentheses, scale, suffix } = expr
296296
let str = ''
297297
if (length != null) str = scale ? `${length}, ${scale}` : length
298298
if (parentheses) str = `(${str})`
299299
if (suffix && suffix.length) str += ` ${suffix.join(' ')}`
300-
return `${dataType}${str}`
300+
const prefix = schema ? `${schema}.` : ''
301+
return `${prefix}${dataType}${str}`
301302
}
302303

303304
function arrayStructTypeToSQL(expr) {

test/postgres.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,6 +2322,16 @@ describe('Postgres', () => {
23222322
'CREATE TABLE "t_pgsql_generate_test" (id_col SERIAL NOT NULL PRIMARY KEY, small_int_col SMALLINT NOT NULL, small_serial_col SMALLSERIAL NOT NULL, integer_type_col INTEGER NOT NULL, big_int_col BIGINT NOT NULL)'
23232323
]
23242324
},
2325+
{
2326+
title: 'custom data type for create table',
2327+
sql: [
2328+
`create type public.Gender AS ENUM ('MALE', 'FEMALE');
2329+
create table users (
2330+
gender public."Gender"
2331+
);`,
2332+
`CREATE TYPE "public"."Gender" AS ENUM ('MALE', 'FEMALE') ; CREATE TABLE "users" (gender public.Gender)`
2333+
]
2334+
},
23252335
]
23262336
neatlyNestTestedSQL(SQL_LIST)
23272337
})

0 commit comments

Comments
 (0)