-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSqliteBridge.cpp
More file actions
83 lines (74 loc) · 2.67 KB
/
SqliteBridge.cpp
File metadata and controls
83 lines (74 loc) · 2.67 KB
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
#include "SqliteBridge.h"
void SQLiteBridge::setDatabase(QSqlDatabase& db)
{
database = db;
QVariant v = database.driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0) {
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
if (handle != 0)
{
this->handle = (sqlite3*) handle;
if(sqlite3_create_function(this->handle,
"bridgescalar",
-1,
SQLITE_UTF8,
this,
&SQLiteBridge::scalarHandler,
NULL,
NULL) != SQLITE_OK)
qDebug() << "Could not create scalar function";
}
else
qDebug() << "Could not get sqlite handle";
}
else
qDebug() << "Could not get proper QVariant for sqlite handle";
}
void SQLiteBridge::scalarHandler(sqlite3_context* context, int args, sqlite3_value** values)
{
QString type;
if(sqlite3_value_type(values[0]) != SQLITE_TEXT)
return;
else
type = (const char*) sqlite3_value_text(values[0]);
QList<QVariant> returnValues;
for (int i=1; i < args; i++)
{
sqlite3_value* value = values[i];
QVariant data;
switch(sqlite3_value_type(value))
{
case SQLITE_INTEGER: data = QVariant::fromValue<int>(sqlite3_value_int(value)); break;
case SQLITE_TEXT: data = QVariant::fromValue<QString>(QString((const char*)sqlite3_value_text(value))); break;
case SQLITE_FLOAT: data = QVariant::fromValue<double>(sqlite3_value_double(value)); break;
case SQLITE_BLOB:
{
const char* raw = (const char* ) sqlite3_value_blob(value);
QByteArray array(raw, sqlite3_value_bytes(value));
data = QVariant::fromValue<QByteArray>(array);
}
}
returnValues.append(data);
}
SQLiteBridge* ptr = (SQLiteBridge* )sqlite3_user_data(context);
if (type == "insert")
ptr->emitRowInserted(returnValues);
else
qDebug() << "Unknown trigger type";
}
void SQLiteBridge::addInsertTrigger(const QString& tableName, const QStringList& columns)
{
if (columns.size() < 1)
return;
QStringList newColumns(columns);
for(int i=0; i<newColumns.size(); ++i)
newColumns[i] = newColumns[i].prepend("NEW.");
QString query("CREATE TRIGGER insert_trigger_%1 AFTER INSERT ON `%1` FOR EACH ROW BEGIN \
SELECT bridgescalar('insert', %2); \
END;");
query = query.arg(tableName).arg(newColumns.join(", "));
if (database.exec(query).lastError().type() != QSqlError::NoError)
qDebug() << "Could not add trigger: " << database.lastError().databaseText() << database.lastError().driverText() << "\n" << query;
else
qDebug() << "Added INSERT trigger on " << tableName;
}