Repository: nomic-ai/gpt4all
File: gpt4all-chat/src/database.cpp (C++)

Code before:
    where not exists(
        select 1
        from embeddings e
        where e.chunk_id = c.id and e.model = co.embedding_model
    );
)"_s;

static const QString SELECT_COUNT_CHUNKS_SQL = uR"(
    select count(c.id)
    from chunks c
    join documents d on d.id = c.document_id
    where d.folder_id = ?;
)"_s;

static const QString SELECT_CHUNKS_FTS_SQL = uR"(
    select id, bm25(chunks_fts) as score
    from chunks_fts
    where chunks_fts match ?
    order by score limit %1;
)"_s;

static bool addChunk(QSqlQuery &q, int document_id, const QString &chunk_text, const QString &file,
                     const QString &title, const QString &author, const QString &subject, const QString &keywords,
                     int page, int from, int to, int words, int *chunk_id)
{
    if (!q.prepare(INSERT_CHUNK_SQL[0]))
        return false;
    q.addBindValue(document_id);
    q.addBindValue(chunk_text);
    q.addBindValue(file);
    q.addBindValue(title);
    q.addBindValue(author);
    q.addBindValue(subject);
    q.addBindValue(keywords);
    q.addBindValue(page);
    q.addBindValue(from);
    q.addBindValue(to);
    q.addBindValue(words);
    if (!q.exec() || !q.next())
        return false;
    *chunk_id = q.value(0).toInt();

    if (!q.prepare(INSERT_CHUNK_SQL[1]))
        return false;
    q.addBindValue(document_id);
    q.addBindValue(chunk_text);
    q.addBindValue(file);
    q.addBindValue(title);
    q.addBindValue(author);
    q.addBindValue(subject);
    q.addBindValue(keywords);

Review comment (suggestion):
The point of making these arrays is so we can set the same parameters for both queries and use a loop. This is the closest approximation to multi-statement queries we can achieve with Qt's SQLite engine.

Code after:
    where not exists(
        select 1
        from embeddings e
        where e.chunk_id = c.id and e.model = co.embedding_model
    );
)"_s;

static const QString SELECT_COUNT_CHUNKS_SQL = uR"(
    select count(c.id)
    from chunks c
    join documents d on d.id = c.document_id
    where d.folder_id = ?;
)"_s;

static const QString SELECT_CHUNKS_FTS_SQL = uR"(
    select id, bm25(chunks_fts) as score
    from chunks_fts
    where chunks_fts match ?
    order by score limit %1;
)"_s;

static bool addChunk(QSqlQuery &q, int document_id, const QString &chunk_text, const QString &file,
                     const QString &title, const QString &author, const QString &subject, const QString &keywords,
                     int page, int from, int to, int words, int *chunk_id)
{
    if (!q.prepare(INSERT_CHUNK_SQL))
        return false;
    q.addBindValue(document_id);
    q.addBindValue(chunk_text);
    q.addBindValue(file);
    q.addBindValue(title);
    q.addBindValue(author);
    q.addBindValue(subject);
    q.addBindValue(keywords);
    q.addBindValue(page);
    q.addBindValue(from);
    q.addBindValue(to);
    q.addBindValue(words);
    if (!q.exec() || !q.next())
        return false;
    *chunk_id = q.value(0).toInt();

    if (!q.prepare(INSERT_CHUNK_FTS_SQL))
        return false;
    q.addBindValue(document_id);
    q.addBindValue(chunk_text);
    q.addBindValue(file);
    q.addBindValue(title);
    q.addBindValue(author);
    q.addBindValue(subject);
    q.addBindValue(keywords);