mirror of
https://github.com/nocodb/nocodb.git
synced 2026-02-02 02:47:29 +00:00
1st work
This commit is contained in:
20
packages/nocodb/src/dbQueryClient/index.ts
Normal file
20
packages/nocodb/src/dbQueryClient/index.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
packages/nocodb/src/dbQueryClient/mysql.ts
Normal file
47
packages/nocodb/src/dbQueryClient/mysql.ts
Normal 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})`;
|
||||
}
|
||||
}
|
||||
47
packages/nocodb/src/dbQueryClient/pg.ts
Normal file
47
packages/nocodb/src/dbQueryClient/pg.ts
Normal 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}`;
|
||||
}
|
||||
}
|
||||
46
packages/nocodb/src/dbQueryClient/sqlite.ts
Normal file
46
packages/nocodb/src/dbQueryClient/sqlite.ts
Normal 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})`;
|
||||
}
|
||||
}
|
||||
14
packages/nocodb/src/dbQueryClient/types.ts
Normal file
14
packages/nocodb/src/dbQueryClient/types.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user