squel 6.2.0 → 6.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +132 -0
- package/dist/browser/squel.min.js +1 -1
- package/dist/cjs/index.js +599 -85
- package/dist/cjs/index.js.map +6 -6
- package/dist/esm/index.js +599 -85
- package/dist/esm/index.js.map +6 -6
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/types.d.ts +58 -1
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [6.3.0](https://github.com/hiddentao/squel/compare/v6.2.1...v6.3.0) (2026-06-13)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **mssql:** apply/cross_apply/outer_apply to mssql flavour ([#409](https://github.com/hiddentao/squel/issues/409)) ([25afd5e](https://github.com/hiddentao/squel/commit/25afd5eb045958d288f2376e1bf4c0292431b0f4))
|
|
11
|
+
|
|
12
|
+
## [6.2.1](https://github.com/hiddentao/squel/compare/v6.2.0...v6.2.1) (2026-06-13)
|
|
13
|
+
|
|
5
14
|
## [6.2.0](https://github.com/hiddentao/squel/compare/v6.1.0...v6.2.0) (2026-06-13)
|
|
6
15
|
|
|
7
16
|
|
package/README.md
CHANGED
|
@@ -16,6 +16,10 @@ Full guide and API documentation at [https://hiddentao.github.io/squel](https://
|
|
|
16
16
|
- Written in TypeScript with first-class type definitions.
|
|
17
17
|
- Works in Node.js and the browser.
|
|
18
18
|
- Supports the standard SQL queries: `SELECT`, `UPDATE`, `INSERT`, `DELETE`.
|
|
19
|
+
- Supports dialect-specific modern SQL: Common Table Expressions (CTEs), Recursive CTEs, Postgres/MySQL UPSERT, MS SQL Server `MERGE`.
|
|
20
|
+
- Supports Window Functions via the fluent `OVER` clause builder.
|
|
21
|
+
- Supports dialect-aware JSON value extraction via `jsonExtract`.
|
|
22
|
+
- Supports Postgres/SQLite `RETURNING` and SQL Server `OUTPUT` clauses.
|
|
19
23
|
- Supports non-standard commands for MySQL, PostgreSQL, and Microsoft SQL Server.
|
|
20
24
|
- Supports parameterized queries for safe value escaping.
|
|
21
25
|
- Extensible — build any custom query or command you need.
|
|
@@ -292,6 +296,134 @@ squel.pragma().compress(9).toParam()
|
|
|
292
296
|
// { text: 'PRAGMA COMPRESS ?', values: [9] }
|
|
293
297
|
```
|
|
294
298
|
|
|
299
|
+
### Modern SQL Features
|
|
300
|
+
|
|
301
|
+
Squel includes first-class support for advanced SQL query patterns across dialects.
|
|
302
|
+
|
|
303
|
+
#### Common Table Expressions (CTE) & Recursive CTEs
|
|
304
|
+
|
|
305
|
+
```javascript
|
|
306
|
+
const subQuery = squel.select().from("users").where("active = ?", true);
|
|
307
|
+
|
|
308
|
+
squel.select()
|
|
309
|
+
.with("active_users", subQuery)
|
|
310
|
+
.from("active_users")
|
|
311
|
+
.toString()
|
|
312
|
+
// WITH active_users AS (SELECT * FROM users WHERE (active = TRUE)) SELECT * FROM active_users
|
|
313
|
+
|
|
314
|
+
// Recursive CTE (dialects automatically format, e.g. SQL Server omits RECURSIVE)
|
|
315
|
+
squel.select()
|
|
316
|
+
.withRecursive("employee_tree", squel.select().from("employees"))
|
|
317
|
+
.from("employee_tree")
|
|
318
|
+
.toString()
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
#### Window Functions (OVER clause)
|
|
322
|
+
|
|
323
|
+
```javascript
|
|
324
|
+
squel.select()
|
|
325
|
+
.from("employees")
|
|
326
|
+
.field("name")
|
|
327
|
+
.field(
|
|
328
|
+
squel.over("AVG(salary)")
|
|
329
|
+
.partitionBy("department")
|
|
330
|
+
.orderBy("hire_date", false), // DESC
|
|
331
|
+
"avg_dept_salary"
|
|
332
|
+
)
|
|
333
|
+
.toString()
|
|
334
|
+
// SELECT name, AVG(salary) OVER (PARTITION BY department ORDER BY hire_date DESC) AS "avg_dept_salary" FROM employees
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
#### JSON extraction helper (`jsonExtract`)
|
|
338
|
+
|
|
339
|
+
Provides dialect-aware JSON field path extraction:
|
|
340
|
+
|
|
341
|
+
```javascript
|
|
342
|
+
squel.select()
|
|
343
|
+
.from("users")
|
|
344
|
+
.where(squel.jsonExtract("profile", "$.name") + " = ?", "John")
|
|
345
|
+
.toString()
|
|
346
|
+
// Default/MySQL: SELECT * FROM users WHERE (json_extract(profile, '$.name') = 'John')
|
|
347
|
+
// Postgres: SELECT * FROM users WHERE (profile->>'name' = 'John')
|
|
348
|
+
// SQL Server: SELECT * FROM users WHERE (JSON_VALUE(profile, '$.name') = 'John')
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
#### Upsert/MERGE
|
|
352
|
+
|
|
353
|
+
Fluent Postgres/MySQL UPSERT and SQL Server MERGE support:
|
|
354
|
+
|
|
355
|
+
```javascript
|
|
356
|
+
// Postgres UPSERT
|
|
357
|
+
squel.useFlavour("postgres")
|
|
358
|
+
.insert()
|
|
359
|
+
.into("users")
|
|
360
|
+
.set("email", "john@example.com")
|
|
361
|
+
.onConflict("email")
|
|
362
|
+
.doUpdate()
|
|
363
|
+
.set("updated_at", new Date())
|
|
364
|
+
.toString()
|
|
365
|
+
|
|
366
|
+
// MySQL ON DUPLICATE KEY UPDATE
|
|
367
|
+
squel.useFlavour("mysql")
|
|
368
|
+
.insert()
|
|
369
|
+
.into("users")
|
|
370
|
+
.set("email", "john@example.com")
|
|
371
|
+
.onDuplicateKeyUpdate()
|
|
372
|
+
.set("updated_at", new Date())
|
|
373
|
+
.toString()
|
|
374
|
+
|
|
375
|
+
// SQL Server MERGE
|
|
376
|
+
squel.useFlavour("mssql")
|
|
377
|
+
.merge()
|
|
378
|
+
.into("target_table", "t")
|
|
379
|
+
.using("source_table", "s", "t.id = s.id")
|
|
380
|
+
.whenMatched()
|
|
381
|
+
.update({ "t.val": squel.str("s.val") })
|
|
382
|
+
.whenNotMatched()
|
|
383
|
+
.insert({ id: squel.str("s.id"), val: squel.str("s.val") })
|
|
384
|
+
.toString()
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
#### RETURNING & OUTPUT
|
|
388
|
+
|
|
389
|
+
```javascript
|
|
390
|
+
// Postgres RETURNING
|
|
391
|
+
squel.useFlavour("postgres")
|
|
392
|
+
.insert()
|
|
393
|
+
.into("users")
|
|
394
|
+
.set("name", "John")
|
|
395
|
+
.returning("id")
|
|
396
|
+
.toString()
|
|
397
|
+
// INSERT INTO users (name) VALUES ('John') RETURNING id
|
|
398
|
+
|
|
399
|
+
// SQL Server OUTPUT
|
|
400
|
+
squel.useFlavour("mssql")
|
|
401
|
+
.insert()
|
|
402
|
+
.into("users")
|
|
403
|
+
.set("name", "John")
|
|
404
|
+
.output("id")
|
|
405
|
+
.toString()
|
|
406
|
+
// INSERT INTO users (name) OUTPUT INSERTED.id VALUES ('John')
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
#### CROSS/OUTER APPLY (MSSQL)
|
|
410
|
+
|
|
411
|
+
Provides `CROSS APPLY` and `OUTER APPLY` support for the MSSQL flavour:
|
|
412
|
+
|
|
413
|
+
```javascript
|
|
414
|
+
// MSSQL CROSS/OUTER APPLY
|
|
415
|
+
squel.useFlavour("mssql")
|
|
416
|
+
.select()
|
|
417
|
+
.from("table1")
|
|
418
|
+
.cross_apply("table2", "t2")
|
|
419
|
+
.outer_apply(
|
|
420
|
+
squel.select().from("bar").where("bar.id = table1.id"),
|
|
421
|
+
"s"
|
|
422
|
+
)
|
|
423
|
+
.toString()
|
|
424
|
+
// SELECT * FROM table1 CROSS APPLY table2 t2 OUTER APPLY (SELECT * FROM bar WHERE (bar.id = table1.id)) s
|
|
425
|
+
```
|
|
426
|
+
|
|
295
427
|
## Non-standard SQL flavours
|
|
296
428
|
|
|
297
429
|
Squel supports the standard SQL commands and reserved words. A number of database engines provide their own non-standard commands; Squel makes it easy to load different "flavours" of SQL that augment the core builders with engine-specific features.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var{defineProperty:b,getOwnPropertyNames:V,getOwnPropertyDescriptor:$}=Object,z=Object.prototype.hasOwnProperty;function A(c){return this[c]}var q=(c)=>{var t=(O??=new WeakMap).get(c),s;if(t)return t;if(t=b({},"__esModule",{value:!0}),c&&typeof c==="object"||typeof c==="function"){for(var r of V(c))if(!z.call(t,r))b(t,r,{get:A.bind(c,r),enumerable:!(s=$(c,r))||s.enumerable})}return O.set(c,t),t},O;var C=(c)=>c;function D(c,t){this[c]=C.bind(null,t)}var I=(c,t)=>{for(var s in t)b(c,s,{get:t[s],enumerable:!0,configurable:!0,set:D.bind(t,s)})};var j={};I(j,{squel:()=>S,default:()=>H});var P={name:"squel",version:"6.2.0",description:"SQL query string builder",keywords:["sql","database","rdbms","query-builder","mysql","postgres","mssql"],author:"Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)",contributors:["Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)","Sergej Brjuchanov <serges@seznam.cz>"],license:"MIT",repository:{type:"git",url:"https://github.com/hiddentao/squel.git"},homepage:"https://github.com/hiddentao/squel",bugs:{url:"https://github.com/hiddentao/squel/issues"},type:"module",main:"./dist/cjs/index.js",module:"./dist/esm/index.js",types:"./dist/types/index.d.ts",exports:{".":{types:"./dist/types/index.d.ts",import:"./dist/esm/index.js",require:"./dist/cjs/index.js"},"./browser":"./dist/browser/squel.min.js"},files:["dist","README.md","LICENSE.md","CHANGELOG.md"],engines:{node:">=18.0.0"},publishConfig:{access:"public",provenance:!0},scripts:{build:"bun run scripts/build.ts",test:"bun test","test:coverage":"bun test --coverage --coverage-reporter=lcov","test:watch":"bun test --watch",lint:"biome check --write ./src ./tests ./scripts",format:"biome format --write ./src ./tests ./scripts",check:"biome check ./src ./tests ./scripts",typecheck:"tsc --noEmit",prepare:"husky",prepublishOnly:"bun run build && bun run test"},dependencies:{},devDependencies:{"@biomejs/biome":"^1.9.4","@commitlint/cli":"^19.8.1","@commitlint/config-conventional":"^19.8.1","@types/bun":"latest","@types/node":"^22.0.0","commit-and-tag-version":"^12.6.0",husky:"^9.1.7",typescript:"^5.3.0"}};function p(c,t){return c.length?c+t:c}function y(c,...t){if(c&&t){for(let s of t)if(s&&typeof s==="object")for(let r of Object.getOwnPropertyNames(s))c[r]=s[r]}return c}function x(c){return!!c&&c.constructor.prototype===Object.prototype}function B(c){return!!c&&c.constructor.prototype===Array.prototype}function E(c){if(!c)return c;let t=c;if(typeof t.clone==="function")return t.clone();if(x(c)||B(c)){let s=new t.constructor;for(let r of Object.getOwnPropertyNames(c))if(typeof t[r]!=="function")s[r]=E(t[r]);return s}return JSON.parse(JSON.stringify(c))}function v(c,t,s){let r=typeof t;if(r!=="function"&&r!=="string")throw Error("type must be a class constructor or string");if(typeof s!=="function")throw Error("handler must be a function");for(let e of c)if(e.type===t){e.handler=s;return}c.push({type:t,handler:s})}function T(c,t,s){return F(c,t)||F(c,s)}function F(c,t){for(let s of t)if(typeof c===s.type||typeof s.type!=="string"&&c instanceof s.type)return s.handler;return}function N(c=null){let t={isSquelBuilder(e){return!!e&&!!e._toParamString}},s=(e)=>{return!t.isSquelBuilder(e)||!e.options.rawNesting};t.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" ",stringFormatter:null,rawNesting:!1},t.globalValueHandlers=[],t.registerValueHandler=(e,n)=>{v(t.globalValueHandlers,e,n)},t.Cloneable=class{clone(){let e=new this.constructor;return y(e,E(y({},this)))}},t.BaseBuilder=class extends t.Cloneable{options;constructor(e){super();let n=JSON.parse(JSON.stringify(t.DefaultQueryBuilderOptions));n.stringFormatter=t.DefaultQueryBuilderOptions.stringFormatter,this.options=y(n,e)}registerValueHandler(e,n){return v(this.options.valueHandlers,e,n),this}_sanitizeExpression(e){if(!t.isSquelBuilder(e)){if(typeof e!=="string")throw Error("expression must be a string or builder instance")}return e}_sanitizeName(e,n){if(typeof e!=="string")throw Error(`${n} must be a string`);return e}_sanitizeField(e){if(!t.isSquelBuilder(e))return this._sanitizeName(e,"field name");return e}_sanitizeBaseBuilder(e){if(t.isSquelBuilder(e))return e;throw Error("must be a builder instance")}_sanitizeTable(e,n){if(typeof e!=="string")try{return this._sanitizeBaseBuilder(e)}catch(i){throw Error("table name must be a string or a builder")}return this._sanitizeName(e,"table")}_sanitizeTableAlias(e){return this._sanitizeName(e,"table alias")}_sanitizeFieldAlias(e){return this._sanitizeName(e,"field alias")}_sanitizeLimitOffset(e){let n=Number.parseInt(e,10);if(n<0||Number.isNaN(n))throw Error("limit/offset must be >= 0");return n}_sanitizeValue(e){let n=typeof e;if(e===null);else if(n==="string"||n==="number"||n==="boolean");else if(t.isSquelBuilder(e));else if(!T(e,this.options.valueHandlers,t.globalValueHandlers))throw Error("field value must be a string, number, boolean, null or one of the registered custom value types");return e}_escapeValue(e){return this.options.replaceSingleQuotes&&e?e.replace(/'/g,this.options.singleQuoteReplacement):e}_formatTableName(e){if(this.options.autoQuoteTableNames){let n=this.options.nameQuoteCharacter;e=`${n}${e}${n}`}return e}_formatFieldAlias(e){if(this.options.autoQuoteAliasNames){let n=this.options.fieldAliasQuoteCharacter;e=`${n}${e}${n}`}return e}_formatTableAlias(e){if(this.options.autoQuoteAliasNames){let n=this.options.tableAliasQuoteCharacter;e=`${n}${e}${n}`}return this.options.useAsForTableAliasNames?`AS ${e}`:e}_formatFieldName(e,n={}){if(this.options.autoQuoteFieldNames){let i=this.options.nameQuoteCharacter;if(n.ignorePeriodsForFieldNameQuotes)e=`${i}${e}${i}`;else e=e.split(".").map((a)=>a==="*"?a:`${i}${a}${i}`).join(".")}return e}_formatCustomValue(e,n,i){let a=T(e,this.options.valueHandlers,t.globalValueHandlers);if(a){if(e=a(e,n,i),e?.rawNesting)return{formatted:!0,rawNesting:!0,value:e.value}}return{formatted:!!a,value:e}}_formatValueForParamArray(e,n={}){if(B(e))return e.map((i)=>this._formatValueForParamArray(i,n));return this._formatCustomValue(e,!0,n).value}_formatValueForQueryString(e,n={}){let{rawNesting:i,formatted:a,value:o}=this._formatCustomValue(e,!1,n),l=o;if(a){if(i)return l;return this._applyNestingFormatting(l,s(e))}if(B(l))l=l.map((u)=>this._formatValueForQueryString(u)),l=this._applyNestingFormatting(l.join(", "),s(l));else{let u=typeof l;if(l===null)l="NULL";else if(u==="boolean")l=l?"TRUE":"FALSE";else if(t.isSquelBuilder(l))l=this._applyNestingFormatting(l.toString(),s(l));else if(u!=="number"){if(u==="string"&&this.options.stringFormatter)return this.options.stringFormatter(l);if(n.dontQuote)l=`${l}`;else l=`'${this._escapeValue(l)}'`}}return l}_applyNestingFormatting(e,n=!0){if(e&&typeof e==="string"&&n&&!this.options.rawNesting){let i=e.startsWith("(")&&e.endsWith(")");if(i){let a=0,o=1;while(e.length-1>++a){let l=e.charAt(a);if(l==="(")o++;else if(l===")"){if(o--,o<1){i=!1;break}}}}if(!i)e=`(${e})`}return e}_buildString(e,n,i={}){let{nested:a,buildParameterized:o,formattingOptions:l}=i;n=n||[],e=e||"";let u="",d=-1,h=[],f=this.options.parameterCharacter,_=0;while(e.length>_)if(e.substring(_,_+f.length)===f){let g=n[++d];if(o)if(t.isSquelBuilder(g)){let w=g._toParamString({buildParameterized:o,nested:!0});u+=w.text,w.values.forEach((k)=>h.push(k))}else if(g=this._formatValueForParamArray(g,l),B(g)){let w=g.map(()=>f).join(", ");u+=`(${w})`,g.forEach((k)=>h.push(k))}else u+=f,h.push(g);else u+=this._formatValueForQueryString(g,l);_+=f.length}else u+=e.charAt(_),_++;return{text:this._applyNestingFormatting(u,!!a),values:h}}_buildManyStrings(e,n,i={}){let a=[],o=[];for(let u=0;e.length>u;++u){let d=e[u],h=n[u],{text:f,values:_}=this._buildString(d,h,{buildParameterized:i.buildParameterized,nested:!1});a.push(f),_.forEach((g)=>o.push(g))}let l=a.join(this.options.separator);return{text:l.length?this._applyNestingFormatting(l,!!i.nested):"",values:o}}_toParamString(e){throw Error("Not yet implemented")}toString(e={}){return this._toParamString(e).text}toParam(e={}){return this._toParamString({...e,buildParameterized:!0})}},t.Expression=class extends t.BaseBuilder{_nodes;constructor(e){super(e);this._nodes=[]}and(e,...n){return e=this._sanitizeExpression(e),this._nodes.push({type:"AND",expr:e,para:n}),this}or(e,...n){return e=this._sanitizeExpression(e),this._nodes.push({type:"OR",expr:e,para:n}),this}_toParamString(e={}){let n=[],i=[];for(let a of this._nodes){let{type:o,expr:l,para:u}=a,{text:d,values:h}=t.isSquelBuilder(l)?l._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(l,u,{buildParameterized:e.buildParameterized});if(n.length)n.push(o);n.push(d),h.forEach((f)=>i.push(f))}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:i}}},t.Case=class extends t.BaseBuilder{_fieldName=null;_cases;_elseValue=null;constructor(e,n={}){super(n);let i=null;if(x(e))n=e;else if(e)i=this._sanitizeField(e);this._fieldName=i,this.options=y({...t.DefaultQueryBuilderOptions},n),this._cases=[]}when(e,...n){return this._cases.unshift({expression:e,values:n||[]}),this}then(e){if(this._cases.length===0)throw Error("when() needs to be called first");return this._cases[0].result=e,this}else(e){return this._elseValue=e,this}_toParamString(e={}){let n="",i=[];for(let{expression:a,values:o,result:l}of this._cases){n=p(n," ");let u=this._buildString(a,o,{buildParameterized:e.buildParameterized,nested:!0});n+=`WHEN ${u.text} THEN ${this._formatValueForQueryString(l)}`,u.values.forEach((d)=>i.push(d))}if(n.length){if(n+=` ELSE ${this._formatValueForQueryString(this._elseValue)} END`,this._fieldName)n=`${this._fieldName} ${n}`;n=`CASE ${n}`}else n=this._formatValueForQueryString(this._elseValue);return{text:n,values:i}}},t.Block=class extends t.BaseBuilder{constructor(e){super(e)}exposedMethods(){let e={},n=this;while(n){for(let i of Object.getOwnPropertyNames(n))if(i!=="constructor"&&typeof n[i]==="function"&&!i.startsWith("_")&&!t.Block.prototype[i])e[i]=n[i];n=Object.getPrototypeOf(n)}return e}},t.StringBlock=class extends t.Block{_str;constructor(e,n){super(e);this._str=n}_toParamString(e={}){return{text:this._str,values:[]}}},t.FunctionBlock=class extends t.Block{_strings;_values;constructor(e){super(e);this._strings=[],this._values=[]}function(e,...n){this._strings.push(e),this._values.push(n)}_toParamString(e={}){return this._buildManyStrings(this._strings,this._values,e)}},t.registerValueHandler(t.FunctionBlock,(e,n=!1)=>{return n?e.toParam():e.toString()}),t.AbstractTableBlock=class extends t.Block{_tables;constructor(e,n){super(e);this._tables=[]}_table(e,n=null){if(n=n?this._sanitizeTableAlias(n):n,e=this._sanitizeTable(e),this.options.singleTable)this._tables=[];this._tables.push({table:e,alias:n})}_hasTable(){return this._tables.length>0}_toParamString(e={}){let n="",i=[];if(this._hasTable()){for(let{table:a,alias:o}of this._tables){n=p(n,", ");let l;if(t.isSquelBuilder(a)){let{text:u,values:d}=a._toParamString({buildParameterized:e.buildParameterized,nested:!0});l=u,d.forEach((h)=>i.push(h))}else l=this._formatTableName(a);if(o)l+=` ${this._formatTableAlias(o)}`;n+=l}if(this.options.prefix)n=`${this.options.prefix} ${n}`}return{text:n,values:i}}},t.TargetTableBlock=class extends t.AbstractTableBlock{target(e){this._table(e)}},t.UpdateTableBlock=class extends t.AbstractTableBlock{table(e,n=null){this._table(e,n)}_toParamString(e={}){if(!this._hasTable())throw Error("table() needs to be called");return super._toParamString(e)}},t.FromTableBlock=class extends t.AbstractTableBlock{constructor(e){super(y({},e,{prefix:"FROM"}))}from(e,n=null){this._table(e,n)}},t.IntoTableBlock=class extends t.AbstractTableBlock{constructor(e){super(y({},e,{prefix:"INTO",singleTable:!0}))}into(e){this._table(e)}_toParamString(e={}){if(!this._hasTable())throw Error("into() needs to be called");return super._toParamString(e)}},t.GetFieldBlock=class extends t.Block{_fields;constructor(e){super(e);this._fields=[]}fields(e,n={}){if(B(e))for(let i of e)this.field(i,null,n);else for(let i in e){let a=e[i];this.field(i,a,n)}}field(e,n=null,i={}){if(n=n?this._sanitizeFieldAlias(n):n,e=this._sanitizeField(e),this._fields.filter((o)=>o.name===e&&o.alias===n).length)return this;this._fields.push({name:e,alias:n,options:i});return}_toParamString(e={}){let{queryBuilder:n,buildParameterized:i}=e,a="",o=[];for(let l of this._fields){a=p(a,", ");let{name:u,alias:d,options:h}=l;if(typeof u==="string")a+=this._formatFieldName(u,h);else{let f=u._toParamString({nested:!0,buildParameterized:i});a+=f.text,f.values.forEach((_)=>o.push(_))}if(d)a+=` AS ${this._formatFieldAlias(d)}`}if(!a.length){if(n?.getBlock(t.FromTableBlock)?._hasTable())a="*"}return{text:a,values:o}}},t.AbstractSetFieldBlock=class extends t.Block{_fields;_values;_valueOptions;constructor(e){super(e);this._fields=[],this._values=[[]],this._valueOptions=[[]]}_reset(){this._fields=[],this._values=[[]],this._valueOptions=[[]]}_set(e,n,i={}){if(this._values.length>1)throw Error("Cannot set multiple rows of fields this way.");if(typeof n<"u")n=this._sanitizeValue(n);e=this._sanitizeField(e);let a=this._fields.indexOf(e);if(a===-1)this._fields.push(e),a=this._fields.length-1;this._values[0][a]=n,this._valueOptions[0][a]=i}_setFields(e,n={}){if(typeof e!=="object")throw Error(`Expected an object but got ${typeof e}`);for(let i in e)this._set(i,e[i],n)}_setFieldsRows(e,n={}){if(!B(e))throw Error(`Expected an array of objects but got ${typeof e}`);this._reset();for(let i=0;e.length>i;++i){let a=e[i];for(let o in a){let l=a[o];o=this._sanitizeField(o),l=this._sanitizeValue(l);let u=this._fields.indexOf(o);if(i>0&&u===-1)throw Error("All fields in subsequent rows must match the fields in the first row");if(u===-1)this._fields.push(o),u=this._fields.length-1;if(!B(this._values[i]))this._values[i]=[],this._valueOptions[i]=[];this._values[i][u]=l,this._valueOptions[i][u]=n}}}},t.SetFieldBlock=class extends t.AbstractSetFieldBlock{set(e,n,i){this._set(e,n,i)}setFields(e,n){this._setFields(e,n)}_toParamString(e={}){let{buildParameterized:n}=e;if(this._fields.length<=0)throw Error("set() needs to be called");let i="",a=[];for(let o=0;o<this._fields.length;++o){i=p(i,", ");let l=this._formatFieldName(this._fields[o]),u=this._values[0][o];if(l.indexOf("=")<0)l=`${l} = ${this.options.parameterCharacter}`;let d=this._buildString(l,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][o]});i+=d.text,d.values.forEach((h)=>a.push(h))}return{text:`SET ${i}`,values:a}}},t.InsertFieldValueBlock=class extends t.AbstractSetFieldBlock{set(e,n,i={}){this._set(e,n,i)}setFields(e,n){this._setFields(e,n)}setFieldsRows(e,n){this._setFieldsRows(e,n)}_toParamString(e={}){let{buildParameterized:n}=e,i=this._fields.map((l)=>this._formatFieldName(l)).join(", "),a=[],o=[];for(let l=0;l<this._values.length;++l){a[l]="";for(let u=0;u<this._values[l].length;++u){let d=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});d.values.forEach((h)=>o.push(h)),a[l]=p(a[l],", "),a[l]+=d.text}}return{text:i.length?`(${i}) VALUES (${a.join("), (")})`:"",values:o}}},t.InsertFieldsFromQueryBlock=class extends t.Block{_fields;_query=null;constructor(e){super(e);this._fields=[]}fromQuery(e,n){this._fields=e.map((i)=>this._sanitizeField(i)),this._query=this._sanitizeBaseBuilder(n)}_toParamString(e={}){let n="",i=[];if(this._fields.length&&this._query){let{text:a,values:o}=this._query._toParamString({buildParameterized:e.buildParameterized,nested:!0});n=`(${this._fields.join(", ")}) ${this._applyNestingFormatting(a)}`,i=o}return{text:n,values:i}}},t.DistinctBlock=class extends t.Block{_useDistinct=!1;distinct(){this._useDistinct=!0}_toParamString(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}},t.GroupByBlock=class extends t.Block{_groups;constructor(e){super(e);this._groups=[]}group(e){this._groups.push(this._sanitizeField(e))}_toParamString(e={}){return{text:this._groups.length?`GROUP BY ${this._groups.join(", ")}`:"",values:[]}}},t.AbstractVerbSingleValueBlock=class extends t.Block{_value=null;constructor(e){super(e)}_setValue(e){this._value=e!==null?this._sanitizeLimitOffset(e):e}_toParamString(e={}){let n=this._value!==null?`${this.options.verb} ${this.options.parameterCharacter}`:"",i=this._value!==null?[this._value]:[];return this._buildString(n,i,e)}},t.OffsetBlock=class extends t.AbstractVerbSingleValueBlock{constructor(e){super(y({},e,{verb:"OFFSET"}))}offset(e){this._setValue(e)}},t.LimitBlock=class extends t.AbstractVerbSingleValueBlock{constructor(e){super(y({},e,{verb:"LIMIT"}))}limit(e){this._setValue(e)}},t.AbstractConditionBlock=class extends t.Block{_conditions;constructor(e){super(e);this._conditions=[]}_condition(e,...n){e=this._sanitizeExpression(e),this._conditions.push({expr:e,values:n||[]})}_toParamString(e={}){let n=[],i=[];for(let{expr:o,values:l}of this._conditions){let u=t.isSquelBuilder(o)?o._toParamString({buildParameterized:e.buildParameterized}):this._buildString(o,l,{buildParameterized:e.buildParameterized});if(u.text.length)n.push(u.text);u.values.forEach((d)=>i.push(d))}let a=n.join(") AND (");return{text:a.length?`${this.options.verb} (${a})`:"",values:i}}},t.WhereBlock=class extends t.AbstractConditionBlock{constructor(e){super(y({},e,{verb:"WHERE"}))}where(e,...n){this._condition(e,...n)}},t.HavingBlock=class extends t.AbstractConditionBlock{constructor(e){super(y({},e,{verb:"HAVING"}))}having(e,...n){this._condition(e,...n)}},t.OrderByBlock=class extends t.Block{_orders;constructor(e){super(e);this._orders=[]}order(e,n,...i){e=this._sanitizeField(e);let a;if(typeof n==="string")a=n;else if(n===void 0)a="ASC";else if(n===null)a=null;else a=n?"ASC":"DESC";this._orders.push({field:e,dir:a,values:i||[]})}_toParamString(e={}){let n="",i=[];for(let{field:a,dir:o,values:l}of this._orders){n=p(n,", ");let u=this._buildString(a,l,{buildParameterized:e.buildParameterized});if(n+=u.text,B(u.values))u.values.forEach((d)=>i.push(d));if(o!==null)n+=` ${o}`}return{text:n.length?`ORDER BY ${n}`:"",values:i}}},t.JoinBlock=class extends t.Block{_joins;constructor(e){super(e);this._joins=[]}join(e,n=null,i=null,a="INNER"){e=this._sanitizeTable(e,!0),n=n?this._sanitizeTableAlias(n):n,i=i?this._sanitizeExpression(i):i,this._joins.push({type:a,table:e,alias:n,condition:i})}left_join(e,n=null,i=null){this.join(e,n,i,"LEFT")}right_join(e,n=null,i=null){this.join(e,n,i,"RIGHT")}outer_join(e,n=null,i=null){this.join(e,n,i,"OUTER")}left_outer_join(e,n=null,i=null){this.join(e,n,i,"LEFT OUTER")}full_join(e,n=null,i=null){this.join(e,n,i,"FULL")}cross_join(e,n=null,i=null){this.join(e,n,i,"CROSS")}_toParamString(e={}){let n="",i=[];for(let{type:a,table:o,alias:l,condition:u}of this._joins){n=p(n,this.options.separator);let d;if(t.isSquelBuilder(o)){let h=o._toParamString({buildParameterized:e.buildParameterized,nested:!0});h.values.forEach((f)=>i.push(f)),d=h.text}else d=this._formatTableName(o);if(n+=`${a} JOIN ${d}`,l)n+=` ${this._formatTableAlias(l)}`;if(u){n+=" ON ";let h;if(t.isSquelBuilder(u))h=u._toParamString({buildParameterized:e.buildParameterized});else h=this._buildString(u,[],{buildParameterized:e.buildParameterized});n+=this._applyNestingFormatting(h.text),h.values.forEach((f)=>i.push(f))}}return{text:n,values:i}}},t.UnionBlock=class extends t.Block{_unions;constructor(e){super(e);this._unions=[]}union(e,n="UNION"){e=this._sanitizeTable(e),this._unions.push({type:n,table:e})}union_all(e){this.union(e,"UNION ALL")}_toParamString(e={}){let n="",i=[];for(let{type:a,table:o}of this._unions){n=p(n,this.options.separator);let l;if(o instanceof t.BaseBuilder){let u=o._toParamString({buildParameterized:e.buildParameterized,nested:!0});l=u.text,u.values.forEach((d)=>i.push(d))}else n=this._formatTableName(o),l="";n+=`${a} ${l}`}return{text:n,values:i}}},t.ForBlock=class extends t.Block{_forStr;constructor(e){super(e);this._forStr=null}for(e){this._forStr=e}_toParamString(){return{text:this._forStr!==null?`FOR ${this._forStr}`:"",values:[]}}},t.QueryBuilder=class extends t.BaseBuilder{blocks;constructor(e,n){super(e);this.blocks=n||[];for(let i of this.blocks){let a=i.exposedMethods();for(let o in a){let l=a[o];if(this[o]!==void 0)throw Error(`Builder already has a builder method called: ${o}`);((u,d,h)=>{this[d]=(...f)=>{return h.call(u,...f),this}})(i,o,l)}}}registerValueHandler(e,n){for(let i of this.blocks)i.registerValueHandler(e,n);return super.registerValueHandler(e,n),this}updateOptions(e){this.options=y(this.options,e);for(let n of this.blocks)n.options=y(n.options,e)}_toParamString(e={}){let n=y({},this.options,e),i=this.blocks.map((d)=>d._toParamString({buildParameterized:n.buildParameterized,queryBuilder:this})),a=i.map((d)=>d.text),o=i.map((d)=>d.values),l=a.filter((d)=>d.length>0).join(n.separator),u=[];if(o.forEach((d)=>d.forEach((h)=>u.push(h))),!n.nested){if(n.numberedParameters){let d=n.numberedParametersStartAt!==void 0?n.numberedParametersStartAt:1,h=n.parameterCharacter.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&");l=l.replace(new RegExp(h,"g"),()=>{return`${n.numberedParametersPrefix}${d++}`})}}return{text:this._applyNestingFormatting(l,!!n.nested),values:u}}clone(){let e=this.blocks.map((n)=>n.clone());return new this.constructor(this.options,e)}getBlock(e){return this.blocks.find((n)=>n instanceof e)}},t.Select=class extends t.QueryBuilder{constructor(e,n=null){n=n||[new t.StringBlock(e,"SELECT"),new t.FunctionBlock(e),new t.DistinctBlock(e),new t.GetFieldBlock(e),new t.FromTableBlock(e),new t.JoinBlock(e),new t.WhereBlock(e),new t.GroupByBlock(e),new t.HavingBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e),new t.OffsetBlock(e),new t.UnionBlock(e),new t.ForBlock(e)];super(e,n)}},t.Update=class extends t.QueryBuilder{constructor(e,n=null){n=n||[new t.StringBlock(e,"UPDATE"),new t.UpdateTableBlock(e),new t.SetFieldBlock(e),new t.WhereBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e)];super(e,n)}},t.Delete=class extends t.QueryBuilder{constructor(e,n=null){n=n||[new t.StringBlock(e,"DELETE"),new t.TargetTableBlock(e),new t.FromTableBlock(y({},e,{singleTable:!0})),new t.JoinBlock(e),new t.WhereBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e)];super(e,n)}},t.Insert=class extends t.QueryBuilder{constructor(e,n=null){n=n||[new t.StringBlock(e,"INSERT"),new t.IntoTableBlock(e),new t.InsertFieldValueBlock(e),new t.InsertFieldsFromQueryBlock(e)];super(e,n)}};let r={VERSION:P.version,flavour:c,expr:(e)=>new t.Expression(e),case:(e,n)=>new t.Case(e,n),select:(e,n)=>new t.Select(e,n),update:(e,n)=>new t.Update(e,n),insert:(e,n)=>new t.Insert(e,n),delete:(e,n)=>new t.Delete(e,n),str:(...e)=>{let n=new t.FunctionBlock;return n.function(...e),n},rstr:(...e)=>{let n=new t.FunctionBlock({rawNesting:!0});return n.function(...e),n},registerValueHandler:t.registerValueHandler};return r.remove=r.delete,r.cls=t,r}var m=N();m.flavours={};m.useFlavour=function(t=null){if(!t)return m;if(m.flavours[t]instanceof Function){let s=N(t);return m.flavours[t].call(null,s),s.flavours=m.flavours,s.useFlavour=m.useFlavour,s}throw Error(`Flavour not available: ${t}`)};var S=m;m.flavours.mysql=(c)=>{let t=c.cls;t.MysqlOnDuplicateKeyUpdateBlock=class extends t.AbstractSetFieldBlock{onDupUpdate(s,r,e){this._set(s,r,e)}_toParamString(s={}){let r="",e=[];for(let n=0;n<this._fields.length;++n){r=p(r,", ");let i=this._fields[n],a=this._values[0][n],o=this._valueOptions[0][n];if(typeof a>"u")r+=i;else{let l=this._buildString(`${i} = ${this.options.parameterCharacter}`,[a],{buildParameterized:s.buildParameterized,formattingOptions:o});r+=l.text,l.values.forEach((u)=>e.push(u))}}return{text:!r.length?"":`ON DUPLICATE KEY UPDATE ${r}`,values:e}}},t.Insert=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.StringBlock(s,"INSERT"),new t.IntoTableBlock(s),new t.InsertFieldValueBlock(s),new t.InsertFieldsFromQueryBlock(s),new t.MysqlOnDuplicateKeyUpdateBlock(s)];super(s,r)}},t.Replace=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.StringBlock(s,"REPLACE"),new t.IntoTableBlock(s),new t.InsertFieldValueBlock(s),new t.InsertFieldsFromQueryBlock(s)];super(s,r)}},c.replace=(s,r)=>new t.Replace(s,r)};m.flavours.postgres=(c)=>{let t=c.cls;t.DefaultQueryBuilderOptions.numberedParameters=!0,t.DefaultQueryBuilderOptions.numberedParametersStartAt=1,t.DefaultQueryBuilderOptions.autoQuoteAliasNames=!1,t.DefaultQueryBuilderOptions.useAsForTableAliasNames=!0,t.PostgresOnConflictKeyUpdateBlock=class extends t.AbstractSetFieldBlock{_onConflict;_dupFields;constructor(s){super(s);this._onConflict=!1,this._dupFields=[]}onConflict(s,r){if(this._onConflict=!0,!s)return;if(!B(s))s=[s];if(this._dupFields=s.map(this._sanitizeField.bind(this)),r)Object.keys(r).forEach((e)=>{this._set(e,r[e])})}_toParamString(s={}){let r="",e=[];for(let i=0;i<this._fields.length;++i){r=p(r,", ");let a=this._fields[i],o=this._values[0][i],l=this._valueOptions[0][i];if(typeof o>"u")r+=a;else{let u=this._buildString(`${a} = ${this.options.parameterCharacter}`,[o],{buildParameterized:s.buildParameterized,formattingOptions:l});r+=u.text,u.values.forEach((d)=>e.push(d))}}let n={text:"",values:e};if(this._onConflict){let i=this._dupFields.length?`(${this._dupFields.join(", ")}) `:"",a=r.length?`UPDATE SET ${r}`:"NOTHING";n.text=`ON CONFLICT ${i}DO ${a}`}return n}},t.ReturningBlock=class extends t.Block{_fields;constructor(s){super(s);this._fields=[]}returning(s,r=null,e={}){if(r=r?this._sanitizeFieldAlias(r):r,s=this._sanitizeField(s),this._fields.filter((i)=>i.name===s&&i.alias===r).length)return this;this._fields.push({name:s,alias:r,options:e})}_toParamString(s={}){let{buildParameterized:r}=s,e="",n=[];for(let i of this._fields){e=p(e,", ");let{name:a,alias:o,options:l}=i;if(typeof a==="string")e+=this._formatFieldName(a,l);else{let u=a._toParamString({nested:!0,buildParameterized:r});e+=u.text,u.values.forEach((d)=>n.push(d))}if(o)e+=` AS ${this._formatFieldAlias(o)}`}return{text:e.length>0?`RETURNING ${e}`:"",values:n}}},t.WithBlock=class extends t.Block{_tables;constructor(s){super(s);this._tables=[]}with(s,r){this._tables.push({alias:s,table:r})}_toParamString(s={}){let r=[],e=[];for(let{alias:n,table:i}of this._tables){let a=i._toParamString({buildParameterized:s.buildParameterized,nested:!0});r.push(`${n} AS ${a.text}`),a.values.forEach((o)=>e.push(o))}return{text:r.length?`WITH ${r.join(", ")}`:"",values:e}}},t.UsingBlock=class extends t.AbstractTableBlock{constructor(s){super({...s,prefix:"USING"})}using(s,r=null){this._table(s,r)}},t.DistinctOnBlock=class extends t.Block{_useDistinct=!1;_distinctFields;constructor(s){super(s);this._distinctFields=[]}distinct(...s){this._useDistinct=!0,s.forEach((r)=>{this._distinctFields.push(this._sanitizeField(r))})}_toParamString(){let s="";if(this._useDistinct){if(s="DISTINCT",this._distinctFields.length)s+=` ON (${this._distinctFields.join(", ")})`}return{text:s,values:[]}}},t.Select=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.WithBlock(s),new t.StringBlock(s,"SELECT"),new t.FunctionBlock(s),new t.DistinctOnBlock(s),new t.GetFieldBlock(s),new t.FromTableBlock(s),new t.JoinBlock(s),new t.WhereBlock(s),new t.GroupByBlock(s),new t.HavingBlock(s),new t.OrderByBlock(s),new t.LimitBlock(s),new t.OffsetBlock(s),new t.UnionBlock(s),new t.ForBlock(s)];super(s,r)}},t.Insert=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.WithBlock(s),new t.StringBlock(s,"INSERT"),new t.IntoTableBlock(s),new t.InsertFieldValueBlock(s),new t.InsertFieldsFromQueryBlock(s),new t.PostgresOnConflictKeyUpdateBlock(s),new t.ReturningBlock(s)];super(s,r)}},t.Update=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.WithBlock(s),new t.StringBlock(s,"UPDATE"),new t.UpdateTableBlock(s),new t.SetFieldBlock(s),new t.FromTableBlock(s),new t.WhereBlock(s),new t.OrderByBlock(s),new t.LimitBlock(s),new t.ReturningBlock(s)];super(s,r)}},t.Delete=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.WithBlock(s),new t.StringBlock(s,"DELETE"),new t.TargetTableBlock(s),new t.FromTableBlock({...s,singleTable:!0}),new t.UsingBlock(s),new t.JoinBlock(s),new t.WhereBlock(s),new t.OrderByBlock(s),new t.LimitBlock(s),new t.ReturningBlock(s)];super(s,r)}}};var Q=function(c){c=this._sanitizeLimitOffset(c),this._parent._limits=c};m.flavours.mssql=(c)=>{let t=c.cls;t.DefaultQueryBuilderOptions.replaceSingleQuotes=!0,t.DefaultQueryBuilderOptions.autoQuoteAliasNames=!1,t.DefaultQueryBuilderOptions.numberedParametersPrefix="@",c.registerValueHandler(Date,(s)=>{let r=s;return`'${r.getUTCFullYear()}-${r.getUTCMonth()+1}-${r.getUTCDate()} ${r.getUTCHours()}:${r.getUTCMinutes()}:${r.getUTCSeconds()}'`}),t.MssqlLimitOffsetTopBlock=class extends t.Block{_limits;_offsets;ParentBlock;LimitBlock;TopBlock;OffsetBlock;constructor(s){super(s);this._limits=null,this._offsets=null,this.ParentBlock=class extends t.Block{_parent;constructor(r){super(r.options);this._parent=r}},this.LimitBlock=class extends this.ParentBlock{limit;constructor(r){super(r);this.limit=Q}_toParamString(){let r="";if(this._parent._limits&&this._parent._offsets)r=`FETCH NEXT ${this._parent._limits} ROWS ONLY`;return{text:r,values:[]}}},this.TopBlock=class extends this.ParentBlock{top;constructor(r){super(r);this.top=Q}_toParamString(){let r="";if(this._parent._limits&&!this._parent._offsets)r=`TOP (${this._parent._limits})`;return{text:r,values:[]}}},this.OffsetBlock=class extends this.ParentBlock{offset(r){this._parent._offsets=this._sanitizeLimitOffset(r)}_toParamString(){let r="";if(this._parent._offsets)r=`OFFSET ${this._parent._offsets} ROWS`;return{text:r,values:[]}}}}LIMIT(){return new this.LimitBlock(this)}TOP(){return new this.TopBlock(this)}OFFSET(){return new this.OffsetBlock(this)}},t.MssqlUpdateTopBlock=class extends t.Block{_limits;limit;top;constructor(s){super(s);this._limits=null;let r=(e)=>{this._limits=this._sanitizeLimitOffset(e)};this.limit=r,this.top=r}_toParamString(){return{text:this._limits?`TOP (${this._limits})`:"",values:[]}}},t.MssqlInsertFieldValueBlock=class extends t.InsertFieldValueBlock{_outputs;constructor(s){super(s);this._outputs=[]}output(s){if(typeof s==="string")this._outputs.push(`INSERTED.${this._sanitizeField(s)}`);else s.forEach((r)=>{this._outputs.push(`INSERTED.${this._sanitizeField(r)}`)})}_toParamString(s){let r=super._toParamString(s);if(r.text.length&&this._outputs.length>0){let e=`OUTPUT ${this._outputs.join(", ")} `,n=r.text.indexOf("VALUES");r.text=r.text.substring(0,n)+e+r.text.substring(n)}return r}},t.MssqlUpdateDeleteOutputBlock=class extends t.Block{_outputs;constructor(s){super(s);this._outputs=[]}outputs(s){for(let r in s)this.output(r,s[r])}output(s,r=null){s=this._sanitizeField(s),r=r?this._sanitizeFieldAlias(r):r,this._outputs.push({name:this.options.forDelete?`DELETED.${s}`:`INSERTED.${s}`,alias:r})}_toParamString(s){let r="";if(this._outputs.length){for(let e of this._outputs)if(r=p(r,", "),r+=e.name,e.alias)r+=` AS ${this._formatFieldAlias(e.alias)}`;r=`OUTPUT ${r}`}return{text:r,values:[]}}},t.Select=class extends t.QueryBuilder{constructor(s,r=null){let e=new t.MssqlLimitOffsetTopBlock(s);r=r||[new t.StringBlock(s,"SELECT"),new t.DistinctBlock(s),e.TOP(),new t.GetFieldBlock(s),new t.FromTableBlock(s),new t.JoinBlock(s),new t.WhereBlock(s),new t.GroupByBlock(s),new t.HavingBlock(s),new t.OrderByBlock(s),e.OFFSET(),e.LIMIT(),new t.UnionBlock(s)];super(s,r)}},t.Update=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.StringBlock(s,"UPDATE"),new t.MssqlUpdateTopBlock(s),new t.UpdateTableBlock(s),new t.SetFieldBlock(s),new t.MssqlUpdateDeleteOutputBlock(s),new t.WhereBlock(s)];super(s,r)}},t.Delete=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.StringBlock(s,"DELETE"),new t.TargetTableBlock(s),new t.FromTableBlock(y({},s,{singleTable:!0})),new t.JoinBlock(s),new t.MssqlUpdateDeleteOutputBlock(y({},s,{forDelete:!0})),new t.WhereBlock(s),new t.OrderByBlock(s),new t.LimitBlock(s)];super(s,r)}},t.Insert=class extends t.QueryBuilder{constructor(s,r=null){r=r||[new t.StringBlock(s,"INSERT"),new t.IntoTableBlock(s),new t.MssqlInsertFieldValueBlock(s),new t.InsertFieldsFromQueryBlock(s)];super(s,r)}}};var H=S;})();
|
|
1
|
+
(()=>{var{defineProperty:k,getOwnPropertyNames:z,getOwnPropertyDescriptor:A}=Object,Q=Object.prototype.hasOwnProperty;function V(c){return this[c]}var q=(c)=>{var n=(P??=new WeakMap).get(c),h;if(n)return n;if(n=k({},"__esModule",{value:!0}),c&&typeof c==="object"||typeof c==="function"){for(var a of z(c))if(!Q.call(n,a))k(n,a,{get:V.bind(c,a),enumerable:!(h=A(c,a))||h.enumerable})}return P.set(c,n),n},P;var C=(c)=>c;function D(c,n){this[c]=C.bind(null,n)}var R=(c,n)=>{for(var h in n)k(c,h,{get:n[h],enumerable:!0,configurable:!0,set:D.bind(n,h)})};var U={};R(U,{squel:()=>S,default:()=>H});var v={name:"squel",version:"6.3.0",description:"SQL query string builder",keywords:["sql","database","rdbms","query-builder","mysql","postgres","mssql"],author:"Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)",contributors:["Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)","Sergej Brjuchanov <serges@seznam.cz>"],license:"MIT",repository:{type:"git",url:"https://github.com/hiddentao/squel.git"},homepage:"https://github.com/hiddentao/squel",bugs:{url:"https://github.com/hiddentao/squel/issues"},type:"module",main:"./dist/cjs/index.js",module:"./dist/esm/index.js",types:"./dist/types/index.d.ts",exports:{".":{types:"./dist/types/index.d.ts",import:"./dist/esm/index.js",require:"./dist/cjs/index.js"},"./browser":"./dist/browser/squel.min.js"},files:["dist","README.md","LICENSE.md","CHANGELOG.md"],engines:{node:">=18.0.0"},publishConfig:{access:"public",provenance:!0},scripts:{build:"bun run scripts/build.ts",test:"bun test","test:coverage":"bun test --coverage --coverage-reporter=lcov","test:watch":"bun test --watch",lint:"biome check --write ./src ./tests ./scripts",format:"biome format --write ./src ./tests ./scripts",check:"biome check ./src ./tests ./scripts",typecheck:"tsc --noEmit",prepare:"husky",prepublishOnly:"bun run build && bun run test"},dependencies:{},devDependencies:{"@biomejs/biome":"^1.9.4","@commitlint/cli":"^19.8.1","@commitlint/config-conventional":"^19.8.1","@types/bun":"latest","@types/node":"^22.0.0","commit-and-tag-version":"^12.6.0",husky:"^9.1.7",typescript:"^5.3.0"}};function p(c,n){return c.length?c+n:c}function m(c,...n){if(c&&n){for(let h of n)if(h&&typeof h==="object")for(let a of Object.getOwnPropertyNames(h))c[a]=h[a]}return c}function x(c){return!!c&&c.constructor.prototype===Object.prototype}function B(c){return!!c&&c.constructor.prototype===Array.prototype}function F(c){if(!c)return c;let n=c;if(typeof n.clone==="function")return n.clone();if(x(c)||B(c)){let h=new n.constructor;for(let a of Object.getOwnPropertyNames(c))if(typeof n[a]!=="function"&&a!=="_queryBuilder")h[a]=F(n[a]);return h}return JSON.parse(JSON.stringify(c))}function O(c,n,h){let a=typeof n;if(a!=="function"&&a!=="string")throw Error("type must be a class constructor or string");if(typeof h!=="function")throw Error("handler must be a function");for(let e of c)if(e.type===n){e.handler=h;return}c.push({type:n,handler:h})}function T(c,n,h){return E(c,n)||E(c,h)}function E(c,n){for(let h of n)if(typeof c===h.type||typeof h.type!=="string"&&c instanceof h.type)return h.handler;return}function N(c=null){let n={isSquelBuilder(e){return!!e&&!!e._toParamString}},h=(e)=>{return!n.isSquelBuilder(e)||!e.options.rawNesting};n.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" ",stringFormatter:null,rawNesting:!1},n.globalValueHandlers=[],n.registerValueHandler=(e,t)=>{O(n.globalValueHandlers,e,t)},n.Cloneable=class{clone(){let e=new this.constructor;return m(e,F(m({},this)))}},n.BaseBuilder=class extends n.Cloneable{options;constructor(e){super();let t=JSON.parse(JSON.stringify(n.DefaultQueryBuilderOptions));t.stringFormatter=n.DefaultQueryBuilderOptions.stringFormatter,this.options=m(t,e)}registerValueHandler(e,t){return O(this.options.valueHandlers,e,t),this}_sanitizeExpression(e){if(!n.isSquelBuilder(e)){if(typeof e!=="string")throw Error("expression must be a string or builder instance")}return e}_sanitizeName(e,t){if(typeof e!=="string")throw Error(`${t} must be a string`);return e}_sanitizeField(e){if(!n.isSquelBuilder(e))return this._sanitizeName(e,"field name");return e}_sanitizeBaseBuilder(e){if(n.isSquelBuilder(e))return e;throw Error("must be a builder instance")}_sanitizeTable(e,t){if(typeof e!=="string")try{return this._sanitizeBaseBuilder(e)}catch(r){throw Error("table name must be a string or a builder")}return this._sanitizeName(e,"table")}_sanitizeTableAlias(e){return this._sanitizeName(e,"table alias")}_sanitizeFieldAlias(e){return this._sanitizeName(e,"field alias")}_sanitizeLimitOffset(e){let t=Number.parseInt(e,10);if(t<0||Number.isNaN(t))throw Error("limit/offset must be >= 0");return t}_sanitizeValue(e){let t=typeof e;if(e===null);else if(t==="string"||t==="number"||t==="boolean");else if(n.isSquelBuilder(e));else if(!T(e,this.options.valueHandlers,n.globalValueHandlers))throw Error("field value must be a string, number, boolean, null or one of the registered custom value types");return e}_escapeValue(e){return this.options.replaceSingleQuotes&&e?e.replace(/'/g,this.options.singleQuoteReplacement):e}_formatTableName(e){if(this.options.autoQuoteTableNames){let t=this.options.nameQuoteCharacter;e=`${t}${e}${t}`}return e}_formatFieldAlias(e){if(this.options.autoQuoteAliasNames){let t=this.options.fieldAliasQuoteCharacter;e=`${t}${e}${t}`}return e}_formatTableAlias(e){if(this.options.autoQuoteAliasNames){let t=this.options.tableAliasQuoteCharacter;e=`${t}${e}${t}`}return this.options.useAsForTableAliasNames?`AS ${e}`:e}_formatFieldName(e,t={}){if(this.options.autoQuoteFieldNames){let r=this.options.nameQuoteCharacter;if(t.ignorePeriodsForFieldNameQuotes)e=`${r}${e}${r}`;else e=e.split(".").map((s)=>s==="*"?s:`${r}${s}${r}`).join(".")}return e}_formatCustomValue(e,t,r){let s=T(e,this.options.valueHandlers,n.globalValueHandlers);if(s){if(e=s(e,t,r),e?.rawNesting)return{formatted:!0,rawNesting:!0,value:e.value}}return{formatted:!!s,value:e}}_formatValueForParamArray(e,t={}){if(B(e))return e.map((r)=>this._formatValueForParamArray(r,t));return this._formatCustomValue(e,!0,t).value}_formatValueForQueryString(e,t={}){let{rawNesting:r,formatted:s,value:l}=this._formatCustomValue(e,!1,t),i=l;if(s){if(r)return i;return this._applyNestingFormatting(i,h(e))}if(B(i))i=i.map((o)=>this._formatValueForQueryString(o)),i=this._applyNestingFormatting(i.join(", "),h(i));else{let o=typeof i;if(i===null)i="NULL";else if(o==="boolean")i=i?"TRUE":"FALSE";else if(n.isSquelBuilder(i))i=this._applyNestingFormatting(i.toString(),h(i));else if(o!=="number"){if(o==="string"&&this.options.stringFormatter)return this.options.stringFormatter(i);if(t.dontQuote)i=`${i}`;else i=`'${this._escapeValue(i)}'`}}return i}_applyNestingFormatting(e,t=!0){if(e&&typeof e==="string"&&t&&!this.options.rawNesting){let r=e.startsWith("(")&&e.endsWith(")");if(r){let s=0,l=1;while(e.length-1>++s){let i=e.charAt(s);if(i==="(")l++;else if(i===")"){if(l--,l<1){r=!1;break}}}}if(!r)e=`(${e})`}return e}_buildString(e,t,r={}){let{nested:s,buildParameterized:l,formattingOptions:i}=r;t=t||[],e=e||"";let o="",u=-1,d=[],y=this.options.parameterCharacter,f=0;while(e.length>f)if(e.substring(f,f+y.length)===y){let _=t[++u];if(l)if(n.isSquelBuilder(_)){let w=_._toParamString({buildParameterized:l,nested:!0});o+=w.text,w.values.forEach((b)=>d.push(b))}else if(_=this._formatValueForParamArray(_,i),B(_)){let w=_.map(()=>y).join(", ");o+=`(${w})`,_.forEach((b)=>d.push(b))}else o+=y,d.push(_);else o+=this._formatValueForQueryString(_,i);f+=y.length}else o+=e.charAt(f),f++;return{text:this._applyNestingFormatting(o,!!s),values:d}}_buildManyStrings(e,t,r={}){let s=[],l=[];for(let o=0;e.length>o;++o){let u=e[o],d=t[o],{text:y,values:f}=this._buildString(u,d,{buildParameterized:r.buildParameterized,nested:!1});s.push(y),f.forEach((_)=>l.push(_))}let i=s.join(this.options.separator);return{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:l}}_toParamString(e){throw Error("Not yet implemented")}toString(e={}){return this._toParamString(e).text}toParam(e={}){return this._toParamString({...e,buildParameterized:!0})}},n.Expression=class extends n.BaseBuilder{_nodes;constructor(e){super(e);this._nodes=[]}and(e,...t){return e=this._sanitizeExpression(e),this._nodes.push({type:"AND",expr:e,para:t}),this}or(e,...t){return e=this._sanitizeExpression(e),this._nodes.push({type:"OR",expr:e,para:t}),this}_toParamString(e={}){let t=[],r=[];for(let s of this._nodes){let{type:l,expr:i,para:o}=s,{text:u,values:d}=n.isSquelBuilder(i)?i._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(i,o,{buildParameterized:e.buildParameterized});if(t.length)t.push(l);t.push(u),d.forEach((y)=>r.push(y))}return t=t.join(" "),{text:this._applyNestingFormatting(t,!!e.nested),values:r}}},n.Case=class extends n.BaseBuilder{_fieldName=null;_cases;_elseValue=null;constructor(e,t={}){super(t);let r=null;if(x(e))t=e;else if(e)r=this._sanitizeField(e);this._fieldName=r,this.options=m({...n.DefaultQueryBuilderOptions},t),this._cases=[]}when(e,...t){return this._cases.unshift({expression:e,values:t||[]}),this}then(e){if(this._cases.length===0)throw Error("when() needs to be called first");return this._cases[0].result=e,this}else(e){return this._elseValue=e,this}_toParamString(e={}){let t="",r=[];for(let{expression:s,values:l,result:i}of this._cases){t=p(t," ");let o=this._buildString(s,l,{buildParameterized:e.buildParameterized,nested:!0});t+=`WHEN ${o.text} THEN ${this._formatValueForQueryString(i)}`,o.values.forEach((u)=>r.push(u))}if(t.length){if(t+=` ELSE ${this._formatValueForQueryString(this._elseValue)} END`,this._fieldName)t=`${this._fieldName} ${t}`;t=`CASE ${t}`}else t=this._formatValueForQueryString(this._elseValue);return{text:t,values:r}}},n.Over=class extends n.BaseBuilder{_funcExpr;_funcParams;_partitionFields;_orderFields;_rows;_range;constructor(e,t=[],r){super(r);this._funcExpr=e,this._funcParams=t||[],this._partitionFields=[],this._orderFields=[],this._rows=null,this._range=null}partitionBy(...e){let t=[];for(let r of e)if(B(r))t=t.concat(r);else t.push(r);for(let r of t)this._partitionFields.push(this._sanitizeField(r));return this}orderBy(e,t,...r){e=this._sanitizeField(e);let s;if(typeof t==="string")s=t;else if(t===void 0)s="ASC";else if(t===null)s=null;else s=t?"ASC":"DESC";return this._orderFields.push({field:e,dir:s,values:r||[]}),this}rowsBetween(e,t){if(t===void 0)this._rows=`ROWS BETWEEN ${e}`;else this._rows=`ROWS BETWEEN ${e} AND ${t}`;return this._range=null,this}rangeBetween(e,t){if(t===void 0)this._range=`RANGE BETWEEN ${e}`;else this._range=`RANGE BETWEEN ${e} AND ${t}`;return this._rows=null,this}_toParamString(e={}){let t=[],r;if(n.isSquelBuilder(this._funcExpr))r=this._funcExpr._toParamString({buildParameterized:e.buildParameterized});else r=this._buildString(this._funcExpr,this._funcParams,{buildParameterized:e.buildParameterized});r.values.forEach((i)=>t.push(i));let s="";if(this._partitionFields.length>0){let i="";for(let o of this._partitionFields)if(i=p(i,", "),n.isSquelBuilder(o)){let u=o._toParamString({buildParameterized:e.buildParameterized});i+=u.text,u.values.forEach((d)=>t.push(d))}else i+=this._formatFieldName(o);s=`PARTITION BY ${i}`}if(this._orderFields.length>0){let i="";for(let{field:o,dir:u,values:d}of this._orderFields){i=p(i,", ");let y;if(n.isSquelBuilder(o))y=o._toParamString({buildParameterized:e.buildParameterized});else y=this._buildString(o,d,{buildParameterized:e.buildParameterized});if(i+=y.text,y.values.forEach((f)=>t.push(f)),u!==null)i+=` ${u}`}s=p(s," ")+`ORDER BY ${i}`}let l=this._rows||this._range;if(l)s=p(s," ")+l;return{text:`${r.text} OVER (${s})`,values:t}}},n.JsonExtract=class extends n.BaseBuilder{_field;_path;constructor(e,t,r){super(r);this._field=e,this._path=t}_toParamString(e={}){let t=[],r="";if(n.isSquelBuilder(this._field)){let o=this._field._toParamString({buildParameterized:e.buildParameterized});r=o.text,o.values.forEach((u)=>t.push(u))}else r=this._formatFieldName(this._field);let s=this._path;if(s.startsWith("$"))s=s.slice(1);if(s.startsWith("."))s=s.slice(1);let l=s.split(".").filter(Boolean),i="";if(c==="postgres")if(l.length===0)i=r;else{i=r;for(let o=0;o<l.length;o++){let u=o===l.length-1?"->>":"->";i+=`${u}'${l[o]}'`}}else if(c==="mssql")i=`JSON_VALUE(${r}, '${this._path}')`;else i=`json_extract(${r}, '${this._path}')`;return{text:i,values:t}}},n.Block=class extends n.BaseBuilder{constructor(e){super(e)}exposedMethods(){let e={},t=this;while(t){for(let r of Object.getOwnPropertyNames(t))if(r!=="constructor"&&typeof t[r]==="function"&&!r.startsWith("_")&&!n.Block.prototype[r])e[r]=t[r];t=Object.getPrototypeOf(t)}return e}},n.StringBlock=class extends n.Block{_str;constructor(e,t){super(e);this._str=t}_toParamString(e={}){return{text:this._str,values:[]}}},n.FunctionBlock=class extends n.Block{_strings;_values;constructor(e){super(e);this._strings=[],this._values=[]}function(e,...t){this._strings.push(e),this._values.push(t)}_toParamString(e={}){return this._buildManyStrings(this._strings,this._values,e)}},n.registerValueHandler(n.FunctionBlock,(e,t=!1)=>{return t?e.toParam():e.toString()}),n.AbstractTableBlock=class extends n.Block{_tables;constructor(e,t){super(e);this._tables=[]}_table(e,t=null){if(t=t?this._sanitizeTableAlias(t):t,e=this._sanitizeTable(e),this.options.singleTable)this._tables=[];this._tables.push({table:e,alias:t})}_hasTable(){return this._tables.length>0}_toParamString(e={}){let t="",r=[];if(this._hasTable()){for(let{table:s,alias:l}of this._tables){t=p(t,", ");let i;if(n.isSquelBuilder(s)){let{text:o,values:u}=s._toParamString({buildParameterized:e.buildParameterized,nested:!0});i=o,u.forEach((d)=>r.push(d))}else i=this._formatTableName(s);if(l)i+=` ${this._formatTableAlias(l)}`;t+=i}if(this.options.prefix)t=`${this.options.prefix} ${t}`}return{text:t,values:r}}},n.TargetTableBlock=class extends n.AbstractTableBlock{target(e){this._table(e)}},n.UpdateTableBlock=class extends n.AbstractTableBlock{table(e,t=null){this._table(e,t)}_toParamString(e={}){if(!this._hasTable())throw Error("table() needs to be called");return super._toParamString(e)}},n.FromTableBlock=class extends n.AbstractTableBlock{constructor(e){super(m({},e,{prefix:"FROM"}))}from(e,t=null){this._table(e,t)}},n.IntoTableBlock=class extends n.AbstractTableBlock{constructor(e){super(m({},e,{prefix:"INTO",singleTable:!0}))}into(e){this._table(e)}_toParamString(e={}){if(!this._hasTable())throw Error("into() needs to be called");return super._toParamString(e)}},n.GetFieldBlock=class extends n.Block{_fields;constructor(e){super(e);this._fields=[]}fields(e,t={}){if(B(e))for(let r of e)this.field(r,null,t);else for(let r in e){let s=e[r];this.field(r,s,t)}}field(e,t=null,r={}){if(t=t?this._sanitizeFieldAlias(t):t,e=this._sanitizeField(e),this._fields.filter((l)=>l.name===e&&l.alias===t).length)return this;this._fields.push({name:e,alias:t,options:r});return}_toParamString(e={}){let{queryBuilder:t,buildParameterized:r}=e,s="",l=[];for(let i of this._fields){s=p(s,", ");let{name:o,alias:u,options:d}=i;if(typeof o==="string")s+=this._formatFieldName(o,d);else{let y=o._toParamString({nested:!0,buildParameterized:r});s+=y.text,y.values.forEach((f)=>l.push(f))}if(u)s+=` AS ${this._formatFieldAlias(u)}`}if(!s.length){if(t?.getBlock(n.FromTableBlock)?._hasTable())s="*"}return{text:s,values:l}}},n.AbstractSetFieldBlock=class extends n.Block{_fields;_values;_valueOptions;constructor(e){super(e);this._fields=[],this._values=[[]],this._valueOptions=[[]]}_reset(){this._fields=[],this._values=[[]],this._valueOptions=[[]]}_set(e,t,r={}){if(this._values.length>1)throw Error("Cannot set multiple rows of fields this way.");if(typeof t<"u")t=this._sanitizeValue(t);e=this._sanitizeField(e);let s=this._fields.indexOf(e);if(s===-1)this._fields.push(e),s=this._fields.length-1;this._values[0][s]=t,this._valueOptions[0][s]=r}_setFields(e,t={}){if(typeof e!=="object")throw Error(`Expected an object but got ${typeof e}`);for(let r in e)this._set(r,e[r],t)}_setFieldsRows(e,t={}){if(!B(e))throw Error(`Expected an array of objects but got ${typeof e}`);this._reset();for(let r=0;e.length>r;++r){let s=e[r];for(let l in s){let i=s[l];l=this._sanitizeField(l),i=this._sanitizeValue(i);let o=this._fields.indexOf(l);if(r>0&&o===-1)throw Error("All fields in subsequent rows must match the fields in the first row");if(o===-1)this._fields.push(l),o=this._fields.length-1;if(!B(this._values[r]))this._values[r]=[],this._valueOptions[r]=[];this._values[r][o]=i,this._valueOptions[r][o]=t}}}},n.SetFieldBlock=class extends n.AbstractSetFieldBlock{set(e,t,r){this._set(e,t,r)}setFields(e,t){this._setFields(e,t)}_toParamString(e={}){let{buildParameterized:t}=e;if(this._fields.length<=0)throw Error("set() needs to be called");let r="",s=[];for(let l=0;l<this._fields.length;++l){r=p(r,", ");let i=this._formatFieldName(this._fields[l]),o=this._values[0][l];if(i.indexOf("=")<0)i=`${i} = ${this.options.parameterCharacter}`;let u=this._buildString(i,[o],{buildParameterized:t,formattingOptions:this._valueOptions[0][l]});r+=u.text,u.values.forEach((d)=>s.push(d))}return{text:`SET ${r}`,values:s}}},n.InsertFieldValueBlock=class extends n.AbstractSetFieldBlock{set(e,t,r={}){this._set(e,t,r)}setFields(e,t){this._setFields(e,t)}setFieldsRows(e,t){this._setFieldsRows(e,t)}_toParamString(e={}){let{buildParameterized:t}=e,r=this._fields.map((i)=>this._formatFieldName(i)).join(", "),s=[],l=[];for(let i=0;i<this._values.length;++i){s[i]="";for(let o=0;o<this._values[i].length;++o){let u=this._buildString(this.options.parameterCharacter,[this._values[i][o]],{buildParameterized:t,formattingOptions:this._valueOptions[i][o]});u.values.forEach((d)=>l.push(d)),s[i]=p(s[i],", "),s[i]+=u.text}}return{text:r.length?`(${r}) VALUES (${s.join("), (")})`:"",values:l}}},n.InsertFieldsFromQueryBlock=class extends n.Block{_fields;_query=null;constructor(e){super(e);this._fields=[]}fromQuery(e,t){this._fields=e.map((r)=>this._sanitizeField(r)),this._query=this._sanitizeBaseBuilder(t)}_toParamString(e={}){let t="",r=[];if(this._fields.length&&this._query){let{text:s,values:l}=this._query._toParamString({buildParameterized:e.buildParameterized,nested:!0});t=`(${this._fields.join(", ")}) ${this._applyNestingFormatting(s)}`,r=l}return{text:t,values:r}}},n.DistinctBlock=class extends n.Block{_useDistinct=!1;distinct(){this._useDistinct=!0}_toParamString(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}},n.GroupByBlock=class extends n.Block{_groups;constructor(e){super(e);this._groups=[]}group(e){this._groups.push(this._sanitizeField(e))}_toParamString(e={}){return{text:this._groups.length?`GROUP BY ${this._groups.join(", ")}`:"",values:[]}}},n.AbstractVerbSingleValueBlock=class extends n.Block{_value=null;constructor(e){super(e)}_setValue(e){this._value=e!==null?this._sanitizeLimitOffset(e):e}_toParamString(e={}){let t=this._value!==null?`${this.options.verb} ${this.options.parameterCharacter}`:"",r=this._value!==null?[this._value]:[];return this._buildString(t,r,e)}},n.OffsetBlock=class extends n.AbstractVerbSingleValueBlock{constructor(e){super(m({},e,{verb:"OFFSET"}))}offset(e){this._setValue(e)}},n.LimitBlock=class extends n.AbstractVerbSingleValueBlock{constructor(e){super(m({},e,{verb:"LIMIT"}))}limit(e){this._setValue(e)}},n.AbstractConditionBlock=class extends n.Block{_conditions;constructor(e){super(e);this._conditions=[]}_condition(e,...t){e=this._sanitizeExpression(e),this._conditions.push({expr:e,values:t||[]})}_toParamString(e={}){let t=[],r=[];for(let{expr:l,values:i}of this._conditions){let o=n.isSquelBuilder(l)?l._toParamString({buildParameterized:e.buildParameterized}):this._buildString(l,i,{buildParameterized:e.buildParameterized});if(o.text.length)t.push(o.text);o.values.forEach((u)=>r.push(u))}let s=t.join(") AND (");return{text:s.length?`${this.options.verb} (${s})`:"",values:r}}},n.WhereBlock=class extends n.AbstractConditionBlock{constructor(e){super(m({},e,{verb:"WHERE"}))}where(e,...t){this._condition(e,...t)}},n.HavingBlock=class extends n.AbstractConditionBlock{constructor(e){super(m({},e,{verb:"HAVING"}))}having(e,...t){this._condition(e,...t)}},n.OrderByBlock=class extends n.Block{_orders;constructor(e){super(e);this._orders=[]}order(e,t,...r){e=this._sanitizeField(e);let s;if(typeof t==="string")s=t;else if(t===void 0)s="ASC";else if(t===null)s=null;else s=t?"ASC":"DESC";this._orders.push({field:e,dir:s,values:r||[]})}_toParamString(e={}){let t="",r=[];for(let{field:s,dir:l,values:i}of this._orders){t=p(t,", ");let o=this._buildString(s,i,{buildParameterized:e.buildParameterized});if(t+=o.text,B(o.values))o.values.forEach((u)=>r.push(u));if(l!==null)t+=` ${l}`}return{text:t.length?`ORDER BY ${t}`:"",values:r}}},n.JoinBlock=class extends n.Block{_joins;constructor(e){super(e);this._joins=[]}join(e,t=null,r=null,s="INNER"){e=this._sanitizeTable(e,!0),t=t?this._sanitizeTableAlias(t):t,r=r?this._sanitizeExpression(r):r,this._joins.push({type:s,table:e,alias:t,condition:r})}left_join(e,t=null,r=null){this.join(e,t,r,"LEFT")}right_join(e,t=null,r=null){this.join(e,t,r,"RIGHT")}outer_join(e,t=null,r=null){this.join(e,t,r,"OUTER")}left_outer_join(e,t=null,r=null){this.join(e,t,r,"LEFT OUTER")}full_join(e,t=null,r=null){this.join(e,t,r,"FULL")}cross_join(e,t=null,r=null){this.join(e,t,r,"CROSS")}_toParamString(e={}){let t="",r=[];for(let{type:s,table:l,alias:i,condition:o,isApply:u}of this._joins){t=p(t,this.options.separator);let d;if(n.isSquelBuilder(l)){let y=l._toParamString({buildParameterized:e.buildParameterized,nested:!0});y.values.forEach((f)=>r.push(f)),d=y.text}else d=this._formatTableName(l);if(u)t+=`${s} ${d}`;else t+=`${s} JOIN ${d}`;if(i)t+=` ${this._formatTableAlias(i)}`;if(o){t+=" ON ";let y;if(n.isSquelBuilder(o))y=o._toParamString({buildParameterized:e.buildParameterized});else y=this._buildString(o,[],{buildParameterized:e.buildParameterized});t+=this._applyNestingFormatting(y.text),y.values.forEach((f)=>r.push(f))}}return{text:t,values:r}}},n.UnionBlock=class extends n.Block{_unions;constructor(e){super(e);this._unions=[]}union(e,t="UNION"){e=this._sanitizeTable(e),this._unions.push({type:t,table:e})}union_all(e){this.union(e,"UNION ALL")}_toParamString(e={}){let t="",r=[];for(let{type:s,table:l}of this._unions){t=p(t,this.options.separator);let i;if(l instanceof n.BaseBuilder){let o=l._toParamString({buildParameterized:e.buildParameterized,nested:!0});i=o.text,o.values.forEach((u)=>r.push(u))}else t=this._formatTableName(l),i="";t+=`${s} ${i}`}return{text:t,values:r}}},n.ForBlock=class extends n.Block{_forStr;constructor(e){super(e);this._forStr=null}for(e){this._forStr=e}_toParamString(){return{text:this._forStr!==null?`FOR ${this._forStr}`:"",values:[]}}},n.WithBlock=class extends n.Block{_tables;constructor(e){super(e);this._tables=[]}with(e,t){this._tables.push({alias:e,table:t,recursive:!1})}withRecursive(e,t){this._tables.push({alias:e,table:t,recursive:!0})}_toParamString(e={}){let t=[],r=[],s=!1;for(let{alias:o,table:u,recursive:d}of this._tables){if(d)s=!0;let y=u._toParamString({buildParameterized:e.buildParameterized,nested:!0});t.push(`${o} AS ${y.text}`),y.values.forEach((f)=>r.push(f))}if(!t.length)return{text:"",values:[]};let l=this.options.useRecursiveKeyword===!1;return{text:`${s&&!l?"WITH RECURSIVE":"WITH"} ${t.join(", ")}`,values:r}}},n.ReturningBlock=class extends n.Block{_fields;constructor(e){super(e);this._fields=[]}returning(e,t=null,r={}){if(t=t?this._sanitizeFieldAlias(t):t,e=this._sanitizeField(e),this._fields.filter((l)=>l.name===e&&l.alias===t).length)return this;this._fields.push({name:e,alias:t,options:r})}_toParamString(e={}){let{buildParameterized:t}=e,r="",s=[];for(let l of this._fields){r=p(r,", ");let{name:i,alias:o,options:u}=l;if(typeof i==="string")r+=this._formatFieldName(i,u);else{let d=i._toParamString({nested:!0,buildParameterized:t});r+=d.text,d.values.forEach((y)=>s.push(y))}if(o)r+=` AS ${this._formatFieldAlias(o)}`}return{text:r.length>0?`RETURNING ${r}`:"",values:s}}},n.QueryBuilder=class extends n.BaseBuilder{blocks;constructor(e,t){super(e);this.blocks=t||[];for(let r of this.blocks){Object.defineProperty(r,"_queryBuilder",{value:this,enumerable:!1,writable:!0,configurable:!0});let s=r.exposedMethods();for(let l in s){let i=s[l];if(this[l]!==void 0)throw Error(`Builder already has a builder method called: ${l}`);((o,u,d)=>{this[u]=(...y)=>{let f=d.call(o,...y);if(f!==void 0&&f!==o)return f;return this}})(r,l,i)}}}registerValueHandler(e,t){for(let r of this.blocks)r.registerValueHandler(e,t);return super.registerValueHandler(e,t),this}updateOptions(e){this.options=m(this.options,e);for(let t of this.blocks)t.options=m(t.options,e)}_toParamString(e={}){let t=m({},this.options,e),r=this.blocks.map((u)=>u._toParamString({buildParameterized:t.buildParameterized,queryBuilder:this})),s=r.map((u)=>u.text),l=r.map((u)=>u.values),i=s.filter((u)=>u.length>0).join(t.separator),o=[];if(l.forEach((u)=>u.forEach((d)=>o.push(d))),!t.nested){if(t.numberedParameters){let u=t.numberedParametersStartAt!==void 0?t.numberedParametersStartAt:1,d=t.parameterCharacter.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&");i=i.replace(new RegExp(d,"g"),()=>{return`${t.numberedParametersPrefix}${u++}`})}}return{text:this._applyNestingFormatting(i,!!t.nested),values:o}}clone(){let e=this.blocks.map((t)=>t.clone());return new this.constructor(this.options,e)}getBlock(e){return this.blocks.find((t)=>t instanceof e)}},n.Select=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"SELECT"),new n.FunctionBlock(e),new n.DistinctBlock(e),new n.GetFieldBlock(e),new n.FromTableBlock(e),new n.JoinBlock(e),new n.WhereBlock(e),new n.GroupByBlock(e),new n.HavingBlock(e),new n.OrderByBlock(e),new n.LimitBlock(e),new n.OffsetBlock(e),new n.UnionBlock(e),new n.ForBlock(e)];super(e,t)}},n.Update=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"UPDATE"),new n.UpdateTableBlock(e),new n.SetFieldBlock(e),new n.WhereBlock(e),new n.OrderByBlock(e),new n.LimitBlock(e),new n.ReturningBlock(e)];super(e,t)}},n.Delete=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"DELETE"),new n.TargetTableBlock(e),new n.FromTableBlock(m({},e,{singleTable:!0})),new n.JoinBlock(e),new n.WhereBlock(e),new n.OrderByBlock(e),new n.LimitBlock(e),new n.ReturningBlock(e)];super(e,t)}},n.Insert=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"INSERT"),new n.IntoTableBlock(e),new n.InsertFieldValueBlock(e),new n.InsertFieldsFromQueryBlock(e),new n.ReturningBlock(e)];super(e,t)}};let a={VERSION:v.version,flavour:c,expr:(e)=>new n.Expression(e),case:(e,t)=>new n.Case(e,t),select:(e,t)=>new n.Select(e,t),update:(e,t)=>new n.Update(e,t),insert:(e,t)=>new n.Insert(e,t),delete:(e,t)=>new n.Delete(e,t),str:(...e)=>{let t=new n.FunctionBlock;return t.function(...e),t},rstr:(...e)=>{let t=new n.FunctionBlock({rawNesting:!0});return t.function(...e),t},over:(e,...t)=>new n.Over(e,t),jsonExtract:(e,t)=>new n.JsonExtract(e,t),registerValueHandler:n.registerValueHandler};return a.remove=a.delete,a.cls=n,a}var g=N();g.flavours={};g.useFlavour=function(n=null){if(!n)return g;if(g.flavours[n]instanceof Function){let h=N(n);return g.flavours[n].call(null,h),h.flavours=g.flavours,h.useFlavour=g.useFlavour,h}throw Error(`Flavour not available: ${n}`)};var S=g;g.flavours.mysql=(c)=>{let n=c.cls;class h{builder;block;constructor(a,e){this.builder=a;this.block=e}}n.MysqlOnDuplicateKeyUpdateBlock=class extends n.AbstractSetFieldBlock{onDupUpdate(a,e,t){this._set(a,e,t)}onDuplicateKeyUpdate(){let a=new h(this._queryBuilder,this);return new Proxy(a,{get(e,t,r){if(t==="set")return(l,i,o)=>{return e.block._set(l,i,o),r};let s=e.builder[t];if(typeof s==="function")return(...l)=>{let i=s.apply(e.builder,l);return i===e.builder?r:i};return s}})}_toParamString(a={}){let e="",t=[];for(let r=0;r<this._fields.length;++r){e=p(e,", ");let s=this._fields[r],l=this._values[0][r],i=this._valueOptions[0][r];if(typeof l>"u")e+=s;else{let o=this._buildString(`${s} = ${this.options.parameterCharacter}`,[l],{buildParameterized:a.buildParameterized,formattingOptions:i});e+=o.text,o.values.forEach((u)=>t.push(u))}}return{text:!e.length?"":`ON DUPLICATE KEY UPDATE ${e}`,values:t}}},n.Insert=class extends n.QueryBuilder{constructor(a,e=null){e=e||[new n.WithBlock(a),new n.StringBlock(a,"INSERT"),new n.IntoTableBlock(a),new n.InsertFieldValueBlock(a),new n.InsertFieldsFromQueryBlock(a),new n.MysqlOnDuplicateKeyUpdateBlock(a)];super(a,e)}},n.Replace=class extends n.QueryBuilder{constructor(a,e=null){e=e||[new n.WithBlock(a),new n.StringBlock(a,"REPLACE"),new n.IntoTableBlock(a),new n.InsertFieldValueBlock(a),new n.InsertFieldsFromQueryBlock(a)];super(a,e)}},c.replace=(a,e)=>new n.Replace(a,e)};g.flavours.postgres=(c)=>{let n=c.cls;n.DefaultQueryBuilderOptions.numberedParameters=!0,n.DefaultQueryBuilderOptions.numberedParametersStartAt=1,n.DefaultQueryBuilderOptions.autoQuoteAliasNames=!1,n.DefaultQueryBuilderOptions.useAsForTableAliasNames=!0;class h{builder;block;constructor(a,e){this.builder=a;this.block=e}}n.PostgresOnConflictKeyUpdateBlock=class extends n.AbstractSetFieldBlock{_onConflict;_dupFields;constructor(a){super(a);this._onConflict=!1,this._dupFields=[]}onConflict(a,e){if(this._onConflict=!0,a){if(!B(a))a=[a];this._dupFields=a.map(this._sanitizeField.bind(this))}if(e)Object.keys(e).forEach((t)=>{this._set(t,e[t])});return this._queryBuilder}doUpdate(){let a=new h(this._queryBuilder,this);return new Proxy(a,{get(e,t,r){if(t==="set")return(l,i,o)=>{return e.block._set(l,i,o),r};let s=e.builder[t];if(typeof s==="function")return(...l)=>{let i=s.apply(e.builder,l);return i===e.builder?r:i};return s}})}doNothing(){return this._onConflict=!0,this._fields=[],this._values=[[]],this._valueOptions=[[]],this._queryBuilder}_toParamString(a={}){let e="",t=[];for(let s=0;s<this._fields.length;++s){e=p(e,", ");let l=this._fields[s],i=this._values[0][s],o=this._valueOptions[0][s];if(typeof i>"u")e+=l;else{let u=this._buildString(`${l} = ${this.options.parameterCharacter}`,[i],{buildParameterized:a.buildParameterized,formattingOptions:o});e+=u.text,u.values.forEach((d)=>t.push(d))}}let r={text:"",values:t};if(this._onConflict){let s=this._dupFields.length?`(${this._dupFields.join(", ")}) `:"",l=e.length?`UPDATE SET ${e}`:"NOTHING";r.text=`ON CONFLICT ${s}DO ${l}`}return r}},n.UsingBlock=class extends n.AbstractTableBlock{constructor(a){super({...a,prefix:"USING"})}using(a,e=null){this._table(a,e)}},n.DistinctOnBlock=class extends n.Block{_useDistinct=!1;_distinctFields;constructor(a){super(a);this._distinctFields=[]}distinct(...a){this._useDistinct=!0,a.forEach((e)=>{this._distinctFields.push(this._sanitizeField(e))})}_toParamString(){let a="";if(this._useDistinct){if(a="DISTINCT",this._distinctFields.length)a+=` ON (${this._distinctFields.join(", ")})`}return{text:a,values:[]}}},n.Select=class extends n.QueryBuilder{constructor(a,e=null){e=e||[new n.WithBlock(a),new n.StringBlock(a,"SELECT"),new n.FunctionBlock(a),new n.DistinctOnBlock(a),new n.GetFieldBlock(a),new n.FromTableBlock(a),new n.JoinBlock(a),new n.WhereBlock(a),new n.GroupByBlock(a),new n.HavingBlock(a),new n.OrderByBlock(a),new n.LimitBlock(a),new n.OffsetBlock(a),new n.UnionBlock(a),new n.ForBlock(a)];super(a,e)}},n.Insert=class extends n.QueryBuilder{constructor(a,e=null){e=e||[new n.WithBlock(a),new n.StringBlock(a,"INSERT"),new n.IntoTableBlock(a),new n.InsertFieldValueBlock(a),new n.InsertFieldsFromQueryBlock(a),new n.PostgresOnConflictKeyUpdateBlock(a),new n.ReturningBlock(a)];super(a,e)}},n.Update=class extends n.QueryBuilder{constructor(a,e=null){e=e||[new n.WithBlock(a),new n.StringBlock(a,"UPDATE"),new n.UpdateTableBlock(a),new n.SetFieldBlock(a),new n.FromTableBlock(a),new n.WhereBlock(a),new n.OrderByBlock(a),new n.LimitBlock(a),new n.ReturningBlock(a)];super(a,e)}},n.Delete=class extends n.QueryBuilder{constructor(a,e=null){e=e||[new n.WithBlock(a),new n.StringBlock(a,"DELETE"),new n.TargetTableBlock(a),new n.FromTableBlock({...a,singleTable:!0}),new n.UsingBlock(a),new n.JoinBlock(a),new n.WhereBlock(a),new n.OrderByBlock(a),new n.LimitBlock(a),new n.ReturningBlock(a)];super(a,e)}}};var $=function(c){c=this._sanitizeLimitOffset(c),this._parent._limits=c};g.flavours.mssql=(c)=>{let n=c.cls;n.DefaultQueryBuilderOptions.replaceSingleQuotes=!0,n.DefaultQueryBuilderOptions.autoQuoteAliasNames=!1,n.DefaultQueryBuilderOptions.numberedParametersPrefix="@",n.DefaultQueryBuilderOptions.useRecursiveKeyword=!1,c.registerValueHandler(Date,(e)=>{let t=e;return`'${t.getUTCFullYear()}-${t.getUTCMonth()+1}-${t.getUTCDate()} ${t.getUTCHours()}:${t.getUTCMinutes()}:${t.getUTCSeconds()}'`}),n.MssqlLimitOffsetTopBlock=class extends n.Block{_limits;_offsets;ParentBlock;LimitBlock;TopBlock;OffsetBlock;constructor(e){super(e);this._limits=null,this._offsets=null,this.ParentBlock=class extends n.Block{_parent;constructor(t){super(t.options);this._parent=t}},this.LimitBlock=class extends this.ParentBlock{limit;constructor(t){super(t);this.limit=$}_toParamString(){let t="";if(this._parent._limits&&this._parent._offsets)t=`FETCH NEXT ${this._parent._limits} ROWS ONLY`;return{text:t,values:[]}}},this.TopBlock=class extends this.ParentBlock{top;constructor(t){super(t);this.top=$}_toParamString(){let t="";if(this._parent._limits&&!this._parent._offsets)t=`TOP (${this._parent._limits})`;return{text:t,values:[]}}},this.OffsetBlock=class extends this.ParentBlock{offset(t){this._parent._offsets=this._sanitizeLimitOffset(t)}_toParamString(){let t="";if(this._parent._offsets)t=`OFFSET ${this._parent._offsets} ROWS`;return{text:t,values:[]}}}}LIMIT(){return new this.LimitBlock(this)}TOP(){return new this.TopBlock(this)}OFFSET(){return new this.OffsetBlock(this)}},n.MssqlUpdateTopBlock=class extends n.Block{_limits;limit;top;constructor(e){super(e);this._limits=null;let t=(r)=>{this._limits=this._sanitizeLimitOffset(r)};this.limit=t,this.top=t}_toParamString(){return{text:this._limits?`TOP (${this._limits})`:"",values:[]}}},n.MssqlInsertFieldValueBlock=class extends n.InsertFieldValueBlock{_outputs;constructor(e){super(e);this._outputs=[]}outputs(e){for(let t in e)this.output(t,e[t])}output(e,t=null){if(typeof e==="string")e=this._sanitizeField(e),t=t?this._sanitizeFieldAlias(t):t,this._outputs.push({name:`INSERTED.${e}`,alias:t});else if(Array.isArray(e))e.forEach((r)=>{this.output(r)})}_toParamString(e){let t=super._toParamString(e);if(t.text.length&&this._outputs.length>0){let s=`OUTPUT ${this._outputs.map((i)=>{let o=i.name;if(i.alias)o+=` AS ${this._formatFieldAlias(i.alias)}`;return o}).join(", ")} `,l=t.text.indexOf("VALUES");t.text=t.text.substring(0,l)+s+t.text.substring(l)}return t}},n.MssqlUpdateDeleteOutputBlock=class extends n.Block{_outputs;constructor(e){super(e);this._outputs=[]}outputs(e){for(let t in e)this.output(t,e[t])}output(e,t=null){e=this._sanitizeField(e),t=t?this._sanitizeFieldAlias(t):t,this._outputs.push({name:this.options.forDelete?`DELETED.${e}`:`INSERTED.${e}`,alias:t})}_toParamString(e){let t="";if(this._outputs.length){for(let r of this._outputs)if(t=p(t,", "),t+=r.name,r.alias)t+=` AS ${this._formatFieldAlias(r.alias)}`;t=`OUTPUT ${t}`}return{text:t,values:[]}}},n.MssqlJoinBlock=class extends n.JoinBlock{apply(e,t=null,r="CROSS"){e=this._sanitizeTable(e,!0),t=t?this._sanitizeTableAlias(t):t;let s=r.toUpperCase();if(!s.endsWith("APPLY"))s=`${s} APPLY`;this._joins.push({type:s,table:e,alias:t,condition:null,isApply:!0})}cross_apply(e,t=null){this.apply(e,t,"CROSS")}outer_apply(e,t=null){this.apply(e,t,"OUTER")}},n.Select=class extends n.QueryBuilder{constructor(e,t=null){let r=new n.MssqlLimitOffsetTopBlock(e);t=t||[new n.WithBlock(e),new n.StringBlock(e,"SELECT"),new n.DistinctBlock(e),r.TOP(),new n.GetFieldBlock(e),new n.FromTableBlock(e),new n.MssqlJoinBlock(e),new n.WhereBlock(e),new n.GroupByBlock(e),new n.HavingBlock(e),new n.OrderByBlock(e),r.OFFSET(),r.LIMIT(),new n.UnionBlock(e)];super(e,t)}},n.Update=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"UPDATE"),new n.MssqlUpdateTopBlock(e),new n.UpdateTableBlock(e),new n.SetFieldBlock(e),new n.MssqlUpdateDeleteOutputBlock(e),new n.WhereBlock(e)];super(e,t)}},n.Delete=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"DELETE"),new n.TargetTableBlock(e),new n.FromTableBlock(m({},e,{singleTable:!0})),new n.JoinBlock(e),new n.MssqlUpdateDeleteOutputBlock(m({},e,{forDelete:!0})),new n.WhereBlock(e),new n.OrderByBlock(e),new n.LimitBlock(e)];super(e,t)}},n.Insert=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"INSERT"),new n.IntoTableBlock(e),new n.MssqlInsertFieldValueBlock(e),new n.InsertFieldsFromQueryBlock(e)];super(e,t)}},n.MergeIntoBlock=class extends n.Block{_table=null;_alias=null;into(e,t=null){this._table=this._sanitizeTable(e),this._alias=t?this._sanitizeTableAlias(t):null}_toParamString(){if(!this._table)throw Error("into() needs to be called");let e=this._formatTableName(this._table);if(this._alias)e+=` AS ${this._formatTableAlias(this._alias)}`;return{text:e,values:[]}}},n.MergeUsingBlock=class extends n.Block{_source=null;_alias=null;_condition=null;using(e,t=null,r=null){this._source=typeof e==="string"?this._sanitizeTable(e):this._sanitizeBaseBuilder(e),this._alias=t?this._sanitizeTableAlias(t):null,this._condition=r?this._sanitizeExpression(r):null}_toParamString(e={}){if(!this._source)throw Error("using() needs to be called");let t="",r=[];if(typeof this._source==="string")t=this._formatTableName(this._source);else{let l=this._source._toParamString({buildParameterized:e.buildParameterized,nested:!0});t=l.text,l.values.forEach((i)=>r.push(i))}if(this._alias)t+=` AS ${this._formatTableAlias(this._alias)}`;let s="";if(this._condition){let l;if(typeof this._condition==="string")l=this._buildString(this._condition,[],{buildParameterized:e.buildParameterized});else l=this._condition._toParamString({buildParameterized:e.buildParameterized});s=` ON ${this._applyNestingFormatting(l.text)}`,l.values.forEach((i)=>r.push(i))}return{text:`USING ${t}${s}`,values:r}}};class h{builder;clause;constructor(e,t){this.builder=e;this.clause=t}update(e){return this.clause.action={type:"UPDATE",fields:e},this.builder}delete(){return this.clause.action={type:"DELETE"},this.builder}}class a{builder;clause;constructor(e,t){this.builder=e;this.clause=t}insert(e){return this.clause.action={type:"INSERT",fields:e},this.builder}}n.MergeWhenBlock=class extends n.Block{_clauses;constructor(e){super(e);this._clauses=[]}whenMatched(e=null){let t={type:"MATCHED",condition:e,action:null};return this._clauses.push(t),new h(this._queryBuilder,t)}whenNotMatched(e=null){let t={type:"NOT_MATCHED",condition:e,action:null};return this._clauses.push(t),new a(this._queryBuilder,t)}_toParamString(e={}){let t="",r=[];for(let s of this._clauses){if(!s.action)continue;t=p(t," ");let l="";if(s.condition){let u=this._buildString(s.condition,[],{buildParameterized:e.buildParameterized});l=` AND ${u.text}`,u.values.forEach((d)=>r.push(d))}let i="";if(s.action.type==="UPDATE"){let u="";for(let d of Object.keys(s.action.fields)){u=p(u,", ");let y=s.action.fields[d];if(y&&typeof y._toParamString==="function"){let f=y._toParamString({buildParameterized:e.buildParameterized});u+=`${this._formatFieldName(d)} = ${f.text}`,f.values.forEach((_)=>r.push(_))}else{let f=this._buildString(`${this._formatFieldName(d)} = ${this.options.parameterCharacter}`,[y],{buildParameterized:e.buildParameterized});u+=f.text,f.values.forEach((_)=>r.push(_))}}i=`THEN UPDATE SET ${u}`}else if(s.action.type==="DELETE")i="THEN DELETE";else if(s.action.type==="INSERT"){let u=Object.keys(s.action.fields).map((y)=>this._formatFieldName(y)).join(", "),d="";for(let y of Object.keys(s.action.fields)){d=p(d,", ");let f=s.action.fields[y];if(f&&typeof f._toParamString==="function"){let _=f._toParamString({buildParameterized:e.buildParameterized});d+=_.text,_.values.forEach((w)=>r.push(w))}else{let _=this._buildString(this.options.parameterCharacter,[f],{buildParameterized:e.buildParameterized});d+=_.text,_.values.forEach((w)=>r.push(w))}}i=`THEN INSERT (${u}) VALUES (${d})`}let o=s.type==="MATCHED"?"WHEN MATCHED":"WHEN NOT MATCHED";t+=`${o}${l} ${i}`}return{text:t.length?`${t};`:"",values:r}}},n.Merge=class extends n.QueryBuilder{constructor(e,t=null){t=t||[new n.WithBlock(e),new n.StringBlock(e,"MERGE INTO"),new n.MergeIntoBlock(e),new n.MergeUsingBlock(e),new n.MergeWhenBlock(e)];super(e,t)}},c.merge=(e,t)=>new n.Merge(e,t)};var H=S;})();
|