@minatojs/sql-utils
Advanced tools
Comparing version 2.0.1 to 3.0.0
import { Dict } from 'cosmokit'; | ||
import { Eval, Field, Model, Modifier, Query, Selection } from '@minatojs/core'; | ||
export * from './utils'; | ||
export declare function escapeId(value: string): string; | ||
export type QueryOperators = { | ||
@@ -20,2 +20,3 @@ [K in keyof Query.FieldExpr]?: (key: string, value: NonNullable<Query.FieldExpr[K]>) => string; | ||
tables: Dict<Model>; | ||
protected escapeMap: {}; | ||
protected types: Dict<Transformer>; | ||
@@ -25,2 +26,3 @@ protected createEqualQuery: (key: string, value: any) => string; | ||
protected evalOperators: EvalOperators; | ||
private escapeRegExp?; | ||
constructor(tables: Dict<Model>); | ||
@@ -49,2 +51,3 @@ protected createNullQuery(key: string, value: boolean): string; | ||
stringify(value: any, field?: Field): any; | ||
quote(value: string): string; | ||
} |
@@ -25,22 +25,6 @@ "use strict"; | ||
Builder: () => Builder, | ||
escape: () => escape, | ||
escapeId: () => escapeId, | ||
quote: () => quote | ||
escapeId: () => escapeId | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_cosmokit2 = require("cosmokit"); | ||
// minato/packages/sql-utils/src/utils.ts | ||
var import_cosmokit = require("cosmokit"); | ||
var ESCAPE_CHARS_MAP = { | ||
"\0": "\\0", | ||
"\b": "\\b", | ||
" ": "\\t", | ||
"\n": "\\n", | ||
"\r": "\\r", | ||
"": "\\Z", | ||
"'": "\\'", | ||
"\\": "\\\\" | ||
}; | ||
var ESCAPE_CHARS_REGEXP = new RegExp(`[${Object.values(ESCAPE_CHARS_MAP).join("")}]`, "g"); | ||
function escapeId(value) { | ||
@@ -50,38 +34,6 @@ return "`" + value + "`"; | ||
__name(escapeId, "escapeId"); | ||
function escape(value) { | ||
if ((0, import_cosmokit.isNullable)(value)) | ||
return "NULL"; | ||
switch (typeof value) { | ||
case "boolean": | ||
case "number": | ||
return value + ""; | ||
case "object": | ||
return quote(JSON.stringify(value)); | ||
default: | ||
return quote(value); | ||
} | ||
} | ||
__name(escape, "escape"); | ||
function quote(value) { | ||
let chunkIndex = ESCAPE_CHARS_REGEXP.lastIndex = 0; | ||
let escapedVal = ""; | ||
let match; | ||
while (match = ESCAPE_CHARS_REGEXP.exec(value)) { | ||
escapedVal += value.slice(chunkIndex, match.index) + ESCAPE_CHARS_MAP[match[0]]; | ||
chunkIndex = ESCAPE_CHARS_REGEXP.lastIndex; | ||
} | ||
if (chunkIndex === 0) { | ||
return "'" + value + "'"; | ||
} | ||
if (chunkIndex < value.length) { | ||
return "'" + escapedVal + value.slice(chunkIndex) + "'"; | ||
} | ||
return "'" + escapedVal + "'"; | ||
} | ||
__name(quote, "quote"); | ||
// minato/packages/sql-utils/src/index.ts | ||
var Builder = class { | ||
constructor(tables) { | ||
this.tables = tables; | ||
this.escapeMap = {}; | ||
this.types = {}; | ||
@@ -103,3 +55,3 @@ this.createEqualQuery = this.comparator("="); | ||
$regex: (key, value) => this.createRegExpQuery(key, value), | ||
$regexFor: (key, value) => `${this.escape(value)} REGEXP ${key}`, | ||
$regexFor: (key, value) => `${this.escape(value)} regexp ${key}`, | ||
$bitsAllSet: (key, value) => `${key} & ${this.escape(value)} = ${this.escape(value)}`, | ||
@@ -198,3 +150,3 @@ $bitsAllClear: (key, value) => `${key} & ${this.escape(value)} = 0`, | ||
conditions.push(this.createEqualQuery(key, query)); | ||
} else if ((0, import_cosmokit2.isNullable)(query)) { | ||
} else if ((0, import_cosmokit.isNullable)(query)) { | ||
conditions.push(this.createNullQuery(key, false)); | ||
@@ -332,3 +284,14 @@ } else { | ||
escape(value, field) { | ||
return escape(this.stringify(value, field)); | ||
value = this.stringify(value, field); | ||
if ((0, import_cosmokit.isNullable)(value)) | ||
return "NULL"; | ||
switch (typeof value) { | ||
case "boolean": | ||
case "number": | ||
return value + ""; | ||
case "object": | ||
return this.quote(JSON.stringify(value)); | ||
default: | ||
return this.quote(value); | ||
} | ||
} | ||
@@ -339,2 +302,20 @@ stringify(value, field) { | ||
} | ||
quote(value) { | ||
var _a; | ||
(_a = this.escapeRegExp) != null ? _a : this.escapeRegExp = new RegExp(`[${Object.values(this.escapeMap).join("")}]`, "g"); | ||
let chunkIndex = this.escapeRegExp.lastIndex = 0; | ||
let escapedVal = ""; | ||
let match; | ||
while (match = this.escapeRegExp.exec(value)) { | ||
escapedVal += value.slice(chunkIndex, match.index) + this.escapeMap[match[0]]; | ||
chunkIndex = this.escapeRegExp.lastIndex; | ||
} | ||
if (chunkIndex === 0) { | ||
return "'" + value + "'"; | ||
} | ||
if (chunkIndex < value.length) { | ||
return "'" + escapedVal + value.slice(chunkIndex) + "'"; | ||
} | ||
return "'" + escapedVal + "'"; | ||
} | ||
}; | ||
@@ -345,6 +326,4 @@ __name(Builder, "Builder"); | ||
Builder, | ||
escape, | ||
escapeId, | ||
quote | ||
escapeId | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@minatojs/sql-utils", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "SQL Utilities for Minato", | ||
@@ -32,7 +32,7 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.0.2" | ||
"@minatojs/core": "^2.1.1" | ||
}, | ||
"dependencies": { | ||
"cosmokit": "^1.3.4" | ||
"cosmokit": "^1.4.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
35499
374
Updatedcosmokit@^1.4.0