Refactor select count strategy. Run the count query together with the normal query
This commit is contained in:
parent
0827cfed11
commit
a57a063f67
16
dist/lib/sqlite/db-select.js
vendored
16
dist/lib/sqlite/db-select.js
vendored
@ -18,9 +18,8 @@ export default async function DbSelect({ table, query, count, targetId, }) {
|
|||||||
sqlObj = sqlGenerator({
|
sqlObj = sqlGenerator({
|
||||||
tableName: table,
|
tableName: table,
|
||||||
genObject: finalQuery,
|
genObject: finalQuery,
|
||||||
count,
|
|
||||||
});
|
});
|
||||||
const sql = mysql.format(sqlObj.string, sqlObj.values);
|
let sql = mysql.format(sqlObj.string, sqlObj.values);
|
||||||
const res = DbClient.query(sql);
|
const res = DbClient.query(sql);
|
||||||
const batchRes = res.all();
|
const batchRes = res.all();
|
||||||
let resp = {
|
let resp = {
|
||||||
@ -33,10 +32,17 @@ export default async function DbSelect({ table, query, count, targetId, }) {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
if (count) {
|
if (count) {
|
||||||
const count_val = count ? batchRes[0]?.["COUNT(*)"] : undefined;
|
let count_sql_object = sqlGenerator({
|
||||||
|
tableName: table,
|
||||||
|
genObject: finalQuery,
|
||||||
|
count,
|
||||||
|
});
|
||||||
|
let count_sql = mysql.format(count_sql_object.string, count_sql_object.values);
|
||||||
|
count_sql = `SELECT COUNT(*) FROM (${count_sql}) as c`;
|
||||||
|
const count_res = DbClient.query(count_sql).all();
|
||||||
|
const count_val = count_res[0]?.["COUNT(*)"];
|
||||||
resp["count"] = Number(count_val);
|
resp["count"] = Number(count_val);
|
||||||
delete resp.payload;
|
resp["debug"]["count_sql"] = count_sql;
|
||||||
delete resp.singleRes;
|
|
||||||
}
|
}
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|||||||
11
dist/utils/sql-generator.js
vendored
11
dist/utils/sql-generator.js
vendored
@ -141,10 +141,7 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
|||||||
: undefined;
|
: undefined;
|
||||||
let queryString = (() => {
|
let queryString = (() => {
|
||||||
let str = "SELECT";
|
let str = "SELECT";
|
||||||
if (count) {
|
if (genObject?.selectFields?.[0]) {
|
||||||
str += ` COUNT(*)`;
|
|
||||||
}
|
|
||||||
else if (genObject?.selectFields?.[0]) {
|
|
||||||
if (genObject.join) {
|
if (genObject.join) {
|
||||||
str += ` ${genObject.selectFields
|
str += ` ${genObject.selectFields
|
||||||
?.map((fld) => typeof fld == "object"
|
?.map((fld) => typeof fld == "object"
|
||||||
@ -201,7 +198,7 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
|||||||
}
|
}
|
||||||
str += `, ${countSqls.join(",")}`;
|
str += `, ${countSqls.join(",")}`;
|
||||||
}
|
}
|
||||||
if (genObject?.join && !count) {
|
if (genObject?.join) {
|
||||||
const existingJoinTableNames = [tableName];
|
const existingJoinTableNames = [tableName];
|
||||||
str +=
|
str +=
|
||||||
"," +
|
"," +
|
||||||
@ -364,7 +361,7 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
|||||||
orderSrt += ` ${orderFields.join(", ")} ${order.strategy}`;
|
orderSrt += ` ${orderFields.join(", ")} ${order.strategy}`;
|
||||||
return orderSrt;
|
return orderSrt;
|
||||||
}
|
}
|
||||||
if (genObject?.order && !count) {
|
if (genObject?.order) {
|
||||||
let orderSrt = ` ORDER BY`;
|
let orderSrt = ` ORDER BY`;
|
||||||
if (Array.isArray(genObject.order)) {
|
if (Array.isArray(genObject.order)) {
|
||||||
for (let i = 0; i < genObject.order.length; i++) {
|
for (let i = 0; i < genObject.order.length; i++) {
|
||||||
@ -383,7 +380,7 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
|||||||
}
|
}
|
||||||
if (genObject?.limit && !count)
|
if (genObject?.limit && !count)
|
||||||
queryString += ` LIMIT ${genObject.limit}`;
|
queryString += ` LIMIT ${genObject.limit}`;
|
||||||
if (genObject?.offset && !count) {
|
if (genObject?.offset) {
|
||||||
queryString += ` OFFSET ${genObject.offset}`;
|
queryString += ` OFFSET ${genObject.offset}`;
|
||||||
}
|
}
|
||||||
else if (genObject?.page && genObject.limit && !count) {
|
else if (genObject?.page && genObject.limit && !count) {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@moduletrace/bun-sqlite",
|
"name": "@moduletrace/bun-sqlite",
|
||||||
"version": "1.0.33",
|
"version": "1.0.34",
|
||||||
"description": "SQLite manager for Bun",
|
"description": "SQLite manager for Bun",
|
||||||
"author": "Benjamin Toby",
|
"author": "Benjamin Toby",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
|||||||
@ -44,10 +44,9 @@ export default async function DbSelect<
|
|||||||
sqlObj = sqlGenerator({
|
sqlObj = sqlGenerator({
|
||||||
tableName: table,
|
tableName: table,
|
||||||
genObject: finalQuery,
|
genObject: finalQuery,
|
||||||
count,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const sql = mysql.format(sqlObj.string, sqlObj.values);
|
let sql = mysql.format(sqlObj.string, sqlObj.values);
|
||||||
|
|
||||||
const res = DbClient.query<Schema, Schema[]>(sql);
|
const res = DbClient.query<Schema, Schema[]>(sql);
|
||||||
const batchRes = res.all();
|
const batchRes = res.all();
|
||||||
@ -63,11 +62,24 @@ export default async function DbSelect<
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (count) {
|
if (count) {
|
||||||
const count_val = count ? batchRes[0]?.["COUNT(*)"] : undefined;
|
let count_sql_object = sqlGenerator({
|
||||||
resp["count"] = Number(count_val);
|
tableName: table,
|
||||||
|
genObject: finalQuery,
|
||||||
|
count,
|
||||||
|
});
|
||||||
|
|
||||||
delete resp.payload;
|
let count_sql = mysql.format(
|
||||||
delete resp.singleRes;
|
count_sql_object.string,
|
||||||
|
count_sql_object.values,
|
||||||
|
);
|
||||||
|
|
||||||
|
count_sql = `SELECT COUNT(*) FROM (${count_sql}) as c`;
|
||||||
|
|
||||||
|
const count_res = DbClient.query<Schema, Schema[]>(count_sql).all();
|
||||||
|
|
||||||
|
const count_val = count_res[0]?.["COUNT(*)"];
|
||||||
|
resp["count"] = Number(count_val);
|
||||||
|
resp["debug"]["count_sql"] = count_sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp;
|
return resp;
|
||||||
|
|||||||
@ -221,9 +221,7 @@ export default function sqlGenerator<
|
|||||||
let queryString = (() => {
|
let queryString = (() => {
|
||||||
let str = "SELECT";
|
let str = "SELECT";
|
||||||
|
|
||||||
if (count) {
|
if (genObject?.selectFields?.[0]) {
|
||||||
str += ` COUNT(*)`;
|
|
||||||
} else if (genObject?.selectFields?.[0]) {
|
|
||||||
if (genObject.join) {
|
if (genObject.join) {
|
||||||
str += ` ${genObject.selectFields
|
str += ` ${genObject.selectFields
|
||||||
?.map((fld) =>
|
?.map((fld) =>
|
||||||
@ -295,7 +293,7 @@ export default function sqlGenerator<
|
|||||||
str += `, ${countSqls.join(",")}`;
|
str += `, ${countSqls.join(",")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (genObject?.join && !count) {
|
if (genObject?.join) {
|
||||||
const existingJoinTableNames: string[] = [tableName];
|
const existingJoinTableNames: string[] = [tableName];
|
||||||
|
|
||||||
str +=
|
str +=
|
||||||
@ -493,7 +491,7 @@ export default function sqlGenerator<
|
|||||||
return orderSrt;
|
return orderSrt;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (genObject?.order && !count) {
|
if (genObject?.order) {
|
||||||
let orderSrt = ` ORDER BY`;
|
let orderSrt = ` ORDER BY`;
|
||||||
|
|
||||||
if (Array.isArray(genObject.order)) {
|
if (Array.isArray(genObject.order)) {
|
||||||
@ -514,7 +512,7 @@ export default function sqlGenerator<
|
|||||||
|
|
||||||
if (genObject?.limit && !count) queryString += ` LIMIT ${genObject.limit}`;
|
if (genObject?.limit && !count) queryString += ` LIMIT ${genObject.limit}`;
|
||||||
|
|
||||||
if (genObject?.offset && !count) {
|
if (genObject?.offset) {
|
||||||
queryString += ` OFFSET ${genObject.offset}`;
|
queryString += ` OFFSET ${genObject.offset}`;
|
||||||
} else if (genObject?.page && genObject.limit && !count) {
|
} else if (genObject?.page && genObject.limit && !count) {
|
||||||
queryString += ` OFFSET ${(genObject.page - 1) * genObject.limit}`;
|
queryString += ` OFFSET ${(genObject.page - 1) * genObject.limit}`;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user