-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmysql.c
1341 lines (1149 loc) · 40.9 KB
/
mysql.c
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
#include <alloca.h>
#include <inttypes.h>
#include </home/matthew/janet/janet.h>
#include <stdio.h>
#include <mysql/mysql.h>
#include <string.h>
static Janet safe_ckeywordv(const char *s) {
return s ? janet_ckeywordv(s) : janet_wrap_nil();
}
static void stmt_panic(MYSQL_STMT *statement, const char *where) {
char buf[1024];
snprintf(buf, sizeof(buf), "%s failed: code=%d error=%s", where, mysql_stmt_errno(statement), mysql_stmt_error(statement));
janet_panic(buf);
}
static void conn_panic(MYSQL *conn, const char *where) {
char buf[1024];
snprintf(buf, sizeof(buf), "%s failed: code=%d error=%s", where, mysql_errno(conn), mysql_error(conn));
janet_panic(buf);
}
typedef struct {
unsigned long long affected_rows;
unsigned long long insert_id;
} jmy_result_t;
static int result_gc(void *p, size_t s) {
(void)p;
(void)s;
return 0;
}
static void result_to_string(void *p, JanetBuffer *buffer) {
jmy_result_t *result = (jmy_result_t *)p;
janet_buffer_push_cstring(buffer, "result: ");
char buf[1024];
sprintf(buf, "insert_id %llu affected_rows %llu", result->insert_id, result->affected_rows);
janet_buffer_push_cstring(buffer, buf);
}
static const JanetAbstractType result_type = {
"mysql/result",
result_gc,
NULL,
NULL,
NULL,
NULL,
NULL,
result_to_string,
NULL,
NULL,
NULL,
NULL
};
static Janet result_insert_id(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
jmy_result_t *result = (jmy_result_t *)janet_getabstract(argv, 0, &result_type);
return janet_wrap_number(result->insert_id);
}
static Janet result_affected_rows(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
jmy_result_t *result = (jmy_result_t *)janet_getabstract(argv, 0, &result_type);
return janet_wrap_number(result->affected_rows);
}
typedef struct {
MYSQL_STMT *statement;
MYSQL_RES *r;
int num_fields;
} jmy_rows_t;
static void __ensure_rows_ok(jmy_rows_t *ctx) {
if (ctx->r == NULL) {
janet_panic("mysql/rows is disconnected");
}
}
static int rows_gc(void *p, size_t s) {
(void)s;
jmy_rows_t *rows = (jmy_rows_t *)p;
if (rows->r) {
mysql_free_result(rows->r);
rows->r = NULL;
}
return 0;
}
static void rows_to_string(void *p, JanetBuffer *buffer) {
jmy_rows_t *rows = (jmy_rows_t *)p;
__ensure_rows_ok(rows);
janet_buffer_push_cstring(buffer, "result: ");
if (rows->r) {
janet_buffer_push_cstring(buffer, "...");
}
}
static const JanetAbstractType rows_type = {
"mysql/rows",
rows_gc,
NULL,
NULL,
NULL,
NULL,
NULL,
rows_to_string,
NULL,
NULL,
NULL,
NULL
};
static Janet rows_columns(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
jmy_rows_t *rows = (jmy_rows_t *)janet_getabstract(argv, 0, &rows_type);
__ensure_rows_ok(rows);
int n = mysql_num_fields(rows->r);
MYSQL_FIELD *mysqlFields = mysql_fetch_fields(rows->r);
JanetArray *a = janet_array(n);
for (int i = 0; i < n; i++) {
janet_array_push(a, janet_wrap_string(mysqlFields[i].name));
}
return janet_wrap_array(a);
}
static Janet rows_column_types(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
jmy_rows_t *rows = (jmy_rows_t *)janet_getabstract(argv, 0, &rows_type);
__ensure_rows_ok(rows);
int n = mysql_num_fields(rows->r);
MYSQL_FIELD *mysqlFields = mysql_fetch_fields(rows->r);
JanetArray *a = janet_array(n);
for (int i = 0; i < n; i++) {
janet_array_push(a, janet_wrap_number(mysqlFields[i].type));
}
return janet_wrap_array(a);
}
typedef struct {
MYSQL_STMT *statement;
} jmy_statement_t;
static void __ensure_stmt_ok(jmy_statement_t *stmt) {
if (stmt->statement == NULL) {
janet_panic("mysql/statement is closed");
}
}
static int statement_gc(void *p, size_t s) {
(void)s;
//printf("rows gc %p\n", p);
jmy_statement_t *rows = (jmy_statement_t *)p;
if (rows->statement) {
mysql_stmt_close(rows->statement);
rows->statement = NULL;
}
return 0;
}
static void statement_to_string(void *p, JanetBuffer *buffer) {
jmy_statement_t *stmt = (jmy_statement_t *)p;
(void)stmt;
janet_buffer_push_cstring(buffer, "statement: ");
}
static const JanetAbstractType statement_type = {
"mysql/statement",
statement_gc,
NULL,
NULL,
NULL,
NULL,
NULL,
statement_to_string,
NULL,
NULL,
NULL,
NULL
};
typedef struct {
MYSQL *conn;
bool in_transaction;
} jmy_context_t;
static void __ensure_ctx_ok(jmy_context_t *ctx) {
if (ctx->conn == NULL) {
janet_panic("mysql/context is disconnected");
}
}
static void context_close_i(jmy_context_t *ctx) {
if (ctx->conn) {
mysql_close(ctx->conn);
ctx->conn = NULL;
}
}
static int context_gc(void *p, size_t s) {
(void)s;
jmy_context_t *ctx = (jmy_context_t *)p;
context_close_i(ctx);
return 0;
}
static Janet context_close(int32_t argc, Janet *argv);
static JanetMethod context_methods[] = {
{"close", context_close}, /* So contexts can be used with 'with' */
{NULL, NULL}
};
static int context_get(void *ptr, Janet key, Janet *out) {
(void)ptr;
return janet_getmethod(janet_unwrap_keyword(key), context_methods, out);
}
static const JanetAbstractType context_type = {
"mysql/context",
context_gc,
NULL,
context_get,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
static Janet context_connect(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
// The configuration data is provided as a struct:
// :host
// :username
// :password
// :database
JanetStruct config = janet_getstruct(argv, 0);
Janet host = janet_struct_get(config, janet_ckeywordv("host"));
if (!janet_checktype(host, JANET_STRING)) {
janet_panicf("host is not a string");
}
Janet username = janet_struct_get(config, janet_ckeywordv("username"));
if (!janet_checktype(username, JANET_STRING)) {
janet_panicf("username is not a string");
}
Janet password = janet_struct_get(config, janet_ckeywordv("password"));
if (janet_checktype(password, JANET_NIL)) {
password = janet_wrap_string("");
}
if (!janet_checktype(password, JANET_STRING))
janet_panicf("password is not a string");
Janet database = janet_struct_get(config, janet_ckeywordv("database"));
if (janet_checktype(database, JANET_NIL)) {
database = janet_wrap_string("");
}
if (!janet_checktype(database, JANET_STRING)) {
janet_panicf("database is not a string");
}
jmy_context_t *ctx = (jmy_context_t *)janet_abstract(&context_type, sizeof(jmy_context_t));
MYSQL *conn = mysql_init(NULL);
// Unpack connection parameters.
if (mysql_real_connect(conn,
(const char *)janet_unwrap_string(host), (const char *)janet_unwrap_string(username),
(const char *)janet_unwrap_string(password), (const char *)janet_unwrap_string(database), 0, NULL, 0) == NULL) {
janet_panic("unable to create connection");
}
ctx->conn = conn;
ctx->in_transaction = false;
return janet_wrap_abstract(ctx);
}
static Janet context_select_db(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
jmy_context_t *ctx = (jmy_context_t *)janet_getabstract(argv, 0, &context_type);
__ensure_ctx_ok(ctx);
const char *db = janet_getcstring(argv, 1);
if (mysql_select_db(ctx->conn, db)) {
conn_panic(ctx->conn, "mysql_select_db");
}
return janet_wrap_nil();
}
static Janet context_prepare(int32_t argc, Janet *argv) {
if (argc < 2) {
janet_panic("expected at least a pq context and a query string");
}
if (argc > 10000000) {
janet_panic("too many arguments");
}
jmy_context_t *ctx = (jmy_context_t *)janet_getabstract(argv, 0, &context_type);
__ensure_ctx_ok(ctx);
const char *q = janet_getcstring(argv, 1);
int len = strlen(q);
argc -= 2;
argv += 2;
//puts("init");
MYSQL_STMT *statement = mysql_stmt_init(ctx->conn);
if (statement == NULL) {
conn_panic(ctx->conn, "mysql_stmt_init");
}
if (mysql_stmt_prepare(statement, q, len)) {
mysql_stmt_close(statement);
conn_panic(ctx->conn, "mysql_stmt_prepare");
}
jmy_statement_t *result = (jmy_statement_t *)janet_abstract(&statement_type, sizeof(jmy_statement_t));
result->statement = statement;
return janet_wrap_abstract(result);
}
typedef struct {
MYSQL_BIND *binds;
bool *nulls;
unsigned long *lengths;
bool *errors;
int len;
} jmy_query_bind_t;
static void query_bind_free(jmy_query_bind_t binds) {
if (binds.binds == NULL) {
return;
}
for (int i = 0; i < binds.len; i++) {
janet_sfree(binds.binds[i].buffer);
}
janet_sfree(binds.nulls);
janet_sfree(binds.lengths);
janet_sfree(binds.errors);
}
static jmy_query_bind_t allocate_binds(int num_fields, MYSQL_FIELD *fields) {
MYSQL_BIND *binds = (MYSQL_BIND *)janet_smalloc(sizeof(MYSQL_BIND) * num_fields);
memset(binds, 0, sizeof(MYSQL_BIND) * num_fields);
bool *nulls = (bool *)janet_smalloc(sizeof(bool) * num_fields);
memset(nulls, 0, sizeof(bool) * num_fields);
unsigned long *lengths = janet_smalloc(sizeof(unsigned long) * num_fields);
memset(lengths, 0, sizeof(unsigned long) * num_fields);
bool *errors = janet_smalloc(sizeof(bool) * num_fields);
memset(errors, 0, sizeof(bool) * num_fields);
for (int i = 0; i < num_fields; ++i) {
unsigned long len = 0;
switch (fields[i].type) {
case MYSQL_TYPE_TINY:
len = 1;
break;
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_YEAR:
len = 2;
break;
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
len = 4;
break;
case MYSQL_TYPE_LONGLONG:
len = 8;
break;
case MYSQL_TYPE_FLOAT:
len = 4;
break;
case MYSQL_TYPE_DOUBLE:
len = 8;
break;
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_DATETIME:
len = sizeof(MYSQL_TIME);
break;
case MYSQL_TYPE_JSON:
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
len = fields[i].max_length;
break;
default:
janet_panicf("unknown field type %d\n", fields[i].type);
}
binds[i].buffer_type = fields[i].type;
binds[i].buffer = janet_smalloc(len);
memset(binds[i].buffer, 0, len);
binds[i].buffer_length = len;
binds[i].is_null = &nulls[i];
binds[i].length = &lengths[i];
binds[i].error = &errors[i];
}
jmy_query_bind_t b = {binds, nulls, lengths, errors, num_fields};
return b;
}
typedef struct {
MYSQL_BIND *binds;
unsigned long *lengths;
int len;
} jmy_exec_bind_t;
static void exec_bind_free(jmy_exec_bind_t binds) {
if (binds.binds == NULL) {
return;
}
for (int i = 0; i < binds.len; i++) {
switch (binds.binds[i].buffer_type) {
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_DOUBLE:
janet_sfree(binds.binds[i].buffer);
break;
default:
break;
}
}
janet_sfree(binds.lengths);
janet_sfree(binds.binds);
}
static jmy_exec_bind_t create_exec_bind(int32_t argc, Janet *argv) {
if (argc == 0) {
jmy_exec_bind_t t = { NULL, NULL, 0};
return t;
}
MYSQL_BIND *binds = (MYSQL_BIND *)janet_smalloc(argc * sizeof(MYSQL_BIND));
memset(binds, 0, argc * sizeof(MYSQL_BIND));
unsigned long *lengths = (unsigned long *)janet_smalloc(sizeof(unsigned long) * argc);
memset(lengths, 0, argc * sizeof(unsigned long));
for (int i = 0; i < argc; i++) {
Janet j = argv[i];
switch (janet_type(j)) {
case JANET_NIL: {
binds[i].buffer_type = MYSQL_TYPE_NULL;
bool *v = (bool *)janet_smalloc(sizeof(bool));
*v = true;
binds[i].is_null = v;
break;
}
case JANET_BOOLEAN: {
binds[i].buffer_type = MYSQL_TYPE_TINY;
bool *v = (bool *)janet_smalloc(sizeof(bool));
*v = janet_unwrap_boolean(j);
binds[i].buffer = v;
break;
}
case JANET_BUFFER: {
binds[i].buffer_type = MYSQL_TYPE_STRING;
JanetBuffer *b = janet_unwrap_buffer(j);
binds[i].buffer = b->data;
lengths[i] = b->count;
binds[i].length = &lengths[i];
break;
}
case JANET_KEYWORD:
case JANET_STRING: {
binds[i].buffer_type = MYSQL_TYPE_STRING;
const char *s = (const char *)janet_unwrap_string(j);
binds[i].buffer = (void *)s;
lengths[i] = janet_string_length(s);
binds[i].length = &lengths[i];
break;
}
case JANET_NUMBER: {
binds[i].buffer_type = MYSQL_TYPE_DOUBLE;
double *v = janet_smalloc(sizeof(double));
*v = janet_unwrap_number(j);
binds[i].buffer = v;
break;
}
case JANET_ABSTRACT: {
JanetIntType intt = janet_is_int(j);
if (intt == JANET_INT_S64 || intt == JANET_INT_U64) {
binds[i].buffer_type = MYSQL_TYPE_LONGLONG;
long long *v = janet_smalloc(sizeof(long long));
if (intt == JANET_INT_S64) {
*v = janet_unwrap_s64(j);
} else {
*v = (long long)janet_unwrap_u64(j);
}
binds[i].buffer = v;
break;
}
break;
}
/* fall-thru */
default:
janet_panicf("cannot encode janet type %d", janet_type(j));
}
}
jmy_exec_bind_t b = {
binds,
lengths,
argc,
};
return b;
}
static Janet stmt_exec(jmy_statement_t *stmt, int32_t argc, Janet *argv) {
MYSQL_STMT *statement = stmt->statement;
unsigned long param_count = mysql_stmt_param_count(statement);
if ((unsigned long)argc != param_count) {
janet_panicf("query: wrong arity %d expected got %d\n", param_count, argc);
}
jmy_exec_bind_t binds = create_exec_bind(argc, argv);
if (binds.binds != NULL) {
if (mysql_stmt_bind_param(statement, binds.binds)) {
stmt_panic(statement, "mysql_stmt_bind_param");
}
}
if (mysql_stmt_execute(statement)) {
stmt_panic(statement, "mysql_stmt_execute");
}
exec_bind_free(binds);
int num_fields = mysql_stmt_field_count(statement);
if (num_fields > 0) {
janet_panicf("field_count is %d not zero", num_fields);
}
jmy_result_t *result = (jmy_result_t *)janet_abstract(&result_type, sizeof(jmy_result_t));
result->affected_rows = mysql_stmt_affected_rows(statement);
result->insert_id = mysql_stmt_insert_id(statement);
return janet_wrap_abstract(result);
}
static Janet stmt_select(jmy_statement_t *stmt, int32_t argc, Janet *argv) {
argc -= 1;
argv += 1;
MYSQL_STMT *statement = stmt->statement;
unsigned long param_count = mysql_stmt_param_count(statement);
if ((unsigned long)argc != param_count) {
janet_panicf("query: wrong arity %d expected got %d\n", param_count, argc);
}
jmy_exec_bind_t binds = create_exec_bind(argc, argv);
if (binds.binds != NULL) {
if (mysql_stmt_bind_param(statement, binds.binds)) {
stmt_panic(statement, "mysql_stmt_bind_param");
}
}
if (mysql_stmt_execute(statement)) {
stmt_panic(statement, "mysql_stmt_execute");
}
exec_bind_free(binds);
/* the column count is > 0 if there is a result set */
/* 0 if the result is only the final status packet */
int num_fields = mysql_stmt_field_count(statement);
if (num_fields == 0) {
janet_panicf("unexpected field_count is %d not zero", num_fields);
}
bool truth = 1;
if (mysql_stmt_attr_set(statement, STMT_ATTR_UPDATE_MAX_LENGTH, &truth) != 0) {
stmt_panic(statement, "mysql_stmt_attr_set");
}
if (mysql_stmt_store_result(statement)) {
stmt_panic(statement, "mysql_stmt_store_result");
}
MYSQL_RES *r = mysql_stmt_result_metadata(statement);
if (!r) {
stmt_panic(statement, "mysql_stmt_result_metadata");
}
jmy_rows_t *rows = (jmy_rows_t *)janet_abstract(&rows_type, sizeof(jmy_rows_t));
rows->statement = statement;
rows->num_fields = num_fields;
rows->r = r;
return janet_wrap_abstract(rows);
}
static Janet stmt_close(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
jmy_statement_t *stmt = (jmy_statement_t *)janet_getabstract(argv, 0, &statement_type);
__ensure_stmt_ok(stmt);
if (stmt->statement != NULL) {
mysql_stmt_close(stmt->statement);
stmt->statement = NULL;
}
return janet_wrap_nil();
}
static Janet decode_text(char *v, unsigned long l, MYSQL_FIELD *field) {
if (v == NULL) {
return janet_wrap_nil();
}
Janet jv;
switch (field->type) {
case MYSQL_TYPE_NULL:
jv = janet_wrap_nil();
break;
case MYSQL_TYPE_TINY: {
int i = atoi(v);
if (field->length == 1) {
jv = janet_wrap_boolean(i != 0);
} else {
jv = janet_wrap_number(i);
}
break;
}
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_YEAR: {
int i = atoi(v);
jv = janet_wrap_number(i);
break;
}
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG: {
long i = atol(v);
jv = janet_wrap_number(i);
break;
}
case MYSQL_TYPE_FLOAT: {
float i = atol(v);
jv = janet_wrap_number(i);
break;
}
case MYSQL_TYPE_DOUBLE: {
double i = atof(v);
jv = janet_wrap_number(i);
break;
}
case MYSQL_TYPE_LONGLONG: {
long long int i = atoll(v);
jv = janet_wrap_number(i);
break;
}
case MYSQL_TYPE_DATE: {
MYSQL_TIME t;
memset(&t, 0, sizeof(MYSQL_TIME));
sscanf(v, "%d-%d-%d", &t.year, &t.month, &t.day);
JanetKV *st = janet_struct_begin(3);
janet_struct_put(st, janet_ckeywordv("day"), janet_wrap_number(t.day));
janet_struct_put(st, janet_ckeywordv("month"), janet_wrap_number(t.month));
janet_struct_put(st, janet_ckeywordv("year"), janet_wrap_number(t.year));
jv = janet_wrap_struct(janet_struct_end(st));
break;
}
case MYSQL_TYPE_TIME: {
MYSQL_TIME t;
memset(&t, 0, sizeof(MYSQL_TIME));
sscanf(v, "%d:%d:%d", &t.hour, &t.minute, &t.second);
JanetKV *st = janet_struct_begin(3);
janet_struct_put(st, janet_ckeywordv("seconds"), janet_wrap_number(t.second));
janet_struct_put(st, janet_ckeywordv("minutes"), janet_wrap_number(t.minute));
janet_struct_put(st, janet_ckeywordv("hours"), janet_wrap_number(t.hour));
jv = janet_wrap_struct(janet_struct_end(st));
break;
}
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP2: {
MYSQL_TIME t;
memset(&t, 0, sizeof(MYSQL_TIME));
if (l == 19) {
sscanf(v, "%d-%d-%d %d:%d:%d", &t.year, &t.month, &t.day, &t.hour, &t.minute, &t.second);
} else {
sscanf(v, "%d-%d-%d %d:%d:%d.%ld", &t.year, &t.month, &t.day, &t.hour, &t.minute, &t.second, &t.second_part);
}
int count;
if (field->type == MYSQL_TYPE_DATETIME) {
count = 8;
} else {
count = 7;
}
JanetKV *st = janet_struct_begin(count);
janet_struct_put(st, janet_ckeywordv("microseconds"), janet_wrap_number(t.second_part));
janet_struct_put(st, janet_ckeywordv("seconds"), janet_wrap_number(t.second));
janet_struct_put(st, janet_ckeywordv("minutes"), janet_wrap_number(t.minute));
janet_struct_put(st, janet_ckeywordv("hours"), janet_wrap_number(t.hour));
janet_struct_put(st, janet_ckeywordv("day"), janet_wrap_number(t.day));
janet_struct_put(st, janet_ckeywordv("month"), janet_wrap_number(t.month));
janet_struct_put(st, janet_ckeywordv("year"), janet_wrap_number(t.year));
if (field->type == MYSQL_TYPE_DATETIME) {
janet_struct_put(st, janet_ckeywordv("tz"), janet_wrap_number(t.time_zone_displacement));
}
jv = janet_wrap_struct(janet_struct_end(st));
break;
}
//MYSQL_TYPE_ENUM = 247,
//MYSQL_TYPE_SET = 248,
case MYSQL_TYPE_JSON:
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
jv = janet_wrap_string(janet_string((uint8_t *)v, l));
break;
//MYSQL_TYPE_GEOMETRY = 255
default:
janet_panicf("unexpected mysql type %d\n", field->type);
}
return jv;
}
static Janet rows_text_unpack(jmy_rows_t *rows) {
int n = mysql_num_rows(rows->r);
int num_fields = mysql_num_fields(rows->r);
MYSQL_FIELD *fields = mysql_fetch_fields(rows->r);
JanetArray *a = janet_array(n);
for (int i = 0; i < n; i++) {
JanetTable *t = janet_table(num_fields);
MYSQL_ROW row = mysql_fetch_row(rows->r);
unsigned long *lengths = mysql_fetch_lengths(rows->r);
for (int j = 0; j < num_fields; j++) {
Janet jv = decode_text(row[j], lengths[j], &fields[j]);
Janet k = safe_ckeywordv(fields[j].name);
janet_table_put(t, k, jv);
}
janet_array_push(a, janet_wrap_table(t));
}
return janet_wrap_array(a);
}
static Janet decode_binary(MYSQL_BIND *bind, MYSQL_FIELD *field) {
if (*bind->is_null) {
return janet_wrap_nil();
}
int t = bind->buffer_type;
char *v = bind->buffer;
int l = *bind->length;
Janet jv;
switch (t) {
case MYSQL_TYPE_NULL:
jv = janet_wrap_nil();
break;
case MYSQL_TYPE_TINY:
if (field->length == 1) {
jv = janet_wrap_boolean(*((char *)v));
} else {
jv = janet_wrap_number(*((char *)v));
}
break;
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_YEAR:
jv = janet_wrap_number(*((short int *)v));
break;
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
jv = janet_wrap_number(*((int *)v));
break;
case MYSQL_TYPE_FLOAT:
jv = janet_wrap_number(*((float *)v));
break;
case MYSQL_TYPE_DOUBLE:
jv = janet_wrap_number(*((double *)v));
break;
case MYSQL_TYPE_LONGLONG:
jv = janet_wrap_number(*((long long int *)v));
break;
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP2: {
MYSQL_TIME t = *(MYSQL_TIME *)v;
switch (t.time_type) {
case MYSQL_TIMESTAMP_DATE: {
JanetKV *st = janet_struct_begin(3);
janet_struct_put(st, janet_ckeywordv("day"), janet_wrap_number(t.day));
janet_struct_put(st, janet_ckeywordv("month"), janet_wrap_number(t.month));
janet_struct_put(st, janet_ckeywordv("year"), janet_wrap_number(t.year));
jv = janet_wrap_struct(janet_struct_end(st));
break;
}
//Value is in UTC for `TIMESTAMP` type.
//Value is in local time zone for `DATETIME` type.
case MYSQL_TIMESTAMP_DATETIME: {
int count;
if (field->type != MYSQL_TYPE_TIMESTAMP && field->type != MYSQL_TYPE_TIMESTAMP2) {
count = 8;
} else {
count = 7;
}
JanetKV *st = janet_struct_begin(count);
janet_struct_put(st, janet_ckeywordv("microseconds"), janet_wrap_number(t.second_part));
janet_struct_put(st, janet_ckeywordv("seconds"), janet_wrap_number(t.second));
janet_struct_put(st, janet_ckeywordv("minutes"), janet_wrap_number(t.minute));
janet_struct_put(st, janet_ckeywordv("hours"), janet_wrap_number(t.hour));
janet_struct_put(st, janet_ckeywordv("day"), janet_wrap_number(t.day));
janet_struct_put(st, janet_ckeywordv("month"), janet_wrap_number(t.month));
janet_struct_put(st, janet_ckeywordv("year"), janet_wrap_number(t.year));
if (field->type != MYSQL_TYPE_TIMESTAMP && field->type != MYSQL_TYPE_TIMESTAMP2) {
janet_struct_put(st, janet_ckeywordv("tz"), janet_wrap_number(t.time_zone_displacement));
}
jv = janet_wrap_struct(janet_struct_end(st));
break;
}
/// Stores hour, minute, second and microsecond.
case MYSQL_TIMESTAMP_TIME: {
JanetKV *st = janet_struct_begin(3);
janet_struct_put(st, janet_ckeywordv("seconds"), janet_wrap_number(t.second));
janet_struct_put(st, janet_ckeywordv("minutes"), janet_wrap_number(t.minute));
janet_struct_put(st, janet_ckeywordv("hours"), janet_wrap_number(t.hour));
jv = janet_wrap_struct(janet_struct_end(st));
break;
}
case MYSQL_TIMESTAMP_NONE:
case MYSQL_TIMESTAMP_ERROR:
case MYSQL_TIMESTAMP_DATETIME_TZ:
janet_panicf("unexpected time type %d\n", t.time_type);
break;
}
}
break;
case MYSQL_TYPE_JSON:
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
jv = janet_wrap_string(janet_string((uint8_t *)v, l));
break;
default:
//MYSQL_TYPE_ENUM = 247,
//MYSQL_TYPE_SET = 248,
//MYSQL_TYPE_GEOMETRY = 255
janet_panicf("unexpected mysql type %d\n", t);
}
return jv;
}
static Janet rows_binary_unpack(jmy_rows_t *rows) {
int num_fields = rows->num_fields;
MYSQL_FIELD *fields = mysql_fetch_fields(rows->r);
jmy_query_bind_t binds = allocate_binds(num_fields, fields);
if (mysql_stmt_bind_result(rows->statement, binds.binds)) {
stmt_panic(rows->statement, "mysql_stmt_bind_result");
}
JanetArray *a = janet_array(0);
while (1) {
int status = mysql_stmt_fetch(rows->statement);
if (status == 1 || status == MYSQL_NO_DATA) {
break;
}
if (status == MYSQL_DATA_TRUNCATED) {
janet_panicf("mysql_stmt_fetch failed: MYSQL_DATA_TRUNCATED\n");
}
JanetTable *t = janet_table(num_fields);
for (int j = 0; j < num_fields; ++j) {
if (*binds.binds[j].error) {
janet_panicf("unexpected error in field %d", j);
}
Janet jv = decode_binary(&binds.binds[j], &fields[j]);
Janet k = safe_ckeywordv(fields[j].name);
janet_table_put(t, k, jv);
}
janet_array_push(a, janet_wrap_table(t));
}
mysql_free_result(rows->r);
rows->r = NULL;
query_bind_free(binds);
return janet_wrap_array(a);
}
static Janet rows_unpack(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
jmy_rows_t *rows = (jmy_rows_t *)janet_getabstract(argv, 0, &rows_type);
__ensure_rows_ok(rows);
if (rows->statement == NULL) {
return rows_text_unpack(rows);
} else {
return rows_binary_unpack(rows);
}
}
static int count_subs(const char *q) {
int count = 0;
while (*q != '\0') {
if (*q == '?') {
count++;
}
q++;
}
return count;
}
static char *interpolate_params(MYSQL *conn, const char *q, size_t len, int32_t argc, Janet *argv) {
int n_subs = count_subs(q);
char **pvals = janet_smalloc(n_subs * sizeof(char *));
int *plengths = janet_smalloc(n_subs * sizeof(int));
if (n_subs != argc) {
janet_panicf("query: wrong arity %d expected got %d\n", n_subs, argc);
}
for (int i = 0; i < argc; i++) {
Janet j = argv[i];
switch (janet_type(j)) {
case JANET_NIL: {
const char *null = "NULL";
size_t len = strlen(null);
pvals[i] = janet_smalloc(len + 1);
strcpy(pvals[i], null);
plengths[i] = len;
break;
}
case JANET_BOOLEAN: {
pvals[i] = janet_smalloc(2);