@barishnamazov/gsql 0.2.2 → 0.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/dist/index.js CHANGED
@@ -1,25 +1,25 @@
1
- import{CstParser as Ue}from"chevrotain";import{createToken as c,Lexer as F}from"chevrotain";var fe=c({name:"WhiteSpace",pattern:/\s+/,group:F.SKIPPED}),Ee=c({name:"LineComment",pattern:/\/\/[^\n]*/,group:F.SKIPPED}),Se=c({name:"BlockComment",pattern:/\/\*[\s\S]*?\*\//,group:F.SKIPPED}),r=c({name:"Identifier",pattern:/[a-zA-Z_][a-zA-Z0-9_]*/}),D=c({name:"TemplateIdentifier",pattern:/\{[a-zA-Z_][a-zA-Z0-9_]*\}/}),j=c({name:"Concept",pattern:/concept/,longer_alt:r}),z=c({name:"Schema",pattern:/schema/,longer_alt:r}),Y=c({name:"Mixin",pattern:/mixin/,longer_alt:r}),Q=c({name:"Enum",pattern:/enum/,longer_alt:r}),H=c({name:"Extension",pattern:/extension/,longer_alt:r}),K=c({name:"Function",pattern:/function/,longer_alt:r}),V=c({name:"Func",pattern:/func/,longer_alt:r}),Z=c({name:"Trigger",pattern:/trigger/,longer_alt:r}),R=c({name:"Index",pattern:/index/,longer_alt:r}),I=c({name:"Check",pattern:/check/,longer_alt:r}),J=c({name:"Before",pattern:/before/,longer_alt:r}),W=c({name:"After",pattern:/after/,longer_alt:r}),X=c({name:"Ondelete",pattern:/ondelete/,longer_alt:r}),tt=c({name:"On",pattern:/on/,longer_alt:r}),et=c({name:"Each",pattern:/each/,longer_alt:r}),nt=c({name:"Row",pattern:/row/,longer_alt:r}),st=c({name:"Statement",pattern:/statement/,longer_alt:r}),it=c({name:"Execute",pattern:/execute/,longer_alt:r}),T=c({name:"Unique",pattern:/unique/,longer_alt:r}),y=c({name:"Gin",pattern:/gin/,longer_alt:r}),v=c({name:"Gist",pattern:/gist/,longer_alt:r}),b=c({name:"Btree",pattern:/btree/,longer_alt:r}),k=c({name:"Hash",pattern:/hash/,longer_alt:r}),rt=c({name:"Pkey",pattern:/pkey/,longer_alt:r}),ot=c({name:"Nonull",pattern:/nonull/,longer_alt:r}),at=c({name:"Default",pattern:/default/,longer_alt:r}),ct=c({name:"Ref",pattern:/ref/,longer_alt:r}),lt=c({name:"Cascade",pattern:/cascade/,longer_alt:r}),ht=c({name:"Restrict",pattern:/restrict/,longer_alt:r}),pt=c({name:"SetNull",pattern:/setnull/,longer_alt:r}),mt=c({name:"SetDefault",pattern:/setdefault/,longer_alt:r}),ut=c({name:"NoAction",pattern:/noaction/,longer_alt:r}),ft=c({name:"Update",pattern:/update/,longer_alt:r}),Et=c({name:"Insert",pattern:/insert/,longer_alt:r}),St=c({name:"Delete",pattern:/delete/,longer_alt:r}),Ct=c({name:"Return",pattern:/return/,longer_alt:r}),B=c({name:"New",pattern:/NEW/,longer_alt:r}),_=c({name:"Old",pattern:/OLD/,longer_alt:r}),Ut=c({name:"Serial",pattern:/serial/i,longer_alt:r}),Nt=c({name:"BigSerial",pattern:/bigserial/i,longer_alt:r}),Lt=c({name:"Integer",pattern:/integer/i,longer_alt:r}),gt=c({name:"Bigint",pattern:/bigint/i,longer_alt:r}),Ot=c({name:"SmallInt",pattern:/smallint/i,longer_alt:r}),dt=c({name:"Text",pattern:/text/i,longer_alt:r}),Tt=c({name:"Varchar",pattern:/varchar/i,longer_alt:r}),Mt=c({name:"Char",pattern:/char/i,longer_alt:r}),xt=c({name:"Boolean",pattern:/boolean/i,longer_alt:r}),At=c({name:"Timestamptz",pattern:/timestamptz/i,longer_alt:r}),Dt=c({name:"Timestamp",pattern:/timestamp/i,longer_alt:r}),Rt=c({name:"Date",pattern:/date/i,longer_alt:r}),It=c({name:"Time",pattern:/time/i,longer_alt:r}),yt=c({name:"Jsonb",pattern:/jsonb/i,longer_alt:r}),vt=c({name:"Json",pattern:/json/i,longer_alt:r}),bt=c({name:"Uuid",pattern:/uuid/i,longer_alt:r}),kt=c({name:"Inet",pattern:/inet/i,longer_alt:r}),Bt=c({name:"Citext",pattern:/citext/i,longer_alt:r}),_t=c({name:"Decimal",pattern:/decimal/i,longer_alt:r}),Pt=c({name:"Numeric",pattern:/numeric/i,longer_alt:r}),qt=c({name:"Real",pattern:/real/i,longer_alt:r}),$t=c({name:"DoublePrecision",pattern:/double\s+precision/i,longer_alt:r}),Gt=c({name:"Bytea",pattern:/bytea/i,longer_alt:r}),wt=c({name:"StringLiteral",pattern:/'(?:[^'\\]|\\.)*'/}),P=c({name:"NumberLiteral",pattern:/-?\d+(?:\.\d+)?/}),Ft=c({name:"BooleanLiteral",pattern:/true|false/,longer_alt:r}),jt=c({name:"NullLiteral",pattern:/null/i,longer_alt:r}),Ht=c({name:"Arrow",pattern:/->/}),zt=c({name:"DoubleColon",pattern:/::/}),M=c({name:"LBrace",pattern:/\{/}),x=c({name:"RBrace",pattern:/\}/}),C=c({name:"LParen",pattern:/\(/}),U=c({name:"RParen",pattern:/\)/}),Yt=c({name:"LBracket",pattern:/\[/}),Qt=c({name:"RBracket",pattern:/\]/}),q=c({name:"LAngle",pattern:/</}),$=c({name:"RAngle",pattern:/>/}),L=c({name:"Semicolon",pattern:/;/}),O=c({name:"Comma",pattern:/,/}),G=c({name:"Dot",pattern:/\./}),w=c({name:"Equals",pattern:/=/}),Kt=[fe,Ee,Se,Ht,zt,j,z,Y,Q,H,K,V,Z,R,I,J,W,X,tt,et,nt,st,it,T,y,v,b,k,rt,ot,at,ct,mt,pt,lt,ht,ut,ft,Et,St,Ct,B,_,$t,At,Dt,Nt,Ut,Ot,gt,Lt,Tt,Mt,dt,xt,Rt,It,yt,vt,bt,kt,Bt,_t,Pt,qt,Gt,wt,P,Ft,jt,D,r,M,x,C,U,Yt,Qt,q,$,L,O,G,w],Ce=new F(Kt,{ensureOptimizations:!0});function ne(s){return Ce.tokenize(s)}var Jt=class extends Ue{constructor(){super(Kt,{recoveryEnabled:!0,maxLookahead:3}),this.performSelfAnalysis()}anyIdentifier=this.RULE("anyIdentifier",()=>{this.OR([{ALT:()=>this.CONSUME(r)},{ALT:()=>this.CONSUME(j)},{ALT:()=>this.CONSUME(z)},{ALT:()=>this.CONSUME(Y)},{ALT:()=>this.CONSUME(Q)},{ALT:()=>this.CONSUME(H)},{ALT:()=>this.CONSUME(V)},{ALT:()=>this.CONSUME(Z)},{ALT:()=>this.CONSUME(R)},{ALT:()=>this.CONSUME(I)},{ALT:()=>this.CONSUME(J)},{ALT:()=>this.CONSUME(W)},{ALT:()=>this.CONSUME(tt)},{ALT:()=>this.CONSUME(et)},{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(st)},{ALT:()=>this.CONSUME(it)},{ALT:()=>this.CONSUME(K)},{ALT:()=>this.CONSUME(Ct)},{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(ft)},{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)},{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(rt)},{ALT:()=>this.CONSUME(ot)},{ALT:()=>this.CONSUME(at)},{ALT:()=>this.CONSUME(ct)},{ALT:()=>this.CONSUME(X)},{ALT:()=>this.CONSUME(lt)},{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)},{ALT:()=>this.CONSUME(ut)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(Ut)},{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME(Pt)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(Gt)}])});program=this.RULE("program",()=>{this.MANY(()=>{this.SUBRULE(this.topLevelDeclaration)})});topLevelDeclaration=this.RULE("topLevelDeclaration",()=>{this.OR([{ALT:()=>this.SUBRULE(this.extensionDecl)},{ALT:()=>this.SUBRULE(this.functionDecl)},{ALT:()=>this.SUBRULE(this.conceptDecl)},{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)},{ALT:()=>this.SUBRULE(this.instantiation)},{ALT:()=>this.SUBRULE(this.perInstanceIndex)}])});extensionDecl=this.RULE("extensionDecl",()=>{this.CONSUME(H),this.SUBRULE(this.anyIdentifier),this.CONSUME(L)});functionDecl=this.RULE("functionDecl",()=>{this.CONSUME(V),this.SUBRULE(this.anyIdentifier),this.CONSUME(C),this.CONSUME(U),this.CONSUME(Ht),this.SUBRULE2(this.anyIdentifier),this.SUBRULE(this.functionBody)});functionBody=this.RULE("functionBody",()=>{this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.functionStatement)}),this.CONSUME(x)});functionStatement=this.RULE("functionStatement",()=>{this.OR([{ALT:()=>this.SUBRULE(this.returnStatement)},{ALT:()=>this.SUBRULE(this.assignmentStatement)}])});returnStatement=this.RULE("returnStatement",()=>{this.CONSUME(Ct),this.OR([{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(r)}]),this.CONSUME(L)});assignmentStatement=this.RULE("assignmentStatement",()=>{this.OR([{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(r)}]),this.CONSUME(G),this.SUBRULE(this.anyIdentifier),this.CONSUME(w),this.SUBRULE(this.functionCallExpr),this.CONSUME(L)});functionCallExpr=this.RULE("functionCallExpr",()=>{this.CONSUME(r),this.CONSUME(C),this.CONSUME(U)});conceptDecl=this.RULE("conceptDecl",()=>{this.CONSUME(j),this.CONSUME(r),this.OPTION(()=>{this.SUBRULE(this.typeParamList)}),this.CONSUME(M),this.MANY(()=>{this.OR([{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)}])}),this.CONSUME(x)});typeParamList=this.RULE("typeParamList",()=>{this.CONSUME(q),this.CONSUME(r),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(r)}),this.CONSUME($)});enumDecl=this.RULE("enumDecl",()=>{this.CONSUME(Q),this.SUBRULE(this.anyIdentifier),this.CONSUME(M),this.MANY(()=>{this.SUBRULE2(this.anyIdentifier),this.CONSUME(L)}),this.CONSUME(x)});schemaDecl=this.RULE("schemaDecl",()=>{this.CONSUME(z),this.CONSUME(r),this.OPTION(()=>{this.SUBRULE(this.mixinList)}),this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.schemaBodyItem)}),this.CONSUME(x)});mixinList=this.RULE("mixinList",()=>{this.CONSUME(Y),this.CONSUME(r),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(r)})});schemaBodyItem=this.RULE("schemaBodyItem",()=>{this.OR([{ALT:()=>this.SUBRULE(this.indexDef)},{ALT:()=>this.SUBRULE(this.checkDef)},{ALT:()=>this.SUBRULE(this.triggerDef)},{ALT:()=>this.SUBRULE(this.columnDef)}])});columnDef=this.RULE("columnDef",()=>{this.SUBRULE(this.columnName),this.SUBRULE(this.dataType),this.MANY(()=>{this.SUBRULE(this.columnConstraint)}),this.CONSUME(L)});columnName=this.RULE("columnName",()=>{this.OR([{ALT:()=>{this.CONSUME(D),this.OPTION(()=>{this.CONSUME(r)})}},{ALT:()=>this.CONSUME2(r)}])});dataType=this.RULE("dataType",()=>{this.OR([{ALT:()=>this.CONSUME(Ut)},{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME(Pt)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(Gt)},{ALT:()=>this.CONSUME(r)}]),this.OPTION(()=>{this.CONSUME(C),this.CONSUME(P),this.CONSUME(U)})});columnConstraint=this.RULE("columnConstraint",()=>{this.OR([{ALT:()=>this.CONSUME(rt)},{ALT:()=>this.CONSUME(ot)},{ALT:()=>this.CONSUME(T)},{ALT:()=>this.SUBRULE(this.defaultConstraint)},{ALT:()=>this.SUBRULE(this.refConstraint)},{ALT:()=>this.SUBRULE(this.checkConstraint)},{ALT:()=>this.SUBRULE(this.onDeleteConstraint)}])});defaultConstraint=this.RULE("defaultConstraint",()=>{this.CONSUME(at),this.CONSUME(C),this.SUBRULE(this.defaultValue),this.CONSUME(U)});defaultValue=this.RULE("defaultValue",()=>{this.OR([{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(P)},{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.CONSUME(jt)},{ALT:()=>this.SUBRULE(this.functionCallExpr)},{ALT:()=>{this.SUBRULE(this.anyIdentifier),this.CONSUME(zt),this.SUBRULE2(this.anyIdentifier)}},{ALT:()=>this.SUBRULE3(this.anyIdentifier)}])});refConstraint=this.RULE("refConstraint",()=>{this.CONSUME(ct),this.CONSUME(C),this.CONSUME(r),this.CONSUME(G),this.CONSUME2(r),this.CONSUME(U)});checkConstraint=this.RULE("checkConstraint",()=>{this.CONSUME(I),this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U)});checkExpression=this.RULE("checkExpression",()=>{this.MANY(()=>{this.OR([{ALT:()=>{this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U)}},{ALT:()=>this.CONSUME(r)},{ALT:()=>this.CONSUME(D)},{ALT:()=>this.CONSUME(P)},{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.CONSUME(jt)},{ALT:()=>this.CONSUME(zt)},{ALT:()=>this.CONSUME(w)},{ALT:()=>this.CONSUME(O)},{ALT:()=>this.CONSUME(G)},{ALT:()=>this.CONSUME(q)},{ALT:()=>this.CONSUME($)}])})});onDeleteConstraint=this.RULE("onDeleteConstraint",()=>{this.CONSUME(X),this.CONSUME(C),this.OR([{ALT:()=>this.CONSUME(lt)},{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)},{ALT:()=>this.CONSUME(ut)}]),this.CONSUME(U)});indexDef=this.RULE("indexDef",()=>{this.CONSUME(R),this.CONSUME(C),this.SUBRULE(this.indexColumnList),this.CONSUME(U),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(k)}])}),this.CONSUME(L)});indexColumnList=this.RULE("indexColumnList",()=>{this.SUBRULE(this.indexColumn),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.indexColumn)})});indexColumn=this.RULE("indexColumn",()=>{this.OR([{ALT:()=>this.CONSUME(D)},{ALT:()=>this.CONSUME(r)}]),this.OPTION(()=>{this.CONSUME2(r)})});checkDef=this.RULE("checkDef",()=>{this.CONSUME(I),this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U),this.CONSUME(L)});triggerDef=this.RULE("triggerDef",()=>{this.CONSUME(Z),this.CONSUME(r),this.OR([{ALT:()=>this.CONSUME(J)},{ALT:()=>this.CONSUME(W)}]),this.OR2([{ALT:()=>this.CONSUME(ft)},{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)}]),this.CONSUME(tt),this.CONSUME(et),this.OR3([{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(st)}]),this.CONSUME(it),this.CONSUME(K),this.CONSUME2(r),this.CONSUME(C),this.CONSUME(U),this.CONSUME(L)});instantiation=this.RULE("instantiation",()=>{this.SUBRULE(this.instantiationTargetList),this.CONSUME(w),this.SUBRULE(this.conceptReference),this.CONSUME(L)});instantiationTargetList=this.RULE("instantiationTargetList",()=>{this.SUBRULE(this.instantiationTarget),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.instantiationTarget)})});instantiationTarget=this.RULE("instantiationTarget",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(Yt),this.CONSUME2(r),this.CONSUME(Qt)})});conceptReference=this.RULE("conceptReference",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(q),this.SUBRULE(this.typeArgList),this.CONSUME($)})});typeArgList=this.RULE("typeArgList",()=>{this.SUBRULE(this.typeArg),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.typeArg)})});typeArg=this.RULE("typeArg",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(Yt),this.CONSUME2(r),this.CONSUME(Qt)})});perInstanceIndex=this.RULE("perInstanceIndex",()=>{this.CONSUME(R),this.CONSUME(C),this.CONSUME(r),this.CONSUME(O),this.CONSUME2(r),this.MANY(()=>{this.CONSUME2(O),this.CONSUME3(r)}),this.CONSUME(U),this.MANY2(()=>{this.OR([{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(k)}])}),this.CONSUME(L)})},Vt=new Jt;function E(s,t){let e=s.children[t];if(e&&e.length>0){let i=e[0];if(i&&"image"in i)return i.image}return""}function d(s,t){let e=s.children[t];return e?e.map(i=>"image"in i?i.image:"").filter(i=>i!==""):[]}function m(s,t){let i=s.children[t]?.[0];if(i&&"children"in i)return i}function g(s,t){let e=s.children[t];return e?e.filter(i=>"children"in i):[]}function se(s){let e=g(s,"anyIdentifier")[0];if(e)for(let i of Object.keys(e.children)){let l=e.children[i]?.[0];if(l&&"image"in l)return l.image}return""}function Zt(s){let t=g(s,"anyIdentifier"),e=[];for(let i of t)for(let a of Object.keys(i.children)){let n=i.children[a]?.[0];if(n&&"image"in n){e.push(n.image);break}}return e}var Wt=class{visit(t){let e=[],i=g(t,"topLevelDeclaration");for(let a of i){let l=this.visitTopLevelDeclaration(a);l&&e.push(l)}return{type:"Program",declarations:e}}visitTopLevelDeclaration(t){let e=m(t,"extensionDecl");if(e)return this.visitExtensionDecl(e);let i=m(t,"functionDecl");if(i)return this.visitFunctionDecl(i);let a=m(t,"conceptDecl");if(a)return this.visitConceptDecl(a);let l=m(t,"enumDecl");if(l)return this.visitEnumDecl(l);let n=m(t,"schemaDecl");if(n)return this.visitSchemaDecl(n);let o=m(t,"instantiation");if(o)return this.visitInstantiation(o);let h=m(t,"perInstanceIndex");return h?this.visitPerInstanceIndex(h):null}visitExtensionDecl(t){return{type:"ExtensionDecl",name:se(t)}}visitFunctionDecl(t){let e=Zt(t),i=e[0]??"",a=e[1]??"",l=m(t,"functionBody"),n="";if(l){let o=g(l,"functionStatement"),h=[];for(let p of o){let u=m(p,"returnStatement");if(u){let f=E(u,"Identifier");f||(f=E(u,"New")),f||(f=E(u,"Old")),h.push(`RETURN ${f};`)}let S=m(p,"assignmentStatement");if(S){let f=E(S,"Identifier");f||(f=E(S,"New")),f||(f=E(S,"Old"));let A=se(S),N=m(S,"functionCallExpr"),ue=N?E(N,"Identifier"):"";h.push(`${f}.${A} := ${ue}();`)}}n=h.join(`
2
- `)}return{type:"FunctionDecl",name:i,returnType:a,body:n}}visitConceptDecl(t){let e=E(t,"Identifier"),i=[],a=m(t,"typeParamList");a&&i.push(...d(a,"Identifier"));let l=[],n=g(t,"schemaDecl");for(let h of n){let p=this.visitSchemaDecl(h);p.conceptScope=e,l.push(p)}let o=g(t,"enumDecl");for(let h of o){let p=this.visitEnumDecl(h);p.conceptScope=e,l.push(p)}return{type:"ConceptDecl",name:e,typeParams:i,members:l}}visitEnumDecl(t){let e=Zt(t),i=e[0]??"",a=e.slice(1);return{type:"EnumDecl",name:i,values:a}}visitSchemaDecl(t){let e=E(t,"Identifier"),i=[],a=m(t,"mixinList");a&&i.push(...d(a,"Identifier"));let l=[],n=g(t,"schemaBodyItem");for(let o of n){let h=this.visitSchemaBodyItem(o);h&&l.push(h)}return{type:"SchemaDecl",name:e,mixins:i,members:l}}visitSchemaBodyItem(t){let e=m(t,"columnDef");if(e)return this.visitColumnDef(e);let i=m(t,"indexDef");if(i)return this.visitIndexDef(i);let a=m(t,"checkDef");if(a)return this.visitCheckDef(a);let l=m(t,"triggerDef");return l?this.visitTriggerDef(l):null}visitColumnDef(t){let e=m(t,"columnName"),i="";if(e){let h=E(e,"TemplateIdentifier"),p=E(e,"Identifier");i=h?h+p:p}let a=m(t,"dataType"),l="";if(a){for(let u of Object.keys(a.children)){let f=a.children[u]?.[0];if(f&&"image"in f){l=f.image;break}}let p=a.children.NumberLiteral?.[0];p&&"image"in p&&(l+=`(${p.image})`)}let n=[],o=g(t,"columnConstraint");for(let h of o){let p=this.visitColumnConstraint(h);p&&n.push(p)}return{type:"ColumnDef",name:i,dataType:l,constraints:n}}visitColumnConstraint(t){if(t.children.Pkey)return{type:"PrimaryKey"};if(t.children.Nonull)return{type:"NotNull"};if(t.children.Unique)return{type:"Unique"};let e=m(t,"defaultConstraint");if(e){let n=m(e,"defaultValue"),o="";if(n){let h=E(n,"StringLiteral"),p=E(n,"NumberLiteral"),u=E(n,"BooleanLiteral"),S=E(n,"NullLiteral"),f=m(n,"functionCallExpr"),A=E(n,"DoubleColon"),N=Zt(n);h?o=h:p?o=p:u?o=u:S?o=S:f?o=E(f,"Identifier")+"()":A&&N.length===2&&N[0]&&N[1]?o=`${N[0]}::${N[1]}`:N.length>0&&N[0]&&(o=N[0])}return{type:"Default",value:o}}let i=m(t,"refConstraint");if(i){let n=d(i,"Identifier");return{type:"Reference",table:n[0]??"",column:n[1]??""}}let a=m(t,"checkConstraint");if(a){let n=m(a,"checkExpression");return{type:"Check",value:n?this.reconstructCheckExpression(n):""}}let l=m(t,"onDeleteConstraint");if(l){let n={Cascade:"CASCADE",Restrict:"RESTRICT",SetNull:"SET NULL",SetDefault:"SET DEFAULT",NoAction:"NO ACTION"};for(let[o,h]of Object.entries(n))if(l.children[o])return{type:"OnDelete",action:h}}return null}reconstructCheckExpression(t){let e=[];for(let a of Object.keys(t.children)){let l=t.children[a];if(l){for(let n of l)if("image"in n&&"startOffset"in n){let o=typeof n.startOffset=="number"?n.startOffset:0;e.push({image:n.image,startOffset:o,isParenExpr:!1})}else if("children"in n&&a==="checkExpression"){let o=this.reconstructCheckExpression(n);if(o){let h=1/0;for(let p of Object.keys(n.children)){let u=n.children[p];if(u)for(let S of u)"startOffset"in S&&typeof S.startOffset=="number"&&(h=Math.min(h,S.startOffset))}e.push({image:o,startOffset:h===1/0?0:h,isParenExpr:!1})}}}}e.sort((a,l)=>a.startOffset-l.startOffset);let i="";for(let a=0;a<e.length;a++){let l=e[a],n=a>0?e[a-1]:null;if(!l)continue;let o=l.image,h=[")",",",".","::",">","<"],p=["(",".","::"];if(n&&!l.isParenExpr){let u=n.image,S=u[u.length-1]??"",f=(u===">"||u==="<")&&o==="=",A=(o===">"||o==="<")&&!p.includes(u)&&S!=="(";(!h.includes(o)&&!p.includes(u)&&S!=="("&&!f||A)&&(i+=" ")}else if(n&&l.isParenExpr){let u=n.image;(u[u.length-1]??"")!=="("&&(i+=" ")}i+=o}return i}visitIndexDef(t){let e=[],i=m(t,"indexColumnList");if(i){let n=g(i,"indexColumn");for(let o of n){let h=E(o,"TemplateIdentifier"),p=E(o,"Identifier");e.push(h?h+p:p)}}let a=t.children.Unique!==void 0,l;return t.children.Gin&&(l="gin"),t.children.Gist&&(l="gist"),t.children.Btree&&(l="btree"),t.children.Hash&&(l="hash"),{type:"IndexDef",columns:e,unique:a,using:l}}visitCheckDef(t){let e=m(t,"checkExpression");return{type:"CheckDef",expression:e?this.reconstructCheckExpression(e):""}}visitTriggerDef(t){let e=d(t,"Identifier"),i=e[0]??"",a=e[1]??"",l=t.children.Before?"before":"after",n="update";t.children.Insert&&(n="insert"),t.children.Delete&&(n="delete");let o=t.children.Row?"row":"statement";return{type:"TriggerDef",name:i,timing:l,event:n,forEach:o,executeFunction:a}}visitInstantiation(t){let e=[],i=m(t,"instantiationTargetList");if(i){let o=g(i,"instantiationTarget");for(let h of o){let p=d(h,"Identifier");e.push({tableName:p[0]??"",alias:p[1]})}}let a=m(t,"conceptReference"),l="",n=[];if(a){l=E(a,"Identifier");let o=m(a,"typeArgList");if(o){let h=g(o,"typeArg");for(let p of h){let u=d(p,"Identifier");n.push({tableName:u[0]??"",alias:u[1]})}}}return{type:"Instantiation",targets:e,conceptName:l,typeArgs:n}}visitPerInstanceIndex(t){let e=d(t,"Identifier"),i=e[0]??"",a=e.slice(1),l=t.children.Unique!==void 0,n;return t.children.Gin&&(n="gin"),t.children.Gist&&(n="gist"),t.children.Btree&&(n="btree"),t.children.Hash&&(n="hash"),{type:"PerInstanceIndex",tableName:i,columns:a,unique:l,using:n}}};function Xt(s){let t=ne(s),e=[];for(let n of t.errors)e.push({message:n.message,location:{start:{line:n.line??0,column:n.column??0,offset:n.offset},end:{line:n.line??0,column:n.column??0,offset:n.offset}},severity:"error"});if(e.length>0)return{ast:null,errors:e};Vt.input=t.tokens;let i=Vt.program();for(let n of Vt.errors){let o=n.token;e.push({message:n.message,location:{start:{line:o.startLine??0,column:o.startColumn??0,offset:o.startOffset},end:{line:o.endLine??0,column:o.endColumn??0,offset:o.endOffset??o.startOffset}},severity:"error"})}return e.length>0?{ast:null,errors:e}:{ast:new Wt().visit(i),errors:e}}function te(s){return s.replace(/([A-Z]+)([A-Z][a-z])/g,"$1_$2").replace(/([a-z\d])([A-Z])/g,"$1_$2").toLowerCase()}function oe(s){return s.includes("::")?s.replace(/(\w+)::(\w+)/g,"'$2'::$1"):s}function Ne(s,t){return s==="NULL"||/^'[^']*'::\w+$/.test(s)?s:s.includes("::")?oe(s):`'${s}'::${t}`}function Le(){return{concepts:new Map,schemas:new Map,enums:new Map,tableToSchema:new Map,templateSubs:new Map,enumSql:[],tableSql:[],indexSql:[],triggerSql:[],extensionSql:[],functionSql:[],perInstanceIndexSql:[],generatedEnums:new Set,generatedTables:new Set}}function ge(s,t){for(let e of s.declarations)switch(e.type){case"ConceptDecl":t.concepts.set(e.name,e);break;case"SchemaDecl":t.schemas.set(e.name,e);break;case"EnumDecl":t.enums.set(e.name,e);break}}function ae(s,t){let e=s.match(/^\{([^}]+)\}(.*)$/);if(e){let[,i,a]=e,l;for(let[n,o]of t.templateSubs)if(n===i){l=o;break}return(l??te(i??""))+(a??"")}return s}function Oe(s){return s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ce(s,t){let e=s;for(let[i,a]of t.templateSubs){let l=Oe(i),n=new RegExp(`\\{${l}\\}`,"g");e=e.replace(n,a)}return e=oe(e),e}function le(s,t){let e=s.name;if(t.generatedEnums.has(e))return;t.generatedEnums.add(e),t.enums.set(e,s);let i=s.values.map(a=>`'${a}'`).join(", ");t.enumSql.push(`DO $$ BEGIN
1
+ import{CstParser as Ne}from"chevrotain";import{createToken as c,Lexer as F}from"chevrotain";var Ee=c({name:"WhiteSpace",pattern:/\s+/,group:F.SKIPPED}),Se=c({name:"LineComment",pattern:/\/\/[^\n]*/,group:F.SKIPPED}),Ce=c({name:"BlockComment",pattern:/\/\*[\s\S]*?\*\//,group:F.SKIPPED}),o=c({name:"Identifier",pattern:/[a-zA-Z_][a-zA-Z0-9_]*/}),D=c({name:"TemplateIdentifier",pattern:/\{[a-zA-Z_][a-zA-Z0-9_]*\}/}),j=c({name:"Concept",pattern:/concept/,longer_alt:o}),z=c({name:"Schema",pattern:/schema/,longer_alt:o}),Y=c({name:"Mixin",pattern:/mixin/,longer_alt:o}),Q=c({name:"Enum",pattern:/enum/,longer_alt:o}),H=c({name:"Extension",pattern:/extension/,longer_alt:o}),W=c({name:"Function",pattern:/function/,longer_alt:o}),K=c({name:"Func",pattern:/func/,longer_alt:o}),V=c({name:"Trigger",pattern:/trigger/,longer_alt:o}),R=c({name:"Index",pattern:/index/,longer_alt:o}),I=c({name:"Check",pattern:/check/,longer_alt:o}),Z=c({name:"Before",pattern:/before/,longer_alt:o}),J=c({name:"After",pattern:/after/,longer_alt:o}),X=c({name:"Ondelete",pattern:/ondelete/,longer_alt:o}),tt=c({name:"On",pattern:/on/,longer_alt:o}),et=c({name:"Each",pattern:/each/,longer_alt:o}),nt=c({name:"Row",pattern:/row/,longer_alt:o}),st=c({name:"Statement",pattern:/statement/,longer_alt:o}),it=c({name:"Execute",pattern:/execute/,longer_alt:o}),rt=c({name:"Where",pattern:/where/,longer_alt:o}),T=c({name:"Unique",pattern:/unique/,longer_alt:o}),y=c({name:"Gin",pattern:/gin/,longer_alt:o}),v=c({name:"Gist",pattern:/gist/,longer_alt:o}),k=c({name:"Btree",pattern:/btree/,longer_alt:o}),b=c({name:"Hash",pattern:/hash/,longer_alt:o}),ot=c({name:"Pkey",pattern:/pkey/,longer_alt:o}),at=c({name:"Nonull",pattern:/nonull/,longer_alt:o}),ct=c({name:"Default",pattern:/default/,longer_alt:o}),lt=c({name:"Ref",pattern:/ref/,longer_alt:o}),ht=c({name:"Cascade",pattern:/cascade/,longer_alt:o}),pt=c({name:"Restrict",pattern:/restrict/,longer_alt:o}),mt=c({name:"SetNull",pattern:/setnull/,longer_alt:o}),ut=c({name:"SetDefault",pattern:/setdefault/,longer_alt:o}),ft=c({name:"NoAction",pattern:/noaction/,longer_alt:o}),Et=c({name:"Update",pattern:/update/,longer_alt:o}),St=c({name:"Insert",pattern:/insert/,longer_alt:o}),Ct=c({name:"Delete",pattern:/delete/,longer_alt:o}),Ut=c({name:"Return",pattern:/return/,longer_alt:o}),B=c({name:"New",pattern:/NEW/,longer_alt:o}),_=c({name:"Old",pattern:/OLD/,longer_alt:o}),Nt=c({name:"Serial",pattern:/serial/i,longer_alt:o}),Lt=c({name:"BigSerial",pattern:/bigserial/i,longer_alt:o}),gt=c({name:"Integer",pattern:/integer/i,longer_alt:o}),Ot=c({name:"Bigint",pattern:/bigint/i,longer_alt:o}),dt=c({name:"SmallInt",pattern:/smallint/i,longer_alt:o}),Tt=c({name:"Text",pattern:/text/i,longer_alt:o}),Mt=c({name:"Varchar",pattern:/varchar/i,longer_alt:o}),xt=c({name:"Char",pattern:/char/i,longer_alt:o}),At=c({name:"Boolean",pattern:/boolean/i,longer_alt:o}),Dt=c({name:"Timestamptz",pattern:/timestamptz/i,longer_alt:o}),Rt=c({name:"Timestamp",pattern:/timestamp/i,longer_alt:o}),It=c({name:"Date",pattern:/date/i,longer_alt:o}),yt=c({name:"Time",pattern:/time/i,longer_alt:o}),vt=c({name:"Jsonb",pattern:/jsonb/i,longer_alt:o}),kt=c({name:"Json",pattern:/json/i,longer_alt:o}),bt=c({name:"Uuid",pattern:/uuid/i,longer_alt:o}),Bt=c({name:"Inet",pattern:/inet/i,longer_alt:o}),_t=c({name:"Citext",pattern:/citext/i,longer_alt:o}),Pt=c({name:"Decimal",pattern:/decimal/i,longer_alt:o}),$t=c({name:"Numeric",pattern:/numeric/i,longer_alt:o}),qt=c({name:"Real",pattern:/real/i,longer_alt:o}),wt=c({name:"DoublePrecision",pattern:/double\s+precision/i,longer_alt:o}),Gt=c({name:"Bytea",pattern:/bytea/i,longer_alt:o}),Ft=c({name:"StringLiteral",pattern:/'(?:[^'\\]|\\.)*'/}),P=c({name:"NumberLiteral",pattern:/-?\d+(?:\.\d+)?/}),jt=c({name:"BooleanLiteral",pattern:/true|false/,longer_alt:o}),zt=c({name:"NullLiteral",pattern:/null/i,longer_alt:o}),Wt=c({name:"Arrow",pattern:/->/}),Yt=c({name:"DoubleColon",pattern:/::/}),M=c({name:"LBrace",pattern:/\{/}),x=c({name:"RBrace",pattern:/\}/}),C=c({name:"LParen",pattern:/\(/}),U=c({name:"RParen",pattern:/\)/}),Qt=c({name:"LBracket",pattern:/\[/}),Ht=c({name:"RBracket",pattern:/\]/}),$=c({name:"LAngle",pattern:/</}),q=c({name:"RAngle",pattern:/>/}),L=c({name:"Semicolon",pattern:/;/}),O=c({name:"Comma",pattern:/,/}),w=c({name:"Dot",pattern:/\./}),G=c({name:"Equals",pattern:/=/}),Kt=[Ee,Se,Ce,Wt,Yt,j,z,Y,Q,H,W,K,V,R,I,Z,J,X,tt,et,nt,st,it,rt,T,y,v,k,b,ot,at,ct,lt,ut,mt,ht,pt,ft,Et,St,Ct,Ut,B,_,wt,Dt,Rt,Lt,Nt,dt,Ot,gt,Mt,xt,Tt,At,It,yt,vt,kt,bt,Bt,_t,Pt,$t,qt,Gt,Ft,P,jt,zt,D,o,M,x,C,U,Qt,Ht,$,q,L,O,w,G],Ue=new F(Kt,{ensureOptimizations:!0});function ie(s){return Ue.tokenize(s)}var Jt=class extends Ne{constructor(){super(Kt,{recoveryEnabled:!0,maxLookahead:3}),this.performSelfAnalysis()}anyIdentifier=this.RULE("anyIdentifier",()=>{this.OR([{ALT:()=>this.CONSUME(o)},{ALT:()=>this.CONSUME(j)},{ALT:()=>this.CONSUME(z)},{ALT:()=>this.CONSUME(Y)},{ALT:()=>this.CONSUME(Q)},{ALT:()=>this.CONSUME(H)},{ALT:()=>this.CONSUME(K)},{ALT:()=>this.CONSUME(V)},{ALT:()=>this.CONSUME(R)},{ALT:()=>this.CONSUME(I)},{ALT:()=>this.CONSUME(Z)},{ALT:()=>this.CONSUME(J)},{ALT:()=>this.CONSUME(tt)},{ALT:()=>this.CONSUME(et)},{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(st)},{ALT:()=>this.CONSUME(it)},{ALT:()=>this.CONSUME(W)},{ALT:()=>this.CONSUME(Ut)},{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)},{ALT:()=>this.CONSUME(Ct)},{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(ot)},{ALT:()=>this.CONSUME(at)},{ALT:()=>this.CONSUME(ct)},{ALT:()=>this.CONSUME(lt)},{ALT:()=>this.CONSUME(X)},{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)},{ALT:()=>this.CONSUME(ut)},{ALT:()=>this.CONSUME(ft)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(rt)},{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME(Pt)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(Gt)}])});program=this.RULE("program",()=>{this.MANY(()=>{this.SUBRULE(this.topLevelDeclaration)})});topLevelDeclaration=this.RULE("topLevelDeclaration",()=>{this.OR([{ALT:()=>this.SUBRULE(this.extensionDecl)},{ALT:()=>this.SUBRULE(this.functionDecl)},{ALT:()=>this.SUBRULE(this.conceptDecl)},{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)},{ALT:()=>this.SUBRULE(this.instantiation)},{ALT:()=>this.SUBRULE(this.perInstanceIndex)}])});extensionDecl=this.RULE("extensionDecl",()=>{this.CONSUME(H),this.SUBRULE(this.anyIdentifier),this.CONSUME(L)});functionDecl=this.RULE("functionDecl",()=>{this.CONSUME(K),this.SUBRULE(this.anyIdentifier),this.CONSUME(C),this.CONSUME(U),this.CONSUME(Wt),this.SUBRULE2(this.anyIdentifier),this.SUBRULE(this.functionBody)});functionBody=this.RULE("functionBody",()=>{this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.functionStatement)}),this.CONSUME(x)});functionStatement=this.RULE("functionStatement",()=>{this.OR([{ALT:()=>this.SUBRULE(this.returnStatement)},{ALT:()=>this.SUBRULE(this.assignmentStatement)}])});returnStatement=this.RULE("returnStatement",()=>{this.CONSUME(Ut),this.OR([{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(o)}]),this.CONSUME(L)});assignmentStatement=this.RULE("assignmentStatement",()=>{this.OR([{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(o)}]),this.CONSUME(w),this.SUBRULE(this.anyIdentifier),this.CONSUME(G),this.SUBRULE(this.functionCallExpr),this.CONSUME(L)});functionCallExpr=this.RULE("functionCallExpr",()=>{this.CONSUME(o),this.CONSUME(C),this.CONSUME(U)});conceptDecl=this.RULE("conceptDecl",()=>{this.CONSUME(j),this.CONSUME(o),this.OPTION(()=>{this.SUBRULE(this.typeParamList)}),this.CONSUME(M),this.MANY(()=>{this.OR([{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)}])}),this.CONSUME(x)});typeParamList=this.RULE("typeParamList",()=>{this.CONSUME($),this.CONSUME(o),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(o)}),this.CONSUME(q)});enumDecl=this.RULE("enumDecl",()=>{this.CONSUME(Q),this.SUBRULE(this.anyIdentifier),this.CONSUME(M),this.MANY(()=>{this.SUBRULE2(this.anyIdentifier),this.CONSUME(L)}),this.CONSUME(x)});schemaDecl=this.RULE("schemaDecl",()=>{this.CONSUME(z),this.CONSUME(o),this.OPTION(()=>{this.SUBRULE(this.mixinList)}),this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.schemaBodyItem)}),this.CONSUME(x)});mixinList=this.RULE("mixinList",()=>{this.CONSUME(Y),this.CONSUME(o),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(o)})});schemaBodyItem=this.RULE("schemaBodyItem",()=>{this.OR([{ALT:()=>this.SUBRULE(this.indexDef)},{ALT:()=>this.SUBRULE(this.checkDef)},{ALT:()=>this.SUBRULE(this.triggerDef)},{ALT:()=>this.SUBRULE(this.columnDef)}])});columnDef=this.RULE("columnDef",()=>{this.SUBRULE(this.columnName),this.SUBRULE(this.dataType),this.MANY(()=>{this.SUBRULE(this.columnConstraint)}),this.CONSUME(L)});columnName=this.RULE("columnName",()=>{this.OR([{ALT:()=>{this.CONSUME(D),this.OPTION(()=>{this.CONSUME(o)})}},{ALT:()=>this.CONSUME2(o)}])});dataType=this.RULE("dataType",()=>{this.OR([{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME(Pt)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(Gt)},{ALT:()=>this.CONSUME(o)}]),this.OPTION(()=>{this.CONSUME(C),this.CONSUME(P),this.CONSUME(U)})});columnConstraint=this.RULE("columnConstraint",()=>{this.OR([{ALT:()=>this.CONSUME(ot)},{ALT:()=>this.CONSUME(at)},{ALT:()=>this.CONSUME(T)},{ALT:()=>this.SUBRULE(this.defaultConstraint)},{ALT:()=>this.SUBRULE(this.refConstraint)},{ALT:()=>this.SUBRULE(this.checkConstraint)},{ALT:()=>this.SUBRULE(this.onDeleteConstraint)}])});defaultConstraint=this.RULE("defaultConstraint",()=>{this.CONSUME(ct),this.CONSUME(C),this.SUBRULE(this.defaultValue),this.CONSUME(U)});defaultValue=this.RULE("defaultValue",()=>{this.OR([{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.CONSUME(P)},{ALT:()=>this.CONSUME(jt)},{ALT:()=>this.CONSUME(zt)},{ALT:()=>this.SUBRULE(this.functionCallExpr)},{ALT:()=>{this.SUBRULE(this.anyIdentifier),this.CONSUME(Yt),this.SUBRULE2(this.anyIdentifier)}},{ALT:()=>this.SUBRULE3(this.anyIdentifier)}])});refConstraint=this.RULE("refConstraint",()=>{this.CONSUME(lt),this.CONSUME(C),this.CONSUME(o),this.CONSUME(w),this.CONSUME2(o),this.CONSUME(U)});checkConstraint=this.RULE("checkConstraint",()=>{this.CONSUME(I),this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U)});checkExpression=this.RULE("checkExpression",()=>{this.MANY(()=>{this.OR([{ALT:()=>{this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U)}},{ALT:()=>this.CONSUME(o)},{ALT:()=>this.CONSUME(D)},{ALT:()=>this.CONSUME(P)},{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.CONSUME(jt)},{ALT:()=>this.CONSUME(zt)},{ALT:()=>this.CONSUME(Yt)},{ALT:()=>this.CONSUME(G)},{ALT:()=>this.CONSUME(O)},{ALT:()=>this.CONSUME(w)},{ALT:()=>this.CONSUME($)},{ALT:()=>this.CONSUME(q)}])})});onDeleteConstraint=this.RULE("onDeleteConstraint",()=>{this.CONSUME(X),this.CONSUME(C),this.OR([{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)},{ALT:()=>this.CONSUME(ut)},{ALT:()=>this.CONSUME(ft)}]),this.CONSUME(U)});indexDef=this.RULE("indexDef",()=>{this.CONSUME(R),this.CONSUME(C),this.SUBRULE(this.indexColumnList),this.CONSUME(U),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(b)}])}),this.OPTION(()=>{this.SUBRULE(this.whereClause)}),this.CONSUME(L)});whereClause=this.RULE("whereClause",()=>{this.CONSUME(rt),this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U)});indexColumnList=this.RULE("indexColumnList",()=>{this.SUBRULE(this.indexColumn),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.indexColumn)})});indexColumn=this.RULE("indexColumn",()=>{this.OR([{ALT:()=>this.CONSUME(D)},{ALT:()=>this.CONSUME(o)}]),this.OPTION(()=>{this.CONSUME2(o)})});checkDef=this.RULE("checkDef",()=>{this.CONSUME(I),this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U),this.CONSUME(L)});triggerDef=this.RULE("triggerDef",()=>{this.CONSUME(V),this.CONSUME(o),this.OR([{ALT:()=>this.CONSUME(Z)},{ALT:()=>this.CONSUME(J)}]),this.OR2([{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)},{ALT:()=>this.CONSUME(Ct)}]),this.CONSUME(tt),this.CONSUME(et),this.OR3([{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(st)}]),this.CONSUME(it),this.CONSUME(W),this.CONSUME2(o),this.CONSUME(C),this.CONSUME(U),this.CONSUME(L)});instantiation=this.RULE("instantiation",()=>{this.SUBRULE(this.instantiationTargetList),this.CONSUME(G),this.SUBRULE(this.conceptReference),this.CONSUME(L)});instantiationTargetList=this.RULE("instantiationTargetList",()=>{this.SUBRULE(this.instantiationTarget),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.instantiationTarget)})});instantiationTarget=this.RULE("instantiationTarget",()=>{this.CONSUME(o),this.OPTION(()=>{this.CONSUME(Qt),this.CONSUME2(o),this.CONSUME(Ht)})});conceptReference=this.RULE("conceptReference",()=>{this.CONSUME(o),this.OPTION(()=>{this.CONSUME($),this.SUBRULE(this.typeArgList),this.CONSUME(q)})});typeArgList=this.RULE("typeArgList",()=>{this.SUBRULE(this.typeArg),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.typeArg)})});typeArg=this.RULE("typeArg",()=>{this.CONSUME(o),this.OPTION(()=>{this.CONSUME(Qt),this.CONSUME2(o),this.CONSUME(Ht)})});perInstanceIndex=this.RULE("perInstanceIndex",()=>{this.CONSUME(R),this.CONSUME(C),this.CONSUME(o),this.CONSUME(O),this.CONSUME2(o),this.MANY(()=>{this.CONSUME2(O),this.CONSUME3(o)}),this.CONSUME(U),this.MANY2(()=>{this.OR([{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(b)}])}),this.OPTION(()=>{this.SUBRULE(this.whereClause)}),this.CONSUME(L)})},Vt=new Jt;function S(s,t){let e=s.children[t];if(e&&e.length>0){let i=e[0];if(i&&"image"in i)return i.image}return""}function d(s,t){let e=s.children[t];return e?e.map(i=>"image"in i?i.image:"").filter(i=>i!==""):[]}function m(s,t){let i=s.children[t]?.[0];if(i&&"children"in i)return i}function g(s,t){let e=s.children[t];return e?e.filter(i=>"children"in i):[]}function re(s){let e=g(s,"anyIdentifier")[0];if(e)for(let i of Object.keys(e.children)){let l=e.children[i]?.[0];if(l&&"image"in l)return l.image}return""}function Zt(s){let t=g(s,"anyIdentifier"),e=[];for(let i of t)for(let a of Object.keys(i.children)){let n=i.children[a]?.[0];if(n&&"image"in n){e.push(n.image);break}}return e}var Xt=class{visit(t){let e=[],i=g(t,"topLevelDeclaration");for(let a of i){let l=this.visitTopLevelDeclaration(a);l&&e.push(l)}return{type:"Program",declarations:e}}visitTopLevelDeclaration(t){let e=m(t,"extensionDecl");if(e)return this.visitExtensionDecl(e);let i=m(t,"functionDecl");if(i)return this.visitFunctionDecl(i);let a=m(t,"conceptDecl");if(a)return this.visitConceptDecl(a);let l=m(t,"enumDecl");if(l)return this.visitEnumDecl(l);let n=m(t,"schemaDecl");if(n)return this.visitSchemaDecl(n);let r=m(t,"instantiation");if(r)return this.visitInstantiation(r);let h=m(t,"perInstanceIndex");return h?this.visitPerInstanceIndex(h):null}visitExtensionDecl(t){return{type:"ExtensionDecl",name:re(t)}}visitFunctionDecl(t){let e=Zt(t),i=e[0]??"",a=e[1]??"",l=m(t,"functionBody"),n="";if(l){let r=g(l,"functionStatement"),h=[];for(let p of r){let u=m(p,"returnStatement");if(u){let E=S(u,"Identifier");E||(E=S(u,"New")),E||(E=S(u,"Old")),h.push(`RETURN ${E};`)}let f=m(p,"assignmentStatement");if(f){let E=S(f,"Identifier");E||(E=S(f,"New")),E||(E=S(f,"Old"));let A=re(f),N=m(f,"functionCallExpr"),fe=N?S(N,"Identifier"):"";h.push(`${E}.${A} := ${fe}();`)}}n=h.join(`
2
+ `)}return{type:"FunctionDecl",name:i,returnType:a,body:n}}visitConceptDecl(t){let e=S(t,"Identifier"),i=[],a=m(t,"typeParamList");a&&i.push(...d(a,"Identifier"));let l=[],n=g(t,"schemaDecl");for(let h of n){let p=this.visitSchemaDecl(h);p.conceptScope=e,l.push(p)}let r=g(t,"enumDecl");for(let h of r){let p=this.visitEnumDecl(h);p.conceptScope=e,l.push(p)}return{type:"ConceptDecl",name:e,typeParams:i,members:l}}visitEnumDecl(t){let e=Zt(t),i=e[0]??"",a=e.slice(1);return{type:"EnumDecl",name:i,values:a}}visitSchemaDecl(t){let e=S(t,"Identifier"),i=[],a=m(t,"mixinList");a&&i.push(...d(a,"Identifier"));let l=[],n=g(t,"schemaBodyItem");for(let r of n){let h=this.visitSchemaBodyItem(r);h&&l.push(h)}return{type:"SchemaDecl",name:e,mixins:i,members:l}}visitSchemaBodyItem(t){let e=m(t,"columnDef");if(e)return this.visitColumnDef(e);let i=m(t,"indexDef");if(i)return this.visitIndexDef(i);let a=m(t,"checkDef");if(a)return this.visitCheckDef(a);let l=m(t,"triggerDef");return l?this.visitTriggerDef(l):null}visitColumnDef(t){let e=m(t,"columnName"),i="";if(e){let h=S(e,"TemplateIdentifier"),p=S(e,"Identifier");i=h?h+p:p}let a=m(t,"dataType"),l="";if(a){for(let u of Object.keys(a.children)){let E=a.children[u]?.[0];if(E&&"image"in E){l=E.image;break}}let p=a.children.NumberLiteral?.[0];p&&"image"in p&&(l+=`(${p.image})`)}let n=[],r=g(t,"columnConstraint");for(let h of r){let p=this.visitColumnConstraint(h);p&&n.push(p)}return{type:"ColumnDef",name:i,dataType:l,constraints:n}}visitColumnConstraint(t){if(t.children.Pkey)return{type:"PrimaryKey"};if(t.children.Nonull)return{type:"NotNull"};if(t.children.Unique)return{type:"Unique"};let e=m(t,"defaultConstraint");if(e){let n=m(e,"defaultValue"),r="";if(n){let h=S(n,"StringLiteral"),p=S(n,"NumberLiteral"),u=S(n,"BooleanLiteral"),f=S(n,"NullLiteral"),E=m(n,"functionCallExpr"),A=S(n,"DoubleColon"),N=Zt(n);h?r=h:p?r=p:u?r=u:f?r=f:E?r=S(E,"Identifier")+"()":A&&N.length===2&&N[0]&&N[1]?r=`${N[0]}::${N[1]}`:N.length>0&&N[0]&&(r=N[0])}return{type:"Default",value:r}}let i=m(t,"refConstraint");if(i){let n=d(i,"Identifier");return{type:"Reference",table:n[0]??"",column:n[1]??""}}let a=m(t,"checkConstraint");if(a){let n=m(a,"checkExpression");return{type:"Check",value:n?this.reconstructCheckExpression(n):""}}let l=m(t,"onDeleteConstraint");if(l){let n={Cascade:"CASCADE",Restrict:"RESTRICT",SetNull:"SET NULL",SetDefault:"SET DEFAULT",NoAction:"NO ACTION"};for(let[r,h]of Object.entries(n))if(l.children[r])return{type:"OnDelete",action:h}}return null}reconstructCheckExpression(t){let e=[];for(let a of Object.keys(t.children)){let l=t.children[a];if(l){for(let n of l)if("image"in n&&"startOffset"in n){let r=typeof n.startOffset=="number"?n.startOffset:0;e.push({image:n.image,startOffset:r,isParenExpr:!1})}else if("children"in n&&a==="checkExpression"){let r=this.reconstructCheckExpression(n);if(r){let h=1/0;for(let p of Object.keys(n.children)){let u=n.children[p];if(u)for(let f of u)"startOffset"in f&&typeof f.startOffset=="number"&&(h=Math.min(h,f.startOffset))}e.push({image:r,startOffset:h===1/0?0:h,isParenExpr:!1})}}}}e.sort((a,l)=>a.startOffset-l.startOffset);let i="";for(let a=0;a<e.length;a++){let l=e[a],n=a>0?e[a-1]:null;if(!l)continue;let r=l.image,h=[")",",",".","::",">","<"],p=["(",".","::"];if(n&&!l.isParenExpr){let u=n.image,f=u[u.length-1]??"",E=(u===">"||u==="<")&&r==="=",A=(r===">"||r==="<")&&!p.includes(u)&&f!=="(";(!h.includes(r)&&!p.includes(u)&&f!=="("&&!E||A)&&(i+=" ")}else if(n&&l.isParenExpr){let u=n.image;(u[u.length-1]??"")!=="("&&(i+=" ")}i+=r}return i}visitIndexDef(t){let e=[],i=m(t,"indexColumnList");if(i){let h=g(i,"indexColumn");for(let p of h){let u=S(p,"TemplateIdentifier"),f=S(p,"Identifier");e.push(u?u+f:f)}}let a=t.children.Unique!==void 0,l;t.children.Gin&&(l="gin"),t.children.Gist&&(l="gist"),t.children.Btree&&(l="btree"),t.children.Hash&&(l="hash");let n,r=m(t,"whereClause");if(r){let h=m(r,"checkExpression");h&&(n=this.reconstructCheckExpression(h))}return{type:"IndexDef",columns:e,unique:a,using:l,where:n}}visitCheckDef(t){let e=m(t,"checkExpression");return{type:"CheckDef",expression:e?this.reconstructCheckExpression(e):""}}visitTriggerDef(t){let e=d(t,"Identifier"),i=e[0]??"",a=e[1]??"",l=t.children.Before?"before":"after",n="update";t.children.Insert&&(n="insert"),t.children.Delete&&(n="delete");let r=t.children.Row?"row":"statement";return{type:"TriggerDef",name:i,timing:l,event:n,forEach:r,executeFunction:a}}visitInstantiation(t){let e=[],i=m(t,"instantiationTargetList");if(i){let r=g(i,"instantiationTarget");for(let h of r){let p=d(h,"Identifier");e.push({tableName:p[0]??"",alias:p[1]})}}let a=m(t,"conceptReference"),l="",n=[];if(a){l=S(a,"Identifier");let r=m(a,"typeArgList");if(r){let h=g(r,"typeArg");for(let p of h){let u=d(p,"Identifier");n.push({tableName:u[0]??"",alias:u[1]})}}}return{type:"Instantiation",targets:e,conceptName:l,typeArgs:n}}visitPerInstanceIndex(t){let e=d(t,"Identifier"),i=e[0]??"",a=e.slice(1),l=t.children.Unique!==void 0,n;t.children.Gin&&(n="gin"),t.children.Gist&&(n="gist"),t.children.Btree&&(n="btree"),t.children.Hash&&(n="hash");let r,h=m(t,"whereClause");if(h){let p=m(h,"checkExpression");p&&(r=this.reconstructCheckExpression(p))}return{type:"PerInstanceIndex",tableName:i,columns:a,unique:l,using:n,where:r}}};function te(s){let t=ie(s),e=[];for(let n of t.errors)e.push({message:n.message,location:{start:{line:n.line??0,column:n.column??0,offset:n.offset},end:{line:n.line??0,column:n.column??0,offset:n.offset}},severity:"error"});if(e.length>0)return{ast:null,errors:e};Vt.input=t.tokens;let i=Vt.program();for(let n of Vt.errors){let r=n.token;e.push({message:n.message,location:{start:{line:r.startLine??0,column:r.startColumn??0,offset:r.startOffset},end:{line:r.endLine??0,column:r.endColumn??0,offset:r.endOffset??r.startOffset}},severity:"error"})}return e.length>0?{ast:null,errors:e}:{ast:new Xt().visit(i),errors:e}}function ee(s){return s.replace(/([A-Z]+)([A-Z][a-z])/g,"$1_$2").replace(/([a-z\d])([A-Z])/g,"$1_$2").toLowerCase()}function ce(s){return s.includes("::")?s.replace(/(\w+)::(\w+)/g,"'$2'::$1"):s}function Le(s,t){return s==="NULL"||/^'[^']*'::\w+$/.test(s)?s:s.includes("::")?ce(s):`'${s}'::${t}`}function ge(){return{concepts:new Map,schemas:new Map,enums:new Map,tableToSchema:new Map,templateSubs:new Map,enumSql:[],tableSql:[],indexSql:[],triggerSql:[],extensionSql:[],functionSql:[],perInstanceIndexSql:[],generatedEnums:new Set,generatedTables:new Set}}function Oe(s,t){for(let e of s.declarations)switch(e.type){case"ConceptDecl":t.concepts.set(e.name,e);break;case"SchemaDecl":t.schemas.set(e.name,e);break;case"EnumDecl":t.enums.set(e.name,e);break}}function le(s,t){let e=s.match(/^\{([^}]+)\}(.*)$/);if(e){let[,i,a]=e,l;for(let[n,r]of t.templateSubs)if(n===i){l=r;break}return(l??ee(i??""))+(a??"")}return s}function de(s){return s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ne(s,t){let e=s;for(let[i,a]of t.templateSubs){let l=de(i),n=new RegExp(`\\{${l}\\}`,"g");e=e.replace(n,a)}return e=ce(e),e}function he(s,t){let e=s.name;if(t.generatedEnums.has(e))return;t.generatedEnums.add(e),t.enums.set(e,s);let i=s.values.map(a=>`'${a}'`).join(", ");t.enumSql.push(`DO $$ BEGIN
3
3
  CREATE TYPE ${e} AS ENUM (${i});
4
4
  EXCEPTION
5
5
  WHEN duplicate_object THEN null;
6
- END $$;`)}function de(s,t){let e=ae(s.name,t),i=s.dataType.toUpperCase(),a=[` ${e} ${i}`],l=[];for(let n of s.constraints)switch(n.type){case"PrimaryKey":a.push("PRIMARY KEY");break;case"NotNull":a.push("NOT NULL");break;case"Unique":a.push("UNIQUE");break;case"Default":{let o=n.value??"NULL",h=s.dataType.toLowerCase();t.enums.has(h)&&(o=Ne(o,h)),a.push(`DEFAULT ${o}`);break}case"Reference":{let o=n.table??"";t.tableToSchema.has(o)&&(o=t.tableToSchema.get(o)??o),l.push(`REFERENCES ${o}(${n.column??"id"})`);break}case"Check":a.push(`CHECK (${ce(n.value??"",t)})`);break;case"OnDelete":l.push(`ON DELETE ${n.action??"NO ACTION"}`);break}return[...a,...l].join(" ")}function he(s,t,e){let i=t.columns.map(p=>ae(p,e)),a=i.join(", "),l=i.join("_"),n=`idx_${s}_${l}`,o=t.unique===!0?"UNIQUE ":"",h=t.using?`USING ${t.using} `:"";return`CREATE ${o}INDEX ${n} ON ${s} ${h}(${a});`}function Te(s,t,e){return` CHECK (${ce(t.expression,e)})`}function Me(s,t,e){let i=t.timing.toUpperCase(),a=t.event.toUpperCase(),l=t.forEach==="row"?"FOR EACH ROW":"FOR EACH STATEMENT";return`CREATE TRIGGER ${t.name}_${s}
6
+ END $$;`)}function Te(s,t){let e=le(s.name,t),i=s.dataType.toUpperCase(),a=[` ${e} ${i}`],l=[];for(let n of s.constraints)switch(n.type){case"PrimaryKey":a.push("PRIMARY KEY");break;case"NotNull":a.push("NOT NULL");break;case"Unique":a.push("UNIQUE");break;case"Default":{let r=n.value??"NULL",h=s.dataType.toLowerCase();t.enums.has(h)&&(r=Le(r,h)),a.push(`DEFAULT ${r}`);break}case"Reference":{let r=n.table??"";t.tableToSchema.has(r)&&(r=t.tableToSchema.get(r)??r),l.push(`REFERENCES ${r}(${n.column??"id"})`);break}case"Check":a.push(`CHECK (${ne(n.value??"",t)})`);break;case"OnDelete":l.push(`ON DELETE ${n.action??"NO ACTION"}`);break}return[...a,...l].join(" ")}function pe(s,t,e){let i=t.columns.map(u=>le(u,e)),a=i.join(", "),l=i.join("_"),n=`idx_${s}_${l}`,r=t.unique===!0?"UNIQUE ":"",h=t.using?`USING ${t.using} `:"",p=t.where?` WHERE (${ne(t.where,e)})`:"";return`CREATE ${r}INDEX ${n} ON ${s} ${h}(${a})${p};`}function Me(s,t,e){return` CHECK (${ne(t.expression,e)})`}function xe(s,t,e){let i=t.timing.toUpperCase(),a=t.event.toUpperCase(),l=t.forEach==="row"?"FOR EACH ROW":"FOR EACH STATEMENT";return`CREATE TRIGGER ${t.name}_${s}
7
7
  ${i} ${a} ON ${s}
8
- ${l} EXECUTE FUNCTION ${t.executeFunction}();`}function pe(s,t){let e=[];for(let i of s.mixins){let a=t.schemas.get(i);a&&e.push(...pe(a,t))}return e.push(...s.members),e}function ie(s,t,e){let i=pe(s,e),a=[],l=[],n=[],o=[];for(let h of i)switch(h.type){case"ColumnDef":a.push(h);break;case"IndexDef":l.push(h);break;case"CheckDef":n.push(h);break;case"TriggerDef":o.push(h);break}return{tableName:t,columns:a,indexes:l,checks:n,triggers:o}}function re(s,t){if(t.generatedTables.has(s.tableName))return;t.generatedTables.add(s.tableName);let e=s.columns.map(n=>de(n,t)),i=s.checks.map(n=>Te(s.tableName,n,t)),l=[...e,...i].join(`,
8
+ ${l} EXECUTE FUNCTION ${t.executeFunction}();`}function me(s,t){let e=[];for(let i of s.mixins){let a=t.schemas.get(i);a&&e.push(...me(a,t))}return e.push(...s.members),e}function oe(s,t,e){let i=me(s,e),a=[],l=[],n=[],r=[];for(let h of i)switch(h.type){case"ColumnDef":a.push(h);break;case"IndexDef":l.push(h);break;case"CheckDef":n.push(h);break;case"TriggerDef":r.push(h);break}return{tableName:t,columns:a,indexes:l,checks:n,triggers:r}}function ae(s,t){if(t.generatedTables.has(s.tableName))return;t.generatedTables.add(s.tableName);let e=s.columns.map(n=>Te(n,t)),i=s.checks.map(n=>Me(s.tableName,n,t)),l=[...e,...i].join(`,
9
9
  `);t.tableSql.push(`CREATE TABLE ${s.tableName} (
10
10
  ${l}
11
- );`);for(let n of s.indexes)t.indexSql.push(he(s.tableName,n,t));for(let n of s.triggers)t.triggerSql.push(Me(s.tableName,n,t))}function xe(s,t){t.extensionSql.push(`CREATE EXTENSION IF NOT EXISTS "${s.name}";`)}function Ae(s,t){t.functionSql.push(`CREATE OR REPLACE FUNCTION ${s.name}()
11
+ );`);for(let n of s.indexes)t.indexSql.push(pe(s.tableName,n,t));for(let n of s.triggers)t.triggerSql.push(xe(s.tableName,n,t))}function Ae(s,t){t.extensionSql.push(`CREATE EXTENSION IF NOT EXISTS "${s.name}";`)}function De(s,t){t.functionSql.push(`CREATE OR REPLACE FUNCTION ${s.name}()
12
12
  RETURNS TRIGGER AS $$
13
13
  BEGIN
14
14
  ${s.body}
15
15
  END;
16
- $$ LANGUAGE plpgsql;`)}function De(s,t){le(s,t)}function Re(s,t){t.schemas.set(s.name,s)}function Ie(s,t){let e=t.concepts.get(s.conceptName);if(!e){let n=t.schemas.get(s.conceptName);if(n){let o=s.targets[0];if(o){let h=o.tableName;t.tableToSchema.set(n.name,h);let p=ie(n,h,t);re(p,t)}}return}t.templateSubs.clear();for(let n=0;n<e.typeParams.length;n++){let o=e.typeParams[n],h=s.typeArgs[n];o&&h&&(t.templateSubs.set(o,h.alias??te(o)),t.tableToSchema.set(o,h.tableName))}let i=e.members.filter(n=>n.type==="SchemaDecl"),a=new Map;for(let n=0;n<s.targets.length&&n<i.length;n++){let o=s.targets[n],h=i[n];o&&h&&(a.set(h.name,{name:o.tableName,alias:o.alias}),t.tableToSchema.set(h.name,o.tableName),o.alias?t.templateSubs.set(h.name,o.alias):t.templateSubs.set(h.name,te(h.name)))}let l=e.members.filter(n=>n.type==="EnumDecl");for(let n of l)le(n,t);for(let n of i){let o=a.get(n.name);if(o){let h=ie(n,o.name,t);re(h,t)}}}function ye(s,t){let e={type:"IndexDef",columns:s.columns,unique:s.unique,using:s.using};t.perInstanceIndexSql.push(he(s.tableName,e,t))}function ee(s){let t=Le();ge(s,t);for(let a of s.declarations)ve(a,t);let e=[];t.extensionSql.length>0&&e.push(t.extensionSql.join(`
16
+ $$ LANGUAGE plpgsql;`)}function Re(s,t){he(s,t)}function Ie(s,t){t.schemas.set(s.name,s)}function ye(s,t){let e=t.concepts.get(s.conceptName);if(!e){let n=t.schemas.get(s.conceptName);if(n){let r=s.targets[0];if(r){let h=r.tableName;t.tableToSchema.set(n.name,h);let p=oe(n,h,t);ae(p,t)}}return}t.templateSubs.clear();for(let n=0;n<e.typeParams.length;n++){let r=e.typeParams[n],h=s.typeArgs[n];r&&h&&(t.templateSubs.set(r,h.alias??ee(r)),t.tableToSchema.set(r,h.tableName))}let i=e.members.filter(n=>n.type==="SchemaDecl"),a=new Map;for(let n=0;n<s.targets.length&&n<i.length;n++){let r=s.targets[n],h=i[n];r&&h&&(a.set(h.name,{name:r.tableName,alias:r.alias}),t.tableToSchema.set(h.name,r.tableName),r.alias?t.templateSubs.set(h.name,r.alias):t.templateSubs.set(h.name,ee(h.name)))}let l=e.members.filter(n=>n.type==="EnumDecl");for(let n of l)he(n,t);for(let n of i){let r=a.get(n.name);if(r){let h=oe(n,r.name,t);ae(h,t)}}}function ve(s,t){let e={type:"IndexDef",columns:s.columns,unique:s.unique,using:s.using,where:s.where};t.perInstanceIndexSql.push(pe(s.tableName,e,t))}function se(s){let t=ge();Oe(s,t);for(let a of s.declarations)ke(a,t);let e=[];t.extensionSql.length>0&&e.push(t.extensionSql.join(`
17
17
 
18
18
  `)),t.functionSql.length>0&&e.push(t.functionSql.join(`
19
19
 
20
20
  `)),t.enumSql.length>0&&e.push(t.enumSql.join(`
21
21
 
22
- `));let i=[];for(let a of t.tableSql){a&&i.push(a);let l=a.match(/CREATE TABLE (\w+)/);if(l){let n=l[1]??"";for(let o of t.indexSql)o.includes(` ON ${n} `)&&i.push(o);for(let o of t.triggerSql)o.includes(` ON ${n}`)&&i.push(o)}}return i.length>0&&e.push(i.join(`
22
+ `));let i=[];for(let a of t.tableSql){a&&i.push(a);let l=a.match(/CREATE TABLE (\w+)/);if(l){let n=l[1]??"";for(let r of t.indexSql)r.includes(` ON ${n} `)&&i.push(r);for(let r of t.triggerSql)r.includes(` ON ${n}`)&&i.push(r)}}return i.length>0&&e.push(i.join(`
23
23
 
24
24
  `)),t.perInstanceIndexSql.length>0&&e.push(t.perInstanceIndexSql.join(`
25
25
 
@@ -28,6 +28,6 @@ $$ LANGUAGE plpgsql;`)}function De(s,t){le(s,t)}function Re(s,t){t.schemas.set(s
28
28
  `+e.join(`
29
29
 
30
30
  `)+`
31
- `}function ve(s,t){switch(s.type){case"ExtensionDecl":xe(s,t);break;case"FunctionDecl":Ae(s,t);break;case"EnumDecl":De(s,t);break;case"SchemaDecl":Re(s,t);break;case"ConceptDecl":break;case"Instantiation":Ie(s,t);break;case"PerInstanceIndex":ye(s,t);break}}function me(s){let t=[],e=Xt(s);if(e.errors.length>0)return{success:!1,errors:e.errors,ast:e.ast??void 0};if(!e.ast)return t.push({message:"Failed to parse source code",severity:"error"}),{success:!1,errors:t};try{return{success:!0,sql:ee(e.ast),errors:[],ast:e.ast}}catch(i){let a=i instanceof Error?i.message:String(i);return t.push({message:`Code generation failed: ${a}`,severity:"error"}),{success:!1,errors:t,ast:e.ast}}}function be(s){let t=me(s);if(!t.success||!t.sql){let e=t.errors.map(i=>i.message).join(`
31
+ `}function ke(s,t){switch(s.type){case"ExtensionDecl":Ae(s,t);break;case"FunctionDecl":De(s,t);break;case"EnumDecl":Re(s,t);break;case"SchemaDecl":Ie(s,t);break;case"ConceptDecl":break;case"Instantiation":ye(s,t);break;case"PerInstanceIndex":ve(s,t);break}}function ue(s){let t=[],e=te(s);if(e.errors.length>0)return{success:!1,errors:e.errors,ast:e.ast??void 0};if(!e.ast)return t.push({message:"Failed to parse source code",severity:"error"}),{success:!1,errors:t};try{return{success:!0,sql:se(e.ast),errors:[],ast:e.ast}}catch(i){let a=i instanceof Error?i.message:String(i);return t.push({message:`Code generation failed: ${a}`,severity:"error"}),{success:!1,errors:t,ast:e.ast}}}function be(s){let t=ue(s);if(!t.success||!t.sql){let e=t.errors.map(i=>i.message).join(`
32
32
  `);throw new Error(`Compilation failed:
33
- ${e}`)}return t.sql}export{me as compile,be as compileToSQL,ee as generate,Xt as parse};
33
+ ${e}`)}return t.sql}export{ue as compile,be as compileToSQL,se as generate,te as parse};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barishnamazov/gsql",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Generic SQL - Parametric Polymorphism for SQL Schemas",
5
5
  "module": "src/index.ts",
6
6
  "type": "module",
package/src/generator.ts CHANGED
@@ -309,8 +309,9 @@ function generateIndexSql(tableName: string, index: IndexDef, ctx: GeneratorCont
309
309
  const indexName = `idx_${tableName}_${columnNames}`;
310
310
  const unique = index.unique === true ? "UNIQUE " : "";
311
311
  const using = index.using ? `USING ${index.using} ` : "";
312
+ const where = index.where ? ` WHERE (${expandCheckExpression(index.where, ctx)})` : "";
312
313
 
313
- return `CREATE ${unique}INDEX ${indexName} ON ${tableName} ${using}(${columns});`;
314
+ return `CREATE ${unique}INDEX ${indexName} ON ${tableName} ${using}(${columns})${where};`;
314
315
  }
315
316
 
316
317
  function generateCheckSql(_tableName: string, check: CheckDef, ctx: GeneratorContext): string {
@@ -518,6 +519,7 @@ function processPerInstanceIndex(decl: PerInstanceIndex, ctx: GeneratorContext):
518
519
  columns: decl.columns,
519
520
  unique: decl.unique,
520
521
  using: decl.using,
522
+ where: decl.where,
521
523
  };
522
524
 
523
525
  ctx.perInstanceIndexSql.push(generateIndexSql(decl.tableName, indexDef, ctx));
package/src/lexer.ts CHANGED
@@ -155,6 +155,12 @@ export const Execute = createToken({
155
155
  longer_alt: Identifier,
156
156
  });
157
157
 
158
+ export const Where = createToken({
159
+ name: "Where",
160
+ pattern: /where/,
161
+ longer_alt: Identifier,
162
+ });
163
+
158
164
  export const Unique = createToken({
159
165
  name: "Unique",
160
166
  pattern: /unique/,
@@ -550,6 +556,7 @@ export const allTokens = [
550
556
  Row,
551
557
  Statement,
552
558
  Execute,
559
+ Where,
553
560
  Unique,
554
561
  Gin,
555
562
  Gist,
package/src/parser.ts CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  Row,
28
28
  Statement,
29
29
  Execute,
30
+ Where,
30
31
  Function,
31
32
  Unique,
32
33
  Gin,
@@ -181,6 +182,8 @@ class GSQLCstParser extends CstParser {
181
182
  { ALT: () => this.CONSUME(Gist) },
182
183
  { ALT: () => this.CONSUME(Btree) },
183
184
  { ALT: () => this.CONSUME(Hash) },
185
+ // Index modifiers
186
+ { ALT: () => this.CONSUME(Where) },
184
187
  // Data types
185
188
  { ALT: () => this.CONSUME(Serial) },
186
189
  { ALT: () => this.CONSUME(BigSerial) },
@@ -513,7 +516,7 @@ class GSQLCstParser extends CstParser {
513
516
  this.CONSUME(RParen);
514
517
  });
515
518
 
516
- // index(col1, col2) unique gin;
519
+ // index(col1, col2) unique gin where (condition);
517
520
  private indexDef = this.RULE("indexDef", () => {
518
521
  this.CONSUME(Index);
519
522
  this.CONSUME(LParen);
@@ -528,9 +531,20 @@ class GSQLCstParser extends CstParser {
528
531
  { ALT: () => this.CONSUME(Hash) },
529
532
  ]);
530
533
  });
534
+ this.OPTION(() => {
535
+ this.SUBRULE(this.whereClause);
536
+ });
531
537
  this.CONSUME(Semicolon);
532
538
  });
533
539
 
540
+ // where (expression)
541
+ private whereClause = this.RULE("whereClause", () => {
542
+ this.CONSUME(Where);
543
+ this.CONSUME(LParen);
544
+ this.SUBRULE(this.checkExpression);
545
+ this.CONSUME(RParen);
546
+ });
547
+
534
548
  private indexColumnList = this.RULE("indexColumnList", () => {
535
549
  this.SUBRULE(this.indexColumn);
536
550
  this.MANY(() => {
@@ -632,7 +646,7 @@ class GSQLCstParser extends CstParser {
632
646
  });
633
647
  });
634
648
 
635
- // index(table, column);
649
+ // index(table, column) unique where (condition);
636
650
  private perInstanceIndex = this.RULE("perInstanceIndex", () => {
637
651
  this.CONSUME(Index);
638
652
  this.CONSUME(LParen);
@@ -653,6 +667,9 @@ class GSQLCstParser extends CstParser {
653
667
  { ALT: () => this.CONSUME(Hash) },
654
668
  ]);
655
669
  });
670
+ this.OPTION(() => {
671
+ this.SUBRULE(this.whereClause);
672
+ });
656
673
  this.CONSUME(Semicolon);
657
674
  });
658
675
  }
@@ -1151,11 +1168,22 @@ class CstToAstVisitor {
1151
1168
  if (node.children["Btree"]) using = "btree";
1152
1169
  if (node.children["Hash"]) using = "hash";
1153
1170
 
1171
+ // Extract where clause
1172
+ let where: string | undefined;
1173
+ const whereClauseNode = extractChild(node, "whereClause");
1174
+ if (whereClauseNode) {
1175
+ const exprNode = extractChild(whereClauseNode, "checkExpression");
1176
+ if (exprNode) {
1177
+ where = this.reconstructCheckExpression(exprNode);
1178
+ }
1179
+ }
1180
+
1154
1181
  return {
1155
1182
  type: "IndexDef",
1156
1183
  columns,
1157
1184
  unique,
1158
1185
  using,
1186
+ where,
1159
1187
  };
1160
1188
  }
1161
1189
 
@@ -1248,12 +1276,23 @@ class CstToAstVisitor {
1248
1276
  if (node.children["Btree"]) using = "btree";
1249
1277
  if (node.children["Hash"]) using = "hash";
1250
1278
 
1279
+ // Extract where clause
1280
+ let where: string | undefined;
1281
+ const whereClauseNode = extractChild(node, "whereClause");
1282
+ if (whereClauseNode) {
1283
+ const exprNode = extractChild(whereClauseNode, "checkExpression");
1284
+ if (exprNode) {
1285
+ where = this.reconstructCheckExpression(exprNode);
1286
+ }
1287
+ }
1288
+
1251
1289
  return {
1252
1290
  type: "PerInstanceIndex",
1253
1291
  tableName,
1254
1292
  columns,
1255
1293
  unique,
1256
1294
  using,
1295
+ where,
1257
1296
  };
1258
1297
  }
1259
1298
  }
package/src/types.ts CHANGED
@@ -103,6 +103,7 @@ export interface PerInstanceIndex extends BaseNode {
103
103
  columns: string[];
104
104
  unique?: boolean;
105
105
  using?: string;
106
+ where?: string;
106
107
  }
107
108
 
108
109
  // ============================================================================
@@ -131,6 +132,7 @@ export interface IndexDef extends BaseNode {
131
132
  columns: string[];
132
133
  unique?: boolean;
133
134
  using?: string;
135
+ where?: string;
134
136
  }
135
137
 
136
138
  export interface CheckDef extends BaseNode {