@cap.core/ql 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cql/parser.min.js +2 -0
- package/lib/index.js +50 -0
- package/lib/ql/base-class.js +98 -0
- package/lib/ql/cache.js +38 -0
- package/lib/ql/delete.js +7 -0
- package/lib/ql/insert.js +25 -0
- package/lib/ql/select.js +23 -0
- package/lib/ql/update.js +10 -0
- package/lib/xl/fluent.js +32 -0
- package/lib/xl/ttl.js +29 -0
- package/license +201 -0
- package/package.json +21 -0
- package/readme.md +106 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export const $=Object.assign,px=()=>({param:"?"}),ql={SELECT:function(t){Object.defineProperty(Object.assign(this,t),"_",{value:this})},INSERT:function(t){Object.defineProperty(Object.assign(this,t),"_",{value:this})},UPSERT:function(t){Object.defineProperty(Object.assign(this,t),"_",{value:this})},UPDATE:function(t){Object.defineProperty(Object.assign(this,t),"_",{value:this})},DELETE:function(t){Object.defineProperty(Object.assign(this,t),"_",{value:this})},fluent:{subject:px,where:px,expr:px}};class peg$SyntaxError extends SyntaxError{constructor(t,e,n,r){super(t),this.expected=e,this.found=n,this.location=r,this.name="SyntaxError"}format(t){let e="Error: "+this.message;if(this.location){let n=null;const r=t.find(t=>t.source===this.location.source);r&&(n=r.text.split(/\r\n|\n|\r/g));const s=this.location.start,o=this.location.source&&"function"==typeof this.location.source.offset?this.location.source.offset(s):s,u=this.location.source+":"+o.line+":"+o.column;if(n){const t=this.location.end,r="".padEnd(o.line.toString().length," "),l=n[s.line-1],i=(s.line===t.line?t.column:l.length+1)-s.column||1;e+="\n --\x3e "+u+"\n"+r+" |\n"+o.line+" | "+l+"\n"+r+" | "+"".padEnd(s.column-1," ")+"".padEnd(i,"^")}else e+="\n at "+u}return e}static buildMessage(t,e){function n(t){return t.codePointAt(0).toString(16).toUpperCase()}const r=Object.prototype.hasOwnProperty.call(RegExp.prototype,"unicode")?new RegExp("[\\p{C}\\p{Mn}\\p{Mc}]","gu"):null;function s(t){return r?t.replace(r,t=>"\\u{"+n(t)+"}"):t}function o(t){return s(t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,t=>"\\x0"+n(t)).replace(/[\x10-\x1F\x7F-\x9F]/g,t=>"\\x"+n(t)))}function u(t){return s(t.replace(/\\/g,"\\\\").replace(/\]/g,"\\]").replace(/\^/g,"\\^").replace(/-/g,"\\-").replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,t=>"\\x0"+n(t)).replace(/[\x10-\x1F\x7F-\x9F]/g,t=>"\\x"+n(t)))}const l={literal:t=>'"'+o(t.text)+'"',class(t){const e=t.parts.map(t=>Array.isArray(t)?u(t[0])+"-"+u(t[1]):u(t));return"["+(t.inverted?"^":"")+e.join("")+"]"+(t.unicode?"u":"")},any:()=>"any character",end:()=>"end of input",other:t=>t.description};function i(t){return l[t.type](t)}return"Expected "+function(t){const e=t.map(i);if(e.sort(),e.length>0){let t=1;for(let n=1;n<e.length;n++)e[n-1]!==e[n]&&(e[t]=e[n],t++);e.length=t}switch(e.length){case 1:return e[0];case 2:return e[0]+" or "+e[1];default:return e.slice(0,-1).join(", ")+", or "+e[e.length-1]}}(t)+" but "+function(t){return t?'"'+o(t)+'"':"end of input"}(e)+" found."}}function peg$parse(t,e){const n={},r=(e=void 0!==e?e:{}).grammarSource,s={CQL:xn,entity:Rn,columns:vn,ref:Fn,val:In,vals:Dn,expr:Pn,where:_n,groupBy:Sn,orderBy:$n};let o=xn;const u="select",l="localized",i="distinct",c="one",a="from",f="where",h="group by",p="having",d="order by",g="limit",b="offset",C="as",w="*",A="[",x="]",m="asc",E="desc",L="insert",v="upsert",y="into",_="entries",S="columns",O="rows",T="values",R="update",P="entity",j="with",F="set",I="=",D="delete",N=":",U="#",z="?",B="and",q="or",M="in",Z="between",H="like",W="exists",Y="contains",k="case",G="when",Q="then",V="else",J="end",K="is",X="not",tt="null",et="empty",nt="-",rt=".",st="e",ot="'",ut="''",lt="true",it="false",ct="{",at="}",ft="(",ht=")",pt=",",dt=/^[A-Za-z_$]/,gt=/^[A-Za-z0-9_$]/,bt=/^[!=><+\-*\/%&|\^~]/,Ct=/^[0-9]/,wt=/^[^']/,At=/^[A-Za-z_$0-9]/,xt=/^[ \t\n\r]/,mt=pn("SELECT",!0),Et=pn("LOCALIZED",!0),Lt=pn("DISTINCT",!0),vt=pn("ONE",!0),yt=pn("FROM",!0),_t={type:"any"},St=pn("WHERE",!0),$t=pn("GROUP BY",!0),Ot=pn("HAVING",!0),Tt=pn("ORDER BY",!0),Rt=pn("LIMIT",!0),Pt=pn("OFFSET",!0),jt=pn("as",!0),Ft=pn("*",!1),It=pn("[",!1),Dt=pn("]",!1),Nt=pn("asc",!0),Ut=pn("desc",!0),zt=pn("INSERT",!0),Bt=pn("UPSERT",!0),qt=pn("INTO",!0),Mt=pn("ENTRIES",!0),Zt=pn("COLUMNS",!0),Ht=pn("ROWS",!0),Wt=pn("VALUES",!0),Yt=pn("UPDATE",!0),kt=pn("ENTITY",!0),Gt=pn("WITH",!0),Qt=pn("SET",!0),Vt=pn("=",!1),Jt=pn("DELETE",!0),Kt=gn("a name"),Xt=pn("AS",!0),te=pn("from",!0),ee=dn([["A","Z"],["a","z"],"_","$"],!1,!1,!1),ne=dn([["A","Z"],["a","z"],["0","9"],"_","$"],!1,!1,!1),re=gn("an element ref"),se=gn("an entity ref"),oe=pn(":",!1),ue=gn("a reference"),le=gn("a value"),ie=gn("a list"),ce=gn("a function"),ae=gn("a symbol"),fe=pn("#",!1),he=gn("a parameter"),pe=pn("?",!1),de=gn("an operator"),ge=dn(["!","=",">","<","+","-","*","/","%","&","|","^","~"],!1,!1,!1),be=pn("and",!1),Ce=pn("or",!1),we=pn("in",!1),Ae=pn("between",!1),xe=pn("like",!1),me=pn("exists",!1),Ee=pn("contains",!1),Le=pn("case",!1),ve=pn("when",!1),ye=pn("then",!1),_e=pn("else",!1),Se=pn("end",!1),$e=pn("is",!1),Oe=pn("not",!1),Te=pn("null",!1),Re=pn("empty",!1),Pe=gn("a literal"),je=pn("-",!1),Fe=dn([["0","9"]],!1,!1,!1),Ie=pn(".",!1),De=pn("e",!1),Ne=pn("'",!1),Ue=dn(["'"],!0,!1,!1),ze=pn("''",!1),Be=pn("true",!1),qe=pn("false",!1),Me=pn("{",!1),Ze=pn("}",!1),He=pn("(",!1),We=pn(")",!1),Ye=dn([["A","Z"],["a","z"],"_","$",["0","9"]],!1,!1,!1),ke=gn("a comma"),Ge=pn(",",!1),Qe=gn(""),Ve=dn([" ","\t","\n","\r"],!1,!1,!1);function Je(e){return tr===ql.fluent||(or?.length&&(e.params=or),nr?.length&&(e.args=nr),e.source=t.substring(un,on)),e}function Ke(t,e){t.as=e}function Xe(t,e,n){e?t.inline=n:t.expand=n}function tn(t,e){t.as=e}function en(t,e){t.sort=e.toLowerCase()}function nn(t,e,n){t._.with[e]=n}function rn(t,e){return e?{id:t,...e}:t}function sn(){return"'"}let on=0|e.peg$currPos,un=on;const ln=[{line:1,column:1}];let cn,an=on,fn=e.peg$maxFailExpected||[],hn=0|e.peg$silentFails;if(e.startRule){if(!(e.startRule in s))throw new Error("Can't start parsing from rule \""+e.startRule+'".');o=s[e.startRule]}function pn(t,e){return{type:"literal",text:t,ignoreCase:e}}function dn(t,e,n,r){return{type:"class",parts:t,inverted:e,ignoreCase:n,unicode:r}}function gn(t){return{type:"other",description:t}}function bn(e){let n,r=ln[e];if(r)return r;if(e>=ln.length)n=ln.length-1;else for(n=e;!ln[--n];);for(r=ln[n],r={line:r.line,column:r.column};n<e;)10===t.charCodeAt(n)?(r.line++,r.column=1):r.column++,n++;return ln[e]=r,r}function Cn(t,e,n){const s=bn(t),o=bn(e),u={source:r,start:{offset:t,line:s.line,column:s.column},end:{offset:e,line:o.line,column:o.column}};return n&&r&&"function"==typeof r.offset&&(u.start=r.offset(u.start),u.end=r.offset(u.end)),u}function wn(t){on<an||(on>an&&(an=on,fn=[]),fn.push(t))}function An(t,e,n){return new peg$SyntaxError(peg$SyntaxError.buildMessage(t,e),t,e,n)}function xn(){let e,r;return e=on,r=mn(),r===n&&(r=function(){let e,r,s,o,u,l,i,c,a,f,h,p,d,g;e=on,r=on,s=t.substr(on,6),s.toLowerCase()===L?on+=6:(s=n,0===hn&&wn(zt));s===n&&(s=t.substr(on,6),s.toLowerCase()===v?on+=6:(s=n,0===hn&&wn(Bt)));s!==n?(o=Xn(),o!==n?(un=r,r=new ql[s]):(on=r,r=n)):(on=r,r=n);if(r!==n)if(s=on,o=Hn(),o!==n&&(un=s,b=o,o=void(r._.entries=tr.entries(b))),s=o,s===n&&(s=null),o=on,u=on,l=t.substr(on,4),l.toLowerCase()===y?on+=4:(l=n,0===hn&&wn(qt)),l!==n?(i=Xn(),i!==n?(l=[l,i],u=l):(on=u,u=n)):(on=u,u=n),u!==n?(l=Rn(),l!==n?(un=o,o=function(t,e){t._.into=e}(r,l)):(on=o,o=n)):(on=o,o=n),o!==n){if(u=on,l=mn(),l!==n&&(un=u,l=function(t,e){t._.from=e}(r,l)),u=l,u===n){if(u=on,l=on,i=on,c=t.substr(on,7),c.toLowerCase()===_?on+=7:(c=n,0===hn&&wn(Mt)),c!==n?(a=Xn(),a!==n?(c=[c,a],i=c):(on=i,i=n)):(on=i,i=n),i===n&&(i=null),c=on,a=Hn(),a!==n&&(un=c,a=function(t,e){t._entries(e)}(r,a)),c=a,c!==n?(i=[i,c],l=i):(on=l,l=n),l===n&&(l=null),i=on,c=on,a=t.substr(on,7),a.toLowerCase()===S?on+=7:(a=n,0===hn&&wn(Zt)),a!==n?(f=Xn(),f!==n?(a=[a,f],c=a):(on=c,c=n)):(on=c,c=n),c===n&&(c=null),a=Hn(),a===n)if(a=on,f=kn(),f!==n){for(h=on,p=[],d=Tn();d!==n;)p.push(d),d=on,g=Vn(),g!==n?(g=Tn(),g===n?(on=d,d=n):d=g):d=g;p.length<1?(on=h,h=n):h=p,h!==n?(p=Gn(),p!==n?a=h:(on=a,a=n)):(on=a,a=n)}else on=a,a=n;a!==n?(un=i,i=function(t,e){t._.columns=e}(r,a)):(on=i,i=n),i===n&&(i=null),c=on,a=on,f=t.substr(on,4),f.toLowerCase()===O?on+=4:(f=n,0===hn&&wn(Ht)),f!==n?(h=Xn(),h!==n?(f=[f,h],a=f):(on=a,a=n)):(on=a,a=n),a===n&&(a=null),f=Hn(),f!==n?(un=c,c=function(t,e){t._.rows=e}(r,f)):(on=c,c=n),c===n&&(c=null),a=on,f=on,h=t.substr(on,6),h.toLowerCase()===T?on+=6:(h=n,0===hn&&wn(Wt)),h!==n?(p=Xn(),p!==n?(h=[h,p],f=h):(on=f,f=n)):(on=f,f=n),f!==n?(h=Nn(),h!==n?(un=a,a=function(t,e){t._.values=e.list}(r,h)):(on=a,a=n)):(on=a,a=n),a===n&&(a=null),l=[l,i,c,a],u=l}u!==n?e=r:(on=e,e=n)}else on=e,e=n;else on=e,e=n;var b;return e}(),r===n&&(r=function(){let e,r,s,o,u,l,i,c,a,h,p,d,g,b,C,w,A;e=on,r=on,s=t.substr(on,6),s.toLowerCase()===R?on+=6:(s=n,0===hn&&wn(Yt));s!==n?(o=Xn(),o!==n?(un=r,r=new ql.UPDATE):(on=r,r=n)):(on=r,r=n);if(r!==n)if(s=on,o=on,u=t.substr(on,6),u.toLowerCase()===P?on+=6:(u=n,0===hn&&wn(kt)),u!==n?(l=Xn(),l!==n?(u=[u,l],o=u):(on=o,o=n)):(on=o,o=n),o===n&&(o=null),u=Rn(),u!==n?(un=s,x=u,s=void(r._.entity=x)):(on=s,s=n),s!==n){if(o=on,u=t.substr(on,4),u.toLowerCase()===j?on+=4:(u=n,0===hn&&wn(Gt)),u===n&&(u=t.substr(on,3),u.toLowerCase()===F?on+=3:(u=n,0===hn&&wn(Qt))),u!==n)if(l=Xn(),l!==n){if(i=on,c=Hn(),c!==n&&(un=i,c=function(t,e){t._.with=e}(r,c)),i=c,i===n)if(i=on,un=on,c=function(t){t._.with={}}(r),c=c?n:void 0,c!==n){for(a=on,h=[],p=on,d=Tn(),d!==n?(g=Kn(),61===t.charCodeAt(on)?(b=I,on++):(b=n,0===hn&&wn(Vt)),b!==n?(C=Kn(),w=Pn(),w!==n?(un=p,p=nn(r,d,w)):(on=p,p=n)):(on=p,p=n)):(on=p,p=n);p!==n;)h.push(p),p=on,d=Vn(),d!==n?(d=on,g=Tn(),g!==n?(b=Kn(),61===t.charCodeAt(on)?(C=I,on++):(C=n,0===hn&&wn(Vt)),C!==n?(w=Kn(),A=Pn(),A!==n?(un=d,d=nn(r,g,A)):(on=d,d=n)):(on=d,d=n)):(on=d,d=n),d===n?(on=p,p=n):p=d):p=d;h.length<1?(on=a,a=n):a=h,a!==n?(h=Kn(),c=[c,a,h],i=c):(on=i,i=n)}else on=i,i=n;i!==n?(u=[u,l,i],o=u):(on=o,o=n)}else on=o,o=n;else on=o,o=n;o===n&&(o=null),u=on,l=on,i=t.substr(on,5),i.toLowerCase()===f?on+=5:(i=n,0===hn&&wn(St)),i!==n?(c=Xn(),c!==n?(i=[i,c],l=i):(on=l,l=n)):(on=l,l=n),l!==n?(i=_n(),i!==n?(c=Kn(),un=u,u=function(t,e){t._.where=e}(r,i)):(on=u,u=n)):(on=u,u=n),u===n&&(u=null),e=r}else on=e,e=n;else on=e,e=n;var x;return e}(),r===n&&(r=function(){let e,r,s,o,u,l,i;e=on,r=on,s=t.substr(on,6),s.toLowerCase()===D?on+=6:(s=n,0===hn&&wn(Jt));s!==n?(o=Xn(),o!==n?(un=r,r=new ql.DELETE):(on=r,r=n)):(on=r,r=n);r!==n?(s=on,o=on,u=t.substr(on,4),u.toLowerCase()===a?on+=4:(u=n,0===hn&&wn(yt)),u!==n?(l=Xn(),l!==n?(u=[u,l],o=u):(on=o,o=n)):(on=o,o=n),o===n&&(o=null),u=Rn(),u!==n?(l=Kn(),un=s,c=u,s=void(r._.from=c)):(on=s,s=n),s!==n?(o=on,u=on,l=t.substr(on,5),l.toLowerCase()===f?on+=5:(l=n,0===hn&&wn(St)),l!==n?(i=Xn(),i!==n?(l=[l,i],u=l):(on=u,u=n)):(on=u,u=n),u!==n?(l=_n(),l!==n?(i=Kn(),un=o,o=function(t,e){t._.where=e}(r,l)):(on=o,o=n)):(on=o,o=n),o===n&&(o=null),e=r):(on=e,e=n)):(on=e,e=n);var c;return e}()))),r!==n&&(un=e,r=Je(r)),e=r,e}function mn(){let e,r,s,o,f,h,p,d,g,b,C;return e=on,r=on,s=t.substr(on,6),s.toLowerCase()===u?on+=6:(s=n,0===hn&&wn(mt)),s!==n?(o=Xn(),o!==n?(un=r,r=new ql.SELECT):(on=r,r=n)):(on=r,r=n),r!==n?(s=on,o=t.substr(on,9),o.toLowerCase()===l?on+=9:(o=n,0===hn&&wn(Et)),o!==n?(f=Xn(),f!==n?(h=Kn(),un=s,s=void(r._.localized=!0)):(on=s,s=n)):(on=s,s=n),s===n&&(s=null),o=on,f=t.substr(on,8),f.toLowerCase()===i?on+=8:(f=n,0===hn&&wn(Lt)),f!==n?(h=Xn(),h!==n?(p=Kn(),un=o,o=function(t){t._.distinct=!0}(r)):(on=o,o=n)):(on=o,o=n),o===n&&(o=null),f=on,h=t.substr(on,3),h.toLowerCase()===c?on+=3:(h=n,0===hn&&wn(vt)),h!==n?(p=Xn(),p!==n?(d=Kn(),un=f,f=function(t){t._.one=!0}(r)):(on=f,f=n)):(on=f,f=n),f===n&&(f=null),h=on,p=vn(),p!==n?(d=Kn(),un=h,h=function(t,e){t._.columns=e}(r,p)):(on=h,h=n),h===n&&(h=null),p=on,d=on,g=t.substr(on,4),g.toLowerCase()===a?on+=4:(g=n,0===hn&&wn(yt)),g!==n?(b=Xn(),b!==n?(g=[g,b],d=g):(on=d,d=n)):(on=d,d=n),d!==n?(g=Rn(),g===n&&(g=Zn()),g!==n?(un=p,p=function(t,e){t._.from=e}(r,g)):(on=p,p=n)):(on=p,p=n),p===n&&(p=null),d=on,g=Wn(),g!==n?(b=vn(),b!==n?(C=Yn(),C!==n?(un=d,d=function(t,e){t._.columns=e}(r,b)):(on=d,d=n)):(on=d,d=n)):(on=d,d=n),d===n&&(d=null),g=on,b=En(),b!==n&&(un=g,b=function(t,e){Object.assign(t._,e)}(r,b)),g=b,g===n&&(g=null),e=r):(on=e,e=n),e}function En(){let e,r,s,o,u,l,i,c,a,C,w,A;var x;return e=on,r=on,s=on,hn++,t.length>on?(o=t.charAt(on),on++):(o=n,0===hn&&wn(_t)),hn--,o!==n?(on=s,s=void 0):s=n,s!==n&&(un=r,s={}),r=s,r!==n?(s=on,o=on,u=t.substr(on,5),u.toLowerCase()===f?on+=5:(u=n,0===hn&&wn(St)),u!==n?(l=Xn(),l!==n?(u=[u,l],o=u):(on=o,o=n)):(on=o,o=n),o!==n?(u=_n(),u!==n?(l=Kn(),un=s,x=u,s=void(r.where=x)):(on=s,s=n)):(on=s,s=n),s===n&&(s=null),o=on,u=on,l=t.substr(on,8),l.toLowerCase()===h?on+=8:(l=n,0===hn&&wn($t)),l!==n?(i=Xn(),i!==n?(c=Sn(),c!==n?(a=Kn(),un=u,u=function(t,e){t.groupBy=e}(r,c)):(on=u,u=n)):(on=u,u=n)):(on=u,u=n),u!==n?(l=on,i=t.substr(on,6),i.toLowerCase()===p?on+=6:(i=n,0===hn&&wn(Ot)),i!==n?(c=Xn(),c!==n?(a=_n(),a!==n?(C=Kn(),un=l,l=function(t,e){t.having=e}(r,a)):(on=l,l=n)):(on=l,l=n)):(on=l,l=n),l===n&&(l=null),u=[u,l],o=u):(on=o,o=n),o===n&&(o=null),u=on,l=on,i=t.substr(on,8),i.toLowerCase()===d?on+=8:(i=n,0===hn&&wn(Tt)),i!==n?(c=Xn(),c!==n?(i=[i,c],l=i):(on=l,l=n)):(on=l,l=n),l!==n?(i=$n(),i!==n?(c=Kn(),un=u,u=function(t,e){t.orderBy=e}(r,i)):(on=u,u=n)):(on=u,u=n),u===n&&(u=null),l=on,i=on,c=t.substr(on,5),c.toLowerCase()===g?on+=5:(c=n,0===hn&&wn(Rt)),c!==n?(a=Xn(),a!==n?(C=Fn(),C===n&&(C=In()),C!==n?(w=Kn(),un=i,i=function(t,e){t.limit={rows:e}}(r,C)):(on=i,i=n)):(on=i,i=n)):(on=i,i=n),i!==n?(c=on,a=t.substr(on,6),a.toLowerCase()===b?on+=6:(a=n,0===hn&&wn(Pt)),a!==n?(C=Xn(),C!==n?(w=Fn(),w===n&&(w=In()),w!==n?(A=Kn(),un=c,c=function(t,e){t.limit.offset=e}(r,w)):(on=c,c=n)):(on=c,c=n)):(on=c,c=n),c===n&&(c=null),i=[i,c],l=i):(on=l,l=n),l===n&&(l=null),e=r):(on=e,e=n),e}function Ln(){let e,r,s,o,u;return e=on,r=on,s=Kn(),r=t.substring(r,on),s=t.substr(on,2),s.toLowerCase()===C?on+=2:(s=n,0===hn&&wn(jt)),s!==n?(o=Xn(),o!==n?(u=On(),u!==n?e=u:(on=e,e=n)):(on=e,e=n)):(on=e,e=n),e}function vn(){let e,r,s,o,u,l,i,c,a,f,h;for(e=on,r=[],42===t.charCodeAt(on)?(s=w,on++):(s=n,0===hn&&wn(Ft)),s===n&&(s=on,o=Fn(),o!==n?(u=on,l=Ln(),l!==n&&(un=u,l=Ke(o,l)),u=l,u===n&&(u=null),l=Jn(),l===n&&(l=null),i=on,c=Wn(),c!==n?(a=vn(),a!==n?(f=Yn(),f!==n?(un=i,i=Xe(o,l,a)):(on=i,i=n)):(on=i,i=n)):(on=i,i=n),i!==n?s=o:(on=s,s=n)):(on=s,s=n),s===n&&(s=on,o=Pn(),o!==n?(u=on,l=Ln(),l!==n&&(un=u,l=tn(o,l)),u=l,u===n&&(u=null),s=o):(on=s,s=n)));s!==n;)r.push(s),s=on,o=Vn(),o!==n?(42===t.charCodeAt(on)?(o=w,on++):(o=n,0===hn&&wn(Ft)),o===n&&(o=on,u=Fn(),u!==n?(l=on,i=Ln(),i!==n&&(un=l,i=Ke(u,i)),l=i,l===n&&(l=null),i=Jn(),i===n&&(i=null),c=on,a=Wn(),a!==n?(f=vn(),f!==n?(h=Yn(),h!==n?(un=c,c=Xe(u,i,f)):(on=c,c=n)):(on=c,c=n)):(on=c,c=n),c!==n?o=u:(on=o,o=n)):(on=o,o=n),o===n&&(o=on,u=Pn(),u!==n?(l=on,i=Ln(),i!==n&&(un=l,i=tn(u,i)),l=i,l===n&&(l=null),o=u):(on=o,o=n))),o===n?(on=s,s=n):s=o):s=o;return r.length<1?(on=e,e=n):e=r,e}function yn(){let e,r,s,o,u,l,i,c;return e=on,r=Kn(),91===t.charCodeAt(on)?(s=A,on++):(s=n,0===hn&&wn(It)),s!==n?(o=Kn(),u=function(){let e,r,s,o,u;e=on,r=Hn(),r!==n&&(un=e,l=r,r={where:tr.where(l)});var l;if(e=r,e===n){for(e=on,r=on,s=[],o=In();o!==n;)s.push(o),o=on,u=Vn(),u!==n?(u=In(),u===n?(on=o,o=n):o=u):o=u;s.length<1?(on=r,r=n):r=s,r!==n&&(un=e,r={where:r}),e=r,e===n&&(e=on,r=on,hn++,s=on,o=t.substr(on,5),o.toLowerCase()===f?on+=5:(o=n,0===hn&&wn(St)),o!==n?(u=Xn(),u!==n?(o=[o,u],s=o):(on=s,s=n)):(on=s,s=n),hn--,s===n?r=void 0:(on=r,r=n),r!==n?(s=jn(),s!==n?(un=e,e={where:s.xpr}):(on=e,e=n)):(on=e,e=n),e===n&&(e=En()))}return e}(),u!==n?(l=Kn(),93===t.charCodeAt(on)?(i=x,on++):(i=n,0===hn&&wn(Dt)),i!==n?(c=Kn(),e=u):(on=e,e=n)):(on=e,e=n)):(on=e,e=n),e}function _n(){let t,e;var r;return t=on,e=Hn(),e!==n&&(un=t,r=e,e=tr.where(r)),t=e,t===n&&(t=on,e=jn(),e!==n&&(un=t,e=e.xpr),t=e),t}function Sn(){let t,e,r,s,o;for(t=on,e=on,r=[],s=Fn();s!==n;)r.push(s),s=on,o=Vn(),o!==n?(o=Fn(),o===n?(on=s,s=n):s=o):s=o;return r.length<1?(on=e,e=n):e=r,e!==n?t=e:(on=t,t=n),t}function $n(){let e,r,s,o,u,l,i,c,a;for(e=on,r=on,s=[],o=on,u=Fn(),u!==n?(l=on,i=Xn(),i!==n?(c=t.substr(on,3),c.toLowerCase()===m?on+=3:(c=n,0===hn&&wn(Nt)),c===n&&(c=t.substr(on,4),c.toLowerCase()===E?on+=4:(c=n,0===hn&&wn(Ut))),c!==n?(un=l,l=en(u,c)):(on=l,l=n)):(on=l,l=n),l===n&&(l=null),o=u):(on=o,o=n);o!==n;)s.push(o),o=on,u=Vn(),u!==n?(u=on,l=Fn(),l!==n?(i=on,c=Xn(),c!==n?(a=t.substr(on,3),a.toLowerCase()===m?on+=3:(a=n,0===hn&&wn(Nt)),a===n&&(a=t.substr(on,4),a.toLowerCase()===E?on+=4:(a=n,0===hn&&wn(Ut))),a!==n?(un=i,i=en(l,a)):(on=i,i=n)):(on=i,i=n),i===n&&(i=null),u=l):(on=u,u=n),u===n?(on=o,o=n):o=u):o=u;return s.length<1?(on=r,r=n):r=s,r!==n?e=r:(on=e,e=n),e}function On(){let e,r,s,o,u,l,i,c,f;if(hn++,e=on,r=on,hn++,s=t.substr(on,2),s.toLowerCase()===C?on+=2:(s=n,0===hn&&wn(Xt)),hn--,s===n?r=void 0:(on=r,r=n),r!==n)if(s=on,hn++,o=t.substr(on,4),o.toLowerCase()===a?on+=4:(o=n,0===hn&&wn(te)),hn--,o===n?s=void 0:(on=s,s=n),s!==n)if(o=on,hn++,u=Mn(),hn--,u===n?o=void 0:(on=o,o=n),o!==n){if(u=on,l=on,i=t.charAt(on),dt.test(i)?on++:(i=n,0===hn&&wn(ee)),i!==n){for(c=[],f=t.charAt(on),gt.test(f)?on++:(f=n,0===hn&&wn(ne));f!==n;)c.push(f),f=t.charAt(on),gt.test(f)?on++:(f=n,0===hn&&wn(ne));i=[i,c],l=i}else on=l,l=n;u=l!==n?t.substring(u,on):l,u!==n?e=u:(on=e,e=n)}else on=e,e=n;else on=e,e=n;else on=e,e=n;return hn--,e===n&&(r=n,0===hn&&wn(Kt)),e}function Tn(){let e,r,s,o,u,l;for(hn++,e=on,r=on,s=on,o=[],u=On();u!==n;)o.push(u),u=on,l=Jn(),l!==n?(l=On(),l===n?(on=u,u=n):u=l):u=l;return o.length<1?(on=s,s=n):s=o,r=s!==n?t.substring(r,on):s,r!==n?e=r:(on=e,e=n),hn--,e===n&&(r=n,0===hn&&wn(re)),e}function Rn(){let e,r,s,o,u,l,i;var c;if(hn++,e=on,r=Hn(),r!==n&&(un=e,c=r,r=tr.subject(c)),e=r,e===n){for(e=on,r=on,s=[],o=on,u=On(),u!==n?(l=Jn(),l!==n?(u=[u,l],o=u):(on=o,o=n)):(on=o,o=n);o!==n;)s.push(o),o=on,u=On(),u!==n?(l=Jn(),l!==n?(u=[u,l],o=u):(on=o,o=n)):(on=o,o=n);r=t.substring(r,on),s=on,o=On(),o!==n?(u=Kn(),58===t.charCodeAt(on)?(l=N,on++):(l=n,0===hn&&wn(oe)),l!==n?(i=Kn(),s=o):(on=s,s=n)):(on=s,s=n),s===n&&(s=null),o=Fn(),o!==n?(u=Kn(),un=e,e=function(t,e,n){if(e)return{ref:[t+e,...n.ref]};if(t){let[e]=n.ref;e.id?e.id=t+e.id:n.ref[0]=t+e}return n}(r,s,o)):(on=e,e=n)}return hn--,e===n&&(r=n,0===hn&&wn(se)),e}function Pn(){let t,e;var r;return t=on,e=jn(),e!==n&&(un=t,e=(r=e).xpr.length>1?r:r.xpr[0]),t=e,t}function jn(){let t,e,r,s,o,u;if(t=on,e=[],r=Zn(),r===n&&(r=Nn(),r===n&&(r=zn(),r===n&&(r=In(),r===n&&(r=qn(),r===n&&(r=Un(),r===n&&(r=Fn(),r===n&&(r=on,s=kn(),s!==n?(o=Pn(),o!==n?(u=Gn(),u!==n?r=o:(on=r,r=n)):(on=r,r=n)):(on=r,r=n)))))))),r!==n)for(;r!==n;)e.push(r),r=Zn(),r===n&&(r=Nn(),r===n&&(r=zn(),r===n&&(r=In(),r===n&&(r=qn(),r===n&&(r=Un(),r===n&&(r=Fn(),r===n&&(r=on,s=kn(),s!==n?(o=Pn(),o!==n?(u=Gn(),u!==n?r=o:(on=r,r=n)):(on=r,r=n)):(on=r,r=n))))))));else e=n;return e!==n&&(un=t,e={xpr:e}),t=e,t}function Fn(){let t,e,r,s,o,u,l;for(hn++,t=on,e=on,r=[],s=on,o=On(),o!==n?(u=yn(),u===n&&(u=null),un=s,s=rn(o,u)):(on=s,s=n);s!==n;)r.push(s),s=on,o=Jn(),o!==n?(o=on,u=On(),u!==n?(l=yn(),l===n&&(l=null),un=o,o=rn(u,l)):(on=o,o=n),o===n?(on=s,s=n):s=o):s=o;return r.length<1?(on=e,e=n):e=r,e!==n&&(un=t,e={ref:e}),t=e,hn--,t===n&&(e=n,0===hn&&wn(ue)),t}function In(){let e,r;return hn++,e=Bn(),e===n&&(e=on,r=function(){let e,r;hn++,e=function(){let e,r,s,o,u,l;e=on,39===t.charCodeAt(on)?(r=ot,on++):(r=n,0===hn&&wn(Ne));if(r!==n){for(s=on,o=[],u=t.charAt(on),wt.test(u)?on++:(u=n,0===hn&&wn(Ue)),u===n&&(u=on,t.substr(on,2)===ut?(l=ut,on+=2):(l=n,0===hn&&wn(ze)),l!==n&&(un=u,l="'"),u=l);u!==n;)o.push(u),u=t.charAt(on),wt.test(u)?on++:(u=n,0===hn&&wn(Ue)),u===n&&(u=on,t.substr(on,2)===ut?(l=ut,on+=2):(l=n,0===hn&&wn(ze)),l!==n&&(un=u,l=sn()),u=l);s=t.substring(s,on),39===t.charCodeAt(on)?(o=ot,on++):(o=n,0===hn&&wn(Ne)),o!==n?e=s:(on=e,e=n)}else on=e,e=n;return e}(),e===n&&(e=function(){let e,r,s,o,u,l,i,c,a,f;e=on,r=on,s=on,45===t.charCodeAt(on)?(o=nt,on++):(o=n,0===hn&&wn(je));o===n&&(o=null);u=[],l=t.charAt(on),Ct.test(l)?on++:(l=n,0===hn&&wn(Fe));if(l!==n)for(;l!==n;)u.push(l),l=t.charAt(on),Ct.test(l)?on++:(l=n,0===hn&&wn(Fe));else u=n;if(u!==n){if(l=on,46===t.charCodeAt(on)?(i=rt,on++):(i=n,0===hn&&wn(Ie)),i!==n){if(c=[],a=t.charAt(on),Ct.test(a)?on++:(a=n,0===hn&&wn(Fe)),a!==n)for(;a!==n;)c.push(a),a=t.charAt(on),Ct.test(a)?on++:(a=n,0===hn&&wn(Fe));else c=n;c!==n?(i=[i,c],l=i):(on=l,l=n)}else on=l,l=n;if(l===n&&(l=null),i=on,101===t.charCodeAt(on)?(c=st,on++):(c=n,0===hn&&wn(De)),c!==n){if(a=[],f=t.charAt(on),Ct.test(f)?on++:(f=n,0===hn&&wn(Fe)),f!==n)for(;f!==n;)a.push(f),f=t.charAt(on),Ct.test(f)?on++:(f=n,0===hn&&wn(Fe));else a=n;a!==n?(c=[c,a],i=c):(on=i,i=n)}else on=i,i=n;i===n&&(i=null),o=[o,u,l,i],s=o}else on=s,s=n;r=s!==n?t.substring(r,on):s;r!==n&&(un=e,r=Number(r));return e=r,e}(),e===n&&(e=function(){let e,r,s,o;e=on,t.substr(on,4)===lt?(r=lt,on+=4):(r=n,0===hn&&wn(Be));r!==n?(s=on,hn++,o=Qn(),hn--,o===n?s=void 0:(on=s,s=n),s!==n?(un=e,e=!0):(on=e,e=n)):(on=e,e=n);return e}(),e===n&&(e=function(){let e,r,s,o;e=on,t.substr(on,5)===it?(r=it,on+=5):(r=n,0===hn&&wn(qe));r!==n?(s=on,hn++,o=Qn(),hn--,o===n?s=void 0:(on=s,s=n),s!==n?(un=e,e=!1):(on=e,e=n)):(on=e,e=n);return e}(),e===n&&(e=function(){let e,r,s,o;e=on,t.substr(on,4)===tt?(r=tt,on+=4):(r=n,0===hn&&wn(Te));r!==n?(s=on,hn++,o=Qn(),hn--,o===n?s=void 0:(on=s,s=n),s!==n?(un=e,e=null):(on=e,e=n)):(on=e,e=n);return e}()))));hn--,e===n&&(r=n,0===hn&&wn(Pe));return e}(),r!==n&&(un=e,r={val:r}),e=r),hn--,e===n&&(r=n,0===hn&&wn(le)),e}function Dn(){let t,e,r,s;for(hn++,t=on,e=[],r=zn(),r===n&&(r=In(),r===n&&(r=Fn(),r===n&&(r=Un())));r!==n;)e.push(r),r=on,s=Vn(),s!==n?(s=zn(),s===n&&(s=In(),s===n&&(s=Fn(),s===n&&(s=Un()))),s===n?(on=r,r=n):r=s):r=s;return e.length<2?(on=t,t=n):t=e,hn--,t===n&&(e=n,0===hn&&wn(le)),t}function Nn(){let t,e,r,s;return hn++,t=Bn(),t===n&&(t=on,e=kn(),e!==n?(r=on,s=Dn(),s!==n&&(un=r,s={list:s}),r=s,r!==n?(s=Gn(),s!==n?t=r:(on=t,t=n)):(on=t,t=n)):(on=t,t=n)),hn--,t===n&&(e=n,0===hn&&wn(ie)),t}function Un(){let e,r,s,o,u,l,i;if(hn++,e=on,r=On(),r!==n)if(s=kn(),s!==n){for(o=on,u=[],42===t.charCodeAt(on)?(l=w,on++):(l=n,0===hn&&wn(Ft)),l===n&&(l=Pn());l!==n;)u.push(l),l=on,i=Vn(),i!==n?(42===t.charCodeAt(on)?(i=w,on++):(i=n,0===hn&&wn(Ft)),i===n&&(i=Pn()),i===n?(on=l,l=n):l=i):l=i;u.length<1?(on=o,o=n):o=u,o!==n?(u=Gn(),u!==n?(un=e,e={func:r,args:o}):(on=e,e=n)):(on=e,e=n)}else on=e,e=n;else on=e,e=n;return hn--,e===n&&(r=n,0===hn&&wn(ce)),e}function zn(){let e,r,s;return hn++,e=on,35===t.charCodeAt(on)?(r=U,on++):(r=n,0===hn&&wn(fe)),r!==n?(s=On(),s!==n?(un=e,e={"#":s}):(on=e,e=n)):(on=e,e=n),hn--,e===n&&(r=n,0===hn&&wn(ae)),e}function Bn(){let e,r,s,o;return hn++,e=on,r=Kn(),63===t.charCodeAt(on)?(s=z,on++):(s=n,0===hn&&wn(pe)),s!==n?(o=Kn(),un=e,e=sr()):(on=e,e=n),hn--,e===n&&(r=n,0===hn&&wn(he)),e}function qn(){let e,r,s,o,u;if(hn++,e=on,r=Kn(),s=Mn(),s===n){if(s=on,o=[],u=t.charAt(on),bt.test(u)?on++:(u=n,0===hn&&wn(ge)),u!==n)for(;u!==n;)o.push(u),u=t.charAt(on),bt.test(u)?on++:(u=n,0===hn&&wn(ge));else o=n;s=o!==n?t.substring(s,on):o}return s!==n?(o=Kn(),e=s):(on=e,e=n),hn--,e===n&&(r=n,0===hn&&wn(de)),e}function Mn(){let e,r,s,o,u,l,i,c;if(e=on,t.substr(on,3)===B?(r=B,on+=3):(r=n,0===hn&&wn(be)),r===n&&(t.substr(on,2)===q?(r=q,on+=2):(r=n,0===hn&&wn(Ce)),r===n&&(t.substr(on,2)===M?(r=M,on+=2):(r=n,0===hn&&wn(we)),r===n&&(t.substr(on,7)===Z?(r=Z,on+=7):(r=n,0===hn&&wn(Ae)),r===n&&(t.substr(on,4)===H?(r=H,on+=4):(r=n,0===hn&&wn(xe)),r===n&&(t.substr(on,6)===W?(r=W,on+=6):(r=n,0===hn&&wn(me)),r===n&&(t.substr(on,8)===Y?(r=Y,on+=8):(r=n,0===hn&&wn(Ee)))))))),r!==n?(s=on,hn++,o=Qn(),hn--,o===n?s=void 0:(on=s,s=n),s!==n?e=r:(on=e,e=n)):(on=e,e=n),e===n&&(e=on,t.substr(on,4)===k?(r=k,on+=4):(r=n,0===hn&&wn(Le)),r===n&&(t.substr(on,4)===G?(r=G,on+=4):(r=n,0===hn&&wn(ve)),r===n&&(t.substr(on,4)===Q?(r=Q,on+=4):(r=n,0===hn&&wn(ye)),r===n&&(t.substr(on,4)===V?(r=V,on+=4):(r=n,0===hn&&wn(_e)),r===n&&(t.substr(on,3)===J?(r=J,on+=3):(r=n,0===hn&&wn(Se)))))),r!==n?(s=on,hn++,o=Qn(),hn--,o===n?s=void 0:(on=s,s=n),s!==n?e=r:(on=e,e=n)):(on=e,e=n),e===n)){for(e=on,r=on,s=[],o=on,t.substr(on,2)===K?(u=K,on+=2):(u=n,0===hn&&wn($e)),u===n&&(t.substr(on,3)===X?(u=X,on+=3):(u=n,0===hn&&wn(Oe)),u===n&&(t.substr(on,4)===tt?(u=tt,on+=4):(u=n,0===hn&&wn(Te)),u===n&&(t.substr(on,5)===et?(u=et,on+=5):(u=n,0===hn&&wn(Re))))),u!==n?(l=on,hn++,i=Qn(),hn--,i===n?l=void 0:(on=l,l=n),l!==n?(u=[u,l],o=u):(on=o,o=n)):(on=o,o=n);o!==n;)s.push(o),o=on,u=Kn(),u=on,t.substr(on,2)===K?(l=K,on+=2):(l=n,0===hn&&wn($e)),l===n&&(t.substr(on,3)===X?(l=X,on+=3):(l=n,0===hn&&wn(Oe)),l===n&&(t.substr(on,4)===tt?(l=tt,on+=4):(l=n,0===hn&&wn(Te)),l===n&&(t.substr(on,5)===et?(l=et,on+=5):(l=n,0===hn&&wn(Re))))),l!==n?(i=on,hn++,c=Qn(),hn--,c===n?i=void 0:(on=i,i=n),i!==n?(l=[l,i],u=l):(on=u,u=n)):(on=u,u=n),u===n?(on=o,o=n):o=u;s.length<1?(on=r,r=n):r=s,e=r!==n?t.substring(e,on):r}return e}function Zn(){let t,e,r,s;return t=on,e=kn(),e!==n?(r=mn(),r!==n?(s=Gn(),s!==n?t=r:(on=t,t=n)):(on=t,t=n)):(on=t,t=n),t}function Hn(){let e,r,s,o;return e=on,r=Kn(),63===t.charCodeAt(on)?(s=z,on++):(s=n,0===hn&&wn(pe)),s!==n?(o=Kn(),un=e,e=nr.shift()):(on=e,e=n),e}function Wn(){let e,r,s,o;return e=on,r=Kn(),123===t.charCodeAt(on)?(s=ct,on++):(s=n,0===hn&&wn(Me)),s!==n?(o=Kn(),r=[r,s,o],e=r):(on=e,e=n),e}function Yn(){let e,r,s,o;return e=on,r=Kn(),125===t.charCodeAt(on)?(s=at,on++):(s=n,0===hn&&wn(Ze)),s!==n?(o=Kn(),r=[r,s,o],e=r):(on=e,e=n),e}function kn(){let e,r,s,o;return e=on,r=Kn(),40===t.charCodeAt(on)?(s=ft,on++):(s=n,0===hn&&wn(He)),s!==n?(o=Kn(),r=[r,s,o],e=r):(on=e,e=n),e}function Gn(){let e,r,s,o;return e=on,r=Kn(),41===t.charCodeAt(on)?(s=ht,on++):(s=n,0===hn&&wn(We)),s!==n?(o=Kn(),r=[r,s,o],e=r):(on=e,e=n),e}function Qn(){let e;return e=t.charAt(on),At.test(e)?on++:(e=n,0===hn&&wn(Ye)),e}function Vn(){let e,r,s,o;return hn++,e=on,r=Kn(),44===t.charCodeAt(on)?(s=pt,on++):(s=n,0===hn&&wn(Ge)),s!==n?(o=Kn(),e=s):(on=e,e=n),hn--,e===n&&(r=n,0===hn&&wn(ke)),e}function Jn(){let e,r,s,o;return hn++,e=on,r=Kn(),46===t.charCodeAt(on)?(s=rt,on++):(s=n,0===hn&&wn(Ie)),s!==n?(o=Kn(),un=e,e="."):(on=e,e=n),hn--,e===n&&(r=n,0===hn&&wn(Qe)),e}function Kn(){let e,r,s;for(hn++,e=on,r=[],s=t.charAt(on),xt.test(s)?on++:(s=n,0===hn&&wn(Ve));s!==n;)r.push(s),s=t.charAt(on),xt.test(s)?on++:(s=n,0===hn&&wn(Ve));return e=t.substring(e,on),hn--,e}function Xn(){let e,r,s;if(hn++,e=on,r=[],s=t.charAt(on),xt.test(s)?on++:(s=n,0===hn&&wn(Ve)),s!==n)for(;s!==n;)r.push(s),s=t.charAt(on),xt.test(s)?on++:(s=n,0===hn&&wn(Ve));else r=n;return e=r!==n?t.substring(e,on):r,hn--,e===n&&(r=n,0===hn&&wn(Qe)),e}const{fluent:tr=ql.fluent,late_binding:er}=peg$parse,{args:nr=[],startRule:rr}=e,sr=!rr&&er?()=>or[ur++]={ref:["?"],param:!0}:()=>or[ur]=tr.expr(nr[ur++]),or=[];let ur=0;cn=o();const lr=cn!==n&&on===t.length;function ir(){throw cn!==n&&on<t.length&&wn({type:"end"}),An(fn,an<t.length?function(e=on){const n=t.codePointAt(e);return void 0===n?"":String.fromCodePoint(n)}(an):null,an<t.length?Cn(an,an+1):Cn(an,an))}return e.peg$library?{peg$result:cn,peg$currPos:on,peg$FAILED:n,peg$maxFailExpected:fn,peg$maxFailPos:an,peg$success:lr,peg$throw:lr?void 0:ir}:lr?cn:void ir()}const peg$allowedStartRules=["CQL","entity","columns","ref","val","vals","expr","where","groupBy","orderBy"];export{peg$allowedStartRules as StartRules,peg$SyntaxError as SyntaxError,peg$parse as parse};
|
|
2
|
+
//# sourceMappingURL=parser.min.js.map
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { SELECT as SELECT_ } from './ql/select.js'
|
|
2
|
+
import { INSERT as INSERT_ } from './ql/insert.js'
|
|
3
|
+
import { UPSERT as UPSERT_ } from './ql/insert.js'
|
|
4
|
+
import { UPDATE as UPDATE_ } from './ql/update.js'
|
|
5
|
+
import { DELETE as DELETE_ } from './ql/delete.js'
|
|
6
|
+
|
|
7
|
+
// Re-export the query classes with fluent interfaces
|
|
8
|
+
export const SELECT = SELECT_.chimera (SELECT_.columns)
|
|
9
|
+
export const INSERT = INSERT_.chimera (INSERT_.entries)
|
|
10
|
+
export const UPSERT = UPSERT_.chimera (UPSERT_.entries)
|
|
11
|
+
export const UPDATE = UPDATE_.chimera (UPDATE_.entity)
|
|
12
|
+
export const DELETE = DELETE_.chimera (DELETE_.from)
|
|
13
|
+
|
|
14
|
+
// Re-export all the helper functions and classes from the xl module
|
|
15
|
+
export * from './xl/ttl.js'
|
|
16
|
+
import * as peg from '../cql/parser.min.js'
|
|
17
|
+
import { Query } from './ql/base-class.js'
|
|
18
|
+
export { Query }
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* This is the central entry point for creating queries from various types of input:
|
|
23
|
+
* - If the input is a `Query` instance already, it's returned as-is.
|
|
24
|
+
* - If the input is a `string` or a template literal, it's parsed into a CQN object.
|
|
25
|
+
* - If the input is an `object` with a recognized query kind, it's cast into an instance of
|
|
26
|
+
* the corresponding query class.
|
|
27
|
+
* @param { Query | string | object } qso - The input, as described above, to be converted into an instance of a Query subclass.
|
|
28
|
+
* @param { ...any } args - Optional arguments for template literals or parameterized queries.
|
|
29
|
+
* @import { SELECT } from './ql/select.js'
|
|
30
|
+
* @import { INSERT } from './ql/insert.js'
|
|
31
|
+
* @import { UPSERT } from './ql/insert.js'
|
|
32
|
+
* @import { UPDATE } from './ql/update.js'
|
|
33
|
+
* @import { DELETE } from './ql/delete.js'
|
|
34
|
+
* @returns { SELECT & INSERT & UPSERT & UPDATE & DELETE }
|
|
35
|
+
*/
|
|
36
|
+
export function cql (qso, ...args) {
|
|
37
|
+
if (!qso || qso instanceof Query) return qso
|
|
38
|
+
if (qso.raw) return cql.cache.query4 (qso, ...args)
|
|
39
|
+
if (typeof qso === 'string') return peg.parse (qso, {args})
|
|
40
|
+
else for (let k in qso) if (k in peg.ql) //> usually query.kind is first key
|
|
41
|
+
return Object.setPrototypeOf (qso, peg.ql[k].prototype)
|
|
42
|
+
return null //> for any other object
|
|
43
|
+
}
|
|
44
|
+
export default cql
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
// Prepare a cache for parsed queries, and export it as part of the cql facade
|
|
48
|
+
// for easy access and control over caching behavior.
|
|
49
|
+
import { QueryCache } from './ql/cache.js'
|
|
50
|
+
cql.cache = new QueryCache()
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as peg from '../../cql/parser.min.js'
|
|
2
|
+
import { expr } from '../xl/fluent.js'
|
|
3
|
+
|
|
4
|
+
export class Query {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Turns subclasses of Query into fluent APIs that allow constructing instances with `new`
|
|
8
|
+
* as well as plain function calls. For example, this enables usages like these: @example
|
|
9
|
+
* SELECT`ID,title`.from`Books`
|
|
10
|
+
* UPDATE`Books`.with`${data}`
|
|
11
|
+
* @param fn - The function to be called when the fluent API is invoked without `new`.
|
|
12
|
+
* @type <T> (this:T, fn:()=>T) => fn & T
|
|
13
|
+
*/
|
|
14
|
+
static chimera (fn) { const self = this, kind = this.name
|
|
15
|
+
function fx (...args) { return new.target ? new self : fn.call (self,...args) }
|
|
16
|
+
Object.defineProperty (this.prototype, '_', { get(){ return this[kind] }})
|
|
17
|
+
Object.defineProperty (this.prototype, 'kind', { value: kind })
|
|
18
|
+
Object.defineProperty (this, 'name', { value: 'Query' })
|
|
19
|
+
Object.defineProperty (fx, 'name', { value: kind })
|
|
20
|
+
Object.defineProperty (fx, 'class', { value: this })
|
|
21
|
+
Object.defineProperty (fx, 'prototype', { value: this.prototype })
|
|
22
|
+
Object.setPrototypeOf (fx,this) //> adds static members
|
|
23
|
+
return peg.ql[kind] = fx
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* - Creates a shallow clone of the given `query`.
|
|
28
|
+
* - Subsequent changes to the clone will not affect the original.
|
|
29
|
+
* - However, changes to nested objects will affect both the original and the clone.
|
|
30
|
+
* @param clauses - Optional list of objects with clauses to override in the clone.
|
|
31
|
+
* @type <T> (this:T, clauses:{columns,from,where,having,groupy,orderBy,limit}[]) => T
|
|
32
|
+
*/
|
|
33
|
+
clone (...clauses) {
|
|
34
|
+
const _ = this.kind || Object.keys(this)[0]
|
|
35
|
+
return Object.assign ({ __proto__: this,
|
|
36
|
+
[_]: { __proto__: this[_] }
|
|
37
|
+
}, ...clauses)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* - Fills in any binding `{param}`s with corresponding `{val}`s from given `args`.
|
|
42
|
+
* - If no `args` are provided, the ones collected during parsing are used.
|
|
43
|
+
*/
|
|
44
|
+
bind (...args) {
|
|
45
|
+
let vals = args?.length ? args : this.args || [], i=0
|
|
46
|
+
if (this.params) for (let p of this.params) {
|
|
47
|
+
if ('list' in p) p.list = vals[i++].map(expr)
|
|
48
|
+
else p.val = vals[i++]
|
|
49
|
+
delete p.param
|
|
50
|
+
delete p.ref
|
|
51
|
+
}
|
|
52
|
+
else !function traverse (cqn) { // for manually created CQNs
|
|
53
|
+
Object.entries (cqn) .forEach (([k,x]) => {
|
|
54
|
+
if (!x || typeof x !== 'object') return
|
|
55
|
+
if (!x.param) return traverse(x)
|
|
56
|
+
let val = vals[i++]; if (val === undefined) return
|
|
57
|
+
let y = cqn[k] = {val}
|
|
58
|
+
if (x.as) y.as = x.as
|
|
59
|
+
})
|
|
60
|
+
} (this)
|
|
61
|
+
return this
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Adds the given values to the specified clause in the CQN. If the clause already
|
|
66
|
+
* exists, the new values are appended to the existing ones. Otherwise, a clause
|
|
67
|
+
* is added with the provided values.
|
|
68
|
+
*/
|
|
69
|
+
_add (clause, values, former = this._[clause]) {
|
|
70
|
+
if (values === undefined) return this
|
|
71
|
+
this._[clause] = !former ? values : [ ...former, ...values ]
|
|
72
|
+
return this
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Sets the specified clause in the CQN to the given value, replacing existing
|
|
77
|
+
* one if present, or overriding it in case of cloned queries.
|
|
78
|
+
*/
|
|
79
|
+
_set (clause, value) {
|
|
80
|
+
if (value === undefined) return this
|
|
81
|
+
this._[clause] = value
|
|
82
|
+
return this
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns the main subject `{ref}` of the query, which refers to the entity being queried or modified.
|
|
87
|
+
* - For `SELECT` and `DELETE` queries, this is the `from` clause.
|
|
88
|
+
* - For `INSERT` and `UPSERT` queries, this is the `into` clause.
|
|
89
|
+
* - For `UPDATE` queries, this is the `entity` clause.
|
|
90
|
+
* @returns {ref:string[]} The main subject of the query.
|
|
91
|
+
*/
|
|
92
|
+
get subject() { return this._.from }
|
|
93
|
+
|
|
94
|
+
// Non-enumerable properties that are only valid within the same process
|
|
95
|
+
/** @private */ set source (cql) { Object.defineProperty (this, 'source', { value: cql, writable: true }) }
|
|
96
|
+
/** @private */ set params (arr) { Object.defineProperty (this, 'params', { value: arr, writable: true }) }
|
|
97
|
+
/** @private */ set args (arr) { Object.defineProperty (this, 'args', { value: arr, writable: true }) }
|
|
98
|
+
}
|
package/lib/ql/cache.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as peg from '../../cql/parser.min.js'
|
|
2
|
+
|
|
3
|
+
export class QueryCache extends WeakMap {
|
|
4
|
+
|
|
5
|
+
cached (strings, ...args) {
|
|
6
|
+
if (!strings?.raw) return this.parse (strings, ...args)
|
|
7
|
+
const cached = this.get (strings)
|
|
8
|
+
if (cached) {
|
|
9
|
+
const q = new peg.ql[cached.kind]
|
|
10
|
+
q[q.kind] = cached[cached.kind]
|
|
11
|
+
q.params = cached.params
|
|
12
|
+
if (args?.length) q.args = args
|
|
13
|
+
return q
|
|
14
|
+
} else {
|
|
15
|
+
const q = this.parse (strings, ...args)
|
|
16
|
+
this.set (strings, q)
|
|
17
|
+
return q
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
parse (strings, ...args) {
|
|
22
|
+
const src = String.raw (strings, ...'?'.repeat(args.length))
|
|
23
|
+
return peg.parse (src, {args})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
enable(){
|
|
27
|
+
peg.parse.late_binding = true
|
|
28
|
+
this.query4 = this.cached
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
disable(){
|
|
32
|
+
peg.parse.late_binding = false
|
|
33
|
+
this.query4 = this.parse
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
query4 = this.parse
|
|
37
|
+
|
|
38
|
+
}
|
package/lib/ql/delete.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { subject, where } from '../xl/ttl.js'
|
|
2
|
+
import { Query } from './base-class.js'
|
|
3
|
+
|
|
4
|
+
export class DELETE extends Query { DELETE = {}
|
|
5
|
+
static from (...args) { return (new this)._set('from', subject (...args)) }
|
|
6
|
+
where (...args) { return this._add('where', where (...args)) }
|
|
7
|
+
}
|
package/lib/ql/insert.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { subject, columns, values, fluent, unwrapped } from '../xl/ttl.js'
|
|
2
|
+
import { Query } from './base-class.js'
|
|
3
|
+
import { cql } from '../index.js'
|
|
4
|
+
|
|
5
|
+
export class XSERT extends Query { INSERT = {}
|
|
6
|
+
static entries (...args) { return (new this).entries(...args) }
|
|
7
|
+
static into (x,...tail) {
|
|
8
|
+
if (x.raw) return (new this)._set ('into', subject(x,...tail)) //> INSERT.into`Foo`
|
|
9
|
+
const insert = (new this)._set ('into', subject(x)) //> INSERT.into(Foo)
|
|
10
|
+
return !tail.length ? insert : insert._entries(...tail) //> INSERT.into(Foo, ...?)
|
|
11
|
+
}; _entries (...tail) {
|
|
12
|
+
if (unwrapped(tail)[0]?.split) return this.columns(...tail) //> INSERT.into(Foo, columns)
|
|
13
|
+
return this._set ('entries', fluent.entries(...tail)) //> INSERT.into(Foo, entries)
|
|
14
|
+
}
|
|
15
|
+
into (...args) { return this._set('into', subject(...args)) }
|
|
16
|
+
entries (...args) { return this._set('entries', fluent.entries(...args)) }
|
|
17
|
+
columns (...args) { return this._add('columns', columns(...args).map(c => c.ref[0])) }
|
|
18
|
+
values (...args) { return this._set('values', values(...args)) }
|
|
19
|
+
rows (...args) { return this._set('rows', unwrapped(args)) }
|
|
20
|
+
from (...args) { return this._set('from', cql(...args)) }
|
|
21
|
+
get subject() { return this._.into }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class INSERT extends XSERT { INSERT = {} }
|
|
25
|
+
export class UPSERT extends XSERT { UPSERT = {} }
|
package/lib/ql/select.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { subject, columns, where, orderBy, val } from '../xl/ttl.js'
|
|
2
|
+
import { Query } from './base-class.js'
|
|
3
|
+
|
|
4
|
+
export class SELECT extends Query { SELECT = {}
|
|
5
|
+
static from (...args) { return (new this).from (...args) }
|
|
6
|
+
static columns (...args) { return (new this).columns (...args) }
|
|
7
|
+
static get localized() { return (new this).localized }
|
|
8
|
+
static get distinct() { return (new this).distinct }
|
|
9
|
+
static get one() { return (new this).one }
|
|
10
|
+
get localized() { return this._set('localized', true) }
|
|
11
|
+
get distinct() { return this._set('distinct', true) }
|
|
12
|
+
get one() { return this._set('one', true) }
|
|
13
|
+
columns (...args) { return this._add('columns', columns (...args)) }
|
|
14
|
+
from (...args) { return this._set('from', subject (...args)) }
|
|
15
|
+
where (...args) { return this._add('where', where (...args)) }
|
|
16
|
+
having (...args) { return this._add('having', where (...args)) }
|
|
17
|
+
groupBy (...args) { return this._add('groupBy', columns (...args)) }
|
|
18
|
+
orderBy (...args) { return this._add('orderBy', orderBy (...args)) }
|
|
19
|
+
limit (...args) { return args[0]?.raw ? this.top(...args) : this.top(args[0]).skip(args[1]) }
|
|
20
|
+
top (...args) { if (args[0]) (this._.limit ??= {}).rows = val(...args); return this }
|
|
21
|
+
skip (...args) { if (args[0]) (this._.limit ??= {}).offset = val(...args); return this }
|
|
22
|
+
offset (...args) { return this.skip(...args) }
|
|
23
|
+
}
|
package/lib/ql/update.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { subject, where } from '../xl/ttl.js'
|
|
2
|
+
import { Query } from './base-class.js'
|
|
3
|
+
|
|
4
|
+
export class UPDATE extends Query { UPDATE = {}
|
|
5
|
+
static entity (...args) { return (new this)._set('entity', subject (...args)) }
|
|
6
|
+
where (...args) { return this._add('where', where (...args)) }
|
|
7
|
+
with (data,x) { return this._set('with', data.raw ? x : data) }
|
|
8
|
+
set (data,x) { return this._set('with', data.raw ? x : data) }
|
|
9
|
+
get subject() { return this._.entity }
|
|
10
|
+
}
|
package/lib/xl/fluent.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const unwrapped = args => Array.isArray(args[0]) ? args[0] : args
|
|
2
|
+
|
|
3
|
+
function entity (e,ref) {
|
|
4
|
+
Object.defineProperty (this, 'target', {value:e}) .ref = ref
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const subject = (x,pk) => {
|
|
8
|
+
const id = x.name || x, ref = [ !pk ? id : { id, where: where(pk) } ]
|
|
9
|
+
return x.name ? new entity(x,ref) : {ref}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const entries = (...args) => unwrapped(args)
|
|
13
|
+
export const columns = (...args) => unwrapped(args) .map (ref)
|
|
14
|
+
export const values = (...args) => unwrapped(args) .map (expr)
|
|
15
|
+
|
|
16
|
+
export const where = (x) => {
|
|
17
|
+
if (!x || typeof x !== 'object') return [ val(x) ]
|
|
18
|
+
return Object.entries(x) .map (([k,v]) => ['and', ref(k), '=', val(v)])
|
|
19
|
+
.flat().slice(1)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const orderBy = (obj) => Object.entries(obj) .map (
|
|
23
|
+
([k,v]) => ({ref:[k], sort: {1:'asc','-1':'desc'}[v] || v})
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
export const ref = (r) => ({ ref: String(r).split('.') })
|
|
27
|
+
export const val = (v) => ({ val: v })
|
|
28
|
+
export const expr = (x) => {
|
|
29
|
+
if (Array.isArray(x)) return { list: x.map(expr) }
|
|
30
|
+
if (x && typeof x === 'object') return x //> already a cxn object
|
|
31
|
+
else return val(x)
|
|
32
|
+
}
|
package/lib/xl/ttl.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as fluent from './fluent.js'; export { fluent }
|
|
2
|
+
import * as peg from '../../cql/parser.min.js'
|
|
3
|
+
|
|
4
|
+
export const { unwrapped } = fluent
|
|
5
|
+
export const parse = peg.parse
|
|
6
|
+
parse.ttl = (startRule, x, ...args) => x?.raw && parse (String.raw (x, ...'?'.repeat(args.length)), { startRule, args })
|
|
7
|
+
parse.str = (startRule, x, ...args) => parse (x, { startRule, args })
|
|
8
|
+
parse.fluent = fluent
|
|
9
|
+
|
|
10
|
+
export const subject = (...args) => parse.ttl ('entity', ...args) || fluent.subject (...args)
|
|
11
|
+
export const columns = (...args) => parse.ttl ('columns', ...args) || fluent.columns (...args)
|
|
12
|
+
export const values = (...args) => parse.ttl ('vals', ...args) || fluent.values (...args)
|
|
13
|
+
export const where = (...args) => parse.ttl ('where', ...args) || fluent.where (...args)
|
|
14
|
+
export const having = where
|
|
15
|
+
export const groupBy = columns
|
|
16
|
+
export const orderBy = (...args) => {
|
|
17
|
+
const cxn = parse.ttl ('orderBy', ...args); if (cxn) return cxn
|
|
18
|
+
if (typeof (args = unwrapped(args))[0] === 'string')
|
|
19
|
+
return parse.str ('orderBy', args.join(','))
|
|
20
|
+
else return fluent.orderBy (...args) //> { a:1, b:-1 }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const ref = (r,...tail) => parse.ttl ('ref', r, ...tail) || { ref: r.split('.') }
|
|
24
|
+
export const val = (...args) => parse.ttl ('val', ...args) || { val: args[0] }
|
|
25
|
+
export const expr = (...args) => {
|
|
26
|
+
const cxn = parse.ttl ('expr', ...args); if (cxn) return cxn
|
|
27
|
+
const xpr = args.map (fluent.expr)
|
|
28
|
+
return xpr.length === 1 ? xpr[0] : {xpr}
|
|
29
|
+
}
|
package/license
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cap.core/ql",
|
|
3
|
+
"version": "1.2.3",
|
|
4
|
+
"description": "Minimalistic implementation of `cds.ql`, carved out from CAP.",
|
|
5
|
+
"author": "daniel.hutzel@sap.com",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "lib/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"cql/parser.min.js",
|
|
10
|
+
"lib"
|
|
11
|
+
],
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/cap.core__ql": "file:./@types"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"peggy": "npx peggy cql/grammar.pegjs -o cql/parser.js --format es --allowed-start-rules CQL,entity,columns,ref,val,vals,expr,where,groupBy,orderBy",
|
|
17
|
+
"terser": "npx terser cql/parser.js -o cql/parser.min.js --compress --mangle --ecma 2022 --source-map \"base=ql/cql,url=parser.min.js.map\"",
|
|
18
|
+
"build": "npm run peggy && npm run terser"
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0"
|
|
21
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Minimalistic `cds.ql`
|
|
2
|
+
|
|
3
|
+
This is a minimalistic implementation of `cds.ql` that supports parsing and constructing SQL and [CQL](https://cap.cloud.sap/docs/cds/cql) queries into [CQN](https://cap.cloud.sap/docs/cds/cqn) objects. It provides a fluent API for constructing queries as well as a parser for CQL strings, including tagged template literals.
|
|
4
|
+
|
|
5
|
+
### Setup
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm add cap-js/ql
|
|
9
|
+
```
|
|
10
|
+
In addition, you may want to install the type declarations for better IntelliSense in VSCode:
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
npm add -D cap-js/ql-types
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Usage
|
|
17
|
+
|
|
18
|
+
Import from ESM modules, or from CommonJS modules:
|
|
19
|
+
```js
|
|
20
|
+
import { cql } from '@cap.core/ql'
|
|
21
|
+
```
|
|
22
|
+
```js
|
|
23
|
+
const { cql } = require ('@cap.core/ql')
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Then construct queries like so:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
cql`SELECT ID, title from Books`
|
|
30
|
+
```
|
|
31
|
+
```js
|
|
32
|
+
cql`SELECT from Authors {
|
|
33
|
+
ID, name, books {
|
|
34
|
+
ID, title, genre.name as genre
|
|
35
|
+
}
|
|
36
|
+
}`
|
|
37
|
+
```
|
|
38
|
+
### Try it in Node REPL
|
|
39
|
+
|
|
40
|
+
Start [Node's REPL](https://nodejs.org/learn/command-line/how-to-use-the-nodejs-repl) from your command line:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
node
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Within there import the `cql` function:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const { cql } = await import ('@cap.core/ql')
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Then copy and paste this:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
let ID = 201
|
|
56
|
+
cql`INSERT into Books ${{ ID, title:'Wuthering Heights' }}`
|
|
57
|
+
cql`UPSERT into Books ${{ ID, title:'Wuthering Heights' }}`
|
|
58
|
+
cql`UPDATE Books with ${{ stock:111 }} where ID=${ID}`
|
|
59
|
+
cql`SELECT ID, title, stock from Books where ID=${ID}`
|
|
60
|
+
cql`DELETE from Books where ${[ID]}`
|
|
61
|
+
cql`DELETE Books[${ID}]` // → same as above
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
> ⇒ the constructed CQN query objects are printed as output.
|
|
65
|
+
|
|
66
|
+
## Contribute
|
|
67
|
+
|
|
68
|
+
Feel invited to contribute.
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
git clone --recursive https://github.com/cap-js/ql
|
|
72
|
+
cd cds.ql
|
|
73
|
+
npm i
|
|
74
|
+
```
|
|
75
|
+
> The `--recursive` flag clones the `@types` submodule as well, which contains the type declarations.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
### Try it in REPL or Browser
|
|
79
|
+
|
|
80
|
+
Launch Node's REPL, with `cqn` and all other API entry points preloaded into `global` constants:
|
|
81
|
+
|
|
82
|
+
```shell
|
|
83
|
+
npm run in:repl
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
Alternatively, launch a server and open http://localhost:4004 in your browser:
|
|
88
|
+
|
|
89
|
+
```shell
|
|
90
|
+
npm run in:browser
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Then try out the API in your browser's [Developer Tools](https://developer.mozilla.org/docs/Learn_web_development/Howto/Tools_and_setup/What_are_browser_developer_tools) console – for example, copy and paste the sample lines above to start with.
|
|
94
|
+
|
|
95
|
+
### Running Tests
|
|
96
|
+
|
|
97
|
+
```shell
|
|
98
|
+
npx vitest
|
|
99
|
+
```
|
|
100
|
+
```shell
|
|
101
|
+
cds test
|
|
102
|
+
```
|
|
103
|
+
```shell
|
|
104
|
+
chest
|
|
105
|
+
```
|
|
106
|
+
> The latter ones are for users of [CAP](https://cap.cloud.sap) in general, or [@cap.core/cds-test](https://github.com/cap-js/cds-test) in particular.
|