This commit is contained in:
Fendy Heryanto
2026-01-08 13:46:28 +00:00
parent cdcff441b2
commit 06039686cf
5 changed files with 174 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
import { ClientType } from 'nocodb-sdk';
import { PGDBQueryClient } from '~/dbQueryClient/pg';
import { MySqlDBQueryClient } from '~/dbQueryClient/mysql';
import { SqliteDBQueryClient } from '~/dbQueryClient/sqlite';
export class DBQueryClient {
static get(clientType: ClientType) {
switch (clientType) {
case ClientType.PG: {
return new PGDBQueryClient();
}
case ClientType.MYSQL: {
return new MySqlDBQueryClient();
}
case ClientType.SQLITE: {
return new SqliteDBQueryClient();
}
}
}
}

View File

@@ -0,0 +1,47 @@
import { arrFlatMap } from 'nocodb-sdk';
import type { DBQueryClient } from '~/dbQueryClient/types';
import type { XKnex } from '~/db/CustomKnex';
export class MySqlDBQueryClient implements DBQueryClient {
temporaryTable({
knex,
data,
fields,
alias,
asKnexFrom = true,
}: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
}) {
const fieldsValuePlaceholder = `(${fields.map(() => '?').join(',')})`;
const valuesPlaceholder = data.map(() => fieldsValuePlaceholder).join(', ');
const fieldsPlaceholder = fields.map(() => '??').join(',');
const query = knex.raw(
`(VALUES ${valuesPlaceholder}) AS ?? (${fieldsPlaceholder})`,
[
...arrFlatMap(
data.map((row) =>
fields.reduce((acc, field) => {
acc.push(row[field]);
return acc;
}, []),
),
),
alias,
...fields,
],
);
return asKnexFrom ? knex.from(query) : query;
}
concat(fields: string[]) {
return `CONCAT(${fields.join(', ')})`;
}
simpleCast(field: string, asType: string) {
const useAsType = asType.toUpperCase() === 'TEXT' ? 'CHAR' : asType;
return `CAST(${field} as ${useAsType})`;
}
}

View File

@@ -0,0 +1,47 @@
import { arrFlatMap } from 'nocodb-sdk';
import type { DBQueryClient } from '~/dbQueryClient/types';
import type { XKnex } from '~/db/CustomKnex';
export class PGDBQueryClient implements DBQueryClient {
temporaryTable({
knex,
data,
fields,
alias,
asKnexFrom = true,
}: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
}) {
const fieldsValuePlaceholder = `(${fields.map(() => '?').join(',')})`;
const valuesPlaceholder = data.map(() => fieldsValuePlaceholder).join(', ');
const fieldsPlaceholder = fields.map(() => '??').join(',');
const query = knex.raw(
`(VALUES ${valuesPlaceholder}) AS ?? (${fieldsPlaceholder})`,
[
...arrFlatMap(
data.map((row) =>
fields.reduce((acc, field) => {
acc.push(row[field]);
return acc;
}, []),
),
),
alias,
...fields,
],
);
return asKnexFrom ? knex.from(query) : query;
}
concat(fields: string[]) {
return `CONCAT(${fields.join(', ')})`;
}
simpleCast(field: string, asType: string) {
return `${field}::${asType}`;
}
}

View File

@@ -0,0 +1,46 @@
import { arrFlatMap } from 'nocodb-sdk';
import type { DBQueryClient } from '~/dbQueryClient/types';
import type { XKnex } from '~/db/CustomKnex';
export class SqliteDBQueryClient implements DBQueryClient {
temporaryTable({
knex,
data,
fields,
alias,
asKnexFrom = true,
}: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
}) {
const fieldsValuePlaceholder = `(${fields.map(() => '?').join(',')})`;
const valuesPlaceholder = data.map(() => fieldsValuePlaceholder).join(', ');
const fieldsPlaceholder = fields.map(() => '??').join(',');
const query = knex.raw(
`(VALUES ${valuesPlaceholder}) AS ?? (${fieldsPlaceholder})`,
[
...arrFlatMap(
data.map((row) =>
fields.reduce((acc, field) => {
acc.push(row[field]);
return acc;
}, []),
),
),
alias,
...fields,
],
);
return asKnexFrom ? knex.from(query) : query;
}
concat(fields: string[]) {
return `${fields.join(' || ')}`;
}
simpleCast(field: string, asType: string) {
return `CAST(${field} as ${asType})`;
}
}

View File

@@ -0,0 +1,14 @@
import type { XKnex } from '~/db/CustomKnex';
export interface DBQueryClient {
temporaryTable(payload: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
});
concat(fields: string[]);
simpleCast(field: string, asType: string);
}