query-interface.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __defProps = Object.defineProperties;
  4. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  5. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  8. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  9. var __spreadValues = (a, b) => {
  10. for (var prop in b || (b = {}))
  11. if (__hasOwnProp.call(b, prop))
  12. __defNormalProp(a, prop, b[prop]);
  13. if (__getOwnPropSymbols)
  14. for (var prop of __getOwnPropSymbols(b)) {
  15. if (__propIsEnum.call(b, prop))
  16. __defNormalProp(a, prop, b[prop]);
  17. }
  18. return a;
  19. };
  20. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  21. const sequelizeErrors = require("../../errors");
  22. const QueryTypes = require("../../query-types");
  23. const { QueryInterface } = require("../abstract/query-interface");
  24. const { cloneDeep } = require("../../utils");
  25. const _ = require("lodash");
  26. class SQLiteQueryInterface extends QueryInterface {
  27. async removeColumn(tableName, attributeName, options) {
  28. options = options || {};
  29. const fields = await this.describeTable(tableName, options);
  30. delete fields[attributeName];
  31. const sql = this.queryGenerator.removeColumnQuery(tableName, fields);
  32. const subQueries = sql.split(";").filter((q) => q !== "");
  33. for (const subQuery of subQueries)
  34. await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
  35. }
  36. async changeColumn(tableName, attributeName, dataTypeOrOptions, options) {
  37. options = options || {};
  38. const fields = await this.describeTable(tableName, options);
  39. Object.assign(fields[attributeName], this.normalizeAttribute(dataTypeOrOptions));
  40. const sql = this.queryGenerator.removeColumnQuery(tableName, fields);
  41. const subQueries = sql.split(";").filter((q) => q !== "");
  42. for (const subQuery of subQueries)
  43. await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
  44. }
  45. async renameColumn(tableName, attrNameBefore, attrNameAfter, options) {
  46. options = options || {};
  47. const fields = await this.assertTableHasColumn(tableName, attrNameBefore, options);
  48. fields[attrNameAfter] = __spreadValues({}, fields[attrNameBefore]);
  49. delete fields[attrNameBefore];
  50. const sql = this.queryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields);
  51. const subQueries = sql.split(";").filter((q) => q !== "");
  52. for (const subQuery of subQueries)
  53. await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
  54. }
  55. async removeConstraint(tableName, constraintName, options) {
  56. let createTableSql;
  57. const constraints = await this.showConstraint(tableName, constraintName);
  58. const constraint = constraints.find((constaint) => constaint.constraintName === constraintName);
  59. if (!constraint) {
  60. throw new sequelizeErrors.UnknownConstraintError({
  61. message: `Constraint ${constraintName} on table ${tableName} does not exist`,
  62. constraint: constraintName,
  63. table: tableName
  64. });
  65. }
  66. createTableSql = constraint.sql;
  67. constraint.constraintName = this.queryGenerator.quoteIdentifier(constraint.constraintName);
  68. let constraintSnippet = `, CONSTRAINT ${constraint.constraintName} ${constraint.constraintType} ${constraint.constraintCondition}`;
  69. if (constraint.constraintType === "FOREIGN KEY") {
  70. const referenceTableName = this.queryGenerator.quoteTable(constraint.referenceTableName);
  71. constraint.referenceTableKeys = constraint.referenceTableKeys.map((columnName) => this.queryGenerator.quoteIdentifier(columnName));
  72. const referenceTableKeys = constraint.referenceTableKeys.join(", ");
  73. constraintSnippet += ` REFERENCES ${referenceTableName} (${referenceTableKeys})`;
  74. constraintSnippet += ` ON UPDATE ${constraint.updateAction}`;
  75. constraintSnippet += ` ON DELETE ${constraint.deleteAction}`;
  76. }
  77. createTableSql = createTableSql.replace(constraintSnippet, "");
  78. createTableSql += ";";
  79. const fields = await this.describeTable(tableName, options);
  80. const sql = this.queryGenerator._alterConstraintQuery(tableName, fields, createTableSql);
  81. const subQueries = sql.split(";").filter((q) => q !== "");
  82. for (const subQuery of subQueries)
  83. await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
  84. }
  85. async addConstraint(tableName, options) {
  86. if (!options.fields) {
  87. throw new Error("Fields must be specified through options.fields");
  88. }
  89. if (!options.type) {
  90. throw new Error("Constraint type must be specified through options.type");
  91. }
  92. options = cloneDeep(options);
  93. const constraintSnippet = this.queryGenerator.getConstraintSnippet(tableName, options);
  94. const describeCreateTableSql = this.queryGenerator.describeCreateTableQuery(tableName);
  95. const constraints = await this.sequelize.query(describeCreateTableSql, __spreadProps(__spreadValues({}, options), { type: QueryTypes.SELECT, raw: true }));
  96. let sql = constraints[0].sql;
  97. const index = sql.length - 1;
  98. const createTableSql = `${sql.substr(0, index)}, ${constraintSnippet})${sql.substr(index + 1)};`;
  99. const fields = await this.describeTable(tableName, options);
  100. sql = this.queryGenerator._alterConstraintQuery(tableName, fields, createTableSql);
  101. const subQueries = sql.split(";").filter((q) => q !== "");
  102. for (const subQuery of subQueries)
  103. await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
  104. }
  105. async getForeignKeyReferencesForTable(tableName, options) {
  106. const database = this.sequelize.config.database;
  107. const query = this.queryGenerator.getForeignKeysQuery(tableName, database);
  108. const result = await this.sequelize.query(query, options);
  109. return result.map((row) => ({
  110. tableName,
  111. columnName: row.from,
  112. referencedTableName: row.table,
  113. referencedColumnName: row.to,
  114. tableCatalog: database,
  115. referencedTableCatalog: database
  116. }));
  117. }
  118. async dropAllTables(options) {
  119. options = options || {};
  120. const skip = options.skip || [];
  121. const tableNames = await this.showAllTables(options);
  122. await this.sequelize.query("PRAGMA foreign_keys = OFF", options);
  123. await this._dropAllTables(tableNames, skip, options);
  124. await this.sequelize.query("PRAGMA foreign_keys = ON", options);
  125. }
  126. async describeTable(tableName, options) {
  127. let schema = null;
  128. let schemaDelimiter = null;
  129. if (typeof options === "string") {
  130. schema = options;
  131. } else if (typeof options === "object" && options !== null) {
  132. schema = options.schema || null;
  133. schemaDelimiter = options.schemaDelimiter || null;
  134. }
  135. if (typeof tableName === "object" && tableName !== null) {
  136. schema = tableName.schema;
  137. tableName = tableName.tableName;
  138. }
  139. const sql = this.queryGenerator.describeTableQuery(tableName, schema, schemaDelimiter);
  140. options = __spreadProps(__spreadValues({}, options), { type: QueryTypes.DESCRIBE });
  141. const sqlIndexes = this.queryGenerator.showIndexesQuery(tableName);
  142. try {
  143. const data = await this.sequelize.query(sql, options);
  144. if (_.isEmpty(data)) {
  145. throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`);
  146. }
  147. const indexes = await this.sequelize.query(sqlIndexes, options);
  148. for (const prop in data) {
  149. data[prop].unique = false;
  150. }
  151. for (const index of indexes) {
  152. for (const field of index.fields) {
  153. if (index.unique !== void 0) {
  154. data[field.attribute].unique = index.unique;
  155. }
  156. }
  157. }
  158. const foreignKeys = await this.getForeignKeyReferencesForTable(tableName, options);
  159. for (const foreignKey of foreignKeys) {
  160. data[foreignKey.columnName].references = {
  161. model: foreignKey.referencedTableName,
  162. key: foreignKey.referencedColumnName
  163. };
  164. }
  165. return data;
  166. } catch (e) {
  167. if (e.original && e.original.code === "ER_NO_SUCH_TABLE") {
  168. throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`);
  169. }
  170. throw e;
  171. }
  172. }
  173. }
  174. exports.SQLiteQueryInterface = SQLiteQueryInterface;
  175. //# sourceMappingURL=query-interface.js.map