Repository navigation
Expand file tree
/
Copy path426orderby.js
More file actions
176 lines (162 loc) · 5.86 KB
/
Copy path426orderby.js
File metadata and controls
176 lines (162 loc) · 5.86 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
yy.Select.prototype.compileOrder = function (query, params) {
var self = this;
self.orderColumns = [];
if (this.order) {
// console.log(990, this.order);
if (
this.order &&
this.order.length == 1 &&
this.order[0].expression &&
typeof this.order[0].expression == 'function'
) {
// console.log(991, this.order[0]);
var func = this.order[0].expression;
// console.log(994, func);
var nullsOrder =
this.order[0].nullsOrder == 'FIRST' ? -1 : this.order[0].nullsOrder == 'LAST' ? +1 : 0;
return function (a, b) {
var ra = func(a),
rb = func(b);
if (nullsOrder) {
if (ra == null) return rb == null ? 0 : nullsOrder;
if (rb == null) return -nullsOrder;
}
if (ra > rb) return 1;
if (ra == rb) return 0;
return -1;
};
}
var s = '';
var sk = '';
this.order.forEach(function (ord, idx) {
// console.log(ord instanceof yy.Expression);
// console.log(ord.toJS('a',''));
// console.log(ord.expression instanceof yy.Column);
if (ord.expression instanceof yy.NumValue) {
// Validate that column number is at least 1
if (ord.expression.value < 1) {
throw new Error(
`Invalid column number ${ord.expression.value}. Column numbers must be at least 1.`
);
}
var v = self.columns[ord.expression.value - 1];
// Check if we're dealing with SELECT * case
var hasWildcard =
self.columns.length === 1 &&
self.columns[0] instanceof yy.Column &&
self.columns[0].columnid === '*';
if (hasWildcard) {
// With SELECT *, use positional ordering (resolved at runtime)
// Skip validation as we don't know the column count at compile time
v = {_useColumnIndex: true, columnIndex: ord.expression.value - 1};
} else {
// With explicit columns, validate the column number
if (ord.expression.value > self.columns.length) {
throw new Error(
`You are trying to order by column number ${ord.expression.value} but you have only selected ${self.columns.length} columns.`
);
}
// Also check if the resolved column is a wildcard (shouldn't happen but be safe)
if (v instanceof yy.Column && v.columnid === '*') {
v = {_useColumnIndex: true, columnIndex: ord.expression.value - 1};
}
}
} else if (ord.expression instanceof yy.StringValue) {
// Treat quoted strings in ORDER BY as column references
var v = new yy.Column({columnid: ord.expression.value});
} else {
var v = ord.expression;
}
self.orderColumns.push(v);
var key = '$$$' + idx;
// Date conversion - get columnid based on expression type
var dg = '';
var columnid;
if (ord.expression instanceof yy.Column) {
columnid = ord.expression.columnid;
} else if (ord.expression instanceof yy.ParamValue) {
columnid = params[ord.expression.param];
} else if (ord.expression instanceof yy.StringValue) {
columnid = ord.expression.value;
}
if (columnid) {
if (alasql.options.valueof) {
dg = '.valueOf()';
} else if (query.xcolumns[columnid]) {
var dbtypeid = query.xcolumns[columnid].dbtypeid;
if (
dbtypeid == 'DATE' ||
dbtypeid == 'DATETIME' ||
dbtypeid == 'DATETIME2' ||
dbtypeid == 'STRING' ||
dbtypeid == 'NUMBER'
)
dg = '.valueOf()';
}
}
if (ord.nullsOrder) {
if (ord.nullsOrder == 'FIRST') {
s += "if((a['" + key + "'] != null) && (b['" + key + "'] == null)) return 1;";
} else if (ord.nullsOrder == 'LAST') {
s += "if((a['" + key + "'] == null) && (b['" + key + "'] != null)) return 1;";
}
s += "if((a['" + key + "'] == null) == (b['" + key + "'] == null)) {";
sk += '}';
}
var va = "(a['" + key + "']||'')" + dg;
var vb = "(b['" + key + "']||'')" + dg;
// COLLATE NOCASE ignores case in text; numbers and other values compare as they are
if (ord.nocase) {
va = '(typeof (y=' + va + ")=='string'?y.toUpperCase():y)";
vb = '(typeof (y=' + vb + ")=='string'?y.toUpperCase():y)";
}
s += 'if(' + va + (ord.direction == 'ASC' ? '>' : '<') + vb + ')return 1;';
s += 'if(' + va + '==' + vb + '){';
//console.log(37,s);
/*
if(false) {
//console.log(ord.expression, ord.expression instanceof yy.NumValue);
if(ord.expression instanceof yy.NumValue) {
ord.expression = self.columns[ord.expression.value-1];
//console.log(ord.expression);
ord.expression = new yy.Column({columnid:ord.expression.nick});
};
if(ord.expression instanceof yy.Column) {
var columnid = ord.expression.columnid;
if(query.xcolumns[columnid]) {
var dbtypeid = query.xcolumns[columnid].dbtypeid;
if( dbtypeid == 'DATE' || dbtypeid == 'DATETIME' || dbtypeid == 'DATETIME2') dg = '.valueOf()';
// TODO Add other types mapping
} else {
if(alasql.options.valueof) dg = '.valueOf()'; // TODO Check
}
// COLLATE NOCASE
if(ord.nocase) dg += '.toUpperCase()';
s += 'if((a[\''+columnid+"']||'')"+dg+(ord.direction == 'ASC'?'>':'<')+'(b[\''+columnid+"']||'')"+dg+')return 1;';
s += 'if((a[\''+columnid+"']||'')"+dg+'==(b[\''+columnid+"']||'')"+dg+'){';
} else {
dg = '.valueOf()';
// COLLATE NOCASE
if(ord.nocase) dg += '.toUpperCase()';
s += 'if(('+ord.toJS('a','')+"||'')"+dg+(ord.direction == 'ASC'?'>(':'<(')+ord.toJS('b','')+"||'')"+dg+')return 1;';
s += 'if(('+ord.toJS('a','')+"||'')"+dg+'==('+ord.toJS('b','')+"||'')"+dg+'){';
}
// if(columnid == '_') {
// s += 'if(a'+dg+(ord.direction == 'ASC'?'>':'<')+'b'+dg+')return 1;';
// s += 'if(a'+dg+'==b'+dg+'){';
// } else {
// TODO Add date comparision
// // s += 'if(a[\''+columnid+"']"+dg+(ord.direction == 'ASC'?'>':'<')+'b[\''+columnid+"']"+dg+')return 1;';
// // s += 'if(a[\''+columnid+"']"+dg+'==b[\''+columnid+"']"+dg+'){';
// }
}
*/
sk += '}';
});
s += 'return 0;';
s += sk + 'return -1';
query.orderfns = s;
//console.log('ORDERBY',s);
return new Function('a,b', 'var y;' + s);
}
};