@shumai-one/shumai 0.6.2 → 0.6.4

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.
Files changed (192) hide show
  1. package/bin/{chunk-mcj1xpcc.js → chunk-01vx2chc.js} +2 -2
  2. package/bin/chunk-0epkrexd.js +2 -0
  3. package/bin/chunk-0h5239kt.js +2 -0
  4. package/bin/{chunk-jt0ykktx.js → chunk-0jnstdgf.js} +1 -1
  5. package/bin/{chunk-x03jwaga.js → chunk-0njsjy6x.js} +4 -4
  6. package/bin/{chunk-7naa7tpn.js → chunk-13rq2gtr.js} +2 -2
  7. package/bin/chunk-1aqs9sdh.js +3 -0
  8. package/bin/{chunk-m0q2cgfg.js → chunk-22jbsrka.js} +1 -1
  9. package/bin/chunk-2qknebmd.js +1 -0
  10. package/bin/{chunk-a411xhmm.js → chunk-2qzmh3ae.js} +4 -4
  11. package/bin/{chunk-2nhgdqdg.js → chunk-2tt0b8ym.js} +2 -2
  12. package/bin/{chunk-p3f5aevs.js → chunk-2zm7czk6.js} +1 -1
  13. package/bin/{chunk-p4s95471.js → chunk-396tfwdx.js} +1 -1
  14. package/bin/chunk-3mx4trvh.js +10 -0
  15. package/bin/{chunk-b5rhvzv9.js → chunk-45pb3kzz.js} +2 -2
  16. package/bin/{chunk-q83h0hkx.js → chunk-47fmhzwv.js} +2 -2
  17. package/bin/chunk-4awk4xhq.js +2 -0
  18. package/bin/{chunk-2cdxm1cp.js → chunk-4b2x2qba.js} +23 -23
  19. package/bin/{chunk-knj2s8mg.js → chunk-4rjy7ybr.js} +8 -8
  20. package/bin/{chunk-q8w4mbw4.js → chunk-598x693h.js} +2 -2
  21. package/bin/chunk-5c7ztqs7.js +1 -0
  22. package/bin/{chunk-2jj4j8cn.js → chunk-65dh7x4c.js} +2 -2
  23. package/bin/{chunk-hnkcgvcj.js → chunk-66e2vrky.js} +1 -1
  24. package/bin/{chunk-g22wmpk1.js → chunk-6j3beg6n.js} +2 -2
  25. package/bin/{chunk-cgmebxx6.js → chunk-6ke13k69.js} +1 -1
  26. package/bin/{chunk-7sgrtxbt.js → chunk-6rqakmg0.js} +2 -2
  27. package/bin/{chunk-4mws03es.js → chunk-6ty4tfmf.js} +3 -3
  28. package/bin/chunk-7e9ws6dh.js +129 -0
  29. package/bin/chunk-7mdddkfc.js +1 -0
  30. package/bin/{chunk-q2bew2vc.js → chunk-7q4mgd5x.js} +2 -2
  31. package/bin/chunk-7yv7shad.js +1 -0
  32. package/bin/{chunk-qwpkcp8d.js → chunk-81b8rfv7.js} +2 -2
  33. package/bin/{chunk-atfz7g7e.js → chunk-8a7ymmd4.js} +2 -2
  34. package/bin/chunk-8ac9at5y.js +2 -0
  35. package/bin/{chunk-gxepkvt2.js → chunk-8zcfgz9c.js} +1 -1
  36. package/bin/{chunk-mcw198kg.js → chunk-955kf5kc.js} +8 -8
  37. package/bin/{chunk-jzgbxqdg.js → chunk-978rm9a9.js} +2 -2
  38. package/bin/{chunk-jt02kpfc.js → chunk-9qzbrp4d.js} +2 -2
  39. package/bin/{chunk-tekwpkjf.js → chunk-a283dh05.js} +2 -2
  40. package/bin/chunk-aa2a06cc.js +1 -0
  41. package/bin/{chunk-8cjet7pp.js → chunk-ajr1w3vq.js} +6 -6
  42. package/bin/chunk-akafypge.js +218 -0
  43. package/bin/chunk-amk373q2.js +2 -0
  44. package/bin/chunk-apq2gvvp.js +4 -0
  45. package/bin/chunk-av2w1sez.js +2 -0
  46. package/bin/{chunk-5wapd171.js → chunk-b6kmj34z.js} +12 -12
  47. package/bin/chunk-bkzaw9p8.js +1 -0
  48. package/bin/{chunk-zezje74z.js → chunk-bssb9tar.js} +3 -3
  49. package/bin/chunk-bwvj1aqc.js +1 -0
  50. package/bin/chunk-by9esjn7.js +3 -0
  51. package/bin/chunk-bywk8mbc.js +2 -0
  52. package/bin/{chunk-9xhjy897.js → chunk-c61g9hfa.js} +4 -4
  53. package/bin/{chunk-zrszykjf.js → chunk-cdcgjh8k.js} +1 -1
  54. package/bin/{chunk-yk12hydf.js → chunk-d277evce.js} +4 -4
  55. package/bin/{chunk-55h9ze53.js → chunk-d3brvxh7.js} +2 -2
  56. package/bin/{chunk-9t3p4xp6.js → chunk-dcb1nzf1.js} +1 -1
  57. package/bin/{chunk-mzsw9var.js → chunk-dfbzatq9.js} +2 -2
  58. package/bin/chunk-e0bckx12.js +2 -0
  59. package/bin/{chunk-3ckkwdjx.js → chunk-e2p4s2pj.js} +3 -3
  60. package/bin/chunk-e8zzg57c.js +2 -0
  61. package/bin/{chunk-vp3czaeh.js → chunk-edfaqwaq.js} +2 -2
  62. package/bin/chunk-ehgrp0f9.js +1 -0
  63. package/bin/chunk-evs2aaav.js +12 -0
  64. package/bin/{chunk-s9ksftq2.js → chunk-f5bhcjhw.js} +1 -1
  65. package/bin/{chunk-ns33grgx.js → chunk-fb9dedm2.js} +2 -2
  66. package/bin/{chunk-srxp5jav.js → chunk-ff0mh9p3.js} +32 -32
  67. package/bin/chunk-fpfby5tz.js +1 -0
  68. package/bin/{chunk-eyjfhdp9.js → chunk-gd3b5akr.js} +2 -2
  69. package/bin/{chunk-1wfg56z3.js → chunk-gm1fy3hm.js} +5 -5
  70. package/bin/{chunk-9n1w5dw1.js → chunk-gqkjx7rz.js} +3 -3
  71. package/bin/{chunk-1nvd90g2.js → chunk-h7f5370s.js} +1 -1
  72. package/bin/chunk-hcyhsn8e.js +2 -0
  73. package/bin/{chunk-vcrmjtcz.js → chunk-jxm0gzmf.js} +2 -2
  74. package/bin/{chunk-b7bnp021.js → chunk-k3ggy3xk.js} +2 -2
  75. package/bin/{chunk-257dcd2f.js → chunk-k9vjs21p.js} +2 -2
  76. package/bin/chunk-ka40gv72.js +1 -0
  77. package/bin/chunk-km70p970.js +2 -0
  78. package/bin/{chunk-e4yscwen.js → chunk-m6v8rb39.js} +8 -8
  79. package/bin/chunk-m8spagwp.js +4 -0
  80. package/bin/{chunk-y241cxff.js → chunk-mfh7mg5b.js} +1 -1
  81. package/bin/chunk-n0cn0218.js +2 -0
  82. package/bin/chunk-n54ftsdx.js +2 -0
  83. package/bin/{chunk-02fvcrej.js → chunk-n7cvj15k.js} +7 -7
  84. package/bin/chunk-na5fh7er.js +2 -0
  85. package/bin/chunk-neazdbxy.js +1 -0
  86. package/bin/chunk-nk24qw3m.js +1 -0
  87. package/bin/{chunk-73yncv54.js → chunk-nsbbseff.js} +2 -2
  88. package/bin/chunk-nseef85h.js +1 -0
  89. package/bin/chunk-p35ztj2z.js +3 -0
  90. package/bin/chunk-pcjc9ygv.js +2 -0
  91. package/bin/{chunk-167z646e.js → chunk-pemmtd8q.js} +1 -1
  92. package/bin/{chunk-82tps5n1.js → chunk-pzrqnnpg.js} +1 -1
  93. package/bin/{chunk-a74g6h8a.js → chunk-q43m934j.js} +2 -2
  94. package/bin/{chunk-r1xer8gn.js → chunk-q57m34h0.js} +3 -3
  95. package/bin/chunk-q5k72jz9.js +3 -0
  96. package/bin/chunk-q9fsxakd.js +2 -0
  97. package/bin/chunk-qadpe0ct.js +2 -0
  98. package/bin/chunk-qeypjv4f.css +1 -0
  99. package/bin/chunk-qw92z3wf.js +2 -0
  100. package/bin/chunk-r0byfq1t.js +1 -0
  101. package/bin/chunk-r0q88qw3.js +2 -0
  102. package/bin/chunk-rjhczkh3.js +2 -0
  103. package/bin/{chunk-mwktrwtx.js → chunk-s0wxbybf.js} +1 -1
  104. package/bin/{chunk-q5vkxee3.js → chunk-s4hkcw24.js} +2 -2
  105. package/bin/chunk-szta50g1.js +1 -0
  106. package/bin/chunk-t6cb99gp.js +2 -0
  107. package/bin/chunk-tcr080b9.js +1 -0
  108. package/bin/{chunk-yq32evnn.js → chunk-v4nqx084.js} +5 -5
  109. package/bin/{chunk-ympbfzz4.js → chunk-v7jzc3ye.js} +1 -1
  110. package/bin/{chunk-9zsv6d2f.js → chunk-vbe8wred.js} +1 -1
  111. package/bin/{chunk-hpbn0k9n.js → chunk-vf9s8b5t.js} +2 -2
  112. package/bin/{chunk-m9adtgzk.js → chunk-vk3eqr65.js} +1 -1
  113. package/bin/{chunk-hpwv4am3.js → chunk-vm9bbk97.js} +4 -4
  114. package/bin/{chunk-v05rrxp5.js → chunk-w4gqg27w.js} +2 -2
  115. package/bin/{chunk-x8mjh387.js → chunk-w6ym7tsd.js} +4 -4
  116. package/bin/{chunk-hzkjgcre.js → chunk-ways29d1.js} +1 -1
  117. package/bin/chunk-wesg7tg9.js +1 -0
  118. package/bin/chunk-wmrrapwv.js +2 -0
  119. package/bin/chunk-wnwsjgj8.js +2 -0
  120. package/bin/{chunk-hw6czrya.js → chunk-wrb4xq9v.js} +1 -1
  121. package/bin/{chunk-v5cn90cf.js → chunk-wzrzc07t.js} +2 -2
  122. package/bin/chunk-xd1axsdv.js +2 -0
  123. package/bin/{chunk-cwat1jwx.js → chunk-xk0jmx4s.js} +2 -2
  124. package/bin/{chunk-g6sdezec.js → chunk-y545vfn0.js} +1 -1
  125. package/bin/{chunk-fatmqnp1.js → chunk-y6h885fs.js} +1 -1
  126. package/bin/{chunk-cz13a82m.js → chunk-yncmfr39.js} +2 -2
  127. package/bin/{chunk-01052ssf.js → chunk-yxnz4ypt.js} +11 -11
  128. package/bin/chunk-zc1asygg.js +2 -0
  129. package/bin/{chunk-m82tkhaa.js → chunk-zc7wzcvh.js} +7 -7
  130. package/bin/chunk-zdvhh8mk.js +2 -0
  131. package/bin/{chunk-kvhf12b4.js → chunk-zg0b902x.js} +2 -2
  132. package/bin/{chunk-7v7mypc6.js → chunk-zhq7a7hs.js} +2 -2
  133. package/bin/{chunk-f25fe9th.js → chunk-zmnvskn7.js} +11 -11
  134. package/bin/shumai-app.html +1 -1
  135. package/bin/shumai-app.js +470 -389
  136. package/package.json +7 -7
  137. package/prisma/migrations/20261102032402_add_archive_storage/migration.sql +25 -0
  138. package/prisma/schema.prisma +12 -6
  139. package/bin/chunk-0bj887zm.js +0 -2
  140. package/bin/chunk-1ktk6764.js +0 -10
  141. package/bin/chunk-26qn7fek.js +0 -2
  142. package/bin/chunk-2ne2b54t.js +0 -2
  143. package/bin/chunk-2vbeqczj.js +0 -1
  144. package/bin/chunk-37p6bcx2.js +0 -1
  145. package/bin/chunk-3knfx6dm.js +0 -1
  146. package/bin/chunk-3z85zxkn.js +0 -1
  147. package/bin/chunk-54tbfvya.js +0 -2
  148. package/bin/chunk-56kh5fxc.js +0 -1
  149. package/bin/chunk-576nj64j.js +0 -2
  150. package/bin/chunk-6cnqjngz.js +0 -1
  151. package/bin/chunk-7g46e1wy.js +0 -3
  152. package/bin/chunk-84x8p7v5.js +0 -1
  153. package/bin/chunk-88jg9dq5.js +0 -2
  154. package/bin/chunk-8fyg9dy4.js +0 -2
  155. package/bin/chunk-8gtfgkrn.js +0 -2
  156. package/bin/chunk-8mqbztj8.js +0 -2
  157. package/bin/chunk-8qdtvfx3.css +0 -1
  158. package/bin/chunk-8xb6yz11.js +0 -2
  159. package/bin/chunk-8yjsmr7g.js +0 -2
  160. package/bin/chunk-91rb0jpb.js +0 -2
  161. package/bin/chunk-96qpjght.js +0 -2
  162. package/bin/chunk-9f1xk1nt.js +0 -1
  163. package/bin/chunk-9mk9cvbk.js +0 -1
  164. package/bin/chunk-9nw5w8vh.js +0 -1
  165. package/bin/chunk-ah8fjb6f.js +0 -2
  166. package/bin/chunk-cf5axr8c.js +0 -1
  167. package/bin/chunk-dk42gf7v.js +0 -2
  168. package/bin/chunk-e4f56spj.js +0 -4
  169. package/bin/chunk-eb7mzbfm.js +0 -1
  170. package/bin/chunk-er14d498.js +0 -3
  171. package/bin/chunk-f6avg5qe.js +0 -129
  172. package/bin/chunk-fvx9k1dc.js +0 -2
  173. package/bin/chunk-gtf19jc1.js +0 -2
  174. package/bin/chunk-h1n7jqnw.js +0 -1
  175. package/bin/chunk-hfm82066.js +0 -2
  176. package/bin/chunk-j2s311yc.js +0 -3
  177. package/bin/chunk-kj6h5j5a.js +0 -4
  178. package/bin/chunk-km4gn6b6.js +0 -1
  179. package/bin/chunk-ny1kbmxt.js +0 -2
  180. package/bin/chunk-p3j6ydk1.js +0 -1
  181. package/bin/chunk-pk406dgy.js +0 -2
  182. package/bin/chunk-s1e4qy3q.js +0 -14
  183. package/bin/chunk-swtwaaqe.js +0 -1
  184. package/bin/chunk-tagdrg7p.js +0 -1
  185. package/bin/chunk-twtxnzbv.js +0 -2
  186. package/bin/chunk-v5mcjwxy.js +0 -2
  187. package/bin/chunk-vkzasgxw.js +0 -2
  188. package/bin/chunk-x342vzdx.js +0 -2
  189. package/bin/chunk-xc54vhfz.js +0 -2
  190. package/bin/chunk-xvkwbczx.js +0 -3
  191. package/bin/chunk-z1p13dnc.js +0 -2
  192. package/bin/chunk-zarz3sqn.js +0 -211
@@ -1,7 +1,7 @@
1
1
  // @bun
2
- function So(e){return typeof e>"u"||e===void 0}function dr(e){return typeof e==="string"}function zs(e){return typeof e==="number"}function Vi(e){return typeof e==="boolean"}function Fa(e){return e===null}function rf(e){return e instanceof Date}function La(e){return typeof e==="bigint"}function zg(e){return typeof Buffer<"u"&&Buffer.isBuffer(e)}function fr(e){return typeof e==="function"}function sn(e){return typeof e==="object"&&e!==null}function C(e){return Object.freeze(e)}function xe(e){if(x(e))return e;else return[e]}function x(e){return Array.isArray(e)}function Io(e){return e}var st=C({is(e){return e.kind==="IdentifierNode"},create(e){return C({kind:"IdentifierNode",name:e})}});var jg=["preserve rows","delete rows","drop"],fo=C({is(e){return e.kind==="CreateTableNode"},create(e){return C({kind:"CreateTableNode",table:e,columns:C([])})},cloneWithColumn(e,t){return C({...e,columns:C([...e.columns,t])})},cloneWithConstraint(e,t){return C({...e,constraints:e.constraints?C([...e.constraints,t]):C([t])})},cloneWithFrontModifier(e,t){return C({...e,frontModifiers:e.frontModifiers?C([...e.frontModifiers,t]):C([t])})},cloneWithEndModifier(e,t){return C({...e,endModifiers:e.endModifiers?C([...e.endModifiers,t]):C([t])})},cloneWith(e,t){return C({...e,...t})}});var po=C({is(e){return e.kind==="SchemableIdentifierNode"},create(e){return C({kind:"SchemableIdentifierNode",identifier:st.create(e)})},createWithSchema(e,t){return C({kind:"SchemableIdentifierNode",schema:st.create(e),identifier:st.create(t)})}});var l=C({is(e){return e.kind==="AliasNode"},create(e,t){return C({kind:"AliasNode",node:e,alias:t})}});var mo=C({is(e){return e.kind==="TableNode"},create(e){return C({kind:"TableNode",table:po.create(e)})},createWithSchema(e,t){return C({kind:"TableNode",table:po.createWithSchema(e,t)})}});function Rr(e){return sn(e)&&fr(e.toOperationNode)}function Le(e){return sn(e)&&"expressionType"in e&&Rr(e)}function Ct(e){return sn(e)&&"expression"in e&&dr(e.alias)&&Rr(e)}var I=C({is(e){return e.kind==="SelectModifierNode"},create(e,t){return C({kind:"SelectModifierNode",modifier:e,of:t})},createWithExpression(e){return C({kind:"SelectModifierNode",rawModifier:e})}});var v=C({is(e){return e.kind==="AndNode"},create(e,t){return C({kind:"AndNode",left:e,right:t})}});var T=C({is(e){return e.kind==="OrNode"},create(e,t){return C({kind:"OrNode",left:e,right:t})}});var De=C({is(e){return e.kind==="OnNode"},create(e){return C({kind:"OnNode",on:e})},cloneWithOperation(e,t,r){return C({...e,on:t==="And"?v.create(e.on,r):T.create(e.on,r)})}});var L=C({is(e){return e.kind==="JoinNode"},create(e,t){return C({kind:"JoinNode",joinType:e,table:t,on:void 0})},createWithOn(e,t,r){return C({kind:"JoinNode",joinType:e,table:t,on:De.create(r)})},cloneWithOn(e,t){return C({...e,on:e.on?De.cloneWithOperation(e.on,"And",t):De.create(t)})}});var X=C({is(e){return e.kind==="BinaryOperationNode"},create(e,t,r){return C({kind:"BinaryOperationNode",leftOperand:e,operator:t,rightOperand:r})}});var Lr=["=","==","!=","<>",">",">=","<","<=","in","not in","is","is not","like","not like","match","ilike","not ilike","@>","<@","^@","&&","?","?&","?|","!<","!>","<=>","!~","~","~*","!~*","@@","@@@","!!","<->","regexp","is distinct from","is not distinct from"],Dr=["+","-","*","/","%","^","&","|","#","<<",">>"],kt=["->","->>"],Br=[...Lr,...Dr,"&&","||"],qr=["exists","not exists"],Mr=["not","-",...qr],Et=[...Br,...kt,...Mr,"between","between symmetric"],S=C({is(e){return e.kind==="OperatorNode"},create(e){return C({kind:"OperatorNode",operator:e})}});function Ke(e){return dr(e)&&kt.includes(e)}var Wt=C({is(e){return e.kind==="ColumnNode"},create(e){return C({kind:"ColumnNode",column:st.create(e)})}});var dc=C({is(e){return e.kind==="SelectAllNode"},create(){return C({kind:"SelectAllNode"})}});var Za=C({is(e){return e.kind==="ReferenceNode"},create(e,t){return C({kind:"ReferenceNode",table:t,column:e})},createSelectAll(e){return C({kind:"ReferenceNode",table:e,column:dc.create()})}});class pm{#e;get dynamicReference(){return this.#e}get refType(){return}constructor(e){this.#e=e}toOperationNode(){return He(this.#e)}}function Be(e){return sn(e)&&Rr(e)&&dr(e.dynamicReference)}var D=C({is(e){return e.kind==="OrderByItemNode"},create(e,t){return C({kind:"OrderByItemNode",orderBy:e,direction:t})},cloneWith(e,t){return C({...e,...t})}});var tr=C({is(e){return e.kind==="RawNode"},create(e,t){return C({kind:"RawNode",sqlFragments:C(e),parameters:C(t)})},createWithSql(e){return tr.create([e],[])},createWithChild(e){return tr.create(["",""],[e])},createWithChildren(e){return tr.create(Array(e.length+1).fill(""),e)}});var At=C({is(e){return e.kind==="CollateNode"},create(e){return C({kind:"CollateNode",collation:st.create(e)})}});class Z{#e;constructor(e){this.#e=C(e)}desc(){return new Z({node:D.cloneWith(this.#e.node,{direction:tr.createWithSql("desc")})})}asc(){return new Z({node:D.cloneWith(this.#e.node,{direction:tr.createWithSql("asc")})})}nullsLast(){return new Z({node:D.cloneWith(this.#e.node,{nulls:"last"})})}nullsFirst(){return new Z({node:D.cloneWith(this.#e.node,{nulls:"first"})})}collate(e){return new Z({node:D.cloneWith(this.#e.node,{collation:At.create(e)})})}toOperationNode(){return this.#e.node}}var Rt=new Set;function hi(e){if(Rt.has(e))return;Rt.add(e),console.log(e)}function Xe(e){return e==="asc"||e==="desc"}function k(e){if(e.length===2)return[Ye(e[0],e[1])];if(e.length===1){let[t]=e;if(Array.isArray(t))return hi("orderBy(array) is deprecated, use multiple orderBy calls instead."),t.map((r)=>Ye(r));return[Ye(t)]}throw Error(`Invalid number of arguments at order by! expected 1-2, received ${e.length}`)}function Ye(e,t){let r=Pr(e);if(D.is(r)){if(t)throw Error("Cannot specify direction twice!");return r}return Tt(r,t)}function Pr(e){if(J(e))return Ho(e);if(Be(e))return e.toOperationNode();let[t,r]=e.split(" ");if(r)return hi("`orderBy('column asc')` is deprecated. Use `orderBy('column', 'asc')` instead."),Tt(is(t),r);return is(e)}function Tt(e,t){if(typeof t==="string"){if(!Xe(t))throw Error(`Invalid order by direction: ${t}`);return D.create(e,tr.createWithSql(t))}if(Le(t))return hi("`orderBy(..., expr)` is deprecated. Use `orderBy(..., 'asc')` or `orderBy(..., (ob) => ...)` instead."),D.create(e,t.toOperationNode());let r=D.create(e);if(!t)return r;return t(new Z({node:r})).toOperationNode()}var pe=C({is(e){return e.kind==="JSONReferenceNode"},create(e,t){return C({kind:"JSONReferenceNode",reference:e,traversal:t})},cloneWithTraversal(e,t){return C({...e,traversal:t})}});var qe=C({is(e){return e.kind==="JSONOperatorChainNode"},create(e){return C({kind:"JSONOperatorChainNode",operator:e,values:C([])})},cloneWithValue(e,t){return C({...e,values:C([...e.values,t])})}});var ee=C({is(e){return e.kind==="JSONPathNode"},create(e){return C({kind:"JSONPathNode",inOperator:e,pathLegs:C([])})},cloneWithLeg(e,t){return C({...e,pathLegs:C([...e.pathLegs,t])})}});function He(e){if(dr(e))return is(e);return e.toOperationNode()}function Q(e){if(x(e))return e.map((t)=>w(t));else return[w(e)]}function w(e){if(J(e))return Ho(e);return He(e)}function Lt(e,t){let r=is(e);if(Ke(t))return pe.create(r,qe.create(S.create(t)));let i=t.slice(0,-1);if(Ke(i))return pe.create(r,ee.create(S.create(i)));throw Error(`Invalid JSON operator: ${t}`)}function is(e){if(!e.includes("."))return Za.create(Wt.create(e));let r=e.split(".").map(Ze);if(r.length===3)return Ir(r);if(r.length===2)return Fr(r);throw Error(`invalid column reference ${e}`)}function Dt(e){if(e.includes(" as ")){let[r,i]=e.split(" as ").map(Ze);return l.create(is(r),st.create(i))}else return is(e)}function mm(e){return Wt.create(e)}function fc(e){if(e.includes(" ")){let[r,i]=e.split(" ").map(Ze);if(!Xe(i))throw Error(`invalid order direction "${i}" next to "${r}"`);return k([r,i])[0]}else return mm(e)}function Ir(e){let[t,r,i]=e;return Za.create(Wt.create(i),mo.createWithSchema(t,r))}function Fr(e){let[t,r]=e;return Za.create(Wt.create(r),mo.create(t))}function Ze(e){return e.trim()}var Me=C({is(e){return e.kind==="PrimitiveValueListNode"},create(e){return C({kind:"PrimitiveValueListNode",values:C([...e])})}});var Ba=C({is(e){return e.kind==="ValueListNode"},create(e){return C({kind:"ValueListNode",values:C(e)})}});var _n=C({is(e){return e.kind==="ValueNode"},create(e){return C({kind:"ValueNode",value:e})},createImmediate(e){return C({kind:"ValueNode",value:e,immediate:!0})}});function Bt(e){if(x(e))return Vr(e);return d(e)}function d(e){if(J(e))return Ho(e);return _n.create(e)}function Pe(e){return zs(e)||Vi(e)||Fa(e)}function ge(e){if(!Pe(e))throw Error(`unsafe immediate value ${JSON.stringify(e)}`);return _n.createImmediate(e)}function Vr(e){if(e.some(J))return Ba.create(e.map((t)=>d(t)));return Me.create(e)}var E=C({is(e){return e.kind==="ParensNode"},create(e){return C({kind:"ParensNode",node:e})}});function yr(e){if(e.length===3)return Ie(e[0],e[1],e[2]);else if(e.length===1)return d(e[0]);throw Error(`invalid arguments: ${JSON.stringify(e)}`)}function Ie(e,t,r){if(Ur(t)&&Mt(r))return X.create(w(e),et(t),_n.createImmediate(r));return X.create(w(e),et(t),Bt(r))}function N(e,t,r){return X.create(w(e),et(t),w(r))}function tt(e,t){return de(Object.entries(e).filter(([,r])=>!So(r)).map(([r,i])=>Ie(r,Mt(i)?"is":"=",i)),t)}function de(e,t,r=!0){let i=t==="and"?v.create:T.create;if(e.length===0)return X.create(_n.createImmediate(1),S.create("="),_n.createImmediate(t==="and"?1:0));let n=qt(e[0]);for(let a=1;a<e.length;++a)n=i(n,qt(e[a]));if(e.length>1&&r)return E.create(n);return n}function Ur(e){return e==="is"||e==="is not"}function Mt(e){return Fa(e)||Vi(e)}function et(e){if(dr(e)&&Et.includes(e))return S.create(e);if(Rr(e))return e.toOperationNode();throw Error(`invalid operator ${JSON.stringify(e)}`)}function qt(e){return Rr(e)?e.toOperationNode():e}var _=C({is(e){return e.kind==="OrderByNode"},create(e){return C({kind:"OrderByNode",items:C([...e])})},cloneWithItems(e,t){return C({...e,items:C([...e.items,...t])})}});var rt=C({is(e){return e.kind==="PartitionByNode"},create(e){return C({kind:"PartitionByNode",items:C(e)})},cloneWithItems(e,t){return C({...e,items:C([...e.items,...t])})}});var ve=C({is(e){return e.kind==="OverNode"},create(){return C({kind:"OverNode"})},cloneWithOrderByItems(e,t){return C({...e,orderBy:e.orderBy?_.cloneWithItems(e.orderBy,t):_.create(t)})},cloneWithPartitionByItems(e,t){return C({...e,partitionBy:e.partitionBy?rt.cloneWithItems(e.partitionBy,t):rt.create(t)})}});var oe=C({is(e){return e.kind==="FromNode"},create(e){return C({kind:"FromNode",froms:C(e)})},cloneWithFroms(e,t){return C({...e,froms:C([...e.froms,...t])})}});var it=C({is(e){return e.kind==="GroupByNode"},create(e){return C({kind:"GroupByNode",items:C(e)})},cloneWithItems(e,t){return C({...e,items:C([...e.items,...t])})}});var ot=C({is(e){return e.kind==="HavingNode"},create(e){return C({kind:"HavingNode",having:e})},cloneWithOperation(e,t,r){return C({...e,having:t==="And"?v.create(e.having,r):T.create(e.having,r)})}});var u=C({is(e){return e.kind==="InsertQueryNode"},create(e,t,r){return C({kind:"InsertQueryNode",into:e,...t&&{with:t},replace:r})},createWithoutInto(){return C({kind:"InsertQueryNode"})},cloneWith(e,t){return C({...e,...t})}});var Fe=C({is(e){return e.kind==="ListNode"},create(e){return C({kind:"ListNode",items:C(e)})}});var F=C({is(e){return e.kind==="UpdateQueryNode"},create(e,t){return C({kind:"UpdateQueryNode",table:e.length===1?e[0]:Fe.create(e),...t&&{with:t}})},createWithoutTable(){return C({kind:"UpdateQueryNode"})},cloneWithFromItems(e,t){return C({...e,from:e.from?oe.cloneWithFroms(e.from,t):oe.create(t)})},cloneWithUpdates(e,t){return C({...e,updates:e.updates?C([...e.updates,...t]):t})},cloneWithLimit(e,t){return C({...e,limit:t})}});var be=C({is(e){return e.kind==="UsingNode"},create(e){return C({kind:"UsingNode",tables:C(e)})},cloneWithTables(e,t){return C({...e,tables:C([...e.tables,...t])})}});var te=C({is(e){return e.kind==="DeleteQueryNode"},create(e,t){return C({kind:"DeleteQueryNode",from:oe.create(e),...t&&{with:t}})},cloneWithOrderByItems:(e,t)=>Oe.cloneWithOrderByItems(e,t),cloneWithoutOrderBy:(e)=>Oe.cloneWithoutOrderBy(e),cloneWithLimit(e,t){return C({...e,limit:t})},cloneWithoutLimit(e){return C({...e,limit:void 0})},cloneWithUsing(e,t){return C({...e,using:e.using!==void 0?be.cloneWithTables(e.using,t):be.create(t)})}});var O=C({is(e){return e.kind==="WhereNode"},create(e){return C({kind:"WhereNode",where:e})},cloneWithOperation(e,t,r){return C({...e,where:t==="And"?v.create(e.where,r):T.create(e.where,r)})}});var nt=C({is(e){return e.kind==="ReturningNode"},create(e){return C({kind:"ReturningNode",selections:C(e)})},cloneWithSelections(e,t){return C({...e,selections:e.selections?C([...e.selections,...t]):C(t)})}});var Pt=C({is(e){return e.kind==="ExplainNode"},create(e,t){return C({kind:"ExplainNode",format:e,options:t})}});var R=C({is(e){return e.kind==="WhenNode"},create(e){return C({kind:"WhenNode",condition:e})},cloneWithResult(e,t){return C({...e,result:t})}});var A=C({is(e){return e.kind==="MergeQueryNode"},create(e,t){return C({kind:"MergeQueryNode",into:e,...t&&{with:t}})},cloneWithUsing(e,t){return C({...e,using:t})},cloneWithWhen(e,t){return C({...e,whens:e.whens?C([...e.whens,t]):C([t])})},cloneWithThen(e,t){return C({...e,whens:e.whens?C([...e.whens.slice(0,-1),R.cloneWithResult(e.whens[e.whens.length-1],t)]):void 0})}});var at=C({is(e){return e.kind==="OutputNode"},create(e){return C({kind:"OutputNode",selections:C(e)})},cloneWithSelections(e,t){return C({...e,selections:e.selections?C([...e.selections,...t]):C(t)})}});var Oe=C({is(e){return nr.is(e)||u.is(e)||F.is(e)||te.is(e)||A.is(e)},cloneWithEndModifier(e,t){return C({...e,endModifiers:e.endModifiers?C([...e.endModifiers,t]):C([t])})},cloneWithWhere(e,t){return C({...e,where:e.where?O.cloneWithOperation(e.where,"And",t):O.create(t)})},cloneWithJoin(e,t){return C({...e,joins:e.joins?C([...e.joins,t]):C([t])})},cloneWithReturning(e,t){return C({...e,returning:e.returning?nt.cloneWithSelections(e.returning,t):nt.create(t)})},cloneWithoutReturning(e){return C({...e,returning:void 0})},cloneWithoutWhere(e){return C({...e,where:void 0})},cloneWithExplain(e,t,r){return C({...e,explain:Pt.create(t,r?.toOperationNode())})},cloneWithTop(e,t){return C({...e,top:t})},cloneWithOutput(e,t){return C({...e,output:e.output?at.cloneWithSelections(e.output,t):at.create(t)})},cloneWithOrderByItems(e,t){return C({...e,orderBy:e.orderBy?_.cloneWithItems(e.orderBy,t):_.create(t)})},cloneWithoutOrderBy(e){return C({...e,orderBy:void 0})}});var nr=C({is(e){return e.kind==="SelectQueryNode"},create(e){return C({kind:"SelectQueryNode",...e&&{with:e}})},createFrom(e,t){return C({kind:"SelectQueryNode",from:oe.create(e),...t&&{with:t}})},cloneWithSelections(e,t){return C({...e,selections:e.selections?C([...e.selections,...t]):C(t)})},cloneWithDistinctOn(e,t){return C({...e,distinctOn:e.distinctOn?C([...e.distinctOn,...t]):C(t)})},cloneWithFrontModifier(e,t){return C({...e,frontModifiers:e.frontModifiers?C([...e.frontModifiers,t]):C([t])})},cloneWithOrderByItems:(e,t)=>Oe.cloneWithOrderByItems(e,t),cloneWithGroupByItems(e,t){return C({...e,groupBy:e.groupBy?it.cloneWithItems(e.groupBy,t):it.create(t)})},cloneWithLimit(e,t){return C({...e,limit:t})},cloneWithOffset(e,t){return C({...e,offset:t})},cloneWithFetch(e,t){return C({...e,fetch:t})},cloneWithHaving(e,t){return C({...e,having:e.having?ot.cloneWithOperation(e.having,"And",t):ot.create(t)})},cloneWithSetOperations(e,t){return C({...e,setOperations:e.setOperations?C([...e.setOperations,...t]):C([...t])})},cloneWithoutSelections(e){return C({...e,selections:[]})},cloneWithoutLimit(e){return C({...e,limit:void 0})},cloneWithoutOffset(e){return C({...e,offset:void 0})},cloneWithoutOrderBy:(e)=>Oe.cloneWithoutOrderBy(e),cloneWithoutGroupBy(e){return C({...e,groupBy:void 0})}});class he{#e;constructor(e){this.#e=C(e)}on(...e){return new he({...this.#e,joinNode:L.cloneWithOn(this.#e.joinNode,yr(e))})}onRef(e,t,r){return new he({...this.#e,joinNode:L.cloneWithOn(this.#e.joinNode,N(e,t,r))})}onTrue(){return new he({...this.#e,joinNode:L.cloneWithOn(this.#e.joinNode,tr.createWithSql("true"))})}$call(e){return e(this)}toOperationNode(){return this.#e.joinNode}}var Ft=C({is(e){return e.kind==="PartitionByItemNode"},create(e){return C({kind:"PartitionByItemNode",partitionBy:e})}});function Vt(e){return Q(e).map(Ft.create)}class ue{#e;constructor(e){this.#e=C(e)}orderBy(...e){return new ue({overNode:ve.cloneWithOrderByItems(this.#e.overNode,k(e))})}clearOrderBy(){return new ue({overNode:Oe.cloneWithoutOrderBy(this.#e.overNode)})}partitionBy(e){return new ue({overNode:ve.cloneWithPartitionByItems(this.#e.overNode,Vt(e))})}$call(e){return e(this)}toOperationNode(){return this.#e.overNode}}var me=C({is(e){return e.kind==="SelectionNode"},create(e){return C({kind:"SelectionNode",selection:e})},createSelectAll(){return C({kind:"SelectionNode",selection:dc.create()})},createSelectAllFromTable(e){return C({kind:"SelectionNode",selection:Za.createSelectAll(e)})}});function c(e){if(fr(e))return c(e(B()));else if(x(e))return e.map((t)=>Ut(t));else return[Ut(e)]}function Ut(e){if(dr(e))return me.create(Dt(e));else if(Be(e))return me.create(e.toOperationNode());else return me.create(Ve(e))}function y(e){if(!e)return[me.createSelectAll()];else if(Array.isArray(e))return e.map(zt);else return[zt(e)]}function zt(e){if(dr(e))return me.createSelectAllFromTable(or(e));throw Error(`invalid value selectAll expression: ${JSON.stringify(e)}`)}var Jt=C({is(e){return e.kind==="ValuesNode"},create(e){return C({kind:"ValuesNode",values:C(e)})}});var Qt=C({is(e){return e.kind==="DefaultInsertValueNode"},create(){return C({kind:"DefaultInsertValueNode"})}});function Ue(e){let t=fr(e)?e(B()):e,r=x(t)?t:C([t]);return zr(r)}function zr(e){let t=Jr(e);return[C([...t.keys()].map(Wt.create)),Jt.create(e.map((r)=>Qr(r,t)))]}function Jr(e){let t=new Map;for(let r of e){let i=Object.keys(r);for(let n of i)if(!t.has(n)&&r[n]!==void 0)t.set(n,t.size)}return t}function Qr(e,t){let r=Object.keys(e),i=Array.from({length:t.size}),n=!1,a=r.length;for(let H of r){let Y=t.get(H);if(So(Y)){a--;continue}let we=e[H];if(So(we)||J(we))n=!0;i[Y]=we}if(a<t.size||n){let H=Qt.create();return Ba.create(i.map((Y)=>So(Y)?H:d(Y)))}return Me.create(i)}var pt=C({is(e){return e.kind==="ColumnUpdateNode"},create(e,t){return C({kind:"ColumnUpdateNode",column:e,value:t})}});function _t(...e){if(e.length===2)return[pt.create(w(e[0]),d(e[1]))];return We(e[0])}function We(e){let t=fr(e)?e(B()):e;return Object.entries(t).filter(([r,i])=>i!==void 0).map(([r,i])=>pt.create(Wt.create(r),d(i)))}var $t=C({is(e){return e.kind==="OnDuplicateKeyNode"},create(e){return C({kind:"OnDuplicateKeyNode",updates:e})}});class dt{insertId;numInsertedOrUpdatedRows;constructor(e,t){this.insertId=e,this.numInsertedOrUpdatedRows=t}}class q extends Error{node;constructor(e){super("no result");this.node=e}}function V(e){return Object.prototype.hasOwnProperty.call(e,"prototype")}var g=C({is(e){return e.kind==="OnConflictNode"},create(){return C({kind:"OnConflictNode"})},cloneWith(e,t){return C({...e,...t})},cloneWithIndexWhere(e,t){return C({...e,indexWhere:e.indexWhere?O.cloneWithOperation(e.indexWhere,"And",t):O.create(t)})},cloneWithIndexOrWhere(e,t){return C({...e,indexWhere:e.indexWhere?O.cloneWithOperation(e.indexWhere,"Or",t):O.create(t)})},cloneWithUpdateWhere(e,t){return C({...e,updateWhere:e.updateWhere?O.cloneWithOperation(e.updateWhere,"And",t):O.create(t)})},cloneWithUpdateOrWhere(e,t){return C({...e,updateWhere:e.updateWhere?O.cloneWithOperation(e.updateWhere,"Or",t):O.create(t)})},cloneWithoutIndexWhere(e){return C({...e,indexWhere:void 0})},cloneWithoutUpdateWhere(e){return C({...e,updateWhere:void 0})}});class U{#e;constructor(e){this.#e=C(e)}column(e){let t=Wt.create(e);return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{columns:this.#e.onConflictNode.columns?C([...this.#e.onConflictNode.columns,t]):C([t])})})}columns(e){let t=e.map(Wt.create);return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{columns:this.#e.onConflictNode.columns?C([...this.#e.onConflictNode.columns,...t]):C(t)})})}constraint(e){return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{constraint:st.create(e)})})}expression(e){return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{indexExpression:e.toOperationNode()})})}where(...e){return new U({...this.#e,onConflictNode:g.cloneWithIndexWhere(this.#e.onConflictNode,yr(e))})}whereRef(e,t,r){return new U({...this.#e,onConflictNode:g.cloneWithIndexWhere(this.#e.onConflictNode,N(e,t,r))})}clearWhere(){return new U({...this.#e,onConflictNode:g.cloneWithoutIndexWhere(this.#e.onConflictNode)})}doNothing(){return new Gt({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{doNothing:!0})})}doUpdateSet(e){return new Se({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{updates:We(e)})})}$call(e){return e(this)}}class Gt{#e;constructor(e){this.#e=C(e)}toOperationNode(){return this.#e.onConflictNode}}class Se{#e;constructor(e){this.#e=C(e)}where(...e){return new Se({...this.#e,onConflictNode:g.cloneWithUpdateWhere(this.#e.onConflictNode,yr(e))})}whereRef(e,t,r){return new Se({...this.#e,onConflictNode:g.cloneWithUpdateWhere(this.#e.onConflictNode,N(e,t,r))})}clearWhere(){return new Se({...this.#e,onConflictNode:g.cloneWithoutUpdateWhere(this.#e.onConflictNode)})}$call(e){return e(this)}toOperationNode(){return this.#e.onConflictNode}}var Kt=C({is(e){return e.kind==="TopNode"},create(e,t){return C({kind:"TopNode",expression:e,modifiers:t})}});function M(e,t){if(!zs(e)&&!La(e))throw Error(`Invalid top expression: ${e}`);if(!So(t)&&!_r(t))throw Error(`Invalid top modifiers: ${t}`);return Kt.create(e,t)}function _r(e){return e==="percent"||e==="with ties"||e==="percent with ties"}var ne=C({is(e){return e.kind==="OrActionNode"},create(e){return C({kind:"OrActionNode",action:e})}});class h{#e;constructor(e){this.#e=C(e)}values(e){let[t,r]=Ue(e);return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{columns:t,values:r})})}columns(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{columns:C(e.map(Wt.create))})})}expression(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{values:Ho(e)})})}defaultValues(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{defaultValues:!0})})}modifyEnd(e){return new h({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}ignore(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("ignore")})})}orIgnore(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("ignore")})})}orAbort(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("abort")})})}orFail(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("fail")})})}orReplace(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("replace")})})}orRollback(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("rollback")})})}top(e,t){return new h({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}onConflict(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{onConflict:e(new U({onConflictNode:g.create()})).toOperationNode()})})}onDuplicateKeyUpdate(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{onDuplicateKey:$t.create(We(e))})})}returning(e){return new h({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(){return new h({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y())})}output(e){return new h({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new h({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}clearReturning(){return new h({...this.#e,queryNode:Oe.cloneWithoutReturning(this.#e.queryNode)})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new h({...this.#e})}$castTo(){return new h(this.#e)}$narrowType(){return new h(this.#e)}$assertType(){return new h(this.#e)}withPlugin(e){return new h({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new dt(t.insertId,t.numAffectedRows??BigInt(0))]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new h({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}class ht{numDeletedRows;constructor(e){this.numDeletedRows=e}}var fe=C({is(e){return e.kind==="LimitNode"},create(e){return C({kind:"LimitNode",limit:e})}});var m;class ze{#e;constructor(e){this.#e=C(e)}where(...e){return new m({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,yr(e))})}whereRef(e,t,r){return new m({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,N(e,t,r))})}clearWhere(){return new m({...this.#e,queryNode:Oe.cloneWithoutWhere(this.#e.queryNode)})}top(e,t){return new m({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}using(e){return new m({...this.#e,queryNode:te.cloneWithUsing(this.#e.queryNode,P(e))})}innerJoin(...e){return this.#t("InnerJoin",e)}leftJoin(...e){return this.#t("LeftJoin",e)}rightJoin(...e){return this.#t("RightJoin",e)}fullJoin(...e){return this.#t("FullJoin",e)}#t(e,t){return new m({...this.#e,queryNode:Oe.cloneWithJoin(this.#e.queryNode,re(e,t))})}returning(e){return new m({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new m({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new m({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new m({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}clearReturning(){return new m({...this.#e,queryNode:Oe.cloneWithoutReturning(this.#e.queryNode)})}clearLimit(){return new m({...this.#e,queryNode:te.cloneWithoutLimit(this.#e.queryNode)})}orderBy(...e){return new m({...this.#e,queryNode:Oe.cloneWithOrderByItems(this.#e.queryNode,k(e))})}clearOrderBy(){return new m({...this.#e,queryNode:Oe.cloneWithoutOrderBy(this.#e.queryNode)})}limit(e){return new m({...this.#e,queryNode:te.cloneWithLimit(this.#e.queryNode,fe.create(d(e)))})}modifyEnd(e){return new m({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new m({...this.#e})}$castTo(){return new m(this.#e)}$narrowType(){return new m(this.#e)}$assertType(){return new m(this.#e)}withPlugin(e){return new m({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new ht(t.numAffectedRows??BigInt(0))]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new m({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}m=ze;class ut{numUpdatedRows;numChangedRows;constructor(e,t){this.numUpdatedRows=e,this.numChangedRows=t}}var f;class ce{#e;constructor(e){this.#e=C(e)}where(...e){return new f({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,yr(e))})}whereRef(e,t,r){return new f({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,N(e,t,r))})}clearWhere(){return new f({...this.#e,queryNode:Oe.cloneWithoutWhere(this.#e.queryNode)})}top(e,t){return new f({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}from(e){return new f({...this.#e,queryNode:F.cloneWithFromItems(this.#e.queryNode,P(e))})}innerJoin(...e){return this.#t("InnerJoin",e)}leftJoin(...e){return this.#t("LeftJoin",e)}rightJoin(...e){return this.#t("RightJoin",e)}fullJoin(...e){return this.#t("FullJoin",e)}#t(e,t){return new f({...this.#e,queryNode:Oe.cloneWithJoin(this.#e.queryNode,re(e,t))})}orderBy(...e){return new f({...this.#e,queryNode:Oe.cloneWithOrderByItems(this.#e.queryNode,k(e))})}clearOrderBy(){return new f({...this.#e,queryNode:Oe.cloneWithoutOrderBy(this.#e.queryNode)})}limit(e){return new f({...this.#e,queryNode:F.cloneWithLimit(this.#e.queryNode,fe.create(d(e)))})}set(...e){return new f({...this.#e,queryNode:F.cloneWithUpdates(this.#e.queryNode,_t(...e))})}returning(e){return new f({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new f({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new f({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new f({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}modifyEnd(e){return new f({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}clearReturning(){return new f({...this.#e,queryNode:Oe.cloneWithoutReturning(this.#e.queryNode)})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new f({...this.#e})}$castTo(){return new f(this.#e)}$narrowType(){return new f(this.#e)}$assertType(){return new f(this.#e)}withPlugin(e){return new f({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new ut(t.numAffectedRows??BigInt(0),t.numChangedRows)]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new f({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}f=ce;var mt=C({is(e){return e.kind==="CommonTableExpressionNameNode"},create(e,t){return C({kind:"CommonTableExpressionNameNode",table:mo.create(e),columns:t?C(t.map(Wt.create)):void 0})}});var le=C({is(e){return e.kind==="CommonTableExpressionNode"},create(e,t){return C({kind:"CommonTableExpressionNode",name:e,expression:t})},cloneWith(e,t){return C({...e,...t})}});class Ce{#e;constructor(e){this.#e=C(e)}materialized(){return new Ce({...this.#e,node:le.cloneWith(this.#e.node,{materialized:!0})})}notMaterialized(){return new Ce({...this.#e,node:le.cloneWith(this.#e.node,{materialized:!1})})}toOperationNode(){return this.#e.node}}function ft(e,t){let r=t(Ht()).toOperationNode();if(fr(e))return e($r(r)).toOperationNode();return le.create(jt(e),r)}function $r(e){return(t)=>new Ce({node:le.create(jt(t),e)})}function jt(e){if(e.includes("(")){let t=e.split(/[\(\)]/),r=t[0],i=t[1].split(",").map((n)=>n.trim());return mt.create(r,i)}else return mt.create(e)}var ke=C({is(e){return e.kind==="WithNode"},create(e,t){return C({kind:"WithNode",expressions:C([e]),...t})},cloneWithExpression(e,t){return C({...e,expressions:C([...e.expressions,t])})}});var Yt=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"];function nf(e){let t="";for(let r=0;r<e;++r)t+=Gr();return t}function Gr(){return Yt[~~(Math.random()*Yt.length)]}function It(){return new Xt}class Xt{#e;get queryId(){if(this.#e===void 0)this.#e=nf(8);return this.#e}}function o(e){return e}class Du{nodeStack=[];#e=C({AliasNode:this.transformAlias.bind(this),ColumnNode:this.transformColumn.bind(this),IdentifierNode:this.transformIdentifier.bind(this),SchemableIdentifierNode:this.transformSchemableIdentifier.bind(this),RawNode:this.transformRaw.bind(this),ReferenceNode:this.transformReference.bind(this),SelectQueryNode:this.transformSelectQuery.bind(this),SelectionNode:this.transformSelection.bind(this),TableNode:this.transformTable.bind(this),FromNode:this.transformFrom.bind(this),SelectAllNode:this.transformSelectAll.bind(this),AndNode:this.transformAnd.bind(this),OrNode:this.transformOr.bind(this),ValueNode:this.transformValue.bind(this),ValueListNode:this.transformValueList.bind(this),PrimitiveValueListNode:this.transformPrimitiveValueList.bind(this),ParensNode:this.transformParens.bind(this),JoinNode:this.transformJoin.bind(this),OperatorNode:this.transformOperator.bind(this),WhereNode:this.transformWhere.bind(this),InsertQueryNode:this.transformInsertQuery.bind(this),DeleteQueryNode:this.transformDeleteQuery.bind(this),ReturningNode:this.transformReturning.bind(this),CreateTableNode:this.transformCreateTable.bind(this),AddColumnNode:this.transformAddColumn.bind(this),ColumnDefinitionNode:this.transformColumnDefinition.bind(this),DropTableNode:this.transformDropTable.bind(this),DataTypeNode:this.transformDataType.bind(this),OrderByNode:this.transformOrderBy.bind(this),OrderByItemNode:this.transformOrderByItem.bind(this),GroupByNode:this.transformGroupBy.bind(this),GroupByItemNode:this.transformGroupByItem.bind(this),UpdateQueryNode:this.transformUpdateQuery.bind(this),ColumnUpdateNode:this.transformColumnUpdate.bind(this),LimitNode:this.transformLimit.bind(this),OffsetNode:this.transformOffset.bind(this),OnConflictNode:this.transformOnConflict.bind(this),OnDuplicateKeyNode:this.transformOnDuplicateKey.bind(this),CreateIndexNode:this.transformCreateIndex.bind(this),DropIndexNode:this.transformDropIndex.bind(this),ListNode:this.transformList.bind(this),PrimaryKeyConstraintNode:this.transformPrimaryKeyConstraint.bind(this),UniqueConstraintNode:this.transformUniqueConstraint.bind(this),ReferencesNode:this.transformReferences.bind(this),CheckConstraintNode:this.transformCheckConstraint.bind(this),WithNode:this.transformWith.bind(this),CommonTableExpressionNode:this.transformCommonTableExpression.bind(this),CommonTableExpressionNameNode:this.transformCommonTableExpressionName.bind(this),HavingNode:this.transformHaving.bind(this),CreateSchemaNode:this.transformCreateSchema.bind(this),DropSchemaNode:this.transformDropSchema.bind(this),AlterTableNode:this.transformAlterTable.bind(this),DropColumnNode:this.transformDropColumn.bind(this),RenameColumnNode:this.transformRenameColumn.bind(this),AlterColumnNode:this.transformAlterColumn.bind(this),ModifyColumnNode:this.transformModifyColumn.bind(this),AddConstraintNode:this.transformAddConstraint.bind(this),DropConstraintNode:this.transformDropConstraint.bind(this),RenameConstraintNode:this.transformRenameConstraint.bind(this),ForeignKeyConstraintNode:this.transformForeignKeyConstraint.bind(this),CreateViewNode:this.transformCreateView.bind(this),RefreshMaterializedViewNode:this.transformRefreshMaterializedView.bind(this),DropViewNode:this.transformDropView.bind(this),GeneratedNode:this.transformGenerated.bind(this),DefaultValueNode:this.transformDefaultValue.bind(this),OnNode:this.transformOn.bind(this),ValuesNode:this.transformValues.bind(this),SelectModifierNode:this.transformSelectModifier.bind(this),CreateTypeNode:this.transformCreateType.bind(this),DropTypeNode:this.transformDropType.bind(this),ExplainNode:this.transformExplain.bind(this),DefaultInsertValueNode:this.transformDefaultInsertValue.bind(this),AggregateFunctionNode:this.transformAggregateFunction.bind(this),OverNode:this.transformOver.bind(this),PartitionByNode:this.transformPartitionBy.bind(this),PartitionByItemNode:this.transformPartitionByItem.bind(this),SetOperationNode:this.transformSetOperation.bind(this),BinaryOperationNode:this.transformBinaryOperation.bind(this),UnaryOperationNode:this.transformUnaryOperation.bind(this),UsingNode:this.transformUsing.bind(this),FunctionNode:this.transformFunction.bind(this),CaseNode:this.transformCase.bind(this),WhenNode:this.transformWhen.bind(this),JSONReferenceNode:this.transformJSONReference.bind(this),JSONPathNode:this.transformJSONPath.bind(this),JSONPathLegNode:this.transformJSONPathLeg.bind(this),JSONOperatorChainNode:this.transformJSONOperatorChain.bind(this),TupleNode:this.transformTuple.bind(this),MergeQueryNode:this.transformMergeQuery.bind(this),MatchedNode:this.transformMatched.bind(this),AddIndexNode:this.transformAddIndex.bind(this),CastNode:this.transformCast.bind(this),FetchNode:this.transformFetch.bind(this),TopNode:this.transformTop.bind(this),OutputNode:this.transformOutput.bind(this),OrActionNode:this.transformOrAction.bind(this),CollateNode:this.transformCollate.bind(this)});transformNode(e,t){if(!e)return e;this.nodeStack.push(e);let r=this.transformNodeImpl(e,t);return this.nodeStack.pop(),C(r)}transformNodeImpl(e,t){return this.#e[e.kind](e,t)}transformNodeList(e,t){if(!e)return e;return C(e.map((r)=>this.transformNode(r,t)))}transformSelectQuery(e,t){return o({kind:"SelectQueryNode",from:this.transformNode(e.from,t),selections:this.transformNodeList(e.selections,t),distinctOn:this.transformNodeList(e.distinctOn,t),joins:this.transformNodeList(e.joins,t),groupBy:this.transformNode(e.groupBy,t),orderBy:this.transformNode(e.orderBy,t),where:this.transformNode(e.where,t),frontModifiers:this.transformNodeList(e.frontModifiers,t),endModifiers:this.transformNodeList(e.endModifiers,t),limit:this.transformNode(e.limit,t),offset:this.transformNode(e.offset,t),with:this.transformNode(e.with,t),having:this.transformNode(e.having,t),explain:this.transformNode(e.explain,t),setOperations:this.transformNodeList(e.setOperations,t),fetch:this.transformNode(e.fetch,t),top:this.transformNode(e.top,t)})}transformSelection(e,t){return o({kind:"SelectionNode",selection:this.transformNode(e.selection,t)})}transformColumn(e,t){return o({kind:"ColumnNode",column:this.transformNode(e.column,t)})}transformAlias(e,t){return o({kind:"AliasNode",node:this.transformNode(e.node,t),alias:this.transformNode(e.alias,t)})}transformTable(e,t){return o({kind:"TableNode",table:this.transformNode(e.table,t)})}transformFrom(e,t){return o({kind:"FromNode",froms:this.transformNodeList(e.froms,t)})}transformReference(e,t){return o({kind:"ReferenceNode",column:this.transformNode(e.column,t),table:this.transformNode(e.table,t)})}transformAnd(e,t){return o({kind:"AndNode",left:this.transformNode(e.left,t),right:this.transformNode(e.right,t)})}transformOr(e,t){return o({kind:"OrNode",left:this.transformNode(e.left,t),right:this.transformNode(e.right,t)})}transformValueList(e,t){return o({kind:"ValueListNode",values:this.transformNodeList(e.values,t)})}transformParens(e,t){return o({kind:"ParensNode",node:this.transformNode(e.node,t)})}transformJoin(e,t){return o({kind:"JoinNode",joinType:e.joinType,table:this.transformNode(e.table,t),on:this.transformNode(e.on,t)})}transformRaw(e,t){return o({kind:"RawNode",sqlFragments:C([...e.sqlFragments]),parameters:this.transformNodeList(e.parameters,t)})}transformWhere(e,t){return o({kind:"WhereNode",where:this.transformNode(e.where,t)})}transformInsertQuery(e,t){return o({kind:"InsertQueryNode",into:this.transformNode(e.into,t),columns:this.transformNodeList(e.columns,t),values:this.transformNode(e.values,t),returning:this.transformNode(e.returning,t),onConflict:this.transformNode(e.onConflict,t),onDuplicateKey:this.transformNode(e.onDuplicateKey,t),endModifiers:this.transformNodeList(e.endModifiers,t),with:this.transformNode(e.with,t),ignore:e.ignore,orAction:this.transformNode(e.orAction,t),replace:e.replace,explain:this.transformNode(e.explain,t),defaultValues:e.defaultValues,top:this.transformNode(e.top,t),output:this.transformNode(e.output,t)})}transformValues(e,t){return o({kind:"ValuesNode",values:this.transformNodeList(e.values,t)})}transformDeleteQuery(e,t){return o({kind:"DeleteQueryNode",from:this.transformNode(e.from,t),using:this.transformNode(e.using,t),joins:this.transformNodeList(e.joins,t),where:this.transformNode(e.where,t),returning:this.transformNode(e.returning,t),endModifiers:this.transformNodeList(e.endModifiers,t),with:this.transformNode(e.with,t),orderBy:this.transformNode(e.orderBy,t),limit:this.transformNode(e.limit,t),explain:this.transformNode(e.explain,t),top:this.transformNode(e.top,t),output:this.transformNode(e.output,t)})}transformReturning(e,t){return o({kind:"ReturningNode",selections:this.transformNodeList(e.selections,t)})}transformCreateTable(e,t){return o({kind:"CreateTableNode",table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t),constraints:this.transformNodeList(e.constraints,t),temporary:e.temporary,ifNotExists:e.ifNotExists,onCommit:e.onCommit,frontModifiers:this.transformNodeList(e.frontModifiers,t),endModifiers:this.transformNodeList(e.endModifiers,t),selectQuery:this.transformNode(e.selectQuery,t)})}transformColumnDefinition(e,t){return o({kind:"ColumnDefinitionNode",column:this.transformNode(e.column,t),dataType:this.transformNode(e.dataType,t),references:this.transformNode(e.references,t),primaryKey:e.primaryKey,autoIncrement:e.autoIncrement,unique:e.unique,notNull:e.notNull,unsigned:e.unsigned,defaultTo:this.transformNode(e.defaultTo,t),check:this.transformNode(e.check,t),generated:this.transformNode(e.generated,t),frontModifiers:this.transformNodeList(e.frontModifiers,t),endModifiers:this.transformNodeList(e.endModifiers,t),nullsNotDistinct:e.nullsNotDistinct,identity:e.identity,ifNotExists:e.ifNotExists})}transformAddColumn(e,t){return o({kind:"AddColumnNode",column:this.transformNode(e.column,t)})}transformDropTable(e,t){return o({kind:"DropTableNode",table:this.transformNode(e.table,t),ifExists:e.ifExists,cascade:e.cascade})}transformOrderBy(e,t){return o({kind:"OrderByNode",items:this.transformNodeList(e.items,t)})}transformOrderByItem(e,t){return o({kind:"OrderByItemNode",orderBy:this.transformNode(e.orderBy,t),direction:this.transformNode(e.direction,t),collation:this.transformNode(e.collation,t),nulls:e.nulls})}transformGroupBy(e,t){return o({kind:"GroupByNode",items:this.transformNodeList(e.items,t)})}transformGroupByItem(e,t){return o({kind:"GroupByItemNode",groupBy:this.transformNode(e.groupBy,t)})}transformUpdateQuery(e,t){return o({kind:"UpdateQueryNode",table:this.transformNode(e.table,t),from:this.transformNode(e.from,t),joins:this.transformNodeList(e.joins,t),where:this.transformNode(e.where,t),updates:this.transformNodeList(e.updates,t),returning:this.transformNode(e.returning,t),endModifiers:this.transformNodeList(e.endModifiers,t),with:this.transformNode(e.with,t),explain:this.transformNode(e.explain,t),limit:this.transformNode(e.limit,t),top:this.transformNode(e.top,t),output:this.transformNode(e.output,t),orderBy:this.transformNode(e.orderBy,t)})}transformColumnUpdate(e,t){return o({kind:"ColumnUpdateNode",column:this.transformNode(e.column,t),value:this.transformNode(e.value,t)})}transformLimit(e,t){return o({kind:"LimitNode",limit:this.transformNode(e.limit,t)})}transformOffset(e,t){return o({kind:"OffsetNode",offset:this.transformNode(e.offset,t)})}transformOnConflict(e,t){return o({kind:"OnConflictNode",columns:this.transformNodeList(e.columns,t),constraint:this.transformNode(e.constraint,t),indexExpression:this.transformNode(e.indexExpression,t),indexWhere:this.transformNode(e.indexWhere,t),updates:this.transformNodeList(e.updates,t),updateWhere:this.transformNode(e.updateWhere,t),doNothing:e.doNothing})}transformOnDuplicateKey(e,t){return o({kind:"OnDuplicateKeyNode",updates:this.transformNodeList(e.updates,t)})}transformCreateIndex(e,t){return o({kind:"CreateIndexNode",name:this.transformNode(e.name,t),table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t),unique:e.unique,using:this.transformNode(e.using,t),ifNotExists:e.ifNotExists,where:this.transformNode(e.where,t),nullsNotDistinct:e.nullsNotDistinct})}transformList(e,t){return o({kind:"ListNode",items:this.transformNodeList(e.items,t)})}transformDropIndex(e,t){return o({kind:"DropIndexNode",name:this.transformNode(e.name,t),table:this.transformNode(e.table,t),ifExists:e.ifExists,cascade:e.cascade})}transformPrimaryKeyConstraint(e,t){return o({kind:"PrimaryKeyConstraintNode",columns:this.transformNodeList(e.columns,t),name:this.transformNode(e.name,t),deferrable:e.deferrable,initiallyDeferred:e.initiallyDeferred})}transformUniqueConstraint(e,t){return o({kind:"UniqueConstraintNode",columns:this.transformNodeList(e.columns,t),name:this.transformNode(e.name,t),nullsNotDistinct:e.nullsNotDistinct,deferrable:e.deferrable,initiallyDeferred:e.initiallyDeferred})}transformForeignKeyConstraint(e,t){return o({kind:"ForeignKeyConstraintNode",columns:this.transformNodeList(e.columns,t),references:this.transformNode(e.references,t),name:this.transformNode(e.name,t),onDelete:e.onDelete,onUpdate:e.onUpdate,deferrable:e.deferrable,initiallyDeferred:e.initiallyDeferred})}transformSetOperation(e,t){return o({kind:"SetOperationNode",operator:e.operator,expression:this.transformNode(e.expression,t),all:e.all})}transformReferences(e,t){return o({kind:"ReferencesNode",table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t),onDelete:e.onDelete,onUpdate:e.onUpdate})}transformCheckConstraint(e,t){return o({kind:"CheckConstraintNode",expression:this.transformNode(e.expression,t),name:this.transformNode(e.name,t)})}transformWith(e,t){return o({kind:"WithNode",expressions:this.transformNodeList(e.expressions,t),recursive:e.recursive})}transformCommonTableExpression(e,t){return o({kind:"CommonTableExpressionNode",name:this.transformNode(e.name,t),materialized:e.materialized,expression:this.transformNode(e.expression,t)})}transformCommonTableExpressionName(e,t){return o({kind:"CommonTableExpressionNameNode",table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t)})}transformHaving(e,t){return o({kind:"HavingNode",having:this.transformNode(e.having,t)})}transformCreateSchema(e,t){return o({kind:"CreateSchemaNode",schema:this.transformNode(e.schema,t),ifNotExists:e.ifNotExists})}transformDropSchema(e,t){return o({kind:"DropSchemaNode",schema:this.transformNode(e.schema,t),ifExists:e.ifExists,cascade:e.cascade})}transformAlterTable(e,t){return o({kind:"AlterTableNode",table:this.transformNode(e.table,t),renameTo:this.transformNode(e.renameTo,t),setSchema:this.transformNode(e.setSchema,t),columnAlterations:this.transformNodeList(e.columnAlterations,t),addConstraint:this.transformNode(e.addConstraint,t),dropConstraint:this.transformNode(e.dropConstraint,t),renameConstraint:this.transformNode(e.renameConstraint,t),addIndex:this.transformNode(e.addIndex,t),dropIndex:this.transformNode(e.dropIndex,t)})}transformDropColumn(e,t){return o({kind:"DropColumnNode",column:this.transformNode(e.column,t)})}transformRenameColumn(e,t){return o({kind:"RenameColumnNode",column:this.transformNode(e.column,t),renameTo:this.transformNode(e.renameTo,t)})}transformAlterColumn(e,t){return o({kind:"AlterColumnNode",column:this.transformNode(e.column,t),dataType:this.transformNode(e.dataType,t),dataTypeExpression:this.transformNode(e.dataTypeExpression,t),setDefault:this.transformNode(e.setDefault,t),dropDefault:e.dropDefault,setNotNull:e.setNotNull,dropNotNull:e.dropNotNull})}transformModifyColumn(e,t){return o({kind:"ModifyColumnNode",column:this.transformNode(e.column,t)})}transformAddConstraint(e,t){return o({kind:"AddConstraintNode",constraint:this.transformNode(e.constraint,t)})}transformDropConstraint(e,t){return o({kind:"DropConstraintNode",constraintName:this.transformNode(e.constraintName,t),ifExists:e.ifExists,modifier:e.modifier})}transformRenameConstraint(e,t){return o({kind:"RenameConstraintNode",oldName:this.transformNode(e.oldName,t),newName:this.transformNode(e.newName,t)})}transformCreateView(e,t){return o({kind:"CreateViewNode",name:this.transformNode(e.name,t),temporary:e.temporary,orReplace:e.orReplace,ifNotExists:e.ifNotExists,materialized:e.materialized,columns:this.transformNodeList(e.columns,t),as:this.transformNode(e.as,t)})}transformRefreshMaterializedView(e,t){return o({kind:"RefreshMaterializedViewNode",name:this.transformNode(e.name,t),concurrently:e.concurrently,withNoData:e.withNoData})}transformDropView(e,t){return o({kind:"DropViewNode",name:this.transformNode(e.name,t),ifExists:e.ifExists,materialized:e.materialized,cascade:e.cascade})}transformGenerated(e,t){return o({kind:"GeneratedNode",byDefault:e.byDefault,always:e.always,identity:e.identity,stored:e.stored,expression:this.transformNode(e.expression,t)})}transformDefaultValue(e,t){return o({kind:"DefaultValueNode",defaultValue:this.transformNode(e.defaultValue,t)})}transformOn(e,t){return o({kind:"OnNode",on:this.transformNode(e.on,t)})}transformSelectModifier(e,t){return o({kind:"SelectModifierNode",modifier:e.modifier,rawModifier:this.transformNode(e.rawModifier,t),of:this.transformNodeList(e.of,t)})}transformCreateType(e,t){return o({kind:"CreateTypeNode",name:this.transformNode(e.name,t),enum:this.transformNode(e.enum,t)})}transformDropType(e,t){return o({kind:"DropTypeNode",name:this.transformNode(e.name,t),ifExists:e.ifExists})}transformExplain(e,t){return o({kind:"ExplainNode",format:e.format,options:this.transformNode(e.options,t)})}transformSchemableIdentifier(e,t){return o({kind:"SchemableIdentifierNode",schema:this.transformNode(e.schema,t),identifier:this.transformNode(e.identifier,t)})}transformAggregateFunction(e,t){return o({kind:"AggregateFunctionNode",func:e.func,aggregated:this.transformNodeList(e.aggregated,t),distinct:e.distinct,orderBy:this.transformNode(e.orderBy,t),withinGroup:this.transformNode(e.withinGroup,t),filter:this.transformNode(e.filter,t),over:this.transformNode(e.over,t)})}transformOver(e,t){return o({kind:"OverNode",orderBy:this.transformNode(e.orderBy,t),partitionBy:this.transformNode(e.partitionBy,t)})}transformPartitionBy(e,t){return o({kind:"PartitionByNode",items:this.transformNodeList(e.items,t)})}transformPartitionByItem(e,t){return o({kind:"PartitionByItemNode",partitionBy:this.transformNode(e.partitionBy,t)})}transformBinaryOperation(e,t){return o({kind:"BinaryOperationNode",leftOperand:this.transformNode(e.leftOperand,t),operator:this.transformNode(e.operator,t),rightOperand:this.transformNode(e.rightOperand,t)})}transformUnaryOperation(e,t){return o({kind:"UnaryOperationNode",operator:this.transformNode(e.operator,t),operand:this.transformNode(e.operand,t)})}transformUsing(e,t){return o({kind:"UsingNode",tables:this.transformNodeList(e.tables,t)})}transformFunction(e,t){return o({kind:"FunctionNode",func:e.func,arguments:this.transformNodeList(e.arguments,t)})}transformCase(e,t){return o({kind:"CaseNode",value:this.transformNode(e.value,t),when:this.transformNodeList(e.when,t),else:this.transformNode(e.else,t),isStatement:e.isStatement})}transformWhen(e,t){return o({kind:"WhenNode",condition:this.transformNode(e.condition,t),result:this.transformNode(e.result,t)})}transformJSONReference(e,t){return o({kind:"JSONReferenceNode",reference:this.transformNode(e.reference,t),traversal:this.transformNode(e.traversal,t)})}transformJSONPath(e,t){return o({kind:"JSONPathNode",inOperator:this.transformNode(e.inOperator,t),pathLegs:this.transformNodeList(e.pathLegs,t)})}transformJSONPathLeg(e,t){return o({kind:"JSONPathLegNode",type:e.type,value:e.value})}transformJSONOperatorChain(e,t){return o({kind:"JSONOperatorChainNode",operator:this.transformNode(e.operator,t),values:this.transformNodeList(e.values,t)})}transformTuple(e,t){return o({kind:"TupleNode",values:this.transformNodeList(e.values,t)})}transformMergeQuery(e,t){return o({kind:"MergeQueryNode",into:this.transformNode(e.into,t),using:this.transformNode(e.using,t),whens:this.transformNodeList(e.whens,t),with:this.transformNode(e.with,t),top:this.transformNode(e.top,t),endModifiers:this.transformNodeList(e.endModifiers,t),output:this.transformNode(e.output,t),returning:this.transformNode(e.returning,t)})}transformMatched(e,t){return o({kind:"MatchedNode",not:e.not,bySource:e.bySource})}transformAddIndex(e,t){return o({kind:"AddIndexNode",name:this.transformNode(e.name,t),columns:this.transformNodeList(e.columns,t),unique:e.unique,using:this.transformNode(e.using,t),ifNotExists:e.ifNotExists})}transformCast(e,t){return o({kind:"CastNode",expression:this.transformNode(e.expression,t),dataType:this.transformNode(e.dataType,t)})}transformFetch(e,t){return o({kind:"FetchNode",rowCount:this.transformNode(e.rowCount,t),modifier:e.modifier})}transformTop(e,t){return o({kind:"TopNode",expression:e.expression,modifiers:e.modifiers})}transformOutput(e,t){return o({kind:"OutputNode",selections:this.transformNodeList(e.selections,t)})}transformDataType(e,t){return e}transformSelectAll(e,t){return e}transformIdentifier(e,t){return e}transformValue(e,t){return e}transformPrimitiveValueList(e,t){return e}transformOperator(e,t){return e}transformDefaultInsertValue(e,t){return e}transformOrAction(e,t){return e}transformCollate(e,t){return e}}var Kr=C({AlterTableNode:!0,CreateIndexNode:!0,CreateSchemaNode:!0,CreateTableNode:!0,CreateTypeNode:!0,CreateViewNode:!0,RefreshMaterializedViewNode:!0,DeleteQueryNode:!0,DropIndexNode:!0,DropSchemaNode:!0,DropTableNode:!0,DropTypeNode:!0,DropViewNode:!0,InsertQueryNode:!0,RawNode:!0,SelectQueryNode:!0,UpdateQueryNode:!0,MergeQueryNode:!0}),jr={json_agg:!0,to_json:!0};class ct extends Du{#e;#t=new Set;#r=new Set;constructor(e){super();this.#e=e}transformNodeImpl(e,t){if(!this.#s(e))return super.transformNodeImpl(e,t);let r=this.#p(e);for(let a of r)this.#r.add(a);let i=this.#a(e);for(let a of i)this.#t.add(a);let n=super.transformNodeImpl(e,t);for(let a of i)this.#t.delete(a);for(let a of r)this.#r.delete(a);return n}transformSchemableIdentifier(e,t){let r=super.transformSchemableIdentifier(e,t);if(r.schema||!this.#t.has(e.identifier.name))return r;return{...r,schema:st.create(this.#e)}}transformReferences(e,t){let r=super.transformReferences(e,t);if(r.table.table.schema)return r;return{...r,table:mo.createWithSchema(this.#e,r.table.table.identifier.name)}}transformAggregateFunction(e,t){return{...super.transformAggregateFunction({...e,aggregated:[]},t),aggregated:this.#o(e,t,"aggregated")}}transformFunction(e,t){return{...super.transformFunction({...e,arguments:[]},t),arguments:this.#o(e,t,"arguments")}}transformSelectModifier(e,t){return{...super.transformSelectModifier({...e,of:void 0},t),of:e.of?.map((r)=>mo.is(r)&&!r.table.schema?{...r,table:this.transformIdentifier(r.table.identifier,t)}:this.transformNode(r,t))}}#o(e,t,r){return jr[e.func]?e[r].map((i)=>!mo.is(i)||i.table.schema?this.transformNode(i,t):{...i,table:this.transformIdentifier(i.table.identifier,t)}):this.transformNodeList(e[r],t)}#s(e){return e.kind in Kr}#a(e){let t=new Set;if("name"in e&&e.name&&po.is(e.name))this.#n(e.name,t);if("from"in e&&e.from)for(let r of e.from.froms)this.#i(r,t);if("into"in e&&e.into)this.#i(e.into,t);if("table"in e&&e.table)this.#i(e.table,t);if("joins"in e&&e.joins)for(let r of e.joins)this.#i(r.table,t);if("using"in e&&e.using)if(L.is(e.using))this.#i(e.using.table,t);else this.#i(e.using,t);return t}#p(e){let t=new Set;if("with"in e&&e.with)this.#d(e.with,t);return t}#i(e,t){if(mo.is(e))return this.#n(e.table,t);if(l.is(e)&&mo.is(e.node))return this.#n(e.node.table,t);if(Fe.is(e)){for(let r of e.items)this.#i(r,t);return}if(be.is(e)){for(let r of e.tables)this.#i(r,t);return}}#n(e,t){let r=e.identifier.name;if(!this.#t.has(r)&&!this.#r.has(r))t.add(r)}#d(e,t){for(let r of e.expressions){let i=r.name.table.table.identifier.name;if(!this.#r.has(i))t.add(i)}}}class js{#e;constructor(e){this.#e=new ct(e)}transformQuery(e){return this.#e.transformNode(e.node,e.queryId)}async transformResult(e){return e.result}}var Zt=C({is(e){return e.kind==="MatchedNode"},create(e,t=!1){return C({kind:"MatchedNode",not:e,bySource:t})}});function lt(e,t,r){return R.create(de([Zt.create(!e.isMatched,e.bySource),...t&&t.length>0?[t.length===3&&r?N(t[0],t[1],t[2]):yr(t)]:[]],"and",!1))}function Ne(e){if(dr(e))return tr.create([e],[]);if(Rr(e))return e.toOperationNode();return e}class Ua{#e;#t;#r;constructor(){this.#e=new Promise((e,t)=>{this.#r=t,this.#t=e})}get promise(){return this.#e}resolve=(e)=>{if(this.#t)this.#t(e)};reject=(e)=>{if(this.#r)this.#r(e)}}async function of(e){let t=new Ua,r=new Ua;return e.provideConnection(async(i)=>(t.resolve(i),await r.promise)).catch((i)=>t.reject(i)),C({connection:await t.promise,release:r.resolve})}var Hr=C([]);class Nu{#e;constructor(e=Hr){this.#e=e}get plugins(){return this.#e}transformQuery(e,t){for(let r of this.#e){let i=r.transformQuery({node:e,queryId:t});if(i.kind===e.kind)e=i;else throw Error(["KyselyPlugin.transformQuery must return a node","of the same kind that was given to it.",`The plugin was given a ${e.kind}`,`but it returned a ${i.kind}`].join(" "))}return e}async executeQuery(e){return await this.provideConnection(async(t)=>{let r=await t.executeQuery(e);if("numUpdatedOrDeletedRows"in r)hi("kysely:warning: outdated driver/plugin detected! `QueryResult.numUpdatedOrDeletedRows` has been replaced with `QueryResult.numAffectedRows`.");return await this.#t(r,e.queryId)})}async*stream(e,t){let{connection:r,release:i}=await of(this);try{for await(let n of r.streamQuery(e,t))yield await this.#t(n,e.queryId)}finally{i()}}async#t(e,t){for(let r of this.#e)e=await r.transformResult({result:e,queryId:t});return e}}class ye extends Nu{get adapter(){throw Error("this query cannot be compiled to SQL")}compileQuery(){throw Error("this query cannot be compiled to SQL")}provideConnection(){throw Error("this query cannot be executed")}withConnectionProvider(){throw Error("this query cannot have a connection provider")}withPlugin(e){return new ye([...this.plugins,e])}withPlugins(e){return new ye([...this.plugins,...e])}withPluginAtFront(e){return new ye([e,...this.plugins])}withoutPlugins(){return new ye([])}}var ie=new ye;class Nt{numChangedRows;constructor(e){this.numChangedRows=e}}class G{#e;constructor(e){this.#e=C(e)}modifyEnd(e){return new G({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}top(e,t){return new G({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}using(...e){return new b({...this.#e,queryNode:A.cloneWithUsing(this.#e.queryNode,re("Using",e))})}returning(e){return new G({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new G({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new G({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new G({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}}class b{#e;constructor(e){this.#e=C(e)}modifyEnd(e){return new b({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}top(e,t){return new b({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}whenMatched(){return this.#t([])}whenMatchedAnd(...e){return this.#t(e)}whenMatchedAndRef(e,t,r){return this.#t([e,t,r],!0)}#t(e,t){return new yt({...this.#e,queryNode:A.cloneWithWhen(this.#e.queryNode,lt({isMatched:!0},e,t))})}whenNotMatched(){return this.#r([])}whenNotMatchedAnd(...e){return this.#r(e)}whenNotMatchedAndRef(e,t,r){return this.#r([e,t,r],!0)}whenNotMatchedBySource(){return this.#r([],!1,!0)}whenNotMatchedBySourceAnd(...e){return this.#r(e,!1,!0)}whenNotMatchedBySourceAndRef(e,t,r){return this.#r([e,t,r],!0,!0)}returning(e){return new b({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new b({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new b({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new b({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}#r(e,t=!1,r=!1){let i={...this.#e,queryNode:A.cloneWithWhen(this.#e.queryNode,lt({isMatched:!1,bySource:r},e,t))};return new(r?yt:er)(i)}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new b({...this.#e})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new Nt(t.numAffectedRows)]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}}class yt{#e;constructor(e){this.#e=C(e)}thenDelete(){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne("delete"))})}thenDoNothing(){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne("do nothing"))})}thenUpdate(e){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne(e(new ce({queryId:this.#e.queryId,executor:ie,queryNode:F.createWithoutTable()}))))})}thenUpdateSet(...e){return this.thenUpdate((t)=>t.set(...e))}}class er{#e;constructor(e){this.#e=C(e)}thenDoNothing(){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne("do nothing"))})}thenInsertValues(e){let[t,r]=Ue(e);return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne(u.cloneWith(u.createWithoutInto(),{columns:t,values:r})))})}}class gi{#e;constructor(e){this.#e=C(e)}selectFrom(e){return Ee({queryId:It(),executor:this.#e.executor,queryNode:nr.createFrom(P(e),this.#e.withNode)})}selectNoFrom(e){return Ee({queryId:It(),executor:this.#e.executor,queryNode:nr.cloneWithSelections(nr.create(this.#e.withNode),c(e))})}insertInto(e){return new h({queryId:It(),executor:this.#e.executor,queryNode:u.create(or(e),this.#e.withNode)})}replaceInto(e){return new h({queryId:It(),executor:this.#e.executor,queryNode:u.create(or(e),this.#e.withNode,!0)})}deleteFrom(e){return new ze({queryId:It(),executor:this.#e.executor,queryNode:te.create(P(e),this.#e.withNode)})}updateTable(e){return new ce({queryId:It(),executor:this.#e.executor,queryNode:F.create(P(e),this.#e.withNode)})}mergeInto(e){return new G({queryId:It(),executor:this.#e.executor,queryNode:A.create(wt(e),this.#e.withNode)})}with(e,t){let r=ft(e,t);return new gi({...this.#e,withNode:this.#e.withNode?ke.cloneWithExpression(this.#e.withNode,r):ke.create(r)})}withRecursive(e,t){let r=ft(e,t);return new gi({...this.#e,withNode:this.#e.withNode?ke.cloneWithExpression(this.#e.withNode,r):ke.create(r,{recursive:!0})})}withPlugin(e){return new gi({...this.#e,executor:this.#e.executor.withPlugin(e)})}withoutPlugins(){return new gi({...this.#e,executor:this.#e.executor.withoutPlugins()})}withSchema(e){return new gi({...this.#e,executor:this.#e.executor.withPluginAtFront(new js(e))})}}function Ht(){return new gi({executor:ie})}function rr(e,t){return new he({joinNode:L.create(e,se(t))})}function ir(){return new ue({overNode:ve.create()})}function re(e,t){if(t.length===3)return Xr(e,t[0],t[1],t[2]);else if(t.length===2)return Yr(e,t[0],t[1]);else if(t.length===1)return Zr(e,t[0]);else throw Error("not implemented")}function Yr(e,t,r){return r(rr(e,t)).toOperationNode()}function Xr(e,t,r,i){return L.createWithOn(e,se(t),N(r,"=",i))}function Zr(e,t){return L.create(e,se(t))}var sr=C({is(e){return e.kind==="OffsetNode"},create(e){return C({kind:"OffsetNode",offset:e})}});var ar=C({is(e){return e.kind==="GroupByItemNode"},create(e){return C({kind:"GroupByItemNode",groupBy:e})}});function pr(e){return e=fr(e)?e(B()):e,Q(e).map(ar.create)}var Je=C({is(e){return e.kind==="SetOperationNode"},create(e,t,r){return C({kind:"SetOperationNode",operator:e,expression:t,all:r})}});function ae(e,t,r){if(fr(t))t=t(Qe());if(!x(t))t=[t];return t.map((i)=>Je.create(e,Ho(i),r))}class p{#e;constructor(e){this.#e=e}get expressionType(){return}as(e){return new Ge(this,e)}or(...e){return new _e(T.create(this.#e,yr(e)))}and(...e){return new $e(v.create(this.#e,yr(e)))}$castTo(){return new p(this.#e)}$notNull(){return new p(this.#e)}toOperationNode(){return this.#e}}class Ge{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}toOperationNode(){return l.create(this.#e.toOperationNode(),Rr(this.#t)?this.#t.toOperationNode():st.create(this.#t))}}class _e{#e;constructor(e){this.#e=e}get expressionType(){return}as(e){return new Ge(this,e)}or(...e){return new _e(T.create(this.#e,yr(e)))}$castTo(){return new _e(this.#e)}toOperationNode(){return E.create(this.#e)}}class $e{#e;constructor(e){this.#e=e}get expressionType(){return}as(e){return new Ge(this,e)}and(...e){return new $e(v.create(this.#e,yr(e)))}$castTo(){return new $e(this.#e)}toOperationNode(){return E.create(this.#e)}}var hr=C({is(e){return e.kind==="FetchNode"},create(e,t){return{kind:"FetchNode",rowCount:_n.create(e),modifier:t}}});function ur(e,t){if(!zs(e)&&!La(e))throw Error(`Invalid fetch row count: ${e}`);if(!ei(t))throw Error(`Invalid fetch modifier: ${t}`);return hr.create(e,t)}function ei(e){return e==="only"||e==="with ties"}var s;class Ot{#e;constructor(e){this.#e=C(e)}get expressionType(){return}get isSelectQueryBuilder(){return!0}where(...e){return new s({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,yr(e))})}whereRef(e,t,r){return new s({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,N(e,t,r))})}having(...e){return new s({...this.#e,queryNode:nr.cloneWithHaving(this.#e.queryNode,yr(e))})}havingRef(e,t,r){return new s({...this.#e,queryNode:nr.cloneWithHaving(this.#e.queryNode,N(e,t,r))})}select(e){return new s({...this.#e,queryNode:nr.cloneWithSelections(this.#e.queryNode,c(e))})}distinctOn(e){return new s({...this.#e,queryNode:nr.cloneWithDistinctOn(this.#e.queryNode,Q(e))})}modifyFront(e){return new s({...this.#e,queryNode:nr.cloneWithFrontModifier(this.#e.queryNode,I.createWithExpression(e.toOperationNode()))})}modifyEnd(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.createWithExpression(e.toOperationNode()))})}distinct(){return new s({...this.#e,queryNode:nr.cloneWithFrontModifier(this.#e.queryNode,I.create("Distinct"))})}forUpdate(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForUpdate",e?xe(e).map(or):void 0))})}forShare(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForShare",e?xe(e).map(or):void 0))})}forKeyShare(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForKeyShare",e?xe(e).map(or):void 0))})}forNoKeyUpdate(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForNoKeyUpdate",e?xe(e).map(or):void 0))})}skipLocked(){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("SkipLocked"))})}noWait(){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("NoWait"))})}selectAll(e){return new s({...this.#e,queryNode:nr.cloneWithSelections(this.#e.queryNode,y(e))})}innerJoin(...e){return this.#t("InnerJoin",e)}leftJoin(...e){return this.#t("LeftJoin",e)}rightJoin(...e){return this.#t("RightJoin",e)}fullJoin(...e){return this.#t("FullJoin",e)}crossJoin(...e){return this.#t("CrossJoin",e)}innerJoinLateral(...e){return this.#t("LateralInnerJoin",e)}leftJoinLateral(...e){return this.#t("LateralLeftJoin",e)}crossJoinLateral(...e){return this.#t("LateralCrossJoin",e)}crossApply(...e){return this.#t("CrossApply",e)}outerApply(...e){return this.#t("OuterApply",e)}#t(e,t){return new s({...this.#e,queryNode:Oe.cloneWithJoin(this.#e.queryNode,re(e,t))})}orderBy(...e){return new s({...this.#e,queryNode:Oe.cloneWithOrderByItems(this.#e.queryNode,k(e))})}groupBy(e){return new s({...this.#e,queryNode:nr.cloneWithGroupByItems(this.#e.queryNode,pr(e))})}limit(e){return new s({...this.#e,queryNode:nr.cloneWithLimit(this.#e.queryNode,fe.create(d(e)))})}offset(e){return new s({...this.#e,queryNode:nr.cloneWithOffset(this.#e.queryNode,sr.create(d(e)))})}fetch(e,t="only"){return new s({...this.#e,queryNode:nr.cloneWithFetch(this.#e.queryNode,ur(e,t))})}top(e,t){return new s({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}union(e){return new s({...this.#e,queryNode:nr.cloneWithSetOperations(this.#e.queryNode,ae("union",e,!1))})}unionAll(e){return new s({...this.#e,queryNode:nr.cloneWithSetOperations(this.#e.queryNode,ae("union",e,!0))})}intersect(e){return new s({...this.#e,queryNode:nr.cloneWithSetOperations(this.#e.queryNode,ae("intersect",e,!1))})}intersectAll(e){return new s({...this.#e,queryNode:nr.cloneWithSetOperations(this.#e.queryNode,ae("intersect",e,!0))})}except(e){return new s({...this.#e,queryNode:nr.cloneWithSetOperations(this.#e.queryNode,ae("except",e,!1))})}exceptAll(e){return new s({...this.#e,queryNode:nr.cloneWithSetOperations(this.#e.queryNode,ae("except",e,!0))})}as(e){return new mr(this,e)}clearSelect(){return new s({...this.#e,queryNode:nr.cloneWithoutSelections(this.#e.queryNode)})}clearWhere(){return new s({...this.#e,queryNode:Oe.cloneWithoutWhere(this.#e.queryNode)})}clearLimit(){return new s({...this.#e,queryNode:nr.cloneWithoutLimit(this.#e.queryNode)})}clearOffset(){return new s({...this.#e,queryNode:nr.cloneWithoutOffset(this.#e.queryNode)})}clearOrderBy(){return new s({...this.#e,queryNode:Oe.cloneWithoutOrderBy(this.#e.queryNode)})}clearGroupBy(){return new s({...this.#e,queryNode:nr.cloneWithoutGroupBy(this.#e.queryNode)})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new s({...this.#e})}$castTo(){return new s(this.#e)}$narrowType(){return new s(this.#e)}$assertType(){return new s(this.#e)}$asTuple(){return new p(this.toOperationNode())}$asScalar(){return new p(this.toOperationNode())}withPlugin(e){return new s({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile();return(await this.#e.executor.executeQuery(e)).rows}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new s({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}s=Ot;function Ee(e){return new Ot(e)}class mr{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}get isAliasedSelectQueryBuilder(){return!0}toOperationNode(){return l.create(this.#e.toOperationNode(),st.create(this.#t))}}var z=C({is(e){return e.kind==="AggregateFunctionNode"},create(e,t=[]){return C({kind:"AggregateFunctionNode",func:e,aggregated:t})},cloneWithDistinct(e){return C({...e,distinct:!0})},cloneWithOrderBy(e,t,r=!1){let i=r?"withinGroup":"orderBy";return C({...e,[i]:e[i]?_.cloneWithItems(e[i],t):_.create(t)})},cloneWithFilter(e,t){return C({...e,filter:e.filter?O.cloneWithOperation(e.filter,"And",t):O.create(t)})},cloneWithOrFilter(e,t){return C({...e,filter:e.filter?O.cloneWithOperation(e.filter,"Or",t):O.create(t)})},cloneWithOver(e,t){return C({...e,over:t})}});var xt=C({is(e){return e.kind==="FunctionNode"},create(e,t){return C({kind:"FunctionNode",func:e,arguments:t})}});class W{#e;constructor(e){this.#e=C(e)}get expressionType(){return}as(e){return new cr(this,e)}distinct(){return new W({...this.#e,aggregateFunctionNode:z.cloneWithDistinct(this.#e.aggregateFunctionNode)})}orderBy(...e){return new W({...this.#e,aggregateFunctionNode:Oe.cloneWithOrderByItems(this.#e.aggregateFunctionNode,k(e))})}clearOrderBy(){return new W({...this.#e,aggregateFunctionNode:Oe.cloneWithoutOrderBy(this.#e.aggregateFunctionNode)})}withinGroupOrderBy(...e){return new W({...this.#e,aggregateFunctionNode:z.cloneWithOrderBy(this.#e.aggregateFunctionNode,k(e),!0)})}filterWhere(...e){return new W({...this.#e,aggregateFunctionNode:z.cloneWithFilter(this.#e.aggregateFunctionNode,yr(e))})}filterWhereRef(e,t,r){return new W({...this.#e,aggregateFunctionNode:z.cloneWithFilter(this.#e.aggregateFunctionNode,N(e,t,r))})}over(e){let t=ir();return new W({...this.#e,aggregateFunctionNode:z.cloneWithOver(this.#e.aggregateFunctionNode,(e?e(t):t).toOperationNode())})}$call(e){return e(this)}$castTo(){return new W(this.#e)}$notNull(){return new W(this.#e)}toOperationNode(){return this.#e.aggregateFunctionNode}}class cr{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}toOperationNode(){return l.create(this.#e.toOperationNode(),st.create(this.#t))}}function sf(){let e=(r,i)=>new p(xt.create(r,Q(i??[]))),t=(r,i)=>new W({aggregateFunctionNode:z.create(r,i?Q(i):void 0)});return Object.assign(e,{agg:t,avg(r){return t("avg",[r])},coalesce(...r){return e("coalesce",r)},count(r){return t("count",[r])},countAll(r){return new W({aggregateFunctionNode:z.create("count",y(r))})},max(r){return t("max",[r])},min(r){return t("min",[r])},sum(r){return t("sum",[r])},any(r){return e("any",[r])},jsonAgg(r){return new W({aggregateFunctionNode:z.create("json_agg",[dr(r)?or(r):r.toOperationNode()])})},toJson(r){return new p(xt.create("to_json",[dr(r)?or(r):r.toOperationNode()]))}})}var lr=C({is(e){return e.kind==="UnaryOperationNode"},create(e,t){return C({kind:"UnaryOperationNode",operator:e,operand:t})}});function Nr(e,t){return lr.create(S.create(e),w(t))}var as=C({is(e){return e.kind==="CaseNode"},create(e){return C({kind:"CaseNode",value:e})},cloneWithWhen(e,t){return C({...e,when:C(e.when?[...e.when,t]:[t])})},cloneWithThen(e,t){return C({...e,when:e.when?C([...e.when.slice(0,-1),R.cloneWithResult(e.when[e.when.length-1],t)]):void 0})},cloneWith(e,t){return C({...e,...t})}});class Fu{#e;constructor(e){this.#e=C(e)}when(...e){return new gt({...this.#e,node:as.cloneWithWhen(this.#e.node,R.create(yr(e)))})}}class gt{#e;constructor(e){this.#e=C(e)}then(e){return new wr({...this.#e,node:as.cloneWithThen(this.#e.node,Pe(e)?ge(e):d(e))})}}class wr{#e;constructor(e){this.#e=C(e)}when(...e){return new gt({...this.#e,node:as.cloneWithWhen(this.#e.node,R.create(yr(e)))})}else(e){return new Or({...this.#e,node:as.cloneWith(this.#e.node,{else:Pe(e)?ge(e):d(e)})})}end(){return new p(as.cloneWith(this.#e.node,{isStatement:!1}))}endCase(){return new p(as.cloneWith(this.#e.node,{isStatement:!0}))}}class Or{#e;constructor(e){this.#e=C(e)}end(){return new p(as.cloneWith(this.#e.node,{isStatement:!1}))}endCase(){return new p(as.cloneWith(this.#e.node,{isStatement:!0}))}}var vt=C({is(e){return e.kind==="JSONPathLegNode"},create(e,t){return C({kind:"JSONPathLegNode",type:e,value:t})}});var ti=/^#-\d+$/;class Re{#e;constructor(e){this.#e=e}at(e){if(typeof e!=="number"&&typeof e!=="string"||typeof e==="number"&&!Number.isInteger(e)||typeof e==="string"&&e!=="last"&&!ti.test(e))throw Error(`Unexpected index value in .at(...): ${e}`);return this.#t("ArrayLocation",e)}key(e){return this.#t("Member",e)}#t(e,t){if(pe.is(this.#e))return new Ae(pe.cloneWithTraversal(this.#e,ee.is(this.#e.traversal)?ee.cloneWithLeg(this.#e.traversal,vt.create(e,t)):qe.cloneWithValue(this.#e.traversal,_n.createImmediate(t))));return new Ae(ee.cloneWithLeg(this.#e,vt.create(e,t)))}}class Ae extends Re{#e;constructor(e){super(e);this.#e=e}get expressionType(){return}as(e){return new xr(this,e)}$castTo(){return new Ae(this.#e)}$notNull(){return new Ae(this.#e)}toOperationNode(){return this.#e}}class xr{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}toOperationNode(){return l.create(this.#e.toOperationNode(),Rr(this.#t)?this.#t.toOperationNode():st.create(this.#t))}}var bt=C({is(e){return e.kind==="TupleNode"},create(e){return C({kind:"TupleNode",values:C(e)})}});var ri=["varchar","char","text","integer","int2","int4","int8","smallint","bigint","boolean","real","double precision","float4","float8","decimal","numeric","binary","bytea","date","datetime","time","timetz","timestamp","timestamptz","serial","bigserial","uuid","json","jsonb","blob","varbinary","int4range","int4multirange","int8range","int8multirange","numrange","nummultirange","tsrange","tsmultirange","tstzrange","tstzmultirange","daterange","datemultirange"],ii=[/^varchar\(\d+\)$/,/^char\(\d+\)$/,/^decimal\(\d+, \d+\)$/,/^numeric\(\d+, \d+\)$/,/^binary\(\d+\)$/,/^datetime\(\d+\)$/,/^time\(\d+\)$/,/^timetz\(\d+\)$/,/^timestamp\(\d+\)$/,/^timestamptz\(\d+\)$/,/^varbinary\(\d+\)$/],gr=C({is(e){return e.kind==="DataTypeNode"},create(e){return C({kind:"DataTypeNode",dataType:e})}});function vr(e){if(ri.includes(e))return!0;if(ii.some((t)=>t.test(e)))return!0;return!1}function qs(e){if(Rr(e))return e.toOperationNode();if(vr(e))return gr.create(e);throw Error(`invalid column data type ${JSON.stringify(e)}`)}var br=C({is(e){return e.kind==="CastNode"},create(e,t){return C({kind:"CastNode",expression:e,dataType:t})}});function Qe(e=ie){function t(n,a,j){return new p(Ie(n,a,j))}function r(n,a){return new p(Nr(n,a))}let i=Object.assign(t,{fn:void 0,eb:void 0,selectFrom(n){return Ee({queryId:It(),executor:e,queryNode:nr.createFrom(P(n))})},case(n){return new Fu({node:as.create(So(n)?void 0:w(n))})},ref(n,a){if(So(a))return new p(is(n));return new Re(Lt(n,a))},jsonPath(){return new Re(ee.create())},table(n){return new p(or(n))},val(n){return new p(d(n))},refTuple(...n){return new p(bt.create(n.map(w)))},tuple(...n){return new p(bt.create(n.map(d)))},lit(n){return new p(ge(n))},unary:r,not(n){return r("not",n)},exists(n){return r("exists",n)},neg(n){return r("-",n)},between(n,a,j){return new p(X.create(w(n),S.create("between"),v.create(d(a),d(j))))},betweenSymmetric(n,a,j){return new p(X.create(w(n),S.create("between symmetric"),v.create(d(a),d(j))))},and(n){if(x(n))return new p(de(n,"and"));return new p(tt(n,"and"))},or(n){if(x(n))return new p(de(n,"or"));return new p(tt(n,"or"))},parens(...n){let a=yr(n);if(E.is(a))return new p(a);else return new p(E.create(a))},cast(n,a){return new p(br.create(w(n),qs(a)))},withSchema(n){return Qe(e.withPluginAtFront(new js(n)))}});return i.fn=sf(),i.eb=i,i}function B(e){return Qe()}function Ho(e){if(Rr(e))return e.toOperationNode();else if(fr(e))return e(B()).toOperationNode();throw Error(`invalid expression: ${JSON.stringify(e)}`)}function Ve(e){if(Rr(e))return e.toOperationNode();else if(fr(e))return e(B()).toOperationNode();throw Error(`invalid aliased expression: ${JSON.stringify(e)}`)}function J(e){return Le(e)||Ct(e)||fr(e)}class hm{#e;get table(){return this.#e}constructor(e){this.#e=e}as(e){return new Wr(this.#e,e)}}class Wr{#e;#t;get table(){return this.#e}get alias(){return this.#t}constructor(e,t){this.#e=e,this.#t=t}toOperationNode(){return l.create(or(this.#e),st.create(this.#t))}}function Sr(e){return sn(e)&&Rr(e)&&dr(e.table)&&dr(e.alias)}function P(e){if(x(e))return e.map((t)=>se(t));else return[se(e)]}function se(e){if(dr(e))return wt(e);else if(Sr(e))return e.toOperationNode();else return Ve(e)}function wt(e){if(e.includes(" as ")){let[r,i]=e.split(" as ").map(Cr);return l.create(or(r),st.create(i))}else return or(e)}function or(e){if(e.includes(".")){let[r,i]=e.split(".").map(Cr);return mo.createWithSchema(r,i)}else return mo.create(e)}function Cr(e){return e.trim()}var $s=C({is(e){return e.kind==="CreateViewNode"},create(e){return C({kind:"CreateViewNode",name:po.create(e)})},cloneWith(e,t){return C({...e,...t})}});class Te{#e;constructor(e){this.#e=C(e)}get expressionType(){return}get isRawBuilder(){return!0}as(e){return new kr(this,e)}$castTo(){return new Te({...this.#e})}$notNull(){return new Te(this.#e)}withPlugin(e){return new Te({...this.#e,plugins:this.#e.plugins!==void 0?C([...this.#e.plugins,e]):C([e])})}toOperationNode(){return this.#r(this.#t())}compile(e){return this.#o(this.#t(e))}async execute(e){let t=this.#t(e);return t.executeQuery(this.#o(t))}#t(e){let t=e!==void 0?e.getExecutor():ie;return this.#e.plugins!==void 0?t.withPlugins(this.#e.plugins):t}#r(e){return e.transformQuery(this.#e.rawNode,this.#e.queryId)}#o(e){return e.compileQuery(this.#r(e),this.#e.queryId)}}function K(e){return new Te(e)}class kr{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}get rawBuilder(){return this.#e}toOperationNode(){return l.create(this.#e.toOperationNode(),Rr(this.#t)?this.#t.toOperationNode():st.create(this.#t))}}var je=Object.assign((e,...t)=>K({queryId:It(),rawNode:tr.create(e,t?.map(Er)??[])}),{ref(e){return K({queryId:It(),rawNode:tr.createWithChild(is(e))})},val(e){return K({queryId:It(),rawNode:tr.createWithChild(d(e))})},value(e){return this.val(e)},table(e){return K({queryId:It(),rawNode:tr.createWithChild(or(e))})},id(...e){let t=Array(e.length+1).fill(".");return t[0]="",t[t.length-1]="",K({queryId:It(),rawNode:tr.create(t,e.map(st.create))})},lit(e){return K({queryId:It(),rawNode:tr.createWithChild(_n.createImmediate(e))})},literal(e){return this.lit(e)},raw(e){return K({queryId:It(),rawNode:tr.createWithSql(e)})},join(e,t=je`, `){let r=Array(Math.max(2*e.length-1,0)),i=t.toOperationNode();for(let n=0;n<e.length;++n)if(r[2*n]=Er(e[n]),n!==e.length-1)r[2*n+1]=i;return K({queryId:It(),rawNode:tr.createWithChildren(r)})}});function Er(e){if(Rr(e))return e.toOperationNode();return d(e)}class St{nodeStack=[];get parentNode(){return this.nodeStack[this.nodeStack.length-2]}#e=C({AliasNode:this.visitAlias.bind(this),ColumnNode:this.visitColumn.bind(this),IdentifierNode:this.visitIdentifier.bind(this),SchemableIdentifierNode:this.visitSchemableIdentifier.bind(this),RawNode:this.visitRaw.bind(this),ReferenceNode:this.visitReference.bind(this),SelectQueryNode:this.visitSelectQuery.bind(this),SelectionNode:this.visitSelection.bind(this),TableNode:this.visitTable.bind(this),FromNode:this.visitFrom.bind(this),SelectAllNode:this.visitSelectAll.bind(this),AndNode:this.visitAnd.bind(this),OrNode:this.visitOr.bind(this),ValueNode:this.visitValue.bind(this),ValueListNode:this.visitValueList.bind(this),PrimitiveValueListNode:this.visitPrimitiveValueList.bind(this),ParensNode:this.visitParens.bind(this),JoinNode:this.visitJoin.bind(this),OperatorNode:this.visitOperator.bind(this),WhereNode:this.visitWhere.bind(this),InsertQueryNode:this.visitInsertQuery.bind(this),DeleteQueryNode:this.visitDeleteQuery.bind(this),ReturningNode:this.visitReturning.bind(this),CreateTableNode:this.visitCreateTable.bind(this),AddColumnNode:this.visitAddColumn.bind(this),ColumnDefinitionNode:this.visitColumnDefinition.bind(this),DropTableNode:this.visitDropTable.bind(this),DataTypeNode:this.visitDataType.bind(this),OrderByNode:this.visitOrderBy.bind(this),OrderByItemNode:this.visitOrderByItem.bind(this),GroupByNode:this.visitGroupBy.bind(this),GroupByItemNode:this.visitGroupByItem.bind(this),UpdateQueryNode:this.visitUpdateQuery.bind(this),ColumnUpdateNode:this.visitColumnUpdate.bind(this),LimitNode:this.visitLimit.bind(this),OffsetNode:this.visitOffset.bind(this),OnConflictNode:this.visitOnConflict.bind(this),OnDuplicateKeyNode:this.visitOnDuplicateKey.bind(this),CreateIndexNode:this.visitCreateIndex.bind(this),DropIndexNode:this.visitDropIndex.bind(this),ListNode:this.visitList.bind(this),PrimaryKeyConstraintNode:this.visitPrimaryKeyConstraint.bind(this),UniqueConstraintNode:this.visitUniqueConstraint.bind(this),ReferencesNode:this.visitReferences.bind(this),CheckConstraintNode:this.visitCheckConstraint.bind(this),WithNode:this.visitWith.bind(this),CommonTableExpressionNode:this.visitCommonTableExpression.bind(this),CommonTableExpressionNameNode:this.visitCommonTableExpressionName.bind(this),HavingNode:this.visitHaving.bind(this),CreateSchemaNode:this.visitCreateSchema.bind(this),DropSchemaNode:this.visitDropSchema.bind(this),AlterTableNode:this.visitAlterTable.bind(this),DropColumnNode:this.visitDropColumn.bind(this),RenameColumnNode:this.visitRenameColumn.bind(this),AlterColumnNode:this.visitAlterColumn.bind(this),ModifyColumnNode:this.visitModifyColumn.bind(this),AddConstraintNode:this.visitAddConstraint.bind(this),DropConstraintNode:this.visitDropConstraint.bind(this),RenameConstraintNode:this.visitRenameConstraint.bind(this),ForeignKeyConstraintNode:this.visitForeignKeyConstraint.bind(this),CreateViewNode:this.visitCreateView.bind(this),RefreshMaterializedViewNode:this.visitRefreshMaterializedView.bind(this),DropViewNode:this.visitDropView.bind(this),GeneratedNode:this.visitGenerated.bind(this),DefaultValueNode:this.visitDefaultValue.bind(this),OnNode:this.visitOn.bind(this),ValuesNode:this.visitValues.bind(this),SelectModifierNode:this.visitSelectModifier.bind(this),CreateTypeNode:this.visitCreateType.bind(this),DropTypeNode:this.visitDropType.bind(this),ExplainNode:this.visitExplain.bind(this),DefaultInsertValueNode:this.visitDefaultInsertValue.bind(this),AggregateFunctionNode:this.visitAggregateFunction.bind(this),OverNode:this.visitOver.bind(this),PartitionByNode:this.visitPartitionBy.bind(this),PartitionByItemNode:this.visitPartitionByItem.bind(this),SetOperationNode:this.visitSetOperation.bind(this),BinaryOperationNode:this.visitBinaryOperation.bind(this),UnaryOperationNode:this.visitUnaryOperation.bind(this),UsingNode:this.visitUsing.bind(this),FunctionNode:this.visitFunction.bind(this),CaseNode:this.visitCase.bind(this),WhenNode:this.visitWhen.bind(this),JSONReferenceNode:this.visitJSONReference.bind(this),JSONPathNode:this.visitJSONPath.bind(this),JSONPathLegNode:this.visitJSONPathLeg.bind(this),JSONOperatorChainNode:this.visitJSONOperatorChain.bind(this),TupleNode:this.visitTuple.bind(this),MergeQueryNode:this.visitMergeQuery.bind(this),MatchedNode:this.visitMatched.bind(this),AddIndexNode:this.visitAddIndex.bind(this),CastNode:this.visitCast.bind(this),FetchNode:this.visitFetch.bind(this),TopNode:this.visitTop.bind(this),OutputNode:this.visitOutput.bind(this),OrActionNode:this.visitOrAction.bind(this),CollateNode:this.visitCollate.bind(this)});visitNode=(e)=>{this.nodeStack.push(e),this.#e[e.kind](e),this.nodeStack.pop()}}var oi=/'/g,ni=/['"]/g;class ho extends St{#e="";#t=[];get numParameters(){return this.#t.length}compileQuery(e,t){return this.#e="",this.#t=[],this.nodeStack.splice(0,this.nodeStack.length),this.visitNode(e),C({query:e,queryId:t,sql:this.getSql(),parameters:[...this.#t]})}getSql(){return this.#e}visitSelectQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!u.is(this.parentNode)&&!fo.is(this.parentNode)&&!$s.is(this.parentNode)&&!Je.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("select"),e.distinctOn)this.append(" "),this.compileDistinctOn(e.distinctOn);if(e.frontModifiers?.length)this.append(" "),this.compileList(e.frontModifiers," ");if(e.top)this.append(" "),this.visitNode(e.top);if(e.selections)this.append(" "),this.compileList(e.selections);if(e.from)this.append(" "),this.visitNode(e.from);if(e.joins)this.append(" "),this.compileList(e.joins," ");if(e.where)this.append(" "),this.visitNode(e.where);if(e.groupBy)this.append(" "),this.visitNode(e.groupBy);if(e.having)this.append(" "),this.visitNode(e.having);if(e.setOperations)this.append(" "),this.compileList(e.setOperations," ");if(e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(e.limit)this.append(" "),this.visitNode(e.limit);if(e.offset)this.append(" "),this.visitNode(e.offset);if(e.fetch)this.append(" "),this.visitNode(e.fetch);if(e.endModifiers?.length)this.append(" "),this.compileList(this.sortSelectModifiers([...e.endModifiers])," ");if(t)this.append(")")}visitFrom(e){this.append("from "),this.compileList(e.froms)}visitSelection(e){this.visitNode(e.selection)}visitColumn(e){this.visitNode(e.column)}compileDistinctOn(e){this.append("distinct on ("),this.compileList(e),this.append(")")}compileList(e,t=", "){let r=e.length-1;for(let i=0;i<=r;i++)if(this.visitNode(e[i]),i<r)this.append(t)}visitWhere(e){this.append("where "),this.visitNode(e.where)}visitHaving(e){this.append("having "),this.visitNode(e.having)}visitInsertQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!tr.is(this.parentNode)&&!R.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append(e.replace?"replace":"insert"),e.ignore)hi("`InsertQueryNode.ignore` is deprecated. Use `InsertQueryNode.orAction` instead."),this.append(" ignore");if(e.orAction)this.append(" "),this.visitNode(e.orAction);if(e.top)this.append(" "),this.visitNode(e.top);if(e.into)this.append(" into "),this.visitNode(e.into);if(e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");if(e.output)this.append(" "),this.visitNode(e.output);if(e.values)this.append(" "),this.visitNode(e.values);if(e.defaultValues)this.append(" "),this.append("default values");if(e.onConflict)this.append(" "),this.visitNode(e.onConflict);if(e.onDuplicateKey)this.append(" "),this.visitNode(e.onDuplicateKey);if(e.returning)this.append(" "),this.visitNode(e.returning);if(t)this.append(")");if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitValues(e){this.append("values "),this.compileList(e.values)}visitDeleteQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!tr.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("delete "),e.top)this.visitNode(e.top),this.append(" ");if(this.visitNode(e.from),e.output)this.append(" "),this.visitNode(e.output);if(e.using)this.append(" "),this.visitNode(e.using);if(e.joins)this.append(" "),this.compileList(e.joins," ");if(e.where)this.append(" "),this.visitNode(e.where);if(e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(e.limit)this.append(" "),this.visitNode(e.limit);if(e.returning)this.append(" "),this.visitNode(e.returning);if(t)this.append(")");if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitReturning(e){this.append("returning "),this.compileList(e.selections)}visitAlias(e){this.visitNode(e.node),this.append(" as "),this.visitNode(e.alias)}visitReference(e){if(e.table)this.visitNode(e.table),this.append(".");this.visitNode(e.column)}visitSelectAll(e){this.append("*")}visitIdentifier(e){this.append(this.getLeftIdentifierWrapper()),this.compileUnwrappedIdentifier(e),this.append(this.getRightIdentifierWrapper())}compileUnwrappedIdentifier(e){if(!dr(e.name))throw Error("a non-string identifier was passed to compileUnwrappedIdentifier.");this.append(this.sanitizeIdentifier(e.name))}visitAnd(e){this.visitNode(e.left),this.append(" and "),this.visitNode(e.right)}visitOr(e){this.visitNode(e.left),this.append(" or "),this.visitNode(e.right)}visitValue(e){if(e.immediate)this.appendImmediateValue(e.value);else this.appendValue(e.value)}visitValueList(e){this.append("("),this.compileList(e.values),this.append(")")}visitTuple(e){this.append("("),this.compileList(e.values),this.append(")")}visitPrimitiveValueList(e){this.append("(");let{values:t}=e;for(let r=0;r<t.length;++r)if(this.appendValue(t[r]),r!==t.length-1)this.append(", ");this.append(")")}visitParens(e){this.append("("),this.visitNode(e.node),this.append(")")}visitJoin(e){if(this.append(ai[e.joinType]),this.append(" "),this.visitNode(e.table),e.on)this.append(" "),this.visitNode(e.on)}visitOn(e){this.append("on "),this.visitNode(e.on)}visitRaw(e){let{sqlFragments:t,parameters:r}=e;for(let i=0;i<t.length;++i)if(this.append(t[i]),r.length>i)this.visitNode(r[i])}visitOperator(e){this.append(e.operator)}visitTable(e){this.visitNode(e.table)}visitSchemableIdentifier(e){if(e.schema)this.visitNode(e.schema),this.append(".");this.visitNode(e.identifier)}visitCreateTable(e){if(this.append("create "),e.frontModifiers?.length)this.compileList(e.frontModifiers," "),this.append(" ");if(e.temporary)this.append("temporary ");if(this.append("table "),e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.table),!e.selectQuery)this.append(" ("),this.compileList([...e.columns,...e.constraints??[]]),this.append(")");if(e.onCommit)this.append(" on commit "),this.append(e.onCommit);if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ");if(e.selectQuery)this.append(" as "),this.visitNode(e.selectQuery)}visitColumnDefinition(e){if(e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.column),this.append(" "),this.visitNode(e.dataType),e.unsigned)this.append(" unsigned");if(e.frontModifiers&&e.frontModifiers.length>0)this.append(" "),this.compileList(e.frontModifiers," ");if(e.generated)this.append(" "),this.visitNode(e.generated);if(e.identity)this.append(" identity");if(e.defaultTo)this.append(" "),this.visitNode(e.defaultTo);if(e.notNull)this.append(" not null");if(e.unique)this.append(" unique");if(e.nullsNotDistinct)this.append(" nulls not distinct");if(e.primaryKey)this.append(" primary key");if(e.autoIncrement)this.append(" "),this.append(this.getAutoIncrement());if(e.references)this.append(" "),this.visitNode(e.references);if(e.check)this.append(" "),this.visitNode(e.check);if(e.endModifiers&&e.endModifiers.length>0)this.append(" "),this.compileList(e.endModifiers," ")}getAutoIncrement(){return"auto_increment"}visitReferences(e){if(this.append("references "),this.visitNode(e.table),this.append(" ("),this.compileList(e.columns),this.append(")"),e.onDelete)this.append(" on delete "),this.append(e.onDelete);if(e.onUpdate)this.append(" on update "),this.append(e.onUpdate)}visitDropTable(e){if(this.append("drop table "),e.ifExists)this.append("if exists ");if(this.visitNode(e.table),e.cascade)this.append(" cascade")}visitDataType(e){this.append(e.dataType)}visitOrderBy(e){this.append("order by "),this.compileList(e.items)}visitOrderByItem(e){if(this.visitNode(e.orderBy),e.collation)this.append(" "),this.visitNode(e.collation);if(e.direction)this.append(" "),this.visitNode(e.direction);if(e.nulls)this.append(" nulls "),this.append(e.nulls)}visitGroupBy(e){this.append("group by "),this.compileList(e.items)}visitGroupByItem(e){this.visitNode(e.groupBy)}visitUpdateQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!tr.is(this.parentNode)&&!R.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("update "),e.top)this.visitNode(e.top),this.append(" ");if(e.table)this.visitNode(e.table),this.append(" ");if(this.append("set "),e.updates)this.compileList(e.updates);if(e.output)this.append(" "),this.visitNode(e.output);if(e.from)this.append(" "),this.visitNode(e.from);if(e.joins){if(!e.from)throw Error("Joins in an update query are only supported as a part of a PostgreSQL 'update set from join' query. If you want to create a MySQL 'update join set' query, see https://kysely.dev/docs/examples/update/my-sql-joins");this.append(" "),this.compileList(e.joins," ")}if(e.where)this.append(" "),this.visitNode(e.where);if(e.returning)this.append(" "),this.visitNode(e.returning);if(e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(e.limit)this.append(" "),this.visitNode(e.limit);if(t)this.append(")");if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitColumnUpdate(e){this.visitNode(e.column),this.append(" = "),this.visitNode(e.value)}visitLimit(e){this.append("limit "),this.visitNode(e.limit)}visitOffset(e){this.append("offset "),this.visitNode(e.offset)}visitOnConflict(e){if(this.append("on conflict"),e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");else if(e.constraint)this.append(" on constraint "),this.visitNode(e.constraint);else if(e.indexExpression)this.append(" ("),this.visitNode(e.indexExpression),this.append(")");if(e.indexWhere)this.append(" "),this.visitNode(e.indexWhere);if(e.doNothing===!0)this.append(" do nothing");else if(e.updates){if(this.append(" do update set "),this.compileList(e.updates),e.updateWhere)this.append(" "),this.visitNode(e.updateWhere)}}visitOnDuplicateKey(e){this.append("on duplicate key update "),this.compileList(e.updates)}visitCreateIndex(e){if(this.append("create "),e.unique)this.append("unique ");if(this.append("index "),e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.name),e.table)this.append(" on "),this.visitNode(e.table);if(e.using)this.append(" using "),this.visitNode(e.using);if(e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");if(e.nullsNotDistinct)this.append(" nulls not distinct");if(e.where)this.append(" "),this.visitNode(e.where)}visitDropIndex(e){if(this.append("drop index "),e.ifExists)this.append("if exists ");if(this.visitNode(e.name),e.table)this.append(" on "),this.visitNode(e.table);if(e.cascade)this.append(" cascade")}visitCreateSchema(e){if(this.append("create schema "),e.ifNotExists)this.append("if not exists ");this.visitNode(e.schema)}visitDropSchema(e){if(this.append("drop schema "),e.ifExists)this.append("if exists ");if(this.visitNode(e.schema),e.cascade)this.append(" cascade")}visitPrimaryKeyConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");this.append("primary key ("),this.compileList(e.columns),this.append(")"),this.buildDeferrable(e)}buildDeferrable(e){if(e.deferrable!==void 0)if(e.deferrable)this.append(" deferrable");else this.append(" not deferrable");if(e.initiallyDeferred!==void 0)if(e.initiallyDeferred)this.append(" initially deferred");else this.append(" initially immediate")}visitUniqueConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");if(this.append("unique"),e.nullsNotDistinct)this.append(" nulls not distinct");this.append(" ("),this.compileList(e.columns),this.append(")"),this.buildDeferrable(e)}visitCheckConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");this.append("check ("),this.visitNode(e.expression),this.append(")")}visitForeignKeyConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");if(this.append("foreign key ("),this.compileList(e.columns),this.append(") "),this.visitNode(e.references),e.onDelete)this.append(" on delete "),this.append(e.onDelete);if(e.onUpdate)this.append(" on update "),this.append(e.onUpdate);this.buildDeferrable(e)}visitList(e){this.compileList(e.items)}visitWith(e){if(this.append("with "),e.recursive)this.append("recursive ");this.compileList(e.expressions)}visitCommonTableExpression(e){if(this.visitNode(e.name),this.append(" as "),Vi(e.materialized)){if(!e.materialized)this.append("not ");this.append("materialized ")}this.visitNode(e.expression)}visitCommonTableExpressionName(e){if(this.visitNode(e.table),e.columns)this.append("("),this.compileList(e.columns),this.append(")")}visitAlterTable(e){if(this.append("alter table "),this.visitNode(e.table),this.append(" "),e.renameTo)this.append("rename to "),this.visitNode(e.renameTo);if(e.setSchema)this.append("set schema "),this.visitNode(e.setSchema);if(e.addConstraint)this.visitNode(e.addConstraint);if(e.dropConstraint)this.visitNode(e.dropConstraint);if(e.renameConstraint)this.visitNode(e.renameConstraint);if(e.columnAlterations)this.compileColumnAlterations(e.columnAlterations);if(e.addIndex)this.visitNode(e.addIndex);if(e.dropIndex)this.visitNode(e.dropIndex)}visitAddColumn(e){this.append("add column "),this.visitNode(e.column)}visitRenameColumn(e){this.append("rename column "),this.visitNode(e.column),this.append(" to "),this.visitNode(e.renameTo)}visitDropColumn(e){this.append("drop column "),this.visitNode(e.column)}visitAlterColumn(e){if(this.append("alter column "),this.visitNode(e.column),this.append(" "),e.dataType){if(this.announcesNewColumnDataType())this.append("type ");if(this.visitNode(e.dataType),e.dataTypeExpression)this.append("using "),this.visitNode(e.dataTypeExpression)}if(e.setDefault)this.append("set default "),this.visitNode(e.setDefault);if(e.dropDefault)this.append("drop default");if(e.setNotNull)this.append("set not null");if(e.dropNotNull)this.append("drop not null")}visitModifyColumn(e){this.append("modify column "),this.visitNode(e.column)}visitAddConstraint(e){this.append("add "),this.visitNode(e.constraint)}visitDropConstraint(e){if(this.append("drop constraint "),e.ifExists)this.append("if exists ");if(this.visitNode(e.constraintName),e.modifier==="cascade")this.append(" cascade");else if(e.modifier==="restrict")this.append(" restrict")}visitRenameConstraint(e){this.append("rename constraint "),this.visitNode(e.oldName),this.append(" to "),this.visitNode(e.newName)}visitSetOperation(e){if(this.append(e.operator),this.append(" "),e.all)this.append("all ");this.visitNode(e.expression)}visitCreateView(e){if(this.append("create "),e.orReplace)this.append("or replace ");if(e.materialized)this.append("materialized ");if(e.temporary)this.append("temporary ");if(this.append("view "),e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.name),this.append(" "),e.columns)this.append("("),this.compileList(e.columns),this.append(") ");if(e.as)this.append("as "),this.visitNode(e.as)}visitRefreshMaterializedView(e){if(this.append("refresh materialized view "),e.concurrently)this.append("concurrently ");if(this.visitNode(e.name),e.withNoData)this.append(" with no data");else this.append(" with data")}visitDropView(e){if(this.append("drop "),e.materialized)this.append("materialized ");if(this.append("view "),e.ifExists)this.append("if exists ");if(this.visitNode(e.name),e.cascade)this.append(" cascade")}visitGenerated(e){if(this.append("generated "),e.always)this.append("always ");if(e.byDefault)this.append("by default ");if(this.append("as "),e.identity)this.append("identity");if(e.expression)this.append("("),this.visitNode(e.expression),this.append(")");if(e.stored)this.append(" stored")}visitDefaultValue(e){this.append("default "),this.visitNode(e.defaultValue)}visitSelectModifier(e){if(e.rawModifier)this.visitNode(e.rawModifier);else this.append(si[e.modifier]);if(e.of)this.append(" of "),this.compileList(e.of,", ")}visitCreateType(e){if(this.append("create type "),this.visitNode(e.name),e.enum)this.append(" as enum "),this.visitNode(e.enum)}visitDropType(e){if(this.append("drop type "),e.ifExists)this.append("if exists ");this.visitNode(e.name)}visitExplain(e){if(this.append("explain"),e.options||e.format){if(this.append(" "),this.append(this.getLeftExplainOptionsWrapper()),e.options){if(this.visitNode(e.options),e.format)this.append(this.getExplainOptionsDelimiter())}if(e.format)this.append("format"),this.append(this.getExplainOptionAssignment()),this.append(e.format);this.append(this.getRightExplainOptionsWrapper())}}visitDefaultInsertValue(e){this.append("default")}visitAggregateFunction(e){if(this.append(e.func),this.append("("),e.distinct)this.append("distinct ");if(this.compileList(e.aggregated),e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(this.append(")"),e.withinGroup)this.append(" within group ("),this.visitNode(e.withinGroup),this.append(")");if(e.filter)this.append(" filter("),this.visitNode(e.filter),this.append(")");if(e.over)this.append(" "),this.visitNode(e.over)}visitOver(e){if(this.append("over("),e.partitionBy){if(this.visitNode(e.partitionBy),e.orderBy)this.append(" ")}if(e.orderBy)this.visitNode(e.orderBy);this.append(")")}visitPartitionBy(e){this.append("partition by "),this.compileList(e.items)}visitPartitionByItem(e){this.visitNode(e.partitionBy)}visitBinaryOperation(e){this.visitNode(e.leftOperand),this.append(" "),this.visitNode(e.operator),this.append(" "),this.visitNode(e.rightOperand)}visitUnaryOperation(e){if(this.visitNode(e.operator),!this.isMinusOperator(e.operator))this.append(" ");this.visitNode(e.operand)}isMinusOperator(e){return S.is(e)&&e.operator==="-"}visitUsing(e){this.append("using "),this.compileList(e.tables)}visitFunction(e){this.append(e.func),this.append("("),this.compileList(e.arguments),this.append(")")}visitCase(e){if(this.append("case"),e.value)this.append(" "),this.visitNode(e.value);if(e.when)this.append(" "),this.compileList(e.when," ");if(e.else)this.append(" else "),this.visitNode(e.else);if(this.append(" end"),e.isStatement)this.append(" case")}visitWhen(e){if(this.append("when "),this.visitNode(e.condition),e.result)this.append(" then "),this.visitNode(e.result)}visitJSONReference(e){this.visitNode(e.reference),this.visitNode(e.traversal)}visitJSONPath(e){if(e.inOperator)this.visitNode(e.inOperator);this.append("'$");for(let t of e.pathLegs)this.visitNode(t);this.append("'")}visitJSONPathLeg(e){let t=e.type==="ArrayLocation",r=String(e.value);if(t)this.append("["),this.append(this.sanitizeStringLiteral(r)),this.append("]");else this.append('."'),this.append(this.sanitizeJSONPathMemberValue(r)),this.append('"')}visitJSONOperatorChain(e){for(let t=0,r=e.values.length;t<r;t++){if(t===r-1)this.visitNode(e.operator);else this.append("->");this.visitNode(e.values[t])}}visitMergeQuery(e){if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("merge "),e.top)this.visitNode(e.top),this.append(" ");if(this.append("into "),this.visitNode(e.into),e.using)this.append(" "),this.visitNode(e.using);if(e.whens)this.append(" "),this.compileList(e.whens," ");if(e.returning)this.append(" "),this.visitNode(e.returning);if(e.output)this.append(" "),this.visitNode(e.output);if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitMatched(e){if(e.not)this.append("not ");if(this.append("matched"),e.bySource)this.append(" by source")}visitAddIndex(e){if(this.append("add "),e.unique)this.append("unique ");if(this.append("index "),this.visitNode(e.name),e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");if(e.using)this.append(" using "),this.visitNode(e.using)}visitCast(e){this.append("cast("),this.visitNode(e.expression),this.append(" as "),this.visitNode(e.dataType),this.append(")")}visitFetch(e){this.append("fetch next "),this.visitNode(e.rowCount),this.append(` rows ${e.modifier}`)}visitOutput(e){this.append("output "),this.compileList(e.selections)}visitTop(e){if(this.append(`top(${e.expression})`),e.modifiers)this.append(` ${e.modifiers}`)}visitOrAction(e){this.append(e.action)}visitCollate(e){this.append("collate "),this.visitNode(e.collation)}append(e){this.#e+=e}appendValue(e){this.addParameter(e),this.append(this.getCurrentParameterPlaceholder())}getLeftIdentifierWrapper(){return'"'}getRightIdentifierWrapper(){return'"'}getCurrentParameterPlaceholder(){return"$"+this.numParameters}getLeftExplainOptionsWrapper(){return"("}getExplainOptionAssignment(){return" "}getExplainOptionsDelimiter(){return", "}getRightExplainOptionsWrapper(){return")"}sanitizeIdentifier(e){let t=this.getLeftIdentifierWrapper(),r=this.getRightIdentifierWrapper(),i="";for(let n of e)if(i+=n,n===t)i+=t;else if(n===r)i+=r;return i}sanitizeStringLiteral(e){return e.replace(oi,"''")}sanitizeJSONPathMemberValue(e){return e.replace(ni,(t)=>t==="'"?"''":"\\\"")}addParameter(e){this.#t.push(e)}appendImmediateValue(e){if(dr(e))this.appendStringLiteral(e);else if(zs(e)||Vi(e)||La(e))this.append(e.toString());else if(Fa(e))this.append("null");else if(rf(e))this.appendImmediateValue(e.toISOString());else throw Error(`invalid immediate value ${e}`)}appendStringLiteral(e){this.append("'"),this.append(this.sanitizeStringLiteral(e)),this.append("'")}sortSelectModifiers(e){return e.sort((t,r)=>t.modifier&&r.modifier?Ar[t.modifier]-Ar[r.modifier]:1),C(e)}compileColumnAlterations(e){this.compileList(e)}announcesNewColumnDataType(){return!0}}var si=C({ForKeyShare:"for key share",ForNoKeyUpdate:"for no key update",ForUpdate:"for update",ForShare:"for share",NoWait:"nowait",SkipLocked:"skip locked",Distinct:"distinct"}),Ar=C({ForKeyShare:1,ForNoKeyUpdate:1,ForUpdate:1,ForShare:1,NoWait:2,SkipLocked:2,Distinct:0}),ai=C({InnerJoin:"inner join",LeftJoin:"left join",RightJoin:"right join",FullJoin:"full join",CrossJoin:"cross join",LateralInnerJoin:"inner join lateral",LateralLeftJoin:"left join lateral",LateralCrossJoin:"cross join lateral",OuterApply:"outer apply",CrossApply:"cross apply",Using:"using"});var gm="kysely_migration",Am="kysely_migration_lock";function _m(e){let t=e.filter((i)=>i.pk>0),r=t.length===1?t[0]:void 0;return r?.type.toLowerCase()==="integer"?r.name:void 0}async function Tr(e,t,r){let i=_m(r);if(!i)return;let{rows:n}=await je`
2
+ function Mo(e){return typeof e>"u"||e===void 0}function fr(e){return typeof e==="string"}function Vs(e){return typeof e==="number"}function ta(e){return typeof e==="boolean"}function Va(e){return e===null}function xf(e){return e instanceof Date}function Ka(e){return typeof e==="bigint"}function fA(e){return typeof Buffer<"u"&&Buffer.isBuffer(e)}function pr(e){return typeof e==="function"}function an(e){return typeof e==="object"&&e!==null}function C(e){return Object.freeze(e)}function xe(e){if(x(e))return e;else return[e]}function x(e){return Array.isArray(e)}function Oo(e){return e}var it=C({is(e){return e.kind==="IdentifierNode"},create(e){return C({kind:"IdentifierNode",name:e})}});var pA=["preserve rows","delete rows","drop"],Ao=C({is(e){return e.kind==="CreateTableNode"},create(e){return C({kind:"CreateTableNode",table:e,columns:C([])})},cloneWithColumn(e,t){return C({...e,columns:C([...e.columns,t])})},cloneWithConstraint(e,t){return C({...e,constraints:e.constraints?C([...e.constraints,t]):C([t])})},cloneWithFrontModifier(e,t){return C({...e,frontModifiers:e.frontModifiers?C([...e.frontModifiers,t]):C([t])})},cloneWithEndModifier(e,t){return C({...e,endModifiers:e.endModifiers?C([...e.endModifiers,t]):C([t])})},cloneWith(e,t){return C({...e,...t})}});var _o=C({is(e){return e.kind==="SchemableIdentifierNode"},create(e){return C({kind:"SchemableIdentifierNode",identifier:it.create(e)})},createWithSchema(e,t){return C({kind:"SchemableIdentifierNode",schema:it.create(e),identifier:it.create(t)})}});var l=C({is(e){return e.kind==="AliasNode"},create(e,t){return C({kind:"AliasNode",node:e,alias:t})}});var yo=C({is(e){return e.kind==="TableNode"},create(e){return C({kind:"TableNode",table:_o.create(e)})},createWithSchema(e,t){return C({kind:"TableNode",table:_o.createWithSchema(e,t)})}});function Or(e){return an(e)&&pr(e.toOperationNode)}function Le(e){return an(e)&&"expressionType"in e&&Or(e)}function St(e){return an(e)&&"expression"in e&&fr(e.alias)&&Or(e)}var I=C({is(e){return e.kind==="SelectModifierNode"},create(e,t){return C({kind:"SelectModifierNode",modifier:e,of:t})},createWithExpression(e){return C({kind:"SelectModifierNode",rawModifier:e})}});var v=C({is(e){return e.kind==="AndNode"},create(e,t){return C({kind:"AndNode",left:e,right:t})}});var T=C({is(e){return e.kind==="OrNode"},create(e,t){return C({kind:"OrNode",left:e,right:t})}});var De=C({is(e){return e.kind==="OnNode"},create(e){return C({kind:"OnNode",on:e})},cloneWithOperation(e,t,r){return C({...e,on:t==="And"?v.create(e.on,r):T.create(e.on,r)})}});var L=C({is(e){return e.kind==="JoinNode"},create(e,t){return C({kind:"JoinNode",joinType:e,table:t,on:void 0})},createWithOn(e,t,r){return C({kind:"JoinNode",joinType:e,table:t,on:De.create(r)})},cloneWithOn(e,t){return C({...e,on:e.on?De.cloneWithOperation(e.on,"And",t):De.create(t)})}});var X=C({is(e){return e.kind==="BinaryOperationNode"},create(e,t,r){return C({kind:"BinaryOperationNode",leftOperand:e,operator:t,rightOperand:r})}});var Lr=["=","==","!=","<>",">",">=","<","<=","in","not in","is","is not","like","not like","match","ilike","not ilike","@>","<@","^@","&&","?","?&","?|","!<","!>","<=>","!~","~","~*","!~*","@@","@@@","!!","<->","regexp","is distinct from","is not distinct from"],Dr=["+","-","*","/","%","^","&","|","#","<<",">>"],Ct=["->","->>"],Br=[...Lr,...Dr,"&&","||"],qr=["exists","not exists"],Mr=["not","-",...qr],Et=[...Br,...Ct,...Mr,"between","between symmetric"],S=C({is(e){return e.kind==="OperatorNode"},create(e){return C({kind:"OperatorNode",operator:e})}});function Ke(e){return fr(e)&&Ct.includes(e)}var Jt=C({is(e){return e.kind==="ColumnNode"},create(e){return C({kind:"ColumnNode",column:it.create(e)})}});var Sc=C({is(e){return e.kind==="SelectAllNode"},create(){return C({kind:"SelectAllNode"})}});var Wa=C({is(e){return e.kind==="ReferenceNode"},create(e,t){return C({kind:"ReferenceNode",table:t,column:e})},createSelectAll(e){return C({kind:"ReferenceNode",table:e,column:Sc.create()})}});class Bm{#e;get dynamicReference(){return this.#e}get refType(){return}constructor(e){this.#e=e}toOperationNode(){return He(this.#e)}}function Be(e){return an(e)&&Or(e)&&fr(e.dynamicReference)}var D=C({is(e){return e.kind==="OrderByItemNode"},create(e,t){return C({kind:"OrderByItemNode",orderBy:e,direction:t})},cloneWith(e,t){return C({...e,...t})}});var nr=C({is(e){return e.kind==="RawNode"},create(e,t){return C({kind:"RawNode",sqlFragments:C(e),parameters:C(t)})},createWithSql(e){return nr.create([e],[])},createWithChild(e){return nr.create(["",""],[e])},createWithChildren(e){return nr.create(Array(e.length+1).fill(""),e)}});var At=C({is(e){return e.kind==="CollateNode"},create(e){return C({kind:"CollateNode",collation:it.create(e)})}});class Z{#e;constructor(e){this.#e=C(e)}desc(){return new Z({node:D.cloneWith(this.#e.node,{direction:nr.createWithSql("desc")})})}asc(){return new Z({node:D.cloneWith(this.#e.node,{direction:nr.createWithSql("asc")})})}nullsLast(){return new Z({node:D.cloneWith(this.#e.node,{nulls:"last"})})}nullsFirst(){return new Z({node:D.cloneWith(this.#e.node,{nulls:"first"})})}collate(e){return new Z({node:D.cloneWith(this.#e.node,{collation:At.create(e)})})}toOperationNode(){return this.#e.node}}var Rt=new Set;function vi(e){if(Rt.has(e))return;Rt.add(e),console.log(e)}function Xe(e){return e==="asc"||e==="desc"}function k(e){if(e.length===2)return[Ye(e[0],e[1])];if(e.length===1){let[t]=e;if(Array.isArray(t))return vi("orderBy(array) is deprecated, use multiple orderBy calls instead."),t.map((r)=>Ye(r));return[Ye(t)]}throw Error(`Invalid number of arguments at order by! expected 1-2, received ${e.length}`)}function Ye(e,t){let r=Pr(e);if(D.is(r)){if(t)throw Error("Cannot specify direction twice!");return r}return Tt(r,t)}function Pr(e){if(J(e))return Vo(e);if(Be(e))return e.toOperationNode();let[t,r]=e.split(" ");if(r)return vi("`orderBy('column asc')` is deprecated. Use `orderBy('column', 'asc')` instead."),Tt(ps(t),r);return ps(e)}function Tt(e,t){if(typeof t==="string"){if(!Xe(t))throw Error(`Invalid order by direction: ${t}`);return D.create(e,nr.createWithSql(t))}if(Le(t))return vi("`orderBy(..., expr)` is deprecated. Use `orderBy(..., 'asc')` or `orderBy(..., (ob) => ...)` instead."),D.create(e,t.toOperationNode());let r=D.create(e);if(!t)return r;return t(new Z({node:r})).toOperationNode()}var pe=C({is(e){return e.kind==="JSONReferenceNode"},create(e,t){return C({kind:"JSONReferenceNode",reference:e,traversal:t})},cloneWithTraversal(e,t){return C({...e,traversal:t})}});var qe=C({is(e){return e.kind==="JSONOperatorChainNode"},create(e){return C({kind:"JSONOperatorChainNode",operator:e,values:C([])})},cloneWithValue(e,t){return C({...e,values:C([...e.values,t])})}});var ee=C({is(e){return e.kind==="JSONPathNode"},create(e){return C({kind:"JSONPathNode",inOperator:e,pathLegs:C([])})},cloneWithLeg(e,t){return C({...e,pathLegs:C([...e.pathLegs,t])})}});function He(e){if(fr(e))return ps(e);return e.toOperationNode()}function Q(e){if(x(e))return e.map((t)=>w(t));else return[w(e)]}function w(e){if(J(e))return Vo(e);return He(e)}function Lt(e,t){let r=ps(e);if(Ke(t))return pe.create(r,qe.create(S.create(t)));let i=t.slice(0,-1);if(Ke(i))return pe.create(r,ee.create(S.create(i)));throw Error(`Invalid JSON operator: ${t}`)}function ps(e){if(!e.includes("."))return Wa.create(Jt.create(e));let r=e.split(".").map(Ze);if(r.length===3)return Ir(r);if(r.length===2)return Fr(r);throw Error(`invalid column reference ${e}`)}function Dt(e){if(e.includes(" as ")){let[r,i]=e.split(" as ").map(Ze);return l.create(ps(r),it.create(i))}else return ps(e)}function Um(e){return Jt.create(e)}function Ic(e){if(e.includes(" ")){let[r,i]=e.split(" ").map(Ze);if(!Xe(i))throw Error(`invalid order direction "${i}" next to "${r}"`);return k([r,i])[0]}else return Um(e)}function Ir(e){let[t,r,i]=e;return Wa.create(Jt.create(i),yo.createWithSchema(t,r))}function Fr(e){let[t,r]=e;return Wa.create(Jt.create(r),yo.create(t))}function Ze(e){return e.trim()}var Me=C({is(e){return e.kind==="PrimitiveValueListNode"},create(e){return C({kind:"PrimitiveValueListNode",values:C([...e])})}});var Ja=C({is(e){return e.kind==="ValueListNode"},create(e){return C({kind:"ValueListNode",values:C(e)})}});var bn=C({is(e){return e.kind==="ValueNode"},create(e){return C({kind:"ValueNode",value:e})},createImmediate(e){return C({kind:"ValueNode",value:e,immediate:!0})}});function Bt(e){if(x(e))return Vr(e);return d(e)}function d(e){if(J(e))return Vo(e);return bn.create(e)}function Pe(e){return Vs(e)||ta(e)||Va(e)}function ge(e){if(!Pe(e))throw Error(`unsafe immediate value ${JSON.stringify(e)}`);return bn.createImmediate(e)}function Vr(e){if(e.some(J))return Ja.create(e.map((t)=>d(t)));return Me.create(e)}var E=C({is(e){return e.kind==="ParensNode"},create(e){return C({kind:"ParensNode",node:e})}});function br(e){if(e.length===3)return Ie(e[0],e[1],e[2]);else if(e.length===1)return d(e[0]);throw Error(`invalid arguments: ${JSON.stringify(e)}`)}function Ie(e,t,r){if(Ur(t)&&Mt(r))return X.create(w(e),et(t),bn.createImmediate(r));return X.create(w(e),et(t),Bt(r))}function N(e,t,r){return X.create(w(e),et(t),w(r))}function tt(e,t){return de(Object.entries(e).filter(([,r])=>!Mo(r)).map(([r,i])=>Ie(r,Mt(i)?"is":"=",i)),t)}function de(e,t,r=!0){let i=t==="and"?v.create:T.create;if(e.length===0)return X.create(bn.createImmediate(1),S.create("="),bn.createImmediate(t==="and"?1:0));let n=qt(e[0]);for(let a=1;a<e.length;++a)n=i(n,qt(e[a]));if(e.length>1&&r)return E.create(n);return n}function Ur(e){return e==="is"||e==="is not"}function Mt(e){return Va(e)||ta(e)}function et(e){if(fr(e)&&Et.includes(e))return S.create(e);if(Or(e))return e.toOperationNode();throw Error(`invalid operator ${JSON.stringify(e)}`)}function qt(e){return Or(e)?e.toOperationNode():e}var _=C({is(e){return e.kind==="OrderByNode"},create(e){return C({kind:"OrderByNode",items:C([...e])})},cloneWithItems(e,t){return C({...e,items:C([...e.items,...t])})}});var rt=C({is(e){return e.kind==="PartitionByNode"},create(e){return C({kind:"PartitionByNode",items:C(e)})},cloneWithItems(e,t){return C({...e,items:C([...e.items,...t])})}});var ve=C({is(e){return e.kind==="OverNode"},create(){return C({kind:"OverNode"})},cloneWithOrderByItems(e,t){return C({...e,orderBy:e.orderBy?_.cloneWithItems(e.orderBy,t):_.create(t)})},cloneWithPartitionByItems(e,t){return C({...e,partitionBy:e.partitionBy?rt.cloneWithItems(e.partitionBy,t):rt.create(t)})}});var oe=C({is(e){return e.kind==="FromNode"},create(e){return C({kind:"FromNode",froms:C(e)})},cloneWithFroms(e,t){return C({...e,froms:C([...e.froms,...t])})}});var ot=C({is(e){return e.kind==="GroupByNode"},create(e){return C({kind:"GroupByNode",items:C(e)})},cloneWithItems(e,t){return C({...e,items:C([...e.items,...t])})}});var nt=C({is(e){return e.kind==="HavingNode"},create(e){return C({kind:"HavingNode",having:e})},cloneWithOperation(e,t,r){return C({...e,having:t==="And"?v.create(e.having,r):T.create(e.having,r)})}});var u=C({is(e){return e.kind==="InsertQueryNode"},create(e,t,r){return C({kind:"InsertQueryNode",into:e,...t&&{with:t},replace:r})},createWithoutInto(){return C({kind:"InsertQueryNode"})},cloneWith(e,t){return C({...e,...t})}});var Fe=C({is(e){return e.kind==="ListNode"},create(e){return C({kind:"ListNode",items:C(e)})}});var F=C({is(e){return e.kind==="UpdateQueryNode"},create(e,t){return C({kind:"UpdateQueryNode",table:e.length===1?e[0]:Fe.create(e),...t&&{with:t}})},createWithoutTable(){return C({kind:"UpdateQueryNode"})},cloneWithFromItems(e,t){return C({...e,from:e.from?oe.cloneWithFroms(e.from,t):oe.create(t)})},cloneWithUpdates(e,t){return C({...e,updates:e.updates?C([...e.updates,...t]):t})},cloneWithLimit(e,t){return C({...e,limit:t})}});var be=C({is(e){return e.kind==="UsingNode"},create(e){return C({kind:"UsingNode",tables:C(e)})},cloneWithTables(e,t){return C({...e,tables:C([...e.tables,...t])})}});var te=C({is(e){return e.kind==="DeleteQueryNode"},create(e,t){return C({kind:"DeleteQueryNode",from:oe.create(e),...t&&{with:t}})},cloneWithOrderByItems:(e,t)=>Oe.cloneWithOrderByItems(e,t),cloneWithoutOrderBy:(e)=>Oe.cloneWithoutOrderBy(e),cloneWithLimit(e,t){return C({...e,limit:t})},cloneWithoutLimit(e){return C({...e,limit:void 0})},cloneWithUsing(e,t){return C({...e,using:e.using!==void 0?be.cloneWithTables(e.using,t):be.create(t)})}});var O=C({is(e){return e.kind==="WhereNode"},create(e){return C({kind:"WhereNode",where:e})},cloneWithOperation(e,t,r){return C({...e,where:t==="And"?v.create(e.where,r):T.create(e.where,r)})}});var st=C({is(e){return e.kind==="ReturningNode"},create(e){return C({kind:"ReturningNode",selections:C(e)})},cloneWithSelections(e,t){return C({...e,selections:e.selections?C([...e.selections,...t]):C(t)})}});var Pt=C({is(e){return e.kind==="ExplainNode"},create(e,t){return C({kind:"ExplainNode",format:e,options:t})}});var R=C({is(e){return e.kind==="WhenNode"},create(e){return C({kind:"WhenNode",condition:e})},cloneWithResult(e,t){return C({...e,result:t})}});var A=C({is(e){return e.kind==="MergeQueryNode"},create(e,t){return C({kind:"MergeQueryNode",into:e,...t&&{with:t}})},cloneWithUsing(e,t){return C({...e,using:t})},cloneWithWhen(e,t){return C({...e,whens:e.whens?C([...e.whens,t]):C([t])})},cloneWithThen(e,t){return C({...e,whens:e.whens?C([...e.whens.slice(0,-1),R.cloneWithResult(e.whens[e.whens.length-1],t)]):void 0})}});var at=C({is(e){return e.kind==="OutputNode"},create(e){return C({kind:"OutputNode",selections:C(e)})},cloneWithSelections(e,t){return C({...e,selections:e.selections?C([...e.selections,...t]):C(t)})}});var Oe=C({is(e){return sr.is(e)||u.is(e)||F.is(e)||te.is(e)||A.is(e)},cloneWithEndModifier(e,t){return C({...e,endModifiers:e.endModifiers?C([...e.endModifiers,t]):C([t])})},cloneWithWhere(e,t){return C({...e,where:e.where?O.cloneWithOperation(e.where,"And",t):O.create(t)})},cloneWithJoin(e,t){return C({...e,joins:e.joins?C([...e.joins,t]):C([t])})},cloneWithReturning(e,t){return C({...e,returning:e.returning?st.cloneWithSelections(e.returning,t):st.create(t)})},cloneWithoutReturning(e){return C({...e,returning:void 0})},cloneWithoutWhere(e){return C({...e,where:void 0})},cloneWithExplain(e,t,r){return C({...e,explain:Pt.create(t,r?.toOperationNode())})},cloneWithTop(e,t){return C({...e,top:t})},cloneWithOutput(e,t){return C({...e,output:e.output?at.cloneWithSelections(e.output,t):at.create(t)})},cloneWithOrderByItems(e,t){return C({...e,orderBy:e.orderBy?_.cloneWithItems(e.orderBy,t):_.create(t)})},cloneWithoutOrderBy(e){return C({...e,orderBy:void 0})}});var sr=C({is(e){return e.kind==="SelectQueryNode"},create(e){return C({kind:"SelectQueryNode",...e&&{with:e}})},createFrom(e,t){return C({kind:"SelectQueryNode",from:oe.create(e),...t&&{with:t}})},cloneWithSelections(e,t){return C({...e,selections:e.selections?C([...e.selections,...t]):C(t)})},cloneWithDistinctOn(e,t){return C({...e,distinctOn:e.distinctOn?C([...e.distinctOn,...t]):C(t)})},cloneWithFrontModifier(e,t){return C({...e,frontModifiers:e.frontModifiers?C([...e.frontModifiers,t]):C([t])})},cloneWithOrderByItems:(e,t)=>Oe.cloneWithOrderByItems(e,t),cloneWithGroupByItems(e,t){return C({...e,groupBy:e.groupBy?ot.cloneWithItems(e.groupBy,t):ot.create(t)})},cloneWithLimit(e,t){return C({...e,limit:t})},cloneWithOffset(e,t){return C({...e,offset:t})},cloneWithFetch(e,t){return C({...e,fetch:t})},cloneWithHaving(e,t){return C({...e,having:e.having?nt.cloneWithOperation(e.having,"And",t):nt.create(t)})},cloneWithSetOperations(e,t){return C({...e,setOperations:e.setOperations?C([...e.setOperations,...t]):C([...t])})},cloneWithoutSelections(e){return C({...e,selections:[]})},cloneWithoutLimit(e){return C({...e,limit:void 0})},cloneWithoutOffset(e){return C({...e,offset:void 0})},cloneWithoutOrderBy:(e)=>Oe.cloneWithoutOrderBy(e),cloneWithoutGroupBy(e){return C({...e,groupBy:void 0})}});class he{#e;constructor(e){this.#e=C(e)}on(...e){return new he({...this.#e,joinNode:L.cloneWithOn(this.#e.joinNode,br(e))})}onRef(e,t,r){return new he({...this.#e,joinNode:L.cloneWithOn(this.#e.joinNode,N(e,t,r))})}onTrue(){return new he({...this.#e,joinNode:L.cloneWithOn(this.#e.joinNode,nr.createWithSql("true"))})}$call(e){return e(this)}toOperationNode(){return this.#e.joinNode}}var It=C({is(e){return e.kind==="PartitionByItemNode"},create(e){return C({kind:"PartitionByItemNode",partitionBy:e})}});function Ft(e){return Q(e).map(It.create)}class ue{#e;constructor(e){this.#e=C(e)}orderBy(...e){return new ue({overNode:ve.cloneWithOrderByItems(this.#e.overNode,k(e))})}clearOrderBy(){return new ue({overNode:Oe.cloneWithoutOrderBy(this.#e.overNode)})}partitionBy(e){return new ue({overNode:ve.cloneWithPartitionByItems(this.#e.overNode,Ft(e))})}$call(e){return e(this)}toOperationNode(){return this.#e.overNode}}var me=C({is(e){return e.kind==="SelectionNode"},create(e){return C({kind:"SelectionNode",selection:e})},createSelectAll(){return C({kind:"SelectionNode",selection:Sc.create()})},createSelectAllFromTable(e){return C({kind:"SelectionNode",selection:Wa.createSelectAll(e)})}});function c(e){if(pr(e))return c(e(B()));else if(x(e))return e.map((t)=>Vt(t));else return[Vt(e)]}function Vt(e){if(fr(e))return me.create(Dt(e));else if(Be(e))return me.create(e.toOperationNode());else return me.create(Ve(e))}function y(e){if(!e)return[me.createSelectAll()];else if(Array.isArray(e))return e.map(Ut);else return[Ut(e)]}function Ut(e){if(fr(e))return me.createSelectAllFromTable(ir(e));throw Error(`invalid value selectAll expression: ${JSON.stringify(e)}`)}var zt=C({is(e){return e.kind==="ValuesNode"},create(e){return C({kind:"ValuesNode",values:C(e)})}});var Qt=C({is(e){return e.kind==="DefaultInsertValueNode"},create(){return C({kind:"DefaultInsertValueNode"})}});function Ue(e){let t=pr(e)?e(B()):e,r=x(t)?t:C([t]);return zr(r)}function zr(e){let t=Jr(e);return[C([...t.keys()].map(Jt.create)),zt.create(e.map((r)=>Qr(r,t)))]}function Jr(e){let t=new Map;for(let r of e){let i=Object.keys(r);for(let n of i)if(!t.has(n)&&r[n]!==void 0)t.set(n,t.size)}return t}function Qr(e,t){let r=Object.keys(e),i=Array.from({length:t.size}),n=!1,a=r.length;for(let H of r){let Y=t.get(H);if(Mo(Y)){a--;continue}let we=e[H];if(Mo(we)||J(we))n=!0;i[Y]=we}if(a<t.size||n){let H=Qt.create();return Ja.create(i.map((Y)=>Mo(Y)?H:d(Y)))}return Me.create(i)}var pt=C({is(e){return e.kind==="ColumnUpdateNode"},create(e,t){return C({kind:"ColumnUpdateNode",column:e,value:t})}});function _t(...e){if(e.length===2)return[pt.create(w(e[0]),d(e[1]))];return We(e[0])}function We(e){let t=pr(e)?e(B()):e;return Object.entries(t).filter(([r,i])=>i!==void 0).map(([r,i])=>pt.create(Jt.create(r),d(i)))}var $t=C({is(e){return e.kind==="OnDuplicateKeyNode"},create(e){return C({kind:"OnDuplicateKeyNode",updates:e})}});class dt{insertId;numInsertedOrUpdatedRows;constructor(e,t){this.insertId=e,this.numInsertedOrUpdatedRows=t}}class q extends Error{node;constructor(e){super("no result");this.node=e}}function V(e){return Object.prototype.hasOwnProperty.call(e,"prototype")}var g=C({is(e){return e.kind==="OnConflictNode"},create(){return C({kind:"OnConflictNode"})},cloneWith(e,t){return C({...e,...t})},cloneWithIndexWhere(e,t){return C({...e,indexWhere:e.indexWhere?O.cloneWithOperation(e.indexWhere,"And",t):O.create(t)})},cloneWithIndexOrWhere(e,t){return C({...e,indexWhere:e.indexWhere?O.cloneWithOperation(e.indexWhere,"Or",t):O.create(t)})},cloneWithUpdateWhere(e,t){return C({...e,updateWhere:e.updateWhere?O.cloneWithOperation(e.updateWhere,"And",t):O.create(t)})},cloneWithUpdateOrWhere(e,t){return C({...e,updateWhere:e.updateWhere?O.cloneWithOperation(e.updateWhere,"Or",t):O.create(t)})},cloneWithoutIndexWhere(e){return C({...e,indexWhere:void 0})},cloneWithoutUpdateWhere(e){return C({...e,updateWhere:void 0})}});class U{#e;constructor(e){this.#e=C(e)}column(e){let t=Jt.create(e);return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{columns:this.#e.onConflictNode.columns?C([...this.#e.onConflictNode.columns,t]):C([t])})})}columns(e){let t=e.map(Jt.create);return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{columns:this.#e.onConflictNode.columns?C([...this.#e.onConflictNode.columns,...t]):C(t)})})}constraint(e){return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{constraint:it.create(e)})})}expression(e){return new U({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{indexExpression:e.toOperationNode()})})}where(...e){return new U({...this.#e,onConflictNode:g.cloneWithIndexWhere(this.#e.onConflictNode,br(e))})}whereRef(e,t,r){return new U({...this.#e,onConflictNode:g.cloneWithIndexWhere(this.#e.onConflictNode,N(e,t,r))})}clearWhere(){return new U({...this.#e,onConflictNode:g.cloneWithoutIndexWhere(this.#e.onConflictNode)})}doNothing(){return new Gt({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{doNothing:!0})})}doUpdateSet(e){return new Se({...this.#e,onConflictNode:g.cloneWith(this.#e.onConflictNode,{updates:We(e)})})}$call(e){return e(this)}}class Gt{#e;constructor(e){this.#e=C(e)}toOperationNode(){return this.#e.onConflictNode}}class Se{#e;constructor(e){this.#e=C(e)}where(...e){return new Se({...this.#e,onConflictNode:g.cloneWithUpdateWhere(this.#e.onConflictNode,br(e))})}whereRef(e,t,r){return new Se({...this.#e,onConflictNode:g.cloneWithUpdateWhere(this.#e.onConflictNode,N(e,t,r))})}clearWhere(){return new Se({...this.#e,onConflictNode:g.cloneWithoutUpdateWhere(this.#e.onConflictNode)})}$call(e){return e(this)}toOperationNode(){return this.#e.onConflictNode}}var Kt=C({is(e){return e.kind==="TopNode"},create(e,t){return C({kind:"TopNode",expression:e,modifiers:t})}});function M(e,t){if(!Vs(e)&&!Ka(e))throw Error(`Invalid top expression: ${e}`);if(!Mo(t)&&!_r(t))throw Error(`Invalid top modifiers: ${t}`);return Kt.create(e,t)}function _r(e){return e==="percent"||e==="with ties"||e==="percent with ties"}var ne=C({is(e){return e.kind==="OrActionNode"},create(e){return C({kind:"OrActionNode",action:e})}});class h{#e;constructor(e){this.#e=C(e)}values(e){let[t,r]=Ue(e);return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{columns:t,values:r})})}columns(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{columns:C(e.map(Jt.create))})})}expression(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{values:Vo(e)})})}defaultValues(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{defaultValues:!0})})}modifyEnd(e){return new h({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}ignore(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("ignore")})})}orIgnore(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("ignore")})})}orAbort(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("abort")})})}orFail(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("fail")})})}orReplace(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("replace")})})}orRollback(){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{orAction:ne.create("rollback")})})}top(e,t){return new h({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}onConflict(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{onConflict:e(new U({onConflictNode:g.create()})).toOperationNode()})})}onDuplicateKeyUpdate(e){return new h({...this.#e,queryNode:u.cloneWith(this.#e.queryNode,{onDuplicateKey:$t.create(We(e))})})}returning(e){return new h({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(){return new h({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y())})}output(e){return new h({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new h({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}clearReturning(){return new h({...this.#e,queryNode:Oe.cloneWithoutReturning(this.#e.queryNode)})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new h({...this.#e})}$castTo(){return new h(this.#e)}$narrowType(){return new h(this.#e)}$assertType(){return new h(this.#e)}withPlugin(e){return new h({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new dt(t.insertId,t.numAffectedRows??BigInt(0))]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new h({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}class ht{numDeletedRows;constructor(e){this.numDeletedRows=e}}var fe=C({is(e){return e.kind==="LimitNode"},create(e){return C({kind:"LimitNode",limit:e})}});var m;class ze{#e;constructor(e){this.#e=C(e)}where(...e){return new m({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,br(e))})}whereRef(e,t,r){return new m({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,N(e,t,r))})}clearWhere(){return new m({...this.#e,queryNode:Oe.cloneWithoutWhere(this.#e.queryNode)})}top(e,t){return new m({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}using(e){return new m({...this.#e,queryNode:te.cloneWithUsing(this.#e.queryNode,P(e))})}innerJoin(...e){return this.#t("InnerJoin",e)}leftJoin(...e){return this.#t("LeftJoin",e)}rightJoin(...e){return this.#t("RightJoin",e)}fullJoin(...e){return this.#t("FullJoin",e)}#t(e,t){return new m({...this.#e,queryNode:Oe.cloneWithJoin(this.#e.queryNode,re(e,t))})}returning(e){return new m({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new m({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new m({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new m({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}clearReturning(){return new m({...this.#e,queryNode:Oe.cloneWithoutReturning(this.#e.queryNode)})}clearLimit(){return new m({...this.#e,queryNode:te.cloneWithoutLimit(this.#e.queryNode)})}orderBy(...e){return new m({...this.#e,queryNode:Oe.cloneWithOrderByItems(this.#e.queryNode,k(e))})}clearOrderBy(){return new m({...this.#e,queryNode:Oe.cloneWithoutOrderBy(this.#e.queryNode)})}limit(e){return new m({...this.#e,queryNode:te.cloneWithLimit(this.#e.queryNode,fe.create(d(e)))})}modifyEnd(e){return new m({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new m({...this.#e})}$castTo(){return new m(this.#e)}$narrowType(){return new m(this.#e)}$assertType(){return new m(this.#e)}withPlugin(e){return new m({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new ht(t.numAffectedRows??BigInt(0))]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new m({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}m=ze;class ut{numUpdatedRows;numChangedRows;constructor(e,t){this.numUpdatedRows=e,this.numChangedRows=t}}var f;class ce{#e;constructor(e){this.#e=C(e)}where(...e){return new f({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,br(e))})}whereRef(e,t,r){return new f({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,N(e,t,r))})}clearWhere(){return new f({...this.#e,queryNode:Oe.cloneWithoutWhere(this.#e.queryNode)})}top(e,t){return new f({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}from(e){return new f({...this.#e,queryNode:F.cloneWithFromItems(this.#e.queryNode,P(e))})}innerJoin(...e){return this.#t("InnerJoin",e)}leftJoin(...e){return this.#t("LeftJoin",e)}rightJoin(...e){return this.#t("RightJoin",e)}fullJoin(...e){return this.#t("FullJoin",e)}#t(e,t){return new f({...this.#e,queryNode:Oe.cloneWithJoin(this.#e.queryNode,re(e,t))})}orderBy(...e){return new f({...this.#e,queryNode:Oe.cloneWithOrderByItems(this.#e.queryNode,k(e))})}clearOrderBy(){return new f({...this.#e,queryNode:Oe.cloneWithoutOrderBy(this.#e.queryNode)})}limit(e){return new f({...this.#e,queryNode:F.cloneWithLimit(this.#e.queryNode,fe.create(d(e)))})}set(...e){return new f({...this.#e,queryNode:F.cloneWithUpdates(this.#e.queryNode,_t(...e))})}returning(e){return new f({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new f({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new f({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new f({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}modifyEnd(e){return new f({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}clearReturning(){return new f({...this.#e,queryNode:Oe.cloneWithoutReturning(this.#e.queryNode)})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new f({...this.#e})}$castTo(){return new f(this.#e)}$narrowType(){return new f(this.#e)}$assertType(){return new f(this.#e)}withPlugin(e){return new f({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new ut(t.numAffectedRows??BigInt(0),t.numChangedRows)]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new f({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}f=ce;var mt=C({is(e){return e.kind==="CommonTableExpressionNameNode"},create(e,t){return C({kind:"CommonTableExpressionNameNode",table:yo.create(e),columns:t?C(t.map(Jt.create)):void 0})}});var le=C({is(e){return e.kind==="CommonTableExpressionNode"},create(e,t){return C({kind:"CommonTableExpressionNode",name:e,expression:t})},cloneWith(e,t){return C({...e,...t})}});class Ce{#e;constructor(e){this.#e=C(e)}materialized(){return new Ce({...this.#e,node:le.cloneWith(this.#e.node,{materialized:!0})})}notMaterialized(){return new Ce({...this.#e,node:le.cloneWith(this.#e.node,{materialized:!1})})}toOperationNode(){return this.#e.node}}function ft(e,t){let r=t(Ht()).toOperationNode();if(pr(e))return e($r(r)).toOperationNode();return le.create(jt(e),r)}function $r(e){return(t)=>new Ce({node:le.create(jt(t),e)})}function jt(e){if(e.includes("(")){let t=e.split(/[\(\)]/),r=t[0],i=t[1].split(",").map((n)=>n.trim());return mt.create(r,i)}else return mt.create(e)}var ke=C({is(e){return e.kind==="WithNode"},create(e,t){return C({kind:"WithNode",expressions:C([e]),...t})},cloneWithExpression(e,t){return C({...e,expressions:C([...e.expressions,t])})}});var Yt=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"];function Cf(e){let t="";for(let r=0;r<e;++r)t+=Gr();return t}function Gr(){return Yt[~~(Math.random()*Yt.length)]}function kt(){return new Xt}class Xt{#e;get queryId(){if(this.#e===void 0)this.#e=Cf(8);return this.#e}}function o(e){return e}class Ju{nodeStack=[];#e=C({AliasNode:this.transformAlias.bind(this),ColumnNode:this.transformColumn.bind(this),IdentifierNode:this.transformIdentifier.bind(this),SchemableIdentifierNode:this.transformSchemableIdentifier.bind(this),RawNode:this.transformRaw.bind(this),ReferenceNode:this.transformReference.bind(this),SelectQueryNode:this.transformSelectQuery.bind(this),SelectionNode:this.transformSelection.bind(this),TableNode:this.transformTable.bind(this),FromNode:this.transformFrom.bind(this),SelectAllNode:this.transformSelectAll.bind(this),AndNode:this.transformAnd.bind(this),OrNode:this.transformOr.bind(this),ValueNode:this.transformValue.bind(this),ValueListNode:this.transformValueList.bind(this),PrimitiveValueListNode:this.transformPrimitiveValueList.bind(this),ParensNode:this.transformParens.bind(this),JoinNode:this.transformJoin.bind(this),OperatorNode:this.transformOperator.bind(this),WhereNode:this.transformWhere.bind(this),InsertQueryNode:this.transformInsertQuery.bind(this),DeleteQueryNode:this.transformDeleteQuery.bind(this),ReturningNode:this.transformReturning.bind(this),CreateTableNode:this.transformCreateTable.bind(this),AddColumnNode:this.transformAddColumn.bind(this),ColumnDefinitionNode:this.transformColumnDefinition.bind(this),DropTableNode:this.transformDropTable.bind(this),DataTypeNode:this.transformDataType.bind(this),OrderByNode:this.transformOrderBy.bind(this),OrderByItemNode:this.transformOrderByItem.bind(this),GroupByNode:this.transformGroupBy.bind(this),GroupByItemNode:this.transformGroupByItem.bind(this),UpdateQueryNode:this.transformUpdateQuery.bind(this),ColumnUpdateNode:this.transformColumnUpdate.bind(this),LimitNode:this.transformLimit.bind(this),OffsetNode:this.transformOffset.bind(this),OnConflictNode:this.transformOnConflict.bind(this),OnDuplicateKeyNode:this.transformOnDuplicateKey.bind(this),CreateIndexNode:this.transformCreateIndex.bind(this),DropIndexNode:this.transformDropIndex.bind(this),ListNode:this.transformList.bind(this),PrimaryKeyConstraintNode:this.transformPrimaryKeyConstraint.bind(this),UniqueConstraintNode:this.transformUniqueConstraint.bind(this),ReferencesNode:this.transformReferences.bind(this),CheckConstraintNode:this.transformCheckConstraint.bind(this),WithNode:this.transformWith.bind(this),CommonTableExpressionNode:this.transformCommonTableExpression.bind(this),CommonTableExpressionNameNode:this.transformCommonTableExpressionName.bind(this),HavingNode:this.transformHaving.bind(this),CreateSchemaNode:this.transformCreateSchema.bind(this),DropSchemaNode:this.transformDropSchema.bind(this),AlterTableNode:this.transformAlterTable.bind(this),DropColumnNode:this.transformDropColumn.bind(this),RenameColumnNode:this.transformRenameColumn.bind(this),AlterColumnNode:this.transformAlterColumn.bind(this),ModifyColumnNode:this.transformModifyColumn.bind(this),AddConstraintNode:this.transformAddConstraint.bind(this),DropConstraintNode:this.transformDropConstraint.bind(this),RenameConstraintNode:this.transformRenameConstraint.bind(this),ForeignKeyConstraintNode:this.transformForeignKeyConstraint.bind(this),CreateViewNode:this.transformCreateView.bind(this),RefreshMaterializedViewNode:this.transformRefreshMaterializedView.bind(this),DropViewNode:this.transformDropView.bind(this),GeneratedNode:this.transformGenerated.bind(this),DefaultValueNode:this.transformDefaultValue.bind(this),OnNode:this.transformOn.bind(this),ValuesNode:this.transformValues.bind(this),SelectModifierNode:this.transformSelectModifier.bind(this),CreateTypeNode:this.transformCreateType.bind(this),DropTypeNode:this.transformDropType.bind(this),ExplainNode:this.transformExplain.bind(this),DefaultInsertValueNode:this.transformDefaultInsertValue.bind(this),AggregateFunctionNode:this.transformAggregateFunction.bind(this),OverNode:this.transformOver.bind(this),PartitionByNode:this.transformPartitionBy.bind(this),PartitionByItemNode:this.transformPartitionByItem.bind(this),SetOperationNode:this.transformSetOperation.bind(this),BinaryOperationNode:this.transformBinaryOperation.bind(this),UnaryOperationNode:this.transformUnaryOperation.bind(this),UsingNode:this.transformUsing.bind(this),FunctionNode:this.transformFunction.bind(this),CaseNode:this.transformCase.bind(this),WhenNode:this.transformWhen.bind(this),JSONReferenceNode:this.transformJSONReference.bind(this),JSONPathNode:this.transformJSONPath.bind(this),JSONPathLegNode:this.transformJSONPathLeg.bind(this),JSONOperatorChainNode:this.transformJSONOperatorChain.bind(this),TupleNode:this.transformTuple.bind(this),MergeQueryNode:this.transformMergeQuery.bind(this),MatchedNode:this.transformMatched.bind(this),AddIndexNode:this.transformAddIndex.bind(this),CastNode:this.transformCast.bind(this),FetchNode:this.transformFetch.bind(this),TopNode:this.transformTop.bind(this),OutputNode:this.transformOutput.bind(this),OrActionNode:this.transformOrAction.bind(this),CollateNode:this.transformCollate.bind(this)});transformNode(e,t){if(!e)return e;this.nodeStack.push(e);let r=this.transformNodeImpl(e,t);return this.nodeStack.pop(),C(r)}transformNodeImpl(e,t){return this.#e[e.kind](e,t)}transformNodeList(e,t){if(!e)return e;return C(e.map((r)=>this.transformNode(r,t)))}transformSelectQuery(e,t){return o({kind:"SelectQueryNode",from:this.transformNode(e.from,t),selections:this.transformNodeList(e.selections,t),distinctOn:this.transformNodeList(e.distinctOn,t),joins:this.transformNodeList(e.joins,t),groupBy:this.transformNode(e.groupBy,t),orderBy:this.transformNode(e.orderBy,t),where:this.transformNode(e.where,t),frontModifiers:this.transformNodeList(e.frontModifiers,t),endModifiers:this.transformNodeList(e.endModifiers,t),limit:this.transformNode(e.limit,t),offset:this.transformNode(e.offset,t),with:this.transformNode(e.with,t),having:this.transformNode(e.having,t),explain:this.transformNode(e.explain,t),setOperations:this.transformNodeList(e.setOperations,t),fetch:this.transformNode(e.fetch,t),top:this.transformNode(e.top,t)})}transformSelection(e,t){return o({kind:"SelectionNode",selection:this.transformNode(e.selection,t)})}transformColumn(e,t){return o({kind:"ColumnNode",column:this.transformNode(e.column,t)})}transformAlias(e,t){return o({kind:"AliasNode",node:this.transformNode(e.node,t),alias:this.transformNode(e.alias,t)})}transformTable(e,t){return o({kind:"TableNode",table:this.transformNode(e.table,t)})}transformFrom(e,t){return o({kind:"FromNode",froms:this.transformNodeList(e.froms,t)})}transformReference(e,t){return o({kind:"ReferenceNode",column:this.transformNode(e.column,t),table:this.transformNode(e.table,t)})}transformAnd(e,t){return o({kind:"AndNode",left:this.transformNode(e.left,t),right:this.transformNode(e.right,t)})}transformOr(e,t){return o({kind:"OrNode",left:this.transformNode(e.left,t),right:this.transformNode(e.right,t)})}transformValueList(e,t){return o({kind:"ValueListNode",values:this.transformNodeList(e.values,t)})}transformParens(e,t){return o({kind:"ParensNode",node:this.transformNode(e.node,t)})}transformJoin(e,t){return o({kind:"JoinNode",joinType:e.joinType,table:this.transformNode(e.table,t),on:this.transformNode(e.on,t)})}transformRaw(e,t){return o({kind:"RawNode",sqlFragments:C([...e.sqlFragments]),parameters:this.transformNodeList(e.parameters,t)})}transformWhere(e,t){return o({kind:"WhereNode",where:this.transformNode(e.where,t)})}transformInsertQuery(e,t){return o({kind:"InsertQueryNode",into:this.transformNode(e.into,t),columns:this.transformNodeList(e.columns,t),values:this.transformNode(e.values,t),returning:this.transformNode(e.returning,t),onConflict:this.transformNode(e.onConflict,t),onDuplicateKey:this.transformNode(e.onDuplicateKey,t),endModifiers:this.transformNodeList(e.endModifiers,t),with:this.transformNode(e.with,t),ignore:e.ignore,orAction:this.transformNode(e.orAction,t),replace:e.replace,explain:this.transformNode(e.explain,t),defaultValues:e.defaultValues,top:this.transformNode(e.top,t),output:this.transformNode(e.output,t)})}transformValues(e,t){return o({kind:"ValuesNode",values:this.transformNodeList(e.values,t)})}transformDeleteQuery(e,t){return o({kind:"DeleteQueryNode",from:this.transformNode(e.from,t),using:this.transformNode(e.using,t),joins:this.transformNodeList(e.joins,t),where:this.transformNode(e.where,t),returning:this.transformNode(e.returning,t),endModifiers:this.transformNodeList(e.endModifiers,t),with:this.transformNode(e.with,t),orderBy:this.transformNode(e.orderBy,t),limit:this.transformNode(e.limit,t),explain:this.transformNode(e.explain,t),top:this.transformNode(e.top,t),output:this.transformNode(e.output,t)})}transformReturning(e,t){return o({kind:"ReturningNode",selections:this.transformNodeList(e.selections,t)})}transformCreateTable(e,t){return o({kind:"CreateTableNode",table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t),constraints:this.transformNodeList(e.constraints,t),temporary:e.temporary,ifNotExists:e.ifNotExists,onCommit:e.onCommit,frontModifiers:this.transformNodeList(e.frontModifiers,t),endModifiers:this.transformNodeList(e.endModifiers,t),selectQuery:this.transformNode(e.selectQuery,t)})}transformColumnDefinition(e,t){return o({kind:"ColumnDefinitionNode",column:this.transformNode(e.column,t),dataType:this.transformNode(e.dataType,t),references:this.transformNode(e.references,t),primaryKey:e.primaryKey,autoIncrement:e.autoIncrement,unique:e.unique,notNull:e.notNull,unsigned:e.unsigned,defaultTo:this.transformNode(e.defaultTo,t),check:this.transformNode(e.check,t),generated:this.transformNode(e.generated,t),frontModifiers:this.transformNodeList(e.frontModifiers,t),endModifiers:this.transformNodeList(e.endModifiers,t),nullsNotDistinct:e.nullsNotDistinct,identity:e.identity,ifNotExists:e.ifNotExists})}transformAddColumn(e,t){return o({kind:"AddColumnNode",column:this.transformNode(e.column,t)})}transformDropTable(e,t){return o({kind:"DropTableNode",table:this.transformNode(e.table,t),ifExists:e.ifExists,cascade:e.cascade})}transformOrderBy(e,t){return o({kind:"OrderByNode",items:this.transformNodeList(e.items,t)})}transformOrderByItem(e,t){return o({kind:"OrderByItemNode",orderBy:this.transformNode(e.orderBy,t),direction:this.transformNode(e.direction,t),collation:this.transformNode(e.collation,t),nulls:e.nulls})}transformGroupBy(e,t){return o({kind:"GroupByNode",items:this.transformNodeList(e.items,t)})}transformGroupByItem(e,t){return o({kind:"GroupByItemNode",groupBy:this.transformNode(e.groupBy,t)})}transformUpdateQuery(e,t){return o({kind:"UpdateQueryNode",table:this.transformNode(e.table,t),from:this.transformNode(e.from,t),joins:this.transformNodeList(e.joins,t),where:this.transformNode(e.where,t),updates:this.transformNodeList(e.updates,t),returning:this.transformNode(e.returning,t),endModifiers:this.transformNodeList(e.endModifiers,t),with:this.transformNode(e.with,t),explain:this.transformNode(e.explain,t),limit:this.transformNode(e.limit,t),top:this.transformNode(e.top,t),output:this.transformNode(e.output,t),orderBy:this.transformNode(e.orderBy,t)})}transformColumnUpdate(e,t){return o({kind:"ColumnUpdateNode",column:this.transformNode(e.column,t),value:this.transformNode(e.value,t)})}transformLimit(e,t){return o({kind:"LimitNode",limit:this.transformNode(e.limit,t)})}transformOffset(e,t){return o({kind:"OffsetNode",offset:this.transformNode(e.offset,t)})}transformOnConflict(e,t){return o({kind:"OnConflictNode",columns:this.transformNodeList(e.columns,t),constraint:this.transformNode(e.constraint,t),indexExpression:this.transformNode(e.indexExpression,t),indexWhere:this.transformNode(e.indexWhere,t),updates:this.transformNodeList(e.updates,t),updateWhere:this.transformNode(e.updateWhere,t),doNothing:e.doNothing})}transformOnDuplicateKey(e,t){return o({kind:"OnDuplicateKeyNode",updates:this.transformNodeList(e.updates,t)})}transformCreateIndex(e,t){return o({kind:"CreateIndexNode",name:this.transformNode(e.name,t),table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t),unique:e.unique,using:this.transformNode(e.using,t),ifNotExists:e.ifNotExists,where:this.transformNode(e.where,t),nullsNotDistinct:e.nullsNotDistinct})}transformList(e,t){return o({kind:"ListNode",items:this.transformNodeList(e.items,t)})}transformDropIndex(e,t){return o({kind:"DropIndexNode",name:this.transformNode(e.name,t),table:this.transformNode(e.table,t),ifExists:e.ifExists,cascade:e.cascade})}transformPrimaryKeyConstraint(e,t){return o({kind:"PrimaryKeyConstraintNode",columns:this.transformNodeList(e.columns,t),name:this.transformNode(e.name,t),deferrable:e.deferrable,initiallyDeferred:e.initiallyDeferred})}transformUniqueConstraint(e,t){return o({kind:"UniqueConstraintNode",columns:this.transformNodeList(e.columns,t),name:this.transformNode(e.name,t),nullsNotDistinct:e.nullsNotDistinct,deferrable:e.deferrable,initiallyDeferred:e.initiallyDeferred})}transformForeignKeyConstraint(e,t){return o({kind:"ForeignKeyConstraintNode",columns:this.transformNodeList(e.columns,t),references:this.transformNode(e.references,t),name:this.transformNode(e.name,t),onDelete:e.onDelete,onUpdate:e.onUpdate,deferrable:e.deferrable,initiallyDeferred:e.initiallyDeferred})}transformSetOperation(e,t){return o({kind:"SetOperationNode",operator:e.operator,expression:this.transformNode(e.expression,t),all:e.all})}transformReferences(e,t){return o({kind:"ReferencesNode",table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t),onDelete:e.onDelete,onUpdate:e.onUpdate})}transformCheckConstraint(e,t){return o({kind:"CheckConstraintNode",expression:this.transformNode(e.expression,t),name:this.transformNode(e.name,t)})}transformWith(e,t){return o({kind:"WithNode",expressions:this.transformNodeList(e.expressions,t),recursive:e.recursive})}transformCommonTableExpression(e,t){return o({kind:"CommonTableExpressionNode",name:this.transformNode(e.name,t),materialized:e.materialized,expression:this.transformNode(e.expression,t)})}transformCommonTableExpressionName(e,t){return o({kind:"CommonTableExpressionNameNode",table:this.transformNode(e.table,t),columns:this.transformNodeList(e.columns,t)})}transformHaving(e,t){return o({kind:"HavingNode",having:this.transformNode(e.having,t)})}transformCreateSchema(e,t){return o({kind:"CreateSchemaNode",schema:this.transformNode(e.schema,t),ifNotExists:e.ifNotExists})}transformDropSchema(e,t){return o({kind:"DropSchemaNode",schema:this.transformNode(e.schema,t),ifExists:e.ifExists,cascade:e.cascade})}transformAlterTable(e,t){return o({kind:"AlterTableNode",table:this.transformNode(e.table,t),renameTo:this.transformNode(e.renameTo,t),setSchema:this.transformNode(e.setSchema,t),columnAlterations:this.transformNodeList(e.columnAlterations,t),addConstraint:this.transformNode(e.addConstraint,t),dropConstraint:this.transformNode(e.dropConstraint,t),renameConstraint:this.transformNode(e.renameConstraint,t),addIndex:this.transformNode(e.addIndex,t),dropIndex:this.transformNode(e.dropIndex,t)})}transformDropColumn(e,t){return o({kind:"DropColumnNode",column:this.transformNode(e.column,t)})}transformRenameColumn(e,t){return o({kind:"RenameColumnNode",column:this.transformNode(e.column,t),renameTo:this.transformNode(e.renameTo,t)})}transformAlterColumn(e,t){return o({kind:"AlterColumnNode",column:this.transformNode(e.column,t),dataType:this.transformNode(e.dataType,t),dataTypeExpression:this.transformNode(e.dataTypeExpression,t),setDefault:this.transformNode(e.setDefault,t),dropDefault:e.dropDefault,setNotNull:e.setNotNull,dropNotNull:e.dropNotNull})}transformModifyColumn(e,t){return o({kind:"ModifyColumnNode",column:this.transformNode(e.column,t)})}transformAddConstraint(e,t){return o({kind:"AddConstraintNode",constraint:this.transformNode(e.constraint,t)})}transformDropConstraint(e,t){return o({kind:"DropConstraintNode",constraintName:this.transformNode(e.constraintName,t),ifExists:e.ifExists,modifier:e.modifier})}transformRenameConstraint(e,t){return o({kind:"RenameConstraintNode",oldName:this.transformNode(e.oldName,t),newName:this.transformNode(e.newName,t)})}transformCreateView(e,t){return o({kind:"CreateViewNode",name:this.transformNode(e.name,t),temporary:e.temporary,orReplace:e.orReplace,ifNotExists:e.ifNotExists,materialized:e.materialized,columns:this.transformNodeList(e.columns,t),as:this.transformNode(e.as,t)})}transformRefreshMaterializedView(e,t){return o({kind:"RefreshMaterializedViewNode",name:this.transformNode(e.name,t),concurrently:e.concurrently,withNoData:e.withNoData})}transformDropView(e,t){return o({kind:"DropViewNode",name:this.transformNode(e.name,t),ifExists:e.ifExists,materialized:e.materialized,cascade:e.cascade})}transformGenerated(e,t){return o({kind:"GeneratedNode",byDefault:e.byDefault,always:e.always,identity:e.identity,stored:e.stored,expression:this.transformNode(e.expression,t)})}transformDefaultValue(e,t){return o({kind:"DefaultValueNode",defaultValue:this.transformNode(e.defaultValue,t)})}transformOn(e,t){return o({kind:"OnNode",on:this.transformNode(e.on,t)})}transformSelectModifier(e,t){return o({kind:"SelectModifierNode",modifier:e.modifier,rawModifier:this.transformNode(e.rawModifier,t),of:this.transformNodeList(e.of,t)})}transformCreateType(e,t){return o({kind:"CreateTypeNode",name:this.transformNode(e.name,t),enum:this.transformNode(e.enum,t)})}transformDropType(e,t){return o({kind:"DropTypeNode",name:this.transformNode(e.name,t),ifExists:e.ifExists})}transformExplain(e,t){return o({kind:"ExplainNode",format:e.format,options:this.transformNode(e.options,t)})}transformSchemableIdentifier(e,t){return o({kind:"SchemableIdentifierNode",schema:this.transformNode(e.schema,t),identifier:this.transformNode(e.identifier,t)})}transformAggregateFunction(e,t){return o({kind:"AggregateFunctionNode",func:e.func,aggregated:this.transformNodeList(e.aggregated,t),distinct:e.distinct,orderBy:this.transformNode(e.orderBy,t),withinGroup:this.transformNode(e.withinGroup,t),filter:this.transformNode(e.filter,t),over:this.transformNode(e.over,t)})}transformOver(e,t){return o({kind:"OverNode",orderBy:this.transformNode(e.orderBy,t),partitionBy:this.transformNode(e.partitionBy,t)})}transformPartitionBy(e,t){return o({kind:"PartitionByNode",items:this.transformNodeList(e.items,t)})}transformPartitionByItem(e,t){return o({kind:"PartitionByItemNode",partitionBy:this.transformNode(e.partitionBy,t)})}transformBinaryOperation(e,t){return o({kind:"BinaryOperationNode",leftOperand:this.transformNode(e.leftOperand,t),operator:this.transformNode(e.operator,t),rightOperand:this.transformNode(e.rightOperand,t)})}transformUnaryOperation(e,t){return o({kind:"UnaryOperationNode",operator:this.transformNode(e.operator,t),operand:this.transformNode(e.operand,t)})}transformUsing(e,t){return o({kind:"UsingNode",tables:this.transformNodeList(e.tables,t)})}transformFunction(e,t){return o({kind:"FunctionNode",func:e.func,arguments:this.transformNodeList(e.arguments,t)})}transformCase(e,t){return o({kind:"CaseNode",value:this.transformNode(e.value,t),when:this.transformNodeList(e.when,t),else:this.transformNode(e.else,t),isStatement:e.isStatement})}transformWhen(e,t){return o({kind:"WhenNode",condition:this.transformNode(e.condition,t),result:this.transformNode(e.result,t)})}transformJSONReference(e,t){return o({kind:"JSONReferenceNode",reference:this.transformNode(e.reference,t),traversal:this.transformNode(e.traversal,t)})}transformJSONPath(e,t){return o({kind:"JSONPathNode",inOperator:this.transformNode(e.inOperator,t),pathLegs:this.transformNodeList(e.pathLegs,t)})}transformJSONPathLeg(e,t){return o({kind:"JSONPathLegNode",type:e.type,value:e.value})}transformJSONOperatorChain(e,t){return o({kind:"JSONOperatorChainNode",operator:this.transformNode(e.operator,t),values:this.transformNodeList(e.values,t)})}transformTuple(e,t){return o({kind:"TupleNode",values:this.transformNodeList(e.values,t)})}transformMergeQuery(e,t){return o({kind:"MergeQueryNode",into:this.transformNode(e.into,t),using:this.transformNode(e.using,t),whens:this.transformNodeList(e.whens,t),with:this.transformNode(e.with,t),top:this.transformNode(e.top,t),endModifiers:this.transformNodeList(e.endModifiers,t),output:this.transformNode(e.output,t),returning:this.transformNode(e.returning,t)})}transformMatched(e,t){return o({kind:"MatchedNode",not:e.not,bySource:e.bySource})}transformAddIndex(e,t){return o({kind:"AddIndexNode",name:this.transformNode(e.name,t),columns:this.transformNodeList(e.columns,t),unique:e.unique,using:this.transformNode(e.using,t),ifNotExists:e.ifNotExists})}transformCast(e,t){return o({kind:"CastNode",expression:this.transformNode(e.expression,t),dataType:this.transformNode(e.dataType,t)})}transformFetch(e,t){return o({kind:"FetchNode",rowCount:this.transformNode(e.rowCount,t),modifier:e.modifier})}transformTop(e,t){return o({kind:"TopNode",expression:e.expression,modifiers:e.modifiers})}transformOutput(e,t){return o({kind:"OutputNode",selections:this.transformNodeList(e.selections,t)})}transformDataType(e,t){return e}transformSelectAll(e,t){return e}transformIdentifier(e,t){return e}transformValue(e,t){return e}transformPrimitiveValueList(e,t){return e}transformOperator(e,t){return e}transformDefaultInsertValue(e,t){return e}transformOrAction(e,t){return e}transformCollate(e,t){return e}}var Kr=C({AlterTableNode:!0,CreateIndexNode:!0,CreateSchemaNode:!0,CreateTableNode:!0,CreateTypeNode:!0,CreateViewNode:!0,RefreshMaterializedViewNode:!0,DeleteQueryNode:!0,DropIndexNode:!0,DropSchemaNode:!0,DropTableNode:!0,DropTypeNode:!0,DropViewNode:!0,InsertQueryNode:!0,RawNode:!0,SelectQueryNode:!0,UpdateQueryNode:!0,MergeQueryNode:!0}),jr={json_agg:!0,to_json:!0};class ct extends Ju{#e;#t=new Set;#r=new Set;constructor(e){super();this.#e=e}transformNodeImpl(e,t){if(!this.#s(e))return super.transformNodeImpl(e,t);let r=this.#p(e);for(let a of r)this.#r.add(a);let i=this.#a(e);for(let a of i)this.#t.add(a);let n=super.transformNodeImpl(e,t);for(let a of i)this.#t.delete(a);for(let a of r)this.#r.delete(a);return n}transformSchemableIdentifier(e,t){let r=super.transformSchemableIdentifier(e,t);if(r.schema||!this.#t.has(e.identifier.name))return r;return{...r,schema:it.create(this.#e)}}transformReferences(e,t){let r=super.transformReferences(e,t);if(r.table.table.schema)return r;return{...r,table:yo.createWithSchema(this.#e,r.table.table.identifier.name)}}transformAggregateFunction(e,t){return{...super.transformAggregateFunction({...e,aggregated:[]},t),aggregated:this.#o(e,t,"aggregated")}}transformFunction(e,t){return{...super.transformFunction({...e,arguments:[]},t),arguments:this.#o(e,t,"arguments")}}transformSelectModifier(e,t){return{...super.transformSelectModifier({...e,of:void 0},t),of:e.of?.map((r)=>yo.is(r)&&!r.table.schema?{...r,table:this.transformIdentifier(r.table.identifier,t)}:this.transformNode(r,t))}}#o(e,t,r){return jr[e.func]?e[r].map((i)=>!yo.is(i)||i.table.schema?this.transformNode(i,t):{...i,table:this.transformIdentifier(i.table.identifier,t)}):this.transformNodeList(e[r],t)}#s(e){return e.kind in Kr}#a(e){let t=new Set;if("name"in e&&e.name&&_o.is(e.name))this.#n(e.name,t);if("from"in e&&e.from)for(let r of e.from.froms)this.#i(r,t);if("into"in e&&e.into)this.#i(e.into,t);if("table"in e&&e.table)this.#i(e.table,t);if("joins"in e&&e.joins)for(let r of e.joins)this.#i(r.table,t);if("using"in e&&e.using)if(L.is(e.using))this.#i(e.using.table,t);else this.#i(e.using,t);return t}#p(e){let t=new Set;if("with"in e&&e.with)this.#d(e.with,t);return t}#i(e,t){if(yo.is(e))return this.#n(e.table,t);if(l.is(e)&&yo.is(e.node))return this.#n(e.node.table,t);if(Fe.is(e)){for(let r of e.items)this.#i(r,t);return}if(be.is(e)){for(let r of e.tables)this.#i(r,t);return}}#n(e,t){let r=e.identifier.name;if(!this.#t.has(r)&&!this.#r.has(r))t.add(r)}#d(e,t){for(let r of e.expressions){let i=r.name.table.table.identifier.name;if(!this.#r.has(i))t.add(i)}}}class Ks{#e;constructor(e){this.#e=new ct(e)}transformQuery(e){return this.#e.transformNode(e.node,e.queryId)}async transformResult(e){return e.result}}var Zt=C({is(e){return e.kind==="MatchedNode"},create(e,t=!1){return C({kind:"MatchedNode",not:e,bySource:t})}});function lt(e,t,r){return R.create(de([Zt.create(!e.isMatched,e.bySource),...t&&t.length>0?[t.length===3&&r?N(t[0],t[1],t[2]):br(t)]:[]],"and",!1))}function Ne(e){if(fr(e))return nr.create([e],[]);if(Or(e))return e.toOperationNode();return e}class Ya{#e;#t;#r;constructor(){this.#e=new Promise((e,t)=>{this.#r=t,this.#t=e})}get promise(){return this.#e}resolve=(e)=>{if(this.#t)this.#t(e)};reject=(e)=>{if(this.#r)this.#r(e)}}async function Sf(e){let t=new Ya,r=new Ya;return e.provideConnection(async(i)=>(t.resolve(i),await r.promise)).catch((i)=>t.reject(i)),C({connection:await t.promise,release:r.resolve})}var Hr=C([]);class Yu{#e;constructor(e=Hr){this.#e=e}get plugins(){return this.#e}transformQuery(e,t){for(let r of this.#e){let i=r.transformQuery({node:e,queryId:t});if(i.kind===e.kind)e=i;else throw Error(["KyselyPlugin.transformQuery must return a node","of the same kind that was given to it.",`The plugin was given a ${e.kind}`,`but it returned a ${i.kind}`].join(" "))}return e}async executeQuery(e){return await this.provideConnection(async(t)=>{let r=await t.executeQuery(e);if("numUpdatedOrDeletedRows"in r)vi("kysely:warning: outdated driver/plugin detected! `QueryResult.numUpdatedOrDeletedRows` has been replaced with `QueryResult.numAffectedRows`.");return await this.#t(r,e.queryId)})}async*stream(e,t){let{connection:r,release:i}=await Sf(this);try{for await(let n of r.streamQuery(e,t))yield await this.#t(n,e.queryId)}finally{i()}}async#t(e,t){for(let r of this.#e)e=await r.transformResult({result:e,queryId:t});return e}}class ye extends Yu{get adapter(){throw Error("this query cannot be compiled to SQL")}compileQuery(){throw Error("this query cannot be compiled to SQL")}provideConnection(){throw Error("this query cannot be executed")}withConnectionProvider(){throw Error("this query cannot have a connection provider")}withPlugin(e){return new ye([...this.plugins,e])}withPlugins(e){return new ye([...this.plugins,...e])}withPluginAtFront(e){return new ye([e,...this.plugins])}withoutPlugins(){return new ye([])}}var ie=new ye;class Nt{numChangedRows;constructor(e){this.numChangedRows=e}}class G{#e;constructor(e){this.#e=C(e)}modifyEnd(e){return new G({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}top(e,t){return new G({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}using(...e){return new b({...this.#e,queryNode:A.cloneWithUsing(this.#e.queryNode,re("Using",e))})}returning(e){return new G({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new G({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new G({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new G({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}}class b{#e;constructor(e){this.#e=C(e)}modifyEnd(e){return new b({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,e.toOperationNode())})}top(e,t){return new b({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}whenMatched(){return this.#t([])}whenMatchedAnd(...e){return this.#t(e)}whenMatchedAndRef(e,t,r){return this.#t([e,t,r],!0)}#t(e,t){return new yt({...this.#e,queryNode:A.cloneWithWhen(this.#e.queryNode,lt({isMatched:!0},e,t))})}whenNotMatched(){return this.#r([])}whenNotMatchedAnd(...e){return this.#r(e)}whenNotMatchedAndRef(e,t,r){return this.#r([e,t,r],!0)}whenNotMatchedBySource(){return this.#r([],!1,!0)}whenNotMatchedBySourceAnd(...e){return this.#r(e,!1,!0)}whenNotMatchedBySourceAndRef(e,t,r){return this.#r([e,t,r],!0,!0)}returning(e){return new b({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,c(e))})}returningAll(e){return new b({...this.#e,queryNode:Oe.cloneWithReturning(this.#e.queryNode,y(e))})}output(e){return new b({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,c(e))})}outputAll(e){return new b({...this.#e,queryNode:Oe.cloneWithOutput(this.#e.queryNode,y(e))})}#r(e,t=!1,r=!1){let i={...this.#e,queryNode:A.cloneWithWhen(this.#e.queryNode,lt({isMatched:!1,bySource:r},e,t))};return new(r?yt:er)(i)}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new b({...this.#e})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile(),t=await this.#e.executor.executeQuery(e),{adapter:r}=this.#e.executor,i=e.query;if(i.returning&&r.supportsReturning||i.output&&r.supportsOutput)return t.rows;return[new Nt(t.numAffectedRows)]}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}}class yt{#e;constructor(e){this.#e=C(e)}thenDelete(){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne("delete"))})}thenDoNothing(){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne("do nothing"))})}thenUpdate(e){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne(e(new ce({queryId:this.#e.queryId,executor:ie,queryNode:F.createWithoutTable()}))))})}thenUpdateSet(...e){return this.thenUpdate((t)=>t.set(...e))}}class er{#e;constructor(e){this.#e=C(e)}thenDoNothing(){return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne("do nothing"))})}thenInsertValues(e){let[t,r]=Ue(e);return new b({...this.#e,queryNode:A.cloneWithThen(this.#e.queryNode,Ne(u.cloneWith(u.createWithoutInto(),{columns:t,values:r})))})}}class bi{#e;constructor(e){this.#e=C(e)}selectFrom(e){return Ee({queryId:kt(),executor:this.#e.executor,queryNode:sr.createFrom(P(e),this.#e.withNode)})}selectNoFrom(e){return Ee({queryId:kt(),executor:this.#e.executor,queryNode:sr.cloneWithSelections(sr.create(this.#e.withNode),c(e))})}insertInto(e){return new h({queryId:kt(),executor:this.#e.executor,queryNode:u.create(ir(e),this.#e.withNode)})}replaceInto(e){return new h({queryId:kt(),executor:this.#e.executor,queryNode:u.create(ir(e),this.#e.withNode,!0)})}deleteFrom(e){return new ze({queryId:kt(),executor:this.#e.executor,queryNode:te.create(P(e),this.#e.withNode)})}updateTable(e){return new ce({queryId:kt(),executor:this.#e.executor,queryNode:F.create(P(e),this.#e.withNode)})}mergeInto(e){return new G({queryId:kt(),executor:this.#e.executor,queryNode:A.create(wt(e),this.#e.withNode)})}with(e,t){let r=ft(e,t);return new bi({...this.#e,withNode:this.#e.withNode?ke.cloneWithExpression(this.#e.withNode,r):ke.create(r)})}withRecursive(e,t){let r=ft(e,t);return new bi({...this.#e,withNode:this.#e.withNode?ke.cloneWithExpression(this.#e.withNode,r):ke.create(r,{recursive:!0})})}withPlugin(e){return new bi({...this.#e,executor:this.#e.executor.withPlugin(e)})}withoutPlugins(){return new bi({...this.#e,executor:this.#e.executor.withoutPlugins()})}withSchema(e){return new bi({...this.#e,executor:this.#e.executor.withPluginAtFront(new Ks(e))})}}function Ht(){return new bi({executor:ie})}function tr(e,t){return new he({joinNode:L.create(e,se(t))})}function rr(){return new ue({overNode:ve.create()})}function re(e,t){if(t.length===3)return Xr(e,t[0],t[1],t[2]);else if(t.length===2)return Yr(e,t[0],t[1]);else if(t.length===1)return Zr(e,t[0]);else throw Error("not implemented")}function Yr(e,t,r){return r(tr(e,t)).toOperationNode()}function Xr(e,t,r,i){return L.createWithOn(e,se(t),N(r,"=",i))}function Zr(e,t){return L.create(e,se(t))}var or=C({is(e){return e.kind==="OffsetNode"},create(e){return C({kind:"OffsetNode",offset:e})}});var ar=C({is(e){return e.kind==="GroupByItemNode"},create(e){return C({kind:"GroupByItemNode",groupBy:e})}});function dr(e){return e=pr(e)?e(B()):e,Q(e).map(ar.create)}var Je=C({is(e){return e.kind==="SetOperationNode"},create(e,t,r){return C({kind:"SetOperationNode",operator:e,expression:t,all:r})}});function ae(e,t,r){if(pr(t))t=t(Qe());if(!x(t))t=[t];return t.map((i)=>Je.create(e,Vo(i),r))}class p{#e;constructor(e){this.#e=e}get expressionType(){return}as(e){return new Ge(this,e)}or(...e){return new _e(T.create(this.#e,br(e)))}and(...e){return new $e(v.create(this.#e,br(e)))}$castTo(){return new p(this.#e)}$notNull(){return new p(this.#e)}toOperationNode(){return this.#e}}class Ge{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}toOperationNode(){return l.create(this.#e.toOperationNode(),Or(this.#t)?this.#t.toOperationNode():it.create(this.#t))}}class _e{#e;constructor(e){this.#e=e}get expressionType(){return}as(e){return new Ge(this,e)}or(...e){return new _e(T.create(this.#e,br(e)))}$castTo(){return new _e(this.#e)}toOperationNode(){return E.create(this.#e)}}class $e{#e;constructor(e){this.#e=e}get expressionType(){return}as(e){return new Ge(this,e)}and(...e){return new $e(v.create(this.#e,br(e)))}$castTo(){return new $e(this.#e)}toOperationNode(){return E.create(this.#e)}}var hr=C({is(e){return e.kind==="FetchNode"},create(e,t){return{kind:"FetchNode",rowCount:bn.create(e),modifier:t}}});function ur(e,t){if(!Vs(e)&&!Ka(e))throw Error(`Invalid fetch row count: ${e}`);if(!ei(t))throw Error(`Invalid fetch modifier: ${t}`);return hr.create(e,t)}function ei(e){return e==="only"||e==="with ties"}var s;class Ot{#e;constructor(e){this.#e=C(e)}get expressionType(){return}get isSelectQueryBuilder(){return!0}where(...e){return new s({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,br(e))})}whereRef(e,t,r){return new s({...this.#e,queryNode:Oe.cloneWithWhere(this.#e.queryNode,N(e,t,r))})}having(...e){return new s({...this.#e,queryNode:sr.cloneWithHaving(this.#e.queryNode,br(e))})}havingRef(e,t,r){return new s({...this.#e,queryNode:sr.cloneWithHaving(this.#e.queryNode,N(e,t,r))})}select(e){return new s({...this.#e,queryNode:sr.cloneWithSelections(this.#e.queryNode,c(e))})}distinctOn(e){return new s({...this.#e,queryNode:sr.cloneWithDistinctOn(this.#e.queryNode,Q(e))})}modifyFront(e){return new s({...this.#e,queryNode:sr.cloneWithFrontModifier(this.#e.queryNode,I.createWithExpression(e.toOperationNode()))})}modifyEnd(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.createWithExpression(e.toOperationNode()))})}distinct(){return new s({...this.#e,queryNode:sr.cloneWithFrontModifier(this.#e.queryNode,I.create("Distinct"))})}forUpdate(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForUpdate",e?xe(e).map(ir):void 0))})}forShare(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForShare",e?xe(e).map(ir):void 0))})}forKeyShare(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForKeyShare",e?xe(e).map(ir):void 0))})}forNoKeyUpdate(e){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("ForNoKeyUpdate",e?xe(e).map(ir):void 0))})}skipLocked(){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("SkipLocked"))})}noWait(){return new s({...this.#e,queryNode:Oe.cloneWithEndModifier(this.#e.queryNode,I.create("NoWait"))})}selectAll(e){return new s({...this.#e,queryNode:sr.cloneWithSelections(this.#e.queryNode,y(e))})}innerJoin(...e){return this.#t("InnerJoin",e)}leftJoin(...e){return this.#t("LeftJoin",e)}rightJoin(...e){return this.#t("RightJoin",e)}fullJoin(...e){return this.#t("FullJoin",e)}crossJoin(...e){return this.#t("CrossJoin",e)}innerJoinLateral(...e){return this.#t("LateralInnerJoin",e)}leftJoinLateral(...e){return this.#t("LateralLeftJoin",e)}crossJoinLateral(...e){return this.#t("LateralCrossJoin",e)}crossApply(...e){return this.#t("CrossApply",e)}outerApply(...e){return this.#t("OuterApply",e)}#t(e,t){return new s({...this.#e,queryNode:Oe.cloneWithJoin(this.#e.queryNode,re(e,t))})}orderBy(...e){return new s({...this.#e,queryNode:Oe.cloneWithOrderByItems(this.#e.queryNode,k(e))})}groupBy(e){return new s({...this.#e,queryNode:sr.cloneWithGroupByItems(this.#e.queryNode,dr(e))})}limit(e){return new s({...this.#e,queryNode:sr.cloneWithLimit(this.#e.queryNode,fe.create(d(e)))})}offset(e){return new s({...this.#e,queryNode:sr.cloneWithOffset(this.#e.queryNode,or.create(d(e)))})}fetch(e,t="only"){return new s({...this.#e,queryNode:sr.cloneWithFetch(this.#e.queryNode,ur(e,t))})}top(e,t){return new s({...this.#e,queryNode:Oe.cloneWithTop(this.#e.queryNode,M(e,t))})}union(e){return new s({...this.#e,queryNode:sr.cloneWithSetOperations(this.#e.queryNode,ae("union",e,!1))})}unionAll(e){return new s({...this.#e,queryNode:sr.cloneWithSetOperations(this.#e.queryNode,ae("union",e,!0))})}intersect(e){return new s({...this.#e,queryNode:sr.cloneWithSetOperations(this.#e.queryNode,ae("intersect",e,!1))})}intersectAll(e){return new s({...this.#e,queryNode:sr.cloneWithSetOperations(this.#e.queryNode,ae("intersect",e,!0))})}except(e){return new s({...this.#e,queryNode:sr.cloneWithSetOperations(this.#e.queryNode,ae("except",e,!1))})}exceptAll(e){return new s({...this.#e,queryNode:sr.cloneWithSetOperations(this.#e.queryNode,ae("except",e,!0))})}as(e){return new mr(this,e)}clearSelect(){return new s({...this.#e,queryNode:sr.cloneWithoutSelections(this.#e.queryNode)})}clearWhere(){return new s({...this.#e,queryNode:Oe.cloneWithoutWhere(this.#e.queryNode)})}clearLimit(){return new s({...this.#e,queryNode:sr.cloneWithoutLimit(this.#e.queryNode)})}clearOffset(){return new s({...this.#e,queryNode:sr.cloneWithoutOffset(this.#e.queryNode)})}clearOrderBy(){return new s({...this.#e,queryNode:Oe.cloneWithoutOrderBy(this.#e.queryNode)})}clearGroupBy(){return new s({...this.#e,queryNode:sr.cloneWithoutGroupBy(this.#e.queryNode)})}$call(e){return e(this)}$if(e,t){if(e)return t(this);return new s({...this.#e})}$castTo(){return new s(this.#e)}$narrowType(){return new s(this.#e)}$assertType(){return new s(this.#e)}$asTuple(){return new p(this.toOperationNode())}$asScalar(){return new p(this.toOperationNode())}withPlugin(e){return new s({...this.#e,executor:this.#e.executor.withPlugin(e)})}toOperationNode(){return this.#e.executor.transformQuery(this.#e.queryNode,this.#e.queryId)}compile(){return this.#e.executor.compileQuery(this.toOperationNode(),this.#e.queryId)}async execute(){let e=this.compile();return(await this.#e.executor.executeQuery(e)).rows}async executeTakeFirst(){let[e]=await this.execute();return e}async executeTakeFirstOrThrow(e=q){let t=await this.executeTakeFirst();if(t===void 0)throw V(e)?new e(this.toOperationNode()):e(this.toOperationNode());return t}async*stream(e=100){let t=this.compile(),r=this.#e.executor.stream(t,e);for await(let i of r)yield*i.rows}async explain(e,t){return await new s({...this.#e,queryNode:Oe.cloneWithExplain(this.#e.queryNode,e,t)}).execute()}}s=Ot;function Ee(e){return new Ot(e)}class mr{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}get isAliasedSelectQueryBuilder(){return!0}toOperationNode(){return l.create(this.#e.toOperationNode(),it.create(this.#t))}}var z=C({is(e){return e.kind==="AggregateFunctionNode"},create(e,t=[]){return C({kind:"AggregateFunctionNode",func:e,aggregated:t})},cloneWithDistinct(e){return C({...e,distinct:!0})},cloneWithOrderBy(e,t,r=!1){let i=r?"withinGroup":"orderBy";return C({...e,[i]:e[i]?_.cloneWithItems(e[i],t):_.create(t)})},cloneWithFilter(e,t){return C({...e,filter:e.filter?O.cloneWithOperation(e.filter,"And",t):O.create(t)})},cloneWithOrFilter(e,t){return C({...e,filter:e.filter?O.cloneWithOperation(e.filter,"Or",t):O.create(t)})},cloneWithOver(e,t){return C({...e,over:t})}});var xt=C({is(e){return e.kind==="FunctionNode"},create(e,t){return C({kind:"FunctionNode",func:e,arguments:t})}});class W{#e;constructor(e){this.#e=C(e)}get expressionType(){return}as(e){return new cr(this,e)}distinct(){return new W({...this.#e,aggregateFunctionNode:z.cloneWithDistinct(this.#e.aggregateFunctionNode)})}orderBy(...e){return new W({...this.#e,aggregateFunctionNode:Oe.cloneWithOrderByItems(this.#e.aggregateFunctionNode,k(e))})}clearOrderBy(){return new W({...this.#e,aggregateFunctionNode:Oe.cloneWithoutOrderBy(this.#e.aggregateFunctionNode)})}withinGroupOrderBy(...e){return new W({...this.#e,aggregateFunctionNode:z.cloneWithOrderBy(this.#e.aggregateFunctionNode,k(e),!0)})}filterWhere(...e){return new W({...this.#e,aggregateFunctionNode:z.cloneWithFilter(this.#e.aggregateFunctionNode,br(e))})}filterWhereRef(e,t,r){return new W({...this.#e,aggregateFunctionNode:z.cloneWithFilter(this.#e.aggregateFunctionNode,N(e,t,r))})}over(e){let t=rr();return new W({...this.#e,aggregateFunctionNode:z.cloneWithOver(this.#e.aggregateFunctionNode,(e?e(t):t).toOperationNode())})}$call(e){return e(this)}$castTo(){return new W(this.#e)}$notNull(){return new W(this.#e)}toOperationNode(){return this.#e.aggregateFunctionNode}}class cr{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}toOperationNode(){return l.create(this.#e.toOperationNode(),it.create(this.#t))}}function If(){let e=(r,i)=>new p(xt.create(r,Q(i??[]))),t=(r,i)=>new W({aggregateFunctionNode:z.create(r,i?Q(i):void 0)});return Object.assign(e,{agg:t,avg(r){return t("avg",[r])},coalesce(...r){return e("coalesce",r)},count(r){return t("count",[r])},countAll(r){return new W({aggregateFunctionNode:z.create("count",y(r))})},max(r){return t("max",[r])},min(r){return t("min",[r])},sum(r){return t("sum",[r])},any(r){return e("any",[r])},jsonAgg(r){return new W({aggregateFunctionNode:z.create("json_agg",[fr(r)?ir(r):r.toOperationNode()])})},toJson(r){return new p(xt.create("to_json",[fr(r)?ir(r):r.toOperationNode()]))}})}var lr=C({is(e){return e.kind==="UnaryOperationNode"},create(e,t){return C({kind:"UnaryOperationNode",operator:e,operand:t})}});function Nr(e,t){return lr.create(S.create(e),w(t))}var ms=C({is(e){return e.kind==="CaseNode"},create(e){return C({kind:"CaseNode",value:e})},cloneWithWhen(e,t){return C({...e,when:C(e.when?[...e.when,t]:[t])})},cloneWithThen(e,t){return C({...e,when:e.when?C([...e.when.slice(0,-1),R.cloneWithResult(e.when[e.when.length-1],t)]):void 0})},cloneWith(e,t){return C({...e,...t})}});class Xu{#e;constructor(e){this.#e=C(e)}when(...e){return new gt({...this.#e,node:ms.cloneWithWhen(this.#e.node,R.create(br(e)))})}}class gt{#e;constructor(e){this.#e=C(e)}then(e){return new yr({...this.#e,node:ms.cloneWithThen(this.#e.node,Pe(e)?ge(e):d(e))})}}class yr{#e;constructor(e){this.#e=C(e)}when(...e){return new gt({...this.#e,node:ms.cloneWithWhen(this.#e.node,R.create(br(e)))})}else(e){return new wr({...this.#e,node:ms.cloneWith(this.#e.node,{else:Pe(e)?ge(e):d(e)})})}end(){return new p(ms.cloneWith(this.#e.node,{isStatement:!1}))}endCase(){return new p(ms.cloneWith(this.#e.node,{isStatement:!0}))}}class wr{#e;constructor(e){this.#e=C(e)}end(){return new p(ms.cloneWith(this.#e.node,{isStatement:!1}))}endCase(){return new p(ms.cloneWith(this.#e.node,{isStatement:!0}))}}var vt=C({is(e){return e.kind==="JSONPathLegNode"},create(e,t){return C({kind:"JSONPathLegNode",type:e,value:t})}});var ti=/^#-\d+$/;class Re{#e;constructor(e){this.#e=e}at(e){if(typeof e!=="number"&&typeof e!=="string"||typeof e==="number"&&!Number.isInteger(e)||typeof e==="string"&&e!=="last"&&!ti.test(e))throw Error(`Unexpected index value in .at(...): ${e}`);return this.#t("ArrayLocation",e)}key(e){return this.#t("Member",e)}#t(e,t){if(pe.is(this.#e))return new Ae(pe.cloneWithTraversal(this.#e,ee.is(this.#e.traversal)?ee.cloneWithLeg(this.#e.traversal,vt.create(e,t)):qe.cloneWithValue(this.#e.traversal,bn.createImmediate(t))));return new Ae(ee.cloneWithLeg(this.#e,vt.create(e,t)))}}class Ae extends Re{#e;constructor(e){super(e);this.#e=e}get expressionType(){return}as(e){return new xr(this,e)}$castTo(){return new Ae(this.#e)}$notNull(){return new Ae(this.#e)}toOperationNode(){return this.#e}}class xr{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}toOperationNode(){return l.create(this.#e.toOperationNode(),Or(this.#t)?this.#t.toOperationNode():it.create(this.#t))}}var bt=C({is(e){return e.kind==="TupleNode"},create(e){return C({kind:"TupleNode",values:C(e)})}});var ri=["varchar","char","text","integer","int2","int4","int8","smallint","bigint","boolean","real","double precision","float4","float8","decimal","numeric","binary","bytea","date","datetime","time","timetz","timestamp","timestamptz","serial","bigserial","uuid","json","jsonb","blob","varbinary","int4range","int4multirange","int8range","int8multirange","numrange","nummultirange","tsrange","tsmultirange","tstzrange","tstzmultirange","daterange","datemultirange"],ii=[/^varchar\(\d+\)$/,/^char\(\d+\)$/,/^decimal\(\d+, \d+\)$/,/^numeric\(\d+, \d+\)$/,/^binary\(\d+\)$/,/^datetime\(\d+\)$/,/^time\(\d+\)$/,/^timetz\(\d+\)$/,/^timestamp\(\d+\)$/,/^timestamptz\(\d+\)$/,/^varbinary\(\d+\)$/],gr=C({is(e){return e.kind==="DataTypeNode"},create(e){return C({kind:"DataTypeNode",dataType:e})}});function vr(e){if(ri.includes(e))return!0;if(ii.some((t)=>t.test(e)))return!0;return!1}function Ws(e){if(Or(e))return e.toOperationNode();if(vr(e))return gr.create(e);throw Error(`invalid column data type ${JSON.stringify(e)}`)}var Wr=C({is(e){return e.kind==="CastNode"},create(e,t){return C({kind:"CastNode",expression:e,dataType:t})}});function Qe(e=ie){function t(n,a,j){return new p(Ie(n,a,j))}function r(n,a){return new p(Nr(n,a))}let i=Object.assign(t,{fn:void 0,eb:void 0,selectFrom(n){return Ee({queryId:kt(),executor:e,queryNode:sr.createFrom(P(n))})},case(n){return new Xu({node:ms.create(Mo(n)?void 0:w(n))})},ref(n,a){if(Mo(a))return new p(ps(n));return new Re(Lt(n,a))},jsonPath(){return new Re(ee.create())},table(n){return new p(ir(n))},val(n){return new p(d(n))},refTuple(...n){return new p(bt.create(n.map(w)))},tuple(...n){return new p(bt.create(n.map(d)))},lit(n){return new p(ge(n))},unary:r,not(n){return r("not",n)},exists(n){return r("exists",n)},neg(n){return r("-",n)},between(n,a,j){return new p(X.create(w(n),S.create("between"),v.create(d(a),d(j))))},betweenSymmetric(n,a,j){return new p(X.create(w(n),S.create("between symmetric"),v.create(d(a),d(j))))},and(n){if(x(n))return new p(de(n,"and"));return new p(tt(n,"and"))},or(n){if(x(n))return new p(de(n,"or"));return new p(tt(n,"or"))},parens(...n){let a=br(n);if(E.is(a))return new p(a);else return new p(E.create(a))},cast(n,a){return new p(Wr.create(w(n),Ws(a)))},withSchema(n){return Qe(e.withPluginAtFront(new Ks(n)))}});return i.fn=If(),i.eb=i,i}function B(e){return Qe()}function Vo(e){if(Or(e))return e.toOperationNode();else if(pr(e))return e(B()).toOperationNode();throw Error(`invalid expression: ${JSON.stringify(e)}`)}function Ve(e){if(Or(e))return e.toOperationNode();else if(pr(e))return e(B()).toOperationNode();throw Error(`invalid aliased expression: ${JSON.stringify(e)}`)}function J(e){return Le(e)||St(e)||pr(e)}class zm{#e;get table(){return this.#e}constructor(e){this.#e=e}as(e){return new Sr(this.#e,e)}}class Sr{#e;#t;get table(){return this.#e}get alias(){return this.#t}constructor(e,t){this.#e=e,this.#t=t}toOperationNode(){return l.create(ir(this.#e),it.create(this.#t))}}function Cr(e){return an(e)&&Or(e)&&fr(e.table)&&fr(e.alias)}function P(e){if(x(e))return e.map((t)=>se(t));else return[se(e)]}function se(e){if(fr(e))return wt(e);else if(Cr(e))return e.toOperationNode();else return Ve(e)}function wt(e){if(e.includes(" as ")){let[r,i]=e.split(" as ").map(kr);return l.create(ir(r),it.create(i))}else return ir(e)}function ir(e){if(e.includes(".")){let[r,i]=e.split(".").map(kr);return yo.createWithSchema(r,i)}else return yo.create(e)}function kr(e){return e.trim()}var Js=C({is(e){return e.kind==="CreateViewNode"},create(e){return C({kind:"CreateViewNode",name:_o.create(e)})},cloneWith(e,t){return C({...e,...t})}});class Te{#e;constructor(e){this.#e=C(e)}get expressionType(){return}get isRawBuilder(){return!0}as(e){return new Er(this,e)}$castTo(){return new Te({...this.#e})}$notNull(){return new Te(this.#e)}withPlugin(e){return new Te({...this.#e,plugins:this.#e.plugins!==void 0?C([...this.#e.plugins,e]):C([e])})}toOperationNode(){return this.#r(this.#t())}compile(e){return this.#o(this.#t(e))}async execute(e){let t=this.#t(e);return t.executeQuery(this.#o(t))}#t(e){let t=e!==void 0?e.getExecutor():ie;return this.#e.plugins!==void 0?t.withPlugins(this.#e.plugins):t}#r(e){return e.transformQuery(this.#e.rawNode,this.#e.queryId)}#o(e){return e.compileQuery(this.#r(e),this.#e.queryId)}}function K(e){return new Te(e)}class Er{#e;#t;constructor(e,t){this.#e=e,this.#t=t}get expression(){return this.#e}get alias(){return this.#t}get rawBuilder(){return this.#e}toOperationNode(){return l.create(this.#e.toOperationNode(),Or(this.#t)?this.#t.toOperationNode():it.create(this.#t))}}var je=Object.assign((e,...t)=>K({queryId:kt(),rawNode:nr.create(e,t?.map(Ar)??[])}),{ref(e){return K({queryId:kt(),rawNode:nr.createWithChild(ps(e))})},val(e){return K({queryId:kt(),rawNode:nr.createWithChild(d(e))})},value(e){return this.val(e)},table(e){return K({queryId:kt(),rawNode:nr.createWithChild(ir(e))})},id(...e){let t=Array(e.length+1).fill(".");return t[0]="",t[t.length-1]="",K({queryId:kt(),rawNode:nr.create(t,e.map(it.create))})},lit(e){return K({queryId:kt(),rawNode:nr.createWithChild(bn.createImmediate(e))})},literal(e){return this.lit(e)},raw(e){return K({queryId:kt(),rawNode:nr.createWithSql(e)})},join(e,t=je`, `){let r=Array(Math.max(2*e.length-1,0)),i=t.toOperationNode();for(let n=0;n<e.length;++n)if(r[2*n]=Ar(e[n]),n!==e.length-1)r[2*n+1]=i;return K({queryId:kt(),rawNode:nr.createWithChildren(r)})}});function Ar(e){if(Or(e))return e.toOperationNode();return d(e)}class Wt{nodeStack=[];get parentNode(){return this.nodeStack[this.nodeStack.length-2]}#e=C({AliasNode:this.visitAlias.bind(this),ColumnNode:this.visitColumn.bind(this),IdentifierNode:this.visitIdentifier.bind(this),SchemableIdentifierNode:this.visitSchemableIdentifier.bind(this),RawNode:this.visitRaw.bind(this),ReferenceNode:this.visitReference.bind(this),SelectQueryNode:this.visitSelectQuery.bind(this),SelectionNode:this.visitSelection.bind(this),TableNode:this.visitTable.bind(this),FromNode:this.visitFrom.bind(this),SelectAllNode:this.visitSelectAll.bind(this),AndNode:this.visitAnd.bind(this),OrNode:this.visitOr.bind(this),ValueNode:this.visitValue.bind(this),ValueListNode:this.visitValueList.bind(this),PrimitiveValueListNode:this.visitPrimitiveValueList.bind(this),ParensNode:this.visitParens.bind(this),JoinNode:this.visitJoin.bind(this),OperatorNode:this.visitOperator.bind(this),WhereNode:this.visitWhere.bind(this),InsertQueryNode:this.visitInsertQuery.bind(this),DeleteQueryNode:this.visitDeleteQuery.bind(this),ReturningNode:this.visitReturning.bind(this),CreateTableNode:this.visitCreateTable.bind(this),AddColumnNode:this.visitAddColumn.bind(this),ColumnDefinitionNode:this.visitColumnDefinition.bind(this),DropTableNode:this.visitDropTable.bind(this),DataTypeNode:this.visitDataType.bind(this),OrderByNode:this.visitOrderBy.bind(this),OrderByItemNode:this.visitOrderByItem.bind(this),GroupByNode:this.visitGroupBy.bind(this),GroupByItemNode:this.visitGroupByItem.bind(this),UpdateQueryNode:this.visitUpdateQuery.bind(this),ColumnUpdateNode:this.visitColumnUpdate.bind(this),LimitNode:this.visitLimit.bind(this),OffsetNode:this.visitOffset.bind(this),OnConflictNode:this.visitOnConflict.bind(this),OnDuplicateKeyNode:this.visitOnDuplicateKey.bind(this),CreateIndexNode:this.visitCreateIndex.bind(this),DropIndexNode:this.visitDropIndex.bind(this),ListNode:this.visitList.bind(this),PrimaryKeyConstraintNode:this.visitPrimaryKeyConstraint.bind(this),UniqueConstraintNode:this.visitUniqueConstraint.bind(this),ReferencesNode:this.visitReferences.bind(this),CheckConstraintNode:this.visitCheckConstraint.bind(this),WithNode:this.visitWith.bind(this),CommonTableExpressionNode:this.visitCommonTableExpression.bind(this),CommonTableExpressionNameNode:this.visitCommonTableExpressionName.bind(this),HavingNode:this.visitHaving.bind(this),CreateSchemaNode:this.visitCreateSchema.bind(this),DropSchemaNode:this.visitDropSchema.bind(this),AlterTableNode:this.visitAlterTable.bind(this),DropColumnNode:this.visitDropColumn.bind(this),RenameColumnNode:this.visitRenameColumn.bind(this),AlterColumnNode:this.visitAlterColumn.bind(this),ModifyColumnNode:this.visitModifyColumn.bind(this),AddConstraintNode:this.visitAddConstraint.bind(this),DropConstraintNode:this.visitDropConstraint.bind(this),RenameConstraintNode:this.visitRenameConstraint.bind(this),ForeignKeyConstraintNode:this.visitForeignKeyConstraint.bind(this),CreateViewNode:this.visitCreateView.bind(this),RefreshMaterializedViewNode:this.visitRefreshMaterializedView.bind(this),DropViewNode:this.visitDropView.bind(this),GeneratedNode:this.visitGenerated.bind(this),DefaultValueNode:this.visitDefaultValue.bind(this),OnNode:this.visitOn.bind(this),ValuesNode:this.visitValues.bind(this),SelectModifierNode:this.visitSelectModifier.bind(this),CreateTypeNode:this.visitCreateType.bind(this),DropTypeNode:this.visitDropType.bind(this),ExplainNode:this.visitExplain.bind(this),DefaultInsertValueNode:this.visitDefaultInsertValue.bind(this),AggregateFunctionNode:this.visitAggregateFunction.bind(this),OverNode:this.visitOver.bind(this),PartitionByNode:this.visitPartitionBy.bind(this),PartitionByItemNode:this.visitPartitionByItem.bind(this),SetOperationNode:this.visitSetOperation.bind(this),BinaryOperationNode:this.visitBinaryOperation.bind(this),UnaryOperationNode:this.visitUnaryOperation.bind(this),UsingNode:this.visitUsing.bind(this),FunctionNode:this.visitFunction.bind(this),CaseNode:this.visitCase.bind(this),WhenNode:this.visitWhen.bind(this),JSONReferenceNode:this.visitJSONReference.bind(this),JSONPathNode:this.visitJSONPath.bind(this),JSONPathLegNode:this.visitJSONPathLeg.bind(this),JSONOperatorChainNode:this.visitJSONOperatorChain.bind(this),TupleNode:this.visitTuple.bind(this),MergeQueryNode:this.visitMergeQuery.bind(this),MatchedNode:this.visitMatched.bind(this),AddIndexNode:this.visitAddIndex.bind(this),CastNode:this.visitCast.bind(this),FetchNode:this.visitFetch.bind(this),TopNode:this.visitTop.bind(this),OutputNode:this.visitOutput.bind(this),OrActionNode:this.visitOrAction.bind(this),CollateNode:this.visitCollate.bind(this)});visitNode=(e)=>{this.nodeStack.push(e),this.#e[e.kind](e),this.nodeStack.pop()}}var oi=/'/g,ni=/['"]/g;class vo extends Wt{#e="";#t=[];get numParameters(){return this.#t.length}compileQuery(e,t){return this.#e="",this.#t=[],this.nodeStack.splice(0,this.nodeStack.length),this.visitNode(e),C({query:e,queryId:t,sql:this.getSql(),parameters:[...this.#t]})}getSql(){return this.#e}visitSelectQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!u.is(this.parentNode)&&!Ao.is(this.parentNode)&&!Js.is(this.parentNode)&&!Je.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("select"),e.distinctOn)this.append(" "),this.compileDistinctOn(e.distinctOn);if(e.frontModifiers?.length)this.append(" "),this.compileList(e.frontModifiers," ");if(e.top)this.append(" "),this.visitNode(e.top);if(e.selections)this.append(" "),this.compileList(e.selections);if(e.from)this.append(" "),this.visitNode(e.from);if(e.joins)this.append(" "),this.compileList(e.joins," ");if(e.where)this.append(" "),this.visitNode(e.where);if(e.groupBy)this.append(" "),this.visitNode(e.groupBy);if(e.having)this.append(" "),this.visitNode(e.having);if(e.setOperations)this.append(" "),this.compileList(e.setOperations," ");if(e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(e.limit)this.append(" "),this.visitNode(e.limit);if(e.offset)this.append(" "),this.visitNode(e.offset);if(e.fetch)this.append(" "),this.visitNode(e.fetch);if(e.endModifiers?.length)this.append(" "),this.compileList(this.sortSelectModifiers([...e.endModifiers])," ");if(t)this.append(")")}visitFrom(e){this.append("from "),this.compileList(e.froms)}visitSelection(e){this.visitNode(e.selection)}visitColumn(e){this.visitNode(e.column)}compileDistinctOn(e){this.append("distinct on ("),this.compileList(e),this.append(")")}compileList(e,t=", "){let r=e.length-1;for(let i=0;i<=r;i++)if(this.visitNode(e[i]),i<r)this.append(t)}visitWhere(e){this.append("where "),this.visitNode(e.where)}visitHaving(e){this.append("having "),this.visitNode(e.having)}visitInsertQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!nr.is(this.parentNode)&&!R.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append(e.replace?"replace":"insert"),e.ignore)vi("`InsertQueryNode.ignore` is deprecated. Use `InsertQueryNode.orAction` instead."),this.append(" ignore");if(e.orAction)this.append(" "),this.visitNode(e.orAction);if(e.top)this.append(" "),this.visitNode(e.top);if(e.into)this.append(" into "),this.visitNode(e.into);if(e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");if(e.output)this.append(" "),this.visitNode(e.output);if(e.values)this.append(" "),this.visitNode(e.values);if(e.defaultValues)this.append(" "),this.append("default values");if(e.onConflict)this.append(" "),this.visitNode(e.onConflict);if(e.onDuplicateKey)this.append(" "),this.visitNode(e.onDuplicateKey);if(e.returning)this.append(" "),this.visitNode(e.returning);if(t)this.append(")");if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitValues(e){this.append("values "),this.compileList(e.values)}visitDeleteQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!nr.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("delete "),e.top)this.visitNode(e.top),this.append(" ");if(this.visitNode(e.from),e.output)this.append(" "),this.visitNode(e.output);if(e.using)this.append(" "),this.visitNode(e.using);if(e.joins)this.append(" "),this.compileList(e.joins," ");if(e.where)this.append(" "),this.visitNode(e.where);if(e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(e.limit)this.append(" "),this.visitNode(e.limit);if(e.returning)this.append(" "),this.visitNode(e.returning);if(t)this.append(")");if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitReturning(e){this.append("returning "),this.compileList(e.selections)}visitAlias(e){this.visitNode(e.node),this.append(" as "),this.visitNode(e.alias)}visitReference(e){if(e.table)this.visitNode(e.table),this.append(".");this.visitNode(e.column)}visitSelectAll(e){this.append("*")}visitIdentifier(e){this.append(this.getLeftIdentifierWrapper()),this.compileUnwrappedIdentifier(e),this.append(this.getRightIdentifierWrapper())}compileUnwrappedIdentifier(e){if(!fr(e.name))throw Error("a non-string identifier was passed to compileUnwrappedIdentifier.");this.append(this.sanitizeIdentifier(e.name))}visitAnd(e){this.visitNode(e.left),this.append(" and "),this.visitNode(e.right)}visitOr(e){this.visitNode(e.left),this.append(" or "),this.visitNode(e.right)}visitValue(e){if(e.immediate)this.appendImmediateValue(e.value);else this.appendValue(e.value)}visitValueList(e){this.append("("),this.compileList(e.values),this.append(")")}visitTuple(e){this.append("("),this.compileList(e.values),this.append(")")}visitPrimitiveValueList(e){this.append("(");let{values:t}=e;for(let r=0;r<t.length;++r)if(this.appendValue(t[r]),r!==t.length-1)this.append(", ");this.append(")")}visitParens(e){this.append("("),this.visitNode(e.node),this.append(")")}visitJoin(e){if(this.append(ai[e.joinType]),this.append(" "),this.visitNode(e.table),e.on)this.append(" "),this.visitNode(e.on)}visitOn(e){this.append("on "),this.visitNode(e.on)}visitRaw(e){let{sqlFragments:t,parameters:r}=e;for(let i=0;i<t.length;++i)if(this.append(t[i]),r.length>i)this.visitNode(r[i])}visitOperator(e){this.append(e.operator)}visitTable(e){this.visitNode(e.table)}visitSchemableIdentifier(e){if(e.schema)this.visitNode(e.schema),this.append(".");this.visitNode(e.identifier)}visitCreateTable(e){if(this.append("create "),e.frontModifiers?.length)this.compileList(e.frontModifiers," "),this.append(" ");if(e.temporary)this.append("temporary ");if(this.append("table "),e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.table),!e.selectQuery)this.append(" ("),this.compileList([...e.columns,...e.constraints??[]]),this.append(")");if(e.onCommit)this.append(" on commit "),this.append(e.onCommit);if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ");if(e.selectQuery)this.append(" as "),this.visitNode(e.selectQuery)}visitColumnDefinition(e){if(e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.column),this.append(" "),this.visitNode(e.dataType),e.unsigned)this.append(" unsigned");if(e.frontModifiers&&e.frontModifiers.length>0)this.append(" "),this.compileList(e.frontModifiers," ");if(e.generated)this.append(" "),this.visitNode(e.generated);if(e.identity)this.append(" identity");if(e.defaultTo)this.append(" "),this.visitNode(e.defaultTo);if(e.notNull)this.append(" not null");if(e.unique)this.append(" unique");if(e.nullsNotDistinct)this.append(" nulls not distinct");if(e.primaryKey)this.append(" primary key");if(e.autoIncrement)this.append(" "),this.append(this.getAutoIncrement());if(e.references)this.append(" "),this.visitNode(e.references);if(e.check)this.append(" "),this.visitNode(e.check);if(e.endModifiers&&e.endModifiers.length>0)this.append(" "),this.compileList(e.endModifiers," ")}getAutoIncrement(){return"auto_increment"}visitReferences(e){if(this.append("references "),this.visitNode(e.table),this.append(" ("),this.compileList(e.columns),this.append(")"),e.onDelete)this.append(" on delete "),this.append(e.onDelete);if(e.onUpdate)this.append(" on update "),this.append(e.onUpdate)}visitDropTable(e){if(this.append("drop table "),e.ifExists)this.append("if exists ");if(this.visitNode(e.table),e.cascade)this.append(" cascade")}visitDataType(e){this.append(e.dataType)}visitOrderBy(e){this.append("order by "),this.compileList(e.items)}visitOrderByItem(e){if(this.visitNode(e.orderBy),e.collation)this.append(" "),this.visitNode(e.collation);if(e.direction)this.append(" "),this.visitNode(e.direction);if(e.nulls)this.append(" nulls "),this.append(e.nulls)}visitGroupBy(e){this.append("group by "),this.compileList(e.items)}visitGroupByItem(e){this.visitNode(e.groupBy)}visitUpdateQuery(e){let t=this.parentNode!==void 0&&!E.is(this.parentNode)&&!nr.is(this.parentNode)&&!R.is(this.parentNode);if(this.parentNode===void 0&&e.explain)this.visitNode(e.explain),this.append(" ");if(t)this.append("(");if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("update "),e.top)this.visitNode(e.top),this.append(" ");if(e.table)this.visitNode(e.table),this.append(" ");if(this.append("set "),e.updates)this.compileList(e.updates);if(e.output)this.append(" "),this.visitNode(e.output);if(e.from)this.append(" "),this.visitNode(e.from);if(e.joins){if(!e.from)throw Error("Joins in an update query are only supported as a part of a PostgreSQL 'update set from join' query. If you want to create a MySQL 'update join set' query, see https://kysely.dev/docs/examples/update/my-sql-joins");this.append(" "),this.compileList(e.joins," ")}if(e.where)this.append(" "),this.visitNode(e.where);if(e.returning)this.append(" "),this.visitNode(e.returning);if(e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(e.limit)this.append(" "),this.visitNode(e.limit);if(t)this.append(")");if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitColumnUpdate(e){this.visitNode(e.column),this.append(" = "),this.visitNode(e.value)}visitLimit(e){this.append("limit "),this.visitNode(e.limit)}visitOffset(e){this.append("offset "),this.visitNode(e.offset)}visitOnConflict(e){if(this.append("on conflict"),e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");else if(e.constraint)this.append(" on constraint "),this.visitNode(e.constraint);else if(e.indexExpression)this.append(" ("),this.visitNode(e.indexExpression),this.append(")");if(e.indexWhere)this.append(" "),this.visitNode(e.indexWhere);if(e.doNothing===!0)this.append(" do nothing");else if(e.updates){if(this.append(" do update set "),this.compileList(e.updates),e.updateWhere)this.append(" "),this.visitNode(e.updateWhere)}}visitOnDuplicateKey(e){this.append("on duplicate key update "),this.compileList(e.updates)}visitCreateIndex(e){if(this.append("create "),e.unique)this.append("unique ");if(this.append("index "),e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.name),e.table)this.append(" on "),this.visitNode(e.table);if(e.using)this.append(" using "),this.visitNode(e.using);if(e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");if(e.nullsNotDistinct)this.append(" nulls not distinct");if(e.where)this.append(" "),this.visitNode(e.where)}visitDropIndex(e){if(this.append("drop index "),e.ifExists)this.append("if exists ");if(this.visitNode(e.name),e.table)this.append(" on "),this.visitNode(e.table);if(e.cascade)this.append(" cascade")}visitCreateSchema(e){if(this.append("create schema "),e.ifNotExists)this.append("if not exists ");this.visitNode(e.schema)}visitDropSchema(e){if(this.append("drop schema "),e.ifExists)this.append("if exists ");if(this.visitNode(e.schema),e.cascade)this.append(" cascade")}visitPrimaryKeyConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");this.append("primary key ("),this.compileList(e.columns),this.append(")"),this.buildDeferrable(e)}buildDeferrable(e){if(e.deferrable!==void 0)if(e.deferrable)this.append(" deferrable");else this.append(" not deferrable");if(e.initiallyDeferred!==void 0)if(e.initiallyDeferred)this.append(" initially deferred");else this.append(" initially immediate")}visitUniqueConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");if(this.append("unique"),e.nullsNotDistinct)this.append(" nulls not distinct");this.append(" ("),this.compileList(e.columns),this.append(")"),this.buildDeferrable(e)}visitCheckConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");this.append("check ("),this.visitNode(e.expression),this.append(")")}visitForeignKeyConstraint(e){if(e.name)this.append("constraint "),this.visitNode(e.name),this.append(" ");if(this.append("foreign key ("),this.compileList(e.columns),this.append(") "),this.visitNode(e.references),e.onDelete)this.append(" on delete "),this.append(e.onDelete);if(e.onUpdate)this.append(" on update "),this.append(e.onUpdate);this.buildDeferrable(e)}visitList(e){this.compileList(e.items)}visitWith(e){if(this.append("with "),e.recursive)this.append("recursive ");this.compileList(e.expressions)}visitCommonTableExpression(e){if(this.visitNode(e.name),this.append(" as "),ta(e.materialized)){if(!e.materialized)this.append("not ");this.append("materialized ")}this.visitNode(e.expression)}visitCommonTableExpressionName(e){if(this.visitNode(e.table),e.columns)this.append("("),this.compileList(e.columns),this.append(")")}visitAlterTable(e){if(this.append("alter table "),this.visitNode(e.table),this.append(" "),e.renameTo)this.append("rename to "),this.visitNode(e.renameTo);if(e.setSchema)this.append("set schema "),this.visitNode(e.setSchema);if(e.addConstraint)this.visitNode(e.addConstraint);if(e.dropConstraint)this.visitNode(e.dropConstraint);if(e.renameConstraint)this.visitNode(e.renameConstraint);if(e.columnAlterations)this.compileColumnAlterations(e.columnAlterations);if(e.addIndex)this.visitNode(e.addIndex);if(e.dropIndex)this.visitNode(e.dropIndex)}visitAddColumn(e){this.append("add column "),this.visitNode(e.column)}visitRenameColumn(e){this.append("rename column "),this.visitNode(e.column),this.append(" to "),this.visitNode(e.renameTo)}visitDropColumn(e){this.append("drop column "),this.visitNode(e.column)}visitAlterColumn(e){if(this.append("alter column "),this.visitNode(e.column),this.append(" "),e.dataType){if(this.announcesNewColumnDataType())this.append("type ");if(this.visitNode(e.dataType),e.dataTypeExpression)this.append("using "),this.visitNode(e.dataTypeExpression)}if(e.setDefault)this.append("set default "),this.visitNode(e.setDefault);if(e.dropDefault)this.append("drop default");if(e.setNotNull)this.append("set not null");if(e.dropNotNull)this.append("drop not null")}visitModifyColumn(e){this.append("modify column "),this.visitNode(e.column)}visitAddConstraint(e){this.append("add "),this.visitNode(e.constraint)}visitDropConstraint(e){if(this.append("drop constraint "),e.ifExists)this.append("if exists ");if(this.visitNode(e.constraintName),e.modifier==="cascade")this.append(" cascade");else if(e.modifier==="restrict")this.append(" restrict")}visitRenameConstraint(e){this.append("rename constraint "),this.visitNode(e.oldName),this.append(" to "),this.visitNode(e.newName)}visitSetOperation(e){if(this.append(e.operator),this.append(" "),e.all)this.append("all ");this.visitNode(e.expression)}visitCreateView(e){if(this.append("create "),e.orReplace)this.append("or replace ");if(e.materialized)this.append("materialized ");if(e.temporary)this.append("temporary ");if(this.append("view "),e.ifNotExists)this.append("if not exists ");if(this.visitNode(e.name),this.append(" "),e.columns)this.append("("),this.compileList(e.columns),this.append(") ");if(e.as)this.append("as "),this.visitNode(e.as)}visitRefreshMaterializedView(e){if(this.append("refresh materialized view "),e.concurrently)this.append("concurrently ");if(this.visitNode(e.name),e.withNoData)this.append(" with no data");else this.append(" with data")}visitDropView(e){if(this.append("drop "),e.materialized)this.append("materialized ");if(this.append("view "),e.ifExists)this.append("if exists ");if(this.visitNode(e.name),e.cascade)this.append(" cascade")}visitGenerated(e){if(this.append("generated "),e.always)this.append("always ");if(e.byDefault)this.append("by default ");if(this.append("as "),e.identity)this.append("identity");if(e.expression)this.append("("),this.visitNode(e.expression),this.append(")");if(e.stored)this.append(" stored")}visitDefaultValue(e){this.append("default "),this.visitNode(e.defaultValue)}visitSelectModifier(e){if(e.rawModifier)this.visitNode(e.rawModifier);else this.append(si[e.modifier]);if(e.of)this.append(" of "),this.compileList(e.of,", ")}visitCreateType(e){if(this.append("create type "),this.visitNode(e.name),e.enum)this.append(" as enum "),this.visitNode(e.enum)}visitDropType(e){if(this.append("drop type "),e.ifExists)this.append("if exists ");this.visitNode(e.name)}visitExplain(e){if(this.append("explain"),e.options||e.format){if(this.append(" "),this.append(this.getLeftExplainOptionsWrapper()),e.options){if(this.visitNode(e.options),e.format)this.append(this.getExplainOptionsDelimiter())}if(e.format)this.append("format"),this.append(this.getExplainOptionAssignment()),this.append(e.format);this.append(this.getRightExplainOptionsWrapper())}}visitDefaultInsertValue(e){this.append("default")}visitAggregateFunction(e){if(this.append(e.func),this.append("("),e.distinct)this.append("distinct ");if(this.compileList(e.aggregated),e.orderBy)this.append(" "),this.visitNode(e.orderBy);if(this.append(")"),e.withinGroup)this.append(" within group ("),this.visitNode(e.withinGroup),this.append(")");if(e.filter)this.append(" filter("),this.visitNode(e.filter),this.append(")");if(e.over)this.append(" "),this.visitNode(e.over)}visitOver(e){if(this.append("over("),e.partitionBy){if(this.visitNode(e.partitionBy),e.orderBy)this.append(" ")}if(e.orderBy)this.visitNode(e.orderBy);this.append(")")}visitPartitionBy(e){this.append("partition by "),this.compileList(e.items)}visitPartitionByItem(e){this.visitNode(e.partitionBy)}visitBinaryOperation(e){this.visitNode(e.leftOperand),this.append(" "),this.visitNode(e.operator),this.append(" "),this.visitNode(e.rightOperand)}visitUnaryOperation(e){if(this.visitNode(e.operator),!this.isMinusOperator(e.operator))this.append(" ");this.visitNode(e.operand)}isMinusOperator(e){return S.is(e)&&e.operator==="-"}visitUsing(e){this.append("using "),this.compileList(e.tables)}visitFunction(e){this.append(e.func),this.append("("),this.compileList(e.arguments),this.append(")")}visitCase(e){if(this.append("case"),e.value)this.append(" "),this.visitNode(e.value);if(e.when)this.append(" "),this.compileList(e.when," ");if(e.else)this.append(" else "),this.visitNode(e.else);if(this.append(" end"),e.isStatement)this.append(" case")}visitWhen(e){if(this.append("when "),this.visitNode(e.condition),e.result)this.append(" then "),this.visitNode(e.result)}visitJSONReference(e){this.visitNode(e.reference),this.visitNode(e.traversal)}visitJSONPath(e){if(e.inOperator)this.visitNode(e.inOperator);this.append("'$");for(let t of e.pathLegs)this.visitNode(t);this.append("'")}visitJSONPathLeg(e){let t=e.type==="ArrayLocation",r=String(e.value);if(t)this.append("["),this.append(this.sanitizeStringLiteral(r)),this.append("]");else this.append('."'),this.append(this.sanitizeJSONPathMemberValue(r)),this.append('"')}visitJSONOperatorChain(e){for(let t=0,r=e.values.length;t<r;t++){if(t===r-1)this.visitNode(e.operator);else this.append("->");this.visitNode(e.values[t])}}visitMergeQuery(e){if(e.with)this.visitNode(e.with),this.append(" ");if(this.append("merge "),e.top)this.visitNode(e.top),this.append(" ");if(this.append("into "),this.visitNode(e.into),e.using)this.append(" "),this.visitNode(e.using);if(e.whens)this.append(" "),this.compileList(e.whens," ");if(e.returning)this.append(" "),this.visitNode(e.returning);if(e.output)this.append(" "),this.visitNode(e.output);if(e.endModifiers?.length)this.append(" "),this.compileList(e.endModifiers," ")}visitMatched(e){if(e.not)this.append("not ");if(this.append("matched"),e.bySource)this.append(" by source")}visitAddIndex(e){if(this.append("add "),e.unique)this.append("unique ");if(this.append("index "),this.visitNode(e.name),e.columns)this.append(" ("),this.compileList(e.columns),this.append(")");if(e.using)this.append(" using "),this.visitNode(e.using)}visitCast(e){this.append("cast("),this.visitNode(e.expression),this.append(" as "),this.visitNode(e.dataType),this.append(")")}visitFetch(e){this.append("fetch next "),this.visitNode(e.rowCount),this.append(` rows ${e.modifier}`)}visitOutput(e){this.append("output "),this.compileList(e.selections)}visitTop(e){if(this.append(`top(${e.expression})`),e.modifiers)this.append(` ${e.modifiers}`)}visitOrAction(e){this.append(e.action)}visitCollate(e){this.append("collate "),this.visitNode(e.collation)}append(e){this.#e+=e}appendValue(e){this.addParameter(e),this.append(this.getCurrentParameterPlaceholder())}getLeftIdentifierWrapper(){return'"'}getRightIdentifierWrapper(){return'"'}getCurrentParameterPlaceholder(){return"$"+this.numParameters}getLeftExplainOptionsWrapper(){return"("}getExplainOptionAssignment(){return" "}getExplainOptionsDelimiter(){return", "}getRightExplainOptionsWrapper(){return")"}sanitizeIdentifier(e){let t=this.getLeftIdentifierWrapper(),r=this.getRightIdentifierWrapper(),i="";for(let n of e)if(i+=n,n===t)i+=t;else if(n===r)i+=r;return i}sanitizeStringLiteral(e){return e.replace(oi,"''")}sanitizeJSONPathMemberValue(e){return e.replace(ni,(t)=>t==="'"?"''":"\\\"")}addParameter(e){this.#t.push(e)}appendImmediateValue(e){if(fr(e))this.appendStringLiteral(e);else if(Vs(e)||ta(e)||Ka(e))this.append(e.toString());else if(Va(e))this.append("null");else if(xf(e))this.appendImmediateValue(e.toISOString());else throw Error(`invalid immediate value ${e}`)}appendStringLiteral(e){this.append("'"),this.append(this.sanitizeStringLiteral(e)),this.append("'")}sortSelectModifiers(e){return e.sort((t,r)=>t.modifier&&r.modifier?Rr[t.modifier]-Rr[r.modifier]:1),C(e)}compileColumnAlterations(e){this.compileList(e)}announcesNewColumnDataType(){return!0}}var si=C({ForKeyShare:"for key share",ForNoKeyUpdate:"for no key update",ForUpdate:"for update",ForShare:"for share",NoWait:"nowait",SkipLocked:"skip locked",Distinct:"distinct"}),Rr=C({ForKeyShare:1,ForNoKeyUpdate:1,ForUpdate:1,ForShare:1,NoWait:2,SkipLocked:2,Distinct:0}),ai=C({InnerJoin:"inner join",LeftJoin:"left join",RightJoin:"right join",FullJoin:"full join",CrossJoin:"cross join",LateralInnerJoin:"inner join lateral",LateralLeftJoin:"left join lateral",LateralCrossJoin:"cross join lateral",OuterApply:"outer apply",CrossApply:"cross apply",Using:"using"});var jm="kysely_migration",qm="kysely_migration_lock";function $m(e){let t=e.filter((i)=>i.pk>0),r=t.length===1?t[0]:void 0;return r?.type.toLowerCase()==="integer"?r.name:void 0}async function Tr(e,t,r){let i=$m(r);if(!i)return;let{rows:n}=await je`
3
3
  PRAGMA index_list(${je.id(t)})
4
- `.execute(e.withoutPlugins());return n.some((a)=>a.origin==="pk")?void 0:i}function af(e,t,r){return{name:e.name,isView:e.type==="view",columns:t.map((i)=>({name:i.name,dataType:i.type,isNullable:i.notnull===0,isAutoIncrementing:i.name===r,hasDefaultValue:i.dflt_value!=null}))}}async function pi(e,t){let{rows:r}=await je`
4
+ `.execute(e.withoutPlugins());return n.some((a)=>a.origin==="pk")?void 0:i}function Tf(e,t,r){return{name:e.name,isView:e.type==="view",columns:t.map((i)=>({name:i.name,dataType:i.type,isNullable:i.notnull===0,isAutoIncrementing:i.name===r,hasDefaultValue:i.dflt_value!=null}))}}async function pi(e,t){let{rows:r}=await je`
5
5
  PRAGMA table_info(${je.id(t)})
6
- `.execute(e.withoutPlugins());if(r.length===0)return;let i=await Tr(e,t,r);return af({name:t},r,i)}async function qg(e,t){if(t.length===0)return[];try{return await e.introspection.getTables()}catch{return(await Promise.all(t.map((r)=>pi(e,r)))).filter((r)=>r!==void 0)}}function Lu(e){let t=e;async function r(){return[]}async function i(n={withInternalKyselyTables:!1}){let a=t.selectFrom("sqlite_schema").where("type","=","table").where("name","not like","sqlite_%").select("name");if(!n.withInternalKyselyTables)a=a.where("name","!=",gm).where("name","!=",Am);let j=await a.execute();return Promise.all(j.map(async({name:H})=>{let Y=await e.selectFrom(je`pragma_table_info(${H})`.as("table_info")).select(["cid","name","type","notnull","dflt_value","pk"]).execute(),we=await Tr(e,H,Y);return af({name:H},Y,we)}))}return{getSchemas:r,getTables:i,getMetadata:async(n)=>({tables:await i(n)})}}
7
- export{So,dr,zs,Vi,Fa,rf,La,zg,fr,sn,C,Io,st,jg,fo,po,mo,Rr,Wt,dc,Za,pm,tr,hi,is,mm,fc,Ba,_n,yr,Oe,nr,nf,It,Du,js,Ua,of,Nu,gi,sf,as,Fu,qs,Ho,hm,or,$s,je,ho,gm,Am,_m,af,qg,Lu};
6
+ `.execute(e.withoutPlugins());if(r.length===0)return;let i=await Tr(e,t,r);return Tf({name:t},r,i)}async function mA(e,t){if(t.length===0)return[];try{return await e.introspection.getTables()}catch{return(await Promise.all(t.map((r)=>pi(e,r)))).filter((r)=>r!==void 0)}}function ed(e){let t=e;async function r(){return[]}async function i(n={withInternalKyselyTables:!1}){let a=t.selectFrom("sqlite_schema").where("type","=","table").where("name","not like","sqlite_%").select("name");if(!n.withInternalKyselyTables)a=a.where("name","!=",jm).where("name","!=",qm);let j=await a.execute();return Promise.all(j.map(async({name:H})=>{let Y=await e.selectFrom(je`pragma_table_info(${H})`.as("table_info")).select(["cid","name","type","notnull","dflt_value","pk"]).execute(),we=await Tr(e,H,Y);return Tf({name:H},Y,we)}))}return{getSchemas:r,getTables:i,getMetadata:async(n)=>({tables:await i(n)})}}
7
+ export{Mo,fr,Vs,ta,Va,xf,Ka,fA,pr,an,C,Oo,it,pA,Ao,_o,yo,Or,Jt,Sc,Wa,Bm,nr,vi,ps,Um,Ic,Ja,bn,br,Oe,sr,Cf,kt,Ju,Ks,Ya,Sf,Yu,bi,If,ms,Xu,Ws,Vo,zm,ir,Js,je,vo,jm,qm,$m,Tf,mA,ed};