File: mys.c

package info (click to toggle)
golf 601.4.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,824 kB
  • sloc: ansic: 20,020; sh: 1,171; makefile: 292
file content (532 lines) | stat: -rw-r--r-- 17,800 bytes parent folder | download
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018-2025 Gliim LLC. 
// Licensed under Apache License v2. See LICENSE file.
// On the web http://golf-lang.com/ - this file is part of Golf framework.

// 
// mariadb native interface, used by db.c for making of db-specific libs
//
// All errors must go through gg_maria_error() and the sole point of entry is gg_maria_exec()
// in order to avoid disorderly rollbacks in case of errors
//

#include "golf.h"
static gg_num gg_maria_stmt_exec();
static int gg_maria_stmt_rows (char ***row, unsigned long **lens);
static gg_num gg_maria_stmt_exec();
static void gg_maria_add_input(gg_num i, char *arg);
static void gg_maria_bind_input ();
static int gg_maria_prep_stmt(void **prep, char *stmt, gg_num num_of_args);

static char *cerror = NULL;


gg_num gg_maria_checkc()
{
    GG_TRACE("");
    return (mysql_ping (GG_CURR_DB.dbc->maria.con) == 0 ? 1 : 0);
}

char *gg_maria_errm(char *errm, gg_num errmsize, char *s, char *sname, gg_num lnum, char *er, char is_prep)
{
    GG_TRACE("");
    if (is_prep == 0)
    {
        snprintf(errm,errmsize,"Error during query [%s], additional [%s] file [%s], line [%ld] : [%s]%s", s, cerror==NULL?"":cerror, sname, lnum, er ,atol(er) == ER_PARSE_ERROR ?  "Problem with parsing SQL statement" : mysql_error(GG_CURR_DB.dbc->maria.con));
        return errm;
    }
    else
    {
        snprintf(errm,errmsize,"Error during query [%s], additional [%s] file [%s], line [%ld] : [%s]%s", s, cerror==NULL?"":cerror, sname, lnum, er ,atol(er) == ER_PARSE_ERROR ?  "Problem with parsing SQL statement" : mysql_stmt_error(GG_CURR_DB.dbc->maria.stmt));
        return errm;
    }
}


char *gg_maria_error(char *s, char is_prep)
{
    GG_TRACE("");
    GG_UNUSED(s); // used in tracing only
    if (is_prep == 0)
    {
        static char errv[30];
        snprintf (errv, sizeof(errv), "%d", mysql_errno(GG_CURR_DB.dbc->maria.con));
        GG_TRACE ("Error in %s: %s error %s state %s", s, mysql_error(GG_CURR_DB.dbc->maria.con), errv, mysql_sqlstate(GG_CURR_DB.dbc->maria.con));
        return errv;
    }
    else
    {
        static char errv[30];
        snprintf (errv, sizeof(errv), "%d", mysql_stmt_errno(GG_CURR_DB.dbc->maria.stmt));
        GG_TRACE ("Error in %s: %s error %s state %s", s, mysql_stmt_error(GG_CURR_DB.dbc->maria.stmt), errv, mysql_stmt_sqlstate(GG_CURR_DB.dbc->maria.stmt));
        return errv;
    }
}


int gg_maria_rows (char ***row, unsigned long **lens, char is_prep)
{
    GG_TRACE("");
    if (is_prep == 0)
    {
        GG_CURR_DB.need_copy = 1;
        // fetch a row, field by field
        MYSQL_ROW row1; // this is char **, if that changes than code here must be refactored, or made to use
                      // that new structure
        row1 = mysql_fetch_row(GG_CURR_DB.dbc->maria.res);
        *row = row1; // simple now, may not be in the future. In any case, the returning row variable must
                    // be an array of strings
        *lens = mysql_fetch_lengths (GG_CURR_DB.dbc->maria.res);
    }
    else
    {
        GG_CURR_DB.need_copy = 0;
        if (gg_maria_stmt_rows (row, lens)) return 1;
    }
    return 0;
}


//
// Get a row when tuples returned. *row[] is an array of strings which represent fields (columns) and *lens is the
// array of lengths of those fields.
//
int gg_maria_stmt_rows (char ***row, unsigned long **lens)
{
    GG_TRACE("");
    char *sname = "";
    gg_num lnum = 0;
    // get which file and line number is this going on at
    gg_location (&sname, &lnum, 0);

    // number of fields in a row
    gg_num nf = mysql_num_fields(GG_CURR_DB.dbc->maria.res);
    gg_num i;

    // get result binding array
    GG_CURR_DB.dbc->maria.bindout = (MYSQL_BIND*)gg_calloc(nf, sizeof(MYSQL_BIND));

    // get results (columns, lengths)
    *row = (char **)gg_calloc (nf, sizeof(char*));
    *lens = (unsigned long *)gg_calloc (nf, sizeof(unsigned long));

    // rlen is the array of actual lengths of an array of fields in a row, obtained from mariadb fetching
    unsigned long *rlen = (unsigned long*)gg_calloc (nf, sizeof(unsigned long));
    // array of whether fields are NULL
    my_bool *isnull = (char*)gg_calloc (nf, sizeof(char));
    // dummy storage for first pass of fetching
    char dummy[2]; // just in case give it storage
    // set all cols to strings, unknown length

    // set binding for results, we want all as strings. Length is 0 so we can obtain actual lengths in second pass.
    for (i = 0; i < nf; i++) 
    {
        GG_CURR_DB.dbc->maria.bindout[i].buffer_type = MYSQL_TYPE_STRING;
        GG_CURR_DB.dbc->maria.bindout[i].buffer = &(dummy[0]);
        GG_CURR_DB.dbc->maria.bindout[i].buffer_length = 0;
        GG_CURR_DB.dbc->maria.bindout[i].is_null = &(isnull[i]);
        GG_CURR_DB.dbc->maria.bindout[i].length = &(rlen[i]);
    }

    // bind results
    if (mysql_stmt_bind_result(GG_CURR_DB.dbc->maria.stmt, GG_CURR_DB.dbc->maria.bindout) != 0)
    {
        cerror = "Cannot initialize statement";
        return 1;
    }
    gg_num cdata = 0; // index of fields within a row

    //
    // Get a single row
    //
    // set empty for each row, because we don't know the size of its columns and each must be separately allocated
    for (i = 0; i < nf; i++) 
    {
        GG_CURR_DB.dbc->maria.bindout[i].buffer = &(dummy[0]);
        GG_CURR_DB.dbc->maria.bindout[i].buffer_length = 0;
    }
    // fetch a row
    int res = mysql_stmt_fetch(GG_CURR_DB.dbc->maria.stmt);
    // row is either okay or truncated, if not, error
    if (res != 0 && res != MYSQL_DATA_TRUNCATED) 
    {
        cerror = "Cannot fetch row";
        return 1;
    }
    if (res == MYSQL_DATA_TRUNCATED) 
    {
        // if truncated, now we can get actual lengths and allocate them
        for (i = 0; i < nf; i++) 
        {
            GG_CURR_DB.dbc->maria.bindout[i].buffer_length = rlen[i]+1;
            GG_CURR_DB.dbc->maria.bindout[i].buffer = gg_malloc (rlen[i] + 1 + 1); // just in case extra byte of storage
        }
        // now fetch columns based on actual lengths for each field
        for (i = 0; i < nf; i++) 
        {
            if (mysql_stmt_fetch_column(GG_CURR_DB.dbc->maria.stmt, &(GG_CURR_DB.dbc->maria.bindout[i]), i, 0) != 0)
            {
                cerror = "Cannot fetch data for column";
                return 1;
            }
        }
    }
    // present row data for caller, wheter it's truncated or not. The only way it's not truncated is if all columns are NULL
    for (i = 0; i < nf; i++) 
    {
        // use EMPTY_STRING if null, so freeing works
        (*row)[cdata] = GG_CURR_DB.dbc->maria.bindout[i].buffer == NULL ? GG_EMPTY_STRING : GG_CURR_DB.dbc->maria.bindout[i].buffer;
        (*lens)[cdata] = *(GG_CURR_DB.dbc->maria.bindout[i].length);
        cdata++;
    }

    // free allocations
    gg_free_int (GG_CURR_DB.dbc->maria.bindout);
    gg_free_int (rlen);
    gg_free_int (isnull);
    return 0;
}


gg_num gg_maria_nrows(char is_prep)
{
    GG_TRACE("");
    if (is_prep == 0)
    {
        // works since we use buffered (store) method to get results
        return mysql_num_rows (GG_CURR_DB.dbc->maria.res);
    }
    else
    {
        return mysql_stmt_num_rows (GG_CURR_DB.dbc->maria.stmt);
    }
}

void gg_maria_free()
{
    GG_TRACE("");
    if (GG_CURR_DB.dbc->maria.res != NULL) mysql_free_result(GG_CURR_DB.dbc->maria.res);
    GG_CURR_DB.dbc->maria.res = NULL;
}

char *gg_maria_fieldname()
{
    GG_TRACE("");
    return mysql_fetch_field(GG_CURR_DB.dbc->maria.res)->name;
}

gg_num gg_maria_nfield()
{
    GG_TRACE("");
    return mysql_num_fields(GG_CURR_DB.dbc->maria.res);
}

int gg_maria_use(char is_prep)
{
    GG_TRACE("");
    if (is_prep == 0)
    {
        GG_CURR_DB.dbc->maria.res = mysql_use_result(GG_CURR_DB.dbc->maria.con);
        if (GG_CURR_DB.dbc->maria.res == NULL)
        {
            cerror = "Error storing obtained data";
            return 1;
        }
    }
    else 
    {
        GG_CURR_DB.dbc->maria.res = mysql_stmt_result_metadata(GG_CURR_DB.dbc->maria.stmt);
        if (GG_CURR_DB.dbc->maria.res == NULL)
        {
            cerror = "Error storing obtained data (1)";
            return 1;
        }
    }
    return 0;
}

int gg_maria_store(char is_prep)
{
    GG_TRACE("");
    if (is_prep == 0)
    {
        GG_CURR_DB.dbc->maria.res = mysql_store_result(GG_CURR_DB.dbc->maria.con);
        if (GG_CURR_DB.dbc->maria.res == NULL)
        {
            cerror = "Error storing obtained data (2)";
            return 1;
        }
    }
    else
    {
        gg_maria_use (is_prep); // get metadata first
        my_bool update= 1;
        // get lengths
        mysql_stmt_attr_set(GG_CURR_DB.dbc->maria.stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &update);
        // get result to copy to client
        if (mysql_stmt_store_result (GG_CURR_DB.dbc->maria.stmt) != 0)
        {
            cerror = "Error storing obtained data (3)";
            return 1;
        }
    }
    return 0;
}


void gg_maria_close ()
{
    GG_TRACE("");
    mysql_close (GG_CURR_DB.dbc->maria.con);
}


gg_dbc *gg_maria_connect (gg_num abort_if_bad)
{
    GG_TRACE("");
    // reset all prepared statements
    gg_db_prep (NULL);

    // allocate connection to database. Must be done here to account for the actual generated code, which
    // dictates the sizeof(gg_dbc)
    // This must be malloc and NOT gg_malloc since we want to reuse connection across connections.
    // Otherwise gg_malloc would be automatically freed when the request is done, and the next 
    // request would use an invalid pointer. Also must use free to free it, not gg_free.
    if ((GG_CURR_DB.dbc = malloc (sizeof (gg_dbc))) == NULL) GG_FATAL ("Cannot allocate memory for database connection [%m]");

    GG_CURR_DB.dbc->maria.con = mysql_init(NULL);
    GG_CURR_DB.dbc->maria.res = NULL;
    GG_CURR_DB.dbc->maria.bind = NULL;
    GG_CURR_DB.dbc->maria.bindout = NULL;
   
    if (GG_CURR_DB.dbc->maria.con == NULL) 
    {
        char *em = "Cannot initialize database connection";
        GG_TRACE ("%s", em);
        if (abort_if_bad == 1) gg_report_error ("%s", em);
        gg_end_connection (0);
        return NULL; 
    }  

    char db_config_name[150];
    snprintf (db_config_name, sizeof(db_config_name), "%s/%s", gg_get_config()->app.dbconf_dir, GG_CURR_DB.db_name);
    GG_TRACE ("Using db config file [%s]", db_config_name);
    mysql_optionsv (GG_CURR_DB.dbc->maria.con, MYSQL_READ_DEFAULT_FILE, db_config_name);

    GG_TRACE ("Logging in to database, config [%s]", db_config_name);
    if (mysql_real_connect(GG_CURR_DB.dbc->maria.con, NULL, NULL, NULL, 
                   NULL, 0, NULL, 0) == NULL) 
    {
        char em[300];
        snprintf (em, sizeof(em), "Error in logging in to database: error [%s], using config file [%s]", mysql_error(GG_CURR_DB.dbc->maria.con), db_config_name);
        GG_TRACE ("%s", em);
        if (abort_if_bad == 1) gg_report_error ("%s",em);
        gg_end_connection (0);
        return NULL;
    }    
    //
    // These are the most common settings. ANSI_QUOTES means that single quotes
    // are used for string and defense against SQL injection depends on this (could be done
    // either way or for both, but the odds are ansi_quotes is used anyway). UTF8 is used for
    // web communication.
    // So in short, do NOT change either one of these settings!
    //
    // Removed character set, since that can be set in the connection file! No need to hardcode.
    /*if (mysql_set_character_set(GG_CURR_DB.dbc->maria.con, "utf8mb4"))
    {
        char *em = "Cannot set character set to utf8mb4";
        GG_TRACE ("%s", em);
        gg_end_connection (1);
        if (abort_if_bad == 1) gg_report_error ("%s", em);
        return NULL;
    }*/

    if (mysql_query(GG_CURR_DB.dbc->maria.con, "set session sql_mode=ansi_quotes")) 
    {
        char *em = "Cannot set sql_mode to ansi_quotes";
        GG_TRACE ("%s", em);
        gg_end_connection (1);
        if (abort_if_bad == 1) gg_report_error ("%s", em);
        return NULL;
    }
    return GG_CURR_DB.dbc;
}

gg_num gg_maria_exec(char *s, char is_prep, void **prep, gg_num paramcount, char **params)
{   
    GG_TRACE("");
    gg_stmt_cached = 0;
    if (is_prep == 0)
    {
        return (mysql_query(GG_CURR_DB.dbc->maria.con, s));
    }
    else
    {
        if (gg_maria_prep_stmt (prep, s, paramcount)) return 1;
        gg_num i;
        for (i = 0; i < paramcount; i++)
        {
            gg_maria_add_input(i, params[i]);
        }
        gg_maria_bind_input ();
        return gg_maria_stmt_exec();
    }
}


//
// deallocate prepared statement (only when connection lost), st is void * to prep stmt.
//
void gg_maria_close_stmt (void *st)
{
    GG_TRACE("");
    if (st == NULL) return; // statement has not been prepared yet, so cannot deallocate
    if (GG_CURR_DB.dbc != NULL) 
    {
        // we only close statements when connection is lost, so we don't care about return code
        // only that memory is deallocated
        mysql_stmt_close((MYSQL_STMT *)st);
    }
}

//
// Prepare statement with source test stmt and num_of_args input parameters
// prep is the address of void * that points to prepared statement. This is a static
// void *, which survives requests (otherwise prepared statements wouldn't be very useful,
// actually would decrease performance), however prep is set to NULL when connection is 
// reestablished (typically if db server recycles), which is generally rare.
//
int gg_maria_prep_stmt(void **prep, char *stmt, gg_num num_of_args)
{
    GG_TRACE("");
    char *sname = "";
    gg_num lnum = 0;
    gg_location (&sname, &lnum, 0);

    // reuse prepared statement from prep
    if (*prep != NULL) 
    {
        gg_stmt_cached = 1;
        GG_TRACE ("reusing prepared statement");
        GG_CURR_DB.dbc->maria.stmt = (MYSQL_STMT*)*prep;
    }
    else
    {
        GG_TRACE ("creating prepared statement");
        // if prep is NULL, create prepared statement
        char *origs = stmt; // original stmt
        stmt = gg_db_prep_text(stmt); // make ? instead of '%s'
        // init prep stmt
        if ((GG_CURR_DB.dbc->maria.stmt = mysql_stmt_init (GG_CURR_DB.dbc->maria.con)) == NULL)
        {
            cerror = "Cannot initialize statement";
            return 1;
        }
        // prepare it
        if (mysql_stmt_prepare(GG_CURR_DB.dbc->maria.stmt, stmt, strlen(stmt)) != 0)
        {
            cerror = "Cannot prepare statement";
            return 1;
        }
        if (stmt != origs) gg_free_int (stmt); // release statement if actually allocated
        // save it for reuse for as long as the process lives (minus reconnects to db, but those are rare)
        *prep = GG_CURR_DB.dbc->maria.stmt;
    }
    GG_CURR_DB.num_inp = num_of_args;
    // check param count correct before binding as the statement and what golf says may be different, in which case
    // mariadb would access memory that doesn't exist if golf say there's less than really is
    gg_num count = mysql_stmt_param_count(GG_CURR_DB.dbc->maria.stmt);
    if (count != GG_CURR_DB.num_inp)
    {
        cerror = "Wrong number of input parameters";
        return 1;
    }

    if (GG_CURR_DB.num_inp != 0)
    {
        // allocate binds
        GG_CURR_DB.dbc->maria.bind = (MYSQL_BIND*)gg_calloc(num_of_args, sizeof(MYSQL_BIND));
    }
    return 0;
}


//
// Add input parameter to SQL about to be executed. 'i' is the index of parameter
// (which must be 0,1,2.. for each invocation), arg is the argument.
//
void gg_maria_add_input(gg_num i, char *arg)
{
    GG_TRACE("");
    // all inputs are converted to string
    GG_CURR_DB.dbc->maria.bind[i].buffer_type = MYSQL_TYPE_STRING;
    // if null, it's empty
    GG_CURR_DB.dbc->maria.bind[i].buffer = (char*)(arg == NULL ? "":arg);
    GG_CURR_DB.dbc->maria.bind[i].buffer_length = strlen(arg);
    GG_CURR_DB.dbc->maria.bind[i].length = NULL; // this is for array binding
    // never null, it's Golf's simplification
    GG_CURR_DB.dbc->maria.bind[i].is_null = 0;
}

//
// Statement that has been prepared and input params specified, is now bound
//
void gg_maria_bind_input ()
{
    GG_TRACE("");
    if (GG_CURR_DB.num_inp != 0) 
    {
        mysql_stmt_bind_param (GG_CURR_DB.dbc->maria.stmt, GG_CURR_DB.dbc->maria.bind);
    }
}




gg_num gg_maria_affected(char is_prep) 
{
    GG_TRACE("");
    if (is_prep == 0)
    {
        return (gg_num) mysql_affected_rows (GG_CURR_DB.dbc->maria.con);
    }
    else
    {
        return (gg_num) mysql_stmt_affected_rows (GG_CURR_DB.dbc->maria.stmt);
    }
}


//
// Execute statement that's prepared. Input binding is removed.
//
gg_num gg_maria_stmt_exec()
{
    GG_TRACE("");
    gg_num res = mysql_stmt_execute (GG_CURR_DB.dbc->maria.stmt);
    // freeing bind struct must be here and not gg_maria_free because gg_maria_free is not
    // called for non-tuple queries (such as INSERT), but this one is always called. If not like this,
    // bind ptr would remain and double free would be attempted
    if (GG_CURR_DB.dbc->maria.bind != NULL)
    {
        gg_free_int (GG_CURR_DB.dbc->maria.bind);
        GG_CURR_DB.dbc->maria.bind = NULL;
    }
    return res;
}

//
// Escape string for inclusion in quoted params. MariaDB may use different quotes, but we force ansi quotes.
// Even if we didn't, the string would be properly escaped.
// memory for to must be 2*len+1. *len  is the actual encoded length without zero byte counted.
// Returns 0 for okay, 1 otherwise.
//
int gg_maria_escape(char *from, char *to, gg_num *len)
{
    GG_TRACE("");
    *len = (gg_num)mysql_real_escape_string(GG_CURR_DB.dbc->maria.con, to, from, (unsigned long) *len);
    if ((unsigned long)*len == (unsigned long) -1) return 1;
    return 0;
}