UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 13.4 kB
import e from"../../../Graphic.js";import{ArcadeDate as t}from"../../ArcadeDate.js";import{cloneGeometry as r}from"../../kernel.js";import{SqlError as s,SqlErrorCodes as a,FeatureSetError as i,FeatureSetErrorCodes as l}from"../support/errorsupport.js";import n from"../support/FeatureSet.js";import o from"../support/IdSet.js";import{cloneField as h,FeatureServiceDatabaseType as u,layerGeometryEsriConstants as c}from"../support/shared.js";import{reformulateWithoutField as d,toWhereClause as p,translateFunctionToDatabaseSpecific as f,makeToday as g,makeDateString as N,arcadeDateToSqlString as _,convertIntervalToSql as S,combine as w,scanForField as m}from"../support/sqlUtils.js";import{unwrapOrThrow as v,assertIsSome as C}from"../../../core/maybe.js";import{WhereClause as T}from"../../../core/sql/WhereClause.js";import F from"../../../geometry/SpatialReference.js";class W{constructor(e){this.field=e,this.sqlRewritable=!1}postInitialization(e,t){}}class E extends W{constructor(e){super(e),this.sqlRewritable=!0}extractValue(e){return e.attributes[this.field.name]}rewriteSql(e){return{rewritten:this.sqlRewritable,where:e}}}class x extends W{constructor(e,t,r){super(h(e)),this.originalField=e,this.sqlRewritable=!0,this.field.name=t,this.field.alias=r}rewriteSql(e,t){return{rewritten:this.sqlRewritable,where:d(e,this.field.name,this.originalField.name,t.getFieldsIndex())}}extractValue(e){return e.attributes[this.originalField.name]}}class A extends W{constructor(e,t,r){super(e),this.codefield=t,this.lkp=r,this.reverseLkp={};for(const s in r)this.reverseLkp[r[s]]=s;this.sqlRewritable=!0}rewriteSql(e,t){const r=this.evaluateNodeToWhereClause(e.parseTree,u.Standardised,this.field.name,this.codefield instanceof T?p(this.codefield,u.Standardised):this.codefield,e.parameters);return r.includes(A.BADNESS)?{rewritten:!1,where:e}:{rewritten:this.sqlRewritable,where:T.create(r,v(t._parent).getFieldsIndex())}}evaluateNodeToWhereClause(e,r,i=null,l=null,n){let o,h,u,c;switch(e.type){case"interval":return S(this.evaluateNodeToWhereClause(e.value,r,i,l,n),e.qualifier,e.op);case"case-expression":{let t=" CASE ";"simple"===e.format&&(t+=this.evaluateNodeToWhereClause(e.operand,r,i,A.BADNESS,n));for(let s=0;s<e.clauses.length;s++)t+=" WHEN "+this.evaluateNodeToWhereClause(e.clauses[s].operand,r,i,A.BADNESS,n)+" THEN "+this.evaluateNodeToWhereClause(e.clauses[s].value,r,i,A.BADNESS,n);return null!==e.else&&(t+=" ELSE "+this.evaluateNodeToWhereClause(e.else,r,i,A.BADNESS,n)),t+=" END ",t}case"parameter":{const s=n[e.value.toLowerCase()];if("string"==typeof s)return"'"+s.toString().replace(/'/g,"''")+"'";if(s instanceof Date)return N(s,r,null);if(s instanceof t)return _(s,r,null);if(s instanceof Array){const e=[];for(let a=0;a<s.length;a++)"string"==typeof s[a]?e.push("'"+s[a].toString().replace(/'/g,"''")+"'"):s[a]instanceof Date?e.push(N(s[a],r,null)):s[a]instanceof t?e.push(_(s[a],r,null)):e.push(s[a].toString());return e}return s.toString()}case"expression-list":h=[];for(const t of e.value)h.push(this.evaluateNodeToWhereClause(t,r,i,l,n));return h;case"unary-expression":return" ( NOT "+this.evaluateNodeToWhereClause(e.expr,r,i,A.BADNESS,n)+" ) ";case"binary-expression":switch(e.operator){case"AND":return" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" AND "+this.evaluateNodeToWhereClause(e.right,r,i,l,n)+") ";case"OR":return" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" OR "+this.evaluateNodeToWhereClause(e.right,r,i,l,n)+") ";case"IS":if("null"!==e.right.type)throw new s(a.UnsupportedIsRhs);return" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" IS NULL )";case"ISNOT":if("null"!==e.right.type)throw new s(a.UnsupportedIsRhs);return" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" IS NOT NULL )";case"IN":if(o=[],"expression-list"===e.right.type){if("column-reference"===e.left.type&&e.left.column.toUpperCase()===this.field.name.toUpperCase()){const t=[];let s=!0;for(const r of e.right.value){if("string"!==r.type){s=!1;break}if(void 0===this.lkp[r.value]){s=!1;break}t.push(this.lkp[r.value].toString())}if(s)return" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" IN ("+t.join(",")+")) "}return o=this.evaluateNodeToWhereClause(e.right,r,i,l,n)," ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" IN ("+o.join(",")+")) "}return c=this.evaluateNodeToWhereClause(e.right,r,i,l,n),c instanceof Array?" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" IN ("+c.join(",")+")) ":" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" IN ("+c+")) ";case"NOT IN":if(o=[],"expression-list"===e.right.type){if("column-reference"===e.left.type&&e.left.column.toUpperCase()===this.field.name.toUpperCase()){const t=[];let s=!0;for(const r of e.right.value){if("string"!==r.type){s=!1;break}if(void 0===this.lkp[r.value]){s=!1;break}t.push(this.lkp[r.value].toString())}if(s)return" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" NOT IN ("+t.join(",")+")) "}return o=this.evaluateNodeToWhereClause(e.right,r,i,l,n)," ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" NOT IN ("+o.join(",")+")) "}return c=this.evaluateNodeToWhereClause(e.right,r,i,l,n),c instanceof Array?" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" NOT IN ("+c.join(",")+")) ":" ("+this.evaluateNodeToWhereClause(e.left,r,i,l,n)+" NOT IN ("+c+")) ";case"BETWEEN":return u=this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)," ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" BETWEEN "+u[0]+" AND "+u[1]+" ) ";case"NOTBETWEEN":return u=this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)," ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" NOT BETWEEN "+u[0]+" AND "+u[1]+" ) ";case"LIKE":return""!==e.escape?" ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" LIKE "+this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)+" ESCAPE '"+e.escape+"') ":" ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" LIKE "+this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)+") ";case"NOT LIKE":return""!==e.escape?" ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" NOT LIKE "+this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)+" ESCAPE '"+e.escape+"') ":" ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" NOT LIKE "+this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)+") ";case"<>":case"=":if("column-reference"===e.left.type&&"string"===e.right.type){if(e.left.column.toUpperCase()===this.field.name.toUpperCase()&&void 0!==this.lkp[e.right.value.toString()])return" ("+l+" "+e.operator+" "+this.lkp[e.right.value.toString()].toString()+") "}else if("column-reference"===e.right.type&&"string"===e.left.type&&e.right.column.toUpperCase()===this.field.name.toUpperCase())return" ("+this.lkp[e.right.value.toString()].toString()+" "+e.operator+" "+l+") ";return" ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" "+e.operator+" "+this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)+") ";case"<":case">":case">=":case"<=":case"*":case"-":case"+":case"/":case"||":return" ("+this.evaluateNodeToWhereClause(e.left,r,i,A.BADNESS,n)+" "+e.operator+" "+this.evaluateNodeToWhereClause(e.right,r,i,A.BADNESS,n)+") "}case"null":return"null";case"boolean":return!0===e.value?"1":"0";case"string":return"'"+e.value.toString().replace(/'/g,"''")+"'";case"timestamp":case"date":return N(e.value,r,null);case"number":return e.value.toString();case"current-time":return g("date"===e.mode,r);case"column-reference":return i&&i.toLowerCase()===e.column.toLowerCase()?"("+l+")":e.column;case"data-type":return e.value;case"function":{const t=this.evaluateNodeToWhereClause(e.args,r,i,A.BADNESS,n);return f(e.name,t,r)}}throw new s(a.UnsupportedSyntax,{node:e.type})}extractValue(e){return this.codefield instanceof T?this.reverseLkp[this.codefield.calculateValueCompiled(e)]:this.reverseLkp[e.attributes[this.codefield]]}}A.BADNESS="_!!!_BAD_LKP_!!!!";class y extends W{constructor(e,t){super(e),this._sql=t}rewriteSql(e,t){return{rewritten:!0,where:d(e,this.field.name,p(this._sql,u.Standardised),t.getFieldsIndex())}}extractValue(e){return this._sql.calculateValueCompiled(e)}}class D extends n{static findField(e,t){for(const r of e)if(r.name.toLowerCase()===t.toString().toLowerCase())return r;return null}constructor(e){super(e),this._calcFunc=null,this.declaredClass="esri.arcade.featureset.actions.Adapted",this.adaptedFields=[],this._extraFilter=null,this._extraFilter=e.extraFilter,this._parent=e.parentfeatureset,this._maxProcessing=30,this.adaptedFields=e.adaptedFields}_initialiseFeatureSet(){null!==this._parent?(this.geometryType=this._parent.geometryType,this.objectIdField=this._parent.objectIdField,this.globalIdField=this._parent.globalIdField,this.spatialReference=this._parent.spatialReference,this.hasM=this._parent.hasM,this.hasZ=this._parent.hasZ,this.typeIdField=this._parent.typeIdField,this.types=this._parent.types):(this.spatialReference=new F({wkid:4326}),this.objectIdField="",this.globalIdField="",this.geometryType=c.point,this.typeIdField="",this.types=null),this.fields=[];for(const e of this.adaptedFields)e.postInitialization(this,this._parent),this.fields.push(e.field)}async _getSet(e){if(null===this._wset){await this._ensureLoaded();let t=null;return t=this._extraFilter?await this._getFilteredSet("",null,null,null,e):await(this._parent?._getSet(e)),this._checkCancelled(e),C(t),this._wset=new o(t._candidates.slice(0),t._known.slice(0),t._ordered,this._clonePageDefinition(t.pagesDefinition)),this._wset}return this._wset}_isInFeatureSet(e){return v(this._parent)._isInFeatureSet(e)}async _getFeatures(t,s,a,i){const l=[];-1!==s&&void 0===this._featureCache[s]&&l.push(s);const n=this._maxQueryRate();if(!0===this._checkIfNeedToExpandKnownPage(t,n))return await this._expandPagedSet(t,n,0,0,i),this._getFeatures(t,s,a,i);let h=0;for(let e=t._lastFetchedIndex;e<t._known.length&&(h++,h<=a&&(t._lastFetchedIndex+=1),!(void 0===this._featureCache[t._known[e]]&&(t._known[e]!==s&&l.push(t._known[e]),l.length>=n)));e++);if(0===l.length)return"success";t=new o([],l,t._ordered,null);const u=Math.min(l.length,a);await(this._parent?._getFeatures(t,-1,u,i)),this._checkCancelled(i);const c=[];for(let e=0;e<u;e++){const t=this._parent?._featureFromCache(l[e]);void 0!==t&&c.push({geometry:t.geometry,attributes:t.attributes,id:l[e]})}for(const o of c){const t=[];for(const e of this.adaptedFields)t[e.field.name]=e.extractValue(o);this._featureCache[o.id]=new e({attributes:t,geometry:r(o.geometry)})}return"success"}async _fetchAndRefineFeatures(){throw new i(l.NeverReach)}async _getFilteredSet(e,t,r,s,a){let i=!1;const l=this._reformulateWithoutAdaptions(r);i=l.cannot,r=l.where;let n=!1;if(null!==s){n=!0;const e=[];for(const t of this.adaptedFields)if(!(t instanceof E)&&!0===s.scanForField(t.field.name)){if(!(t instanceof x)){s=null,n=!1;break}e.push({field:t.field.name,newfield:t.originalField.name})}s&&e.length>0&&(s=s.replaceFields(e))}null!==r?null!==this._extraFilter&&(r=w(this._extraFilter,r)):r=this._extraFilter,await this._ensureLoaded();const h=await v(this._parent)._getFilteredSet(e,t,r,s,a);let u;return this._checkCancelled(a),u=!0===i?new o(h._candidates.slice(0).concat(h._known.slice(0)),[],!0===n&&h._ordered,this._clonePageDefinition(h.pagesDefinition)):new o(h._candidates.slice(0),h._known.slice(0),!0===n&&h._ordered,this._clonePageDefinition(h.pagesDefinition)),u}_reformulateWithoutAdaptions(e){const t={cannot:!1,where:e};if(null!==e)for(const r of this.adaptedFields)if(!0===m(e,r.field.name)){const s=r.rewriteSql(e,this);if(!0!==s.rewritten){t.cannot=!0,t.where=null;break}t.where=s.where}return t}async _stat(e,t,r,s,a,i,l){let n=!1,o=this._reformulateWithoutAdaptions(t);if(n=o.cannot,t=o.where,o=this._reformulateWithoutAdaptions(a),n=n||o.cannot,null!==(a=o.where)?null!==this._extraFilter&&(a=w(this._extraFilter,a)):a=this._extraFilter,!0===n)return null===a&&""===r&&null===s?this._manualStat(e,t,i,l):{calculated:!1};const h=await v(this._parent)._stat(e,t,r,s,a,i,l);return!1===h.calculated?null===a&&""===r&&null===s?this._manualStat(e,t,i,l):{calculated:!1}:h}async _canDoAggregates(e,t,r,s,a){if(null===this._parent)return!1;for(let n=0;n<e.length;n++)for(const t of this.adaptedFields)if(e[n].toLowerCase()===t.field.name.toLowerCase()&&!(t instanceof E))return!1;const i=[];for(let n=0;n<t.length;n++){const e=t[n];if(null!==e.workingexpr){const t=this._reformulateWithoutAdaptions(e.workingexpr);if(t.cannot)return!1;const r=e.clone();r.workingexpr=t.where,i.push(r)}else i.push(e)}const l=this._reformulateWithoutAdaptions(a);return!l.cannot&&(null!==(a=l.where)?null!==this._extraFilter&&(a=w(this._extraFilter,a)):a=this._extraFilter,this._parent._canDoAggregates(e,i,r,s,a))}async _getAggregatePagesDataSourceDefinition(e,t,r,s,a,n,o){if(null===this._parent)throw new i(l.NeverReach);const h=[];for(let c=0;c<t.length;c++){const e=t[c];if(null!==e.workingexpr){const t=this._reformulateWithoutAdaptions(e.workingexpr);if(t.cannot)throw new i(l.NeverReach);const r=e.clone();r.workingexpr=t.where,h.push(r)}else h.push(e)}const u=this._reformulateWithoutAdaptions(a);if(u.cannot)throw new i(l.NeverReach);return null!==(a=u.where)?null!==this._extraFilter&&(a=w(this._extraFilter,a)):a=this._extraFilter,this._parent._getAggregatePagesDataSourceDefinition(e,h,r,s,a,n,o)}}export{D as AdaptedFeatureSet,W as AdaptedField,x as FieldRename,E as OriginalField,y as SqlExpressionAdapted,A as StringToCodeAdapted};