Blame view

alasql-utility/alasql-utility.js 4.64 KB
420ea6ab   Renato De Donato   workcicle...
1
  function alasql_QUERY (data, fields, filters, aggregators, orders) {
256ece27   Renato De Donato   new controllet
2
      if(fields && fields.length == 0)
89558a41   Renato De Donato   datatype, provide...
3
4
          return [];
  
256ece27   Renato De Donato   new controllet
5
6
7
8
      return alasql(alasql_QUERY_string(fields, filters, aggregators, orders), [data]);
  }
  
  function alasql_QUERY_string (fields, filters, aggregators, orders) {
89558a41   Renato De Donato   datatype, provide...
9
  
256ece27   Renato De Donato   new controllet
10
11
12
13
      if(fields) {
          var _fields = _addParenthesis(fields);
          var select = _alasql_SELECT(_fields);
      }
89558a41   Renato De Donato   datatype, provide...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  
      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;
  
256ece27   Renato De Donato   new controllet
37
      return query;
89558a41   Renato De Donato   datatype, provide...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  }
  
  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) {
256ece27   Renato De Donato   new controllet
53
54
55
56
57
58
59
60
61
      //var logicalOperator;
      //
      ///*DEPRECATED*/if(filters[0].logicalOperator == undefined) {
      //    logicalOperator = "AND"
      //}
      //else {
      //    logicalOperator = filters[0].logicalOperator;
      //    filters = filters.slice(1);
      //}
89558a41   Renato De Donato   datatype, provide...
62
  
256ece27   Renato De Donato   new controllet
63
64
65
      var where = " WHERE ";
      var logicalOperator = filters[0].logicalOperator;
      filters = filters.slice(1);
89558a41   Renato De Donato   datatype, provide...
66
67
68
69
70
71
72
  
      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 + " ";
f833bd4e   Renato De Donato   filter ln
73
          else if(filters[i]["operation"] == "notContains")
89558a41   Renato De Donato   datatype, provide...
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
              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
256ece27   Renato De Donato   new controllet
99
100
              //select += aggregators[i]["operation"] + "(" + aggregators[i]["field"] + ") as " + aggregators[i]["field"] + ", ";
              select += aggregators[i]["operation"] + "(" + aggregators[i]["field"] + "), ";
89558a41   Renato De Donato   datatype, provide...
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
      }
  
      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) {
420ea6ab   Renato De Donato   workcicle...
128
      return "[" + field + "]";
89558a41   Renato De Donato   datatype, provide...
129
130
  }
  
256ece27   Renato De Donato   new controllet
131
  function alasql_transformData (data, fields, round) {
89558a41   Renato De Donato   datatype, provide...
132
133
134
      if(!data || data.length == 0)
          return [];
  
89558a41   Renato De Donato   datatype, provide...
135
136
137
138
139
140
141
      var tData = [];
  
      for (var i in fields){
  
          var field = fields[i];
          var values = [];
  
256ece27   Renato De Donato   new controllet
142
143
144
          for (var j in data) {
              var v = data[j][field];
              if(round)
89558a41   Renato De Donato   datatype, provide...
145
                  if(!isNaN(v) && v % 1 != 0)
256ece27   Renato De Donato   new controllet
146
                      v = Math.round(v * 1000000) / 1000000;
89558a41   Renato De Donato   datatype, provide...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
              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;
  }