Updates
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import { SQLDeleteGeneratorParams } from "../../../types";
|
||||
import sqlEqualityParser from "../../../utils/sql-equality-parser";
|
||||
|
||||
interface SQLDeleteGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
@@ -8,13 +11,10 @@ interface SQLDeleteGenReturn {
|
||||
*/
|
||||
export default function sqlDeleteGenerator({
|
||||
tableName,
|
||||
data,
|
||||
deleteKeyValues,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLDeleteGenReturn | undefined {
|
||||
data,
|
||||
}: SQLDeleteGeneratorParams): SQLDeleteGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
@@ -23,17 +23,44 @@ export default function sqlDeleteGenerator({
|
||||
let deleteBatch: string[] = [];
|
||||
let queryArr: string[] = [];
|
||||
|
||||
Object.keys(data).forEach((ky) => {
|
||||
deleteBatch.push(`${ky}=?`);
|
||||
queryArr.push(data[ky]);
|
||||
});
|
||||
if (data) {
|
||||
Object.keys(data).forEach((ky) => {
|
||||
let value = data[ky] as string | number | null | undefined;
|
||||
const parsedValue =
|
||||
typeof value == "number" ? String(value) : value;
|
||||
|
||||
if (!parsedValue) return;
|
||||
|
||||
if (parsedValue.match(/%/)) {
|
||||
deleteBatch.push(`${ky} LIKE ?`);
|
||||
queryArr.push(parsedValue);
|
||||
} else {
|
||||
deleteBatch.push(`${ky}=?`);
|
||||
queryArr.push(parsedValue);
|
||||
}
|
||||
});
|
||||
} else if (deleteKeyValues) {
|
||||
deleteKeyValues.forEach((ky) => {
|
||||
let value = ky.value as string | number | null | undefined;
|
||||
const parsedValue =
|
||||
typeof value == "number" ? String(value) : value;
|
||||
|
||||
if (!parsedValue) return;
|
||||
|
||||
const operator = sqlEqualityParser(ky.operator || "EQUAL");
|
||||
|
||||
deleteBatch.push(`${ky.key} ${operator} ?`);
|
||||
queryArr.push(parsedValue);
|
||||
});
|
||||
}
|
||||
|
||||
queryStr += ` WHERE ${deleteBatch.join(" AND ")}`;
|
||||
|
||||
return {
|
||||
query: queryStr,
|
||||
values: queryArr,
|
||||
};
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
} catch (error: any) {
|
||||
console.log(`SQL delete gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import sqlEqualityParser from "../../../utils/sql-equality-parser";
|
||||
import { ServerQueryEqualities } from "../../../types";
|
||||
|
||||
type Params = {
|
||||
fieldName: string;
|
||||
value?: string;
|
||||
equality: (typeof ServerQueryEqualities)[number];
|
||||
};
|
||||
|
||||
/**
|
||||
* # SQL Gen Operator Gen
|
||||
* @description Generates an SQL operator for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
export default function sqlGenOperatorGen({
|
||||
fieldName,
|
||||
value,
|
||||
equality,
|
||||
}: Params): string {
|
||||
if (value) {
|
||||
if (equality == "LIKE") {
|
||||
return `LOWER(${fieldName}) LIKE LOWER('%${value}%')`;
|
||||
} else if (equality == "LIKE_RAW") {
|
||||
return `LOWER(${fieldName}) LIKE LOWER('${value}')`;
|
||||
} else if (equality == "NOT LIKE") {
|
||||
return `LOWER(${fieldName}) NOT LIKE LOWER('%${value}%')`;
|
||||
} else if (equality == "NOT LIKE_RAW") {
|
||||
return `LOWER(${fieldName}) NOT LIKE LOWER('${value}')`;
|
||||
} else if (equality == "REGEXP") {
|
||||
return `LOWER(${fieldName}) REGEXP LOWER('${value}')`;
|
||||
} else if (equality == "FULLTEXT") {
|
||||
return `MATCH(${fieldName}) AGAINST('${value}' IN BOOLEAN MODE)`;
|
||||
} else if (equality == "NOT EQUAL") {
|
||||
return `${fieldName} != ${value}`;
|
||||
} else if (equality) {
|
||||
return `${fieldName} ${sqlEqualityParser(equality)} ${value}`;
|
||||
} else {
|
||||
return `${fieldName} = ${value}`;
|
||||
}
|
||||
} else {
|
||||
if (equality == "IS NULL") {
|
||||
return `${fieldName} IS NULL`;
|
||||
} else if (equality == "IS NOT NULL") {
|
||||
return `${fieldName} IS NOT NULL`;
|
||||
} else if (equality) {
|
||||
return `${fieldName} ${sqlEqualityParser(equality)} ?`;
|
||||
} else {
|
||||
return `${fieldName} = ?`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import sqlEqualityParser from "../../../utils/sql-equality-parser";
|
||||
import {
|
||||
ServerQueryParam,
|
||||
ServerQueryParamsJoin,
|
||||
@@ -64,16 +65,30 @@ export default function sqlGenerator<
|
||||
typeof queryObj.value == "number"
|
||||
) {
|
||||
const valueParsed = String(queryObj.value);
|
||||
const operator = sqlEqualityParser(queryObj.equality || "EQUAL");
|
||||
|
||||
if (queryObj.equality == "LIKE") {
|
||||
str = `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`;
|
||||
} else if (queryObj.equality == "LIKE_RAW") {
|
||||
str = `LOWER(${finalFieldName}) LIKE LOWER(?)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "NOT LIKE") {
|
||||
str = `LOWER(${finalFieldName}) NOT LIKE LOWER('%${valueParsed}%')`;
|
||||
} else if (queryObj.equality == "NOT LIKE_RAW") {
|
||||
str = `LOWER(${finalFieldName}) NOT LIKE LOWER(?)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "REGEXP") {
|
||||
str = `${finalFieldName} REGEXP '${valueParsed}'`;
|
||||
str = `LOWER(${finalFieldName}) REGEXP LOWER(?)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "FULLTEXT") {
|
||||
str = `MATCH(${finalFieldName}) AGAINST('${valueParsed}' IN BOOLEAN MODE)`;
|
||||
str = `MATCH(${finalFieldName}) AGAINST(? IN BOOLEAN MODE)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "NOT EQUAL") {
|
||||
str = `${finalFieldName} != ?`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality) {
|
||||
str = `${finalFieldName} ${operator} ?`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else {
|
||||
sqlSearhValues.push(valueParsed);
|
||||
}
|
||||
@@ -173,7 +188,7 @@ export default function sqlGenerator<
|
||||
} else if (genObject?.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
|
||||
?.map((fld) => `${finalDbName}${tableName}.${String(fld)}`)
|
||||
.join(",")}`;
|
||||
} else {
|
||||
str += ` ${genObject.selectFields?.join(",")}`;
|
||||
@@ -272,12 +287,19 @@ export default function sqlGenerator<
|
||||
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
|
||||
}
|
||||
|
||||
if (genObject?.order && !count)
|
||||
if (genObject?.group?.[0]) {
|
||||
queryString += ` GROUP BY ${genObject.group
|
||||
.map((g) => `\`${g.toString()}\``)
|
||||
.join(",")}`;
|
||||
}
|
||||
|
||||
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 && !count) queryString += ` LIMIT ${genObject.limit}`;
|
||||
if (genObject?.offset && !count)
|
||||
|
||||
Reference in New Issue
Block a user