UNPKG

@react-awesome-query-builder/core

Version:
76 lines (74 loc) 2.71 kB
import SqlStringOrig from "sqlstring"; // @deprecated Use dedicated utils instead export { mongoEmptyValue } from "./mongoUtils"; export { spelEscape, spelFixList, spelFormatConcat, spelImportConcat } from "./spelUtils"; // todo: move to sqlUtils export var SqlString = SqlStringOrig; SqlString.trim = function (val) { if ((val === null || val === void 0 ? void 0 : val.charAt(0)) == "'") return val.substring(1, val.length - 1);else return val; }; SqlString.unescapeLike = function (val) { var sqlDialect = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; if (typeof val !== "string") { return val; } var res = val; // unescape % and _ if (sqlDialect === "BigQuery") { // https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator res = res.replace(/\\\\([%_])/g, "$1"); } else { res = res.replace(/\\([%_])/g, "$1"); } return res; }; SqlString.escapeLike = function (val) { var any_start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var any_end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var sqlDialect = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; if (typeof val !== "string") { return val; } // normal escape var res = SqlString.escape(val); // unwrap '' res = SqlString.trim(res); // escape % and _ if (sqlDialect === "BigQuery") { // https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator res = res.replace(/[%_\\]/g, "\\\\$&"); } else { res = res.replace(/[%_]/g, "\\$&"); } // wrap with % for LIKE res = (any_start ? "%" : "") + res + (any_end ? "%" : ""); // wrap '' res = "'" + res + "'"; return res; }; export var sqlEmptyValue = function sqlEmptyValue(fieldDef) { var v = "''"; var type = fieldDef === null || fieldDef === void 0 ? void 0 : fieldDef.type; if (type == "date") { //todo: support other SQL dialects? 0001-01-01 for oracle, 1970-01-01 for timestamp v = "'0000-00-00'"; } else if (type == "datetime") { v = "'0000-00-00 00:00'"; } else if (type == "time") { v = "'00:00'"; } else if (type == "number") { v = "0"; } return v; }; export var stringifyForDisplay = function stringifyForDisplay(v) { return v == null ? "NULL" : v.toString(); }; export var wrapWithBrackets = function wrapWithBrackets(v) { if (v == undefined) return v; if ((v === null || v === void 0 ? void 0 : v[0]) === "(" && (v === null || v === void 0 ? void 0 : v[(v === null || v === void 0 ? void 0 : v.length) - 1]) === ")") { // already wrapped return v; } return "(" + v + ")"; };