forked from sysown/proxysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL_Catalog.cpp
More file actions
377 lines (311 loc) · 10.1 KB
/
MySQL_Catalog.cpp
File metadata and controls
377 lines (311 loc) · 10.1 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "MySQL_Catalog.h"
#include "cpp.h"
#include "proxysql.h"
#include <sstream>
#include <algorithm>
#include "../deps/json/json.hpp"
MySQL_Catalog::MySQL_Catalog(const std::string& path)
: db(NULL), db_path(path)
{
}
MySQL_Catalog::~MySQL_Catalog() {
close();
}
int MySQL_Catalog::init() {
// Initialize database connection
db = new SQLite3DB();
char path_buf[db_path.size() + 1];
strcpy(path_buf, db_path.c_str());
int rc = db->open(path_buf, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
if (rc != SQLITE_OK) {
proxy_error("Failed to open catalog database at %s: %d\n", db_path.c_str(), rc);
return -1;
}
// Initialize schema
return init_schema();
}
void MySQL_Catalog::close() {
if (db) {
delete db;
db = NULL;
}
}
int MySQL_Catalog::init_schema() {
// Enable foreign keys
db->execute("PRAGMA foreign_keys = ON");
// Create tables
int rc = create_tables();
if (rc) {
proxy_error("Failed to create catalog tables\n");
return -1;
}
proxy_info("MySQL Catalog database initialized at %s\n", db_path.c_str());
return 0;
}
int MySQL_Catalog::create_tables() {
// Main catalog table
const char* create_catalog_table =
"CREATE TABLE IF NOT EXISTS catalog ("
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
" kind TEXT NOT NULL," // table, view, domain, metric, note
" key TEXT NOT NULL," // e.g., "db.sales.orders"
" document TEXT NOT NULL," // JSON content
" tags TEXT," // comma-separated tags
" links TEXT," // comma-separated related keys
" created_at INTEGER DEFAULT (strftime('%s', 'now')),"
" updated_at INTEGER DEFAULT (strftime('%s', 'now')),"
" UNIQUE(kind, key)"
");";
if (!db->execute(create_catalog_table)) {
proxy_error("Failed to create catalog table\n");
return -1;
}
// Indexes for search
db->execute("CREATE INDEX IF NOT EXISTS idx_catalog_kind ON catalog(kind)");
db->execute("CREATE INDEX IF NOT EXISTS idx_catalog_tags ON catalog(tags)");
db->execute("CREATE INDEX IF NOT EXISTS idx_catalog_created ON catalog(created_at)");
// Full-text search table for better search (optional enhancement)
db->execute("CREATE VIRTUAL TABLE IF NOT EXISTS catalog_fts USING fts5("
" kind, key, document, tags, content='catalog', content_rowid='id'"
");");
// Triggers to keep FTS in sync
db->execute("DROP TRIGGER IF EXISTS catalog_ai");
db->execute("DROP TRIGGER IF EXISTS catalog_ad");
db->execute("CREATE TRIGGER IF NOT EXISTS catalog_ai AFTER INSERT ON catalog BEGIN"
" INSERT INTO catalog_fts(rowid, kind, key, document, tags)"
" VALUES (new.id, new.kind, new.key, new.document, new.tags);"
"END;");
db->execute("CREATE TRIGGER IF NOT EXISTS catalog_ad AFTER DELETE ON catalog BEGIN"
" INSERT INTO catalog_fts(catalog_fts, rowid, kind, key, document, tags)"
" VALUES ('delete', old.id, old.kind, old.key, old.document, old.tags);"
"END;");
// Merge operations log
const char* create_merge_log =
"CREATE TABLE IF NOT EXISTS merge_log ("
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
" target_key TEXT NOT NULL,"
" source_keys TEXT NOT NULL," // JSON array
" instructions TEXT,"
" created_at INTEGER DEFAULT (strftime('%s', 'now'))"
");";
db->execute(create_merge_log);
return 0;
}
int MySQL_Catalog::upsert(
const std::string& kind,
const std::string& key,
const std::string& document,
const std::string& tags,
const std::string& links
) {
sqlite3_stmt* stmt = NULL;
const char* upsert_sql =
"INSERT INTO catalog(kind, key, document, tags, links, updated_at) "
"VALUES(?1, ?2, ?3, ?4, ?5, strftime('%s', 'now')) "
"ON CONFLICT(kind, key) DO UPDATE SET "
" document = ?3,"
" tags = ?4,"
" links = ?5,"
" updated_at = strftime('%s', 'now')";
int rc = db->prepare_v2(upsert_sql, &stmt);
if (rc != SQLITE_OK) {
proxy_error("Failed to prepare catalog upsert: %d\n", rc);
return -1;
}
(*proxy_sqlite3_bind_text)(stmt, 1, kind.c_str(), -1, SQLITE_TRANSIENT);
(*proxy_sqlite3_bind_text)(stmt, 2, key.c_str(), -1, SQLITE_TRANSIENT);
(*proxy_sqlite3_bind_text)(stmt, 3, document.c_str(), -1, SQLITE_TRANSIENT);
(*proxy_sqlite3_bind_text)(stmt, 4, tags.c_str(), -1, SQLITE_TRANSIENT);
(*proxy_sqlite3_bind_text)(stmt, 5, links.c_str(), -1, SQLITE_TRANSIENT);
SAFE_SQLITE3_STEP2(stmt);
(*proxy_sqlite3_finalize)(stmt);
proxy_debug(PROXY_DEBUG_GENERIC, 3, "Catalog upsert: kind=%s, key=%s\n", kind.c_str(), key.c_str());
return 0;
}
int MySQL_Catalog::get(
const std::string& kind,
const std::string& key,
std::string& document
) {
sqlite3_stmt* stmt = NULL;
const char* get_sql =
"SELECT document FROM catalog "
"WHERE kind = ?1 AND key = ?2";
int rc = db->prepare_v2(get_sql, &stmt);
if (rc != SQLITE_OK) {
proxy_error("Failed to prepare catalog get: %d\n", rc);
return -1;
}
(*proxy_sqlite3_bind_text)(stmt, 1, kind.c_str(), -1, SQLITE_TRANSIENT);
(*proxy_sqlite3_bind_text)(stmt, 2, key.c_str(), -1, SQLITE_TRANSIENT);
rc = (*proxy_sqlite3_step)(stmt);
if (rc == SQLITE_ROW) {
const char* doc = (const char*)(*proxy_sqlite3_column_text)(stmt, 0);
if (doc) {
document = doc;
}
(*proxy_sqlite3_finalize)(stmt);
return 0;
}
(*proxy_sqlite3_finalize)(stmt);
return -1;
}
std::string MySQL_Catalog::search(
const std::string& query,
const std::string& kind,
const std::string& tags,
int limit,
int offset
) {
std::ostringstream sql;
sql << "SELECT kind, key, document, tags, links FROM catalog WHERE 1=1";
// Add kind filter
if (!kind.empty()) {
sql << " AND kind = '" << kind << "'";
}
// Add tags filter
if (!tags.empty()) {
sql << " AND tags LIKE '%" << tags << "%'";
}
// Add search query
if (!query.empty()) {
sql << " AND (key LIKE '%" << query << "%' "
<< "OR document LIKE '%" << query << "%' "
<< "OR tags LIKE '%" << query << "%')";
}
sql << " ORDER BY updated_at DESC LIMIT " << limit << " OFFSET " << offset;
char* error = NULL;
int cols = 0, affected = 0;
SQLite3_result* resultset = NULL;
db->execute_statement(sql.str().c_str(), &error, &cols, &affected, &resultset);
if (error) {
proxy_error("Catalog search error: %s\n", error);
return "[]";
}
// Build JSON result using nlohmann::json
nlohmann::json results = nlohmann::json::array();
if (resultset) {
for (std::vector<SQLite3_row*>::iterator it = resultset->rows.begin();
it != resultset->rows.end(); ++it) {
SQLite3_row* row = *it;
nlohmann::json entry;
entry["kind"] = std::string(row->fields[0] ? row->fields[0] : "");
entry["key"] = std::string(row->fields[1] ? row->fields[1] : "");
// Parse the stored JSON document - nlohmann::json handles escaping
const char* doc_str = row->fields[2];
if (doc_str) {
try {
entry["document"] = nlohmann::json::parse(doc_str);
} catch (const nlohmann::json::parse_error& e) {
// If document is not valid JSON, store as string
entry["document"] = std::string(doc_str);
}
} else {
entry["document"] = nullptr;
}
entry["tags"] = std::string(row->fields[3] ? row->fields[3] : "");
entry["links"] = std::string(row->fields[4] ? row->fields[4] : "");
results.push_back(entry);
}
delete resultset;
}
return results.dump();
}
std::string MySQL_Catalog::list(
const std::string& kind,
int limit,
int offset
) {
std::ostringstream sql;
sql << "SELECT kind, key, document, tags, links FROM catalog";
if (!kind.empty()) {
sql << " WHERE kind = '" << kind << "'";
}
sql << " ORDER BY kind, key ASC LIMIT " << limit << " OFFSET " << offset;
// Get total count
std::ostringstream count_sql;
count_sql << "SELECT COUNT(*) FROM catalog";
if (!kind.empty()) {
count_sql << " WHERE kind = '" << kind << "'";
}
char* error = NULL;
int cols = 0, affected = 0;
SQLite3_result* resultset = NULL;
int total = 0;
SQLite3_result* count_result = db->execute_statement(count_sql.str().c_str(), &error, &cols, &affected);
if (count_result && !count_result->rows.empty()) {
total = atoi(count_result->rows[0]->fields[0]);
}
delete count_result;
resultset = NULL;
db->execute_statement(sql.str().c_str(), &error, &cols, &affected, &resultset);
// Build JSON result using nlohmann::json
nlohmann::json result;
result["total"] = total;
nlohmann::json results = nlohmann::json::array();
if (resultset) {
for (std::vector<SQLite3_row*>::iterator it = resultset->rows.begin();
it != resultset->rows.end(); ++it) {
SQLite3_row* row = *it;
nlohmann::json entry;
entry["kind"] = std::string(row->fields[0] ? row->fields[0] : "");
entry["key"] = std::string(row->fields[1] ? row->fields[1] : "");
// Parse the stored JSON document
const char* doc_str = row->fields[2];
if (doc_str) {
try {
entry["document"] = nlohmann::json::parse(doc_str);
} catch (const nlohmann::json::parse_error& e) {
entry["document"] = std::string(doc_str);
}
} else {
entry["document"] = nullptr;
}
entry["tags"] = std::string(row->fields[3] ? row->fields[3] : "");
entry["links"] = std::string(row->fields[4] ? row->fields[4] : "");
results.push_back(entry);
}
delete resultset;
}
result["results"] = results;
return result.dump();
}
int MySQL_Catalog::merge(
const std::vector<std::string>& keys,
const std::string& target_key,
const std::string& kind,
const std::string& instructions
) {
// Fetch all source entries
std::string source_docs = "";
for (const auto& key : keys) {
std::string doc;
// Try different kinds for flexible merging
if (get("table", key, doc) == 0 || get("view", key, doc) == 0) {
source_docs += doc + "\n\n";
}
}
// Create merged document
std::string merged_doc = "{";
merged_doc += "\"source_keys\":[";
for (size_t i = 0; i < keys.size(); i++) {
if (i > 0) merged_doc += ",";
merged_doc += "\"" + keys[i] + "\"";
}
merged_doc += "],";
merged_doc += "\"instructions\":" + std::string(instructions.empty() ? "\"\"" : "\"" + instructions + "\"");
merged_doc += "}";
return upsert(kind, target_key, merged_doc, "", "");
}
int MySQL_Catalog::remove(
const std::string& kind,
const std::string& key
) {
std::ostringstream sql;
sql << "DELETE FROM catalog WHERE kind = '" << kind << "' AND key = '" << key << "'";
if (!db->execute(sql.str().c_str())) {
proxy_error("Catalog remove error\n");
return -1;
}
return 0;
}