import{r as A,j as a,q as c,fD as q,fE as Y,fF as F,n as j,v as C,fG as K,fH as _,lP as te,lQ as ae,eB as z,bk as re,be as D,aJ as g,an as h,ao as M,c as W,bS as ie,F as Ee,aN as oe,ce as Te,cf as se,lR as le,aj as P,bJ as Ae,aI as B,L as v,a4 as G,a5 as V,d as X,h_ as Z,u as w,w as me,bG as Se,bH as Re}from"./index-DNBEEyYo.js";const ce=({defaultTitle:e,maxNumberOfTabs:n,children:t})=>{const r=[{id:1,title:`${e} 1`}],[E,o]=A.useState(r),[m,i]=A.useState(0),[T,l]=A.useState(!1),S=()=>{if(E.length>=n)return;const s=[...E],I=s[s.length-1].id+1;s.push({id:I,title:`${e} ${I}`}),o(s),i(s.length-1)},d=s=>{if(!(T&&!window.confirm("Tab is busy. Do you want to leave?"))&&E.length>1){const I=[...E].filter(O=>O.id!==s);o(I),s===E[m].id&&i(I.length-1)}};return a.jsx(c,{children:a.jsxs(q,{index:m,variant:"enclosed",onChange:i,size:"sm",children:[a.jsxs(Y,{children:[E.map(s=>a.jsxs(F,{sx:{paddingRight:"5px"},children:[s.title,a.jsx(j,{variant:"ghost",onClick:I=>{I.stopPropagation(),d(s.id)},icon:C.cross,sx:{alignSelf:"end",width:"10px",height:"20px",padding:"10px",marginBottom:"14px",marginLeft:"5px"}})]},s.id)),a.jsx(c,{p:"5px",sx:{borderStyles:"solid",borderWidth:"1px",borderTopLeftRadius:"var(--quartz-radii-md)",borderTopRightRadius:"var(--quartz-radii-md)"},children:a.jsx(j,{variant:"ghost",onClick:S,icon:C.plus,sx:{width:"10px",height:"20px",padding:"10px"},disabled:T})})]}),a.jsx(K,{sx:{borderStyles:"solid",borderWidth:"1px",minWidth:"1234px",maxWidth:"1234px"},children:E.map(s=>a.jsx(_,{sx:{padding:"0px"},children:a.jsx(c,{width:"100%",height:"100%",children:A.cloneElement(t,{setBusy:l})})},s.id))})]})})},$=()=>{const[e,n]=A.useState(),[t,r]=A.useState(),[E,o]=A.useState(!1),[m,{data:i,isLoading:T}]=te(),[l,{data:S,isLoading:d}]=ae();return A.useEffect(()=>{E&&n(void 0)},[E]),A.useEffect(()=>{i!=null&&i.nextUri&&l(i.nextUri)},[i==null?void 0:i.nextUri,l]),A.useEffect(()=>{S!=null&&S.nextUri&&l(S.nextUri)},[S==null?void 0:S.nextUri,l]),A.useEffect(()=>{S&&n(s=>(s==null?void 0:s.id)===S.id?{...s,...S}:S)},[S]),A.useEffect(()=>{i&&n(s=>(s==null?void 0:s.id)===i.id?{...s,...i}:i)},[i]),A.useEffect(()=>{var s;i&&r((s=i==null?void 0:i.stats)==null?void 0:s.state)},[i,r]),A.useEffect(()=>{var s;S&&r((s=S==null?void 0:S.stats)==null?void 0:s.state)},[S,r]),A.useEffect(()=>{o(T||d||!!t&&t!=="FINISHED"&&t!=="FAILED")},[T,d,t,o]),{postStatement:m,queryResult:e,isLoading:E}},Ie=({catalogs:e,schemas:n,tables:t,isLoading:r,onCatalogSelect:E,onSchemaSelect:o,onTableSelect:m,refreshCatalogs:i,refreshSchemas:T,refreshTables:l})=>{const S=z(),[d,s]=A.useState(""),[I,O]=A.useState(""),[R,u]=A.useState("");return a.jsxs(re,{sx:{borderStyle:"none",height:"100%"},children:[a.jsx(D,{label:"Catalog",labelAction:a.jsx(g,{ml:"120px",intent:"inline",padding:"0px",sx:{maxHeight:"25px"},href:"#",onClick:i,children:a.jsx(h,{mainText:"Refresh",children:a.jsx(M,{icon:C.refresh,size:"xs"})})}),onChange:p=>{E(p),s(p),O("")},options:e,value:d,placeholder:"select catalog",mb:"20px",width:"100%",isLoading:r&&d===""}),a.jsx(D,{label:"Schema",labelAction:a.jsx(g,{ml:"120px",intent:"inline",padding:"0px",sx:{maxHeight:"25px"},href:"#",onClick:T,disabled:d==="",children:a.jsx(h,{mainText:d===""?"No catalog selected":"Refresh",children:a.jsx(M,{icon:C.refresh,size:"xs"})})}),onChange:p=>{o(p),O(p),u("")},options:n,value:I,placeholder:"select schema",width:"100%",isLoading:r&&d!==""&&I===""},`${d}-1`),a.jsxs(c,{mt:"30px",flexDirection:"column",children:[a.jsxs(c,{flexDirection:"row",justifyContent:"space-between",children:[a.jsx(W,{children:"Tables"}),a.jsx(g,{intent:"inline",padding:"0px",sx:{maxHeight:"25px"},href:"#",onClick:l,disabled:I==="",children:a.jsx(h,{mainText:I===""?"No schema selected":"Refresh",children:a.jsx(M,{icon:C.refresh,size:"xs"})})})]}),a.jsx(ie,{ml:"5px",width:"95%",mt:"5px",mb:"0px"}),a.jsx(Ee,{bg:S.colors.white,sx:{minHeight:"700px",maxHeight:"700px",overflowY:"auto",overflowX:"hidden",maxWidth:"180px"},children:r&&d!==""&&I!==""?a.jsx(c,{justifySelf:"center",mt:"20px",children:a.jsx(oe,{mr:"auto",size:"md"})}):a.jsx(Te,{m:"2px",sx:{boxShadow:"none"},children:t==null?void 0:t.flat().map(p=>a.jsx(se,{onClick:()=>{u(p),m(p)},width:"177px",isActive:p===R,hasDivider:!0,children:a.jsx(h,{mainText:p,children:p.length>23?`${p.substring(0,20)}...`:p})},p))})})]})]})},Oe=e=>{A.useEffect(()=>{if(!e)return()=>{};const n=t=>{t.preventDefault(),t.returnValue=""};return window.addEventListener("beforeunload",n),()=>{window.removeEventListener("beforeunload",n)}},[e])};function de(e,n){const t=A.useContext(le).navigator,r=n;A.useEffect(()=>{if(!e)return()=>{};const E=t.push,o=t.replace;return t.push=function(...m){if(window.confirm(r))return E.apply(t,m)},t.replace=function(...m){if(window.confirm(r))return o.apply(t,m)},()=>{t.push=E,t.replace=o}},[e,t,r])}const pe=`-------- ALTER
ALTER BRANCH source_branch IN TABLE table_name FAST FORWARD TO target_branch

ALTER MATERIALIZED VIEW [ IF EXISTS ] name RENAME TO new_name

ALTER MATERIALIZED VIEW name SET PROPERTIES property_name = expression [, ...]

ALTER MATERIALIZED VIEW name SET AUTHORIZATION ( user | USER user | ROLE role )

ALTER SCHEMA name RENAME TO new_name

ALTER SCHEMA name SET AUTHORIZATION ( user | USER user | ROLE role )

ALTER TABLE [ IF EXISTS ] name RENAME TO new_name

ALTER TABLE [ IF EXISTS ] name ADD COLUMN [ IF NOT EXISTS ] column_name data_type
[ DEFAULT default ] [ NOT NULL ] [ COMMENT comment ]
[ WITH ( property_name = expression [, ...] ) ]
[ FIRST | LAST | AFTER after_column_name ]

ALTER TABLE [ IF EXISTS ] name DROP COLUMN [ IF EXISTS ] column_name

ALTER TABLE [ IF EXISTS ] name RENAME COLUMN [ IF EXISTS ] old_name TO new_name

ALTER TABLE [ IF EXISTS ] name ALTER COLUMN column_name SET DATA TYPE new_type

ALTER TABLE [ IF EXISTS ] name ALTER COLUMN column_name DROP NOT NULL

ALTER TABLE name SET AUTHORIZATION ( user | USER user | ROLE role )

ALTER TABLE name SET PROPERTIES property_name = expression [, ...]

ALTER TABLE name EXECUTE command [ ( parameter => expression [, ... ] ) ]
    [ WHERE expression ]

ALTER VIEW name RENAME TO new_name

ALTER VIEW name REFRESH

ALTER VIEW name SET AUTHORIZATION ( user | USER user | ROLE role )

-------- ANALYZE
ANALYZE table_name [ WITH ( property_name = expression [, ...] ) ]

-------- CALL
CALL procedure_name ( [ name => ] expression [, ...] )

-------- COMMENT
COMMENT ON ( TABLE | VIEW | COLUMN ) name IS 'comments'

-------- COMMIT
COMMIT [ WORK ]

-------- CREATE
CREATE [ OR REPLACE ] BRANCH [ IF NOT EXISTS ] branch_name
[ WITH ( property_name = expression [, ...] ) ]
IN TABLE table_name
[ FROM source_branch ]

CREATE CATALOG
catalog_name
USING connector_name
[ WITH ( property_name = expression [, ...] ) ]

CREATE [OR REPLACE] FUNCTION
udf_definition

CREATE [ OR REPLACE ] MATERIALIZED VIEW
[ IF NOT EXISTS ] view_name
[ GRACE PERIOD interval ]
[ COMMENT string ]
[ WITH properties ]
AS query

CREATE ROLE role_name
[ WITH ADMIN ( user | USER user | ROLE role | CURRENT_USER | CURRENT_ROLE ) ]
[ IN catalog ]

CREATE SCHEMA [ IF NOT EXISTS ] schema_name
[ AUTHORIZATION ( user | USER user | ROLE role ) ]
[ WITH ( property_name = expression [, ...] ) ]

CREATE [ OR REPLACE ] TABLE [ IF NOT EXISTS ]
table_name (
  { column_name data_type [ DEFAULT default ] [ NOT NULL ]
      [ COMMENT comment ]
      [ WITH ( property_name = expression [, ...] ) ]
  | LIKE existing_table_name
      [ { INCLUDING | EXCLUDING } PROPERTIES ]
  }
  [, ...]
)
[ COMMENT table_comment ]
[ WITH ( property_name = expression [, ...] ) ]

CREATE [ OR REPLACE ] TABLE [ IF NOT EXISTS ] table_name [ ( column_alias, ... ) ]
[ COMMENT table_comment ]
[ WITH ( property_name = expression [, ...] ) ]
AS query
[ WITH [ NO ] DATA ]

CREATE [ OR REPLACE ] VIEW view_name
[ COMMENT view_comment ]
[ SECURITY { DEFINER | INVOKER } ]
AS query

-------- DEALLOCATE
DEALLOCATE PREPARE statement_name

-------- DELETE
DELETE FROM table_name [ @ branch_name ] [ WHERE condition ]

-------- DENY
DENY ( privilege [, ...] | ( ALL PRIVILEGES ) )
ON [ BRANCH branch_name IN ] ( table_name | TABLE table_name | SCHEMA schema_name)
TO ( user | USER user | ROLE role )

-------- DESCRIBE
DESCRIBE table_name

DESCRIBE INPUT statement_name

DESCRIBE OUTPUT statement_name

-------- DROP
DROP BRANCH [ IF EXISTS ] branch_name
IN TABLE table_name

DROP CATALOG catalog_name

DROP FUNCTION [ IF EXISTS ] udf_name ( [ [ parameter_name ] data_type [, ...] ] )

DROP MATERIALIZED VIEW [ IF EXISTS ] view_name

DROP ROLE [ IF EXISTS ] role_name
[ IN catalog ]

DROP SCHEMA [ IF EXISTS ] schema_name [ CASCADE | RESTRICT ]

DROP TABLE  [ IF EXISTS ] table_name

DROP VIEW [ IF EXISTS ] view_name

-------- EXECUTE
EXECUTE statement_name [ USING parameter1 [ , parameter2, ... ] ]

EXECUTE IMMEDIATE statement [ USING parameter1 [ , parameter2, ... ] ]

-------- EXPLAIN
EXPLAIN [ ( option [, ...] ) ] statement

EXPLAIN ANALYZE [VERBOSE] statement

-------- GRANT
GRANT ( privilege [, ...] | ( ALL PRIVILEGES ) )
ON [ BRANCH branch_name IN ] ( table_name | TABLE table_name | SCHEMA schema_name)
TO ( user | USER user | ROLE role )
[ WITH GRANT OPTION ]

GRANT role_name [, ...]
TO ( user | USER user_name | ROLE role_name) [, ...]
[ GRANTED BY ( user | USER user | ROLE role | CURRENT_USER | CURRENT_ROLE ) ]
[ WITH ADMIN OPTION ]
[ IN catalog ]

-------- INSERT INTO
INSERT INTO table_name [ @ branch_name ] [ ( column [, ... ] ) ] query

-------- MATCH_RECOGNIZE
MATCH_RECOGNIZE (
  [ PARTITION BY column [, ...] ]
  [ ORDER BY column [, ...] ]
  [ MEASURES measure_definition [, ...] ]
  [ rows_per_match ]
  [ AFTER MATCH skip_to ]
  PATTERN ( row_pattern )
  [ SUBSET subset_definition [, ...] ]
  DEFINE variable_definition [, ...]
)

-------- MERGE
MERGE INTO target_table [ @ branch_name ] [ [ AS ]  target_alias ]
USING { source_table | query } [ [ AS ] source_alias ]
ON search_condition
when_clause [...]

-------- PREPARE
PREPARE statement_name FROM statement

-------- REFRESH
REFRESH MATERIALIZED VIEW view_name

-------- RESET
RESET SESSION name

RESET SESSION catalog.name

RESET SESSION AUTHORIZATION

-------- REVOKE
REVOKE [ GRANT OPTION FOR ]
( privilege [, ...] | ALL PRIVILEGES )
ON [ BRANCH branch_name IN ] ( table_name | TABLE table_name | SCHEMA schema_name )
FROM ( user | USER user | ROLE role )

REVOKE
[ ADMIN OPTION FOR ]
role_name [, ...]
FROM ( user | USER user | ROLE role) [, ...]
[ GRANTED BY ( user | USER user | ROLE role | CURRENT_USER | CURRENT_ROLE ) ]
[ IN catalog ]

-------- ROLLBACK
ROLLBACK [ WORK ]

-------- SELECT
[ WITH SESSION [ name = expression [, ...] ]
[ WITH [ FUNCTION udf ] [, ...] ]
[ WITH [ RECURSIVE ] with_query [, ...] ]
SELECT [ ALL | DISTINCT ] select_expression [, ...]
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]
[ HAVING condition]
[ WINDOW window_definition_list]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY expression [ ASC | DESC ] [, ...] ]
[ OFFSET count [ ROW | ROWS ] ]
[ LIMIT { count | ALL } ]
[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ]

-------- SET
SET PATH path-element[, ...]

SET ROLE ( role | ALL | NONE )
[ IN catalog ]

SET SESSION name = expression

SET SESSION catalog.name = expression

SET SESSION AUTHORIZATION username

SET TIME ZONE LOCAL

SET TIME ZONE expression

-------- SHOW
SHOW BRANCHES ( FROM | IN ) TABLE table_name

SHOW CATALOGS [ LIKE pattern ]

SHOW COLUMNS FROM table [ LIKE pattern ]

SHOW CREATE FUNCTION function_name

SHOW CREATE MATERIALIZED VIEW view_name

SHOW CREATE SCHEMA schema_name

SHOW CREATE TABLE table_name

SHOW CREATE VIEW view_name

SHOW FUNCTIONS [ FROM schema ] [ LIKE pattern ]

SHOW GRANTS [ ON [ TABLE ] table_name ]

SHOW ROLE GRANTS [ FROM catalog ]

SHOW [CURRENT] ROLES [ FROM catalog ]

SHOW SCHEMAS [ FROM catalog ] [ LIKE pattern ]

SHOW SESSION [ LIKE pattern ]

SHOW STATS FOR table

SHOW STATS FOR ( query )

SHOW TABLES [ FROM schema ] [ LIKE pattern ]

-------- START
START TRANSACTION [ mode [, ...] ]

-------- TRUNCATE
TRUNCATE TABLE table_name

-------- UPDATE
UPDATE table_name [ @ branch_name ]
SET [ ( column = expression [, ... ] ) ] [ WHERE condition ]

-------- USE
USE catalog.schema

USE schema

-------- VALUES
VALUES row [, ...]`,Ne=({isOpen:e,handleToggle:n})=>{const{colorMode:t}=P();return a.jsx(Ae,{size:"3xl",title:"SQL statement syntax",withCloseButton:!0,isOpen:e,onClose:n,children:a.jsx(B,{mode:"sql",labelProps:{align:"top"},value:pe,codeMirrorProps:{theme:`${t}`,readOnly:"true",lineNumbers:"false",maxHeight:"800px",basicSetup:{lineNumbers:!1,foldGutter:!1}}})})},fe=["Create Table","Create View","Create Schema","Create Function","Create Materialized View","Query Plan"],Le=({result:e,isLoading:n,maxHeight:t})=>{const{colorMode:r}=P(),[E,o]=A.useState(0),m=A.useMemo(()=>((e==null?void 0:e.columns)||[]).map(s=>({identifier:{name:s.name}})),[e==null?void 0:e.columns]),i=A.useMemo(()=>((e==null?void 0:e.data)||[]).map(s=>s.map((I,O)=>({identifierName:m[O].identifier.name,value:I}))),[e==null?void 0:e.data,m]),T=A.useMemo(()=>((e==null?void 0:e.columns)||[]).map(s=>[{identifierName:"Name",value:s.name},{identifierName:"Type",value:s.type}]),[e==null?void 0:e.columns]),l=A.useMemo(()=>((e==null?void 0:e.data)||[]).flat()[0],[e==null?void 0:e.data]),S=A.useMemo(()=>{var s,I,O,R,u;return n?a.jsx(v,{sx:{position:"relative",transform:"none"}}):e!=null&&e.error?a.jsx(c,{children:a.jsx(G,{content:a.jsx(c,{alignItems:"center",children:a.jsx(X,{children:e==null?void 0:e.error.message})}),type:V.error})}):!(e!=null&&e.data)&&((s=e==null?void 0:e.stats)==null?void 0:s.state)==="FINISHED"?a.jsx(c,{children:a.jsx(G,{content:a.jsx(c,{alignItems:"center",children:a.jsx(X,{children:"The query returned no data"})}),type:V.warning})}):((I=e==null?void 0:e.columns)==null?void 0:I.length)===1&&fe.includes((O=e==null?void 0:e.columns[0])==null?void 0:O.name)?a.jsx(c,{children:a.jsx(B,{mode:(R=e==null?void 0:e.columns[0])!=null&&R.name.startsWith("Create")?"sql":"",label:(u=e==null?void 0:e.columns[0])==null?void 0:u.name,mt:"0px",width:"100%",readOnly:!0,value:l,codeMirrorProps:{theme:`${r}`,minHeight:"60px",maxHeight:`${t-30}px`,overflow:"auto"}})}):a.jsx(c,{children:i.length>0?a.jsx(Z,{values:i,columnHeaders:m,sx:{overflow:"auto",borderTopColor:"none",maxHeight:`${t-10}px`,marginRight:"0px"}}):a.jsx(W,{sx:{padding:"20px"},children:"No result. Run a query to see results."})})},[e,n,m,i,l,r,t]),d=A.useMemo(()=>n?a.jsx(v,{sx:{position:"relative",transform:"none"}}):a.jsx(c,{children:T.length>0?a.jsx(Z,{values:T,columnHeaders:[{identifier:{name:"Name"}},{identifier:{name:"Type"}}],sx:{borderTopColor:"none",overflow:"auto",maxHeight:`${t-10}px`,marginRight:"0px"}}):a.jsx(W,{sx:{padding:"20px"},children:"No result. Select a table to see its definition."})}),[n,T,t]);return a.jsx(c,{mt:"5px",children:a.jsxs(q,{index:E,variant:"enclosed",onChange:o,size:"sm",children:[a.jsxs(Y,{children:[a.jsx(F,{children:"Results"}),a.jsx(F,{children:"Table"})]}),a.jsxs(K,{sx:{minWidth:"1010px",maxWidth:"1010px",maxHeight:`${t}px`,overflow:"hidden"},children:[a.jsx(_,{sx:{padding:"0px",marginTop:"10px"},children:S}),a.jsx(_,{sx:{padding:"0px",marginTop:"10px"},children:d})]})]})})},k=["IF EXISTS","IF NOT EXISTS","OR REPLACE","MATERIALIZED VIEW","GRANT OPTION","WITH GRANT OPTION","WITH ADMIN OPTION","ADMIN OPTION FOR","GRANT OPTION FOR","SESSION AUTHORIZATION","RESET SESSION","START TRANSACTION","FAST FORWARD TO","SET AUTHORIZATION","SET PROPERTIES","SET DATA TYPE","DROP NOT NULL","ADD COLUMN","DROP COLUMN","RENAME COLUMN","ALTER COLUMN","RENAME TO","ORDER BY","GROUP BY","PARTITION BY","WITH TIES","WITH DATA","NO DATA","ALL PRIVILEGES","GRACE PERIOD","TIME ZONE","ROLE GRANTS","CREATE FUNCTION","CREATE MATERIALIZED VIEW","CREATE SCHEMA","CREATE TABLE","CREATE VIEW"],H={ALTER:{targets:["BRANCH","MATERIALIZED VIEW","SCHEMA","TABLE","VIEW"],BRANCH:{modifiers:[],requiresName:!0,afterName:["IN TABLE","FAST FORWARD TO"]},"MATERIALIZED VIEW":{modifiers:["IF EXISTS"],requiresName:!0,afterName:["RENAME TO","SET PROPERTIES","SET AUTHORIZATION"]},SCHEMA:{modifiers:[],requiresName:!0,afterName:["RENAME TO","SET AUTHORIZATION"]},TABLE:{modifiers:["IF EXISTS"],requiresName:!0,afterName:["RENAME TO","ADD COLUMN","DROP COLUMN","RENAME COLUMN","ALTER COLUMN","SET AUTHORIZATION","SET PROPERTIES","EXECUTE"]},VIEW:{modifiers:[],requiresName:!0,afterName:["RENAME TO","REFRESH","SET AUTHORIZATION"]}},CREATE:{targets:["BRANCH","CATALOG","FUNCTION","MATERIALIZED VIEW","ROLE","SCHEMA","TABLE","VIEW"],BRANCH:{modifiers:["OR REPLACE","IF NOT EXISTS"],requiresName:!0,afterName:["WITH","IN TABLE","FROM"]},CATALOG:{modifiers:[],requiresName:!0,afterName:["USING","WITH"]},FUNCTION:{modifiers:["OR REPLACE"],requiresName:!0,afterName:[]},"MATERIALIZED VIEW":{modifiers:["OR REPLACE","IF NOT EXISTS"],requiresName:!0,afterName:["GRACE PERIOD","COMMENT","WITH","AS"]},ROLE:{modifiers:[],requiresName:!0,afterName:["WITH ADMIN","IN"]},SCHEMA:{modifiers:["IF NOT EXISTS"],requiresName:!0,afterName:["AUTHORIZATION","WITH"]},TABLE:{modifiers:["OR REPLACE","IF NOT EXISTS"],requiresName:!0,afterName:["(","COMMENT","WITH","AS"]},VIEW:{modifiers:["OR REPLACE"],requiresName:!0,afterName:["COMMENT","SECURITY","AS"]}},DROP:{targets:["BRANCH","CATALOG","FUNCTION","MATERIALIZED VIEW","ROLE","SCHEMA","TABLE","VIEW"],BRANCH:{modifiers:["IF EXISTS"],requiresName:!0,afterName:["IN TABLE"]},CATALOG:{modifiers:[],requiresName:!0,afterName:[]},FUNCTION:{modifiers:["IF EXISTS"],requiresName:!0,afterName:[]},"MATERIALIZED VIEW":{modifiers:["IF EXISTS"],requiresName:!0,afterName:[]},ROLE:{modifiers:["IF EXISTS"],requiresName:!0,afterName:["IN"]},SCHEMA:{modifiers:["IF EXISTS"],requiresName:!0,afterName:["CASCADE","RESTRICT"]},TABLE:{modifiers:["IF EXISTS"],requiresName:!0,afterName:[]},VIEW:{modifiers:["IF EXISTS"],requiresName:!0,afterName:[]}},SHOW:{targets:["BRANCHES","CATALOGS","COLUMNS","CREATE FUNCTION","CREATE MATERIALIZED VIEW","CREATE SCHEMA","CREATE TABLE","CREATE VIEW","FUNCTIONS","GRANTS","ROLE GRANTS","ROLES","SCHEMAS","SESSION","STATS","TABLES"],afterTarget:{BRANCHES:["FROM","IN"],CATALOGS:["LIKE"],COLUMNS:["FROM"],"CREATE FUNCTION":[],"CREATE MATERIALIZED VIEW":[],"CREATE SCHEMA":[],"CREATE TABLE":[],"CREATE VIEW":[],FUNCTIONS:["FROM","LIKE"],GRANTS:["ON"],"ROLE GRANTS":["FROM"],ROLES:["FROM"],SCHEMAS:["FROM","LIKE"],SESSION:["LIKE"],STATS:["FOR"],TABLES:["FROM","LIKE"]}},SELECT:{clauses:["FROM","WHERE","GROUP BY","HAVING","WINDOW","UNION","INTERSECT","EXCEPT","ORDER BY","OFFSET","LIMIT","FETCH"]},INSERT:{requiresInto:!0,afterTable:["(","VALUES","SELECT"]},UPDATE:{requiresTable:!0,afterTable:["SET"]},DELETE:{requiresFrom:!0,afterFrom:["WHERE"]},GRANT:{targets:["privilege","role"],afterPrivilege:["ON"],afterOn:["BRANCH","TABLE","SCHEMA"],afterTarget:["TO"],afterTo:["WITH GRANT OPTION"]},REVOKE:{modifiers:["GRANT OPTION FOR","ADMIN OPTION FOR"],targets:["privilege","role"],afterPrivilege:["ON"],afterOn:["BRANCH","TABLE","SCHEMA"],afterTarget:["FROM"]},SET:{targets:["PATH","ROLE","SESSION","TIME ZONE"],afterTarget:{ROLE:["IN"]}},USE:{requiresCatalogOrSchema:!0},EXPLAIN:{modifiers:["ANALYZE"],afterModifier:["VERBOSE"]},DESCRIBE:{targets:["INPUT","OUTPUT"]},MATCH_RECOGNIZE:{targets:["("],afterTarget:{"(":["PARTITION BY","ORDER BY","MEASURES","AFTER MATCH","PATTERN","SUBSET","DEFINE"]}},MERGE:{requiresInto:!0,afterTable:["USING"]},PREPARE:{requiresName:!0,afterName:["FROM"]},REFRESH:{targets:["MATERIALIZED VIEW"]},RESET:{targets:["SESSION","SESSION AUTHORIZATION"]},ROLLBACK:{},START:{targets:["TRANSACTION"]},TRUNCATE:{targets:["TABLE"]},VALUES:{}},Q=["ALTER","ANALYZE","CALL","COMMENT","COMMIT","CREATE","DEALLOCATE","DELETE","DENY","DESCRIBE","DROP","EXECUTE","EXPLAIN","GRANT","INSERT","MATCH_RECOGNIZE","MERGE","PREPARE","REFRESH","RESET","REVOKE","ROLLBACK","SELECT","SET","SHOW","START","TRUNCATE","UPDATE","USE","VALUES"];function ue(e){const n=e.toUpperCase(),t=[];let r=0;for(;r<e.length;){if(/\s/.test(e[r])){r+=1;continue}let E=!1;for(let o=0;o<k.length;o+=1){const m=k[o];if(n.substring(r,r+m.length)===m){const i=e[r+m.length];(!i||/\s/.test(i))&&(t.push(m),r+=m.length,E=!0)}}if(!E)if(/[a-zA-Z_]/.test(e[r])){let o="";for(;r<e.length&&/[a-zA-Z0-9_.]/.test(e[r]);)o+=e[r],r+=1;t.push(o.toUpperCase())}else if(/[0-9]/.test(e[r])){let o="";for(;r<e.length&&/[0-9.]/.test(e[r]);)o+=e[r],r+=1;t.push(o)}else t.push(e[r]),r+=1}return t}function Ce(e){var r,E,o;const n={command:null,target:null,modifiers:[],identifier:null,action:null,position:"start",tokens:e,lastToken:e[e.length-1]||"",qualifiedName:{}};if(e.length===0)return n;const t=e[0];if(Q.includes(t)&&(n.command=t,n.position="command"),e.length===1)return n;if(n.command&&H[n.command]){const m=H[n.command];let i=1;for(;i<e.length;){const T=e[i],l=n.target?m[n.target]:void 0;if((r=l==null?void 0:l.modifiers)!=null&&r.includes(T)||(E=m.modifiers)!=null&&E.includes(T))n.modifiers.push(T),i+=1;else break}if(i<e.length&&m.targets){const T=e[i];m.targets.includes(T)&&(n.target=T,n.position="target",i+=1)}if(i<e.length){const T=e[i];if(T.includes(".")){const l=T.split(".");l.length===3?(n.qualifiedName.catalog=l[0],n.qualifiedName.schema=l[1],n.qualifiedName.table=l[2]):l.length===2&&(n.qualifiedName.catalog=l[0],n.qualifiedName.schema=l[1]),n.identifier=T,n.position="identifier",i+=1}else(!/^[A-Z]+$/.test(T)||T.length<3)&&(n.identifier=T,n.position="identifier",i+=1)}if(i<e.length&&n.identifier&&n.target){const T=e[i],l=m[n.target];(o=l==null?void 0:l.afterName)!=null&&o.includes(T)&&(n.action=T,n.position="action")}}return n}function xe(e,n){if(e.target==="CATALOG")return"CATALOG";if(e.target==="SCHEMA")return"SCHEMA";if(e.target==="TABLE"||e.target==="VIEW"||e.target==="MATERIALIZED VIEW")return"TABLE";switch(e.lastToken){case"FROM":case"INTO":case"JOIN":return"TABLE";case"CATALOG":return"CATALOG";case"SCHEMA":return"SCHEMA";case"TABLE":case"VIEW":return"TABLE";default:return null}}function he(e,n,t,r,E){if(!e.command||e.position==="start")return Q.map(i=>({label:i,type:"keyword",apply:i}));const o=H[e.command];if(!o)return[];if(e.position==="command"&&!e.target&&o.targets)return o.targets.map(i=>({label:i,type:"keyword",apply:i}));const m=xe(e);if(m&&!e.identifier)return N(m,n,t,r);if(e.position==="target"&&e.target&&!e.identifier&&o.afterTarget&&typeof o.afterTarget=="object"&&!Array.isArray(o.afterTarget)){const i=e.target?o.afterTarget[e.target]:void 0;if(Array.isArray(i))return i.map(l=>({label:l,type:"keyword",apply:l}));if(i!=null&&i.modifiers&&i.modifiers.length>0){const S=i.modifiers.filter(s=>!e.modifiers.includes(s)).map(s=>({label:s,type:"keyword",apply:s})),d=e.target==="CATALOG"?"CATALOG":e.target==="SCHEMA"?"SCHEMA":"TABLE";return[...S,...N(d,n,t,r)]}const T=e.target==="CATALOG"?"CATALOG":e.target==="SCHEMA"?"SCHEMA":"TABLE";return N(T,n,t,r)}if(e.position==="identifier"&&e.identifier&&!e.action){const i=o[e.target||""];if(i!=null&&i.afterName)return i.afterName.map(T=>({label:T,type:"keyword",apply:T}))}if(e.command==="SELECT")return ge(e,n,t,r,E);if(e.command==="INSERT")return Me(e,n,t,r);if(e.command==="MERGE")return He(e,n,t,r);if(e.command==="SHOW")return be(e,n,t,r);if(e.command==="USE")return Ue(e,n,t);if(e.command==="ANALYZE"&&!e.identifier)return N("TABLE",n,t,r);if(e.command==="DESCRIBE"){if(!e.target)return[...o.targets||[]].map(i=>({label:i,type:"keyword",apply:i}));if(!e.identifier)return N("TABLE",n,t,r)}return e.command==="TRUNCATE"&&e.lastToken==="TABLE"||e.command==="UPDATE"&&!e.identifier||e.command==="DELETE"&&e.lastToken==="FROM"||e.command==="REFRESH"&&e.lastToken==="VIEW"?N("TABLE",n,t,r):[]}function N(e,n,t,r){return e==="CATALOG"?n.map(E=>({label:E,type:"catalog",apply:E})):e==="SCHEMA"?t&&t.length>0?t.map(E=>({label:E,type:"schema",apply:E})):n.map(E=>({label:E,type:"catalog",apply:E})):e==="TABLE"||e==="VIEW"||e==="MATERIALIZED VIEW"?r&&r.length>0?r.map(E=>({label:E,type:"table",apply:E})):t&&t.length>0?t.map(E=>({label:E,type:"schema",apply:E})):n.map(E=>({label:E,type:"catalog",apply:E})):[]}function ge(e,n,t,r,E){var T;const o=e.tokens.includes("FROM");return o?o&&e.lastToken==="FROM"?N("TABLE",n,t,r):(((T=H.SELECT.clauses)==null?void 0:T.filter(l=>!e.tokens.includes(l)))||[]).map(l=>({label:l,type:"keyword",apply:l})):[{label:"*",type:"keyword",apply:"*"},...E.map(l=>({label:l,type:"column",apply:l})),{label:"FROM",type:"keyword",apply:"FROM"}]}function Me(e,n,t,r,E){const o=e.tokens.includes("INTO");return o?o&&e.lastToken==="INTO"?N("TABLE",n,t,r):e.identifier?[{label:"VALUES",type:"keyword",apply:"VALUES"},{label:"SELECT",type:"keyword",apply:"SELECT"},{label:"(",type:"keyword",apply:"("}]:[]:[{label:"INTO",type:"keyword",apply:"INTO"}]}function He(e,n,t,r,E){const o=e.tokens.includes("INTO");return o?o&&e.lastToken==="INTO"?N("TABLE",n,t,r):e.identifier?[{label:"VALUES",type:"keyword",apply:"VALUES"},{label:"SELECT",type:"keyword",apply:"SELECT"},{label:"(",type:"keyword",apply:"("}]:[]:[{label:"INTO",type:"keyword",apply:"INTO"}]}function be(e,n,t,r){const E=H.SHOW;if(!e.target)return(E.targets||[]).map(o=>({label:o,type:"keyword",apply:o}));if(e.lastToken==="FROM"||e.lastToken==="IN")return e.tokens.includes("TABLE")?N("TABLE",n,t,r):N("SCHEMA",n,t,r);if(e.lastToken==="FOR")return N("TABLE",n,t,r);if(E.afterTarget&&typeof E.afterTarget=="object"&&!Array.isArray(E.afterTarget)){const o=e.target?E.afterTarget[e.target]:void 0;if(o)return o.map(m=>({label:m,type:"keyword",apply:m}))}return[]}function Ue(e,n,t){return t&&t.length>0?t.map(r=>({label:r,type:"schema",apply:r})):n.map(r=>({label:r,type:"catalog",apply:r}))}const ye=(e,n,t,r)=>E=>{const o=E.state.sliceDoc(0,E.pos),m=E.matchBefore(/[\w.]*/);if(!m)return null;const i=ue(o),T=Ce(i),l=he(T,e,n,t,r);return{from:m.from,options:l}},Fe=(e,n)=>e.toUpperCase().startsWith("SELECT")&&!e.toUpperCase().includes("LIMIT")?`${e} LIMIT ${n}`:e,_e=({catalog:e,schema:n,table:t,catalogs:r,schemas:E,tables:o,columns:m,setBusy:i})=>{const T=z(),{id:l}=w(),[S,d]=A.useState("SELECT * ..."),[s,I]=A.useState(10),O=A.useRef(null),[R,u]=A.useState(200),{colorMode:p}=P(),[U,b]=me(),{postStatement:f,queryResult:J,isLoading:x}=$();Oe(x),de(x,"You have a running query. Are you sure you want to leave?"),A.useEffect(()=>{if(!O.current)return()=>{};const L=new ResizeObserver(y=>{const ne=y[0];u(890-ne.contentRect.height)});return L.observe(O.current),()=>L.disconnect()},[]),A.useEffect(()=>{d(t?`SELECT * FROM ${t} LIMIT ${s}`:"SELECT * ...")},[t,s]),A.useEffect(()=>{i&&i(x)},[x,i]);const ee=A.useCallback(L=>{if(l){const y=L.endsWith(";")?L.substring(0,L.length-1):L;f({pId:l,sql:Fe(y,s),catalog:e,schema:n})}},[l,e,n,s,f]);return a.jsxs(a.Fragment,{children:[a.jsx(Ne,{isOpen:U,handleToggle:b}),a.jsxs(c,{flexDirection:"column",gap:"0px",flex:"1 1 0%",children:[a.jsx(c,{sx:{resize:"vertical",marginBottom:"0px"},ref:O,children:a.jsx(B,{mode:"sql",readOnly:!1,labelProps:{align:"top"},value:S,onChange:L=>{d(L)},codeMirrorProps:{theme:`${p}`,maxHeight:"680px",minHeight:"380px"},autoCompleteOverrides:[ye(r||[],E||[],o||[],m||[])]})}),a.jsxs(c,{flexDirection:"column",children:[a.jsxs(c,{flexDirection:"row",sx:{maxHeight:"35px"},bg:T.colors.white,mt:"-6px",justifyContent:"space-between",gap:"20px",children:[a.jsx(c,{flexDirection:"column",children:a.jsxs(g,{sx:{maxHeight:"30px"},ml:"5px",onClick:()=>ee(S.trim()),isLoading:x,children:[a.jsx(M,{icon:C.play,color:"white",size:"sm",mr:"5px"}),"Run"]})}),a.jsx(c,{flexDirection:"column",children:a.jsxs(c,{flexDirection:"row",gap:"10px",children:[a.jsx(c,{mt:"5px",children:a.jsx(g,{intent:"inline",padding:"0px",sx:{maxHeight:"25px"},href:"#",onClick:b,children:a.jsx(h,{mainText:"Help",children:a.jsx(M,{icon:C.interrogation,size:"md"})})})}),a.jsx(D,{onChange:L=>{I(+L)},options:["10","100","1000","10000","100000"],value:`${s}`,placeholder:"limit",variant:"white",label:"Limit",labelPosition:"inline"})]})})]}),a.jsx(c,{overflow:"hidden",children:a.jsx(Le,{result:J,isLoading:x,maxHeight:R})})]})]})]})},De=({setBusy:e})=>{const{id:n}=w(),[t,r]=A.useState(),[E,o]=A.useState(),[m,i]=A.useState(),[T,l]=A.useState(""),[S,d]=A.useState(""),[s,I]=A.useState(""),{postStatement:O,queryResult:R,isLoading:u}=$();A.useEffect(()=>{n&&T===""&&S===""?(O({pId:n,sql:"SHOW CATALOGS",catalog:T,schema:S}),r(void 0)):n&&T!==""&&S===""?(O({pId:n,sql:"SHOW SCHEMAS",catalog:T,schema:S}),o(void 0)):n&&T!==""&&S!==""&&(O({pId:n,sql:"SHOW TABLES",catalog:T,schema:S}),i(void 0))},[n,T,S,O,r,o,i]),A.useEffect(()=>{if(R!=null&&R.data&&R!=null&&R.columns&&(R==null?void 0:R.columns.length)>0)switch(R==null?void 0:R.columns[0].name){case"Catalog":r(R==null?void 0:R.data),o(void 0),i(void 0);break;case"Schema":o(R==null?void 0:R.data),i(void 0);break;case"Table":i(R==null?void 0:R.data);break}},[R,r,o,i]);const p=A.useCallback(()=>{n&&O({pId:n,sql:"SHOW CATALOGS",catalog:void 0,schema:void 0})},[n,O]),U=A.useCallback(()=>{n&&T!==""&&O({pId:n,sql:"SHOW SCHEMAS",catalog:T,schema:void 0})},[n,T,O]),b=A.useCallback(()=>{n&&T!==""&&S!==""&&O({pId:n,sql:"SHOW TABLES",catalog:T,schema:S})},[n,T,S,O]);return a.jsxs(c,{flexDirection:"row",width:"100%",overflow:"hidden",children:[a.jsx(c,{flexDirection:"column",width:"18%",children:a.jsx(Ie,{catalogs:((t==null?void 0:t.flat())||[]).map(f=>({label:f,value:f})),schemas:((E==null?void 0:E.flat())||[]).map(f=>({label:f,value:f})),tables:(m==null?void 0:m.flat())||[],isLoading:u,onCatalogSelect:f=>{d(""),I(""),l(f)},onSchemaSelect:f=>{I(""),d(f)},onTableSelect:I,refreshCatalogs:p,refreshSchemas:U,refreshTables:b})}),a.jsx(c,{flexDirection:"column",width:"82%",sx:{minWidth:"1010px",maxWidth:"1010px"},children:a.jsx(_e,{catalog:T,schema:S,table:s,catalogs:t==null?void 0:t.flat(),schemas:E==null?void 0:E.flat(),tables:m==null?void 0:m.flat(),setBusy:e})})]})},Pe=()=>{Se(Re.queryEngine);const{id:e}=w();return a.jsx(c,{height:"90%",overflow:"hidden",mb:"5px",children:a.jsx(ce,{defaultTitle:"Query",maxNumberOfTabs:11,children:a.jsx(De,{})},e)})};export{Pe as default};
