Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,20 +2132,27 @@ table_ref_list
})
return tail;
}
/ lp:LPAREN+ __ head:table_base tail:table_ref* __ rp:RPAREN+ {
if (lp.length !== rp.length) throw new Error('parentheses not match in from clause')
/ lp:LPAREN+ __ head:table_base tail:table_ref* __ rp:RPAREN+ __ jt:table_ref* {
if (lp.length !== rp.length) throw new Error(`parentheses not match in from clause: ${lp.length} != ${rp.length}`)
tail.unshift(head);
tail.forEach(tableInfo => {
const { table, as } = tableInfo
tableAlias[table] = table
if (as) tableAlias[as] = table
refreshColumnList(columnList)
})
jt.forEach(tableInfo => {
const { table, as } = tableInfo
tableAlias[table] = table
if (as) tableAlias[as] = table
refreshColumnList(columnList)
})
return {
expr: tail,
parentheses: {
length: rp.length
}
},
joins: jt
}
}

Expand Down
11 changes: 9 additions & 2 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,7 @@ table_ref_list
})
return tail;
}
/ lp:LPAREN+ __ head:table_base tail:table_ref* __ rp:RPAREN+ {
/ lp:LPAREN+ __ head:table_base tail:table_ref* __ rp:RPAREN+ __ jt:table_ref* {
if (lp.length !== rp.length) throw new Error(`parentheses not match in from clause: ${lp.length} != ${rp.length}`)
tail.unshift(head);
tail.forEach(tableInfo => {
Expand All @@ -2405,11 +2405,18 @@ table_ref_list
if (as) tableAlias[as] = table
refreshColumnList(columnList)
})
jt.forEach(tableInfo => {
const { table, as } = tableInfo
tableAlias[table] = table
if (as) tableAlias[as] = table
refreshColumnList(columnList)
})
return {
expr: tail,
parentheses: {
length: rp.length
}
},
joins: jt
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function tableToSQL(tableInfo) {
function tablesToSQL(tables) {
if (!tables) return ''
if (!Array.isArray(tables)) {
const { expr, parentheses } = tables
const { expr, parentheses, joins } = tables
const sql = tablesToSQL(expr)
if (parentheses) {
const leftParentheses = []
Expand All @@ -171,7 +171,8 @@ function tablesToSQL(tables) {
leftParentheses.push('(')
rightParentheses.push(')')
}
return leftParentheses.join('') + sql + rightParentheses.join('')
const joinsSQL = joins && joins.length > 0 ? tablesToSQL(['', ...joins]) : ''
return leftParentheses.join('') + sql + rightParentheses.join('') + joinsSQL
}
return sql
}
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 @@ -1206,6 +1206,13 @@ describe('mysql', () => {
'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',
]
},
{
title: 'table ref list with join after parentheses',
sql: [
'select * from (`table`, `table2` as `t2`, `jacob` as `jacobian`) left join`table3` as `t3` on`t2`.`id` = `t3`.`table2_id`',
'SELECT * FROM (`table`, `table2` AS `t2`, `jacob` AS `jacobian`) LEFT JOIN `table3` AS `t3` ON `t2`.`id` = `t3`.`table2_id`'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down