Skip to content

Commit d1870a3

Browse files
committed
Resolve queries without trailing semicolon
1 parent 3a6b44f commit d1870a3

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

plugins/sql-macros/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,16 @@ export class SqlMacrosPlugin extends StarbasePlugin {
107107
}
108108

109109
try {
110+
// Add semicolon if missing
111+
const normalizedSql = sql.trim().endsWith(';') ? sql : `${sql};`
112+
110113
// We allow users to write it `$_exclude` but convert it to `__exclude` so it can be
111114
// parsed with the AST library without throwing an error.
112-
sql = sql.replaceAll('$_exclude', '__exclude')
113-
114-
const normalizedQuery = parser.astify(sql)[0]
115+
const preparedSql = normalizedSql.replaceAll(
116+
'$_exclude',
117+
'__exclude'
118+
)
119+
const normalizedQuery = parser.astify(preparedSql)[0]
115120

116121
// Only process SELECT statements
117122
if (normalizedQuery.type !== 'select') {
@@ -181,8 +186,8 @@ export class SqlMacrosPlugin extends StarbasePlugin {
181186
}))
182187
)
183188

184-
// Convert back to SQL
185-
return parser.sqlify(normalizedQuery)
189+
// Convert back to SQL and remove trailing semicolon to maintain original format
190+
return parser.sqlify(normalizedQuery).replace(/;$/, '')
186191
} catch (error) {
187192
console.error('SQL parsing error:', error)
188193
return sql

0 commit comments

Comments
 (0)