Skip to content

Commit 6c61b4c

Browse files
committed
wip: add backup function
1 parent 76b80b1 commit 6c61b4c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/node_sqlite.cc

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "async_wrap-inl.h"
12
#include "node_sqlite.h"
23
#include <path.h>
34
#include "base_object-inl.h"
@@ -9,13 +10,15 @@
910
#include "node_mem-inl.h"
1011
#include "sqlite3.h"
1112
#include "util-inl.h"
13+
#include "threadpoolwork-inl.h"
1214

1315
#include <cinttypes>
1416

1517
namespace node {
1618
namespace sqlite {
1719

1820
using v8::Array;
21+
using v8::HandleScope;
1922
using v8::ArrayBuffer;
2023
using v8::BigInt;
2124
using v8::Boolean;
@@ -40,6 +43,7 @@ using v8::NewStringType;
4043
using v8::Null;
4144
using v8::Number;
4245
using v8::Object;
46+
using v8::Promise;
4347
using v8::SideEffectType;
4448
using v8::String;
4549
using v8::TryCatch;
@@ -128,6 +132,54 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
128132
isolate->ThrowException(error);
129133
}
130134

135+
class BackupJob: public ThreadPoolWork {
136+
public:
137+
explicit BackupJob(Environment* env,
138+
DatabaseSync* source,
139+
Local<Promise::Resolver> resolver,
140+
const char* source_name,
141+
const char* destination_name,
142+
int pages,
143+
int sleep) : ThreadPoolWork(env, "node_sqlite3.BackupJob"),
144+
env_(env),
145+
source_(source),
146+
/* resolver_(env->isolate(), resolver), */
147+
source_name_(source_name),
148+
destination_name_(destination_name),
149+
pages_(pages),
150+
sleep_(sleep) {
151+
resolver_.Reset(env->isolate(), resolver);
152+
}
153+
154+
void DoThreadPoolWork() override {
155+
std::cout << "Gotta do a backup here" << std::endl;
156+
}
157+
158+
void AfterThreadPoolWork(int status) override {
159+
HandleScope handle_scope(env()->isolate());
160+
161+
if (!resolver_.IsEmpty()) {
162+
Local<Promise::Resolver> resolver = Local<Promise::Resolver>::New(env()->isolate(), resolver_);
163+
Local<String> message = String::NewFromUtf8(env()->isolate(), "Backup completed", NewStringType::kNormal).ToLocalChecked();
164+
165+
// Resolve the promise with a value (Null in this case)
166+
resolver->Resolve(env()->context(), message).ToChecked();
167+
}
168+
}
169+
170+
private:
171+
172+
Environment* env() const { return env_; }
173+
174+
Environment* env_;
175+
DatabaseSync* source_;
176+
Global<Promise::Resolver> resolver_;
177+
std::string source_name_;
178+
std::string destination_name_;
179+
int pages_;
180+
int sleep_;
181+
};
182+
131183
class UserDefinedFunction {
132184
public:
133185
explicit UserDefinedFunction(Environment* env,
@@ -533,6 +585,20 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
533585
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
534586
}
535587

588+
void DatabaseSync::Backup(const FunctionCallbackInfo<Value>& args) {
589+
DatabaseSync* db;
590+
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
591+
Environment* env = Environment::GetCurrent(args);
592+
Local<Promise::Resolver> resolver = Promise::Resolver::New(env->context())
593+
.ToLocalChecked()
594+
.As<Promise::Resolver>();
595+
596+
args.GetReturnValue().Set(resolver->GetPromise());
597+
598+
BackupJob* job = new BackupJob(env, db, resolver, "main", "main", 100, 250);
599+
job->ScheduleWork();
600+
}
601+
536602
void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
537603
DatabaseSync* db;
538604
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
@@ -1718,6 +1784,7 @@ static void Initialize(Local<Object> target,
17181784
SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close);
17191785
SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare);
17201786
SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec);
1787+
SetProtoMethod(isolate, db_tmpl, "backup", DatabaseSync::Backup);
17211788
SetProtoMethod(isolate, db_tmpl, "function", DatabaseSync::CustomFunction);
17221789
SetProtoMethod(
17231790
isolate, db_tmpl, "createSession", DatabaseSync::CreateSession);

src/node_sqlite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class DatabaseSync : public BaseObject {
5757
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
5858
static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args);
5959
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args);
60+
static void Backup(const v8::FunctionCallbackInfo<v8::Value>& args);
6061
static void CustomFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
6162
static void CreateSession(const v8::FunctionCallbackInfo<v8::Value>& args);
6263
static void ApplyChangeset(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -81,6 +82,7 @@ class DatabaseSync : public BaseObject {
8182
bool enable_load_extension_;
8283
sqlite3* connection_;
8384

85+
std::set<sqlite3_backup*> backups_;
8486
std::set<sqlite3_session*> sessions_;
8587
std::unordered_set<StatementSync*> statements_;
8688

0 commit comments

Comments
 (0)