MCPcopy
hub / github.com/sequelize/sequelize / parseConstraintsFromSql

Method parseConstraintsFromSql

lib/dialects/sqlite/query.js:285–333  ·  view source on GitHub ↗
(sql)

Source from the content-addressed store, hash-verified

283 }
284
285 parseConstraintsFromSql(sql) {
286 let constraints = sql.split('CONSTRAINT ');
287 let referenceTableName, referenceTableKeys, updateAction, deleteAction;
288 constraints.splice(0, 1);
289 constraints = constraints.map(constraintSql => {
290 //Parse foreign key snippets
291 if (constraintSql.includes('REFERENCES')) {
292 //Parse out the constraint condition form sql string
293 updateAction = constraintSql.match(/ON UPDATE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/);
294 deleteAction = constraintSql.match(/ON DELETE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/);
295
296 if (updateAction) {
297 updateAction = updateAction[1];
298 }
299
300 if (deleteAction) {
301 deleteAction = deleteAction[1];
302 }
303
304 const referencesRegex = /REFERENCES.+\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/;
305 const referenceConditions = constraintSql.match(referencesRegex)[0].split(' ');
306 referenceTableName = Utils.removeTicks(referenceConditions[1]);
307 let columnNames = referenceConditions[2];
308 columnNames = columnNames.replace(/\(|\)/g, '').split(', ');
309 referenceTableKeys = columnNames.map(column => Utils.removeTicks(column));
310 }
311
312 const constraintCondition = constraintSql.match(/\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/)[0];
313 constraintSql = constraintSql.replace(/\(.+\)/, '');
314 const constraint = constraintSql.split(' ');
315
316 if (['PRIMARY', 'FOREIGN'].includes(constraint[1])) {
317 constraint[1] += ' KEY';
318 }
319
320 return {
321 constraintName: Utils.removeTicks(constraint[0]),
322 constraintType: constraint[1],
323 updateAction,
324 deleteAction,
325 sql: sql.replace(/"/g, '`'), //Sqlite returns double quotes for table name
326 constraintCondition,
327 referenceTableName,
328 referenceTableKeys
329 };
330 });
331
332 return constraints;
333 }
334
335 applyParsers(type, value) {
336 if (type.includes('(')) {

Callers 1

_handleQueryResponseMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected