/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{SqlError as e}from"./errorSupport.js";function t(e){return r(e).trim()}function r(t){let s,o,u,l;switch(t.type){case"interval":return a(r(t.value),t.qualifier,t.op);case"case-expression":{let e="CASE ";"simple"===t.format&&(e+=r(t.operand));for(let n=0;n<t.clauses.length;n++)e+=` WHEN ${r(t.clauses[n].operand)} THEN ${r(t.clauses[n].value)}`;return null!==t.else&&(e+=` ELSE ${r(t.else)}`),e+=" END",e}case"expression-list":o=[];for(const e of t.value)o.push(r(e));return o;case"unary-expression":return`(NOT ${r(t.expr)})`;case"binary-expression":switch(t.operator){case"AND":return`(${r(t.left)} AND ${r(t.right)})`;case"OR":return`(${r(t.left)} OR ${r(t.right)})`;case"IS":if("null"!==t.right.type)throw new e("UnsupportedIsRhs");return`(${r(t.left)} IS NULL)`;case"ISNOT":if("null"!==t.right.type)throw new e("UnsupportedIsRhs");return`(${r(t.left)} IS NOT NULL)`;case"IN":return s=[],"expression-list"===t.right.type?(s=r(t.right),`(${r(t.left)} IN (${s.join(",")}))`):(l=r(t.right),Array.isArray(l)?`(${r(t.left)} IN (${l.join(",")}))`:`(${r(t.left)} IN (${l}))`);case"NOT IN":return s=[],"expression-list"===t.right.type?(s=r(t.right),`(${r(t.left)} NOT IN (${s.join(",")}))`):(l=r(t.right),Array.isArray(l)?`(${r(t.left)} NOT IN (${l.join(",")}))`:`(${r(t.left)} NOT IN (${l}))`);case"BETWEEN":case"NOTBETWEEN":return u=r(t.right),`(${r(t.left)} ${t.operator} ${u[0]} AND ${u[1]})`;case"LIKE":return""!==t.escape?`(${r(t.left)} LIKE ${r(t.right)} ESCAPE '${t.escape}')`:`(${r(t.left)} LIKE ${r(t.right)})`;case"NOT LIKE":return""!==t.escape?`(${r(t.left)} NOT LIKE ${r(t.right)} ESCAPE '${t.escape}')`:`(${r(t.left)} NOT LIKE ${r(t.right)})`;case"<>":case"<":case">":case">=":case"<=":case"=":case"*":case"-":case"+":case"/":case"||":return`(${r(t.left)} ${t.operator} ${r(t.right)})`;default:throw new e("UnsupportedOperator",{operator:t.operator})}case"null":return"null";case"boolean":return!0===t.value?"1":"0";case"string":return`'${t.value.toString().replaceAll("'","''")}'`;case"timestamp":return`timestamp '${t.value}'`;case"date":return`date '${t.value}'`;case"time":return`time '${t.value}'`;case"number":return t.value.toString();case"current-time":return n(t.mode);case"current-user":return"CURRENT_USER";case"column-reference":return c(t);case"data-type":return t.value;case"function":{const e=r(t.args);return i(t.name,e)}}throw new e("UnsupportedSyntax",{node:t.type})}function n(e){return"date"===e?"CURRENT_DATE":"time"===e?"CURRENT_TIME":"CURRENT_TIMESTAMP"}function a(e,t,r){let n="";return n="interval-period"===t.type?s(t):s(t.start)+" TO "+s(t.end),`INTERVAL${""!==r?r+" ":""} ${e} ${n}`}function s(e){return e.period.toUpperCase()}function c(e){return!0===e.delimited?`"${e.column.split('"').join('""')}"`:e.column}function i(t,r){switch(t.toLowerCase().trim()){case"cos":case"sin":case"tan":case"cosh":case"tanh":case"sinh":case"acos":case"asin":case"atan":case"floor":case"log10":case"log":case"abs":if(1!==r.length)throw new e("InvalidFunctionParameters",{function:t.toLowerCase().trim()});return`${t.toUpperCase().trim()}(${r[0]})`;case"ceiling":case"ceil":if(1!==r.length)throw new e("InvalidFunctionParameters",{function:"ceiling"});return"CEILING("+r[0]+")";case"mod":case"power":case"nullif":if(2!==r.length)throw new e("InvalidFunctionParameters",{function:t.toLowerCase().trim()});return`${t.toUpperCase().trim()}(${r[0]},${r[1]})`;case"round":if(2===r.length)return"ROUND("+r[0]+","+r[1]+")";if(1===r.length)return"ROUND("+r[0]+")";throw new e("InvalidFunctionParameters",{function:"round"});case"truncate":if(r.length<1||r.length>2)throw new e("InvalidFunctionParameters",{function:"truncate"});return"TRUNCATE("+r[0]+(1===r.length?")":","+r[1]+")");case"char_length":case"len":if(1!==r.length)throw new e("InvalidFunctionParameters",{function:"char_length"});return"CHAR_LENGTH("+r[0]+")";case"coalesce":case"concat":{if(r.length<1)throw new e("InvalidFunctionParameters",{function:t.toLowerCase()});let n=t.toUpperCase().trim()+"(";for(let e=0;e<r.length;e++)0!==e&&(n+=","),n+=r[e];return n+=")",n}case"lower":case"lcase":if(1!==r.length)throw new e("InvalidFunctionParameters",{function:"lower"});return"LOWER("+r[0]+")";case"upper":case"ucase":if(1!==r.length)throw new e("InvalidFunctionParameters",{function:"upper"});return"UPPER("+r[0]+")";case"substring":{let e="SUBSTRING("+r[0]+" FROM "+r[1];return 3===r.length&&(e+=" FOR "+r[2]),e+=")",e}case"extract":return"EXTRACT("+r[0].replaceAll("'","")+" FROM "+r[1]+")";case"cast":{let e="";switch(r[1].type){case"date":e="DATE";break;case"float":e="FLOAT";break;case"integer":e="INTEGER";break;case"real":e="REAL";break;case"smallint":e="SMALLINT";break;case"timestamp":e="TIMESTAMP";break;case"varchar":e="VARCHAR("+r[1].size.toString()+")"}return`CAST(${r[0]} AS ${e})`}}throw new e("InvalidFunctionParameters",{function:t})}export{t as sqlNodeToString,i as translateFunctionToDatabaseSpecific};
