Skip to content

Commit b8152d6

Browse files
committed
fix: sqlify from mariadb ast to sqlite
1 parent 087ad1f commit b8152d6

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

pegjs/mariadb.pegjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,15 +2504,15 @@ primary
25042504

25052505
column_ref
25062506
= tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw {
2507-
columnList.add(`select::${tbl}::${col}`);
2507+
// columnList.add(`select::${tbl}::${col}`);
25082508
return {
25092509
type: 'column_ref',
25102510
table: tbl,
25112511
column: col
25122512
};
25132513
}
25142514
/ col:column {
2515-
columnList.add(`select::null::${col}`);
2515+
// columnList.add(`select::null::${col}`);
25162516
return {
25172517
type: 'column_ref',
25182518
table: null,

src/column.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
commonOptionConnector,
99
commonTypeValue,
1010
commentToSQL,
11+
getParserOpt,
1112
hasVal,
1213
identifierToSql,
1314
literalToSQL,
@@ -88,10 +89,11 @@ function columnOption(definition) {
8889
const { type, value } = defaultOpt
8990
columnOpt.push(type.toUpperCase(), exprToSQL(value))
9091
}
92+
const { database } = getParserOpt()
9193
columnOpt.push(constraintDefinitionToSQL(check))
9294
columnOpt.push(autoIncrementToSQL(autoIncrement), toUpper(primaryKey), toUpper(uniqueKey), commentToSQL(comment))
9395
columnOpt.push(...commonTypeValue(characterSet))
94-
columnOpt.push(...commonTypeValue(collate))
96+
if (database !== 'sqlite') columnOpt.push(...commonTypeValue(collate))
9597
columnOpt.push(...commonTypeValue(columnFormat))
9698
columnOpt.push(...commonTypeValue(storage))
9799
columnOpt.push(...columnReferenceDefinitionToSQL(referenceDefinition))

src/constrain.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
identifierToSql,
3-
toUpper,
3+
getParserOpt,
44
hasVal,
5+
toUpper,
56
} from './util'
67
import { indexTypeAndOptionToSQL } from './index-definition'
78
import { columnReferenceDefinitionToSQL } from './column'
@@ -17,10 +18,13 @@ function constraintDefinitionToSQL(constraintDefinition) {
1718
reference_definition: referenceDefinition,
1819
} = constraintDefinition
1920
const constraintSQL = []
21+
const { database } = getParserOpt()
2022
constraintSQL.push(toUpper(keyword))
2123
constraintSQL.push(identifierToSql(constraint))
22-
constraintSQL.push(toUpper(constraintType))
23-
constraintSQL.push(identifierToSql(index))
24+
let constraintTypeStr = toUpper(constraintType)
25+
if (database === 'sqlite' && constraintTypeStr === 'UNIQUE KEY') constraintTypeStr = 'UNIQUE'
26+
constraintSQL.push(constraintTypeStr)
27+
constraintSQL.push(database !== 'sqlite' && identifierToSql(index))
2428
constraintSQL.push(...indexTypeAndOptionToSQL(constraintDefinition))
2529
constraintSQL.push(...columnReferenceDefinitionToSQL(referenceDefinition))
2630
constraintSQL.push(toUpper(enforced))

src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,6 @@ export {
355355
columnOrderListToSQL, commonKeywordArgsToSQL, commonOptionConnector,
356356
connector, commonTypeValue,commentToSQL, createBinaryExpr,
357357
createValueExpr, dataTypeToSQL, DEFAULT_OPT, escape, literalToSQL, columnIdentifierToSql,
358-
identifierToSql, onPartitionsToSQL, replaceParams, returningToSQL,
358+
getParserOpt, identifierToSql, onPartitionsToSQL, replaceParams, returningToSQL,
359359
hasVal, setParserOpt, toUpper, topToSQL, triggerEventToSQL,
360360
}

test/sqlite.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,11 @@ describe('sqlite', () => {
147147
})
148148

149149
it('should support sqlify autoincrement to other db', () => {
150-
const sql = 'CREATE TABLE IF NOT EXISTS "SampleTable" ( "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "Name" TEXT NOT NULL);'
151-
const ast = parser.astify(sql, DEFAULT_OPT)
150+
let sql = 'CREATE TABLE IF NOT EXISTS "SampleTable" ( "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "Name" TEXT NOT NULL);'
151+
let ast = parser.astify(sql, DEFAULT_OPT)
152152
expect(parser.sqlify(ast, { database: 'mariadb'})).to.be.equal('CREATE TABLE IF NOT EXISTS `SampleTable` (`ID` INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY UNIQUE, `Name` TEXT NOT NULL)')
153+
sql = ' CREATE TABLE `Test` ( `id` int(11) NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`));'
154+
ast = parser.astify(sql, { database: 'mariadb' })
155+
expect(parser.sqlify(ast, DEFAULT_OPT)).to.be.equal('CREATE TABLE `Test` (`id` INT(11) NOT NULL, `name` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE (`name`))')
153156
})
154157
})

0 commit comments

Comments
 (0)