-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsvar.js
1543 lines (1209 loc) · 35.4 KB
/
dsvar.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// UNCLASSIFIED
/**
* @class DSVAR
* @requires cluster
* @requires enum
* @requires mysql
*/
var // nodejs
CLUSTER = require("cluster");
var // 3rd party bindings
MYSQL = require("mysql");
var
DSVAR = module.exports = {
errors: { //< errors messages
nillUpdate: new Error("nill update query"),
unsafeQuery: new Error("unsafe queries not allowed"),
unsupportedQuery: new Error("query not supported"),
invalidQuery: new Error("query invalid"),
noTable: new Error("dataset definition missing table name"),
noDB: new Error("no database connected"),
noLock: new Error("record lock ID missing"),
isUnlocked: new Error("record never locked"),
failLock: new Error("record locking failed"),
isLocked: new Error("record already locked"),
noExe: new Error("record execute undefined"),
noRecord: new Error("no record found")
},
fetchers: null, //< defined by config
attrs: { //< reserved for dataset attributes derived during config
default: { // default dataset attributes
sql: null, // sql connector
query: "", // sql query
opts: null, // ?-options to sql query
unsafeok: true, // allow/disallow unsafe queries
trace: false, // trace ?-compressed sql queries
journal: true, // attempt journally of updates to jou.table database
ag: "", // default aggregator "" implies "least(?,1)"
index: {select:"*"}, // data search and index
client: "guest", // default client
track: false, // change journal tracking
search: "" // key,key, .... fulltext keys to search
}
},
config: function (opts, cb) { // callback cb(sql connection)
if (opts) Copy(opts,DSVAR);
Trace("CONFIG DSVAR");
if (mysql = DSVAR.mysql) {
mysql.pool = MYSQL.createPool(mysql.opts);
sqlThread( function (sql) {
ENUM.extend(sql.constructor, [ // extend sql connector with useful methods
// key getters
getKeys,
getFields,
jsonKeys,
searchKeys,
geometryKeys,
textKeys,
// record getters
first,
context,
each,
all,
// misc
cache,
flattenCatalog,
// bulk insert records
beginBulk,
endBulk,
// job processing
selectJob,
deleteJob,
updateJob,
insertJob,
executeJob
]);
sql.query("DELETE FROM openv.locks");
//cb(sql);
var
attrs = DSVAR.attrs,
dsFrom = "app",
dsKey = "Tables_in_" + dsFrom;
sql.query(`SHOW TABLES FROM ${dsFrom}`, function (err, recs) {
recs.each( function (n,rec) {
sql.searchKeys( ds = dsFrom + "." + rec[dsKey], [], function (keys) {
var attr = attrs[ds] = {};
for (var key in attrs.default) attr[key] = attrs.default[key];
attr.search = keys.join(",");
});
});
if (cb) cb(sql);
else
sql.release();
});
});
}
return DSVAR;
},
msql: null, //< reserved for mysql connector
io: { //< reserved for socketio
sockets: {
emit: null
}
},
thread: sqlThread
};
var // totem bindings
ENUM = require("enum").extend({
String: [
function Escape() {
var q = "`";
return q + escape(this) + q;
}
],
Array: [
function Escape(slash,cb) {
var q = "`";
if (cb) {
var rtn = [];
this.each(function (n,el) {
rtn.push( cb(el) );
});
return rtn.join(slash);
}
else
return q + this.join(slash ? `${q}${slash}${q}` : `${q},${q}`) + q;
}
]
}),
Copy = ENUM.copy,
Each = ENUM.each,
Log = console.log;
function DATASET(sql,ats) { // create dataset with given sql connector and attributes
if (ats.constructor == String) ats = {table:ats};
if (ats.table) { // default then override attributes
var attrs = DSVAR.attrs[ats.table] || DSVAR.attrs.default;
for (var n in attrs)
switch (n) {
case "select":
case "update":
case "delete":
case "insert":
this.prototype[n] = attrs[n];
break;
default:
this[n] = attrs[n];
}
this.sql = sql; // sql connector
this.err = null; // default sql error response
for (var n in ats) this[n] = ats[n];
}
else
Trace(DSVAR.errors.noTable+"");
}
/*
CRUD interface ds.rec = req where req is an
Error: respond with error req
Array: respond with statis on inserting records req into ds
Object: respond with status on updating ds with req record then respond with status
null: respond with status on deleting records matching ds attributes
Function: respond on req callback with ds records matching ds attributes
"X": dataset CRUD (ds.rec = ds.data) X=select,update,delete,create,execute
"lock.X": lock/unlock record for dataset CRUD
*/
DATASET.prototype = {
x: function xquery(opt,key,buf) { // query builder extends me.query and me.opts
var
me = this, // target ds
keys = key.split(" "), // key the the sql command
ag = keys[1] || "least(?,1)", // method to aggregrate
type = opt ? opt.constructor : null;
if (type)
switch (keys[0]) {
case "BROWSE":
var
slash = "_",
where = me.where,
nodeID = where.NodeID,
nodes = nodeID ? nodeID.split(slash) : [],
pivots = opt.split(",");
me.group = (nodes.length >= pivots.length)
? pivots.concat(["ID"])
: pivots.slice(0,nodes.length+1);
var name = pivots[nodes.length] || "concat('ID',ID)";
var path = me.group.Escape(",'"+slash+"',");
me.query += `, cast(${name} AS char) AS name, group_concat(DISTINCT ${path}) AS NodeID`
+ ", count(ID) AS NodeCount "
+ ", '/tbd' AS `path`, 1 AS `read`, 1 AS `write`, 'v1' AS `group`, 1 AS `locked`";
delete where.NodeID;
nodes.each( function (n,node) {
where[ pivots[n] || "ID" ] = node;
});
break;
case "PIVOT":
var
where = me.where,
nodeID = where.NodeID || "root";
if (nodeID == "root") {
me.query += ", group_concat(DISTINCT ID) AS NodeID, count(ID) AS NodeCount,false AS leaf,true AS expandable,false AS expanded";
me.group = opt;
delete where.NodeID;
}
else {
me.query += `, '${nodeID}' as NodeID, 1 AS NodeCount, true AS leaf,true AS expandable,false AS expanded`;
me.where = `instr(',${nodeID},' , concat(',' , ID , ','))`;
me.group = null;
}
break;
case "":
case "IN":
case "WITH":
if (me.search) {
me.query += `,MATCH(${me.search}) AGAINST('${opt}' ${key}) AS Score`;
me.having = me.score ? "Score>"+me.score : ["Score"];
me.searching = opt;
}
break;
case "HAS":
if (me.search) {
me.query += `,instr(concat(${me.search}),'${opt}') AS Score`;
me.having = me.score ? "Score>"+me.score : ["Score"];
me.searching = opt;
}
break;
case "SELECT":
switch (type) {
case Array:
me.query += ` ${key} ??`;
me.opts.push(opt);
break;
case String:
if (opt == "*")
me.query += ` ${key} *`;
else {
me.query += ` ${key} ${opt}`;
me.unsafe = true;
}
break;
/*
case Object:
if (opt.idx)
me.x(opt.idx, key);
else {
me.query += ` ${key} *`;
me.x(opt.nlp, "");
me.x(opt.bin, "IN BINARY MODE");
me.x(opt.qex, "WITH QUERY EXPANSION");
me.x(opt.has,"HAS");
me.x(opt.browse, "BROWSE");
me.x(opt.pivot, "PIVOT");
}
break;
*/
}
me.x(me.index, "INDEX");
/*
if ( me.geo ) // any geometry fields are returned as geojson
me.query += ","+me.geo;
*/
break;
case "JOIN":
switch (type) {
case Array:
me.query += ` ${mode} ${key} ON ?`;
me.opts.push(opt);
break;
case String:
me.query += ` ${mode} ${key} ON ${opt}`;
break;
case Object:
var mode = opt.left ? "left" : opt.right ? "right" : "";
me.query += ` ${mode} ${key} ? ON least(?,1)`;
for (var n in opt.on)
buf[n] = me.table+"."+opt.on[n];
me.opts.push(opt[mode]);
me.opts.push(buf);
break;
}
break;
case "LIMIT":
switch (type) {
case Array:
me.query += ` ${key} ?`;
me.opts.push(opt);
break;
case String:
me.query += ` ${key} ?`;
me.opts.push(opt.split(","));
break;
case Object:
me.query += ` ${key} ?`;
me.opts.push([opt.start,opt.count]);
break;
}
break;
case "WHERE":
case "HAVING":
me.nowhere = false;
switch (type) {
case Array:
me.query += ` ${key} ??`;
me.opts.push(opt);
break;
case String:
me.safe = false;
me.query += ` ${key} ${opt}`;
break;
case Object:
var rels = [];
for (var n in opt) { // recast and remove unsafes
var test = opt[n];
if (test == null) { // using unsafe expression query (e.g. &x<10)
me.safe = false;
rels.push(n);
delete opt[n];
}
else
switch (test.constructor) {
case Array: // using range query e.g. x=[min,max]
delete opt[n];
switch (test.length) {
case 0:
rels.push( n.Escape() +" IS null" );
break;
case 2:
rels.push(n.Escape() + " BETWEEN " + test[0] + " AND " + test[1] );
break;
default:
}
break;
case Object: // using search query e.g. x={nlp:pattern}
var fld = n.Escape();
if (pat = test.nlp)
rels.push( `MATCH(${fld}) AGAINST('${pat}')` );
else
if (pat = test.bin)
rels.push( `MATCH(${fld}) AGAINST('${pat}') IN BINARY MODE` );
else
if (pat = test.qex)
rels.push( `MATCH(${fld}) AGAINST('${pat}') WITH QUERY EXPANSION` );
else
if (pat = test.has)
rels.push( `INSTR(${fld}, '${pat}')` );
else
if (pat = test.like)
rels.push( `${fld} LIKE '${pat}'` );
else
break;
delete opt[n];
break;
case String: // otherwise using safe query e.g x=value
default:
}
}
for (var n in opt) { // aggregate where clause using least,sum,etc
rels.push(ag);
me.opts.push(opt);
break;
}
rels = rels.join(" AND "); // aggregate remaining clauses
if (rels)
me.query += ` ${key} ${rels}`;
else
me.nowhere = true;
break;
default:
me.unsafe = true;
}
break;
case "ORDER":
switch (type) {
case Array:
var by = [];
opt.each(function (n,opt) {
if (opt.property)
by.push(`${opt.property} ${opt.direction}`);
else
for (var n in opt)
by.push(`${n} ${opt[n]}`);
});
me.query += ` ${key} ${by.join(",")}`;
break;
case String:
me.query += ` ${key} ??`;
me.opts.push(opt.split(","));
break;
case Object:
break;
}
break;
case "SET":
switch (type) {
/*case Array:
me.safe = false;
me.query += ` ${key} ??`;
me.opts.push(opt);
break;*/
case String:
me.safe = false;
me.query += ` ${key} ${opt}`;
break;
case Object:
me.query += ` ${key} ?`;
/*
for (var n in opt) { // object subkeys are json fields
var js = opt[n];
if (typeof js == "object") {
var js = JSON.stringify(js);
me.query += `,json_merge(${n},${js})`;
delete opt[n];
}
*/
me.opts.push(opt);
break;
default:
me.unsafe = true;
}
break;
case "INDEX":
//console.log(opt);
me.x(opt.nlp, "");
me.x(opt.bin, "IN BINARY MODE");
me.x(opt.qex, "WITH QUERY EXPANSION");
me.x(opt.has,"HAS");
me.x(opt.browse, "BROWSE");
me.x(opt.pivot, "PIVOT");
break;
default:
switch (type) {
case Array:
me.query += ` ${key} ??`;
me.opts.push(opt);
break;
case String:
me.query += ` ${key} ??`;
me.opts.push(opt.split(","));
break;
case Object:
me.query += ` ${key} ?`;
me.opts.push(opt);
break;
}
}
},
update: function (req,res) { // update record(s) in dataset
function hawkChange(log) { // journal changes
sql.query("SELECT * FROM openv.hawks WHERE least(?,Power)", log)
.on("result", function (hawk) {
//console.log(hawk);
sql.query(
"INSERT INTO openv.journal SET ? ON DUPLICATE KEY UPDATE Updates=Updates+1",
Copy({
Hawk: hawk.Hawk, // moderator
Power: hawk.Power, // moderator's level
Updates: 1 // init number of updates made
}, log)
);
});
}
var
me = this,
table = me.table,
ID = me.where.ID,
client = me.client,
sql = me.sql;
me.opts = []; me.query = ""; me.safe = true; me.nowhere=true;
me.x(table, "UPDATE");
me.x(req, "SET");
me.x(me.where, "WHERE "+me.ag);
me.x(me.order, "ORDER BY");
if (me.nowhere)
res( DSVAR.errors.unsafeQuery );
else
if ( Each(req) ) // empty so ....
res( DSVAR.errors.nillUpdate );
else
if (me.safe || me.unsafeok) {
/*
//uncomment to disable journalling
me.journal = false;
*/
if (me.journal) { // attempt change journal if enabled
hawkChange({Dataset:me.table, Field:""}); // journal entry for the record itself
/*
// uncomment to enable
for (var key in req) { // journal entry for each record key being changed
hawk({Dataset:me.table, Field:key});
hawk({Dataset:"", Field:key});
}
*/
}
sql.query(me.query, me.opts, function (err,info) {
if (res) res( err || info );
if (DSVAR.io.sockets.emit && ID && !err) // Notify clients of change.
DSVAR.io.sockets.emit( "update", {
path: "/"+me.table+".db",
body: req,
ID: ID,
from: client
//flag: flags.client
});
});
if (me.trace) Trace(me.query);
}
else
if (res)
res( DSVAR.errors.unsafeQuery );
},
select: function (req,res) { // select record(s) from dataset
var
me = this,
table = me.table,
client = me.client,
sql = me.sql;
me.opts = []; me.query = ""; me.safe = true; me.nowhere=true;
me.x(me.index.select, "SELECT SQL_CALC_FOUND_ROWS");
me.x(table, "FROM");
me.x(me.join, "JOIN", {});
me.x(me.where, "WHERE "+me.ag);
me.x(me.having, "HAVING "+me.ag);
me.x(me.order, "ORDER BY");
me.x(me.group, "GROUP BY");
me.x(me.limit, "LIMIT");
if (me.safe || me.unsafeok)
switch (req.name) {
case "each":
sql.query(me.query, me.opts)
.on("error", function (err) {
req(err,me);
})
.on("result", function (rec) {
req(rec,me);
});
break;
case "clone":
var rtn = [];
sql.query(me.query, me.opts, function (err,recs) {
if (err)
return req( err, me );
recs.each(function (n,rec) {
rtn.push( new Object( rec ) );
});
req(rtn,me);
});
break;
case "trace":
Trace(me.query);
sql.query(me.query, me.opts, function (err,recs) {
req( err || recs, me );
});
break;
case "all":
default:
sql.query(me.query, me.opts, function (err,recs) {
if (me.track && me.searching && recs) // track searches if tracking
sql.query(
"INSERT INTO openv.tracks SET ? ON DUPLICATE KEY UPDATE Searched=Searched+1,Returned=Returned+?", [
{ Client: client,
Searching: me.searching,
Searched: 0,
Within: me.table,
Returned: recs.length
}, recs.length
]);
req( err || recs, me );
});
}
else
if (res)
res( DSVAR.errors.unsafeQuery );
if (me.trace) Trace(me.query);
},
delete: function (req,res) { // delete record(s) from dataset
var
me = this,
table = me.table,
ID = me.where.ID,
client = me.client,
sql = me.sql;
me.opts = []; me.query = ""; me.safe = true; me.nowhere=true;
me.x(table, "DELETE FROM");
me.x(me.where, "WHERE "+me.ag);
if (me.nowhere)
res( DSVAR.errors.unsafeQuery );
else
if (me.safe || me.unsafeok) {
me.sql.query(me.query, me.opts, function (err,info) {
if (me.res) me.res(err || info);
if (DSVAR.io.sockets.emit && ID && !err) // Notify clients of change.
DSVAR.io.sockets.emit( "delete", {
path: "/"+me.table+".db",
ID: ID,
from: me.client
//flag: flags.client
});
});
if (me.trace) Trace(me.query);
}
else
if (res)
res( DSVAR.errors.unsafeQuery );
},
insert: function (req,res) { // insert record(s) into dataset
var
me = this,
table = me.table,
ID = me.where.ID,
client = me.client,
sql = me.sql;
me.opts = []; me.query = ""; me.safe = true; me.nowhere=true;
if (!req.length) req = [{}]; // force at least one insert attempt
req.each(function (n,rec) { // insert each record
sql.query(
me.query = Each(rec) // trap empty records
? "INSERT INTO ?? VALUE ()"
: "INSERT INTO ?? SET ?" ,
[table,rec],
function (err,info) {
if (!n && res) { // respond only to first insert
res( err || info );
if (DSVAR.io.sockets.emit && !err) // Notify clients of change.
DSVAR.io.sockets.emit( "insert", {
path: "/"+me.table+".db",
body: rec,
ID: info.insertId,
from: client
//flag: flags.client
});
}
});
if (me.trace) Trace(me.query);
});
},
get rec() { // reserved
},
unlock: function (ID, cb, lockcb) { // unlock record
var
me = this,
sql = me.sql,
lockID = {Lock:`${me.table}.${ID}`, Client:me.client};
if (ID)
sql.query( // attempt to unlock a locked record
"DELETE FROM openv.locks WHERE least(?)",
lockID,
function (err,info) {
if (err)
me.res( DSVAR.errors.failLock );
else
if (info.affectedRows) { // unlocked so commit queued queries
cb();
sql.query("COMMIT");
}
else
if (lockcb) // attempt to lock this record
sql.query(
"INSERT INTO openv.locks SET ?",
lockID,
function (err,info) {
if (err)
me.res( DSVAR.errors.isLocked );
else
sql.query( "START TRANSACTION", function (err) { // queue this transaction
lockcb();
});
});
else // record was never locked
me.res( DSVAR.errors.isUnlocked );
});
else
me.res( DSVAR.errors.noLock );
},
set rec(req) { // crud interface
var
me = this,
res = me.res;
if (req)
switch (req.constructor) {
case Error:
if (res) res(req);
break;
case Array:
me.insert(req,res);
break;
case Object:
me.update(req,res);
break;
case Function:
me.select(req,res);
break;
case String:
if (me.trace) Trace(
`${req.toUpperCase()} ${me.table} FOR ${me.client}`
);
switch (req) {
case "lock.select":
me.rec = function (recs) { // get records
if (recs.constructor == Error)
res( recs );
else
if (rec = recs[0])
me.unlock(rec.ID, function () { // unlocked
res( rec );
}, function () { // locked
res( rec );
});
else
res( DSVAR.errors.noRecord );
};
break;
case "lock.delete":
me.unlock(me.where.ID, function () { // delete if unlocked
me.rec = null;
});
break;
case "lock.insert":
me.unlock(me.where.ID, function () { // insert if unlocked
me.rec = [me.data];
});
break;
case "lock.update":
me.unlock(me.where.ID, function () { // update if unlocked
me.rec = me.data;
});
break;
case "lock.execute":
res( DSVAR.errors.noExe );
break;
case "select": me.rec = me.res; break;
case "update": me.rec = me.data; break;
case "delete": me.rec = null; break;
case "insert": me.rec = [me.data]; break;
case "execute": me.rec = DSVAR.errors.unsupportedQuery; break;
default:
me.rec = DSVAR.errors.invalidQuery;
}
}
else
me.delete(req,res);
return this;
}
}
function getKeys(table, type, keys, cb) {
this.query(`SHOW KEYS FROM ${table} WHERE ?`,{Index_type:type}, function (err, recs) {
recs.each( function (n,rec) {
keys.push(rec.Column_name);
});
cb(keys);
});
}
function getFields(table, type, keys, cb) {
this.query(`SHOW FIELDS FROM ${table} WHERE ?`,{Type:type}, function (err, recs) {
recs.each( function (n, rec) {
keys.push(rec.Field);
});
cb(keys);
});
}
function jsonKeys(table, keys, cb) {
this.getFields(table, "json", keys, cb);
}
function textKeys(table, keys, cb) {
this.getFields(table, "mediumtext", keys, cb);
}
function searchKeys(table, keys, cb) {
this.getKeys(table, "fulltext", keys, cb);
}
function geometryKeys(table, keys, cb) {
this.getFields(table, "geometry", keys, cb);
}
function first(trace, query, args, cb) { // callback cb(rec) or cb(null) if error
var q = query
? this.query(query, args, function (err,recs) {
cb( err ? null : recs[0] );
})
: { sql: "IGNORE", on: function (){} };