aboutsummaryrefslogtreecommitdiff
path: root/src/sql/SqlQuery.cpp
blob: 6201332a725fa1ff3533a1faa642157a6b92459e (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
#include "../../include/odhtdb/sql/SqlQuery.hpp"
#include <sqlite3.h>

namespace odhtdb
{
    SqlQuery::SqlQuery(sqlite3 *_db, const char *sql, std::initializer_list<SqlArg> args) : 
        db(_db),
        stmt(nullptr),
        numColumns(0)
    {
        int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
        if(rc != SQLITE_OK)
        {
            std::string errMsg = "Failed to prepare sqlite statement, error: ";
            errMsg += sqlite3_errmsg(db);
            throw SqlQueryException(errMsg);
        }
        
        int numParams = sqlite3_bind_parameter_count(stmt);
        if(args.size() != numParams)
        {
            std::string errMsg = "Failed to prepare sqlite statement, error: Sql has ";
            errMsg += std::to_string(numParams);
            errMsg += " parameters, got ";
            errMsg += std::to_string(args.size());
            errMsg += " arguments";
            sqlite3_finalize(stmt);
            stmt = nullptr;
            throw SqlQueryException(errMsg);
        }
        
        int paramIndex = 1;
        for(const SqlArg &arg : args)
        {
            rc = arg.bind(stmt, paramIndex);
            if(rc != SQLITE_OK)
            {
                std::string errMsg = "Failed to bind param, error code: ";
                errMsg += std::to_string(rc);
                sqlite3_finalize(stmt);
                stmt = nullptr;
                throw SqlQueryException(errMsg);
            }
            ++paramIndex;
        }
    }
    
    SqlQuery::~SqlQuery()
    {
        sqlite3_finalize(stmt);
    }
    
    bool SqlQuery::next()
    {
        int rc = sqlite3_step(stmt);
        if(rc == SQLITE_DONE)
            return false;
        else if(rc != SQLITE_ROW)
        {
            std::string errMsg = "Failed to perform sql select, error: ";
            errMsg += sqlite3_errmsg(db);
            throw SqlQueryException(errMsg);
        }
        
        numColumns = sqlite3_data_count(stmt);
        return true;
    }
    
    void SqlQuery::checkColumnIndex(int index)
    {
        if(index < 0 || index >= numColumns)
        {
            std::string errMsg;
            if(numColumns == 0)
            {
                errMsg += "Attempt to get column ";
                errMsg += std::to_string(index);
                errMsg += " but result does not have any columns";
            }
            else
            {
                errMsg += "Column index ";
                errMsg += std::to_string(index);
                errMsg += " has to be between 0 and ";
                errMsg += std::to_string(numColumns - 1);
            }
            throw SqlQueryException(errMsg);
        }
    }
    
    int SqlQuery::getInt(int index)
    {
        checkColumnIndex(index);
        return sqlite3_column_int(stmt, index);
    }
    
    i64 SqlQuery::getInt64(int index)
    {
        checkColumnIndex(index);
        return sqlite3_column_int64(stmt, index);
    }
    
    const DataView SqlQuery::getBlob(int index)
    {
        checkColumnIndex(index);
        const void *data = sqlite3_column_blob(stmt, index);
        int size = sqlite3_column_bytes(stmt, index);
        return { (void*)data, (usize)size };
    }
}