alasql-utility.js
4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function alasql_selectData (data, fields, filters) {
if(fields.length == 0)
return [];
var _fields = _addParenthesis(fields);
var select = _alasql_SELECT(_fields);
var where = "";
if(filters && filters.length) {
var _filters = _copy(filters);
where = _alasql_WHERE(_filters);
}
var query = select + " FROM ?" + where;
return alasql(query, [data]);
}
function alasql_complexSelectData (data, fields, filters, aggregators, orders) {
if(fields.length == 0)
return [];
var _fields = _addParenthesis(fields);
var select = _alasql_SELECT(_fields);
var where = "";
if(filters && filters.length) {
var _filters = _copy(filters);
where = _alasql_WHERE(_filters);
}
var orderBy = "";
if(orders && orders.length) {
var _orders = _copy(orders);
orderBy = _alasql_ORDERBY(_orders);
}
var groupBy = "";
if(aggregators && aggregators.length) {
var _aggregators = _copy(aggregators);
groupBy = _alasql_GROUPBY(_aggregators);
select = groupBy[0];
groupBy = groupBy[1];
}
var query = select + " FROM ?" + where + " " + groupBy + " " + orderBy;
return alasql(query, [data]);
}
function _alasql_SELECT (fields) {
var select = "SELECT ";
if(fields[0] == "*")
return select += "*";
for (var i = 0; i < fields.length; i++)
select += fields[i] + ", ";
return select.slice(0, -2);
}
function _alasql_WHERE (filters) {
var where = " WHERE ";
var logicalOperator;
/*DEPRECATED*/if(filters[0].logicalOperator == undefined) {
logicalOperator = "AND"
}
else {
logicalOperator = filters[0].logicalOperator;
filters = filters.slice(1);
}
for (var i=0; i < filters.length; i++)
filters[i]["field"] = _normalizeField(filters[i]["field"]);
for (var i=0; i < filters.length; i++) {
if(filters[i]["operation"] == "contains")
where += filters[i]["field"] + " like '%" + filters[i]["value"] + "%' " + logicalOperator + " ";
else if(filters[i]["operation"] == "not contains")
where += filters[i]["field"] + " not like '%" + filters[i]["value"] + "%' " + logicalOperator + " ";
else if(filters[i]["operation"] == "start")
where += filters[i]["field"] + " like '" + filters[i]["value"] + "%' " + logicalOperator + " ";
else if(filters[i]["operation"] == "ends")
where += filters[i]["field"] + " like '%" + filters[i]["value"] + "' " + logicalOperator + " ";
else
where += filters[i]["field"] + " " + filters[i]["operation"] + " " + filters[i]["value"] + " " + logicalOperator + " ";
}
return where.slice(0, -4);
}
function _alasql_GROUPBY (aggregators) {
for (var i=0; i < aggregators.length; i++)
aggregators[i]["field"] = _normalizeField(aggregators[i]["field"]);
var select = "SELECT ";
var groupBy = "GROUP BY ";
for (var i = 0; i < aggregators.length; i++) {
if(aggregators[i]["operation"] == "GROUP BY") {
select += aggregators[i]["field"] + ", ";
groupBy += aggregators[i]["field"] + ", ";
}
else
select += aggregators[i]["operation"] + "(" + aggregators[i]["field"] + ") as " + aggregators[i]["field"] + ", ";
}
return [select.slice(0, -2), groupBy.slice(0, -2)];
}
function _alasql_ORDERBY (orders) {
for (var i=0; i < orders.length; i++)
orders[i]["field"] = _normalizeField(orders[i]["field"]);
var orderBy = "ORDER BY ";
for (var i = 0; i < orders.length; i++)
orderBy += orders[i]["field"] + " " + orders[i]["operation"] + ", ";
return orderBy.slice(0, -2);
}
function _addParenthesis (fields) {
var result = [];
for (var i=0; i < fields.length; i++)
result.push(_normalizeField(fields[i]));
return result;
}
function _normalizeField (field) {
/*DEPRECATED*/return "[" + field.substring(field.lastIndexOf(",") + 1, field.length) + "]";
//return "[" + field + "]";
}
function transformData (data, fields, truncate) {
if(!data || data.length == 0)
return [];
/*DEPRECATED*/for (var i=0; i < fields.length; i++)
fields[i] = fields[i].substring(fields[i].lastIndexOf(",") + 1, fields[i].length);
var tData = [];
for (var i in fields){
var field = fields[i];
var values = [];
for (var i in data) {
var v = data[i][field];
if(truncate)
if(!isNaN(v) && v % 1 != 0)
v = Math.floor(v * 100) / 100;
values.push(v);
}
tData.push({
name: field,
data: values
});
}
return tData;
};
function _copy (o) {
var out, v, key;
out = Array.isArray(o) ? new Array(o.length) : {};
for (key in o) {
v = o[key];
out[key] = (typeof v === "object") ? this._copy(v) : v;
}
return out;
}