MCPcopy Create free account
hub / github.com/ChakshuGautam/stylusdb-sql / parseInsertQuery

Function parseInsertQuery

src/queryParser.js:182–227  ·  view source on GitHub ↗
(query)

Source from the content-addressed store, hash-verified

180}
181
182function 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
229function parseDeleteQuery(query) {
230 const deleteRegex = /DELETE FROM (\w+)( WHERE (.*))?/i;

Callers 1

executeINSERTQueryFunction · 0.85

Calls 1

cleanColumnNameFunction · 0.85

Tested by

no test coverage detected