sync/sql_func.yml2
author Volker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 21:49:28 +0200
branchsync
changeset 2903 33549a7c7191
parent 2845 d474c5ebfec8
child 3060 910548a84da0
permissions -rw-r--r--
safeguards
vb@2845
     1
// This file is under GNU General Public License 3.0
vb@2845
     2
// see LICENSE.txt
vb@2845
     3
vb@2845
     4
// Copyleft (c) 2017, 2018, p≡p foundation
vb@2845
     5
vb@2845
     6
// Written by Volker Birk
vb@2845
     7
vb@2845
     8
vb@2844
     9
// prepare SQL statement
vb@2844
    10
vb@2844
    11
function "init_sql" {
vb@2844
    12
    param "sql";
vb@2844
    13
    ||
vb@2844
    14
        static const char *sql = `copy '$sql'`;
vb@2844
    15
        static const size_t len = sizeof(`copy '$sql'`);
vb@2844
    16
        sqlite3_stmt *_sql;
vb@2844
    17
        int int_result = sqlite3_prepare_v2(session->db, sql, (int) len, &_sql, NULL);
vb@2844
    18
        assert(int_result == SQLITE_OK);
vb@2844
    19
        if (!(int_result == SQLITE_OK))
vb@2844
    20
            return PEP_UNKNOWN_ERROR;
vb@2844
    21
vb@2844
    22
    ||
vb@2844
    23
}
vb@2844
    24
vb@2844
    25
// exec_sql_* is returning _result
vb@2844
    26
vb@2844
    27
function "exec_sql_int" {
vb@2844
    28
    param "sql";
vb@2844
    29
    call "init_sql" with "sql", "$sql";
vb@2844
    30
    ||
vb@2844
    31
        int _result = 0;
vb@2844
    32
        int_result = sqlite3_step(_sql);
vb@2844
    33
        assert(int_result == SQLITE_ROW);
vb@2844
    34
        if (int_result == SQLITE_ROW)
vb@2844
    35
            _result = sqlite3_column_int(_sql, 0);
vb@2844
    36
        sqlite3_finalize(_sql);
vb@2844
    37
        if (int_result != SQLITE_ROW)
vb@2844
    38
            return PEP_UNKNOWN_ERROR;
vb@2844
    39
vb@2844
    40
    ||
vb@2844
    41
}
vb@2844
    42