/
driver.cpp
1694 lines (1437 loc) · 66.4 KB
/
driver.cpp
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
/**
* package: nodamysql
* sub-package: driver/code
* author: Richard B. Winters <a href='mailto:rik@mmogp.com'>Rik At MMOGP</a>
* copyright: 2011-2015 Massively Modified, Inc.
* license: Apache, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>
*/
// INCLUDES
#include "driver.h"
// MySQL Includes ( Also targets Boost C++ Headers )
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/parameter_metadata.h>
using namespace v8;
// PROTOTYPES
Persistent<Function> Driver::constructor; // Default constructor prototype
/**
* Overloaded Constructor
*
* @return void
*
* @since 0.0.1
*/
Driver::Driver( nkDriverConstructorDefinitionBlockA ) : nkDriverConstructorDefinitionBlockB
{
}
/**
* Destructor
*
* @return void
*
* @since 0.0.1
*/
Driver::~Driver()
{
}
/**
* Initializes the Driver object and exports it to v8 to be wrapped for Node.js
*
* @param Handle<Object> exports Defines a reference to an exports object (Syntactically taken)
*
* @return void
*
* @since 0.0.1
*/
void Driver::Init( Handle<Object> exports )
{
nkIsolationOS
// Prepare constructor template
Local<FunctionTemplate> tpl = nkFunctionTemplate( New );
tpl->SetClassName( nkNewV8SString( "driver" ) );
tpl->InstanceTemplate()->SetInternalFieldCount( 12 );
// Prototype(s) of our methods for v8
nkSetPrototypeMethod( tpl, "query", Query )
nkSetPrototypeMethod( tpl, "select", Select )
nkSetPrototypeMethod( tpl, "insert", Insert )
nkSetPrototypeMethod( tpl, "values", Values )
nkSetPrototypeMethod( tpl, "update", Update )
nkSetPrototypeMethod( tpl, "delete", Delete )
nkSetPrototypeMethod( tpl, "where", Where )
nkSetPrototypeMethod( tpl, "join", Join )
nkSetPrototypeMethod( tpl, "on", On )
nkSetPrototypeMethod( tpl, "limit", Limit )
nkSetPrototypeMethod( tpl, "order", Order )
nkSetPrototypeMethod( tpl, "execute", Execute )
nkSetPrototypeMethod( tpl, "executeQuery", ExecuteQuery )
nkSetPrototypeMethod( tpl, "getConnection", GetConnection )
nkSetPrototypeMethod( tpl, "getQuery", GetQuery )
nkSetPrototypeMethod( tpl, "reset", Reset )
nkSetConstructor( tpl )
nkSetExports( "driver", constructor, tpl )
}
/**
* Returns a new instance of the Driver object
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return Driver New instance of a Driver object
*
* @since 0.0.1
*/
nkMethodReturnType Driver::New( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
if( args.IsConstructCall() )
{
// Invoked as constructor: 'new nodamysql(...)'
Local<Object> config;
if( args[0]->IsUndefined() )
{
config = Object::New( nkIsolate );
config->Set( nkNewV8SString( "host" ), nkNewV8String( "localhost" ) );
config->Set( nkNewV8SString( "port" ), nkNewV8String( "3306" ) );
config->Set( nkNewV8SString( "db" ), nkNewV8String( "test" ) );
config->Set( nkNewV8SString( "user" ), nkNewV8String( "test" ) );
config->Set( nkNewV8SString( "password" ), nkNewV8String( "testpass" ) );
}
else
{
config = args[0]->ToObject();
}
Driver* dvr;
Local<Array> phmap = Array::New( nkIsolate );
Local<Array> phspec = Array::New( nkIsolate );
phspec->Set( 0, Integer::New( nkPreIsolated 0 ) );
phspec->Set( 1, Integer::New( nkPreIsolated 0 ) );
// A model doesn't have to be passed or used
if( config->Get( nkNewV8String( "model" ) )->IsUndefined() )
{
Local<String> host = config->Get( nkNewV8SString( "host" ) )->ToString();
Local<String> port = config->Get( nkNewV8SString( "port" ) )->ToString();
Local<String> db = config->Get( nkNewV8SString( "db" ) )->ToString();
Local<String> user = config->Get( nkNewV8SString( "user" ) )->ToString();
Local<String> pass = config->Get( nkNewV8SString( "password" ) )->ToString();
dvr = new Driver(
nkNewV8PString( host ),
nkNewV8PString( port ),
nkNewV8PString( db ),
nkNewV8PString( user ),
nkNewV8PString( pass ),
nkNewV8PObject( Object::New( nkIsolate ) ),
nkNewV8PBoolean( v8::False( nkIsolate ) ),
nkNewV8PInteger( Integer::New( nkPreIsolated 0 ) ),
nkNewV8PInteger( Integer::New( nkPreIsolated 0 ) ),
nkNewV8PArray( Array::New( nkIsolate ) ),
nkNewV8PBoolean( v8::False( nkIsolate ) ),
nkNewV8PString( nkNewV8String( "" ) )
);
}
else
{
dvr = new Driver(
nkNewV8PString( config->Get( nkNewV8String( "host" ) )->ToString() ),
nkNewV8PString( config->Get( nkNewV8String( "port" ) )->ToString() ),
nkNewV8PString( config->Get( nkNewV8String( "db" ) )->ToString() ),
nkNewV8PString( config->Get( nkNewV8String( "user" ) )->ToString() ),
nkNewV8PString( config->Get( nkNewV8String( "password" ) )->ToString() ),
nkNewV8PObject( config->Get( nkNewV8String( "model" ) )->ToObject() ),
nkNewV8PBoolean( v8::False( nkIsolate ) ),
nkNewV8PInteger( Integer::New( nkPreIsolated 0 ) ),
nkNewV8PInteger( Integer::New( nkPreIsolated 0 ) ),
nkNewV8PArray( Array::New( nkIsolate ) ),
nkNewV8PBoolean( v8::False( nkIsolate ) ),
nkNewV8PString( nkNewV8String( "" )->ToString() )
);
}
phmap->Set( 0, Local<Array>::New( nkPreIsolated phspec ) );
nkResetV8PArray( dvr->phmap_, phmap );
dvr->Wrap( args.This() );
nkReturn( args.This() )
}
else
{
// Invoked as plain function 'nodamysql(...)', turn it into a construct call
const int argc = 12;
Local<Object> config;
if( args[0]->IsUndefined() )
{
config = Object::New( nkIsolate ) ;
config->Set( nkNewV8SString( "host" ), nkNewV8String( "localhost" ) );
config->Set( nkNewV8SString( "port" ), nkNewV8String( "3306" ) );
config->Set( nkNewV8SString( "db" ), nkNewV8String( "test" ) );
config->Set( nkNewV8SString( "user" ), nkNewV8String( "test" ) );
config->Set( nkNewV8SString( "password" ), nkNewV8String( "testpass" ) );
}
else
{
config = args[0]->ToObject();
}
Local<Array> phspec = Array::New( nkIsolate );
phspec->Set( 0, Integer::New( nkPreIsolated 0 ) );
phspec->Set( 1, Integer::New( nkPreIsolated 0 ) );
Local<Value> argv[argc] = {
config->Get( nkNewV8String( "host" ) ),
config->Get( nkNewV8String( "port" ) ),
config->Get( nkNewV8String( "db" ) ),
config->Get( nkNewV8String( "user" ) ),
config->Get( nkNewV8String( "pass" ) ),
config->Get( nkNewV8String( "model" ) ),
Local<Boolean>::New( nkPreIsolated v8::False( nkIsolate ) ),
Integer::New( nkPreIsolated 0 ),
Integer::New( nkPreIsolated 0 ),
phspec,
Local<Boolean>::New( nkPreIsolated v8::False( nkIsolate ) ),
nkNewV8String( "" )
};
nkNewConstructor
nkReturn( nkNewFromConstructor )
}
}
/**
* Sets a supplied query statement (Not prepared!)
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Query( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> type = Local<Integer>::New( nkPreIsolated dvr->type_ );
Local<Integer> prepared = Local<Integer>::New( nkPreIsolated dvr->prepared_ );
Local<String> query = Local<String>::New( nkPreIsolated dvr->query_ );
if( args[0]->IsUndefined() || ( type->IntegerValue() != ( KWAERI_EMPTY || KWAERI_NOPREP ) && prepared->IntegerValue() != KWAERI_NOT_PREP ) )
{
std::cout << "Error: Escaped .Query" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), args[0]->ToString() )->ToString() );
nkResetV8PInteger( dvr->type_, KWAERI_NOPREP );
nkResetV8PInteger( dvr->prepared_, KWAERI_NOT_PREP );
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Builds a SELECT clause
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Select( const nkMethodArgumentType& args )
{
// Always declare isolate and HandleScope
nkIsolation( args )
nkScope
// Unwrap the object
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> type = Local<Integer>::New( nkPreIsolated dvr->type_ );
Local<Boolean> modeled = Local<Boolean>::New( nkPreIsolated dvr->modeled_ );
Local<Object> model = Local<Object>::New( nkPreIsolated dvr->model_ );
// Fetch our arguments and define default behavior for incorrect arguments or no arguments
if( args[0]->IsUndefined() || type->IntegerValue() != KWAERI_EMPTY )
{
std::cout << "Error: Escaped .Select" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
// This is how we convert a JavaScript string to a std::string (c++)
nkV8StringValueType av( args[0] );
std::string select = *av;
// Build our query string part
std::string qp = std::string( "SELECT " );
// If we're using models, we build our select in the proper manner
if( modeled->BooleanValue() )
{
// The supplied model let's us know which columns to select - let's write it out
Local<Array> keys = model->GetOwnPropertyNames(); // Gets us all the keys in an array
for( int i = 0, l = keys->Length(); i < l; i++ )
{
// Get the keys to build the select clause
Local<String> k = keys->Get( i )->ToString();
nkV8StringValueType kav( k );
std::string kv = *kav;
if( i != 0 )
{
qp += ", ";
}
qp += kv;
}
qp += " FROM " + select;
}
else
{ // Otherwise we tack on the user provided string to the query part to build the select clause
qp += select;
}
// Set our query type and the query string.
nkResetV8PStringUtf8( dvr->query_, qp.c_str() );
nkResetV8PInteger( dvr->type_, KWAERI_SELECT );
nkResetV8PInteger( dvr->prepared_, KWAERI_CLAUSE_PREP );
// Return the entire object to allow chaining, results can be checked there
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Builds an INSERT clause
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Insert( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> type = Local<Integer>::New( nkPreIsolated dvr->type_ );
Local<Boolean> modeled = Local<Boolean>::New( nkPreIsolated dvr->modeled_ );
Local<Object> model = Local<Object>::New( nkPreIsolated dvr->model_ );
Local<String> query = Local<String>::New( nkPreIsolated dvr->query_ );
if( args[0]->IsUndefined() || type->IntegerValue() != KWAERI_EMPTY )
{
std::cout << "Error: Escaped .Insert" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
// Fetch the user input
nkV8StringValueType av( args[0] );
std::string insert = *av;
std::string qp;
// If we're using models, the supplied model let's us know which columns to provide values for
if( modeled->BooleanValue() )
{
// Build our query string part
qp = std::string( "INSERT INTO " );
qp += insert;
qp += " ( ";
Local<Array> keys = model->GetOwnPropertyNames(); // Gets us all the keys in an array
for( int i = 0, l = keys->Length(); i < l; i++ )
{
// Get the keys to build the select clause
Local<String> k = keys->Get( i )->ToString();
nkV8StringValueType kav( k );
std::string kv = *kav;
if( i != 0 )
{
qp += ", ";
}
qp += kv;
}
qp += " ) ";
}
else
{ // Otherwise it's in the user supplied string
qp = "INSERT " + insert;
}
// Convert the std string to a v8 string
Handle<Value> qph = nkNewV8String( qp.c_str() );
nkV8StringValueType qpav( qph );
Local<String> qps = nkNewV8String( *qpav );
// And concatenate it with the existing query string
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), qps->ToString() )->ToString() );
nkResetV8PInteger( dvr->type_, KWAERI_INSERT );
nkResetV8PInteger( dvr->prepared_, KWAERI_CLAUSE_PREP );
// Return the entire object to allow chaining, results can be checked there
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Builds upon an INSERT or UPDATE clause
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Values( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> type = Local<Integer>::New( nkPreIsolated dvr->type_ );
Local<Integer> prepared = Local<Integer>::New( nkPreIsolated dvr->prepared_ );
Local<String> query = Local<String>::New( nkPreIsolated dvr->query_ );
Local<Array> phmap = Local<Array>::New( nkPreIsolated dvr->phmap_ );
if( args[0]->IsUndefined() || prepared->IntegerValue() == KWAERI_VALS_PREP_END || ( prepared->IntegerValue() != KWAERI_CLAUSE_PREP && prepared->IntegerValue() != KWAERI_VALS_PREP_START ) || ( type->IntegerValue() != KWAERI_INSERT && type->IntegerValue() != KWAERI_UPDATE ) )
{
std::cout << "Error: Escaped .Values" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
// Get some controls in place
std::string qp;
bool multi = false, adding = false;
if( args[0]->IsArray() )
{
multi = true;
}
else if( prepared->IntegerValue() == KWAERI_VALS_PREP_START )
{
adding = true;
}
// If it's an insert query:
if( type->IntegerValue() == KWAERI_INSERT )
{ // We could pass an array of objects which contain values for multiple records to be inserted
if( !adding )
{
qp = std::string( "VALUES ( " );
}
// If the argument is an array, we're inserting multiple records
if( multi )
{ // Add on to the insert clause, providing values
Local<Array> mvals = Local<Array>::Cast( args[0] );
Local<Array> phspec = Array::New( nkIsolate );
// We'll set the phspec index to an empty array so that the length is 1 :)
phmap->Set( 0, Array::New( nkIsolate ) );
// This tells us how many times to loop when executing the prepared statement (we only need to do this once once);
phspec->Set( 0, Integer::New( nkPreIsolated mvals->Length() ) );
bool prepped = false;
for( int i = 0, l = mvals->Length(); i < l; i++ )
{ // Here we take each object in our array and create the parenthesized record
Handle<Object> vals = mvals->Get( i )->ToObject();
Local<Array> keys = vals->GetOwnPropertyNames(); // Gets us all the keys in an array
for( int ii = 0, ll = keys->Length(); ii < ll; ii++ )
{
// Get the keys to build the VALUES clause, and replace values with question mark
Local<String> k = keys->Get( ii )->ToString();
Local<Array> kvpair = Array::New( nkIsolate ); // Stores the key value pair for the place-holder map
// Because we need all place-holder values to contain a key and value to work with, and because we cannot allow place-holders to be overwritten;
kvpair->Set( 0, k );
kvpair->Set( 1, vals->Get( k )->ToString() );
// Store the key/value pair for the place-holder in the order they are added to the clause.
phmap->Set( phmap->Length(), kvpair ); // Using the Length() property ensures that we always set the next index in the array.
if( !prepped )
{
if( ii != 0 )
{
qp += ", ";
}
qp += "?";
}
}
if( !prepped )
{
qp += " ) ";
prepped = true;
}
if( i == 0 )
{
// This tells us how many parameters are included in each loop (again, only need to do this once)
phspec->Set( 1, Integer::New( nkPreIsolated keys->Length() ) );
}
}
// Set the phmap metadata in the first index
phmap->Set( 0, phspec );
}
else
{ // Add on to the insert clause, providing values
Handle<Object> vals = args[0]->ToObject();
Local<Array> keys = vals->GetOwnPropertyNames(); // Gets us all the keys in an array
Local<Array> phspec = Array::New( nkIsolate );
// This tells us how many times to loop when executing the prepared statement (we only need to do this once once);
int lcount = 1;
if( adding )
{
phspec = Local<Array>::Cast( phmap->Get( 0 ) );
lcount += phspec->Get( 0 )->IntegerValue();
phspec->Set( 0, Integer::New( nkPreIsolated lcount ) );
}
else
{
phspec->Set( 0, Integer::New( nkPreIsolated lcount ) );
phspec->Set( 1, Integer::New( nkPreIsolated keys->Length() ) ); // We only need this the first time on an insert statement that's being looped
}
phmap->Set( 0, phspec );
for( int i = 0, l = keys->Length(); i < l; i++ )
{
// Get the keys to build the VALUES clause, and replace values with question mark
Local<String> k = keys->Get( i )->ToString();
Local<Array> kvpair = Array::New( nkIsolate ); // Stores the key value pair for the place-holder map
// Create the key/value pair
kvpair->Set( 0, k );
kvpair->Set( 1, vals->Get( k )->ToString() );
// Store the key/value pair for the place-holder in the order they are added to the clause.
phmap->Set( phmap->Length(), kvpair ); // Using the Length() property ensures that we always set the next index in the array.
if( !adding )
{
if( i != 0 )
{
qp += ", ";
}
qp += "?";
}
}
if( !adding )
{
qp += " )";
}
}
}
else if( type->IntegerValue() == KWAERI_UPDATE )
{
// Add on to the update clause, providing values
Handle<Object> vals = args[0]->ToObject();
Local<Array> keys = vals->GetOwnPropertyNames(); // Gets us all the keys in an array
Local<Array> phspec = Array::New( nkIsolate );
// This tells us how many times to loop when executing the prepared statement (we only need to do this once once);
int lcount = 1;
if( adding )
{
phspec = Local<Array>::Cast( phmap->Get( 0 ) );
lcount += phspec->Get( 0 )->IntegerValue();
phspec->Set( KWAERI_PH_KEY, Integer::New( nkPreIsolated lcount ) );
}
else
{
phspec->Set( KWAERI_PH_KEY, Integer::New( nkPreIsolated lcount ) );
phspec->Set( KWAERI_PH_VALUE, Integer::New( nkPreIsolated keys->Length() ) ); // We only need this the first time on an insert statement that's being looped
qp = std::string( "SET " );
}
phmap->Set( KWAERI_PH_KEY, phspec );
for( int i = 0, l = keys->Length(); i < l; i++ )
{
// Get the keys to build the where clause, and replace values with question mark
Local<String> k = keys->Get( i )->ToString();
Local<Array> kvpair = Array::New( nkIsolate ); // Stores the key value pair for the place-holder map
// Create the key/value pair
kvpair->Set( KWAERI_PH_KEY, k );
kvpair->Set( KWAERI_PH_VALUE, vals->Get( k ) );
// Store the values and conditions for the placeholders in the order they are added to the clause
phmap->Set( phmap->Length(), kvpair );
nkV8StringValueType kav( k );
std::string kv = *kav;
if( !adding )
{
if( i != 0 )
{
qp += ", ";
}
qp += kv + "=? ";
}
}
}
// Convert the std string to a v8 string
Handle<Value> qph = nkNewV8String( qp.c_str() );
nkV8StringValueType qpav( qph );
Local<String> qps = nkNewV8String( *qpav );
// Concatenate the new query part with the existing query string
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), qps->ToString() )->ToString() );
if( !multi )
{
nkResetV8PInteger( dvr->prepared_, KWAERI_VALS_PREP_START );
}
else
{
nkResetV8PInteger( dvr->prepared_, KWAERI_VALS_PREP_END );
}
// Return the entire object to allow chaining.
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Builds a UPDATE clause
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Update( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> type = Local<Integer>::New( nkPreIsolated dvr->type_ );
Local<String> query = Local<String>::New( nkPreIsolated dvr->query_ );
if( args[0]->IsUndefined() || ( type->IntegerValue() != KWAERI_EMPTY ) )
{
std::cout << "Error: Escaped .Update" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
// Get the name of the database to update and convert it to a std string
nkV8StringValueType av( args[0] );
std::string update = *av;
// Build our query string part
std::string qp = std::string( "UPDATE " );
qp += update + " ";
// Convert the query part from std string to v8 string
Handle<Value> qph = nkNewV8String( qp.c_str() );
nkV8StringValueType qpav( qph );
Local<String> qps = nkNewV8String( *qpav );
// Update the query string
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), qps->ToString() )->ToString() );
nkResetV8PInteger( dvr->type_, KWAERI_UPDATE );
nkResetV8PInteger( dvr->prepared_, KWAERI_CLAUSE_PREP );
// Return the entire object to allow chaining
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Builds a DELETE clause
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Delete( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> type = Local<Integer>::New( nkPreIsolated dvr->type_ );
Local<String> query = Local<String>::New( nkPreIsolated dvr->query_ );
if( args[0]->IsUndefined() || type->IntegerValue() != KWAERI_EMPTY )
{
std::cout << "Error: Escaped .Delete" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
// Fetch the user input (database name) and convert it to a std string
nkV8StringValueType av( args[0] );
std::string remove = *av;
// Build our query string part
std::string qp = std::string( "DELETE FROM " );
qp += remove;
// Convert the std string to a v8 string
Handle<Value> qph = nkNewV8String( qp.c_str() );
nkV8StringValueType qpav( qph );
Local<String> qps = nkNewV8String( *qpav );
// Update the query string
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), qps->ToString() )->ToString() );
nkResetV8PInteger( dvr->type_, KWAERI_DELETE );
nkResetV8PInteger( dvr->prepared_, KWAERI_CLAUSE_PREP );
// Return the entire object to allow for chaining
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Builds a WHERE clause
*
* @param const ARGUMENTS& ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Where( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> type = Local<Integer>::New( nkPreIsolated dvr->type_ );
Local<Integer> prepared = Local<Integer>::New( nkPreIsolated dvr->prepared_ );
Local<String> query = Local<String>::New( nkPreIsolated dvr->query_ );
Local<Array> phmap = Local<Array>::New( nkPreIsolated dvr->phmap_ );
// The supplied argument should be an object with arrays defining operators, types, and values for each where clause argument
if( args[0]->IsUndefined() || !args[0]->IsObject() || prepared->IntegerValue() < KWAERI_CLAUSE_PREP || type->IntegerValue() == KWAERI_INSERT || type->IntegerValue() == KWAERI_NOPREP )
{
std::cout << "Error: Escaped .Where" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
// We'll need to make sure that the phmap has its specs set so Length() returns at least 1
if( phmap->Length() < 1 )
{
Local<Array> phspec = Array::New( nkIsolate );
phspec->Set( 0, Integer::New( nkPreIsolated 1 ) );
phspec->Set( 1, Integer::New( nkPreIsolated 0 ) );
phmap->Set( 0, phspec );
}
// Now prepare to modify the phspec array as needed
Local<Array> ophspec = Local<Array>::Cast( phmap->Get( 0 ) );
Local<Array> phspec = Array::New( nkIsolate );
phspec->Set( 0, ophspec->Get( 0 ) ); // This flag could sometimes increase from the use of this method, such as from a DELETE statement, but we won't mess with that quite yet
int cindex = 0; // Holds the total number of parameters that are being added to the phmap for this clause
// Begin building our where clause
std::string qp = " WHERE ( ";
// We loop through the where object instead of setting it as the value of phmap_ because Update/Set/Where also uses the placeholder map in Set and Where at times.
Handle<Object> where = args[0]->ToObject();
Local<Array> keys = where->GetOwnPropertyNames(); // Gets us all the keys for an object inside of an array
bool prevor = false; // A flag for determining if additional where clause arguments should be and'd or or'd
for( int i = 0, l = keys->Length(); i < l; i++ )
{
// Get the key for this where clause argument, as well as the data array which defines the operator to use, the type to set, and the value to set.
Local<String> k = keys->Get( i )->ToString();
Local<Array> data = Local<Array>::Cast( where->Get( k ) );
// Convert the key and the operator to std:string
nkV8StringValueType kav( k ), oav( data->Get( KWAERI_OPERATOR ) );
std::string kv = *kav, ov = *oav;
// Build upon the query string
if( i != 0 && !prevor )
{
qp += " AND ";
}
else if( i != 0 && prevor )
{
qp += " OR ";
prevor = false; // Don't forget to reset this flag!
}
// Add the key and operator value to the query string
qp += kv + " " + ov;
int acount = data->Length();
// The 'where' array could have more than 2 elements if - for instance - they are using a BETWEEN or providing an OR operator.
if( acount > 2 )
{ // We need to know our operator to determine how to build the string
if( ov == std::string( "BETWEEN" ) )
{ // Fancy up our string
qp += "( ";
// Start with 1 (cuz 0 is the operator)
for( int ii = KWAERI_WHERE_VALUE; ii < acount; ii++ )
{ // After the first iteration we'll need to AND or OR the additional values, respectively
if( ii > 1 )
{
qp += " AND ";
}
Local<Array> kvpair = Array::New( nkIsolate ); // Stores the key value pair for the place-holder map
// Create the key/value pair
kvpair->Set( KWAERI_PH_KEY, k );
kvpair->Set( KWAERI_PH_VALUE, data->Get( ii ) );
// Store the values and conditions for the placeholders in the order they are added to the clause
phmap->Set( phmap->Length(), kvpair );
cindex++;
qp += "?";
}
// Finish fancying up our string
qp += " ) ";
}
else
{ // If there are more than two elements, but it's not BETWEEN for the operator, the user is adding an OR statement for the current condition, or an OR statement for the next condition
nkV8StringValueType waav( data->Get( KWAERI_WHERE_ADD ) );
std::string wav = *waav;
if( wav == std::string( "OR" ) || wav == std::string( "or" ) )
{ // If the user is OR'ing for the current condition, they'll have actually supplied 4 or more elements..
if( acount > 3 )
{ // Fancy up our string
qp += "( ";
for( int ii = 1; ii < acount; ii++ )
{
if( ii > 1 )
{
qp += " OR ";
}
Local<Array> kvpair = Array::New( nkIsolate ); // Stores the key value pair for the place-holder map
// Create the key/value pair
kvpair->Set( KWAERI_PH_KEY, k );
kvpair->Set( KWAERI_PH_VALUE, data->Get( ii ) );
// Store the values and conditions for the placeholders in the order they are added to the clause
phmap->Set( phmap->Length(), kvpair );
cindex++;
qp += "?";
}
// Finish fancying up our string
qp += " ) ";
}
else
{ // If the user is OR'ing for the next condition, we just need to set prevor to true
prevor = true;
}
}
}
}
else
{
// Otherwise we set a single placeholder
Local<Array> kvpair = Array::New( nkIsolate ); // Stores the key value pair for the place-holder map
// Create the key/value pair
kvpair->Set( KWAERI_PH_KEY, k );
kvpair->Set( KWAERI_PH_VALUE, data->Get( KWAERI_WHERE_VALUE ) );
// Store the values and conditions for the placeholders in the order they are added to the clause
phmap->Set( phmap->Length(), kvpair );
cindex++;
qp += "?";
}
}
// Add on the new part.
qp += " )";
// Let's set the new parameter count to be that of the existing plus the new amount from this clause. Remember we are not allowing chaining yet.
phspec->Set( 1, Integer::New( nkPreIsolated ophspec->Get( 1 )->IntegerValue() + cindex ) );
phmap->Set( 0, phspec );
// Convert the query string part from std string to v8 string
Handle<Value> qph = nkNewV8String( qp.c_str() );
nkV8StringValueType qpav( qph );
Local<String> qps = nkNewV8String( *qpav );
// Concatenate the new query part with the existing query
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), qps->ToString() )->ToString() );
// Concatenate the new query part with the existing query
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), qps->ToString() )->ToString() );
nkResetV8PInteger( dvr->prepared_, KWAERI_WHERE_PREP_START );
// Return the entire object to allow chaining
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Sets tables to join to the query
*
* @param const ARGUMENTS& args ARGUMENTS passed during invocation
*
* @return String The current query string
*
* @since 0.0.1
*/
nkMethodReturnType Driver::Join( const nkMethodArgumentType& args )
{
nkIsolation( args )
nkScope
Driver* dvr = ObjectWrap::Unwrap<Driver>( nkUnwrapThis );
Local<Integer> prepared = Local<Integer>::New( nkPreIsolated dvr->prepared_ );
Local<String> query = Local<String>::New( nkPreIsolated dvr->query_ );
if( args[0]->IsUndefined() || prepared->IntegerValue() < KWAERI_CLAUSE_PREP )
{
std::cout << "Error: Escaped .Join" << std::endl;
Driver* rdvre = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvre->Wrap( args.This() );
nkReturn( args.This() )
}
// Get the database name for which we are joining
nkV8StringValueType av( args[0] );
std::string db = *av;
// Build our query string part
std::string qp = std::string( " JOIN " );
qp += db;
// Convert the query string part from std string to v8 string
Handle<Value> qph = nkNewV8String( qp.c_str() );
nkV8StringValueType qpav( qph );
Local<String> qps = nkNewV8String( *qpav );
// Concatenate the new query part with the existing query
nkResetV8PString( dvr->query_, String::Concat( query->ToString(), qps->ToString() )->ToString() );
nkResetV8PInteger( dvr->prepared_, KWAERI_JOIN_PREP );
// Return the entire object to allow chaining
Driver* rdvr = new Driver( dvr->host_, dvr->port_, dvr->db_, dvr->user_, dvr->password_, dvr->model_, dvr->modeled_, dvr->type_, dvr->prepared_, dvr->phmap_, dvr->mapped_, dvr->query_ );
rdvr->Wrap( args.This() );
nkReturn( args.This() )
}
/**
* Sets the 'ON' clause for joining tables
*