Updates
This commit is contained in:
@@ -8,6 +8,7 @@ type Param<T extends { [key: string]: any } = { [key: string]: any }> = {
|
||||
genObject?: ServerQueryParam<T>;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
count?: boolean;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
@@ -21,7 +22,7 @@ type Return = {
|
||||
*/
|
||||
export default function sqlGenerator<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({ tableName, genObject, dbFullName }: Param<T>): Return {
|
||||
>({ tableName, genObject, dbFullName, count }: Param<T>): Return {
|
||||
const finalQuery = genObject?.query ? genObject.query : undefined;
|
||||
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
@@ -162,7 +163,10 @@ export default function sqlGenerator<
|
||||
|
||||
let queryString = (() => {
|
||||
let str = "SELECT";
|
||||
if (genObject?.selectFields?.[0]) {
|
||||
|
||||
if (count) {
|
||||
str += ` COUNT(*)`;
|
||||
} else if (genObject?.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
|
||||
@@ -178,7 +182,7 @@ export default function sqlGenerator<
|
||||
}
|
||||
}
|
||||
|
||||
if (genObject?.join) {
|
||||
if (genObject?.join && !count) {
|
||||
const existingJoinTableNames: string[] = [tableName];
|
||||
|
||||
str +=
|
||||
@@ -264,15 +268,16 @@ export default function sqlGenerator<
|
||||
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
|
||||
}
|
||||
|
||||
if (genObject?.order)
|
||||
if (genObject?.order && !count)
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${finalDbName}${tableName}.${String(genObject.order.field)}`
|
||||
: String(genObject.order.field)
|
||||
} ${genObject.order.strategy}`;
|
||||
|
||||
if (genObject?.limit) queryString += ` LIMIT ${genObject.limit}`;
|
||||
if (genObject?.offset) queryString += ` OFFSET ${genObject.offset}`;
|
||||
if (genObject?.limit && !count) queryString += ` LIMIT ${genObject.limit}`;
|
||||
if (genObject?.offset && !count)
|
||||
queryString += ` OFFSET ${genObject.offset}`;
|
||||
|
||||
return {
|
||||
string: queryString,
|
||||
|
||||
Reference in New Issue
Block a user