summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/util-sql-imp-ffi.lua
blob: cbfd7bcf5f68ad167ad619b3bd0ab3287bfbd7ac (plain)
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
if not modules then modules = { } end modules ['util-sql-imp-ffi'] = {
    version   = 1.001,
    comment   = "companion to util-sql.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- I looked at luajit-mysql to see how the ffi mapping was done but it didn't work
-- out that well (at least not on windows) but I got the picture. As I have somewhat
-- different demands I simplified.

local tonumber = tonumber
local concat = table.concat
local format, byte = string.format, string.byte
local lpegmatch = lpeg.match
local setmetatable, type = setmetatable, type
local sleep = os.sleep
local formatters = string.formatters

local trace_sql     = false  trackers.register("sql.trace",  function(v) trace_sql     = v end)
local trace_queries = false  trackers.register("sql.queries",function(v) trace_queries = v end)
local report_state  = logs.reporter("sql","ffi")

if not utilities.sql then
    require("util-sql")
end

ffi.cdef [[

    /*
        This is as lean and mean as possible. After all we just need a connection and
        a query. The rest is handled already in the Lua code elsewhere.
    */

    void free(void*ptr);
    void * malloc(size_t size);

    typedef void MYSQL_instance;
    typedef void MYSQL_result;
    typedef char **MYSQL_row;
    typedef unsigned int MYSQL_offset;

    typedef struct st_mysql_field {
        char *name;
        char *org_name;
        char *table;
        char *org_table;
        char *db;
        char *catalog;
        char *def;
        unsigned long length;
        unsigned long max_length;
        unsigned int name_length;
        unsigned int org_name_length;
        unsigned int table_length;
        unsigned int org_table_length;
        unsigned int db_length;
        unsigned int catalog_length;
        unsigned int def_length;
        unsigned int flags;
        unsigned int decimals;
        unsigned int charsetnr;
        int type;
        void *extension;
    } MYSQL_field;

    MYSQL_instance * mysql_init (
        MYSQL_instance *mysql
    );

    MYSQL_instance * mysql_real_connect (
        MYSQL_instance *mysql,
        const char *host,
        const char *user,
        const char *passwd,
        const char *db,
        unsigned int port,
        const char *unix_socket,
        unsigned long clientflag
    );

    unsigned int mysql_errno (
        MYSQL_instance *mysql
    );

    const char *mysql_error (
        MYSQL_instance *mysql
    );

    /* int mysql_query (
        MYSQL_instance *mysql,
        const char *q
    ); */

    int mysql_real_query (
        MYSQL_instance *mysql,
        const char *q,
        unsigned long length
    );

    MYSQL_result * mysql_store_result (
        MYSQL_instance *mysql
    );

    void mysql_free_result (
        MYSQL_result *result
    );

    unsigned long long mysql_num_rows (
        MYSQL_result *res
    );

    MYSQL_row mysql_fetch_row (
        MYSQL_result *result
    );

    unsigned int mysql_affected_rows (
        MYSQL_instance *mysql
    );

    unsigned int mysql_field_count (
        MYSQL_instance *mysql
    );

    unsigned int mysql_num_fields (
        MYSQL_result *res
    );

    /* MYSQL_field *mysql_fetch_field (
        MYSQL_result *result
    ); */

    MYSQL_field * mysql_fetch_fields (
        MYSQL_result *res
    );

    MYSQL_offset mysql_field_seek(
        MYSQL_result *result,
        MYSQL_offset offset
    );

    void mysql_close(
        MYSQL_instance *sock
    );

    /* unsigned long * mysql_fetch_lengths(
        MYSQL_result *result
    ); */

]]
-- trackers.enable("*lib*")
local sql                    = utilities.sql
----- mysql                  = ffi.load(os.name == "windows" and "libmysql" or "libmysqlclient")
----- mysql                  = ffilib(os.name == "windows" and "libmysql" or "libmysqlclient")
local mysql                  = ffilib(os.name == "windows" and "libmysql" or "libmysql")

if not mysql then
    report_state("unable to load library")
end

local nofretries             = 5
local retrydelay             = 1

local cache                  = { }
local helpers                = sql.helpers
local methods                = sql.methods
local validspecification     = helpers.validspecification
local querysplitter          = helpers.querysplitter
local dataprepared           = helpers.preparetemplate
local serialize              = sql.serialize
local deserialize            = sql.deserialize

local mysql_open_session     = mysql.mysql_init

local mysql_open_connection  = mysql.mysql_real_connect
local mysql_execute_query    = mysql.mysql_real_query
local mysql_close_connection = mysql.mysql_close

local mysql_affected_rows    = mysql.mysql_affected_rows
local mysql_field_count      = mysql.mysql_field_count
local mysql_field_seek       = mysql.mysql_field_seek
local mysql_num_fields       = mysql.mysql_num_fields
local mysql_fetch_fields     = mysql.mysql_fetch_fields
----- mysql_fetch_field      = mysql.mysql_fetch_field
local mysql_num_rows         = mysql.mysql_num_rows
local mysql_fetch_row        = mysql.mysql_fetch_row
----- mysql_fetch_lengths    = mysql.mysql_fetch_lengths
local mysql_init             = mysql.mysql_init
local mysql_store_result     = mysql.mysql_store_result
local mysql_free_result      = mysql.mysql_free_result

local mysql_error_number     = mysql.mysql_errno
local mysql_error_message    = mysql.mysql_error

local NULL                   = ffi.cast("MYSQL_result *",0)

local ffi_tostring           = ffi.string
local ffi_gc                 = ffi.gc

local instance               = mysql.mysql_init(nil)

local mysql_constant_false   = false
local mysql_constant_true    = true

local wrapresult  do

    local function collect(t)
        local result = t._result_
        if result then
            ffi_gc(result,mysql_free_result)
        end
    end

    local function finish(t)
        local result = t._result_
        if result then
            t._result_ = nil
            ffi_gc(result,mysql_free_result)
        end
    end

    local function getcoldata(t)
        local result = t._result_
        local nofrows   = t.nofrows
        local noffields = t.noffields
        local names     = { }
        local types     = { }
        local fields    = mysql_fetch_fields(result)
        for i=1,noffields do
            local field = fields[i-1]
            names[i] = ffi_tostring(field.name)
            types[i] = tonumber(field.type) -- todo
        end
        t.names = names
        t.types = types
    end

    local function getcolnames(t)
        local names = t.names
        if names then
            return names
        end
        getcoldata(t)
        return t.names
    end

    local function getcoltypes(t)
        local types = t.types
        if types then
            return types
        end
        getcoldata(t)
        return t.types
    end

    local function numrows(t)
        return t.nofrows
    end

    -- local function fetch(t)
    --     local
    --     local row    = mysql_fetch_row(result)
    --     local result = { }
    --     for i=1,t.noffields do
    --         result[i] = ffi_tostring(row[i-1])
    --     end
    --     return unpack(result)
    -- end

    local mt = {
        __gc    = collect,
        __index = {
            _result_    = nil,
            close       = finish,
            numrows     = numrows,
            getcolnames = getcolnames,
            getcoltypes = getcoltypes,
         -- fetch       = fetch, -- not efficient
        }
    }

    wrapresult = function(connection)
        local result = mysql_store_result(connection)
        if result ~= NULL then
            mysql_field_seek(result,0)
            local t = {
                _result_  = result,
                nofrows   = tonumber(mysql_num_rows  (result) or 0) or 0,
                noffields = tonumber(mysql_num_fields(result) or 0) or 0,
            }
            return setmetatable(t,mt)
        elseif tonumber(mysql_field_count(connection) or 0) or 0 > 0 then
            return tonumber(mysql_affected_rows(connection))
        end
    end

end

local initializesession  do

    -- timeouts = [ connect_timeout |wait_timeout | interactive_timeout ]

    local timeout -- = 3600 -- to be tested

    -- connection

    local function close(t)
        -- just a struct ?
    end

    local function execute(t,query)
        if query and query ~= "" then
            local connection = t._connection_
            local result = mysql_execute_query(connection,query,#query)
            if result == 0 then
                return wrapresult(connection)
            else
             -- mysql_error_number(connection)
                return false, ffi_tostring(mysql_error_message(connection))
            end
        end
        return false
    end

    local mt = {
        __index = {
            close   = close,
            execute = execute,
        }
    }

    -- session

    local function open(t,database,username,password,host,port)
        local connection = mysql_open_connection(
            t._session_,
            host or "localhost",
            username or "",
            password or "",
            database or "",
            port or 0,
            NULL,
            0
        )
        if connection ~= NULL then
            if timeout then
                execute(connection,formatters["SET SESSION connect_timeout=%s ;"](timeout))
            end
            local t = {
                _connection_ = connection,
            }
            return setmetatable(t,mt)
        end
    end

    local function message(t)
        return mysql_error_message(t._session_)
    end

    local function close(t)
        local connection = t._connection_
        if connection and connection ~= NULL then
            ffi_gc(connection, mysql_close)
            t.connection = nil
        end
    end

    local mt = {
        __index = {
            connect = open,
            close   = close,
            message = message,
        },
    }

    initializesession = function()
        local session = {
            _session_ = mysql_open_session(instance) -- maybe share, single thread anyway
        }
        return setmetatable(session,mt)
    end

end

local executequery  do

    local function connect(session,specification)
        return session:connect(
            specification.database or "",
            specification.username or "",
            specification.password or "",
            specification.host     or "",
            specification.port
        )
    end

    local function fetched(specification,query,converter)
        if not query or query == "" then
            report_state("no valid query")
            return false
        end
        local id = specification.id
        local session, connection
        if id then
            local c = cache[id]
            if c then
                session    = c.session
                connection = c.connection
            end
            if not connection then
                session = initializesession()
                if not session then
                    return formatters["no session for %a"](id)
                end
                connection = connect(session,specification)
                if not connection then
                    return formatters["no connection for %a"](id)
                end
                cache[id] = { session = session, connection = connection }
            end
        else
            session = initializesession()
            if not session then
                return "no session"
            end
            connection = connect(session,specification)
            if not connection then
                return "no connection"
            end
        end
        if not connection then
            report_state("error in connection: %s@%s to %s:%s",
                specification.database or "no database",
                specification.username or "no username",
                specification.host     or "no host",
                specification.port     or "no port"
            )
            return "no connection"
        end
        query = lpegmatch(querysplitter,query)
        local result, okay
        for i=1,#query do
            local q = query[i]
            local r, m = connection:execute(q)
            if m then
                report_state("error in query to host %a: %s",specification.host,string.collapsespaces(q or "?"))
                if m then
                    report_state("message: %s",m)
                end
            end
            local t = type(r)
            if t == "table" then
                result = r
                okay = true
            elseif t == "number" then
                okay = true
            end
        end
        if not okay then -- can go
            -- why do we close a session
            if connection then
                connection:close()
            end
            if session then
                session:close()
            end
            if id then
                cache[id] = nil
            end
            return "execution error"
        end
        local data, keys
        if result then
            if converter then
                data = converter.ffi(result)
            else
                local _result_  = result._result_
                local noffields = result.noffields
                local nofrows   = result.nofrows
                keys = result:getcolnames()
                data = { }
                if noffields > 0 and nofrows > 0 then
                    for i=1,nofrows do
                        local cells = { }
                        local row   = mysql_fetch_row(_result_)
                        for j=1,noffields do
                            local s = row[j-1]
                            local k = keys[j]
                            if s == NULL then
                                cells[k] = ""
                            else
                                cells[k] = ffi_tostring(s)
                            end
                        end
                        data[i] = cells
                    end
                end
            end
            result:close()
        end
        --
        if not id then
            if connection then
                connection:close()
            end
            if session then
                session:close()
            end
        end
        return false, data, keys
    end

    local function datafetched(specification,query,converter)
        local callokay, connectionerror, data, keys = pcall(fetched,specification,query,converter)
        if not callokay then
            report_state("call error, retrying")
            callokay, connectionerror, data, keys = pcall(fetched,specification,query,converter)
        elseif connectionerror then
            report_state("error: %s, retrying",connectionerror)
            callokay, connectionerror, data, keys = pcall(fetched,specification,query,converter)
        end
        if not callokay then
            report_state("persistent call error")
        elseif connectionerror then
            report_state("persistent error: %s",connectionerror)
        end
        return data or { }, keys or { }
    end

    executequery = function(specification)
        if trace_sql then
            report_state("executing library")
        end
        if not validspecification(specification) then
            report_state("error in specification")
            return
        end
        local query = dataprepared(specification)
        if not query then
            report_state("error in preparation")
            return
        end
        local data, keys = datafetched(specification,query,specification.converter)
        if not data then
            report_state("error in fetching")
            return
        end
        local one = data[1]
        if one then
            setmetatable(data,{ __index = one } )
        end
        return data, keys
    end

end

local wraptemplate = [[
----- mysql           = ffi.load(os.name == "windows" and "libmysql" or "libmysqlclient")
local mysql           = ffi.load(os.name == "windows" and "libmysql" or "libmysql")

local mysql_fetch_row = mysql.mysql_fetch_row
local ffi_tostring    = ffi.string

local converters      = utilities.sql.converters
local deserialize     = utilities.sql.deserialize

local tostring        = tostring
local tonumber        = tonumber
local booleanstring   = string.booleanstring

local NULL            = ffi.cast("MYSQL_result *",0)

%s

return function(result)
    if not result then
        return { }
    end
    local nofrows = result.nofrows
    if nofrows == 0 then
        return { }
    end
    local noffields = result.noffields
    local target    = { } -- no %s needed here
    local _result_  = result._result_
    -- we can share cells
    for i=1,nofrows do
        local cells = { }
        local row   = mysql_fetch_row(_result_)
        for j=1,noffields do
            local s = row[j-1]
            if s == NULL then
                cells[j] = ""
            else
                cells[j] = ffi_tostring(s)
            end
        end
        target[%s] = {
            %s
        }
    end
    result:close()
    return target
end
]]

local celltemplate = "cells[%s]"

methods.ffi = {
    runner       = function() end,    -- never called
    execute      = executequery,
    initialize   = initializesession, -- returns session
    usesfiles    = false,
    wraptemplate = wraptemplate,
    celltemplate = celltemplate,
}