diff --git a/src/column.js b/src/column.js index 79e29185..6a173c48 100644 --- a/src/column.js +++ b/src/column.js @@ -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, @@ -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)) @@ -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(' ') } diff --git a/test/create.spec.js b/test/create.spec.js index cc56280a..7e69bf88 100644 --- a/test/create.spec.js +++ b/test/create.spec.js @@ -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)"); @@ -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)"); diff --git a/test/mysql-mariadb.spec.js b/test/mysql-mariadb.spec.js index ae0931f9..6e099758 100644 --- a/test/mysql-mariadb.spec.js +++ b/test/mysql-mariadb.spec.js @@ -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