API Reference Source

lib/dialects/db2/query-interface.js

  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 _ = require("lodash");
  22. const Utils = require("../../utils");
  23. const Op = require("../../operators");
  24. const { QueryInterface } = require("../abstract/query-interface");
  25. const QueryTypes = require("../../query-types");
  26. class Db2QueryInterface extends QueryInterface {
  27. async getForeignKeyReferencesForTable(tableName, options) {
  28. const queryOptions = __spreadProps(__spreadValues({}, options), {
  29. type: QueryTypes.FOREIGNKEYS
  30. });
  31. const query = this.queryGenerator.getForeignKeysQuery(tableName, this.sequelize.config.username.toUpperCase());
  32. return this.sequelize.query(query, queryOptions);
  33. }
  34. async upsert(tableName, insertValues, updateValues, where, options) {
  35. options = __spreadValues({}, options);
  36. const model = options.model;
  37. const wheres = [];
  38. const attributes = Object.keys(insertValues);
  39. let indexes = [];
  40. let indexFields;
  41. options = _.clone(options);
  42. if (!Utils.isWhereEmpty(where)) {
  43. wheres.push(where);
  44. }
  45. indexes = _.map(model.uniqueKeys, (value) => {
  46. return value.fields;
  47. });
  48. model._indexes.forEach((value) => {
  49. if (value.unique) {
  50. indexFields = value.fields.map((field) => {
  51. if (_.isPlainObject(field)) {
  52. return field.attribute;
  53. }
  54. return field;
  55. });
  56. indexes.push(indexFields);
  57. }
  58. });
  59. for (const index of indexes) {
  60. if (_.intersection(attributes, index).length === index.length) {
  61. where = {};
  62. for (const field of index) {
  63. where[field] = insertValues[field];
  64. }
  65. wheres.push(where);
  66. }
  67. }
  68. where = { [Op.or]: wheres };
  69. options.type = QueryTypes.UPSERT;
  70. options.raw = true;
  71. const sql = this.queryGenerator.upsertQuery(tableName, insertValues, updateValues, where, model, options);
  72. const result = await this.sequelize.query(sql, options);
  73. return [result, void 0];
  74. }
  75. async createTable(tableName, attributes, options, model) {
  76. let sql = "";
  77. options = __spreadValues({}, options);
  78. if (options && options.uniqueKeys) {
  79. _.forOwn(options.uniqueKeys, (uniqueKey) => {
  80. if (uniqueKey.customIndex === void 0) {
  81. uniqueKey.customIndex = true;
  82. }
  83. });
  84. }
  85. if (model) {
  86. options.uniqueKeys = options.uniqueKeys || model.uniqueKeys;
  87. }
  88. attributes = _.mapValues(attributes, (attribute) => this.sequelize.normalizeAttribute(attribute));
  89. if (options.indexes) {
  90. options.indexes.forEach((fields) => {
  91. const fieldArr = fields.fields;
  92. if (fieldArr.length === 1) {
  93. fieldArr.forEach((field) => {
  94. for (const property in attributes) {
  95. if (field === attributes[property].field) {
  96. attributes[property].unique = true;
  97. }
  98. }
  99. });
  100. }
  101. });
  102. }
  103. if (options.alter) {
  104. if (options.indexes) {
  105. options.indexes.forEach((fields) => {
  106. const fieldArr = fields.fields;
  107. if (fieldArr.length === 1) {
  108. fieldArr.forEach((field) => {
  109. for (const property in attributes) {
  110. if (field === attributes[property].field && attributes[property].unique) {
  111. attributes[property].unique = false;
  112. }
  113. }
  114. });
  115. }
  116. });
  117. }
  118. }
  119. if (!tableName.schema && (options.schema || !!model && model._schema)) {
  120. tableName = this.queryGenerator.addSchema({
  121. tableName,
  122. _schema: !!model && model._schema || options.schema
  123. });
  124. }
  125. attributes = this.queryGenerator.attributesToSQL(attributes, { table: tableName, context: "createTable" });
  126. sql = this.queryGenerator.createTableQuery(tableName, attributes, options);
  127. return await this.sequelize.query(sql, options);
  128. }
  129. }
  130. exports.Db2QueryInterface = Db2QueryInterface;
  131. //# sourceMappingURL=query-interface.js.map