Skip to content

Commit

Permalink
Merge pull request #2161 from taozhi8833998/refactor-table-list-item
Browse files Browse the repository at this point in the history
refactor: add schema and server name in tableList
  • Loading branch information
taozhi8833998 authored Oct 10, 2024
2 parents 099e070 + 223b74e commit 71a0ffc
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 111 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ const tableList = parser.tableList('SELECT * FROM t', opt);

console.log(tableList); // ["select::null::t"]
```
- if the table name is prefixed with database name, the table name will be parsed as **dbName::tableName**
- if the table name is prefixed with database and schema name, the table name will be parsed as **dbName.schemaName::tableName**
- if the table name is prefixed with server name in TransactSQL, the table name will be parsed as **serverName.dbName.schemaName::tableName**

### Get the SQL visited columns

Expand Down
34 changes: 18 additions & 16 deletions pegjs/flinksql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ create_table_stmt
ir: (KW_IGNORE / KW_REPLACE)? __
as: KW_AS? __
qe: union_stmt? {
if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`create::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand All @@ -693,7 +693,7 @@ create_table_stmt
t:table_ref_list __
wr:(KW_WITH __ LPAREN __ with_table_options __ RPAREN)? __
lt:create_like_table {
if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`create::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -934,7 +934,7 @@ drop_stmt
= a:KW_DROP __
r:KW_TABLE __
t:table_ref_list {
if(t) t.forEach(tt => tableList.add(`${a}::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`${a}::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -975,7 +975,7 @@ truncate_stmt
}
=> AstStatement<truncate_stmt_node>
*/
if(t) t.forEach(tt => tableList.add(`${a}::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`${a}::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -1021,7 +1021,7 @@ alter_table_stmt
}
=> AstStatement<alter_table_stmt_node>
*/
if (t && t.length > 0) t.forEach(table => tableList.add(`alter::${table.db}::${table.table}`));
if (t && t.length > 0) t.forEach(table => tableList.add(`alter::${[table.db, table.schema].filter(Boolean).join('.') || null}::${table.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -1518,7 +1518,7 @@ rename_stmt
}
=> AstStatement<rename_stmt_node>
*/
t.forEach(tg => tg.forEach(dt => dt.table && tableList.add(`rename::${dt.db}::${dt.table}`)))
t.forEach(tg => tg.forEach(dt => dt.table && tableList.add(`rename::${[dt.db, dt.schema].filter(Boolean).join('.') || null}::${dt.table}`)))
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -1580,7 +1580,7 @@ lock_stmt
=> AstStatement<lock_stmt_node>
*/

if (t) t.forEach(tt => tableList.add(`lock::${tt.db}::${tt.table}`))
if (t) t.forEach(tt => tableList.add(`lock::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`))
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -1677,7 +1677,7 @@ select_stmt_nake
orderby?: order_by_clause;
limit?: limit_clause;
}*/
if(f) f.forEach(info => info.table && tableList.add(`select::${info.db}::${info.table}`));
if(f) f.forEach(info => info.table && tableList.add(`select::${[info.db, info.schema].filter(Boolean).join('.') || null}::${info.table}`));
return {
with: cte,
type: 'select',
Expand Down Expand Up @@ -1955,7 +1955,6 @@ table_name
}
/ dt:ident __ DOT __ STAR {
// => IGNORE
tableList.add(`select::${dt}::(.*)`);
return {
db: dt,
table: '*'
Expand Down Expand Up @@ -2060,10 +2059,11 @@ update_stmt
*/
const dbObj = {}
if (t) t.forEach(tableInfo => {
const { db, as, table, join } = tableInfo
const { db, as, schema, table, join } = tableInfo
const action = join ? 'select' : 'update'
if (db) dbObj[table] = db
if (table) tableList.add(`${action}::${db}::${table}`)
const fullName = [db, schema].filter(Boolean).join('.') || null
if (db) dbObj[table] = fullName
if (table) tableList.add(`${action}::${fullName}::${table}`)
});
if(l) {
l.forEach(col => {
Expand Down Expand Up @@ -2105,15 +2105,17 @@ delete_stmt
=> AstStatement<delete_stmt_node>
*/
if(f) f.forEach(tableInfo => {
const { db, as, table, join } = tableInfo
const { db, schema, as, table, join } = tableInfo
const action = join ? 'select' : 'delete'
if (table) tableList.add(`${action}::${db}::${table}`)
const fullName = [db, schema].filter(Boolean).join('.') || null
if (table) tableList.add(`${action}::${fullName}::${table}`)
if (!join) columnList.add(`delete::${table}::(.*)`);
});
if (t === null && f.length === 1) {
const tableInfo = f[0]
t = [{
db: tableInfo.db,
schema: tableInfo.schema,
table: tableInfo.table,
as: tableInfo.as,
addition: true
Expand Down Expand Up @@ -2192,7 +2194,7 @@ replace_insert_stmt
=> AstStatement<replace_insert_stmt_node>
*/
if (t) {
tableList.add(`insert::${t.db}::${t.table}`)
tableList.add(`insert::${[t.db, t.schema].filter(Boolean).join('.') || null}::${t.table}`)
t.as = null
}
if (c) {
Expand Down Expand Up @@ -2230,7 +2232,7 @@ insert_no_columns_stmt
r:returning_stmt? {
// => AstStatement<replace_insert_stmt_node>
if (t) {
tableList.add(`insert::${t.db}::${t.table}`)
tableList.add(`insert::${[t.db, t.schema].filter(Boolean).join('.') || null}::${t.table}`)
columnList.add(`insert::${t.table}::(.*)`);
t.as = null
}
Expand Down
33 changes: 18 additions & 15 deletions pegjs/noql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ create_table_stmt
}
=> AstStatement<create_table_stmt_node>
*/
if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`create::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -829,7 +829,7 @@ create_table_stmt
}
=> AstStatement<create_table_stmt_node>;
*/
if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`create::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -1290,7 +1290,7 @@ drop_stmt
}
=> AstStatement<drop_stmt_node>
*/
if(t) t.forEach(tt => tableList.add(`${a}::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`${a}::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -1342,7 +1342,7 @@ truncate_stmt
}
=> AstStatement<truncate_stmt_node>
*/
if(t) t.forEach(tt => tableList.add(`${a}::${tt.db}::${tt.table}`));
if(t) t.forEach(tt => tableList.add(`${a}::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -1526,7 +1526,7 @@ alter_table_stmt
}
=> AstStatement<alter_table_stmt_node>
*/
if (t && t.length > 0) t.forEach(table => tableList.add(`alter::${table.db}::${table.table}`));
if (t && t.length > 0) t.forEach(table => tableList.add(`alter::${[table.db, table.schema].filter(Boolean).join('.') || null}::${table.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -2124,7 +2124,7 @@ rename_stmt
}
=> AstStatement<rename_stmt_node>
*/
t.forEach(tg => tg.forEach(dt => dt.table && tableList.add(`rename::${dt.db}::${dt.table}`)))
t.forEach(tg => tg.forEach(dt => dt.table && tableList.add(`rename::${[dt.db, dt.schema].filter(Boolean).join('.') || null}::${dt.table}`)))
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -2179,7 +2179,7 @@ lock_stmt
=> AstStatement<lock_stmt_node>
*/

if (t) t.forEach(tt => tableList.add(`lock::${tt.db}::${tt.table}`))
if (t) t.forEach(tt => tableList.add(`lock::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`))
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand Down Expand Up @@ -2737,7 +2737,7 @@ select_stmt_nake
if ((ci && fi) || (ci && li) || (fi && li) || (ci && fi && li)) {
throw new Error('A given SQL statement can contain at most one INTO clause')
}
if(f) f.forEach(info => info.table && tableList.add(`select::${info.db}::${info.table}`));
if(f) f.forEach(info => info.table && tableList.add(`select::${[info.db, info.schema].filter(Boolean).join('.') || null}::${info.table}`));
return {
with: cte,
type: 'select',
Expand Down Expand Up @@ -3318,10 +3318,11 @@ update_stmt
*/
const dbObj = {}
if (t) t.forEach(tableInfo => {
const { db, as, table, join } = tableInfo
const { db, as, schema, table, join } = tableInfo
const action = join ? 'select' : 'update'
if (db) dbObj[table] = db
if (table) tableList.add(`${action}::${db}::${table}`)
const fullName = [db, schema].filter(Boolean).join('.') || null
if (db) dbObj[table] = fullName
if (table) tableList.add(`${action}::${fullName}::${table}`)
});
if(l) {
l.forEach(col => {
Expand Down Expand Up @@ -3365,15 +3366,17 @@ delete_stmt
=> AstStatement<delete_stmt_node>
*/
if(f) f.forEach(tableInfo => {
const { db, as, table, join } = tableInfo
const { db, as, schema, table, join } = tableInfo
const action = join ? 'select' : 'delete'
if (table) tableList.add(`${action}::${db}::${table}`)
const fullName = [db, schema].filter(Boolean).join('.') || null
if (table) tableList.add(`${action}::${fullName}::${table}`)
if (!join) columnList.add(`delete::${table}::(.*)`);
});
if (t === null && f.length === 1) {
const tableInfo = f[0]
t = [{
db: tableInfo.db,
schema: tableInfo.schema,
table: tableInfo.table,
as: tableInfo.as,
addition: true
Expand Down Expand Up @@ -3499,7 +3502,7 @@ replace_insert_stmt
=> AstStatement<replace_insert_stmt_node>
*/
if (t) {
tableList.add(`insert::${t.db}::${t.table}`)
tableList.add(`insert::${[t.db, t.schema].filter(Boolean).join('.') || null}::${t.table}`)
t.as = null
}
if (c) {
Expand Down Expand Up @@ -3538,7 +3541,7 @@ insert_no_columns_stmt
r:returning_stmt? {
// => AstStatement<replace_insert_stmt_node>
if (t) {
tableList.add(`insert::${t.db}::${t.table}`)
tableList.add(`insert::${[t.db, t.schema].filter(Boolean).join('.') || null}::${t.table}`)
columnList.add(`insert::${t.table}::(.*)`);
t.as = null
}
Expand Down
Loading

0 comments on commit 71a0ffc

Please sign in to comment.