Skip to content

Commit

Permalink
Add prepare function to allow execution of arbitrary SQL on pack files
Browse files Browse the repository at this point in the history
  • Loading branch information
ethindp committed Oct 31, 2024
1 parent 62820a1 commit e57e597
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions plugin/integrated/sqlite/nvgt_sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void init_sqlite() {
sqlite_started = true;
}

sqlite3statement::sqlite3statement(sqlite3DB* p, sqlite3_stmt* s) : parent(p), statement(s), ref_count(1) {}
sqlite3statement::sqlite3statement(sqlite3_stmt* s) : statement(s), ref_count(1) {}
void sqlite3statement::add_ref() {
asAtomicInc(ref_count);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ sqlite3statement* sqlite3DB::prepare(const std::string& statement, int* statemen
if (err != SQLITE_OK) return nullptr;
sqlite3statement* ret = NULL;
if (st)
ret = new sqlite3statement(this, st);
ret = new sqlite3statement(st);
if (tail && statement_tail)
*statement_tail = (tail - statement.c_str());
return ret;
Expand Down
4 changes: 2 additions & 2 deletions plugin/integrated/sqlite/nvgt_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* 3. This notice may not be removed or altered from any source distribution.
*/

#pragma once
#include <string>
#include "../../src/nvgt_plugin.h"
#include <scriptarray.h>
Expand All @@ -20,9 +21,8 @@ class sqlite3DB;
class sqlite3statement {
int ref_count;
public:
sqlite3DB* parent;
sqlite3_stmt* statement;
sqlite3statement(sqlite3DB* p, sqlite3_stmt* s);
sqlite3statement(sqlite3_stmt* s);
void add_ref();
void release();
int step();
Expand Down
9 changes: 9 additions & 0 deletions plugin/integrated/sqlite/pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,14 @@ void* pack::open_file(const std::string& file_name, const bool rw) {
return nvgt_datastream_create(&stream, "", 1);
}

sqlite3statement* pack::prepare(const string& statement, const bool persistant) {
sqlite3_stmt* stmt;
if (const auto rc = sqlite3_prepare_v3(db, statement.data(), statement.size(), persistant ? SQLITE_PREPARE_PERSISTENT : 0, &stmt, nullptr); rc != SQLITE_OK) {
throw runtime_error(Poco::format("Parse error: %s", string(sqlite3_errmsg(db))));
}
return new sqlite3statement(stmt);
}

blob_stream_buf::blob_stream_buf(): Poco::BufferedBidirectionalStreamBuf(4096, ios::in) {
}

Expand Down Expand Up @@ -920,5 +928,6 @@ void RegisterScriptPack(asIScriptEngine* engine) {
engine->RegisterObjectMethod("sqlite_pack", "void allocate_file(const string& file_name, const int64 size, const bool allow_replace = false)", asMETHODPR(pack, allocate_file, (const string&, const int64_t, const bool), void), asCALL_THISCALL);
engine->RegisterObjectMethod("sqlite_pack", "bool rename_file(const string& old, const string& new_)", asMETHOD(pack, rename_file), asCALL_THISCALL);
engine->RegisterObjectMethod("sqlite_pack", "void clear()", asMETHOD(pack, clear), asCALL_THISCALL);
engine->RegisterObjectMethod("sqlite_pack", "sqlite3statement@ prepare(const string& statement, const bool persistant = false)", asMETHOD(pack, prepare), asCALL_THISCALL);
}

2 changes: 2 additions & 0 deletions plugin/integrated/sqlite/pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string_view>
#include <iostream>
#include <ios>
#include "nvgt_sqlite.h"

class blob_stream;

Expand Down Expand Up @@ -55,6 +56,7 @@ class pack : public Poco::RefCountedObject {
void allocate_file(const std::string& file_name, const std::int64_t size, const bool allow_replace = false);
bool rename_file(const std::string& old, const std::string& new_);
void clear();
sqlite3statement* prepare(const std::string& statement, const bool persistant = false);
};

class blob_stream_buf: public Poco::BufferedBidirectionalStreamBuf {
Expand Down

0 comments on commit e57e597

Please sign in to comment.