Skip to content

Commit 47b7aeb

Browse files
Merge pull request #2528 from taozhi8833998/feat-generated-by-default-pg
feat: support generate by default in pg
2 parents cd30f8b + 805c6b0 commit 47b7aeb

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

pegjs/postgresql.pegjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,13 @@ column_definition_opt
12951295
// => { comment: keyword_comment; }
12961296
return { comment: co }
12971297
}
1298+
/ gbd:('GENERATED'i)? __ 'BY'i __ 'DEFAULT'i __ 'AS'i __ 'IDENTITY'i {
1299+
// => { generated_by_default: { type: 'origin', value: string } }
1300+
const sql = []
1301+
if (gbd) sql.push('generated')
1302+
sql.push('by', 'default', 'as', 'identity')
1303+
return { generated_by_default: { type: 'origin', value: sql.join(' ').toLowerCase('') } }
1304+
}
12981305
/ ca:collate_expr {
12991306
// => { collate: collate_expr; }
13001307
return { collate: ca }

src/column.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function columnOption(definition) {
111111
primary_key: primaryKey,
112112
column_format: columnFormat,
113113
reference_definition: referenceDefinition,
114+
generated_by_default: generateByDefault,
114115
} = definition
115116
const nullSQL = [toUpper(nullable && nullable.action), toUpper(nullable && nullable.value)].filter(hasVal).join(' ')
116117
if (!generated) columnOpt.push(nullSQL)
@@ -123,7 +124,7 @@ function columnOption(definition) {
123124
columnOpt.push(constraintDefinitionToSQL(check))
124125
columnOpt.push(generatedExpressionToSQL(generated))
125126
if (generated) columnOpt.push(nullSQL)
126-
columnOpt.push(autoIncrementToSQL(autoIncrement), toUpper(primaryKey), toUpper(uniqueKey), commentToSQL(comment))
127+
columnOpt.push(autoIncrementToSQL(autoIncrement), toUpper(primaryKey), toUpper(uniqueKey), literalToSQL(generateByDefault), commentToSQL(comment))
127128
columnOpt.push(...commonTypeValue(characterSet))
128129
if (database.toLowerCase() !== 'sqlite') columnOpt.push(exprToSQL(collate))
129130
columnOpt.push(...commonTypeValue(columnFormat))

test/postgres.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,15 @@ describe('Postgres', () => {
19131913
`SELECT "p".description AS "description", "p".invoice_date AS "invoice_date" FROM "anonymized_schema"."payments" AS "p" INNER JOIN "anonymized_schema"."entities" AS "e" ON "p".entity_id = "e".entity_id LEFT JOIN "anonymized_schema"."entities" AS "pe" ON "p".parent_entity_id = "pe".entity_id LEFT JOIN "anonymized_schema"."entities" AS "ppe" ON "pe".parent_ids[array_upper("pe".parent_ids, 1)] = "ppe".entity_id WHERE "e".entity_type IN ('A', 'B') AND "p".invoice_date >= '2025-08-01' AND "p".invoice_date < '2025-09-01' AND CAST("p".amount AS NUMERIC) > 0`
19141914
]
19151915
},
1916+
{
1917+
title: 'generate by default',
1918+
sql: [
1919+
`CREATE TABLE "t_logs" (
1920+
"log_id" INTEGER NOT NULL UNIQUE GENERATED BY DEFAULT AS IDENTITY
1921+
);`,
1922+
'CREATE TABLE "t_logs" ("log_id" INTEGER NOT NULL UNIQUE GENERATED BY DEFAULT AS IDENTITY)'
1923+
]
1924+
},
19161925
]
19171926
function neatlyNestTestedSQL(sqlList){
19181927
sqlList.forEach(sqlInfo => {

0 commit comments

Comments
 (0)