Updates
This commit is contained in:
parent
9402c859f7
commit
ea40a1a8c2
@ -6,6 +6,7 @@ type Params = {
|
||||
queryObj: ServerQueryObject<{
|
||||
[key: string]: any;
|
||||
}, string>;
|
||||
isValueFieldValue?: boolean;
|
||||
};
|
||||
type Return = {
|
||||
str?: string;
|
||||
@ -15,5 +16,5 @@ type Return = {
|
||||
* # SQL Gen Operator Gen
|
||||
* @description Generates an SQL operator for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj, }: Params): Return;
|
||||
export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj, isValueFieldValue, }: Params): Return;
|
||||
export {};
|
||||
|
||||
@ -9,7 +9,7 @@ const sql_equality_parser_1 = __importDefault(require("../../../utils/sql-equali
|
||||
* # SQL Gen Operator Gen
|
||||
* @description Generates an SQL operator for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
function sqlGenOperatorGen({ fieldName, value, equality, queryObj, }) {
|
||||
function sqlGenOperatorGen({ fieldName, value, equality, queryObj, isValueFieldValue, }) {
|
||||
if (queryObj.nullValue) {
|
||||
return { str: `${fieldName} IS NULL` };
|
||||
}
|
||||
@ -17,94 +17,96 @@ function sqlGenOperatorGen({ fieldName, value, equality, queryObj, }) {
|
||||
return { str: `${fieldName} IS NOT NULL` };
|
||||
}
|
||||
if (value) {
|
||||
const finalValue = isValueFieldValue ? value : "?";
|
||||
const finalParams = isValueFieldValue ? undefined : value;
|
||||
if (equality == "MATCH") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(? IN NATURAL LANGUAGE MODE)`,
|
||||
param: value,
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN NATURAL LANGUAGE MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "MATCH_BOOLEAN") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(? IN BOOLEAN MODE)`,
|
||||
param: value,
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN BOOLEAN MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(?)`,
|
||||
param: `%${value}%`,
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
}
|
||||
else if (equality == "LIKE_LOWER_RAW") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(?)`,
|
||||
param: value,
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "LIKE") {
|
||||
return {
|
||||
str: `${fieldName} LIKE ?`,
|
||||
param: `%${value}%`,
|
||||
str: `${fieldName} LIKE ${finalValue}`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
}
|
||||
else if (equality == "LIKE_RAW") {
|
||||
return {
|
||||
str: `${fieldName} LIKE ?`,
|
||||
param: value,
|
||||
str: `${fieldName} LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "NOT_LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(?)`,
|
||||
param: `%${value}%`,
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
}
|
||||
else if (equality == "NOT_LIKE_LOWER_RAW") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(?)`,
|
||||
param: value,
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "NOT LIKE") {
|
||||
return {
|
||||
str: `${fieldName} NOT LIKE ?`,
|
||||
param: value,
|
||||
str: `${fieldName} NOT LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "NOT LIKE_RAW") {
|
||||
return {
|
||||
str: `${fieldName} NOT LIKE ?`,
|
||||
param: value,
|
||||
str: `${fieldName} NOT LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "REGEXP") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) REGEXP LOWER(?)`,
|
||||
param: value,
|
||||
str: `LOWER(${fieldName}) REGEXP LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "FULLTEXT") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(? IN BOOLEAN MODE)`,
|
||||
param: value,
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN BOOLEAN MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "NOT EQUAL") {
|
||||
return {
|
||||
str: `${fieldName} != ?`,
|
||||
param: value,
|
||||
str: `${fieldName} != ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality) {
|
||||
return {
|
||||
str: `${fieldName} ${(0, sql_equality_parser_1.default)(equality)} ?`,
|
||||
param: value,
|
||||
str: `${fieldName} ${(0, sql_equality_parser_1.default)(equality)} ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
str: `${fieldName} = ?`,
|
||||
param: value,
|
||||
str: `${fieldName} = ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,27 +30,38 @@ function sqlGenerator({ tableName, genObject, dbFullName, count }) {
|
||||
return field;
|
||||
})();
|
||||
let str = `${finalFieldName}=?`;
|
||||
function grabValue(val) {
|
||||
var _a;
|
||||
const valueParsed = val;
|
||||
if (!valueParsed)
|
||||
return;
|
||||
const valueString = typeof valueParsed == "string"
|
||||
? valueParsed
|
||||
: valueParsed
|
||||
? valueParsed.fieldName && valueParsed.tableName
|
||||
? `${valueParsed.tableName}.${valueParsed.fieldName}`
|
||||
: (_a = valueParsed.value) === null || _a === void 0 ? void 0 : _a.toString()
|
||||
: undefined;
|
||||
const valueEquality = typeof valueParsed == "object"
|
||||
? valueParsed.equality || queryObj.equality
|
||||
: queryObj.equality;
|
||||
const operatorStrParam = (0, sql_gen_operator_gen_1.default)({
|
||||
queryObj,
|
||||
equality: valueEquality,
|
||||
fieldName: finalFieldName || "",
|
||||
value: (valueString === null || valueString === void 0 ? void 0 : valueString.toString()) || "",
|
||||
isValueFieldValue: Boolean(typeof valueParsed == "object" &&
|
||||
valueParsed.fieldName &&
|
||||
valueParsed.tableName),
|
||||
});
|
||||
return operatorStrParam;
|
||||
}
|
||||
if (Array.isArray(queryObj.value)) {
|
||||
const strArray = [];
|
||||
queryObj.value.forEach((val) => {
|
||||
var _a;
|
||||
const valueParsed = val;
|
||||
if (!valueParsed)
|
||||
const operatorStrParam = grabValue(val);
|
||||
if (!operatorStrParam)
|
||||
return;
|
||||
const valueString = typeof valueParsed == "string"
|
||||
? valueParsed
|
||||
: valueParsed
|
||||
? (_a = valueParsed.value) === null || _a === void 0 ? void 0 : _a.toString()
|
||||
: undefined;
|
||||
const valueEquality = typeof valueParsed == "object"
|
||||
? valueParsed.equality || queryObj.equality
|
||||
: queryObj.equality;
|
||||
const operatorStrParam = (0, sql_gen_operator_gen_1.default)({
|
||||
queryObj,
|
||||
equality: valueEquality,
|
||||
fieldName: finalFieldName || "",
|
||||
value: (valueString === null || valueString === void 0 ? void 0 : valueString.toString()) || "",
|
||||
});
|
||||
if (operatorStrParam.str && operatorStrParam.param) {
|
||||
strArray.push(operatorStrParam.str);
|
||||
sqlSearhValues.push(operatorStrParam.param);
|
||||
@ -61,6 +72,15 @@ function sqlGenerator({ tableName, genObject, dbFullName, count }) {
|
||||
});
|
||||
str = "(" + strArray.join(` ${queryObj.operator || "AND"} `) + ")";
|
||||
}
|
||||
else if (typeof queryObj.value == "object") {
|
||||
const operatorStrParam = grabValue(queryObj.value);
|
||||
if (operatorStrParam === null || operatorStrParam === void 0 ? void 0 : operatorStrParam.str) {
|
||||
str = operatorStrParam.str;
|
||||
if (operatorStrParam.param) {
|
||||
sqlSearhValues.push(operatorStrParam.param);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
const valueParsed = queryObj.value
|
||||
? String(queryObj.value)
|
||||
|
||||
5
dist/package-shared/types/index.d.ts
vendored
5
dist/package-shared/types/index.d.ts
vendored
@ -900,11 +900,14 @@ export type TableSelectFieldsObject<T extends {
|
||||
export type ServerQueryValuesObject = {
|
||||
value?: string | number;
|
||||
equality?: (typeof ServerQueryEqualities)[number];
|
||||
tableName?: string;
|
||||
fieldName?: string;
|
||||
};
|
||||
export type ServerQueryObjectValue = string | (string | ServerQueryValuesObject | undefined | null) | (string | ServerQueryValuesObject | undefined | null)[];
|
||||
export type ServerQueryObject<T extends object = {
|
||||
[key: string]: any;
|
||||
}, K extends string = string> = {
|
||||
value?: string | (string | ServerQueryValuesObject | undefined | null)[];
|
||||
value?: ServerQueryObjectValue;
|
||||
nullValue?: boolean;
|
||||
notNullValue?: boolean;
|
||||
operator?: (typeof ServerQueryOperators)[number];
|
||||
|
||||
@ -11,6 +11,7 @@ type Params = {
|
||||
},
|
||||
string
|
||||
>;
|
||||
isValueFieldValue?: boolean;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
@ -27,6 +28,7 @@ export default function sqlGenOperatorGen({
|
||||
value,
|
||||
equality,
|
||||
queryObj,
|
||||
isValueFieldValue,
|
||||
}: Params): Return {
|
||||
if (queryObj.nullValue) {
|
||||
return { str: `${fieldName} IS NULL` };
|
||||
@ -37,80 +39,85 @@ export default function sqlGenOperatorGen({
|
||||
}
|
||||
|
||||
if (value) {
|
||||
const finalValue = isValueFieldValue ? value : "?";
|
||||
const finalParams = isValueFieldValue ? undefined : value;
|
||||
|
||||
if (equality == "MATCH") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(? IN NATURAL LANGUAGE MODE)`,
|
||||
param: value,
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN NATURAL LANGUAGE MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "MATCH_BOOLEAN") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(? IN BOOLEAN MODE)`,
|
||||
param: value,
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN BOOLEAN MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(?)`,
|
||||
param: `%${value}%`,
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
} else if (equality == "LIKE_LOWER_RAW") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(?)`,
|
||||
param: value,
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "LIKE") {
|
||||
return {
|
||||
str: `${fieldName} LIKE ?`,
|
||||
param: `%${value}%`,
|
||||
str: `${fieldName} LIKE ${finalValue}`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
} else if (equality == "LIKE_RAW") {
|
||||
return {
|
||||
str: `${fieldName} LIKE ?`,
|
||||
param: value,
|
||||
str: `${fieldName} LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT_LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(?)`,
|
||||
param: `%${value}%`,
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
} else if (equality == "NOT_LIKE_LOWER_RAW") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(?)`,
|
||||
param: value,
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT LIKE") {
|
||||
return {
|
||||
str: `${fieldName} NOT LIKE ?`,
|
||||
param: value,
|
||||
str: `${fieldName} NOT LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT LIKE_RAW") {
|
||||
return {
|
||||
str: `${fieldName} NOT LIKE ?`,
|
||||
param: value,
|
||||
str: `${fieldName} NOT LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "REGEXP") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) REGEXP LOWER(?)`,
|
||||
param: value,
|
||||
str: `LOWER(${fieldName}) REGEXP LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "FULLTEXT") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(? IN BOOLEAN MODE)`,
|
||||
param: value,
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN BOOLEAN MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT EQUAL") {
|
||||
return {
|
||||
str: `${fieldName} != ?`,
|
||||
param: value,
|
||||
str: `${fieldName} != ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality) {
|
||||
return {
|
||||
str: `${fieldName} ${sqlEqualityParser(equality)} ?`,
|
||||
param: value,
|
||||
str: `${fieldName} ${sqlEqualityParser(
|
||||
equality
|
||||
)} ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
str: `${fieldName} = ?`,
|
||||
param: value,
|
||||
str: `${fieldName} = ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -5,6 +5,7 @@ import {
|
||||
ServerQueryParamsJoin,
|
||||
ServerQueryParamsJoinMatchObject,
|
||||
ServerQueryQueryObject,
|
||||
ServerQueryValuesObject,
|
||||
} from "../../../types";
|
||||
import sqlGenOperatorGen from "./sql-gen-operator-gen";
|
||||
|
||||
@ -59,31 +60,47 @@ export default function sqlGenerator<
|
||||
|
||||
let str = `${finalFieldName}=?`;
|
||||
|
||||
function grabValue(val?: string | ServerQueryValuesObject | null) {
|
||||
const valueParsed = val;
|
||||
|
||||
if (!valueParsed) return;
|
||||
|
||||
const valueString =
|
||||
typeof valueParsed == "string"
|
||||
? valueParsed
|
||||
: valueParsed
|
||||
? valueParsed.fieldName && valueParsed.tableName
|
||||
? `${valueParsed.tableName}.${valueParsed.fieldName}`
|
||||
: valueParsed.value?.toString()
|
||||
: undefined;
|
||||
|
||||
const valueEquality =
|
||||
typeof valueParsed == "object"
|
||||
? valueParsed.equality || queryObj.equality
|
||||
: queryObj.equality;
|
||||
|
||||
const operatorStrParam = sqlGenOperatorGen({
|
||||
queryObj,
|
||||
equality: valueEquality,
|
||||
fieldName: finalFieldName || "",
|
||||
value: valueString?.toString() || "",
|
||||
isValueFieldValue: Boolean(
|
||||
typeof valueParsed == "object" &&
|
||||
valueParsed.fieldName &&
|
||||
valueParsed.tableName
|
||||
),
|
||||
});
|
||||
|
||||
return operatorStrParam;
|
||||
}
|
||||
|
||||
if (Array.isArray(queryObj.value)) {
|
||||
const strArray: string[] = [];
|
||||
|
||||
queryObj.value.forEach((val) => {
|
||||
const valueParsed = val;
|
||||
const operatorStrParam = grabValue(val);
|
||||
|
||||
if (!valueParsed) return;
|
||||
|
||||
const valueString =
|
||||
typeof valueParsed == "string"
|
||||
? valueParsed
|
||||
: valueParsed
|
||||
? valueParsed.value?.toString()
|
||||
: undefined;
|
||||
|
||||
const valueEquality =
|
||||
typeof valueParsed == "object"
|
||||
? valueParsed.equality || queryObj.equality
|
||||
: queryObj.equality;
|
||||
|
||||
const operatorStrParam = sqlGenOperatorGen({
|
||||
queryObj,
|
||||
equality: valueEquality,
|
||||
fieldName: finalFieldName || "",
|
||||
value: valueString?.toString() || "",
|
||||
});
|
||||
if (!operatorStrParam) return;
|
||||
|
||||
if (operatorStrParam.str && operatorStrParam.param) {
|
||||
strArray.push(operatorStrParam.str);
|
||||
@ -94,6 +111,14 @@ export default function sqlGenerator<
|
||||
});
|
||||
|
||||
str = "(" + strArray.join(` ${queryObj.operator || "AND"} `) + ")";
|
||||
} else if (typeof queryObj.value == "object") {
|
||||
const operatorStrParam = grabValue(queryObj.value);
|
||||
if (operatorStrParam?.str) {
|
||||
str = operatorStrParam.str;
|
||||
if (operatorStrParam.param) {
|
||||
sqlSearhValues.push(operatorStrParam.param);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const valueParsed = queryObj.value
|
||||
? String(queryObj.value)
|
||||
|
||||
@ -1096,13 +1096,20 @@ export type TableSelectFieldsObject<
|
||||
export type ServerQueryValuesObject = {
|
||||
value?: string | number;
|
||||
equality?: (typeof ServerQueryEqualities)[number];
|
||||
tableName?: string;
|
||||
fieldName?: string;
|
||||
};
|
||||
|
||||
export type ServerQueryObjectValue =
|
||||
| string
|
||||
| (string | ServerQueryValuesObject | undefined | null)
|
||||
| (string | ServerQueryValuesObject | undefined | null)[];
|
||||
|
||||
export type ServerQueryObject<
|
||||
T extends object = { [key: string]: any },
|
||||
K extends string = string
|
||||
> = {
|
||||
value?: string | (string | ServerQueryValuesObject | undefined | null)[];
|
||||
value?: ServerQueryObjectValue;
|
||||
nullValue?: boolean;
|
||||
notNullValue?: boolean;
|
||||
operator?: (typeof ServerQueryOperators)[number];
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "5.7.21",
|
||||
"version": "5.7.22",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user