query.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. 'use strict';
  2. const _ = require('lodash');
  3. const Utils = require('../../utils');
  4. const AbstractQuery = require('../abstract/query');
  5. const QueryTypes = require('../../query-types');
  6. const sequelizeErrors = require('../../errors');
  7. const parserStore = require('../parserStore')('sqlite');
  8. const { logger } = require('../../utils/logger');
  9. const debug = logger.debugContext('sql:sqlite');
  10. class Query extends AbstractQuery {
  11. getInsertIdField() {
  12. return 'lastID';
  13. }
  14. /**
  15. * rewrite query with parameters.
  16. *
  17. * @param {string} sql
  18. * @param {Array|object} values
  19. * @param {string} dialect
  20. * @private
  21. */
  22. static formatBindParameters(sql, values, dialect) {
  23. let bindParam;
  24. if (Array.isArray(values)) {
  25. bindParam = {};
  26. values.forEach((v, i) => {
  27. bindParam[`$${i + 1}`] = v;
  28. });
  29. sql = AbstractQuery.formatBindParameters(sql, values, dialect, { skipValueReplace: true })[0];
  30. } else {
  31. bindParam = {};
  32. if (typeof values === 'object') {
  33. for (const k of Object.keys(values)) {
  34. bindParam[`$${k}`] = values[k];
  35. }
  36. }
  37. sql = AbstractQuery.formatBindParameters(sql, values, dialect, { skipValueReplace: true })[0];
  38. }
  39. return [sql, bindParam];
  40. }
  41. _collectModels(include, prefix) {
  42. const ret = {};
  43. if (include) {
  44. for (const _include of include) {
  45. let key;
  46. if (!prefix) {
  47. key = _include.as;
  48. } else {
  49. key = `${prefix}.${_include.as}`;
  50. }
  51. ret[key] = _include.model;
  52. if (_include.include) {
  53. _.merge(ret, this._collectModels(_include.include, key));
  54. }
  55. }
  56. }
  57. return ret;
  58. }
  59. _handleQueryResponse(metaData, columnTypes, err, results) {
  60. if (err) {
  61. err.sql = this.sql;
  62. throw this.formatError(err);
  63. }
  64. let result = this.instance;
  65. // add the inserted row id to the instance
  66. if (this.isInsertQuery(results, metaData) || this.isUpsertQuery()) {
  67. this.handleInsertQuery(results, metaData);
  68. if (!this.instance) {
  69. // handle bulkCreate AI primary key
  70. if (
  71. metaData.constructor.name === 'Statement'
  72. && this.model
  73. && this.model.autoIncrementAttribute
  74. && this.model.autoIncrementAttribute === this.model.primaryKeyAttribute
  75. && this.model.rawAttributes[this.model.primaryKeyAttribute]
  76. ) {
  77. const startId = metaData[this.getInsertIdField()] - metaData.changes + 1;
  78. result = [];
  79. for (let i = startId; i < startId + metaData.changes; i++) {
  80. result.push({ [this.model.rawAttributes[this.model.primaryKeyAttribute].field]: i });
  81. }
  82. } else {
  83. result = metaData[this.getInsertIdField()];
  84. }
  85. }
  86. }
  87. if (this.isShowTablesQuery()) {
  88. return results.map(row => row.name);
  89. }
  90. if (this.isShowConstraintsQuery()) {
  91. result = results;
  92. if (results && results[0] && results[0].sql) {
  93. result = this.parseConstraintsFromSql(results[0].sql);
  94. }
  95. return result;
  96. }
  97. if (this.isSelectQuery()) {
  98. if (this.options.raw) {
  99. return this.handleSelectQuery(results);
  100. }
  101. // This is a map of prefix strings to models, e.g. user.projects -> Project model
  102. const prefixes = this._collectModels(this.options.include);
  103. results = results.map(result => {
  104. return _.mapValues(result, (value, name) => {
  105. let model;
  106. if (name.includes('.')) {
  107. const lastind = name.lastIndexOf('.');
  108. model = prefixes[name.substr(0, lastind)];
  109. name = name.substr(lastind + 1);
  110. } else {
  111. model = this.options.model;
  112. }
  113. const tableName = model.getTableName().toString().replace(/`/g, '');
  114. const tableTypes = columnTypes[tableName] || {};
  115. if (tableTypes && !(name in tableTypes)) {
  116. // The column is aliased
  117. _.forOwn(model.rawAttributes, (attribute, key) => {
  118. if (name === key && attribute.field) {
  119. name = attribute.field;
  120. return false;
  121. }
  122. });
  123. }
  124. return Object.prototype.hasOwnProperty.call(tableTypes, name)
  125. ? this.applyParsers(tableTypes[name], value)
  126. : value;
  127. });
  128. });
  129. return this.handleSelectQuery(results);
  130. }
  131. if (this.isShowOrDescribeQuery()) {
  132. return results;
  133. }
  134. if (this.sql.includes('PRAGMA INDEX_LIST')) {
  135. return this.handleShowIndexesQuery(results);
  136. }
  137. if (this.sql.includes('PRAGMA INDEX_INFO')) {
  138. return results;
  139. }
  140. if (this.sql.includes('PRAGMA TABLE_INFO')) {
  141. // this is the sqlite way of getting the metadata of a table
  142. result = {};
  143. let defaultValue;
  144. for (const _result of results) {
  145. if (_result.dflt_value === null) {
  146. // Column schema omits any "DEFAULT ..."
  147. defaultValue = undefined;
  148. } else if (_result.dflt_value === 'NULL') {
  149. // Column schema is a "DEFAULT NULL"
  150. defaultValue = null;
  151. } else {
  152. defaultValue = _result.dflt_value;
  153. }
  154. result[_result.name] = {
  155. type: _result.type,
  156. allowNull: _result.notnull === 0,
  157. defaultValue,
  158. primaryKey: _result.pk !== 0
  159. };
  160. if (result[_result.name].type === 'TINYINT(1)') {
  161. result[_result.name].defaultValue = { '0': false, '1': true }[result[_result.name].defaultValue];
  162. }
  163. if (typeof result[_result.name].defaultValue === 'string') {
  164. result[_result.name].defaultValue = result[_result.name].defaultValue.replace(/'/g, '');
  165. }
  166. }
  167. return result;
  168. }
  169. if (this.sql.includes('PRAGMA foreign_keys;')) {
  170. return results[0];
  171. }
  172. if (this.sql.includes('PRAGMA foreign_keys')) {
  173. return results;
  174. }
  175. if (this.sql.includes('PRAGMA foreign_key_list')) {
  176. return results;
  177. }
  178. if ([QueryTypes.BULKUPDATE, QueryTypes.BULKDELETE].includes(this.options.type)) {
  179. return metaData.changes;
  180. }
  181. if (this.options.type === QueryTypes.VERSION) {
  182. return results[0].version;
  183. }
  184. if (this.options.type === QueryTypes.RAW) {
  185. return [results, metaData];
  186. }
  187. if (this.isUpsertQuery()) {
  188. return [result, null];
  189. }
  190. if (this.isUpdateQuery() || this.isInsertQuery()) {
  191. return [result, metaData.changes];
  192. }
  193. return result;
  194. }
  195. async run(sql, parameters) {
  196. const conn = this.connection;
  197. this.sql = sql;
  198. const method = this.getDatabaseMethod();
  199. const complete = this._logQuery(sql, debug, parameters);
  200. return new Promise((resolve, reject) => conn.serialize(async () => {
  201. const columnTypes = {};
  202. const executeSql = () => {
  203. if (sql.startsWith('-- ')) {
  204. return resolve();
  205. }
  206. const query = this;
  207. // cannot use arrow function here because the function is bound to the statement
  208. function afterExecute(executionError, results) {
  209. try {
  210. complete();
  211. // `this` is passed from sqlite, we have no control over this.
  212. // eslint-disable-next-line no-invalid-this
  213. resolve(query._handleQueryResponse(this, columnTypes, executionError, results));
  214. return;
  215. } catch (error) {
  216. reject(error);
  217. }
  218. }
  219. if (!parameters) parameters = [];
  220. conn[method](sql, parameters, afterExecute);
  221. return null;
  222. };
  223. if (this.getDatabaseMethod() === 'all') {
  224. let tableNames = [];
  225. if (this.options && this.options.tableNames) {
  226. tableNames = this.options.tableNames;
  227. } else if (/FROM `(.*?)`/i.exec(this.sql)) {
  228. tableNames.push(/FROM `(.*?)`/i.exec(this.sql)[1]);
  229. }
  230. // If we already have the metadata for the table, there's no need to ask for it again
  231. tableNames = tableNames.filter(tableName => !(tableName in columnTypes) && tableName !== 'sqlite_master');
  232. if (!tableNames.length) {
  233. return executeSql();
  234. }
  235. await Promise.all(tableNames.map(tableName =>
  236. new Promise(resolve => {
  237. tableName = tableName.replace(/`/g, '');
  238. columnTypes[tableName] = {};
  239. conn.all(`PRAGMA table_info(\`${tableName}\`)`, (err, results) => {
  240. if (!err) {
  241. for (const result of results) {
  242. columnTypes[tableName][result.name] = result.type;
  243. }
  244. }
  245. resolve();
  246. });
  247. })));
  248. }
  249. return executeSql();
  250. }));
  251. }
  252. parseConstraintsFromSql(sql) {
  253. let constraints = sql.split('CONSTRAINT ');
  254. let referenceTableName, referenceTableKeys, updateAction, deleteAction;
  255. constraints.splice(0, 1);
  256. constraints = constraints.map(constraintSql => {
  257. //Parse foreign key snippets
  258. if (constraintSql.includes('REFERENCES')) {
  259. //Parse out the constraint condition form sql string
  260. updateAction = constraintSql.match(/ON UPDATE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/);
  261. deleteAction = constraintSql.match(/ON DELETE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/);
  262. if (updateAction) {
  263. updateAction = updateAction[1];
  264. }
  265. if (deleteAction) {
  266. deleteAction = deleteAction[1];
  267. }
  268. const referencesRegex = /REFERENCES.+\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/;
  269. const referenceConditions = constraintSql.match(referencesRegex)[0].split(' ');
  270. referenceTableName = Utils.removeTicks(referenceConditions[1]);
  271. let columnNames = referenceConditions[2];
  272. columnNames = columnNames.replace(/\(|\)/g, '').split(', ');
  273. referenceTableKeys = columnNames.map(column => Utils.removeTicks(column));
  274. }
  275. const constraintCondition = constraintSql.match(/\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/)[0];
  276. constraintSql = constraintSql.replace(/\(.+\)/, '');
  277. const constraint = constraintSql.split(' ');
  278. if (constraint[1] === 'PRIMARY' || constraint[1] === 'FOREIGN') {
  279. constraint[1] += ' KEY';
  280. }
  281. return {
  282. constraintName: Utils.removeTicks(constraint[0]),
  283. constraintType: constraint[1],
  284. updateAction,
  285. deleteAction,
  286. sql: sql.replace(/"/g, '`'), //Sqlite returns double quotes for table name
  287. constraintCondition,
  288. referenceTableName,
  289. referenceTableKeys
  290. };
  291. });
  292. return constraints;
  293. }
  294. applyParsers(type, value) {
  295. if (type.includes('(')) {
  296. // Remove the length part
  297. type = type.substr(0, type.indexOf('('));
  298. }
  299. type = type.replace('UNSIGNED', '').replace('ZEROFILL', '');
  300. type = type.trim().toUpperCase();
  301. const parse = parserStore.get(type);
  302. if (value !== null && parse) {
  303. return parse(value, { timezone: this.sequelize.options.timezone });
  304. }
  305. return value;
  306. }
  307. formatError(err) {
  308. switch (err.code) {
  309. case 'SQLITE_CONSTRAINT': {
  310. if (err.message.includes('FOREIGN KEY constraint failed')) {
  311. return new sequelizeErrors.ForeignKeyConstraintError({
  312. parent: err
  313. });
  314. }
  315. let fields = [];
  316. // Sqlite pre 2.2 behavior - Error: SQLITE_CONSTRAINT: columns x, y are not unique
  317. let match = err.message.match(/columns (.*?) are/);
  318. if (match !== null && match.length >= 2) {
  319. fields = match[1].split(', ');
  320. } else {
  321. // Sqlite post 2.2 behavior - Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: table.x, table.y
  322. match = err.message.match(/UNIQUE constraint failed: (.*)/);
  323. if (match !== null && match.length >= 2) {
  324. fields = match[1].split(', ').map(columnWithTable => columnWithTable.split('.')[1]);
  325. }
  326. }
  327. const errors = [];
  328. let message = 'Validation error';
  329. for (const field of fields) {
  330. errors.push(new sequelizeErrors.ValidationErrorItem(
  331. this.getUniqueConstraintErrorMessage(field),
  332. 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
  333. field,
  334. this.instance && this.instance[field],
  335. this.instance,
  336. 'not_unique'
  337. ));
  338. }
  339. if (this.model) {
  340. _.forOwn(this.model.uniqueKeys, constraint => {
  341. if (_.isEqual(constraint.fields, fields) && !!constraint.msg) {
  342. message = constraint.msg;
  343. return false;
  344. }
  345. });
  346. }
  347. return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields });
  348. }
  349. case 'SQLITE_BUSY':
  350. return new sequelizeErrors.TimeoutError(err);
  351. default:
  352. return new sequelizeErrors.DatabaseError(err);
  353. }
  354. }
  355. async handleShowIndexesQuery(data) {
  356. // Sqlite returns indexes so the one that was defined last is returned first. Lets reverse that!
  357. return Promise.all(data.reverse().map(async item => {
  358. item.fields = [];
  359. item.primary = false;
  360. item.unique = !!item.unique;
  361. item.constraintName = item.name;
  362. const columns = await this.run(`PRAGMA INDEX_INFO(\`${item.name}\`)`);
  363. for (const column of columns) {
  364. item.fields[column.seqno] = {
  365. attribute: column.name,
  366. length: undefined,
  367. order: undefined
  368. };
  369. }
  370. return item;
  371. }));
  372. }
  373. getDatabaseMethod() {
  374. if (this.isInsertQuery() || this.isUpdateQuery() || this.isUpsertQuery() || this.isBulkUpdateQuery() || this.sql.toLowerCase().includes('CREATE TEMPORARY TABLE'.toLowerCase()) || this.options.type === QueryTypes.BULKDELETE) {
  375. return 'run';
  376. }
  377. return 'all';
  378. }
  379. }
  380. module.exports = Query;
  381. module.exports.Query = Query;
  382. module.exports.default = Query;