Skip to content

Commit

Permalink
Merge pull request #1618 from taozhi8833998/feat-db-prefix-column-mysql
Browse files Browse the repository at this point in the history
Feat db prefix column mysql
  • Loading branch information
taozhi8833998 authored Oct 12, 2023
2 parents 5683af4 + 57a071e commit 588dba8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
45 changes: 37 additions & 8 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ show_stmt
}
}
}
/ KW_SHOW __ k:(('CHARACTER'i __ 'SET'i) / 'COLLATION'i) __ e:(like_op_right / where_clause)? {
/ KW_SHOW __ k:(('CHARACTER'i __ 'SET'i) / 'COLLATION'i / 'DATABASES'i) __ e:(like_op_right / where_clause)? {
let keyword = Array.isArray(k) && k || [k]
return {
tableList: Array.from(tableList),
Expand Down Expand Up @@ -1739,19 +1739,30 @@ column_list_item
const { as, ...expr } = fs
return { expr, as }
}
/ tbl:(ident __ DOT)? __ STAR {
const table = tbl && tbl[0] || null
columnList.add(`select::${table}::(.*)`);
/ db:ident __ DOT __ table:ident __ DOT __ STAR {
columnList.add(`select::${db}::${table}::(.*)`);
return {
expr: {
type: 'column_ref',
db: db,
table: table,
column: '*'
},
as: null
};
}
/ a:assign_stmt {
/ table:(ident __ DOT)? __ STAR {
columnList.add(`select::${table}::(.*)`);
return {
expr: {
type: 'column_ref',
table: table && table[0] || null,
column: '*'
},
as: null
};
}
/ a:select_assign_stmt {
return { expr: a, as: null }
}
/ e:binary_column_expr __ alias:alias_clause? {
Expand Down Expand Up @@ -2521,16 +2532,25 @@ primary
}

column_ref
= tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw {
// columnList.add(`select::${tbl}::${col}`);
= db:(ident_name / backticks_quoted_ident) __ DOT __ tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw {
columnList.add(`select::${db}::${tbl}::${col}`);
return {
type: 'column_ref',
db: db,
table: tbl,
column: col
};
}
/ tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw {
columnList.add(`select::${tbl}::${col}`);
return {
type: 'column_ref',
table: tbl,
column: col
};
}
/ col:column {
// columnList.add(`select::null::${col}`);
columnList.add(`select::null::${col}`);
return {
type: 'column_ref',
table: null,
Expand Down Expand Up @@ -3435,6 +3455,15 @@ assign_stmt
};
}

select_assign_stmt
= va:(var_decl / without_prefix_var_decl) __ s:KW_ASSIGN __ e:proc_expr {
return {
type: 'assign',
left: va,
symbol: s,
right: e
};
}

return_stmt
= KW_RETURN __ e:proc_expr {
Expand Down
3 changes: 0 additions & 3 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,6 @@ column_list_item
return {
expr: {
type: 'column_ref',
db: null,
table: table && table[0] || null,
column: '*'
},
Expand Down Expand Up @@ -2838,7 +2837,6 @@ column_ref
columnList.add(`select::${tbl}::${col}`);
return {
type: 'column_ref',
db: null,
table: tbl,
column: col
};
Expand All @@ -2847,7 +2845,6 @@ column_ref
columnList.add(`select::null::${col}`);
return {
type: 'column_ref',
db: null,
table: null,
column: col
};
Expand Down
6 changes: 3 additions & 3 deletions src/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ function columnOffsetToSQL(column, isDual) {
}
function columnRefToSQL(expr) {
const {
array_index, arrows = [], as, collate, column, isDual, schema, table, parentheses, properties,
array_index, arrows = [], as, collate, column, db, isDual, schema, table, parentheses, properties,
suffix, order_by, subFields = [],
} = expr
let str = column === '*' ? '*' : columnOffsetToSQL(column, isDual)
if (table) str = `${identifierToSql(table)}.${str}`
if (schema) str = `${identifierToSql(schema)}.${str}`
const prefix = [schema, db, table].filter(hasVal).map(val => `${identifierToSql(val)}`).join('.')
if (prefix) str = `${prefix}.${str}`
if (array_index) {
str = `${str}[${literalToSQL(array_index.index)}]`
if (array_index.property) str = `${str}.${literalToSQL(array_index.property)}`
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 @@ -804,6 +804,13 @@ describe('mysql', () => {
'CREATE TABLE `users` (`id` INT(11) NOT NULL) ENGINE = INNODB AUTO_INCREMENT = 10833 DEFAULT CHARSET = utf8 CHECKSUM = 1 DELAY_KEY_WRITE = 1 ROW_FORMAT = DYNAMIC'
]
},
{
title: 'column with db prefix',
sql: [
'SELECT d.t.* FROM d.t',
'SELECT `d`.`t`.* FROM `d`.`t`'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down
2 changes: 1 addition & 1 deletion test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('select', () => {
it('should parse "table.*" column expressions', () => {
const ast = parser.astify('SELECT t.* FROM t');
expect(ast.columns).to.eql([
{ expr: { type: 'column_ref', 'table': 't', column: '*' }, as: null }
{ expr: { type: 'column_ref', table: 't', column: '*' }, as: null }
]);
});
it('should parse json column query expressions', () => {
Expand Down

0 comments on commit 588dba8

Please sign in to comment.