Skip to content

Commit

Permalink
Merge pull request #1585 from taozhi8833998/fix-sqlify-db
Browse files Browse the repository at this point in the history
fix: sqlify from mariadb ast to sqlite
  • Loading branch information
taozhi8833998 authored Sep 18, 2023
2 parents 9d10973 + b8152d6 commit 7317bdf
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2504,15 +2504,15 @@ 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,
column: col
};
}
/ col:column {
columnList.add(`select::null::${col}`);
// columnList.add(`select::null::${col}`);
return {
type: 'column_ref',
table: null,
Expand Down
4 changes: 3 additions & 1 deletion src/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
commonOptionConnector,
commonTypeValue,
commentToSQL,
getParserOpt,
hasVal,
identifierToSql,
literalToSQL,
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 7 additions & 3 deletions src/constrain.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
identifierToSql,
toUpper,
getParserOpt,
hasVal,
toUpper,
} from './util'
import { indexTypeAndOptionToSQL } from './index-definition'
import { columnReferenceDefinitionToSQL } from './column'
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
7 changes: 5 additions & 2 deletions test/sqlite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`))')
})
})

0 comments on commit 7317bdf

Please sign in to comment.