2024-08-20 01:36:41 +00:00
|
|
|
#include <napi.h>
|
|
|
|
#include "dal.h"
|
|
|
|
|
|
|
|
static void _InitSQLite(const Napi::CallbackInfo& args) {
|
|
|
|
Napi::Buffer<uint8_t> buf = args[0].As<Napi::Buffer<uint8_t>>();
|
2024-08-20 11:09:35 +00:00
|
|
|
GoString charstr = {reinterpret_cast<char *>(buf.Data()), long(buf.Length())};
|
2024-08-20 01:36:41 +00:00
|
|
|
InitSQLite(charstr);
|
|
|
|
}
|
|
|
|
|
2024-08-26 19:10:35 +00:00
|
|
|
static Napi::Object RowIterator(const Napi::CallbackInfo& args) {
|
|
|
|
Napi::Env env = args.Env();
|
|
|
|
Napi::Object it = Napi::Object::New( env );
|
2024-08-20 01:36:41 +00:00
|
|
|
Napi::Buffer<uint8_t> buf = args[0].As<Napi::Buffer<uint8_t>>();
|
2024-08-20 11:09:35 +00:00
|
|
|
GoSlice input = {reinterpret_cast<void *>(buf.Data()), long(buf.Length()), long(buf.Length())};
|
2024-08-26 19:10:35 +00:00
|
|
|
int iter = CreateRowIterator(input);
|
|
|
|
auto next_row = [=](const Napi::CallbackInfo& a){
|
|
|
|
void* next = NextRow(iter);
|
|
|
|
if (next == nullptr) {
|
|
|
|
FreeIter(iter);
|
|
|
|
return Napi::Buffer<uint8_t>::New(env, 0);
|
|
|
|
}
|
|
|
|
Napi::Buffer<uint8_t> val = Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(next), GetLen(iter));
|
|
|
|
free(next);
|
|
|
|
return val;
|
|
|
|
};
|
2024-08-29 20:39:27 +00:00
|
|
|
auto cleanup = [=](const Napi::CallbackInfo& a){
|
2024-08-26 19:10:35 +00:00
|
|
|
FreeIter(iter);
|
|
|
|
};
|
|
|
|
it.Set("next", Napi::Function::New(env, next_row));
|
2024-08-29 20:39:27 +00:00
|
|
|
it.Set("cleanup", Napi::Function::New(env, cleanup));
|
2024-08-26 19:10:35 +00:00
|
|
|
return it;
|
2024-08-20 01:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
2024-08-29 20:13:48 +00:00
|
|
|
exports["initSQLite"] = Napi::Function::New(env, _InitSQLite);
|
|
|
|
exports["rowIterator"] = Napi::Function::New(env, RowIterator);
|
2024-08-20 01:36:41 +00:00
|
|
|
return exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
|
|
|
|
|