Unverified Commit 34d62838 by Kewde

Merge branch 'mohd-akram-napi'

parents 1752f424 cc4b598c
......@@ -24,4 +24,5 @@ test/nw/app.nw
local.env
.mason
.eslintrc.js
setup.sh
\ No newline at end of file
setup.sh
/build-tmp-napi-v3
......@@ -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)
[![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)
[![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
The `sqlite3` module works with:
* Node.js v4.x, v6.x, v8.x, v10.x, v11.x, v12.x and v13.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
* Node.js v11.x, v12.x, v13.x and v14.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).
......
environment:
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
platform: x64
- nodejs_version: 11
......@@ -42,6 +12,10 @@ environment:
platform: x64
- nodejs_version: 13
platform: x86
- nodejs_version: 14
platform: x64
- nodejs_version: 14
platform: x86
# electron
- nodejs_version: 12
platform: x64
......@@ -113,100 +87,6 @@ environment:
NODE_RUNTIME: electron
NODE_RUNTIME_VERSION: 6.0.0
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
......
......@@ -7,10 +7,21 @@
"targets": [
{
"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": [
["sqlite != 'internal'", {
"include_dirs": [ "<(sqlite)/include" ],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")", "<(sqlite)/include" ],
"libraries": [
"-l<(sqlite_libname)"
],
......@@ -26,6 +37,7 @@
},
{
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")",
"deps/sqlite3.gyp:sqlite3"
]
}
......@@ -36,7 +48,8 @@
"src/database.cc",
"src/node_sqlite3.cc",
"src/statement.cc"
]
],
"defines": [ "NAPI_VERSION=<(napi_build_version)", "NAPI_DISABLE_CPP_EXCEPTIONS=1" ]
},
{
"target_name": "action_after_build",
......
......@@ -9,10 +9,13 @@
},
"binary": {
"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",
"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": [
"Konstantin Käfer <mail@kkaefer.com>",
......@@ -37,7 +40,7 @@
"url": "git://github.com/mapbox/node-sqlite3.git"
},
"dependencies": {
"nan": "^2.12.1",
"node-addon-api": "2.0.0",
"node-pre-gyp": "^0.11.0"
},
"devDependencies": {
......
#ifndef NODE_SQLITE3_SRC_ASYNC_H
#define NODE_SQLITE3_SRC_ASYNC_H
#include <napi.h>
#include <uv.h>
#include "threading.h"
#include <node_version.h>
#if defined(NODE_SQLITE3_BOOST_THREADING)
#include <boost/thread/mutex.hpp>
......@@ -26,10 +28,12 @@ public:
: callback(cb_), parent(parent_) {
watcher.data = this;
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);
std::vector<Item*> rows;
NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
......@@ -51,7 +55,7 @@ public:
// Need to call the listener again to ensure all items have been
// processed. Is this a bug in uv_async? Feels like uv_close
// should handle that.
listener(&watcher, 0);
listener(&watcher);
uv_close((uv_handle_t*)&watcher, close);
}
......
......@@ -8,10 +8,9 @@
#include <set>
#include <sqlite3.h>
#include <nan.h>
#include <napi.h>
using namespace v8;
using namespace node;
using namespace Napi;
namespace node_sqlite3 {
......@@ -92,22 +91,20 @@ namespace node_sqlite3 {
* backup.finish();
*
*/
class Backup : public Nan::ObjectWrap {
class Backup : public Napi::ObjectWrap<Backup> {
public:
static Nan::Persistent<FunctionTemplate> constructor_template;
static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init);
static NAN_METHOD(New);
static Napi::Object Init(Napi::Env env, Napi::Object exports);
struct Baton {
uv_work_t request;
napi_async_work request;
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();
request.data = this;
callback.Reset(cb_);
callback.Reset(cb_, 1);
}
virtual ~Baton() {
backup->Unref();
......@@ -121,7 +118,7 @@ public:
std::string sourceName;
std::string destName;
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) {
backup->Ref();
}
......@@ -137,7 +134,7 @@ public:
struct StepBaton : Baton {
int pages;
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_) {}
};
......@@ -149,21 +146,23 @@ public:
Baton* baton;
};
Backup(Database* db_) : Nan::ObjectWrap(),
db(db_),
_handle(NULL),
_otherDb(NULL),
_destDb(NULL),
inited(false),
locked(true),
completed(false),
failed(false),
remaining(-1),
pageCount(-1),
finished(false) {
void init(Database* db_) {
db = db_;
_handle = NULL;
_otherDb = NULL;
_destDb = NULL;
inited = false;
locked = true;
completed = false;
failed = false;
remaining = -1;
pageCount = -1;
finished = false;
db->Ref();
}
Backup(const Napi::CallbackInfo& info);
~Backup() {
if (!finished) {
FinishAll();
......@@ -173,21 +172,21 @@ public:
WORK_DEFINITION(Step);
WORK_DEFINITION(Finish);
static NAN_GETTER(IdleGetter);
static NAN_GETTER(CompletedGetter);
static NAN_GETTER(FailedGetter);
static NAN_GETTER(PageCountGetter);
static NAN_GETTER(RemainingGetter);
static NAN_GETTER(FatalErrorGetter);
static NAN_GETTER(RetryErrorGetter);
Napi::Value IdleGetter(const Napi::CallbackInfo& info);
Napi::Value CompletedGetter(const Napi::CallbackInfo& info);
Napi::Value FailedGetter(const Napi::CallbackInfo& info);
Napi::Value PageCountGetter(const Napi::CallbackInfo& info);
Napi::Value RemainingGetter(const Napi::CallbackInfo& info);
Napi::Value FatalErrorGetter(const Napi::CallbackInfo& info);
Napi::Value RetryErrorGetter(const Napi::CallbackInfo& info);
static NAN_SETTER(FatalErrorSetter);
static NAN_SETTER(RetryErrorSetter);
void FatalErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
void RetryErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
protected:
static void Work_BeginInitialize(Database::Baton* baton);
static void Work_Initialize(uv_work_t* req);
static void Work_AfterInitialize(uv_work_t* req);
static void Work_Initialize(napi_env env, void* data);
static void Work_AfterInitialize(napi_env env, napi_status status, void* data);
void Schedule(Work_Callback callback, Baton* baton);
void Process();
......@@ -215,7 +214,7 @@ protected:
bool finished;
std::queue<Call*> queue;
Nan::Persistent<Array> retryErrors;
Napi::Reference<Array> retryErrors;
};
}
......
......@@ -3,45 +3,48 @@
#define NODE_SQLITE3_SRC_DATABASE_H
#include <assert.h>
#include <string>
#include <queue>
#include <sqlite3.h>
#include <nan.h>
#include <napi.h>
#include "async.h"
using namespace v8;
using namespace Napi;
namespace node_sqlite3 {
class Database;
class Database : public Nan::ObjectWrap {
class Database : public Napi::ObjectWrap<Database> {
public:
static Nan::Persistent<FunctionTemplate> constructor_template;
static NAN_MODULE_INIT(Init);
static inline bool HasInstance(Local<Value> val) {
Nan::HandleScope scope;
if (!val->IsObject()) return false;
Local<Object> obj = val.As<Object>();
return Nan::New(constructor_template)->HasInstance(obj);
static Napi::FunctionReference constructor;
static Napi::Object Init(Napi::Env env, Napi::Object exports);
static inline bool HasInstance(Napi::Value val) {
Napi::Env env = val.Env();
Napi::HandleScope scope(env);
if (!val.IsObject()) return false;
Napi::Object obj = val.As<Napi::Object>();
return obj.InstanceOf(constructor.Value());
}
struct Baton {
uv_work_t request;
napi_async_work request;
Database* db;
Nan::Persistent<Function> callback;
Napi::FunctionReference callback;
int status;
std::string message;
Baton(Database* db_, Local<Function> cb_) :
Baton(Database* db_, Napi::Function cb_) :
db(db_), status(SQLITE_OK) {
db->Ref();
request.data = this;
callback.Reset(cb_);
if (!cb_.IsUndefined() && cb_.IsFunction()) {
callback.Reset(cb_, 1);
}
}
virtual ~Baton() {
db->Unref();
......@@ -52,19 +55,19 @@ public:
struct OpenBaton : Baton {
std::string filename;
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_) {}
};
struct ExecBaton : Baton {
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_) {}
};
struct LoadExtensionBaton : Baton {
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_) {}
};
......@@ -100,19 +103,20 @@ public:
friend class Statement;
friend class Backup;
protected:
Database() : Nan::ObjectWrap(),
_handle(NULL),
open(false),
closing(false),
locked(false),
pending(0),
serialize(false),
debug_trace(NULL),
debug_profile(NULL),
update_event(NULL) {
void init() {
_handle = NULL;
open = false;
closing = false;
locked = false;
pending = 0;
serialize = false;
debug_trace = NULL;
debug_profile = NULL;
update_event = NULL;
}
Database(const Napi::CallbackInfo& info);
~Database() {
RemoveCallbacks();
sqlite3_close(_handle);
......@@ -120,40 +124,40 @@ protected:
open = false;
}
static NAN_METHOD(New);
protected:
static void Work_BeginOpen(Baton* baton);
static void Work_Open(uv_work_t* req);
static void Work_AfterOpen(uv_work_t* req);
static void Work_Open(napi_env env, void* data);
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 Process();
static NAN_METHOD(Exec);
Napi::Value Exec(const Napi::CallbackInfo& info);
static void Work_BeginExec(Baton* baton);
static void Work_Exec(uv_work_t* req);
static void Work_AfterExec(uv_work_t* req);
static void Work_Exec(napi_env env, void* data);
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 NAN_METHOD(Close);
Napi::Value Close(const Napi::CallbackInfo& info);
static void Work_BeginClose(Baton* baton);
static void Work_Close(uv_work_t* req);
static void Work_AfterClose(uv_work_t* req);
static void Work_Close(napi_env env, void* data);
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_LoadExtension(uv_work_t* req);
static void Work_AfterLoadExtension(uv_work_t* req);
static void Work_LoadExtension(napi_env env, void* data);
static void Work_AfterLoadExtension(napi_env env, napi_status status, void* data);
static NAN_METHOD(Serialize);
static NAN_METHOD(Parallelize);
Napi::Value Serialize(const Napi::CallbackInfo& info);
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);
......
......@@ -13,53 +13,57 @@ using namespace node_sqlite3;
namespace {
NAN_MODULE_INIT(RegisterModule) {
Nan::HandleScope scope;
Napi::Object RegisterModule(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Database::Init(target);
Statement::Init(target);
Backup::Init(target);
Database::Init(env, exports);
Statement::Init(env, exports);
Backup::Init(env, exports);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READONLY, OPEN_READONLY);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READWRITE, OPEN_READWRITE);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_CREATE, OPEN_CREATE);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_FULLMUTEX, OPEN_FULLMUTEX);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_URI, OPEN_URI);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_SHAREDCACHE, OPEN_SHAREDCACHE);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_PRIVATECACHE, OPEN_PRIVATECACHE);
DEFINE_CONSTANT_STRING(target, SQLITE_VERSION, VERSION);
exports.DefineProperties({
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_READONLY, OPEN_READONLY)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_READWRITE, OPEN_READWRITE)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_CREATE, OPEN_CREATE)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_FULLMUTEX, OPEN_FULLMUTEX)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_URI, OPEN_URI)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_SHAREDCACHE, OPEN_SHAREDCACHE)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OPEN_PRIVATECACHE, OPEN_PRIVATECACHE)
DEFINE_CONSTANT_STRING(exports, SQLITE_VERSION, VERSION)
#ifdef SQLITE_SOURCE_ID
DEFINE_CONSTANT_STRING(target, SQLITE_SOURCE_ID, SOURCE_ID);
DEFINE_CONSTANT_STRING(exports, SQLITE_SOURCE_ID, SOURCE_ID)
#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(target, SQLITE_ERROR, ERROR);
DEFINE_CONSTANT_INTEGER(target, SQLITE_INTERNAL, INTERNAL);
DEFINE_CONSTANT_INTEGER(target, SQLITE_PERM, PERM);
DEFINE_CONSTANT_INTEGER(target, SQLITE_ABORT, ABORT);
DEFINE_CONSTANT_INTEGER(target, SQLITE_BUSY, BUSY);
DEFINE_CONSTANT_INTEGER(target, SQLITE_LOCKED, LOCKED);
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOMEM, NOMEM);
DEFINE_CONSTANT_INTEGER(target, SQLITE_READONLY, READONLY);
DEFINE_CONSTANT_INTEGER(target, SQLITE_INTERRUPT, INTERRUPT);
DEFINE_CONSTANT_INTEGER(target, SQLITE_IOERR, IOERR);
DEFINE_CONSTANT_INTEGER(target, SQLITE_CORRUPT, CORRUPT);
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOTFOUND, NOTFOUND);
DEFINE_CONSTANT_INTEGER(target, SQLITE_FULL, FULL);
DEFINE_CONSTANT_INTEGER(target, SQLITE_CANTOPEN, CANTOPEN);
DEFINE_CONSTANT_INTEGER(target, SQLITE_PROTOCOL, PROTOCOL);
DEFINE_CONSTANT_INTEGER(target, SQLITE_EMPTY, EMPTY);
DEFINE_CONSTANT_INTEGER(target, SQLITE_SCHEMA, SCHEMA);
DEFINE_CONSTANT_INTEGER(target, SQLITE_TOOBIG, TOOBIG);
DEFINE_CONSTANT_INTEGER(target, SQLITE_CONSTRAINT, CONSTRAINT);
DEFINE_CONSTANT_INTEGER(target, SQLITE_MISMATCH, MISMATCH);
DEFINE_CONSTANT_INTEGER(target, SQLITE_MISUSE, MISUSE);
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOLFS, NOLFS);
DEFINE_CONSTANT_INTEGER(target, SQLITE_AUTH, AUTH);
DEFINE_CONSTANT_INTEGER(target, SQLITE_FORMAT, FORMAT);
DEFINE_CONSTANT_INTEGER(target, SQLITE_RANGE, RANGE);
DEFINE_CONSTANT_INTEGER(target, SQLITE_NOTADB, NOTADB);
DEFINE_CONSTANT_INTEGER(exports, SQLITE_OK, OK)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_ERROR, ERROR)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_INTERNAL, INTERNAL)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_PERM, PERM)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_ABORT, ABORT)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_BUSY, BUSY)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_LOCKED, LOCKED)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOMEM, NOMEM)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_READONLY, READONLY)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_INTERRUPT, INTERRUPT)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_IOERR, IOERR)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_CORRUPT, CORRUPT)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOTFOUND, NOTFOUND)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_FULL, FULL)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_CANTOPEN, CANTOPEN)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_PROTOCOL, PROTOCOL)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_EMPTY, EMPTY)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_SCHEMA, SCHEMA)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_TOOBIG, TOOBIG)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_CONSTRAINT, CONSTRAINT)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_MISMATCH, MISMATCH)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_MISUSE, MISUSE)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOLFS, NOLFS)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_AUTH, AUTH)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_FORMAT, FORMAT)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_RANGE, RANGE)
DEFINE_CONSTANT_INTEGER(exports, SQLITE_NOTADB, NOTADB)
});
return exports;
}
}
......@@ -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
#define NODE_SQLITE3_SRC_STATEMENT_H
#include "database.h"
#include "threading.h"
#include <cstdlib>
#include <cstring>
#include <string>
......@@ -12,10 +8,13 @@
#include <vector>
#include <sqlite3.h>
#include <nan.h>
#include <napi.h>
#include <uv.h>
using namespace v8;
using namespace node;
#include "database.h"
#include "threading.h"
using namespace Napi;
namespace node_sqlite3 {
......@@ -71,23 +70,22 @@ typedef Row Parameters;
class Statement : public Nan::ObjectWrap {
class Statement : public Napi::ObjectWrap<Statement> {
public:
static Nan::Persistent<FunctionTemplate> constructor_template;
static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init);
static NAN_METHOD(New);
static Napi::Object Init(Napi::Env env, Napi::Object exports);
static Napi::Value New(const Napi::CallbackInfo& info);
struct Baton {
uv_work_t request;
napi_async_work request;
Statement* stmt;
Nan::Persistent<Function> callback;
Napi::FunctionReference callback;
Parameters parameters;
Baton(Statement* stmt_, Local<Function> cb_) : stmt(stmt_) {
Baton(Statement* stmt_, Napi::Function cb_) : stmt(stmt_) {
stmt->Ref();
request.data = this;
callback.Reset(cb_);
callback.Reset(cb_, 1);
}
virtual ~Baton() {
for (unsigned int i = 0; i < parameters.size(); i++) {
......@@ -100,20 +98,20 @@ public:
};
struct RowBaton : Baton {
RowBaton(Statement* stmt_, Local<Function> cb_) :
RowBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {}
Row row;
};
struct RunBaton : Baton {
RunBaton(Statement* stmt_, Local<Function> cb_) :
RunBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_), inserted_id(0), changes(0) {}
sqlite3_int64 inserted_id;
int changes;
};
struct RowsBaton : Baton {
RowsBaton(Statement* stmt_, Local<Function> cb_) :
RowsBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {}
Rows rows;
};
......@@ -121,10 +119,10 @@ public:
struct Async;
struct EachBaton : Baton {
Nan::Persistent<Function> completed;
Napi::FunctionReference completed;
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_) {}
virtual ~EachBaton() {
completed.Reset();
......@@ -134,7 +132,7 @@ public:
struct PrepareBaton : Database::Baton {
Statement* stmt;
std::string sql;
PrepareBaton(Database* db_, Local<Function> cb_, Statement* stmt_) :
PrepareBaton(Database* db_, Napi::Function cb_, Statement* stmt_) :
Baton(db_, cb_), stmt(stmt_) {
stmt->Ref();
}
......@@ -143,7 +141,7 @@ public:
if (!db->IsOpen() && db->IsLocked()) {
// The database handle was closed before the statement could be
// prepared.
stmt->Finalize();
stmt->Finalize_();
}
}
};
......@@ -166,15 +164,17 @@ public:
// Store the callbacks here because we don't have
// access to the baton in the async callback.
Nan::Persistent<Function> item_cb;
Nan::Persistent<Function> completed_cb;
Napi::FunctionReference item_cb;
Napi::FunctionReference completed_cb;
Async(Statement* st, uv_async_cb async_cb) :
stmt(st), completed(false), retrieved(0) {
watcher.data = this;
NODE_SQLITE3_MUTEX_INIT
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() {
......@@ -185,18 +185,20 @@ public:
}
};
Statement(Database* db_) : Nan::ObjectWrap(),
db(db_),
_handle(NULL),
status(SQLITE_OK),
prepared(false),
locked(true),
finalized(false) {
void init(Database* db_) {
db = db_;
_handle = NULL;
status = SQLITE_OK;
prepared = false;
locked = true;
finalized = false;
db->Ref();
}
Statement(const Napi::CallbackInfo& info);
~Statement() {
if (!finalized) Finalize();
if (!finalized) Finalize_();
}
WORK_DEFINITION(Bind);
......@@ -206,25 +208,25 @@ public:
WORK_DEFINITION(Each);
WORK_DEFINITION(Reset);
static NAN_METHOD(Finalize);
Napi::Value Finalize_(const Napi::CallbackInfo& info);
protected:
static void Work_BeginPrepare(Database::Baton* baton);
static void Work_Prepare(uv_work_t* req);
static void Work_AfterPrepare(uv_work_t* req);
static void Work_Prepare(napi_env env, void* data);
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 Finalize(Baton* baton);
void Finalize();
static void Finalize_(Baton* baton);
void Finalize_();
template <class T> inline Values::Field* BindParameter(const Local<Value> source, T pos);
template <class T> T* Bind(Nan::NAN_METHOD_ARGS_TYPE info, int start = 0, int end = -1);
template <class T> inline Values::Field* BindParameter(const Napi::Value source, T pos);
template <class T> T* Bind(const Napi::CallbackInfo& info, int start = 0, int end = -1);
bool Bind(const Parameters &parameters);
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 Process();
void CleanQueue();
......
......@@ -11,7 +11,7 @@ describe('query properties', function() {
it('should return the correct lastID', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?, ?)");
var j = 1;
for (var i = 0; i < 1000; i++) {
for (var i = 0; i < 5000; i++) {
stmt.run(i, "demo", function(err) {
if (err) throw err;
// Relies on SQLite's row numbering to be gapless and starting
......@@ -25,7 +25,7 @@ describe('query properties', function() {
it('should return the correct changes count', function(done) {
db.run("UPDATE foo SET id = id + 1 WHERE id % 2 = 0", function(err) {
if (err) throw err;
assert.equal(500, this.changes);
assert.equal(2500, this.changes);
done();
});
});
......
......@@ -10,11 +10,11 @@ describe('error handling', function() {
it('throw when calling Database() without new', function() {
assert.throws(function() {
sqlite3.Database(':memory:');
}, (/Use the new operator to create new Database objects/));
}, (/Class constructors cannot be invoked without 'new'/));
assert.throws(function() {
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) {
......
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