(query)
| 180 | } |
| 181 | |
| 182 | function parseInsertQuery(query) { |
| 183 | // Simplify the query by removing schema names and table references from column names |
| 184 | let simplifiedQuery = query.replace(/"?\w+"?\."(\w+)"?/g, '$1'); |
| 185 | |
| 186 | // Parse the INSERT INTO part |
| 187 | const insertRegex = /INSERT INTO "?(\w+)"?\s\(([^)]+)\)\sVALUES\s\(([^)]+)\)/i; |
| 188 | const match = simplifiedQuery.match(insertRegex); |
| 189 | |
| 190 | if (!match) { |
| 191 | throw new Error("Invalid INSERT INTO syntax."); |
| 192 | } |
| 193 | |
| 194 | const [, table, columns, values] = match; |
| 195 | |
| 196 | // Function to clean and remove surrounding quotes from column names |
| 197 | const cleanColumnName = (name) => { |
| 198 | return name.trim().replace(/^"?(.+?)"?$/g, '$1'); |
| 199 | }; |
| 200 | |
| 201 | // Function to clean and remove surrounding single quotes from values |
| 202 | const cleanValue = (value) => { |
| 203 | return value.trim().replace(/^'(.*)'$/g, '$1').replace(/^"(.*)"$/g, '$1'); |
| 204 | }; |
| 205 | |
| 206 | // Function to clean returning column names by removing table prefixes and quotes |
| 207 | const cleanReturningColumn = (name) => { |
| 208 | return name.trim().replace(/\w+\./g, '').replace(/^"?(.+?)"?$/g, '$1'); |
| 209 | }; |
| 210 | |
| 211 | // Parse and clean columns and values |
| 212 | const parsedColumns = columns.split(',').map(cleanColumnName); |
| 213 | const parsedValues = values.split(',').map(cleanValue); |
| 214 | |
| 215 | // Parse the RETURNING part if present |
| 216 | const returningMatch = simplifiedQuery.match(/RETURNING\s(.+)$/i); |
| 217 | const returningColumns = returningMatch |
| 218 | ? returningMatch[1].split(',').map(cleanReturningColumn) |
| 219 | : []; |
| 220 | return { |
| 221 | type: 'INSERT', |
| 222 | table: cleanColumnName(table), |
| 223 | columns: parsedColumns, |
| 224 | values: parsedValues, |
| 225 | returningColumns |
| 226 | }; |
| 227 | } |
| 228 | |
| 229 | function parseDeleteQuery(query) { |
| 230 | const deleteRegex = /DELETE FROM (\w+)( WHERE (.*))?/i; |
no test coverage detected