25 SqliteTripleSource::SqliteTripleSource(sqlite3 *db) : db(db)
31 int rid = (context !=
nullptr) ? context->
rid : -1;
32 std::string cond =
"";
40 char *
query =
nullptr;
41 query = sqlite3_mprintf(
" AND ns = %Q",
t.getNs());
46 cond +=
" AND ns IS NULL";
50 char *
query =
nullptr;
51 query = sqlite3_mprintf(
" AND name = %Q",
t.getName());
56 cond +=
" AND name IS NULL";
60 char *
query =
nullptr;
61 query = sqlite3_mprintf(
" AND value = %Q",
t.getValue());
66 cond +=
" AND value IS NULL";
74 sqlite3_stmt *statement =
nullptr;
75 char *
query =
nullptr;
76 query = sqlite3_mprintf(
"SELECT id FROM tags WHERE %s",
79 int result = sqlite3_prepare_v2(db,
query, -1, &statement,
nullptr);
80 if (result!=SQLITE_OK) {
81 yCWarning(SQLITETRIPLESOURCE,
"Error in query");
83 while (result == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) {
85 yCWarning(SQLITETRIPLESOURCE,
"WARNING: multiple matches ignored");
87 out = sqlite3_column_int(statement,0);
88 yCTrace(SQLITETRIPLESOURCE,
"Match %d", out);
91 sqlite3_finalize(statement);
98 char *
query =
nullptr;
99 query = sqlite3_mprintf(
"DELETE FROM tags WHERE %s",
condition(ti,context).c_str());
101 int result = sqlite3_exec(db,
query,
nullptr,
nullptr,
nullptr);
102 if (result!=SQLITE_OK) {
103 yCWarning(SQLITETRIPLESOURCE,
"Error in query");
110 char *
query =
nullptr;
111 query = sqlite3_mprintf(
"DELETE FROM tags WHERE rid IS NOT NULL AND rid NOT IN (SELECT id FROM tags)");
113 int result = sqlite3_exec(db,
query,
nullptr,
nullptr,
nullptr);
114 if (result!=SQLITE_OK) {
115 yCWarning(SQLITETRIPLESOURCE,
"Error in query");
123 sqlite3_stmt *statement =
nullptr;
124 char *
query =
nullptr;
125 query = sqlite3_mprintf(
"SELECT id, ns, name, value FROM tags WHERE %s",
condition(ti,context).c_str());
127 int result = sqlite3_prepare_v2(db,
query, -1, &statement,
nullptr);
128 if (result!=SQLITE_OK) {
129 yCWarning(SQLITETRIPLESOURCE,
"Error in query");
131 while (result == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) {
133 char *ns = (
char *)sqlite3_column_text(statement,1);
134 char *name = (
char *)sqlite3_column_text(statement,2);
135 char *value = (
char *)sqlite3_column_text(statement,3);
141 if (name !=
nullptr) {
145 if (value !=
nullptr) {
151 sqlite3_finalize(statement);
158 int rid = (context !=
nullptr)?context->
rid:-1;
159 char buf[100] =
"NULL";
161 std::snprintf(buf, 100,
"%d", rid);
169 char *
query = sqlite3_mprintf(
"INSERT INTO tags (rid,ns,name,value) VALUES(%s,%Q,%Q,%Q)",
171 t.getNs(),
t.getName(),
t.getValue());
173 int result = sqlite3_exec(db,
query,
nullptr,
nullptr, &msg);
174 if (result!=SQLITE_OK) {
175 if (msg !=
nullptr) {
176 yCError(SQLITETRIPLESOURCE,
"Error: %s", msg);
178 yCError(SQLITETRIPLESOURCE,
"(Location): %s:%d", __FILE__, __LINE__);
188 char *
query =
nullptr;
189 if (
t.hasName||
t.hasNs) {
192 query = sqlite3_mprintf(
"UPDATE tags SET value = %Q WHERE %s",
196 query = sqlite3_mprintf(
"UPDATE tags SET value = %Q WHERE id = %Q",
201 int result = sqlite3_exec(db,
query,
nullptr,
nullptr, &msg);
202 if (result!=SQLITE_OK) {
203 if (msg !=
nullptr) {
204 yCError(SQLITETRIPLESOURCE,
"Error: %s", msg);
208 int ct = sqlite3_changes(db);
209 if (ct==0 && (
t.hasName||
t.hasNs)) {
217 int result = sqlite3_exec(db,
"BEGIN TRANSACTION;",
nullptr,
nullptr,
nullptr);
218 yCDebug(SQLITETRIPLESOURCE,
"Query: BEGIN TRANSACTION;");
219 if (result!=SQLITE_OK) {
220 yCWarning(SQLITETRIPLESOURCE,
"Error in BEGIN query");
226 int result = sqlite3_exec(db,
"END TRANSACTION;",
nullptr,
nullptr,
nullptr);
227 yCDebug(SQLITETRIPLESOURCE,
"Query: END TRANSACTION;");
228 if (result!=SQLITE_OK) {
229 yCWarning(SQLITETRIPLESOURCE,
"Error in END query");