aboutsummaryrefslogtreecommitdiff
path: root/src/sqlite3.cpp
blob: 0fd96e34a2f999d287480e7ba36d9ae8d1ad5899 (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
/*
    sqlite3.cpp
    Copyright (C) 2019 Richard Knight


    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */

#include <plugin.h>
#include <iostream>
#include <sqlite3.h>
#include "connection.h"
#include "sqlite.h"


void SqliteConnection::Init(csnd::Csound* csound, LoginData* login) {
    int result = sqlite3_open(login->dbName, &conn);
    std::cout << "Y";
    if (result) {
        throw std::runtime_error("connection not established");
    }
}

void SqliteConnection::Close(csnd::Csound* csound) {
    sqlite3_close(conn);
}

void SqliteConnection::Exec(char* sql) {
    sqlite3_stmt* stmt = Query(sql);
    int rc = sqlite3_step(stmt);
    rc = sqlite3_finalize(stmt);
}

sqlite3_stmt* SqliteConnection::Query(char* sql) {
    sqlite3_stmt* stmt = NULL;
    int rc = sqlite3_prepare_v2(conn, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        throw std::runtime_error(sqlite3_errmsg(conn));
    }
    return stmt;
}

MYFLT SqliteConnection::Scalar(char* sql, int row, int col) {
    sqlite3_stmt *stmt = Query(sql);
    int colCount = sqlite3_column_count(stmt);
    int rc = sqlite3_step(stmt);
    int rowIndex = 0;
    while (rc != SQLITE_DONE && rc != SQLITE_OK) {
        if (rowIndex == row) {

            if (col > colCount -1) {
                rc = sqlite3_finalize(stmt);
                throw std::runtime_error("column number out of range");
            }
            MYFLT result = (MYFLT) sqlite3_column_double(stmt, col);
            rc = sqlite3_finalize(stmt);
            return result;
        }
        rc = sqlite3_step(stmt);
        rowIndex++;
    }
    rc = sqlite3_finalize(stmt);
    throw std::runtime_error("no result");  
}

char* SqliteConnection::ScalarString(char* sql, int row, int col) {
    sqlite3_stmt *stmt = Query(sql);
    int colCount = sqlite3_column_count(stmt);
    int rc = sqlite3_step(stmt);
    int rowIndex = 0;
    while (rc != SQLITE_DONE && rc != SQLITE_OK) {
        if (rowIndex == row) {

            if (col > colCount -1) {
                rc = sqlite3_finalize(stmt);
                throw std::runtime_error("column number out of range");
            }
            char* result = sqlite3_column_text(stmt, col);
            rc = sqlite3_finalize(stmt);
            return result;
        }
        rc = sqlite3_step(stmt);
        rowIndex++;
    }
    rc = sqlite3_finalize(stmt);
    throw std::runtime_error("no result");   

}

int SqliteConnection::RowCount(sqlite3_stmt* stmt) {
    int rowCount = 0;
    int rc = sqlite3_step(stmt);
    while (rc != SQLITE_DONE && rc != SQLITE_OK) {
        rc = sqlite3_step(stmt);
        rowCount ++;
    }
    rc = sqlite3_reset(stmt);
    return rowCount;
}

void SqliteConnection::ToArray(sqlite3_stmt* stmt, csnd::Csound* csound, ARRAYDAT* array, bool asString) {
    int colNum = sqlite3_column_count(stmt);
    int rowNum = RowCount(stmt);
    int totalResults = colNum * rowNum;
    array->sizes = csound->calloc(sizeof(int32_t) * 2);
    array->sizes[0] = rowNum;
    array->sizes[1] = colNum;
    array->dimensions = 2;
    CS_VARIABLE *var = array->arrayType->createVariable(csound, NULL);
    array->arrayMemberSize = var->memBlockSize;
    array->data = csound->calloc(var->memBlockSize * totalResults);
    STRINGDAT* strings;
    if (asString) {
        strings = (STRINGDAT*) array->data;
    }

    int colIndex;
    int rowIndex;
    int index = 0;
    int rc = sqlite3_step(stmt);
    while (rc != SQLITE_DONE && rc != SQLITE_OK) {
        colIndex = 0;
        while (colIndex < colNum) {
            if (asString) {
                char* item = sqlite3_column_text(stmt, colIndex);
                strings[index].size = strlen(item) + 1;
                strings[index].data = csound->strdup(item);
            } else {
                array->data[index] = (MYFLT) sqlite3_column_double(stmt, colIndex);
            }
            colIndex ++;
            index++;
        }            
        rc = sqlite3_step(stmt);
        rowIndex++;
    }
    rc = sqlite3_finalize(stmt);
}

void SqliteConnection::ArrayQueryString(char* sql, csnd::Csound* csound, ARRAYDAT* array) {
    ToArray(Query(sql), csound, array, true);
}

void SqliteConnection::ArrayQuery(char* sql, csnd::Csound* csound, ARRAYDAT* array) {
    ToArray(Query(sql), csound, array, false);
}