|
1 | 1 | import dayjs from 'dayjs' |
2 | 2 | import type { SchemaType } from '@/store/modules/code-run/types' |
3 | 3 | import type { TSColumn } from '@/types/query' |
| 4 | +import { getTableRefForSql } from '@/utils/sql' |
4 | 5 |
|
5 | 6 | function findWhereClausePosition(sql: string) { |
6 | 7 | // Normalize case for easier comparison |
@@ -93,17 +94,20 @@ export function addTsCondition(sql: string, column: string, start: number | stri |
93 | 94 | return `${sql.slice(0, whereIndex - 1)} WHERE ${column} >= ${start} and ${column} < ${end} ${sql.slice(whereIndex)}` |
94 | 95 | } |
95 | 96 |
|
96 | | -export const TableNameReg = /(?<=from|FROM)\s+([^\s;]+)/ |
| 97 | +export const TableNameReg = /(?<=from|FROM)\s+([^\s;]+)/i |
| 98 | + |
| 99 | +/** |
| 100 | + * Parse table reference after FROM and normalize it to a safe, quoted identifier: |
| 101 | + * - "db"."table" → "db"."table" |
| 102 | + * - db.table → "db"."table" |
| 103 | + * - table → "table" |
| 104 | + */ |
97 | 105 | export function parseTable(sql: string) { |
98 | 106 | const result = sql.match(TableNameReg) |
99 | | - if (result && result.length) { |
100 | | - const tableName = result[1].trim() |
101 | | - // Remove quotes if they exist |
102 | | - const cleanTableName = tableName.replace(/^["'`]|["'`]$/g, '') |
103 | | - const arr = cleanTableName.split('.') |
104 | | - return arr[arr.length - 1] |
105 | | - } |
106 | | - return '' |
| 107 | + if (!result?.[1]) return '' |
| 108 | + const raw = result[1].trim() |
| 109 | + // Let SQL utils handle proper quoting / escaping for identifiers |
| 110 | + return getTableRefForSql({ table: raw }) |
107 | 111 | } |
108 | 112 |
|
109 | 113 | export function parseTimeRange(sql: string, tsColumn: string, multiple: number): string[] | number { |
|
0 commit comments