diff --git a/src/common/libs/sqlUtils.ts b/src/common/libs/sqlUtils.ts index 9e8e2b10..ff953b76 100644 --- a/src/common/libs/sqlUtils.ts +++ b/src/common/libs/sqlUtils.ts @@ -8,6 +8,7 @@ import customizations from '../customizations'; import { ClientCode } from '../interfaces/antares'; import { getArrayDepth } from './getArrayDepth'; import hexToBinary, { HexChar } from './hexToBinary'; +import * as antares from 'common/interfaces/antares'; /** * Escapes a string fo SQL use @@ -209,3 +210,20 @@ export const jsonToSqlInsert = (args: { return insertsString; }; + +export const formatJsonForSqlWhere = (jsonValue: object, clientType: antares.ClientCode) => { + const formattedValue = JSON.stringify(jsonValue); + + switch (clientType) { + case 'mysql': + return ` = CAST('${formattedValue}' AS JSON)`; + case 'maria': + return ` = '${formattedValue}'`; + case 'pg': + return `::text = '${formattedValue}'`; + case 'firebird': + case 'sqlite': + default: + return ` = '${formattedValue}'`; + } +}; diff --git a/src/main/ipc-handlers/tables.ts b/src/main/ipc-handlers/tables.ts index 8d7bbea7..568d90c3 100644 --- a/src/main/ipc-handlers/tables.ts +++ b/src/main/ipc-handlers/tables.ts @@ -3,7 +3,7 @@ import { ARRAY, BIT, BLOB, BOOLEAN, DATE, DATETIME, FLOAT, LONG_TEXT, NUMBER, TE import * as antares from 'common/interfaces/antares'; import { InsertRowsParams } from 'common/interfaces/tableApis'; import { fakerCustom } from 'common/libs/fakerCustom'; -import { sqlEscaper } from 'common/libs/sqlUtils'; +import { formatJsonForSqlWhere, sqlEscaper } from 'common/libs/sqlUtils'; import { ipcMain } from 'electron'; import * as fs from 'fs'; import * as moment from 'moment'; @@ -233,26 +233,12 @@ export default (connections: Record) => { for (const key in orgRow) { if (typeof orgRow[key] === 'string') - orgRow[key] = `'${orgRow[key]}'`; - else if (typeof orgRow[key] === 'object') { - switch (connections[params.uid]._client) { - case 'mysql': - orgRow[key] = `CAST('${JSON.stringify(orgRow[key])}' AS JSON)`; - break; - case 'maria': - orgRow[key] = `'${JSON.stringify(orgRow[key])}'`; - break; - case 'pg': - orgRow[key] = `::text='${JSON.stringify(orgRow[key])}'`; - break; - case 'firebird': - case 'sqlite': - } - } - - if (orgRow[key] === null) + orgRow[key] = ` = '${orgRow[key]}'`; + else if (typeof orgRow[key] === 'object' && orgRow[key] !== null) + orgRow[key] = formatJsonForSqlWhere(orgRow[key], connections[params.uid]._client); + else if (orgRow[key] === null) orgRow[key] = `IS ${orgRow[key]}`; - else if (!String(orgRow[key]).includes('::text=')) + else orgRow[key] = `= ${orgRow[key]}`; }