Skip to content

Commit

Permalink
chore: tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxing9 committed Jan 11, 2025
1 parent ba36769 commit 2edbf9f
Show file tree
Hide file tree
Showing 25 changed files with 1,186 additions and 603 deletions.
22 changes: 19 additions & 3 deletions apps/nestjs-backend/src/db-provider/db.provider.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export interface IDbProvider {

searchQuery(
originQueryBuilder: Knex.QueryBuilder,
fieldMap?: { [fieldId: string]: IFieldInstance },
searchFields: IFieldInstance[],
search?: [string, string?, boolean?]
): Knex.QueryBuilder;

Expand All @@ -143,15 +143,31 @@ export interface IDbProvider {
searchIndexRo: Partial<ISearchIndexByQueryRo>,
baseSortIndex?: string,
setFilterQuery?: (qb: Knex.QueryBuilder) => void,
setSortQuery?: (qb: Knex.QueryBuilder) => void
setSortQuery?: (qb: Knex.QueryBuilder) => void,
withFullTextIndex?: boolean
): Knex.QueryBuilder;

searchCountQuery(
originQueryBuilder: Knex.QueryBuilder,
searchField: IFieldInstance[],
searchValue: string
searchValue: string,
withFullTextIndex?: boolean
): Knex.QueryBuilder;

getSearchTsIndexSql(
originQueryBuilder: Knex.QueryBuilder,
dbTableName: string,
searchField: IFieldInstance[]
): string[];

getClearSearchTsIndexSql(
originQueryBuilder: Knex.QueryBuilder,
dbTableName: string,
searchField: IFieldInstance[]
): string[];

getExistFtsIndexSql(originQueryBuilder: Knex.QueryBuilder, dbTableName: string): string | null;

shareFilterCollaboratorsQuery(
originQueryBuilder: Knex.QueryBuilder,
dbFieldName: string,
Expand Down
57 changes: 49 additions & 8 deletions apps/nestjs-backend/src/db-provider/postgres.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import { FilterQueryPostgres } from './filter-query/postgres/filter-query.postgr
import type { IGroupQueryExtra, IGroupQueryInterface } from './group-query/group-query.interface';
import { GroupQueryPostgres } from './group-query/group-query.postgres';
import { SearchQueryAbstract } from './search-query/abstract';
import { SearchQueryBuilder, SearchQueryPostgres } from './search-query/search-query.postgres';
import { FullTextSearchQueryPostgresBuilder } from './search-query/search-fts-query.postgres';
import {
SearchQueryPostgresBuilder,
SearchQueryPostgres,
} from './search-query/search-query.postgres';
import { SortQueryPostgres } from './sort-query/postgres/sort-query.postgres';
import type { ISortQueryInterface } from './sort-query/sort-query.interface';

Expand Down Expand Up @@ -320,22 +324,29 @@ export class PostgresProvider implements IDbProvider {

searchQuery(
originQueryBuilder: Knex.QueryBuilder,
fieldMap?: { [fieldId: string]: IFieldInstance },
searchFields: IFieldInstance[],
search?: [string, string?, boolean?]
) {
return SearchQueryAbstract.factory(SearchQueryPostgres, originQueryBuilder, fieldMap, search);
return SearchQueryAbstract.appendQueryBuilder(
SearchQueryPostgres,
originQueryBuilder,
searchFields,
search
);
}

searchCountQuery(
originQueryBuilder: Knex.QueryBuilder,
searchField: IFieldInstance[],
searchValue: string
searchValue: string,
withFullTextIndex?: boolean
) {
return SearchQueryAbstract.buildSearchCountQuery(
SearchQueryPostgres,
originQueryBuilder,
searchField,
searchValue
searchValue,
withFullTextIndex
);
}

Expand All @@ -346,19 +357,49 @@ export class PostgresProvider implements IDbProvider {
searchIndexRo: ISearchIndexByQueryRo,
baseSortIndex?: string,
setFilterQuery?: (qb: Knex.QueryBuilder) => void,
setSortQuery?: (qb: Knex.QueryBuilder) => void
setSortQuery?: (qb: Knex.QueryBuilder) => void,
withFullTextIndex?: boolean
) {
return new SearchQueryBuilder(
return new SearchQueryPostgresBuilder(
originQueryBuilder,
dbTableName,
searchField,
searchIndexRo,
baseSortIndex,
setFilterQuery,
setSortQuery
setSortQuery,
withFullTextIndex
).getSearchIndexQuery();
}

getSearchTsIndexSql(
originQueryBuilder: Knex.QueryBuilder,
dbTableName: string,
searchField: IFieldInstance[]
) {
return new FullTextSearchQueryPostgresBuilder(
originQueryBuilder,
dbTableName,
searchField
).getSearchFieldIndexSql();
}

getClearSearchTsIndexSql(
originQueryBuilder: Knex.QueryBuilder,
dbTableName: string,
searchField: IFieldInstance[]
) {
return new FullTextSearchQueryPostgresBuilder(
originQueryBuilder,
dbTableName,
searchField
).getClearSearchTsIndexSql();
}

getExistFtsIndexSql(originQueryBuilder: Knex.QueryBuilder, dbTableName: string) {
return FullTextSearchQueryPostgresBuilder.getExistFtsIndexSql(originQueryBuilder, dbTableName);
}

shareFilterCollaboratorsQuery(
originQueryBuilder: Knex.QueryBuilder,
dbFieldName: string,
Expand Down
199 changes: 43 additions & 156 deletions apps/nestjs-backend/src/db-provider/search-query/abstract.ts
Original file line number Diff line number Diff line change
@@ -1,127 +1,48 @@
import { CellValueType } from '@teable/core';
import type { Knex } from 'knex';
import type { IFieldInstance } from '../../features/field/model/factory';
import type { ISearchQueryConstructor } from './types';

export abstract class SearchQueryAbstract {
static factory(
static appendQueryBuilder(
// eslint-disable-next-line @typescript-eslint/naming-convention
SearchQuery: new (
originQueryBuilder: Knex.QueryBuilder,
field: IFieldInstance,
searchValue: string
) => SearchQueryAbstract,
SearchQuery: ISearchQueryConstructor,
originQueryBuilder: Knex.QueryBuilder,
fieldMap?: { [fieldId: string]: IFieldInstance },
search?: [string, string?, boolean?]
searchFields: IFieldInstance[],
search?: [string, string?, boolean?],
withFullTextIndex?: boolean
) {
if (!search || !fieldMap) {
if (!search || !searchFields?.length) {
return originQueryBuilder;
}

let searchArr = [];

if (!search?.[1]) {
searchArr = Object.values(fieldMap).map((f) => f.id);
} else {
searchArr = search[1]?.split(',');
}

const searchValue = search[0];

searchArr.forEach((item) => {
const field = fieldMap?.[item];

if (!field) {
return;
}

if (field.cellValueType === CellValueType.Boolean) {
return;
}

const searchQueryBuilder = new SearchQuery(originQueryBuilder, field, searchValue);

if (field.isMultipleCellValue) {
switch (field.cellValueType) {
case CellValueType.DateTime:
searchQueryBuilder.multipleDate();
break;
case CellValueType.Number:
searchQueryBuilder.multipleNumber();
break;
case CellValueType.String:
if (field.isStructuredCellValue) {
searchQueryBuilder.multipleJson();
} else {
searchQueryBuilder.multipleText();
}
break;
}
return;
}

switch (field.cellValueType) {
case CellValueType.DateTime:
searchQueryBuilder.date();
break;
case CellValueType.Number:
searchQueryBuilder.number();
break;
case CellValueType.String:
if (field.isStructuredCellValue) {
searchQueryBuilder.json();
} else {
searchQueryBuilder.text();
}
break;
}
searchFields.forEach((fIns) => {
const builder = new SearchQuery(originQueryBuilder, fIns, searchValue, withFullTextIndex);
builder.appendBuilder();
});

return originQueryBuilder;
}

static buildSearchIndexQuery(
// eslint-disable-next-line @typescript-eslint/naming-convention
SearchQuery: new (
originQueryBuilder: Knex.QueryBuilder,
field: IFieldInstance,
searchValue: string
) => SearchQueryAbstract,
SearchQuery: ISearchQueryConstructor,
queryBuilder: Knex.QueryBuilder,
searchField: IFieldInstance[],
searchValue: string,
dbTableName: string
dbTableName: string,
withFullTextIndex?: boolean
) {
const knexInstance = queryBuilder.client;
const searchQuery = searchField.map((field) => {
const searchQueryBuilder = new SearchQuery(queryBuilder, field, searchValue);
if (field.isMultipleCellValue) {
switch (field.cellValueType) {
case CellValueType.DateTime:
return searchQueryBuilder.getMultipleDateSqlQuery();
case CellValueType.Number:
return searchQueryBuilder.getMultipleNumberSqlQuery();
case CellValueType.String:
if (field.isStructuredCellValue) {
return searchQueryBuilder.getMultipleJsonSqlQuery();
} else {
return searchQueryBuilder.getMultipleTextSqlQuery();
}
}
}

switch (field.cellValueType) {
case CellValueType.DateTime:
return searchQueryBuilder.getDateSqlQuery();
case CellValueType.Number:
return searchQueryBuilder.getNumberSqlQuery();
case CellValueType.String:
if (field.isStructuredCellValue) {
return searchQueryBuilder.getJsonSqlQuery();
} else {
return searchQueryBuilder.getTextSqlQuery();
}
}
const searchQueryBuilder = new SearchQuery(
queryBuilder,
field,
searchValue,
withFullTextIndex
);
return searchQueryBuilder.getSql();
});

queryBuilder.with('search_field_union_table', (qb) => {
Expand Down Expand Up @@ -163,45 +84,20 @@ export abstract class SearchQueryAbstract {

static buildSearchCountQuery(
// eslint-disable-next-line @typescript-eslint/naming-convention
SearchQuery: new (
originQueryBuilder: Knex.QueryBuilder,
field: IFieldInstance,
searchValue: string
) => SearchQueryAbstract,
SearchQuery: ISearchQueryConstructor,
queryBuilder: Knex.QueryBuilder,
searchField: IFieldInstance[],
searchValue: string
searchValue: string,
withFullTextIndex?: boolean
) {
const searchQuery = searchField.map((field) => {
const searchQueryBuilder = new SearchQuery(queryBuilder, field, searchValue);

if (field.isMultipleCellValue) {
switch (field.cellValueType) {
case CellValueType.DateTime:
return searchQueryBuilder.getMultipleDateSqlQuery();
case CellValueType.Number:
return searchQueryBuilder.getMultipleNumberSqlQuery();
case CellValueType.String:
if (field.isStructuredCellValue) {
return searchQueryBuilder.getMultipleJsonSqlQuery();
} else {
return searchQueryBuilder.getMultipleTextSqlQuery();
}
}
}

switch (field.cellValueType) {
case CellValueType.DateTime:
return searchQueryBuilder.getDateSqlQuery();
case CellValueType.Number:
return searchQueryBuilder.getNumberSqlQuery();
case CellValueType.String:
if (field.isStructuredCellValue) {
return searchQueryBuilder.getJsonSqlQuery();
} else {
return searchQueryBuilder.getTextSqlQuery();
}
}
const searchQueryBuilder = new SearchQuery(
queryBuilder,
field,
searchValue,
withFullTextIndex
);
return searchQueryBuilder.getSql();
});

const knexInstance = queryBuilder.client;
Expand All @@ -220,38 +116,29 @@ export abstract class SearchQueryAbstract {
constructor(
protected readonly originQueryBuilder: Knex.QueryBuilder,
protected readonly field: IFieldInstance,
protected readonly searchValue: string
protected readonly searchValue: string,
protected readonly withFullTextIndex?: boolean
) {}

abstract multipleNumber(): Knex.QueryBuilder;

abstract multipleDate(): Knex.QueryBuilder;

abstract multipleText(): Knex.QueryBuilder;

abstract multipleJson(): Knex.QueryBuilder;

abstract json(): Knex.QueryBuilder;

abstract text(): Knex.QueryBuilder;
protected abstract json(): Knex.QueryBuilder;

abstract date(): Knex.QueryBuilder;
protected abstract text(): Knex.QueryBuilder;

abstract number(): Knex.QueryBuilder;
protected abstract date(): Knex.QueryBuilder;

abstract getNumberSqlQuery(): string;
protected abstract number(): Knex.QueryBuilder;

abstract getDateSqlQuery(): string;
protected abstract multipleNumber(): Knex.QueryBuilder;

abstract getTextSqlQuery(): string;
protected abstract multipleDate(): Knex.QueryBuilder;

abstract getJsonSqlQuery(): string;
protected abstract multipleText(): Knex.QueryBuilder;

abstract getMultipleNumberSqlQuery(): string;
protected abstract multipleJson(): Knex.QueryBuilder;

abstract getMultipleDateSqlQuery(): string;
abstract getSql(): string;

abstract getMultipleTextSqlQuery(): string;
abstract getQuery(): Knex.QueryBuilder;

abstract getMultipleJsonSqlQuery(): string;
abstract appendBuilder(): Knex.QueryBuilder;
}
Loading

0 comments on commit 2edbf9f

Please sign in to comment.