Unverified Commit 34d62838 by Kewde

Merge branch 'mohd-akram-napi'

parents 1752f424 cc4b598c
...@@ -24,4 +24,5 @@ test/nw/app.nw ...@@ -24,4 +24,5 @@ test/nw/app.nw
local.env local.env
.mason .mason
.eslintrc.js .eslintrc.js
setup.sh setup.sh
\ No newline at end of file /build-tmp-napi-v3
...@@ -7,12 +7,13 @@ Asynchronous, non-blocking [SQLite3](https://sqlite.org/) bindings for [Node.js] ...@@ -7,12 +7,13 @@ Asynchronous, non-blocking [SQLite3](https://sqlite.org/) bindings for [Node.js]
[![Coverage Status](https://coveralls.io/repos/mapbox/node-sqlite3/badge.svg?branch=master&service=github)](https://coveralls.io/github/mapbox/node-sqlite3?branch=master) [![Coverage Status](https://coveralls.io/repos/mapbox/node-sqlite3/badge.svg?branch=master&service=github)](https://coveralls.io/github/mapbox/node-sqlite3?branch=master)
[![Dependencies](https://david-dm.org/mapbox/node-sqlite3.svg)](https://david-dm.org/mapbox/node-sqlite3) [![Dependencies](https://david-dm.org/mapbox/node-sqlite3.svg)](https://david-dm.org/mapbox/node-sqlite3)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fmapbox%2Fnode-sqlite3.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fmapbox%2Fnode-sqlite3?ref=badge_shield) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fmapbox%2Fnode-sqlite3.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fmapbox%2Fnode-sqlite3?ref=badge_shield)
[![N-API v3 Badge](https://img.shields.io/badge/N--API-v3-green.svg)](https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api)
## Supported platforms ## Supported platforms
The `sqlite3` module works with: The `sqlite3` module works with:
* Node.js v4.x, v6.x, v8.x, v10.x, v11.x, v12.x and v13.x. * Node.js v11.x, v12.x, v13.x and v14.x.
* Electron v1.3.X, v1.6.x, v1.7.x, v1.8.x, v2.0.x, v3.0.x, v4.0.x, v4.1.x, v4.2.x, v5.0.x, v6.0.x, v6.1.x, v7.0.x, v7.1.x, v8.0.x, v8.1.x and v8.2.x * Electron v6.0.x, v6.1.x, v7.0.x, v7.1.x, v8.0.x, v8.1.x and v8.2.x
Binaries for most Node versions and platforms are provided by default via [node-pre-gyp](https://github.com/mapbox/node-pre-gyp). Binaries for most Node versions and platforms are provided by default via [node-pre-gyp](https://github.com/mapbox/node-pre-gyp).
......
environment: environment:
matrix: matrix:
- nodejs_version: 4
platform: x64
msvs_toolset: 12
- nodejs_version: 4
platform: x86
msvs_toolset: 12
- nodejs_version: 5
platform: x64
- nodejs_version: 5
platform: x86
- nodejs_version: 6
platform: x64
- nodejs_version: 6
platform: x86
- nodejs_version: 7
platform: x64
- nodejs_version: 7
platform: x86
- nodejs_version: 8
platform: x64
- nodejs_version: 8
platform: x86
- nodejs_version: 9
platform: x64
- nodejs_version: 9
platform: x86
- nodejs_version: 10
platform: x64
- nodejs_version: 10
platform: x86
- nodejs_version: 11 - nodejs_version: 11
platform: x64 platform: x64
- nodejs_version: 11 - nodejs_version: 11
...@@ -42,6 +12,10 @@ environment: ...@@ -42,6 +12,10 @@ environment:
platform: x64 platform: x64
- nodejs_version: 13 - nodejs_version: 13
platform: x86 platform: x86
- nodejs_version: 14
platform: x64
- nodejs_version: 14
platform: x86
# electron # electron
- nodejs_version: 12 - nodejs_version: 12
platform: x64 platform: x64
...@@ -113,100 +87,6 @@ environment: ...@@ -113,100 +87,6 @@ environment:
NODE_RUNTIME: electron NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 6.0.0 NODE_RUNTIME_VERSION: 6.0.0
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 12
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 5.0.0
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 12
platform: x86
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 5.0.0
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 10
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 4.2.0
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 10
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 4.1.0
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 10
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 4.0.0
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 10
platform: x86
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 4.0.0
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 10
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 3.0.6
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 10
platform: x86
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 3.0.6
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 8
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 2.0.1
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 8
platform: x86
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 2.0.1
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 8
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.8.4
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 8
platform: x86
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.8.4
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 7
platform: x64
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.7.12
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 7
platform: x86
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.7.12
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 6
platform: x64
msvs_toolset: 12
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.6.2
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 6
platform: x86
msvs_toolset: 12
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.6.2
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 6
platform: x64
msvs_toolset: 12
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.3.14
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
- nodejs_version: 6
platform: x86
msvs_toolset: 12
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 1.3.14
TOOLSET_ARGS: --dist-url=https://electronjs.org/headers
os: Visual Studio 2015 os: Visual Studio 2015
......
...@@ -7,10 +7,21 @@ ...@@ -7,10 +7,21 @@
"targets": [ "targets": [
{ {
"target_name": "<(module_name)", "target_name": "<(module_name)",
"include_dirs": ["<!(node -e \"require('nan')\")"], "cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"xcode_settings": { "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"CLANG_CXX_LIBRARY": "libc++",
"MACOSX_DEPLOYMENT_TARGET": "10.7",
},
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 },
},
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"],
"conditions": [ "conditions": [
["sqlite != 'internal'", { ["sqlite != 'internal'", {
"include_dirs": [ "<(sqlite)/include" ], "include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")", "<(sqlite)/include" ],
"libraries": [ "libraries": [
"-l<(sqlite_libname)" "-l<(sqlite_libname)"
], ],
...@@ -26,6 +37,7 @@ ...@@ -26,6 +37,7 @@
}, },
{ {
"dependencies": [ "dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")",
"deps/sqlite3.gyp:sqlite3" "deps/sqlite3.gyp:sqlite3"
] ]
} }
...@@ -36,7 +48,8 @@ ...@@ -36,7 +48,8 @@
"src/database.cc", "src/database.cc",
"src/node_sqlite3.cc", "src/node_sqlite3.cc",
"src/statement.cc" "src/statement.cc"
] ],
"defines": [ "NAPI_VERSION=<(napi_build_version)", "NAPI_DISABLE_CPP_EXCEPTIONS=1" ]
}, },
{ {
"target_name": "action_after_build", "target_name": "action_after_build",
......
...@@ -9,10 +9,13 @@ ...@@ -9,10 +9,13 @@
}, },
"binary": { "binary": {
"module_name": "node_sqlite3", "module_name": "node_sqlite3",
"module_path": "./lib/binding/{node_abi}-{platform}-{arch}", "module_path": "./lib/binding/napi-v{napi_build_version}-{platform}-{arch}",
"host": "https://mapbox-node-binary.s3.amazonaws.com", "host": "https://mapbox-node-binary.s3.amazonaws.com",
"remote_path": "./{name}/v{version}/{toolset}/", "remote_path": "./{name}/v{version}/{toolset}/",
"package_name": "{node_abi}-{platform}-{arch}.tar.gz" "package_name": "napi-v{napi_build_version}-{platform}-{arch}.tar.gz",
"napi_versions": [
3
]
}, },
"contributors": [ "contributors": [
"Konstantin Käfer <mail@kkaefer.com>", "Konstantin Käfer <mail@kkaefer.com>",
...@@ -37,7 +40,7 @@ ...@@ -37,7 +40,7 @@
"url": "git://github.com/mapbox/node-sqlite3.git" "url": "git://github.com/mapbox/node-sqlite3.git"
}, },
"dependencies": { "dependencies": {
"nan": "^2.12.1", "node-addon-api": "2.0.0",
"node-pre-gyp": "^0.11.0" "node-pre-gyp": "^0.11.0"
}, },
"devDependencies": { "devDependencies": {
......
#ifndef NODE_SQLITE3_SRC_ASYNC_H #ifndef NODE_SQLITE3_SRC_ASYNC_H
#define NODE_SQLITE3_SRC_ASYNC_H #define NODE_SQLITE3_SRC_ASYNC_H
#include <napi.h>
#include <uv.h>
#include "threading.h" #include "threading.h"
#include <node_version.h>
#if defined(NODE_SQLITE3_BOOST_THREADING) #if defined(NODE_SQLITE3_BOOST_THREADING)
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
...@@ -26,10 +28,12 @@ public: ...@@ -26,10 +28,12 @@ public:
: callback(cb_), parent(parent_) { : callback(cb_), parent(parent_) {
watcher.data = this; watcher.data = this;
NODE_SQLITE3_MUTEX_INIT NODE_SQLITE3_MUTEX_INIT
uv_async_init(uv_default_loop(), &watcher, reinterpret_cast<uv_async_cb>(listener)); uv_loop_t *loop;
napi_get_uv_event_loop(parent_->Env(), &loop);
uv_async_init(loop, &watcher, reinterpret_cast<uv_async_cb>(listener));
} }
static void listener(uv_async_t* handle, int status) { static void listener(uv_async_t* handle) {
Async* async = static_cast<Async*>(handle->data); Async* async = static_cast<Async*>(handle->data);
std::vector<Item*> rows; std::vector<Item*> rows;
NODE_SQLITE3_MUTEX_LOCK(&async->mutex) NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
...@@ -51,7 +55,7 @@ public: ...@@ -51,7 +55,7 @@ public:
// Need to call the listener again to ensure all items have been // Need to call the listener again to ensure all items have been
// processed. Is this a bug in uv_async? Feels like uv_close // processed. Is this a bug in uv_async? Feels like uv_close
// should handle that. // should handle that.
listener(&watcher, 0); listener(&watcher);
uv_close((uv_handle_t*)&watcher, close); uv_close((uv_handle_t*)&watcher, close);
} }
......
...@@ -8,10 +8,9 @@ ...@@ -8,10 +8,9 @@
#include <set> #include <set>
#include <sqlite3.h> #include <sqlite3.h>
#include <nan.h> #include <napi.h>
using namespace v8; using namespace Napi;
using namespace node;
namespace node_sqlite3 { namespace node_sqlite3 {
...@@ -92,22 +91,20 @@ namespace node_sqlite3 { ...@@ -92,22 +91,20 @@ namespace node_sqlite3 {
* backup.finish(); * backup.finish();
* *
*/ */
class Backup : public Nan::ObjectWrap { class Backup : public Napi::ObjectWrap<Backup> {
public: public:
static Nan::Persistent<FunctionTemplate> constructor_template; static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init); static Napi::Object Init(Napi::Env env, Napi::Object exports);
static NAN_METHOD(New);
struct Baton { struct Baton {
uv_work_t request; napi_async_work request;
Backup* backup; Backup* backup;
Nan::Persistent<Function> callback; Napi::FunctionReference callback;
Baton(Backup* backup_, Local<Function> cb_) : backup(backup_) { Baton(Backup* backup_, Napi::Function cb_) : backup(backup_) {
backup->Ref(); backup->Ref();
request.data = this; callback.Reset(cb_, 1);
callback.Reset(cb_);
} }
virtual ~Baton() { virtual ~Baton() {
backup->Unref(); backup->Unref();
...@@ -121,7 +118,7 @@ public: ...@@ -121,7 +118,7 @@ public:
std::string sourceName; std::string sourceName;
std::string destName; std::string destName;
bool filenameIsDest; bool filenameIsDest;
InitializeBaton(Database* db_, Local<Function> cb_, Backup* backup_) : InitializeBaton(Database* db_, Napi::Function cb_, Backup* backup_) :
Baton(db_, cb_), backup(backup_), filenameIsDest(true) { Baton(db_, cb_), backup(backup_), filenameIsDest(true) {
backup->Ref(); backup->Ref();
} }
...@@ -137,7 +134,7 @@ public: ...@@ -137,7 +134,7 @@ public:
struct StepBaton : Baton { struct StepBaton : Baton {
int pages; int pages;
std::set<int> retryErrorsSet; std::set<int> retryErrorsSet;
StepBaton(Backup* backup_, Local<Function> cb_, int pages_) : StepBaton(Backup* backup_, Napi::Function cb_, int pages_) :
Baton(backup_, cb_), pages(pages_) {} Baton(backup_, cb_), pages(pages_) {}
}; };
...@@ -149,21 +146,23 @@ public: ...@@ -149,21 +146,23 @@ public:
Baton* baton; Baton* baton;
}; };
Backup(Database* db_) : Nan::ObjectWrap(), void init(Database* db_) {
db(db_), db = db_;
_handle(NULL), _handle = NULL;
_otherDb(NULL), _otherDb = NULL;
_destDb(NULL), _destDb = NULL;
inited(false), inited = false;
locked(true), locked = true;
completed(false), completed = false;
failed(false), failed = false;
remaining(-1), remaining = -1;
pageCount(-1), pageCount = -1;
finished(false) { finished = false;
db->Ref(); db->Ref();
} }
Backup(const Napi::CallbackInfo& info);
~Backup() { ~Backup() {
if (!finished) { if (!finished) {
FinishAll(); FinishAll();
...@@ -173,21 +172,21 @@ public: ...@@ -173,21 +172,21 @@ public:
WORK_DEFINITION(Step); WORK_DEFINITION(Step);
WORK_DEFINITION(Finish); WORK_DEFINITION(Finish);
static NAN_GETTER(IdleGetter); Napi::Value IdleGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(CompletedGetter); Napi::Value CompletedGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(FailedGetter); Napi::Value FailedGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(PageCountGetter); Napi::Value PageCountGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(RemainingGetter); Napi::Value RemainingGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(FatalErrorGetter); Napi::Value FatalErrorGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(RetryErrorGetter); Napi::Value RetryErrorGetter(const Napi::CallbackInfo& info);
static NAN_SETTER(FatalErrorSetter); void FatalErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
static NAN_SETTER(RetryErrorSetter); void RetryErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
protected: protected:
static void Work_BeginInitialize(Database::Baton* baton); static void Work_BeginInitialize(Database::Baton* baton);
static void Work_Initialize(uv_work_t* req); static void Work_Initialize(napi_env env, void* data);
static void Work_AfterInitialize(uv_work_t* req); static void Work_AfterInitialize(napi_env env, napi_status status, void* data);
void Schedule(Work_Callback callback, Baton* baton); void Schedule(Work_Callback callback, Baton* baton);
void Process(); void Process();
...@@ -215,7 +214,7 @@ protected: ...@@ -215,7 +214,7 @@ protected:
bool finished; bool finished;
std::queue<Call*> queue; std::queue<Call*> queue;
Nan::Persistent<Array> retryErrors; Napi::Reference<Array> retryErrors;
}; };
} }
......
...@@ -3,45 +3,48 @@ ...@@ -3,45 +3,48 @@
#define NODE_SQLITE3_SRC_DATABASE_H #define NODE_SQLITE3_SRC_DATABASE_H
#include <assert.h>
#include <string> #include <string>
#include <queue> #include <queue>
#include <sqlite3.h> #include <sqlite3.h>
#include <nan.h> #include <napi.h>
#include "async.h" #include "async.h"
using namespace v8; using namespace Napi;
namespace node_sqlite3 { namespace node_sqlite3 {
class Database; class Database;
class Database : public Nan::ObjectWrap { class Database : public Napi::ObjectWrap<Database> {
public: public:
static Nan::Persistent<FunctionTemplate> constructor_template; static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init); static Napi::Object Init(Napi::Env env, Napi::Object exports);
static inline bool HasInstance(Local<Value> val) { static inline bool HasInstance(Napi::Value val) {
Nan::HandleScope scope; Napi::Env env = val.Env();
if (!val->IsObject()) return false; Napi::HandleScope scope(env);
Local<Object> obj = val.As<Object>(); if (!val.IsObject()) return false;
return Nan::New(constructor_template)->HasInstance(obj); Napi::Object obj = val.As<Napi::Object>();
return obj.InstanceOf(constructor.Value());
} }
struct Baton { struct Baton {
uv_work_t request; napi_async_work request;
Database* db; Database* db;
Nan::Persistent<Function> callback; Napi::FunctionReference callback;
int status; int status;
std::string message; std::string message;
Baton(Database* db_, Local<Function> cb_) : Baton(Database* db_, Napi::Function cb_) :
db(db_), status(SQLITE_OK) { db(db_), status(SQLITE_OK) {
db->Ref(); db->Ref();
request.data = this; if (!cb_.IsUndefined() && cb_.IsFunction()) {
callback.Reset(cb_); callback.Reset(cb_, 1);
}
} }
virtual ~Baton() { virtual ~Baton() {
db->Unref(); db->Unref();
...@@ -52,19 +55,19 @@ public: ...@@ -52,19 +55,19 @@ public:
struct OpenBaton : Baton { struct OpenBaton : Baton {
std::string filename; std::string filename;
int mode; int mode;
OpenBaton(Database* db_, Local<Function> cb_, const char* filename_, int mode_) : OpenBaton(Database* db_, Napi::Function cb_, const char* filename_, int mode_) :
Baton(db_, cb_), filename(filename_), mode(mode_) {} Baton(db_, cb_), filename(filename_), mode(mode_) {}
}; };
struct ExecBaton : Baton { struct ExecBaton : Baton {
std::string sql; std::string sql;
ExecBaton(Database* db_, Local<Function> cb_, const char* sql_) : ExecBaton(Database* db_, Napi::Function cb_, const char* sql_) :
Baton(db_, cb_), sql(sql_) {} Baton(db_, cb_), sql(sql_) {}
}; };
struct LoadExtensionBaton : Baton { struct LoadExtensionBaton : Baton {
std::string filename; std::string filename;
LoadExtensionBaton(Database* db_, Local<Function> cb_, const char* filename_) : LoadExtensionBaton(Database* db_, Napi::Function cb_, const char* filename_) :
Baton(db_, cb_), filename(filename_) {} Baton(db_, cb_), filename(filename_) {}
}; };
...@@ -100,19 +103,20 @@ public: ...@@ -100,19 +103,20 @@ public:
friend class Statement; friend class Statement;
friend class Backup; friend class Backup;
protected: void init() {
Database() : Nan::ObjectWrap(), _handle = NULL;
_handle(NULL), open = false;
open(false), closing = false;
closing(false), locked = false;
locked(false), pending = 0;
pending(0), serialize = false;
serialize(false), debug_trace = NULL;
debug_trace(NULL), debug_profile = NULL;
debug_profile(NULL), update_event = NULL;
update_event(NULL) {
} }
Database(const Napi::CallbackInfo& info);
~Database() { ~Database() {
RemoveCallbacks(); RemoveCallbacks();
sqlite3_close(_handle); sqlite3_close(_handle);
...@@ -120,40 +124,40 @@ protected: ...@@ -120,40 +124,40 @@ protected:
open = false; open = false;
} }
static NAN_METHOD(New); protected:
static void Work_BeginOpen(Baton* baton); static void Work_BeginOpen(Baton* baton);
static void Work_Open(uv_work_t* req); static void Work_Open(napi_env env, void* data);
static void Work_AfterOpen(uv_work_t* req); static void Work_AfterOpen(napi_env env, napi_status status, void* data);
static NAN_GETTER(OpenGetter); Napi::Value OpenGetter(const Napi::CallbackInfo& info);
void Schedule(Work_Callback callback, Baton* baton, bool exclusive = false); void Schedule(Work_Callback callback, Baton* baton, bool exclusive = false);
void Process(); void Process();
static NAN_METHOD(Exec); Napi::Value Exec(const Napi::CallbackInfo& info);
static void Work_BeginExec(Baton* baton); static void Work_BeginExec(Baton* baton);
static void Work_Exec(uv_work_t* req); static void Work_Exec(napi_env env, void* data);
static void Work_AfterExec(uv_work_t* req); static void Work_AfterExec(napi_env env, napi_status status, void* data);
static NAN_METHOD(Wait); Napi::Value Wait(const Napi::CallbackInfo& info);
static void Work_Wait(Baton* baton); static void Work_Wait(Baton* baton);
static NAN_METHOD(Close); Napi::Value Close(const Napi::CallbackInfo& info);
static void Work_BeginClose(Baton* baton); static void Work_BeginClose(Baton* baton);
static void Work_Close(uv_work_t* req); static void Work_Close(napi_env env, void* data);
static void Work_AfterClose(uv_work_t* req); static void Work_AfterClose(napi_env env, napi_status status, void* data);
static NAN_METHOD(LoadExtension); Napi::Value LoadExtension(const Napi::CallbackInfo& info);
static void Work_BeginLoadExtension(Baton* baton); static void Work_BeginLoadExtension(Baton* baton);
static void Work_LoadExtension(uv_work_t* req); static void Work_LoadExtension(napi_env env, void* data);
static void Work_AfterLoadExtension(uv_work_t* req); static void Work_AfterLoadExtension(napi_env env, napi_status status, void* data);
static NAN_METHOD(Serialize); Napi::Value Serialize(const Napi::CallbackInfo& info);
static NAN_METHOD(Parallelize); Napi::Value Parallelize(const Napi::CallbackInfo& info);
static NAN_METHOD(Configure); Napi::Value Configure(const Napi::CallbackInfo& info);
static NAN_METHOD(Interrupt); Napi::Value Interrupt(const Napi::CallbackInfo& info);
static void SetBusyTimeout(Baton* baton); static void SetBusyTimeout(Baton* baton);
......
...@@ -13,53 +13,57 @@ using namespace node_sqlite3; ...@@ -13,53 +13,57 @@ using namespace node_sqlite3;
namespace { namespace {
NAN_MODULE_INIT(RegisterModule) { Napi::Object RegisterModule(Napi::Env env, Napi::Object exports) {
Nan::HandleScope scope; Napi::HandleScope scope(env);
Database::Init(target); Database::Init(env, exports);
Statement::Init(target); Statement::Init(env, exports);
Backup::Init(target); Backup::Init(env, exports);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READONLY, OPEN_READONLY); exports.DefineProperties({
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READWRITE, OPEN_READWRITE); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_READONLY, OPEN_READONLY)
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_CREATE, OPEN_CREATE); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_READWRITE, OPEN_READWRITE)
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_FULLMUTEX, OPEN_FULLMUTEX); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_CREATE, OPEN_CREATE)
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_URI, OPEN_URI); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_FULLMUTEX, OPEN_FULLMUTEX)
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_SHAREDCACHE, OPEN_SHAREDCACHE); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_URI, OPEN_URI)
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_PRIVATECACHE, OPEN_PRIVATECACHE); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_SHAREDCACHE, OPEN_SHAREDCACHE)
DEFINE_CONSTANT_STRING(target, SQLITE_VERSION, VERSION); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_PRIVATECACHE, OPEN_PRIVATECACHE)
DEFINE_CONSTANT_STRING(exports, SQLITE_VERSION, VERSION)
#ifdef SQLITE_SOURCE_ID #ifdef SQLITE_SOURCE_ID
DEFINE_CONSTANT_STRING(target, SQLITE_SOURCE_ID, SOURCE_ID); DEFINE_CONSTANT_STRING(exports, SQLITE_SOURCE_ID, SOURCE_ID)
#endif #endif
DEFINE_CONSTANT_INTEGER(target, SQLITE_VERSION_NUMBER, VERSION_NUMBER); DEFINE_CONSTANT_INTEGER(exports, SQLITE_VERSION_NUMBER, VERSION_NUMBER)
DEFINE_CONSTANT_INTEGER(target, SQLITE_OK, OK); DEFINE_CONSTANT_INTEGER(exports, SQLITE_OK, OK)
DEFINE_CONSTANT_INTEGER(target, SQLITE_ERROR, ERROR); DEFINE_CONSTANT_INTEGER(exports, SQLITE_ERROR, ERROR)
DEFINE_CONSTANT_INTEGER(target, SQLITE_INTERNAL, INTERNAL); DEFINE_CONSTANT_INTEGER(exports, SQLITE_INTERNAL, INTERNAL)
DEFINE_CONSTANT_INTEGER(target, SQLITE_PERM, PERM); DEFINE_CONSTANT_INTEGER(exports, SQLITE_PERM, PERM)
DEFINE_CONSTANT_INTEGER(target, SQLITE_ABORT, ABORT); DEFINE_CONSTANT_INTEGER(exports, SQLITE_ABORT, ABORT)
DEFINE_CONSTANT_INTEGER(target, SQLITE_BUSY, BUSY); DEFINE_CONSTANT_INTEGER(exports, SQLITE_BUSY, BUSY)
DEFINE_CONSTANT_INTEGER(target, SQLITE_LOCKED, LOCKED); DEFINE_CONSTANT_INTEGER(exports, SQLITE_LOCKED, LOCKED)
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOMEM, NOMEM); DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOMEM, NOMEM)
DEFINE_CONSTANT_INTEGER(target, SQLITE_READONLY, READONLY); DEFINE_CONSTANT_INTEGER(exports, SQLITE_READONLY, READONLY)
DEFINE_CONSTANT_INTEGER(target, SQLITE_INTERRUPT, INTERRUPT); DEFINE_CONSTANT_INTEGER(exports, SQLITE_INTERRUPT, INTERRUPT)
DEFINE_CONSTANT_INTEGER(target, SQLITE_IOERR, IOERR); DEFINE_CONSTANT_INTEGER(exports, SQLITE_IOERR, IOERR)
DEFINE_CONSTANT_INTEGER(target, SQLITE_CORRUPT, CORRUPT); DEFINE_CONSTANT_INTEGER(exports, SQLITE_CORRUPT, CORRUPT)
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOTFOUND, NOTFOUND); DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOTFOUND, NOTFOUND)
DEFINE_CONSTANT_INTEGER(target, SQLITE_FULL, FULL); DEFINE_CONSTANT_INTEGER(exports, SQLITE_FULL, FULL)
DEFINE_CONSTANT_INTEGER(target, SQLITE_CANTOPEN, CANTOPEN); DEFINE_CONSTANT_INTEGER(exports, SQLITE_CANTOPEN, CANTOPEN)
DEFINE_CONSTANT_INTEGER(target, SQLITE_PROTOCOL, PROTOCOL); DEFINE_CONSTANT_INTEGER(exports, SQLITE_PROTOCOL, PROTOCOL)
DEFINE_CONSTANT_INTEGER(target, SQLITE_EMPTY, EMPTY); DEFINE_CONSTANT_INTEGER(exports, SQLITE_EMPTY, EMPTY)
DEFINE_CONSTANT_INTEGER(target, SQLITE_SCHEMA, SCHEMA); DEFINE_CONSTANT_INTEGER(exports, SQLITE_SCHEMA, SCHEMA)
DEFINE_CONSTANT_INTEGER(target, SQLITE_TOOBIG, TOOBIG); DEFINE_CONSTANT_INTEGER(exports, SQLITE_TOOBIG, TOOBIG)
DEFINE_CONSTANT_INTEGER(target, SQLITE_CONSTRAINT, CONSTRAINT); DEFINE_CONSTANT_INTEGER(exports, SQLITE_CONSTRAINT, CONSTRAINT)
DEFINE_CONSTANT_INTEGER(target, SQLITE_MISMATCH, MISMATCH); DEFINE_CONSTANT_INTEGER(exports, SQLITE_MISMATCH, MISMATCH)
DEFINE_CONSTANT_INTEGER(target, SQLITE_MISUSE, MISUSE); DEFINE_CONSTANT_INTEGER(exports, SQLITE_MISUSE, MISUSE)
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOLFS, NOLFS); DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOLFS, NOLFS)
DEFINE_CONSTANT_INTEGER(target, SQLITE_AUTH, AUTH); DEFINE_CONSTANT_INTEGER(exports, SQLITE_AUTH, AUTH)
DEFINE_CONSTANT_INTEGER(target, SQLITE_FORMAT, FORMAT); DEFINE_CONSTANT_INTEGER(exports, SQLITE_FORMAT, FORMAT)
DEFINE_CONSTANT_INTEGER(target, SQLITE_RANGE, RANGE); DEFINE_CONSTANT_INTEGER(exports, SQLITE_RANGE, RANGE)
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOTADB, NOTADB); DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOTADB, NOTADB)
});
return exports;
} }
} }
...@@ -108,4 +112,4 @@ const char* sqlite_authorizer_string(int type) { ...@@ -108,4 +112,4 @@ const char* sqlite_authorizer_string(int type) {
} }
} }
NODE_MODULE(node_sqlite3, RegisterModule) NODE_API_MODULE(node_sqlite3, RegisterModule)
#ifndef NODE_SQLITE3_SRC_STATEMENT_H #ifndef NODE_SQLITE3_SRC_STATEMENT_H
#define NODE_SQLITE3_SRC_STATEMENT_H #define NODE_SQLITE3_SRC_STATEMENT_H
#include "database.h"
#include "threading.h"
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <string> #include <string>
...@@ -12,10 +8,13 @@ ...@@ -12,10 +8,13 @@
#include <vector> #include <vector>
#include <sqlite3.h> #include <sqlite3.h>
#include <nan.h> #include <napi.h>
#include <uv.h>
using namespace v8; #include "database.h"
using namespace node; #include "threading.h"
using namespace Napi;
namespace node_sqlite3 { namespace node_sqlite3 {
...@@ -71,23 +70,22 @@ typedef Row Parameters; ...@@ -71,23 +70,22 @@ typedef Row Parameters;
class Statement : public Nan::ObjectWrap { class Statement : public Napi::ObjectWrap<Statement> {
public: public:
static Nan::Persistent<FunctionTemplate> constructor_template; static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init); static Napi::Object Init(Napi::Env env, Napi::Object exports);
static NAN_METHOD(New); static Napi::Value New(const Napi::CallbackInfo& info);
struct Baton { struct Baton {
uv_work_t request; napi_async_work request;
Statement* stmt; Statement* stmt;
Nan::Persistent<Function> callback; Napi::FunctionReference callback;
Parameters parameters; Parameters parameters;
Baton(Statement* stmt_, Local<Function> cb_) : stmt(stmt_) { Baton(Statement* stmt_, Napi::Function cb_) : stmt(stmt_) {
stmt->Ref(); stmt->Ref();
request.data = this; callback.Reset(cb_, 1);
callback.Reset(cb_);
} }
virtual ~Baton() { virtual ~Baton() {
for (unsigned int i = 0; i < parameters.size(); i++) { for (unsigned int i = 0; i < parameters.size(); i++) {
...@@ -100,20 +98,20 @@ public: ...@@ -100,20 +98,20 @@ public:
}; };
struct RowBaton : Baton { struct RowBaton : Baton {
RowBaton(Statement* stmt_, Local<Function> cb_) : RowBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {} Baton(stmt_, cb_) {}
Row row; Row row;
}; };
struct RunBaton : Baton { struct RunBaton : Baton {
RunBaton(Statement* stmt_, Local<Function> cb_) : RunBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_), inserted_id(0), changes(0) {} Baton(stmt_, cb_), inserted_id(0), changes(0) {}
sqlite3_int64 inserted_id; sqlite3_int64 inserted_id;
int changes; int changes;
}; };
struct RowsBaton : Baton { struct RowsBaton : Baton {
RowsBaton(Statement* stmt_, Local<Function> cb_) : RowsBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {} Baton(stmt_, cb_) {}
Rows rows; Rows rows;
}; };
...@@ -121,10 +119,10 @@ public: ...@@ -121,10 +119,10 @@ public:
struct Async; struct Async;
struct EachBaton : Baton { struct EachBaton : Baton {
Nan::Persistent<Function> completed; Napi::FunctionReference completed;
Async* async; // Isn't deleted when the baton is deleted. Async* async; // Isn't deleted when the baton is deleted.
EachBaton(Statement* stmt_, Local<Function> cb_) : EachBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {} Baton(stmt_, cb_) {}
virtual ~EachBaton() { virtual ~EachBaton() {
completed.Reset(); completed.Reset();
...@@ -134,7 +132,7 @@ public: ...@@ -134,7 +132,7 @@ public:
struct PrepareBaton : Database::Baton { struct PrepareBaton : Database::Baton {
Statement* stmt; Statement* stmt;
std::string sql; std::string sql;
PrepareBaton(Database* db_, Local<Function> cb_, Statement* stmt_) : PrepareBaton(Database* db_, Napi::Function cb_, Statement* stmt_) :
Baton(db_, cb_), stmt(stmt_) { Baton(db_, cb_), stmt(stmt_) {
stmt->Ref(); stmt->Ref();
} }
...@@ -143,7 +141,7 @@ public: ...@@ -143,7 +141,7 @@ public:
if (!db->IsOpen() && db->IsLocked()) { if (!db->IsOpen() && db->IsLocked()) {
// The database handle was closed before the statement could be // The database handle was closed before the statement could be
// prepared. // prepared.
stmt->Finalize(); stmt->Finalize_();
} }
} }
}; };
...@@ -166,15 +164,17 @@ public: ...@@ -166,15 +164,17 @@ public:
// Store the callbacks here because we don't have // Store the callbacks here because we don't have
// access to the baton in the async callback. // access to the baton in the async callback.
Nan::Persistent<Function> item_cb; Napi::FunctionReference item_cb;
Nan::Persistent<Function> completed_cb; Napi::FunctionReference completed_cb;
Async(Statement* st, uv_async_cb async_cb) : Async(Statement* st, uv_async_cb async_cb) :
stmt(st), completed(false), retrieved(0) { stmt(st), completed(false), retrieved(0) {
watcher.data = this; watcher.data = this;
NODE_SQLITE3_MUTEX_INIT NODE_SQLITE3_MUTEX_INIT
stmt->Ref(); stmt->Ref();
uv_async_init(uv_default_loop(), &watcher, async_cb); uv_loop_t *loop;
napi_get_uv_event_loop(stmt->Env(), &loop);
uv_async_init(loop, &watcher, async_cb);
} }
~Async() { ~Async() {
...@@ -185,18 +185,20 @@ public: ...@@ -185,18 +185,20 @@ public:
} }
}; };
Statement(Database* db_) : Nan::ObjectWrap(), void init(Database* db_) {
db(db_), db = db_;
_handle(NULL), _handle = NULL;
status(SQLITE_OK), status = SQLITE_OK;
prepared(false), prepared = false;
locked(true), locked = true;
finalized(false) { finalized = false;
db->Ref(); db->Ref();
} }
Statement(const Napi::CallbackInfo& info);
~Statement() { ~Statement() {
if (!finalized) Finalize(); if (!finalized) Finalize_();
} }
WORK_DEFINITION(Bind); WORK_DEFINITION(Bind);
...@@ -206,25 +208,25 @@ public: ...@@ -206,25 +208,25 @@ public:
WORK_DEFINITION(Each); WORK_DEFINITION(Each);
WORK_DEFINITION(Reset); WORK_DEFINITION(Reset);
static NAN_METHOD(Finalize); Napi::Value Finalize_(const Napi::CallbackInfo& info);
protected: protected:
static void Work_BeginPrepare(Database::Baton* baton); static void Work_BeginPrepare(Database::Baton* baton);
static void Work_Prepare(uv_work_t* req); static void Work_Prepare(napi_env env, void* data);
static void Work_AfterPrepare(uv_work_t* req); static void Work_AfterPrepare(napi_env env, napi_status status, void* data);
static void AsyncEach(uv_async_t* handle, int status); static void AsyncEach(uv_async_t* handle);
static void CloseCallback(uv_handle_t* handle); static void CloseCallback(uv_handle_t* handle);
static void Finalize(Baton* baton); static void Finalize_(Baton* baton);
void Finalize(); void Finalize_();
template <class T> inline Values::Field* BindParameter(const Local<Value> source, T pos); template <class T> inline Values::Field* BindParameter(const Napi::Value source, T pos);
template <class T> T* Bind(Nan::NAN_METHOD_ARGS_TYPE info, int start = 0, int end = -1); template <class T> T* Bind(const Napi::CallbackInfo& info, int start = 0, int end = -1);
bool Bind(const Parameters &parameters); bool Bind(const Parameters &parameters);
static void GetRow(Row* row, sqlite3_stmt* stmt); static void GetRow(Row* row, sqlite3_stmt* stmt);
static Local<Object> RowToJS(Row* row); static Napi::Value RowToJS(Napi::Env env, Row* row);
void Schedule(Work_Callback callback, Baton* baton); void Schedule(Work_Callback callback, Baton* baton);
void Process(); void Process();
void CleanQueue(); void CleanQueue();
......
...@@ -11,7 +11,7 @@ describe('query properties', function() { ...@@ -11,7 +11,7 @@ describe('query properties', function() {
it('should return the correct lastID', function(done) { it('should return the correct lastID', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?, ?)"); var stmt = db.prepare("INSERT INTO foo VALUES(?, ?)");
var j = 1; var j = 1;
for (var i = 0; i < 1000; i++) { for (var i = 0; i < 5000; i++) {
stmt.run(i, "demo", function(err) { stmt.run(i, "demo", function(err) {
if (err) throw err; if (err) throw err;
// Relies on SQLite's row numbering to be gapless and starting // Relies on SQLite's row numbering to be gapless and starting
...@@ -25,7 +25,7 @@ describe('query properties', function() { ...@@ -25,7 +25,7 @@ describe('query properties', function() {
it('should return the correct changes count', function(done) { it('should return the correct changes count', function(done) {
db.run("UPDATE foo SET id = id + 1 WHERE id % 2 = 0", function(err) { db.run("UPDATE foo SET id = id + 1 WHERE id % 2 = 0", function(err) {
if (err) throw err; if (err) throw err;
assert.equal(500, this.changes); assert.equal(2500, this.changes);
done(); done();
}); });
}); });
......
...@@ -10,11 +10,11 @@ describe('error handling', function() { ...@@ -10,11 +10,11 @@ describe('error handling', function() {
it('throw when calling Database() without new', function() { it('throw when calling Database() without new', function() {
assert.throws(function() { assert.throws(function() {
sqlite3.Database(':memory:'); sqlite3.Database(':memory:');
}, (/Use the new operator to create new Database objects/)); }, (/Class constructors cannot be invoked without 'new'/));
assert.throws(function() { assert.throws(function() {
sqlite3.Statement(); sqlite3.Statement();
}, (/Use the new operator to create new Statement objects/)); }, (/Class constructors cannot be invoked without 'new'/));
}); });
it('should error when calling Database#get on a missing table', function(done) { it('should error when calling Database#get on a missing table', function(done) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment