diff --git a/dist/lib/sqlite/db-schema-manager.js b/dist/lib/sqlite/db-schema-manager.js index 9107040..65941cc 100644 --- a/dist/lib/sqlite/db-schema-manager.js +++ b/dist/lib/sqlite/db-schema-manager.js @@ -406,10 +406,9 @@ class SQLiteSchemaManager { if (!existingIndexes.includes(index.indexName)) { console.log(`Creating index: ${index.indexName}`); const fields = index.indexTableFields - .map((f) => `"${f.value}"`) + .map((f) => `"${f}"`) .join(", "); - const unique = index.indexType === "regular" ? "" : ""; // SQLite doesn't have FULLTEXT in CREATE INDEX - this.db.run(`CREATE ${unique}INDEX "${index.indexName}" ON "${table.tableName}" (${fields})`); + this.db.run(`CREATE INDEX "${index.indexName}" ON "${table.tableName}" (${fields})`); } } } diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index 08285be..792a1b8 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -228,12 +228,12 @@ export interface BUN_SQLITE_ForeignKeyType { * Describes a table index and the fields it covers. */ export interface BUN_SQLITE_IndexSchemaType { - id?: string | number; + /** + * Name of the index as it would appear on schema. Eg. + * `idx_user_id_index` + */ indexName?: string; - indexType?: (typeof IndexTypes)[number]; - indexTableFields?: BUN_SQLITE_IndexTableFieldType[]; - alias?: string; - newTempIndex?: boolean; + indexTableFields?: string[]; } /** * Describes a multi-field uniqueness rule for a table. @@ -714,11 +714,12 @@ export type TableSelectFieldsObject; + distinct?: boolean; }; export type TableSelectFieldsBasicDirective = { alias: string; @@ -746,7 +747,7 @@ export type ServerQueryObjectValue = string | number | ServerQueryValuesObject | */ export type ServerQueryObject = { +}, K extends string = string> = SQLComparisonsParams & { value?: ServerQueryObjectValue; nullValue?: boolean; notNullValue?: boolean; @@ -826,6 +827,7 @@ export type GroupConcatObject = { * Separator. Default `,` */ separator?: string; + distinct?: boolean; }; export type SelectFieldObject; + distinct?: boolean; +}; +export declare const SQlComparisons: readonly [">", "<>", "<", "=", ">=", "<=", "!=", "IS NOT", "IS", "IS NULL", "IS NOT NULL", "IN", "NOT IN", "LIKE", "NOT LIKE", "GLOB", "NOT GLOB"]; +export type SQLBetween = { + min: SQLInsertGenValueType; + max: SQLInsertGenValueType; +}; +export type SQLComparisonsParams = { + raw_equality?: (typeof SQlComparisons)[number]; + between?: SQLBetween; + not_between?: SQLBetween; }; /** * Defines how a root-table field maps to a join-table field in an `ON` clause. */ export type ServerQueryParamsJoinMatchObject = { +}> = SQLComparisonsParams & { /** Field name from the **Root Table** */ source?: string | ServerQueryParamsJoinMatchSourceTargetObject; /** Field name from the **Join Table** */ diff --git a/dist/types/index.js b/dist/types/index.js index 60ff51d..2c05bbd 100644 --- a/dist/types/index.js +++ b/dist/types/index.js @@ -81,6 +81,25 @@ export const ServerQueryEqualities = [ "MATCH", "MATCH_BOOLEAN", ]; +export const SQlComparisons = [ + ">", + "<>", + "<", + "=", + ">=", + "<=", + "!=", + "IS NOT", + "IS", + "IS NULL", + "IS NOT NULL", + "IN", + "NOT IN", + "LIKE", + "NOT LIKE", + "GLOB", + "NOT GLOB", +]; /** * Uppercase HTTP methods supported by the CRUD helpers. */ diff --git a/dist/utils/sql-generator-gen-join-str.d.ts b/dist/utils/sql-generator-gen-join-str.d.ts index 869b9f6..5dbf8cb 100644 --- a/dist/utils/sql-generator-gen-join-str.d.ts +++ b/dist/utils/sql-generator-gen-join-str.d.ts @@ -1,8 +1,11 @@ -import type { ServerQueryParamsJoin, ServerQueryParamsJoinMatchObject } from "../types"; +import type { ServerQueryParamsJoin, ServerQueryParamsJoinMatchObject, SQLInsertGenValueType } from "../types"; type Param = { mtch: ServerQueryParamsJoinMatchObject; join: ServerQueryParamsJoin; table_name: string; }; -export default function sqlGenGenJoinStr({ join, mtch, table_name }: Param): string; +export default function sqlGenGenJoinStr({ join, mtch, table_name }: Param): { + str: string; + values: SQLInsertGenValueType[]; +}; export {}; diff --git a/dist/utils/sql-generator-gen-join-str.js b/dist/utils/sql-generator-gen-join-str.js index 8b754e4..0120afa 100644 --- a/dist/utils/sql-generator-gen-join-str.js +++ b/dist/utils/sql-generator-gen-join-str.js @@ -1,23 +1,37 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }) { + let values = []; if (mtch.__batch) { let btch_mtch = ``; btch_mtch += `(`; for (let i = 0; i < mtch.__batch.matches.length; i++) { const __mtch = mtch.__batch.matches[i]; - btch_mtch += `${sqlGenGenJoinStr({ join, mtch: __mtch, table_name })}`; + const { str, values: batch_values } = sqlGenGenJoinStr({ + join, + mtch: __mtch, + table_name, + }); + btch_mtch += str; + values.push(...batch_values); if (i < mtch.__batch.matches.length - 1) { btch_mtch += ` ${mtch.__batch.operator || "OR"} `; } } btch_mtch += `)`; - return btch_mtch; + return { + str: btch_mtch, + values, + }; } - return `${typeof mtch.source == "object" ? mtch.source.tableName : table_name}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}=${(() => { + const equality = mtch.raw_equality || "="; + const lhs = `${typeof mtch.source == "object" ? mtch.source.tableName : table_name}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}`; + const rhs = `${(() => { if (mtch.targetLiteral) { - if (typeof mtch.targetLiteral == "number") { - return `${mtch.targetLiteral}`; - } - return `'${mtch.targetLiteral}'`; + values.push(mtch.targetLiteral); + // if (typeof mtch.targetLiteral == "number") { + // return `${mtch.targetLiteral}`; + // } + // return `'${mtch.targetLiteral}'`; + return `?`; } if (join.alias) { return `${typeof mtch.target == "object" @@ -30,4 +44,22 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }) { ? mtch.target.tableName : join.tableName}.${typeof mtch.target == "object" ? mtch.target.fieldName : mtch.target}`; })()}`; + if (mtch.between) { + values.push(mtch.between.min, mtch.between.max); + return { + str: `${lhs} BETWEEN ? AND ?`, + values, + }; + } + if (mtch.not_between) { + values.push(mtch.not_between.min, mtch.not_between.max); + return { + str: `${lhs} NOT BETWEEN ? AND ?`, + values, + }; + } + return { + str: `${lhs} ${equality} ${rhs}`, + values, + }; } diff --git a/dist/utils/sql-generator-gen-query-str.js b/dist/utils/sql-generator-gen-query-str.js index 5acfc78..cd1311b 100644 --- a/dist/utils/sql-generator-gen-query-str.js +++ b/dist/utils/sql-generator-gen-query-str.js @@ -94,9 +94,36 @@ export default function sqlGenGenQueryStr(params) { return `${joinTableName}.${selectField}`; } else if (typeof selectField == "object") { - let aliasSelectField = selectField.count - ? `COUNT(${joinTableName}.${selectField.field})` - : `${joinTableName}.${selectField.field}`; + let aliasSelectField = `${joinTableName}.${selectField.field}`; + if (selectField.count) { + aliasSelectField = `COUNT(${joinTableName}.${selectField.field})`; + } + if (selectField.sum) { + aliasSelectField = `SUM(${joinTableName}.${selectField.field})`; + } + if (selectField.average) { + aliasSelectField = `AVERAGE(${joinTableName}.${selectField.field})`; + } + if (selectField.max) { + aliasSelectField = `MAX(${joinTableName}.${selectField.field})`; + } + if (selectField.min) { + aliasSelectField = `MIN(${joinTableName}.${selectField.field})`; + } + if (selectField.distinct) { + aliasSelectField = `DISTINCT ${joinTableName}.${selectField.field}`; + } + if (selectField.group_concat && + selectField.alias) { + return sqlGenGrabConcatStr({ + field: `${joinTableName}.${selectField.field}`, + alias: selectField.alias, + separator: selectField.group_concat + .separator, + distinct: selectField.group_concat + .distinct, + }); + } if (selectField.alias) aliasSelectField += ` AS ${selectField.alias}`; return aliasSelectField; @@ -135,22 +162,28 @@ export default function sqlGenGenQueryStr(params) { if (Array.isArray(join.match)) { return ("(" + join.match - .map((mtch) => sqlGenGenJoinStr({ - mtch, - join, - table_name, - })) + .map((mtch) => { + const { str, values } = sqlGenGenJoinStr({ + mtch, + join, + table_name, + }); + sqlSearhValues.push(...values); + return str; + }) .join(join.operator ? ` ${join.operator} ` : " AND ") + ")"); } else if (typeof join.match == "object") { - return sqlGenGenJoinStr({ + const { str, values } = sqlGenGenJoinStr({ mtch: join.match, join, table_name, }); + sqlSearhValues.push(...values); + return str; } })()); }) diff --git a/dist/utils/sql-generator-gen-search-str.d.ts b/dist/utils/sql-generator-gen-search-str.d.ts index 307085e..563daf8 100644 --- a/dist/utils/sql-generator-gen-search-str.d.ts +++ b/dist/utils/sql-generator-gen-search-str.d.ts @@ -1,4 +1,4 @@ -import type { ServerQueryParamsJoin, ServerQueryQueryObject } from "../types"; +import type { ServerQueryParamsJoin, ServerQueryQueryObject, SQLInsertGenValueType } from "../types"; type Param = { queryObj: ServerQueryQueryObject[string]; join?: (ServerQueryParamsJoin | ServerQueryParamsJoin[] | undefined)[]; @@ -7,6 +7,6 @@ type Param = { }; export default function sqlGenGenSearchStr({ queryObj, join, field, table_name, }: Param): { str: string; - values: (string | number | Float32Array | Buffer)[]; + values: SQLInsertGenValueType[]; }; export {}; diff --git a/dist/utils/sql-generator-gen-search-str.js b/dist/utils/sql-generator-gen-search-str.js index 0ff0e6c..9f75431 100644 --- a/dist/utils/sql-generator-gen-search-str.js +++ b/dist/utils/sql-generator-gen-search-str.js @@ -61,6 +61,14 @@ export default function sqlGenGenSearchStr({ queryObj, join, field, table_name, } } } + else if (queryObj.raw_equality && queryObj.value) { + str = `${finalFieldName} ${queryObj.raw_equality} ?`; + sqlSearhValues.push(queryObj.value); + } + else if (queryObj.between) { + str = `${finalFieldName} BETWEEN ? AND ?`; + sqlSearhValues.push(queryObj.between.min, queryObj.between.max); + } else { const valueParsed = queryObj.value ? queryObj.value : undefined; const operatorStrParam = sqlGenOperatorGen({ diff --git a/dist/utils/sql-generator-grab-concat-str.d.ts b/dist/utils/sql-generator-grab-concat-str.d.ts index 9955643..3eb39c7 100644 --- a/dist/utils/sql-generator-grab-concat-str.d.ts +++ b/dist/utils/sql-generator-grab-concat-str.d.ts @@ -2,6 +2,7 @@ type Param = { field: string; alias: string; separator?: string; + distinct?: boolean; }; -export default function sqlGenGrabConcatStr({ alias, field, separator, }: Param): string; +export default function sqlGenGrabConcatStr({ alias, field, separator, distinct, }: Param): string; export {}; diff --git a/dist/utils/sql-generator-grab-concat-str.js b/dist/utils/sql-generator-grab-concat-str.js index 099b0a1..e2f343d 100644 --- a/dist/utils/sql-generator-grab-concat-str.js +++ b/dist/utils/sql-generator-grab-concat-str.js @@ -1,4 +1,10 @@ -export default function sqlGenGrabConcatStr({ alias, field, separator = ",", }) { - let gc = `GROUP_CONCAT(${field}, '${separator}') AS ${alias}`; +export default function sqlGenGrabConcatStr({ alias, field, separator = ",", distinct, }) { + let gc = `GROUP_CONCAT(`; + if (distinct) { + gc += `DISTINCT `; + } + gc += `${field}, '${separator}'`; + gc += `)`; + gc += ` AS ${alias}`; return gc; } diff --git a/dist/utils/sql-generator-grab-select-field-sql.js b/dist/utils/sql-generator-grab-select-field-sql.js index 6772388..4056788 100644 --- a/dist/utils/sql-generator-grab-select-field-sql.js +++ b/dist/utils/sql-generator-grab-select-field-sql.js @@ -13,24 +13,37 @@ export default function sqlGenGrabSelectFieldSQL({ selectFields, append_table_na const fld_name = `${String(fld.fieldName)}`; if (fld.count) { fld_str += `COUNT(${fld_name})`; - if (fld.count.alias) { - fld_str += ` AS ${fld.count.alias}`; - } } else if (fld.sum) { - fld_str += `SUM(${fld_name}) AS ${fld.sum.alias}`; + fld_str += `SUM(${fld_name})`; + } + else if (fld.average) { + fld_str += `AVERAGE(${fld_name})`; + } + else if (fld.max) { + fld_str += `MAX(${fld_name})`; + } + else if (fld.min) { + fld_str += `MIN(${fld_name})`; + } + else if (fld.distinct) { + fld_str += `DISTINCT ${fld_name}`; } else if (fld.group_concat) { fld_str += sqlGenGrabConcatStr({ field: fld_name, alias: fld.group_concat.alias, separator: fld.group_concat.separator, + distinct: fld.group_concat.distinct, }); } else { fld_str += final_fld_name + (fld.alias ? ` as ${fld.alias}` : ``); } + if (fld.alias) { + fld_str += ` AS ${fld.alias}`; + } } else { fld_str += final_fld_name; diff --git a/package.json b/package.json index 137c8fc..29cfefe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bun-sqlite", - "version": "1.1.0", + "version": "1.1.1", "description": "SQLite manager for Bun", "author": "Benjamin Toby", "main": "dist/index.js", diff --git a/src/lib/sqlite/db-schema-manager.ts b/src/lib/sqlite/db-schema-manager.ts index 27cf859..5fb2706 100644 --- a/src/lib/sqlite/db-schema-manager.ts +++ b/src/lib/sqlite/db-schema-manager.ts @@ -589,11 +589,10 @@ class SQLiteSchemaManager { if (!existingIndexes.includes(index.indexName)) { console.log(`Creating index: ${index.indexName}`); const fields = index.indexTableFields - .map((f) => `"${f.value}"`) + .map((f) => `"${f}"`) .join(", "); - const unique = index.indexType === "regular" ? "" : ""; // SQLite doesn't have FULLTEXT in CREATE INDEX this.db.run( - `CREATE ${unique}INDEX "${index.indexName}" ON "${table.tableName}" (${fields})`, + `CREATE INDEX "${index.indexName}" ON "${table.tableName}" (${fields})`, ); } } diff --git a/src/types/index.ts b/src/types/index.ts index 2e30a8a..3553323 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -232,12 +232,12 @@ export interface BUN_SQLITE_ForeignKeyType { * Describes a table index and the fields it covers. */ export interface BUN_SQLITE_IndexSchemaType { - id?: string | number; + /** + * Name of the index as it would appear on schema. Eg. + * `idx_user_id_index` + */ indexName?: string; - indexType?: (typeof IndexTypes)[number]; - indexTableFields?: BUN_SQLITE_IndexTableFieldType[]; - alias?: string; - newTempIndex?: boolean; + indexTableFields?: string[]; } /** @@ -785,11 +785,12 @@ export type TableSelectFieldsObject< count?: { alias?: string; }; - sum?: TableSelectFieldsBasicDirective; - max?: TableSelectFieldsBasicDirective; - min?: TableSelectFieldsBasicDirective; - average?: TableSelectFieldsBasicDirective; + sum?: boolean; + max?: boolean; + min?: boolean; + average?: boolean; group_concat?: Omit; + distinct?: boolean; }; export type TableSelectFieldsBasicDirective = { @@ -828,7 +829,7 @@ export type ServerQueryObjectValue = export type ServerQueryObject< T extends object = { [key: string]: any }, K extends string = string, -> = { +> = SQLComparisonsParams & { value?: ServerQueryObjectValue; nullValue?: boolean; notNullValue?: boolean; @@ -921,12 +922,50 @@ export type GroupConcatObject = { * Separator. Default `,` */ separator?: string; + distinct?: boolean; }; export type SelectFieldObject = { field: keyof Field; alias?: string; count?: boolean; + sum?: boolean; + max?: boolean; + min?: boolean; + average?: boolean; + group_concat?: Pick; + distinct?: boolean; +}; + +export const SQlComparisons = [ + ">", + "<>", + "<", + "=", + ">=", + "<=", + "!=", + "IS NOT", + "IS", + "IS NULL", + "IS NOT NULL", + "IN", + "NOT IN", + "LIKE", + "NOT LIKE", + "GLOB", + "NOT GLOB", +] as const; + +export type SQLBetween = { + min: SQLInsertGenValueType; + max: SQLInsertGenValueType; +}; + +export type SQLComparisonsParams = { + raw_equality?: (typeof SQlComparisons)[number]; + between?: SQLBetween; + not_between?: SQLBetween; }; /** @@ -934,7 +973,7 @@ export type SelectFieldObject = { */ export type ServerQueryParamsJoinMatchObject< Field extends object = { [key: string]: any }, -> = { +> = SQLComparisonsParams & { /** Field name from the **Root Table** */ source?: string | ServerQueryParamsJoinMatchSourceTargetObject; /** Field name from the **Join Table** */ diff --git a/src/utils/sql-generator-gen-join-str.ts b/src/utils/sql-generator-gen-join-str.ts index f7c513e..be646de 100644 --- a/src/utils/sql-generator-gen-join-str.ts +++ b/src/utils/sql-generator-gen-join-str.ts @@ -1,6 +1,7 @@ import type { ServerQueryParamsJoin, ServerQueryParamsJoinMatchObject, + SQLInsertGenValueType, } from "../types"; type Param = { @@ -10,6 +11,8 @@ type Param = { }; export default function sqlGenGenJoinStr({ join, mtch, table_name }: Param) { + let values: SQLInsertGenValueType[] = []; + if (mtch.__batch) { let btch_mtch = ``; btch_mtch += `(`; @@ -18,7 +21,17 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }: Param) { const __mtch = mtch.__batch.matches[ i ] as ServerQueryParamsJoinMatchObject; - btch_mtch += `${sqlGenGenJoinStr({ join, mtch: __mtch, table_name })}`; + + const { str, values: batch_values } = sqlGenGenJoinStr({ + join, + mtch: __mtch, + table_name, + }); + + btch_mtch += str; + + values.push(...batch_values); + if (i < mtch.__batch.matches.length - 1) { btch_mtch += ` ${mtch.__batch.operator || "OR"} `; } @@ -26,19 +39,28 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }: Param) { btch_mtch += `)`; - return btch_mtch; + return { + str: btch_mtch, + values, + }; } - return `${ + const equality = mtch.raw_equality || "="; + + const lhs = `${ typeof mtch.source == "object" ? mtch.source.tableName : table_name - }.${ - typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source - }=${(() => { + }.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}`; + + const rhs = `${(() => { if (mtch.targetLiteral) { - if (typeof mtch.targetLiteral == "number") { - return `${mtch.targetLiteral}`; - } - return `'${mtch.targetLiteral}'`; + values.push(mtch.targetLiteral); + + // if (typeof mtch.targetLiteral == "number") { + // return `${mtch.targetLiteral}`; + // } + // return `'${mtch.targetLiteral}'`; + + return `?`; } if (join.alias) { @@ -61,4 +83,27 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }: Param) { typeof mtch.target == "object" ? mtch.target.fieldName : mtch.target }`; })()}`; + + if (mtch.between) { + values.push(mtch.between.min, mtch.between.max); + + return { + str: `${lhs} BETWEEN ? AND ?`, + values, + }; + } + + if (mtch.not_between) { + values.push(mtch.not_between.min, mtch.not_between.max); + + return { + str: `${lhs} NOT BETWEEN ? AND ?`, + values, + }; + } + + return { + str: `${lhs} ${equality} ${rhs}`, + values, + }; } diff --git a/src/utils/sql-generator-gen-query-str.ts b/src/utils/sql-generator-gen-query-str.ts index cfd3c01..4475d8a 100644 --- a/src/utils/sql-generator-gen-query-str.ts +++ b/src/utils/sql-generator-gen-query-str.ts @@ -121,9 +121,48 @@ export default function sqlGenGenQueryStr< if (typeof selectField == "string") { return `${joinTableName}.${selectField}`; } else if (typeof selectField == "object") { - let aliasSelectField = selectField.count - ? `COUNT(${joinTableName}.${selectField.field})` - : `${joinTableName}.${selectField.field}`; + let aliasSelectField = `${joinTableName}.${selectField.field}`; + + if (selectField.count) { + aliasSelectField = `COUNT(${joinTableName}.${selectField.field})`; + } + + if (selectField.sum) { + aliasSelectField = `SUM(${joinTableName}.${selectField.field})`; + } + + if (selectField.average) { + aliasSelectField = `AVERAGE(${joinTableName}.${selectField.field})`; + } + + if (selectField.max) { + aliasSelectField = `MAX(${joinTableName}.${selectField.field})`; + } + + if (selectField.min) { + aliasSelectField = `MIN(${joinTableName}.${selectField.field})`; + } + + if (selectField.distinct) { + aliasSelectField = `DISTINCT ${joinTableName}.${selectField.field}`; + } + + if ( + selectField.group_concat && + selectField.alias + ) { + return sqlGenGrabConcatStr({ + field: `${joinTableName}.${selectField.field}`, + alias: selectField.alias, + separator: + selectField.group_concat + .separator, + distinct: + selectField.group_concat + .distinct, + }); + } + if (selectField.alias) aliasSelectField += ` AS ${selectField.alias}`; return aliasSelectField; @@ -168,13 +207,18 @@ export default function sqlGenGenQueryStr< return ( "(" + join.match - .map((mtch) => - sqlGenGenJoinStr({ - mtch, - join, - table_name, - }), - ) + .map((mtch) => { + const { str, values } = + sqlGenGenJoinStr({ + mtch, + join, + table_name, + }); + + sqlSearhValues.push(...values); + + return str; + }) .join( join.operator ? ` ${join.operator} ` @@ -183,11 +227,15 @@ export default function sqlGenGenQueryStr< ")" ); } else if (typeof join.match == "object") { - return sqlGenGenJoinStr({ + const { str, values } = sqlGenGenJoinStr({ mtch: join.match, join, table_name, }); + + sqlSearhValues.push(...values); + + return str; } })() ); diff --git a/src/utils/sql-generator-gen-search-str.ts b/src/utils/sql-generator-gen-search-str.ts index 12022d2..230974d 100644 --- a/src/utils/sql-generator-gen-search-str.ts +++ b/src/utils/sql-generator-gen-search-str.ts @@ -1,7 +1,9 @@ import type { + QueryRawValueType, ServerQueryParamsJoin, ServerQueryQueryObject, ServerQueryValuesObject, + SQLInsertGenValueType, } from "../types"; import sqlGenOperatorGen from "./sql-gen-operator-gen"; @@ -18,7 +20,7 @@ export default function sqlGenGenSearchStr({ field, table_name, }: Param) { - let sqlSearhValues = []; + let sqlSearhValues: SQLInsertGenValueType[] = []; const finalFieldName = (() => { if (queryObj?.tableName) { @@ -91,6 +93,12 @@ export default function sqlGenGenSearchStr({ sqlSearhValues.push(operatorStrParam.param); } } + } else if (queryObj.raw_equality && queryObj.value) { + str = `${finalFieldName} ${queryObj.raw_equality} ?`; + sqlSearhValues.push(queryObj.value); + } else if (queryObj.between) { + str = `${finalFieldName} BETWEEN ? AND ?`; + sqlSearhValues.push(queryObj.between.min, queryObj.between.max); } else { const valueParsed = queryObj.value ? queryObj.value : undefined; diff --git a/src/utils/sql-generator-grab-concat-str.ts b/src/utils/sql-generator-grab-concat-str.ts index 9d5242e..24d8aef 100644 --- a/src/utils/sql-generator-grab-concat-str.ts +++ b/src/utils/sql-generator-grab-concat-str.ts @@ -2,13 +2,24 @@ type Param = { field: string; alias: string; separator?: string; + distinct?: boolean; }; export default function sqlGenGrabConcatStr({ alias, field, separator = ",", + distinct, }: Param) { - let gc = `GROUP_CONCAT(${field}, '${separator}') AS ${alias}`; + let gc = `GROUP_CONCAT(`; + + if (distinct) { + gc += `DISTINCT `; + } + + gc += `${field}, '${separator}'`; + gc += `)`; + gc += ` AS ${alias}`; + return gc; } diff --git a/src/utils/sql-generator-grab-select-field-sql.ts b/src/utils/sql-generator-grab-select-field-sql.ts index 1a85bf0..668570b 100644 --- a/src/utils/sql-generator-grab-select-field-sql.ts +++ b/src/utils/sql-generator-grab-select-field-sql.ts @@ -28,21 +28,31 @@ export default function sqlGenGrabSelectFieldSQL< if (fld.count) { fld_str += `COUNT(${fld_name})`; - if (fld.count.alias) { - fld_str += ` AS ${fld.count.alias}`; - } } else if (fld.sum) { - fld_str += `SUM(${fld_name}) AS ${fld.sum.alias}`; + fld_str += `SUM(${fld_name})`; + } else if (fld.average) { + fld_str += `AVERAGE(${fld_name})`; + } else if (fld.max) { + fld_str += `MAX(${fld_name})`; + } else if (fld.min) { + fld_str += `MIN(${fld_name})`; + } else if (fld.distinct) { + fld_str += `DISTINCT ${fld_name}`; } else if (fld.group_concat) { fld_str += sqlGenGrabConcatStr({ field: fld_name, alias: fld.group_concat.alias, separator: fld.group_concat.separator, + distinct: fld.group_concat.distinct, }); } else { fld_str += final_fld_name + (fld.alias ? ` as ${fld.alias}` : ``); } + + if (fld.alias) { + fld_str += ` AS ${fld.alias}`; + } } else { fld_str += final_fld_name; }