Skip to content

Commit

Permalink
fix: assign stmt with alias in mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Jan 22, 2025
1 parent 1cab221 commit 1242851
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2039,8 +2039,8 @@ column_list_item
as: null
};
}
/ a:select_assign_stmt {
return { expr: a, as: null }
/ a:select_assign_stmt __ alias:alias_clause? {
return { expr: a, as: alias }
}
/ e:binary_column_expr __ alias:alias_clause? {
return { expr: e, as: alias };
Expand Down
4 changes: 2 additions & 2 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2295,8 +2295,8 @@ column_list_item
...getLocationObject(),
};
}
/ a:select_assign_stmt {
return { expr: a, as: null }
/ a:select_assign_stmt __ alias:alias_clause? {
return { expr: a, as: alias }
}
/ e:binary_column_expr __ alias:alias_clause? {
return { expr: e, as: alias };
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 @@ -1199,6 +1199,13 @@ describe('mysql', () => {
"CREATE TABLE `table1` (`id` INT NOT NULL PRIMARY KEY, `data` VARCHAR(255) NOT NULL, `removed_id` VARCHAR(55) GENERATED ALWAYS AS (TRIM(TRAILING concat('.', substring_index(`data`, '.', -(3))) FROM TRIM(LEADING concat(substring_index(`data`, '.', 3), '.') FROM `data`))) STORED)"
]
},
{
title: 'assign with alias',
sql: [
'SELECT T2.id FROM ( SELECT @r AS _id, (SELECT @r := parent_id FROM product_category WHERE id = _id AND is_active = 1) AS parent_id, @l := @l + 1 AS lvl FROM (SELECT @r := :catId, @l := 0) vars, product_category h WHERE @r <> 0) T1 JOIN product_category T2 ON T1._id = T2.id ORDER BY T1.lvl DESC;',
'SELECT `T2`.`id` FROM (SELECT @r AS `_id`, (SELECT @r := `parent_id` FROM `product_category` WHERE `id` = `_id` AND `is_active` = 1) AS `parent_id`, @l := @l + 1 AS `lvl` FROM (SELECT @r := :catId, @l := 0) AS `vars`, `product_category` AS `h` WHERE @r <> 0) AS `T1` INNER JOIN `product_category` AS `T2` ON `T1`.`_id` = `T2`.`id` ORDER BY `T1`.`lvl` DESC',
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit 1242851

Please sign in to comment.