(sql)
| 303 | } |
| 304 | |
| 305 | parseConstraintsFromSql(sql) { |
| 306 | let constraints = sql.split('CONSTRAINT '); |
| 307 | let referenceTableName, referenceTableKeys, updateAction, deleteAction; |
| 308 | constraints.splice(0, 1); |
| 309 | constraints = constraints.map(constraintSql => { |
| 310 | //Parse foreign key snippets |
| 311 | if (constraintSql.includes('REFERENCES')) { |
| 312 | //Parse out the constraint condition form sql string |
| 313 | updateAction = constraintSql.match(/ON UPDATE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/); |
| 314 | deleteAction = constraintSql.match(/ON DELETE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/); |
| 315 | |
| 316 | if (updateAction) { |
| 317 | updateAction = updateAction[1]; |
| 318 | } |
| 319 | |
| 320 | if (deleteAction) { |
| 321 | deleteAction = deleteAction[1]; |
| 322 | } |
| 323 | |
| 324 | const referencesRegex = /REFERENCES.+\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/; |
| 325 | const referenceConditions = constraintSql.match(referencesRegex)[0].split(' '); |
| 326 | referenceTableName = Utils.removeTicks(referenceConditions[1]); |
| 327 | let columnNames = referenceConditions[2]; |
| 328 | columnNames = columnNames.replace(/\(|\)/g, '').split(', '); |
| 329 | referenceTableKeys = columnNames.map(column => Utils.removeTicks(column)); |
| 330 | } |
| 331 | |
| 332 | const constraintCondition = constraintSql.match(/\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/)[0]; |
| 333 | constraintSql = constraintSql.replace(/\(.+\)/, ''); |
| 334 | const constraint = constraintSql.split(' '); |
| 335 | |
| 336 | if (['PRIMARY', 'FOREIGN'].includes(constraint[1])) { |
| 337 | constraint[1] += ' KEY'; |
| 338 | } |
| 339 | |
| 340 | return { |
| 341 | constraintName: Utils.removeTicks(constraint[0]), |
| 342 | constraintType: constraint[1], |
| 343 | updateAction, |
| 344 | deleteAction, |
| 345 | sql: sql.replace(/"/g, '`'), //Sqlite returns double quotes for table name |
| 346 | constraintCondition, |
| 347 | referenceTableName, |
| 348 | referenceTableKeys |
| 349 | }; |
| 350 | }); |
| 351 | |
| 352 | return constraints; |
| 353 | } |
| 354 | |
| 355 | applyParsers(type, value) { |
| 356 | if (type.includes('(')) { |
no outgoing calls
no test coverage detected