diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index 85daabed..0ba14e9e 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -2504,7 +2504,7 @@ primary column_ref = tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw { - columnList.add(`select::${tbl}::${col}`); + // columnList.add(`select::${tbl}::${col}`); return { type: 'column_ref', table: tbl, @@ -2512,7 +2512,7 @@ column_ref }; } / col:column { - columnList.add(`select::null::${col}`); + // columnList.add(`select::null::${col}`); return { type: 'column_ref', table: null, diff --git a/src/column.js b/src/column.js index 9f48c8c9..9e84b20b 100644 --- a/src/column.js +++ b/src/column.js @@ -8,6 +8,7 @@ import { commonOptionConnector, commonTypeValue, commentToSQL, + getParserOpt, hasVal, identifierToSql, literalToSQL, @@ -88,10 +89,11 @@ function columnOption(definition) { const { type, value } = defaultOpt columnOpt.push(type.toUpperCase(), exprToSQL(value)) } + const { database } = getParserOpt() columnOpt.push(constraintDefinitionToSQL(check)) columnOpt.push(autoIncrementToSQL(autoIncrement), toUpper(primaryKey), toUpper(uniqueKey), commentToSQL(comment)) columnOpt.push(...commonTypeValue(characterSet)) - columnOpt.push(...commonTypeValue(collate)) + if (database !== 'sqlite') columnOpt.push(...commonTypeValue(collate)) columnOpt.push(...commonTypeValue(columnFormat)) columnOpt.push(...commonTypeValue(storage)) columnOpt.push(...columnReferenceDefinitionToSQL(referenceDefinition)) diff --git a/src/constrain.js b/src/constrain.js index 0f0dd40b..75a9bc3c 100644 --- a/src/constrain.js +++ b/src/constrain.js @@ -1,7 +1,8 @@ import { identifierToSql, - toUpper, + getParserOpt, hasVal, + toUpper, } from './util' import { indexTypeAndOptionToSQL } from './index-definition' import { columnReferenceDefinitionToSQL } from './column' @@ -17,10 +18,13 @@ function constraintDefinitionToSQL(constraintDefinition) { reference_definition: referenceDefinition, } = constraintDefinition const constraintSQL = [] + const { database } = getParserOpt() constraintSQL.push(toUpper(keyword)) constraintSQL.push(identifierToSql(constraint)) - constraintSQL.push(toUpper(constraintType)) - constraintSQL.push(identifierToSql(index)) + let constraintTypeStr = toUpper(constraintType) + if (database === 'sqlite' && constraintTypeStr === 'UNIQUE KEY') constraintTypeStr = 'UNIQUE' + constraintSQL.push(constraintTypeStr) + constraintSQL.push(database !== 'sqlite' && identifierToSql(index)) constraintSQL.push(...indexTypeAndOptionToSQL(constraintDefinition)) constraintSQL.push(...columnReferenceDefinitionToSQL(referenceDefinition)) constraintSQL.push(toUpper(enforced)) diff --git a/src/util.js b/src/util.js index d05648e2..7640ee29 100644 --- a/src/util.js +++ b/src/util.js @@ -355,6 +355,6 @@ export { columnOrderListToSQL, commonKeywordArgsToSQL, commonOptionConnector, connector, commonTypeValue,commentToSQL, createBinaryExpr, createValueExpr, dataTypeToSQL, DEFAULT_OPT, escape, literalToSQL, columnIdentifierToSql, - identifierToSql, onPartitionsToSQL, replaceParams, returningToSQL, + getParserOpt, identifierToSql, onPartitionsToSQL, replaceParams, returningToSQL, hasVal, setParserOpt, toUpper, topToSQL, triggerEventToSQL, } diff --git a/test/sqlite.spec.js b/test/sqlite.spec.js index 8cc5253f..c1e8ed49 100644 --- a/test/sqlite.spec.js +++ b/test/sqlite.spec.js @@ -147,8 +147,11 @@ describe('sqlite', () => { }) it('should support sqlify autoincrement to other db', () => { - const sql = 'CREATE TABLE IF NOT EXISTS "SampleTable" ( "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "Name" TEXT NOT NULL);' - const ast = parser.astify(sql, DEFAULT_OPT) + let sql = 'CREATE TABLE IF NOT EXISTS "SampleTable" ( "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "Name" TEXT NOT NULL);' + let ast = parser.astify(sql, DEFAULT_OPT) 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)') + 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`));' + ast = parser.astify(sql, { database: 'mariadb' }) + 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`))') }) })