aboutsummaryrefslogtreecommitdiff
path: root/src/sql/Sql.cpp
blob: 0bfbbe6e9a0434e658cd178787cf929e23430a11 (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
#include "../../include/odhtdb/sql/Sql.hpp"
#include <sqlite3.h>
#include <exception>
#include <cassert>

namespace odhtdb
{
    int SqlArg::bind(sqlite3_stmt *stmt, int paramIndex) const
    {
        switch(type)
        {
            case Type::DATA_VIEW:
                return sqlite3_bind_blob(stmt, paramIndex, dataView.data, dataView.size, SQLITE_STATIC);
            case Type::INT:
                return sqlite3_bind_int(stmt, paramIndex, integer);
            case Type::INT64:
                return sqlite3_bind_int64(stmt, paramIndex, integer64);
            case Type::UINT64: // TODO: Find a way to use u64 in sqlite
                return sqlite3_bind_int64(stmt, paramIndex, uinteger64);
        }
        return SQLITE_OK;
    }
    
    std::string SqlArg::toString() const
    {
        switch(type)
        {
            case Type::DATA_VIEW:
                return std::string((const char*)dataView.data, dataView.size);
            case Type::INT:
                return std::to_string(integer);
            case Type::INT64:
                return std::to_string(integer64);
            case Type::UINT64: // TODO: Find a way to use u64 in sqlite
                return std::to_string(uinteger64);
            default:
                return "";
        }
    }
    
    SqlTransaction::SqlTransaction(sqlite3 *_db) : 
        db(_db)
    {
        assert(db);
        sqlite3_exec(db, "BEGIN", 0, 0, 0);
    }
    
    SqlTransaction::~SqlTransaction()
    {
        if(std::uncaught_exception())
            sqlite3_exec(db, "ROLLBACK", 0, 0, 0);
    }
    
    void SqlTransaction::commit()
    {
        sqlite3_exec(db, "COMMIT", 0, 0, 0);
    }
}