From 520143819d52a36a885a8172503a44d0afe3c9f0 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Thu, 2 Nov 2023 10:52:55 +0000 Subject: [PATCH 1/2] fix: Fixed issue with sql query spliting in SqliteClient when there is ';' in the query itself --- .../db/sql-client/lib/sqlite/SqliteClient.ts | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts b/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts index 8b81c1f6c9..cc529be0de 100644 --- a/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts +++ b/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts @@ -1630,8 +1630,36 @@ class SqliteClient extends KnexClient { const trx = await this.sqlClient.transaction(); + const splitQueries = (query) => { + const queries = []; + let quotationCount = 0; + let currentQuery = ''; + + for (let i = 0; i < query.length; i++) { + if (query[i] === '"' || query[i] === "'") { + // Ignore if quotation is escaped + if (i > 0 && query[i - 1] !== '\\') { + quotationCount++; + } + } + + if (query[i] === ';' && quotationCount % 2 === 0) { + queries.push(currentQuery); + currentQuery = ''; + } else { + currentQuery += query[i]; + } + } + + if (currentQuery.trim() !== '') { + queries.push(currentQuery); + } + + return queries; + }; + try { - const queries = upQuery.split(';'); + const queries = splitQueries(upQuery); for (let i = 0; i < queries.length; i++) { if (queries[i].trim() !== '') { await trx.raw(queries[i]); From 96a754ce1d1fb8c306d21278e4b56bc7cd718de8 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Thu, 2 Nov 2023 10:52:55 +0000 Subject: [PATCH 2/2] fix: Fixed split query logic --- .../src/db/sql-client/lib/sqlite/SqliteClient.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts b/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts index cc529be0de..240a7f5f10 100644 --- a/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts +++ b/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts @@ -1633,10 +1633,18 @@ class SqliteClient extends KnexClient { const splitQueries = (query) => { const queries = []; let quotationCount = 0; + let quotationMode: 'double' | 'single' | undefined = undefined; let currentQuery = ''; for (let i = 0; i < query.length; i++) { - if (query[i] === '"' || query[i] === "'") { + if (!quotationMode && (query[i] === '"' || query[i] === "'")) { + quotationMode = query[i] === '"' ? 'double' : 'single'; + } + + if ( + (quotationMode === 'double' && query[i] === '"') || + (quotationMode === 'single' && query[i] === "'") + ) { // Ignore if quotation is escaped if (i > 0 && query[i - 1] !== '\\') { quotationCount++; @@ -1646,6 +1654,7 @@ class SqliteClient extends KnexClient { if (query[i] === ';' && quotationCount % 2 === 0) { queries.push(currentQuery); currentQuery = ''; + quotationMode = undefined; } else { currentQuery += query[i]; }