Skip to content

Commit

Permalink
Merge pull request #2040 from taozhi8833998/fix-column-option-mysql
Browse files Browse the repository at this point in the history
fix: column options order in mysql
  • Loading branch information
taozhi8833998 authored Aug 1, 2024
2 parents 3359ea4 + 53d23d3 commit 27902df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
20 changes: 9 additions & 11 deletions src/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,17 @@ function columnReferenceDefinitionToSQL(referenceDefinition) {
return reference.filter(hasVal)
}

function generatedExpressionToSQL(generated) {
if (!generated) return
const result = [toUpper(generated.value), `(${exprToSQL(generated.expr)})`, toUpper(generated.storage_type)]
return result.filter(hasVal).join(' ')
}

function columnOption(definition) {
const columnOpt = []
const {
nullable, character_set: characterSet, check, comment, collate, storage, using,
default_val: defaultOpt,
default_val: defaultOpt, generated,
auto_increment: autoIncrement,
unique: uniqueKey,
primary_key: primaryKey,
Expand All @@ -112,6 +118,7 @@ function columnOption(definition) {
}
const { database } = getParserOpt()
columnOpt.push(constraintDefinitionToSQL(check))
columnOpt.push(generatedExpressionToSQL(generated))
columnOpt.push(autoIncrementToSQL(autoIncrement), toUpper(primaryKey), toUpper(uniqueKey), commentToSQL(comment))
columnOpt.push(...commonTypeValue(characterSet))
if (database !== 'sqlite') columnOpt.push(exprToSQL(collate))
Expand All @@ -136,22 +143,13 @@ function columnOrderToSQL(columnOrder) {
return result.filter(hasVal).join(' ')
}

function generatedExpressionToSQL(generated) {
if (!generated) return
const result = [toUpper(generated.value), `(${exprToSQL(generated.expr)})`, toUpper(generated.storage_type)]
return result.filter(hasVal).join(' ')
}

function columnDefinitionToSQL(columnDefinition) {
const column = []
const name = columnRefToSQL(columnDefinition.column)
const dataType = columnDataType(columnDefinition.definition)
column.push(name)
column.push(dataType)
const columnOpt = columnOption(columnDefinition)
column.push(columnOpt)
const generated = generatedExpressionToSQL(columnDefinition.generated)
column.push(generated)
column.push(columnOption(columnDefinition))
return column.filter(hasVal).join(' ')
}

Expand Down
4 changes: 2 additions & 2 deletions test/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('create', () => {

it('should support generated columns', () =>{
expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) CHARACTER SET latin1 COLLATE latin1_general_cs AS (CONCAT(first_name,' ',last_name)) STORED NOT NULL);`))
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) NOT NULL CHARACTER SET latin1 COLLATE latin1_general_cs AS (CONCAT(`first_name`, ' ', `last_name`)) STORED)");
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) NOT NULL AS (CONCAT(`first_name`, ' ', `last_name`)) STORED CHARACTER SET latin1 COLLATE latin1_general_cs)");

expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) AS (CONCAT(first_name,' ',last_name)) STORED);`))
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) AS (CONCAT(`first_name`, ' ', `last_name`)) STORED)");
Expand All @@ -144,7 +144,7 @@ describe('create', () => {

it('should support generated columns with generated always', () =>{
expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) CHARACTER SET latin1 COLLATE latin1_general_cs GENERATED ALWAYS AS (CONCAT(first_name,' ',last_name)) STORED NOT NULL);`))
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) NOT NULL CHARACTER SET latin1 COLLATE latin1_general_cs GENERATED ALWAYS AS (CONCAT(`first_name`, ' ', `last_name`)) STORED)");
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) NOT NULL GENERATED ALWAYS AS (CONCAT(`first_name`, ' ', `last_name`)) STORED CHARACTER SET latin1 COLLATE latin1_general_cs)");

expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) GENERATED ALWAYS AS (CONCAT(first_name,' ',last_name)) VIRTUAL);`))
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) GENERATED ALWAYS AS (CONCAT(`first_name`, ' ', `last_name`)) VIRTUAL)");
Expand Down
7 changes: 7 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,13 @@ describe('mysql', () => {
'EXPLAIN SELECT * FROM `incidents` WHERE `id` > 10 AND `is_delete` = 0'
]
},
{
title: 'column options order',
sql: [
"alter table a modify column b VARCHAR(200) GENERATED ALWAYS AS (json_unquote(json_extract(`json`, '$.b'))) STORED COMMENT 'some comment'",
"ALTER TABLE `a` MODIFY COLUMN `b` VARCHAR(200) GENERATED ALWAYS AS (json_unquote(json_extract(`json`, '$.b'))) STORED COMMENT 'some comment'"
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit 27902df

Please sign in to comment.