Skip to content

Commit 79c6a70

Browse files
Merge pull request taozhi8833998#1317 from taozhi8833998/feat-create-table-mysql
feat: create table index with length in mysql
2 parents 0ed8a90 + 83d5f02 commit 79c6a70

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

pegjs/mariadb.pegjs

+27-1
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,38 @@ ALTER_CHANGE_COLUMN
798798
},
799799
}
800800
}
801+
column_idx_ref
802+
= col:column_without_kw __ LPAREN __ l:[0-9]+ __ RPAREN __ ob:(KW_ASC / KW_DESC)? {
803+
return {
804+
type: 'column_ref',
805+
column: col,
806+
suffix: `(${parseInt(l.join(''), 10)})`,
807+
order_by: ob
808+
};
809+
}
810+
/ col:column_without_kw __ ob:(KW_ASC / KW_DESC)? {
811+
return {
812+
type: 'column_ref',
813+
column: col,
814+
order_by: ob
815+
};
816+
}
817+
818+
column_ref_idx_list
819+
= head:column_idx_ref tail:(__ COMMA __ column_idx_ref)* {
820+
return createList(head, tail);
821+
}
822+
823+
cte_idx_column_definition
824+
= LPAREN __ l:column_ref_idx_list __ RPAREN {
825+
return l
826+
}
801827

802828
create_index_definition
803829
= kc:(KW_INDEX / KW_KEY) __
804830
c:column? __
805831
t:index_type? __
806-
de:cte_column_definition __
832+
de:cte_idx_column_definition __
807833
id:index_options? __
808834
{
809835
return {

pegjs/mysql.pegjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,15 @@ cte_column_definition
15181518
}
15191519

15201520
column_idx_ref
1521-
= col:column_without_kw __ ob:(KW_ASC / KW_DESC)? {
1521+
= col:column_without_kw __ LPAREN __ l:[0-9]+ __ RPAREN __ ob:(KW_ASC / KW_DESC)? {
1522+
return {
1523+
type: 'column_ref',
1524+
column: col,
1525+
suffix: `(${parseInt(l.join(''), 10)})`,
1526+
order_by: ob
1527+
};
1528+
}
1529+
/ col:column_without_kw __ ob:(KW_ASC / KW_DESC)? {
15221530
return {
15231531
type: 'column_ref',
15241532
column: col,

src/index-definition.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ function indexTypeAndOptionToSQL(indexDefinition) {
7373

7474
function indexDefinitionToSQL(indexDefinition) {
7575
const indexSQL = []
76-
const {
77-
keyword,
78-
index,
79-
} = indexDefinition
76+
const { keyword, index } = indexDefinition
8077
indexSQL.push(toUpper(keyword))
8178
indexSQL.push(index)
8279
indexSQL.push(...indexTypeAndOptionToSQL(indexDefinition))

test/mysql-mariadb.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,13 @@ describe('mysql', () => {
505505
'SELECT * FROM (SELECT 1) MINUS SELECT * FROM (SELECT 2)'
506506
]
507507
},
508+
{
509+
title: 'index column length',
510+
sql: [
511+
'CREATE TABLE `Translation` (`id` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,`en-GB` text,PRIMARY KEY (`id`),KEY `Translation_en-GB_btree_idx` (`en-GB`(768))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
512+
'CREATE TABLE `Translation` (`id` CHAR(36) NOT NULL CHARACTER SET ASCII COLLATE ASCII_BIN, `en-GB` TEXT, PRIMARY KEY (`id`), KEY Translation_en-GB_btree_idx (`en-GB` (768))) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci',
513+
]
514+
},
508515
]
509516
SQL_LIST.forEach(sqlInfo => {
510517
const { title, sql } = sqlInfo

0 commit comments

Comments
 (0)