Skip to content

Commit

Permalink
Merge pull request #1930 from taozhi8833998/feat-create-table-mysql
Browse files Browse the repository at this point in the history
feat: support create table in mysql 8.x and drop view in sqlite
  • Loading branch information
taozhi8833998 authored Jun 1, 2024
2 parents 7d9ef8c + cfcbfdf commit a3e448e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
13 changes: 9 additions & 4 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ drop_stmt
keyword: r.toLowerCase(),
prefix: ife,
name: t,
options: [{ type: 'origin', value: op }],
options: op && [{ type: 'origin', value: op }],
}
};
}
Expand Down Expand Up @@ -1203,7 +1203,8 @@ column_ref_idx_list
}

cte_idx_column_definition
= LPAREN __ l:column_ref_idx_list __ RPAREN {
= LPAREN __ l:(column_ref_idx_list / expr_list) __ RPAREN {
if (l.type) return l.value
return l
}

Expand Down Expand Up @@ -3247,6 +3248,7 @@ cast_expr
type: 'cast',
keyword: c.toLowerCase(),
expr: e,
symbol: 'as',
target: t
};
}
Expand All @@ -3255,6 +3257,7 @@ cast_expr
type: 'cast',
keyword: c.toLowerCase(),
expr: e,
symbol: 'as',
target: {
dataType: 'DECIMAL(' + precision + ')'
}
Expand All @@ -3265,6 +3268,7 @@ cast_expr
type: 'cast',
keyword: c.toLowerCase(),
expr: e,
symbol: 'as',
target: {
dataType: 'DECIMAL(' + precision + ', ' + scale + ')'
}
Expand All @@ -3275,6 +3279,7 @@ cast_expr
type: 'cast',
keyword: c.toLowerCase(),
expr: e,
symbol: 'as',
target: {
dataType: s + (t ? ' ' + t: '')
}
Expand Down Expand Up @@ -3933,8 +3938,8 @@ binary_type


character_string_type
= t:(KW_CHAR / KW_VARCHAR) __ LPAREN __ l:[0-9]+ __ RPAREN {
return { dataType: t, length: parseInt(l.join(''), 10), parentheses: true };
= t:(KW_CHAR / KW_VARCHAR) __ LPAREN __ l:[0-9]+ __ RPAREN __ s:('ARRAY'i)? {
return { dataType: t, length: parseInt(l.join(''), 10), parentheses: true, suffix: s && ['ARRAY'] };
}
/ t:KW_CHAR { return { dataType: t }; }
/ t:KW_VARCHAR { return { dataType: t }; }
Expand Down
9 changes: 5 additions & 4 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ drop_stmt
keyword: r.toLowerCase(),
prefix: ife,
name: t,
options: [{ type: 'origin', value: op }],
options: op && [{ type: 'origin', value: op }],
}
};
}
Expand Down Expand Up @@ -2093,7 +2093,8 @@ column_ref_idx_list
}

cte_idx_column_definition
= LPAREN __ l:column_ref_idx_list __ RPAREN {
= LPAREN __ l:(column_ref_idx_list / expr_list) __ RPAREN {
if (l.type) return l.value
return l
}

Expand Down Expand Up @@ -4253,8 +4254,8 @@ binary_type
/ t:KW_BINARY { return { dataType: t }; }

character_string_type
= t:(KW_CHAR / KW_VARCHAR) __ LPAREN __ l:[0-9]+ __ RPAREN {
return { dataType: t, length: parseInt(l.join(''), 10), parentheses: true };
= t:(KW_CHAR / KW_VARCHAR) __ LPAREN __ l:[0-9]+ __ RPAREN __ s:('ARRAY'i)? {
return { dataType: t, length: parseInt(l.join(''), 10), parentheses: true, suffix: s && ['ARRAY'] };
}
/ t:KW_CHAR { return { dataType: t }; }
/ t:KW_VARCHAR { return { dataType: t }; }
Expand Down
20 changes: 20 additions & 0 deletions pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ if_not_exists_stmt
return 'IF NOT EXISTS'
}

if_exists
= 'if'i __ 'exists'i {
return 'if exists'
}

create_trigger_stmt
= kw: KW_CREATE __
tp:(KW_TEMPORARY / KW_TEMP)? __
Expand Down Expand Up @@ -622,6 +627,21 @@ drop_stmt
}
};
}
/ a:KW_DROP __
r:KW_VIEW __
ife:if_exists? __
t:table_ref_list {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a.toLowerCase(),
keyword: r.toLowerCase(),
prefix: ife,
name: t,
}
};
}
/ a:KW_DROP __
r:KW_INDEX __
i:column_ref __
Expand Down
2 changes: 1 addition & 1 deletion src/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function columnToSQL(column, isDual) {
result.push([toUpper(type), type && '(', columnsStr, type && ')'].filter(hasVal).join(''))
return result.filter(hasVal).join(' ')
}
if (expr.parentheses && Reflect.has(expr, 'array_index')) str = `(${str})`
if (expr.parentheses && Reflect.has(expr, 'array_index') && expr.type !== 'cast') str = `(${str})`
if (expr.array_index && expr.type !== 'column_ref') {
str = `${str}${arrayIndexToSQL(expr.array_index)}`
}
Expand Down
6 changes: 3 additions & 3 deletions src/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function arrayDimensionToSymbol(target) {
}

function castToSQL(expr) {
const { arrows = [], collate, target, expr: expression, keyword, symbol, as: alias, properties = [] } = expr
const { arrows = [], collate, target, expr: expression, keyword, symbol, as: alias, parentheses: outParentheses, properties = [] } = expr
const { length, dataType, parentheses, quoted, scale, suffix: dataTypeSuffix, expr: targetExpr } = target
let str = targetExpr ? exprToSQL(targetExpr) : ''
if (length != null) str = scale ? `${length}, ${scale}` : length
Expand All @@ -41,8 +41,8 @@ function castToSQL(expr) {
if (alias) suffix += ` AS ${identifierToSql(alias)}`
if (collate) suffix += ` ${commonTypeValue(collate).join(' ')}`
const arrayDimension = arrayDimensionToSymbol(target)
const result = [prefix, symbolChar, quoted, dataType, quoted, arrayDimension, str, suffix]
return result.filter(hasVal).join('')
const result = [prefix, symbolChar, quoted, dataType, quoted, arrayDimension, str, suffix].filter(hasVal).join('')
return outParentheses ? `(${result})` : result
}

function extractFunToSQL(stmt) {
Expand Down
10 changes: 10 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,16 @@ describe('mysql', () => {
"SET @_mystoredprocedure_0 = '1', @_mystoredprocedure_1 = '2'"
]
},
{
title: 'create table for key',
sql: [
`CREATE TABLE some_table (
col JSON,
KEY \`idx_col\` ((CAST(\`col\` AS CHAR(12) ARRAY)))
);`,
'CREATE TABLE `some_table` (`col` JSON, KEY idx_col ((CAST(`col` AS CHAR(12) ARRAY))))'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down
4 changes: 3 additions & 1 deletion test/sqlite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ describe('sqlite', () => {
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`))')
})

it('should support create view', () => {
it('should support create or drop view', () => {
let sql = 'create view v1 as select * from t1'
expect(getParsedSql(sql)).to.be.equal('CREATE VIEW `v1` AS SELECT * FROM `t1`')
sql = 'create temp view if not exists s.v1(a, b, c) as select * from t1'
expect(getParsedSql(sql)).to.be.equal('CREATE TEMP VIEW IF NOT EXISTS `s`.`v1` (`a`, `b`, `c`) AS SELECT * FROM `t1`')
sql = 'DROP VIEW IF EXISTS view_name;',
expect(getParsedSql(sql)).to.be.equal('DROP VIEW IF EXISTS `view_name`')
})
})

0 comments on commit a3e448e

Please sign in to comment.