Skip to content

Commit

Permalink
Merge pull request #2079 from taozhi8833998/fix-from-table-in-parenth…
Browse files Browse the repository at this point in the history
…eses-mysql

fix: from table with alias in parentheses
  • Loading branch information
taozhi8833998 authored Aug 24, 2024
2 parents 441b938 + ee57f22 commit c43188c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,7 +2170,7 @@ table_base
};
}
}
/ LPAREN __ t:table_name __ r:RPAREN __ alias:alias_clause? {
/ LPAREN __ t:table_name __ alias:alias_clause? __ r:RPAREN {
const parentheses = true
if (t.type === 'var') {
t.as = alias;
Expand Down
2 changes: 1 addition & 1 deletion pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,7 @@ table_base
...getLocationObject(),
};
}
/ LPAREN __ t:table_name __ r:RPAREN __ alias:alias_clause? {
/ LPAREN __ t:table_name __ alias:alias_clause? __ r:RPAREN {
const parentheses = true
if (t.type === 'var') {
t.as = alias;
Expand Down
6 changes: 3 additions & 3 deletions src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ function tableToSQL(tableInfo) {
}
}
tableName = [toUpper(prefixStr), tableName, toUpper(suffix)].filter(hasVal).join(' ')
let str = [serverName, database, schemaStr, tableName].filter(hasVal).join('.')
if (tableInfo.parentheses) str = `(${str})`
const str = [serverName, database, schemaStr, tableName].filter(hasVal).join('.')
const result = [str]
if (tablesample) {
const tableSampleSQL = ['TABLESAMPLE', exprToSQL(tablesample.expr), literalToSQL(tablesample.repeatable)].filter(hasVal).join(' ')
result.push(tableSampleSQL)
}
result.push(temporalTableToSQL(temporal_table), commonOptionConnector('AS', identifierToSql, as), operatorToSQL(operator))
if (table_hint) result.push(toUpper(table_hint.keyword), `(${table_hint.expr.map(tableHintToSQL).filter(hasVal).join(', ')})`)
return result.filter(hasVal).join(' ')
const tableSQL = result.filter(hasVal).join(' ')
return tableInfo.parentheses ? `(${tableSQL})` : tableSQL
}

/**
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 @@ -1094,6 +1094,13 @@ describe('mysql', () => {
"INSERT INTO `test` (t1, t2, t3, t4, t5) VALUES (0,1,334,'21.42',' '), (0,1,335,'23.lua',' select(\\'#\\', ...)'), (0,1,334,'21.42',' ')"
]
},
{
title: 'from table alias in parentheses',
sql: [
"SELECT DISTINCT `pv`.`text` as product_page_text, `pv`.`lp_url`, `pp`.`short_label` as product_item_text, `pp`.`id` as promoid, `pv`.`id` as visibility_id, `pp`.`tnc` FROM (`product_promotion` pp) JOIN `promotion_visibility` pv ON `pv`.`promotion_id`=`pp`.`id` WHERE `pv`.`property` = 'app_product_page_text' AND `pp`.`start_date` < 1724305037 AND `pp`.`end_date` > 1724305037 AND `pv`.`is_active` = 1 AND `pp`.`is_active` = 1 AND `pp`.`id` IN ('5725,8560') ORDER BY `pp`.`priority` DESC",
"SELECT DISTINCT `pv`.`text` AS `product_page_text`, `pv`.`lp_url`, `pp`.`short_label` AS `product_item_text`, `pp`.`id` AS `promoid`, `pv`.`id` AS `visibility_id`, `pp`.`tnc` FROM (`product_promotion` AS `pp`) INNER JOIN `promotion_visibility` AS `pv` ON `pv`.`promotion_id` = `pp`.`id` WHERE `pv`.`property` = 'app_product_page_text' AND `pp`.`start_date` < 1724305037 AND `pp`.`end_date` > 1724305037 AND `pv`.`is_active` = 1 AND `pp`.`is_active` = 1 AND `pp`.`id` IN ('5725,8560') ORDER BY `pp`.`priority` DESC",
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit c43188c

Please sign in to comment.