Repository navigation
Expand file tree
/
Copy pathSchema.cpp
More file actions
372 lines (340 loc) · 12.1 KB
/
Copy pathSchema.cpp
File metadata and controls
372 lines (340 loc) · 12.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
#include "Schema.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QStringList>
namespace {
// Parses the column list out of
// CREATE VIRTUAL TABLE name USING vec0(embedding float[384], id integer primary key, ...)
// PRAGMA table_info can't be used on a vec0 table (Qt's SQLite has no vec0
// module), so the declaration itself is the only source of column info.
QVector<Column> parseVec0Columns(const QString& createSql)
{
QVector<Column> columns;
const int usingPos = createSql.indexOf(QStringLiteral("vec0"), 0, Qt::CaseInsensitive);
if (usingPos < 0) {
return columns;
}
const int open = createSql.indexOf(QLatin1Char('('), usingPos);
if (open < 0) {
return columns;
}
// Split the argument list on top-level commas only: "float[384]" and
// any nested parentheses must not be split apart.
QStringList parts;
QString current;
int depth = 1;
for (int i = open + 1; i < createSql.size(); ++i) {
const QChar ch = createSql.at(i);
if (ch == QLatin1Char('(') || ch == QLatin1Char('[')) {
++depth;
} else if (ch == QLatin1Char(')') || ch == QLatin1Char(']')) {
--depth;
if (depth == 0) {
break; // closing paren of vec0(...)
}
} else if (ch == QLatin1Char(',') && depth == 1) {
parts << current;
current.clear();
continue;
}
current += ch;
}
if (!current.trimmed().isEmpty()) {
parts << current;
}
for (const QString& rawPart : parts) {
QString part = rawPart.simplified();
if (part.isEmpty()) {
continue;
}
// Auxiliary columns are declared as "+name type".
if (part.startsWith(QLatin1Char('+'))) {
part.remove(0, 1);
}
// Table-level options such as "chunk_size=1024" are not columns.
const int space = part.indexOf(QLatin1Char(' '));
const int eq = part.indexOf(QLatin1Char('='));
if (eq >= 0 && (space < 0 || eq < space)) {
continue;
}
Column col;
QString name = space < 0 ? part : part.left(space);
name.remove(QLatin1Char('"')).remove(QLatin1Char('`'));
name.remove(QLatin1Char('[')).remove(QLatin1Char(']'));
col.name = name;
col.type = space < 0 ? QString() : part.mid(space + 1);
col.isPrimaryKey = part.contains(QStringLiteral("primary key"), Qt::CaseInsensitive);
columns.push_back(col);
}
return columns;
}
} // namespace
bool Schema::load(QSqlDatabase& db)
{
m_tables.clear();
m_vectorTables.clear();
m_foreignKeys.clear();
m_indexes.clear();
m_errorMessage.clear();
m_hasVectorTables = false;
// sqlite-vec (vec0) virtual tables cannot be inspected through Qt's bundled
// SQLite (the vec0 module is not loaded there, so PRAGMA table_info fails with
// "no such module: vec0"). They (and their shadow tables) are kept out of
// m_tables, but each vec0 table is still recorded in m_vectorTables, with
// its columns parsed from the CREATE statement, so the table list can show
// it. Browsing the vectors themselves is done via Tools > Vector space (3D).
QStringList vecTables;
{
QSqlQuery vecQuery(db);
if (vecQuery.exec(
"SELECT name, sql FROM sqlite_master "
"WHERE type='table' AND sql LIKE 'CREATE VIRTUAL TABLE%' "
"AND lower(sql) LIKE '%using vec0%' "
"ORDER BY name;")) {
while (vecQuery.next()) {
Table vecTable;
vecTable.name = vecQuery.value(0).toString();
vecTable.columns = parseVec0Columns(vecQuery.value(1).toString());
vecTables << vecTable.name;
m_vectorTables.push_back(vecTable);
}
}
}
m_hasVectorTables = !vecTables.isEmpty();
static const QStringList kVecShadowSuffixes = {"_chunks", "_rowids", "_vector_chunks", "_info", "_auxiliary"};
const auto isVecObject = [&vecTables](const QString& name) {
for (const QString& vt : vecTables) {
if (name == vt) {
return true;
}
for (const QString& suffix : kVecShadowSuffixes) {
if (name.startsWith(vt + suffix)) {
return true;
}
}
}
return false;
};
QSqlQuery tableListQuery(db);
if (!tableListQuery.exec(
"SELECT name FROM sqlite_master "
"WHERE type='table' AND name NOT LIKE 'sqlite_%' "
"ORDER BY name;")) {
m_errorMessage = tableListQuery.lastError().text();
return false;
}
while (tableListQuery.next()) {
Table table;
table.name = tableListQuery.value(0).toString();
if (isVecObject(table.name)) {
continue;
}
// PRAGMA statements can't be parameterised, so we build the string
// directly. Table names come from sqlite_master, not user input,
// so this is safe.
QSqlQuery columnQuery(db);
if (!columnQuery.exec(QString("PRAGMA table_info(%1);").arg(table.name))) {
m_errorMessage = columnQuery.lastError().text();
return false;
}
while (columnQuery.next()) {
Column col;
col.name = columnQuery.value("name").toString();
col.type = columnQuery.value("type").toString();
col.isNotNull = columnQuery.value("notnull").toInt() != 0;
col.isPrimaryKey = columnQuery.value("pk").toInt() != 0;
table.columns.push_back(col);
}
QSqlQuery fkQuery(db);
if (!fkQuery.exec(QString("PRAGMA foreign_key_list(%1);").arg(table.name))) {
m_errorMessage = fkQuery.lastError().text();
return false;
}
while (fkQuery.next()) {
ForeignKey fk;
fk.fromTable = table.name;
fk.fromColumn = fkQuery.value("from").toString();
fk.toTable = fkQuery.value("table").toString();
fk.toColumn = fkQuery.value("to").toString();
m_foreignKeys.push_back(fk);
}
QSqlQuery indexListQuery(db);
if (!indexListQuery.exec(QString("PRAGMA index_list(%1);").arg(table.name))) {
m_errorMessage = indexListQuery.lastError().text();
return false;
}
while (indexListQuery.next()) {
const QString indexName = indexListQuery.value("name").toString();
// SQLite creates these implicitly to back PRIMARY KEY/UNIQUE
// constraints; they can't be dropped directly and aren't
// something the user created, so skip them.
if (indexName.startsWith("sqlite_autoindex_")) {
continue;
}
Index index;
index.name = indexName;
index.table = table.name;
index.isUnique = indexListQuery.value("unique").toInt() != 0;
QSqlQuery indexInfoQuery(db);
if (!indexInfoQuery.exec(QString("PRAGMA index_info(%1);").arg(index.name))) {
m_errorMessage = indexInfoQuery.lastError().text();
return false;
}
while (indexInfoQuery.next()) {
index.columns << indexInfoQuery.value("name").toString();
}
m_indexes.push_back(index);
}
m_tables.push_back(table);
}
// "REFERENCES other" without a column list leaves PRAGMA's "to" empty,
// meaning "the primary key of other". Resolve it here so every
// consumer (JOIN ... ON ...) gets a usable column name.
for (auto& fk : m_foreignKeys) {
if (!fk.toColumn.isEmpty()) {
continue;
}
if (const Table* target = findTable(fk.toTable)) {
for (const auto& col : target->columns) {
if (col.isPrimaryKey) {
fk.toColumn = col.name;
break;
}
}
}
}
addVectorRelations();
return true;
}
bool Schema::isVectorTable(const QString& tableName) const
{
for (const auto& t : m_vectorTables) {
if (t.name == tableName) {
return true;
}
}
return false;
}
const Table* Schema::findAnyTable(const QString& tableName) const
{
if (const Table* t = findTable(tableName)) {
return t;
}
for (const auto& t : m_vectorTables) {
if (t.name == tableName) {
return &t;
}
}
return nullptr;
}
void Schema::addVectorRelations()
{
static const QStringList kPrefixes = {"vec_", "vss_", "embeddings_", "embedding_", "vec"};
static const QStringList kSuffixes = {"_vec", "_vectors", "_embeddings", "_embedding", "_emb"};
for (const auto& vt : m_vectorTables) {
QString vecPk;
for (const auto& col : vt.columns) {
if (col.isPrimaryKey) {
vecPk = col.name;
break;
}
}
if (vecPk.isEmpty()) {
vecPk = QStringLiteral("rowid");
}
// Candidate base-table names: the vec table's name with a known
// prefix/suffix removed.
QStringList bases;
for (const QString& p : kPrefixes) {
if (vt.name.startsWith(p, Qt::CaseInsensitive) && vt.name.size() > p.size()) {
bases << vt.name.mid(p.size());
}
}
for (const QString& s : kSuffixes) {
if (vt.name.endsWith(s, Qt::CaseInsensitive) && vt.name.size() > s.size()) {
bases << vt.name.left(vt.name.size() - s.size());
}
}
for (const QString& base : bases) {
const Table* target = nullptr;
for (const auto& t : m_tables) {
if (t.name.compare(base, Qt::CaseInsensitive) == 0) {
target = &t;
break;
}
}
if (!target) {
continue;
}
QString toColumn;
for (const auto& col : target->columns) {
if (col.isPrimaryKey) {
toColumn = col.name;
break;
}
}
if (toColumn.isEmpty()) {
continue;
}
ForeignKey fk;
fk.fromTable = vt.name;
fk.fromColumn = vecPk;
fk.toTable = target->name;
fk.toColumn = toColumn;
m_foreignKeys.push_back(fk);
break;
}
}
}
const Table* Schema::findTable(const QString& tableName) const
{
for (const auto& t : m_tables) {
if (t.name == tableName) {
return &t;
}
}
return nullptr;
}
QVector<ForeignKey> Schema::relationsFor(const QString& tableName) const
{
QVector<ForeignKey> result;
for (const auto& fk : m_foreignKeys) {
if (fk.fromTable == tableName || fk.toTable == tableName) {
result.push_back(fk);
}
}
return result;
}
QVector<Index> Schema::indexesFor(const QString& tableName) const
{
QVector<Index> result;
for (const auto& index : m_indexes) {
if (index.table == tableName) {
result.push_back(index);
}
}
return result;
}
QVector<JoinCandidate> Schema::joinCandidates(const QVector<QString>& queryTables) const
{
QVector<JoinCandidate> result;
for (const auto& fk : m_foreignKeys) {
if (fk.fromTable == fk.toTable) {
continue; // self-references would need aliases
}
const bool fromInQuery = queryTables.contains(fk.fromTable);
const bool toInQuery = queryTables.contains(fk.toTable);
if (fromInQuery == toInQuery) {
continue; // both already joined, or unrelated to the query
}
JoinCandidate candidate;
candidate.relation = fk;
candidate.table = fromInQuery ? fk.toTable : fk.fromTable;
candidate.anchorTable = fromInQuery ? fk.fromTable : fk.toTable;
if (!findAnyTable(candidate.table)) {
continue; // FK pointing at a table that doesn't exist
}
result.push_back(candidate);
}
return result;
}