Updates
This commit is contained in:
@@ -5,8 +5,9 @@ interface SQLDeleteGenReturn {
|
||||
/**
|
||||
* # SQL Delete Generator
|
||||
*/
|
||||
export default function sqlDeleteGenerator({ tableName, data, }: {
|
||||
export default function sqlDeleteGenerator({ tableName, data, dbFullName, }: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLDeleteGenReturn | undefined;
|
||||
export {};
|
||||
|
||||
@@ -4,9 +4,10 @@ exports.default = sqlDeleteGenerator;
|
||||
/**
|
||||
* # SQL Delete Generator
|
||||
*/
|
||||
function sqlDeleteGenerator({ tableName, data, }) {
|
||||
function sqlDeleteGenerator({ tableName, data, dbFullName, }) {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
let queryStr = `DELETE FROM ${finalDbName}${tableName}`;
|
||||
let deleteBatch = [];
|
||||
let queryArr = [];
|
||||
Object.keys(data).forEach((ky) => {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { ServerQueryParam } from "../../../types";
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
dbFullName?: string;
|
||||
};
|
||||
type Return = {
|
||||
string: string;
|
||||
|
||||
+16
-15
@@ -11,16 +11,17 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
const sqlSearhValues = [];
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
/**
|
||||
* # Generate Query
|
||||
*/
|
||||
function genSqlSrchStr({ queryObj, join, field, }) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj === null || queryObj === void 0 ? void 0 : queryObj.tableName) {
|
||||
return `${dbFullName}.${queryObj.tableName}.${field}`;
|
||||
return `${finalDbName}${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${dbFullName}.${tableName}.${field}`;
|
||||
return `${finalDbName}${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@@ -87,18 +88,18 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
function generateJoinStr(
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join) {
|
||||
return `${dbFullName}.${typeof mtch.source == "object" ? mtch.source.tableName : tableName}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}=${(() => {
|
||||
return `${finalDbName}${typeof mtch.source == "object" ? mtch.source.tableName : tableName}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}=${(() => {
|
||||
if (mtch.targetLiteral) {
|
||||
return `'${mtch.targetLiteral}'`;
|
||||
}
|
||||
if (join.alias) {
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
return `${finalDbName}${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
: mtch.target}`;
|
||||
}
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
return `${finalDbName}${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
@@ -110,7 +111,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
let str = "SELECT";
|
||||
if ((_a = genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${dbFullName}.${tableName}.${fld}`).join(",")}`;
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${finalDbName}${tableName}.${fld}`).join(",")}`;
|
||||
}
|
||||
else {
|
||||
str += ` ${(_c = genObject.selectFields) === null || _c === void 0 ? void 0 : _c.join(",")}`;
|
||||
@@ -118,7 +119,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
}
|
||||
else {
|
||||
if (genObject.join) {
|
||||
str += ` ${dbFullName}.${tableName}.*`;
|
||||
str += ` ${finalDbName}${tableName}.*`;
|
||||
}
|
||||
else {
|
||||
str += " *";
|
||||
@@ -141,12 +142,12 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return `${dbFullName}.${joinTableName}.${selectField}`;
|
||||
return `${finalDbName}${joinTableName}.${selectField}`;
|
||||
}
|
||||
else if (typeof selectField == "object") {
|
||||
let aliasSelectField = selectField.count
|
||||
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
|
||||
: `${dbFullName}.${joinTableName}.${selectField.field}`;
|
||||
? `COUNT(${finalDbName}${joinTableName}.${selectField.field})`
|
||||
: `${finalDbName}${joinTableName}.${selectField.field}`;
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${selectField.alias}`;
|
||||
return aliasSelectField;
|
||||
@@ -155,13 +156,13 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
.join(",");
|
||||
}
|
||||
else {
|
||||
return `${dbFullName}.${joinTableName}.*`;
|
||||
return `${finalDbName}${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
str += ` FROM ${dbFullName}.${tableName}`;
|
||||
str += ` FROM ${finalDbName}${tableName}`;
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
" " +
|
||||
@@ -170,10 +171,10 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
return (join.joinType +
|
||||
" " +
|
||||
(join.alias
|
||||
? `${dbFullName}.${join.tableName}` +
|
||||
? `${finalDbName}${join.tableName}` +
|
||||
" " +
|
||||
join.alias
|
||||
: `${dbFullName}.${join.tableName}`) +
|
||||
: `${finalDbName}${join.tableName}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@@ -200,7 +201,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
}
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${genObject.join
|
||||
? `${dbFullName}.${tableName}.${genObject.order.field}`
|
||||
? `${finalDbName}${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field} ${genObject.order.strategy}`;
|
||||
if (genObject.limit)
|
||||
queryString += ` LIMIT ${genObject.limit}`;
|
||||
|
||||
@@ -5,8 +5,9 @@ interface SQLInsertGenReturn {
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
export default function sqlInsertGenerator({ tableName, data, }: {
|
||||
export default function sqlInsertGenerator({ tableName, data, dbFullName, }: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLInsertGenReturn | undefined;
|
||||
export {};
|
||||
|
||||
@@ -5,10 +5,10 @@ exports.default = sqlInsertGenerator;
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
function sqlInsertGenerator({ tableName, data, }) {
|
||||
function sqlInsertGenerator({ tableName, data, dbFullName, }) {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
try {
|
||||
if (Array.isArray(data) && (data === null || data === void 0 ? void 0 : data[0])) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys = [];
|
||||
data.forEach((dt) => {
|
||||
const kys = Object.keys(dt);
|
||||
@@ -33,7 +33,7 @@ function sqlInsertGenerator({ tableName, data, }) {
|
||||
})
|
||||
.join(",")})`);
|
||||
});
|
||||
let query = `INSERT INTO ${tableName} (${insertKeys.join(",")}) VALUES ${queryBatches.join(",")}`;
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(",")}) VALUES ${queryBatches.join(",")}`;
|
||||
return {
|
||||
query: query,
|
||||
values: queryValues,
|
||||
|
||||
Reference in New Issue
Block a user