-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtotal.js
114 lines (105 loc) · 2.71 KB
/
total.js
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
!global.F && require('total.js');
exports.run = function(opt, self, cmd) {
var db = opt.type === 'nosql' ? NOSQL : TABLE;
switch (cmd.type) {
case 'find':
case 'read':
WHERE(db(cmd.builder.options.table).find(), cmd.builder);
break;
case 'list':
WHERE(db(cmd.builder.options.table).listing(), cmd.builder);
break;
case 'count':
WHERE(db(cmd.builder.options.table).count(), cmd.builder);
break;
case 'scalar':
WHERE(db(cmd.builder.options.table).scalar(), cmd.builder);
break;
case 'insert':
WHERE(db(cmd.builder.options.table).insert(cmd.builder.value, cmd.unique), cmd.builder);
break;
case 'update':
WHERE(db(cmd.builder.options.table).update(cmd.builder.value, cmd.insert), cmd.builder);
break;
case 'modify':
WHERE(db(cmd.builder.options.table).modify(cmd.builder.value, cmd.insert), cmd.builder);
break;
case 'remove':
WHERE(db(cmd.builder.options.table).remove(), cmd.builder);
break;
default:
cmd.builder.$callback(new Error('Operation "' + cmd.type + '" not found'));
break;
}
};
function WHERE(db, builder) {
builder.options.fields && db.fields.apply(db, builder.options.fields);
for (var i = 0; i < builder.$commands.length; i++) {
var cmd = builder.$commands[i];
if (typeof(cmd.value) === 'function')
cmd.value = cmd.value();
switch (cmd.type) {
case 'where':
db.where(cmd.name, cmd.compare, cmd.value);
break;
case 'in':
db.in(cmd.name, cmd.value);
break;
case 'notin':
db.notin(cmd.name, cmd.value);
break;
case 'code':
db.code(cmd.value);
break;
case 'between':
if (typeof(cmd.a) === 'function')
cmd.a = cmd.a();
if (typeof(cmd.b) === 'function')
cmd.b = cmd.b();
db.between(cmd.name, cmd.a, cmd.b);
break;
case 'search':
db.search(cmd.name, cmd.value, cmd.compare);
break;
case 'fulltext':
db.fulltext(cmd.name, cmd.value, cmd.weight);
break;
case 'contains':
db.contains(cmd.name);
break;
case 'empty':
db.empty(cmd.name);
break;
case 'year':
db.year(cmd.name, cmd.compare, cmd.value);
break;
case 'month':
db.month(cmd.name, cmd.compare, cmd.value);
break;
case 'day':
db.day(cmd.name, cmd.compare, cmd.value);
break;
case 'or':
db.or();
break;
case 'end':
db.end();
break;
case 'and':
db.and();
break;
case 'sort':
db.sort(cmd.name, cmd.desc);
break;
case 'regexp':
db.regexp(cmd.name, cmd.value);
break;
}
}
builder.options.skip && db.skip(builder.options.skip);
builder.options.take && db.take(builder.options.take);
builder.options.first && db.first();
db.callback(function(err, response, count) {
builder.$callback(err, response, count);
});
}