(sqlText: string)
| 188 | * @throws Error if the SQL cannot be parsed |
| 189 | */ |
| 190 | export function parseSQLStatements(sqlText: string): string[] { |
| 191 | if (!sqlText || typeof sqlText !== 'string') { |
| 192 | throw new Error('SQL text must be a non-empty string'); |
| 193 | } |
| 194 | |
| 195 | try { |
| 196 | const sqlQuery = sql`${sql.__dangerous__rawValue(sqlText)}`; |
| 197 | const splitResults = splitSqlQuery(sqlQuery); |
| 198 | |
| 199 | const statements = splitResults |
| 200 | .map((query) => { |
| 201 | const formatted = query.format({ |
| 202 | escapeIdentifier: (str: string) => `"${str}"`, |
| 203 | formatValue: (_value: unknown, index: number) => ({ |
| 204 | placeholder: `$${index + 1}`, |
| 205 | value: _value, |
| 206 | }), |
| 207 | }); |
| 208 | return formatted.text.trim(); |
| 209 | }) |
| 210 | .filter((s) => { |
| 211 | const withoutComments = s |
| 212 | .replace(/--.*$/gm, '') |
| 213 | .replace(/\/\*[\s\S]*?\*\//g, '') |
| 214 | .trim(); |
| 215 | return withoutComments.length; |
| 216 | }); |
| 217 | |
| 218 | logger.debug(`Parsed ${statements.length} SQL statements from input`); |
| 219 | return statements; |
| 220 | } catch (parseError) { |
| 221 | logger.error('Failed to parse SQL:', parseError); |
| 222 | throw new Error( |
| 223 | `Invalid SQL format: ${parseError instanceof Error ? parseError.message : String(parseError)}` |
| 224 | ); |
| 225 | } |
| 226 | } |
no outgoing calls
no test coverage detected