Skip to content

Commit

Permalink
Format src
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Jan 16, 2024
1 parent fcb7df3 commit deae651
Showing 1 changed file with 60 additions and 36 deletions.
96 changes: 60 additions & 36 deletions src/50expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,29 @@
const toTypeNumberOps = new Set(['-', '*', '/', '%', '^']);
const toTypeStringOps = new Set(['||']);
const toTypeBoolOps = new Set([
'AND', 'OR', 'NOT', '=', '==', '===', '!=', '!==', '!===',
'>', '>=', '<', '<=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE',
'REGEXP', 'GLOB', 'BETWEEN', 'NOT BETWEEN', 'IS NULL', 'IS NOT NULL'
'AND',
'OR',
'NOT',
'=',
'==',
'===',
'!=',
'!==',
'!===',
'>',
'>=',
'<',
'<=',
'IN',
'NOT IN',
'LIKE',
'NOT LIKE',
'REGEXP',
'GLOB',
'BETWEEN',
'NOT BETWEEN',
'IS NULL',
'IS NOT NULL',
]);
class Op {
constructor(params) {
Expand Down Expand Up @@ -227,7 +247,6 @@
return `${leftStr} ${this.op} ${this.allsome ? this.allsome + ' ' : ''}${this.right.toString()}`;
}


findAggregator(query) {
if (this.left && this.left.findAggregator) {
this.left.findAggregator(query);
Expand All @@ -239,13 +258,9 @@
}

toType(tableid) {
if (toTypeNumberOps.has(this.op))
return 'number';


if (toTypeStringOps.has(this.op))
return 'string';
if (toTypeNumberOps.has(this.op)) return 'number';

if (toTypeStringOps.has(this.op)) return 'string';

if (this.op === '+') {
const leftType = this.left.toType(tableid);
Expand All @@ -259,18 +274,13 @@
}
}

if (toTypeBoolOps.has(this.op) || this.allsome)
return 'boolean';


if (!this.op)
return this.left.toType(tableid);
if (toTypeBoolOps.has(this.op) || this.allsome) return 'boolean';

if (!this.op) return this.left.toType(tableid);

return 'unknown';
}


toJS(context, tableid, defcols) {
// console.log(this);
var s;
Expand Down Expand Up @@ -556,7 +566,6 @@
op = '&&';
}


var expr = s || '(' + leftJS() + op + rightJS() + ')';

var declareRefs = 'y=[(' + refs.join('), (') + ')]';
Expand Down Expand Up @@ -724,7 +733,7 @@
'~': '~',
'-': '-',
'+': '+',
'NOT': '!',
NOT: '!',
};

class UniOp {
Expand All @@ -733,7 +742,7 @@
}

toString() {
const { op, right } = this;
const {op, right} = this;
const res = right.toString();

switch (op) {
Expand Down Expand Up @@ -767,8 +776,6 @@
}
}



toJS(context, tableid, defcols) {
if (this.right instanceof Column && this.op === '#') {
return `(alasql.databases[alasql.useid].objects['${this.right.columnid}'])`;
Expand All @@ -786,10 +793,8 @@

throw new Error(`Unsupported operator: ${this.op}`);
}

}


class Column {
constructor(params) {
assign(this, params);
Expand All @@ -813,38 +818,52 @@
return s;
}


toJS(context, tableid, defcols) {
if (!this.tableid && tableid === '' && !defcols) {
return this.columnid !== '_' ? `${context}['${this.columnid}']` : (context === 'g' ? "g['_']" : context);
return this.columnid !== '_'
? `${context}['${this.columnid}']`
: context === 'g'
? "g['_']"
: context;
}

if (context === 'g') {
return `g['${this.nick}']`;
}

if (this.tableid) {
return this.columnid !== '_' ? `${context}['${this.tableid}']['${this.columnid}']` : (context === 'g' ? "g['_']" : `${context}['${this.tableid}']`);
return this.columnid !== '_'
? `${context}['${this.tableid}']['${this.columnid}']`
: context === 'g'
? "g['_']"
: `${context}['${this.tableid}']`;
}

if (defcols) {
const tbid = defcols[this.columnid];
if (tbid === '-') {
throw new Error(`Cannot resolve column "${this.columnid}" because it exists in two source tables`);
throw new Error(
`Cannot resolve column "${this.columnid}" because it exists in two source tables`
);
} else if (tbid) {
return this.columnid !== '_' ? `${context}['${tbid}']['${this.columnid}']` : `${context}['${tbid}']`;
return this.columnid !== '_'
? `${context}['${tbid}']['${this.columnid}']`
: `${context}['${tbid}']`;
} else {
return this.columnid !== '_' ? `${context}['${this.tableid || tableid}']['${this.columnid}']` : `${context}['${this.tableid || tableid}']`;
return this.columnid !== '_'
? `${context}['${this.tableid || tableid}']['${this.columnid}']`
: `${context}['${this.tableid || tableid}']`;
}
}

if (tableid === -1) {
return `${context}['${this.columnid}']`;
}

return this.columnid !== '_' ? `${context}['${this.tableid || tableid}']['${this.columnid}']` : `${context}['${this.tableid || tableid}']`;
return this.columnid !== '_'
? `${context}['${this.tableid || tableid}']['${this.columnid}']`
: `${context}['${this.tableid || tableid}']`;
}

}

class AggrValue {
Expand All @@ -853,15 +872,17 @@
}

toString() {
const funcName = this.aggregatorid === 'REDUCE' ? this.funcid.replace(re_invalidFnNameChars, '') : this.aggregatorid;
const funcName =
this.aggregatorid === 'REDUCE'
? this.funcid.replace(re_invalidFnNameChars, '')
: this.aggregatorid;
const distinctPart = this.distinct ? 'DISTINCT ' : '';
const expressionPart = this.expression ? this.expression.toString() : '';
const overPart = this.over ? ` ${this.over.toString()}` : '';

return `${funcName}(${distinctPart}${expressionPart})${overPart}`;
}


findAggregator(query) {
const colas = escapeq(this.toString()) + ':' + query.selectGroup.length;

Expand All @@ -876,9 +897,12 @@
query.selectGroup.push(this);
}


toType() {
if (['SUM', 'COUNT', 'AVG', 'MIN', 'MAX', 'AGGR', 'VAR', 'STDDEV', 'TOTAL'].includes(this.aggregatorid)) {
if (
['SUM', 'COUNT', 'AVG', 'MIN', 'MAX', 'AGGR', 'VAR', 'STDDEV', 'TOTAL'].includes(
this.aggregatorid
)
) {
return 'number';
}

Expand Down

0 comments on commit deae651

Please sign in to comment.