Skip to content

Commit

Permalink
fix(ibmi): update query results handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lohart13 committed Mar 27, 2024
1 parent e144dfe commit 6088347
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions packages/ibmi/src/query.js
Expand Up @@ -4,12 +4,12 @@ import {
AbstractQuery,
ConnectionRefusedError,
DatabaseError,
EmptyResultError,
ForeignKeyConstraintError,
UniqueConstraintError,
UnknownConstraintError,
} from '@sequelize/core';
import { logger } from '@sequelize/core/_non-semver-use-at-your-own-risk_/utils/logger.js';
import { find } from '@sequelize/utils';

const debug = logger.debugContext('sql:ibmi');

Expand Down Expand Up @@ -68,21 +68,25 @@ export class IBMiQuery extends AbstractQuery {
* @private
*/
formatResults(data) {
let result = this.instance;

if (this.isInsertQuery() || this.isUpdateQuery() || this.isUpsertQuery()) {
if (this.instance && this.instance.dataValues) {
for (const key in data[0]) {
if (Object.hasOwn(data[0], key)) {
const record = data[0][key];

const attributes = this.model.modelDefinition.attributes;
const attr = find(
attributes.values(),
attribute => attribute.attributeName === key || attribute.columnName === key,
if (this.isInsertQuery() && !this.isUpsertQuery() && data.length === 0) {
throw new EmptyResultError();
}

if (this.options.returning && Array.isArray(data) && data[0]) {
for (const attributeOrColumnName of Object.keys(data[0])) {
const modelDefinition = this.model.modelDefinition;
const attribute = modelDefinition.columns.get(attributeOrColumnName);
const updatedValue = this._parseDatabaseValue(
data[0][attributeOrColumnName],
attribute?.type,
);

this.instance.dataValues[attr?.attributeName || key] = record;
this.instance.set(attribute?.attributeName ?? attributeOrColumnName, updatedValue, {
raw: true,
comesFromDatabase: true,
});
}
}
}
Expand All @@ -106,7 +110,7 @@ export class IBMiQuery extends AbstractQuery {
}

if (this.isDescribeQuery()) {
result = {};
const result = {};

for (const _result of data) {
const enumRegex = /^enum/i;
Expand All @@ -128,17 +132,12 @@ export class IBMiQuery extends AbstractQuery {
return data[0];
}

if (this.isBulkUpdateQuery() || this.isDeleteQuery() || this.isUpsertQuery()) {
if (this.isDeleteQuery()) {
return data.count;
}

if (this.isInsertQuery(data)) {
// insert queries can't call count, because they are actually select queries wrapped around insert queries to get the inserted id. Need to count the number of results instead.
return [result, data.length];
}

if (this.isUpdateQuery()) {
return [result, data.count];
if (this.isBulkUpdateQuery()) {
return this.options.returning ? this.handleSelectQuery(data) : data.count;
}

if (this.isShowConstraintsQuery()) {
Expand All @@ -150,11 +149,7 @@ export class IBMiQuery extends AbstractQuery {
return [data, data];
}

if (this.isShowIndexesQuery()) {
return data;
}

return result;
return this.instance;
}

handleInsertQuery(results, metaData) {
Expand Down

0 comments on commit 6088347

Please sign in to comment.