Updates
This commit is contained in:
@@ -9,12 +9,16 @@ interface SQLDeleteGenReturn {
|
||||
export default function sqlDeleteGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLDeleteGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
let queryStr = `DELETE FROM ${finalDbName}${tableName}`;
|
||||
|
||||
let deleteBatch: string[] = [];
|
||||
let queryArr: string[] = [];
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
dbFullName?: string;
|
||||
};
|
||||
|
||||
type Return =
|
||||
@@ -34,6 +34,8 @@ export default function sqlGenerator({
|
||||
|
||||
const sqlSearhValues: string[] = [];
|
||||
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
/**
|
||||
* # Generate Query
|
||||
*/
|
||||
@@ -48,10 +50,10 @@ export default function sqlGenerator({
|
||||
}) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${dbFullName}.${queryObj.tableName}.${field}`;
|
||||
return `${finalDbName}${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${dbFullName}.${tableName}.${field}`;
|
||||
return `${finalDbName}${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@@ -133,7 +135,7 @@ export default function sqlGenerator({
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch: import("../../../types").ServerQueryParamsJoinMatchObject,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join: import("../../../types").ServerQueryParamsJoin
|
||||
) {
|
||||
return `${dbFullName}.${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
||||
}.${
|
||||
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
|
||||
@@ -143,7 +145,7 @@ export default function sqlGenerator({
|
||||
}
|
||||
|
||||
if (join.alias) {
|
||||
return `${dbFullName}.${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias
|
||||
@@ -154,7 +156,7 @@ export default function sqlGenerator({
|
||||
}`;
|
||||
}
|
||||
|
||||
return `${dbFullName}.${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName
|
||||
@@ -171,14 +173,14 @@ export default function sqlGenerator({
|
||||
if (genObject.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${dbFullName}.${tableName}.${fld}`)
|
||||
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
|
||||
.join(",")}`;
|
||||
} else {
|
||||
str += ` ${genObject.selectFields?.join(",")}`;
|
||||
}
|
||||
} else {
|
||||
if (genObject.join) {
|
||||
str += ` ${dbFullName}.${tableName}.*`;
|
||||
str += ` ${finalDbName}${tableName}.*`;
|
||||
} else {
|
||||
str += " *";
|
||||
}
|
||||
@@ -204,11 +206,11 @@ export default function sqlGenerator({
|
||||
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;
|
||||
@@ -216,14 +218,14 @@ export default function sqlGenerator({
|
||||
})
|
||||
.join(",");
|
||||
} else {
|
||||
return `${dbFullName}.${joinTableName}.*`;
|
||||
return `${finalDbName}${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
|
||||
str += ` FROM ${dbFullName}.${tableName}`;
|
||||
str += ` FROM ${finalDbName}${tableName}`;
|
||||
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
@@ -234,10 +236,10 @@ export default function sqlGenerator({
|
||||
join.joinType +
|
||||
" " +
|
||||
(join.alias
|
||||
? `${dbFullName}.${join.tableName}` +
|
||||
? `${finalDbName}${join.tableName}` +
|
||||
" " +
|
||||
join.alias
|
||||
: `${dbFullName}.${join.tableName}`) +
|
||||
: `${finalDbName}${join.tableName}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@@ -274,7 +276,7 @@ export default function sqlGenerator({
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${dbFullName}.${tableName}.${genObject.order.field}`
|
||||
? `${finalDbName}${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field
|
||||
} ${genObject.order.strategy}`;
|
||||
|
||||
|
||||
@@ -11,13 +11,16 @@ interface SQLInsertGenReturn {
|
||||
export default function sqlInsertGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLInsertGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
if (Array.isArray(data) && data?.[0]) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys: string[] = [];
|
||||
|
||||
data.forEach((dt) => {
|
||||
@@ -48,7 +51,7 @@ export default function sqlInsertGenerator({
|
||||
.join(",")})`
|
||||
);
|
||||
});
|
||||
let query = `INSERT INTO ${tableName} (${insertKeys.join(
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(
|
||||
","
|
||||
)}) VALUES ${queryBatches.join(",")}`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user