Unverified Commit 34d62838 by Kewde

Merge branch 'mohd-akram-napi'

parents 1752f424 cc4b598c
...@@ -25,3 +25,4 @@ local.env ...@@ -25,3 +25,4 @@ local.env
.mason .mason
.eslintrc.js .eslintrc.js
setup.sh setup.sh
/build-tmp-napi-v3
...@@ -21,7 +21,7 @@ matrix: ...@@ -21,7 +21,7 @@ matrix:
- os: linux - os: linux
dist: trusty dist: trusty
compiler: clang compiler: clang
env: NODE_VERSION="13" env: NODE_VERSION="14"
addons: addons:
apt: apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ] sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
...@@ -29,78 +29,32 @@ matrix: ...@@ -29,78 +29,32 @@ matrix:
- os: linux - os: linux
dist: trusty dist: trusty
compiler: clang compiler: clang
env: NODE_VERSION="12" env: NODE_VERSION="13"
addons: addons:
apt: apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ] sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
dist: trusty
compiler: clang compiler: clang
env: NODE_VERSION="11" env: NODE_VERSION="12"
addons: addons:
apt: apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ] sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="10" env: NODE_VERSION="11"
addons: addons:
apt: apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ] sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux
compiler: clang
env: NODE_VERSION="9"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="8"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="7"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="6"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="5"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="4"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
# test building against external sqlite
- os: linux
compiler: clang
env: NODE_VERSION="8" EXTERNAL_SQLITE=true PUBLISHABLE=false
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5','libsqlite3-dev']
# OS X # OS X
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="14" # node abi 83
- os: osx
compiler: clang
env: NODE_VERSION="13" # node abi 79 env: NODE_VERSION="13" # node abi 79
- os: osx - os: osx
compiler: clang compiler: clang
...@@ -108,31 +62,10 @@ matrix: ...@@ -108,31 +62,10 @@ matrix:
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="11" # node abi 67 env: NODE_VERSION="11" # node abi 67
- os: osx
compiler: clang
env: NODE_VERSION="10" # node abi 64
- os: osx
compiler: clang
env: NODE_VERSION="9" # node abi 59
- os: osx
compiler: clang
env: NODE_VERSION="8" # node abi 57
- os: osx
compiler: clang
env: NODE_VERSION="7" # node abi 51
- os: osx
compiler: clang
env: NODE_VERSION="6" # node abi 48
- os: osx
compiler: clang
env: NODE_VERSION="5" # node abi 47
- os: osx
compiler: clang
env: NODE_VERSION="4" # node abi 46
# electron Linux # electron Linux
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="8.2.0" env: NODE_VERSION="12" ELECTRON_VERSION="8.2.0"
dist: trusty dist: trusty
addons: addons:
apt: apt:
...@@ -140,7 +73,7 @@ matrix: ...@@ -140,7 +73,7 @@ matrix:
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="8.1.0" env: NODE_VERSION="12" ELECTRON_VERSION="8.1.0"
dist: trusty dist: trusty
addons: addons:
apt: apt:
...@@ -148,7 +81,7 @@ matrix: ...@@ -148,7 +81,7 @@ matrix:
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="8.0.0" env: NODE_VERSION="12" ELECTRON_VERSION="8.0.0"
dist: trusty dist: trusty
addons: addons:
apt: apt:
...@@ -156,7 +89,7 @@ matrix: ...@@ -156,7 +89,7 @@ matrix:
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="7.1.0" env: NODE_VERSION="12" ELECTRON_VERSION="7.1.0"
dist: trusty dist: trusty
addons: addons:
apt: apt:
...@@ -164,7 +97,7 @@ matrix: ...@@ -164,7 +97,7 @@ matrix:
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="7.0.0" env: NODE_VERSION="12" ELECTRON_VERSION="7.0.0"
dist: trusty dist: trusty
addons: addons:
apt: apt:
...@@ -172,7 +105,7 @@ matrix: ...@@ -172,7 +105,7 @@ matrix:
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="6.1.0" env: NODE_VERSION="12" ELECTRON_VERSION="6.1.0"
dist: trusty dist: trusty
addons: addons:
apt: apt:
...@@ -180,140 +113,34 @@ matrix: ...@@ -180,140 +113,34 @@ matrix:
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux - os: linux
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="6.0.0" env: NODE_VERSION="12" ELECTRON_VERSION="6.0.0"
dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise
addons: addons:
apt: apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ] sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev'] packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="5.0.0"
dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="4.2.0"
dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="4.1.0"
dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="4.0.0"
dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="3.0.6"
dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libstdc++-4.9-dev']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="2.0.1"
dist: trusty # needed for libc6 / 'version `GLIBC_2.17` not found' error on precise
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5', 'libc6']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.8.4"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.7.12"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.6.2"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
- os: linux
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.3.14"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5', 'gcc-multilib', 'g++-multilib', 'libsqlite3-dev:i386' ]
packages: [ 'clang-3.5']
# electron MacOs # electron MacOs
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="8.2.0" env: NODE_VERSION="12" ELECTRON_VERSION="8.2.0"
- os: osx
compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="8.1.0"
- os: osx
compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="8.0.0"
- os: osx
compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="7.1.0"
- os: osx
compiler: clang
env: NODE_VERSION="8" ELECTRON_VERSION="7.0.0"
- os: osx
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="6.1.0"
- os: osx
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="6.0.0"
- os: osx
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="5.0.0"
- os: osx
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="4.2.0"
- os: osx
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="4.1.0"
- os: osx
compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="4.0.0"
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="3.0.6" env: NODE_VERSION="12" ELECTRON_VERSION="8.1.0"
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="2.0.1" env: NODE_VERSION="12" ELECTRON_VERSION="8.0.0"
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.8.4" env: NODE_VERSION="12" ELECTRON_VERSION="7.1.0"
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.7.12" env: NODE_VERSION="12" ELECTRON_VERSION="7.0.0"
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.6.2" env: NODE_VERSION="12" ELECTRON_VERSION="6.1.0"
- os: osx - os: osx
compiler: clang compiler: clang
env: NODE_VERSION="6" ELECTRON_VERSION="1.3.14" env: NODE_VERSION="12" ELECTRON_VERSION="6.0.0"
env: env:
global: global:
......
...@@ -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);
} }
......
#include <string.h> #include <string.h>
#include <node.h> #include <napi.h>
#include <node_buffer.h>
#include <node_version.h>
#include "macros.h" #include "macros.h"
#include "database.h" #include "database.h"
...@@ -9,31 +7,28 @@ ...@@ -9,31 +7,28 @@
using namespace node_sqlite3; using namespace node_sqlite3;
Nan::Persistent<FunctionTemplate> Backup::constructor_template; Napi::FunctionReference Backup::constructor;
NAN_MODULE_INIT(Backup::Init) { Napi::Object Backup::Init(Napi::Env env, Napi::Object exports) {
Nan::HandleScope scope; Napi::HandleScope scope(env);
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New); Napi::Function t = DefineClass(env, "Backup", {
InstanceMethod("step", &Backup::Step),
InstanceMethod("finish", &Backup::Finish),
InstanceAccessor("idle", &Backup::IdleGetter, nullptr),
InstanceAccessor("completed", &Backup::CompletedGetter, nullptr),
InstanceAccessor("failed", &Backup::FailedGetter, nullptr),
InstanceAccessor("remaining", &Backup::RemainingGetter, nullptr),
InstanceAccessor("pageCount", &Backup::PageCountGetter, nullptr),
InstanceAccessor("retryErrors", &Backup::RetryErrorGetter, &Backup::RetryErrorSetter),
});
t->InstanceTemplate()->SetInternalFieldCount(1); constructor = Napi::Persistent(t);
t->SetClassName(Nan::New("Backup").ToLocalChecked()); constructor.SuppressDestruct();
Nan::SetPrototypeMethod(t, "step", Step); exports.Set("Backup", t);
Nan::SetPrototypeMethod(t, "finish", Finish); return exports;
NODE_SET_GETTER(t, "idle", IdleGetter);
NODE_SET_GETTER(t, "completed", CompletedGetter);
NODE_SET_GETTER(t, "failed", FailedGetter);
NODE_SET_GETTER(t, "remaining", RemainingGetter);
NODE_SET_GETTER(t, "pageCount", PageCountGetter);
NODE_SET_SETTER(t, "retryErrors", RetryErrorGetter, RetryErrorSetter);
constructor_template.Reset(t);
Nan::Set(target, Nan::New("Backup").ToLocalChecked(),
Nan::GetFunction(t).ToLocalChecked());
} }
void Backup::Process() { void Backup::Process() {
...@@ -64,33 +59,35 @@ void Backup::Schedule(Work_Callback callback, Baton* baton) { ...@@ -64,33 +59,35 @@ void Backup::Schedule(Work_Callback callback, Baton* baton) {
} }
template <class T> void Backup::Error(T* baton) { template <class T> void Backup::Error(T* baton) {
Nan::HandleScope scope; Napi::Env env = baton->backup->Env();
Napi::HandleScope scope(env);
Backup* backup = baton->backup; Backup* backup = baton->backup;
// Fail hard on logic errors. // Fail hard on logic errors.
assert(backup->status != 0); assert(backup->status != 0);
EXCEPTION(backup->message, backup->status, exception); EXCEPTION(Napi::String::New(env, backup->message), backup->status, exception);
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsEmpty() && cb.IsFunction()) {
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
TRY_CATCH_CALL(backup->handle(), cb, 1, argv); TRY_CATCH_CALL(backup->Value(), cb, 1, argv);
} }
else { else {
Local<Value> argv[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value argv[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(backup->handle(), 2, argv); EMIT_EVENT(backup->Value(), 2, argv);
} }
} }
void Backup::CleanQueue() { void Backup::CleanQueue() {
Nan::HandleScope scope; Napi::Env env = this->Env();
Napi::HandleScope scope(env);
if (inited && !queue.empty()) { if (inited && !queue.empty()) {
// This backup has already been initialized and is now finished. // This backup has already been initialized and is now finished.
// Fire error for all remaining items in the queue. // Fire error for all remaining items in the queue.
EXCEPTION("Backup is already finished", SQLITE_MISUSE, exception); EXCEPTION(Napi::String::New(env, "Backup is already finished"), SQLITE_MISUSE, exception);
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
bool called = false; bool called = false;
// Clear out the queue so that this object can get GC'ed. // Clear out the queue so that this object can get GC'ed.
...@@ -98,11 +95,11 @@ void Backup::CleanQueue() { ...@@ -98,11 +95,11 @@ void Backup::CleanQueue() {
Call* call = queue.front(); Call* call = queue.front();
queue.pop(); queue.pop();
Local<Function> cb = Nan::New(call->baton->callback); Napi::Function cb = call->baton->callback.Value();
if (inited && !cb.IsEmpty() && if (inited && !cb.IsEmpty() &&
cb->IsFunction()) { cb.IsFunction()) {
TRY_CATCH_CALL(handle(), cb, 1, argv); TRY_CATCH_CALL(Value(), cb, 1, argv);
called = true; called = true;
} }
...@@ -115,8 +112,8 @@ void Backup::CleanQueue() { ...@@ -115,8 +112,8 @@ void Backup::CleanQueue() {
// When we couldn't call a callback function, emit an error on the // When we couldn't call a callback function, emit an error on the
// Backup object. // Backup object.
if (!called) { if (!called) {
Local<Value> info[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value info[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(handle(), 2, info); EMIT_EVENT(Value(), 2, info);
} }
} }
else while (!queue.empty()) { else while (!queue.empty()) {
...@@ -132,65 +129,74 @@ void Backup::CleanQueue() { ...@@ -132,65 +129,74 @@ void Backup::CleanQueue() {
} }
} }
NAN_METHOD(Backup::New) { Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info) {
Napi::Env env = info.Env();
if (!info.IsConstructCall()) { if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Use the new operator to create new Backup objects"); Napi::TypeError::New(env, "Use the new operator to create new Backup objects").ThrowAsJavaScriptException();
return;
} }
int length = info.Length(); int length = info.Length();
if (length <= 0 || !Database::HasInstance(info[0])) { if (length <= 0 || !Database::HasInstance(info[0])) {
return Nan::ThrowTypeError("Database object expected"); Napi::TypeError::New(env, "Database object expected").ThrowAsJavaScriptException();
return;
} }
else if (length <= 1 || !info[1]->IsString()) { else if (length <= 1 || !info[1].IsString()) {
return Nan::ThrowTypeError("Filename expected"); Napi::TypeError::New(env, "Filename expected").ThrowAsJavaScriptException();
return;
} }
else if (length <= 2 || !info[2]->IsString()) { else if (length <= 2 || !info[2].IsString()) {
return Nan::ThrowTypeError("Source database name expected"); Napi::TypeError::New(env, "Source database name expected").ThrowAsJavaScriptException();
return;
} }
else if (length <= 3 || !info[3]->IsString()) { else if (length <= 3 || !info[3].IsString()) {
return Nan::ThrowTypeError("Destination database name expected"); Napi::TypeError::New(env, "Destination database name expected").ThrowAsJavaScriptException();
return;
} }
else if (length <= 4 || !info[4]->IsBoolean()) { else if (length <= 4 || !info[4].IsBoolean()) {
return Nan::ThrowTypeError("Direction flag expected"); Napi::TypeError::New(env, "Direction flag expected").ThrowAsJavaScriptException();
return;
} }
else if (length > 5 && !info[5]->IsUndefined() && !info[5]->IsFunction()) { else if (length > 5 && !info[5].IsUndefined() && !info[5].IsFunction()) {
return Nan::ThrowTypeError("Callback expected"); Napi::TypeError::New(env, "Callback expected").ThrowAsJavaScriptException();
return;
} }
Database* db = Nan::ObjectWrap::Unwrap<Database>(info[0].As<Object>()); Database* db = Napi::ObjectWrap<Database>::Unwrap(info[0].As<Napi::Object>());
Local<String> filename = Local<String>::Cast(info[1]); Napi::String filename = info[1].As<Napi::String>();
Local<String> sourceName = Local<String>::Cast(info[2]); Napi::String sourceName = info[2].As<Napi::String>();
Local<String> destName = Local<String>::Cast(info[3]); Napi::String destName = info[3].As<Napi::String>();
Local<Boolean> filenameIsDest = Local<Boolean>::Cast(info[4]); Napi::Boolean filenameIsDest = info[4].As<Napi::Boolean>();
Nan::ForceSet(info.This(), Nan::New("filename").ToLocalChecked(), filename, ReadOnly); info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("filename", filename));
Nan::ForceSet(info.This(), Nan::New("sourceName").ToLocalChecked(), sourceName, ReadOnly); info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("sourceName", sourceName));
Nan::ForceSet(info.This(), Nan::New("destName").ToLocalChecked(), destName, ReadOnly); info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("destName", destName));
Nan::ForceSet(info.This(), Nan::New("filenameIsDest").ToLocalChecked(), filenameIsDest, ReadOnly); info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("filenameIsDest", filenameIsDest));
Backup* backup = new Backup(db); init(db);
backup->Wrap(info.This());
InitializeBaton* baton = new InitializeBaton(db, Local<Function>::Cast(info[5]), backup); InitializeBaton* baton = new InitializeBaton(db, info[5].As<Napi::Function>(), this);
baton->filename = std::string(*Nan::Utf8String(filename)); baton->filename = filename.Utf8Value();
baton->sourceName = std::string(*Nan::Utf8String(sourceName)); baton->sourceName = sourceName.Utf8Value();
baton->destName = std::string(*Nan::Utf8String(destName)); baton->destName = destName.Utf8Value();
baton->filenameIsDest = Nan::To<bool>(filenameIsDest).FromJust(); baton->filenameIsDest = filenameIsDest.Value();
db->Schedule(Work_BeginInitialize, baton); db->Schedule(Work_BeginInitialize, baton);
info.GetReturnValue().Set(info.This());
} }
void Backup::Work_BeginInitialize(Database::Baton* baton) { void Backup::Work_BeginInitialize(Database::Baton* baton) {
assert(baton->db->open); assert(baton->db->open);
baton->db->pending++; baton->db->pending++;
int status = uv_queue_work(uv_default_loop(), Napi::Env env = baton->db->Env();
&baton->request, Work_Initialize, (uv_after_work_cb)Work_AfterInitialize); int status = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Backup.Initialize"),
Work_Initialize, Work_AfterInitialize, baton, &baton->request
);
assert(status == 0); assert(status == 0);
napi_queue_async_work(env, baton->request);
} }
void Backup::Work_Initialize(uv_work_t* req) { void Backup::Work_Initialize(napi_env e, void* data) {
BACKUP_INIT(InitializeBaton); BACKUP_INIT(InitializeBaton);
// In case stepping fails, we use a mutex to make sure we get the associated // In case stepping fails, we use a mutex to make sure we get the associated
...@@ -219,28 +225,30 @@ void Backup::Work_Initialize(uv_work_t* req) { ...@@ -219,28 +225,30 @@ void Backup::Work_Initialize(uv_work_t* req) {
sqlite3_mutex_leave(mtx); sqlite3_mutex_leave(mtx);
} }
void Backup::Work_AfterInitialize(uv_work_t* req) { void Backup::Work_AfterInitialize(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
BACKUP_INIT(InitializeBaton); BACKUP_INIT(InitializeBaton);
Napi::Env env = backup->Env();
Napi::HandleScope scope(env);
if (backup->status != SQLITE_OK) { if (backup->status != SQLITE_OK) {
Error(baton); Error(baton);
backup->FinishAll(); backup->FinishAll();
} }
else { else {
backup->inited = true; backup->inited = true;
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsEmpty() && cb.IsFunction()) {
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(backup->handle(), cb, 1, argv); TRY_CATCH_CALL(backup->Value(), cb, 1, argv);
} }
} }
BACKUP_END(); BACKUP_END();
} }
NAN_METHOD(Backup::Step) { Napi::Value Backup::Step(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
Napi::Env env = backup->Env();
REQUIRE_ARGUMENT_INTEGER(0, pages); REQUIRE_ARGUMENT_INTEGER(0, pages);
OPTIONAL_ARGUMENT_FUNCTION(1, callback); OPTIONAL_ARGUMENT_FUNCTION(1, callback);
...@@ -248,14 +256,14 @@ NAN_METHOD(Backup::Step) { ...@@ -248,14 +256,14 @@ NAN_METHOD(Backup::Step) {
StepBaton* baton = new StepBaton(backup, callback, pages); StepBaton* baton = new StepBaton(backup, callback, pages);
backup->GetRetryErrors(baton->retryErrorsSet); backup->GetRetryErrors(baton->retryErrorsSet);
backup->Schedule(Work_BeginStep, baton); backup->Schedule(Work_BeginStep, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Backup::Work_BeginStep(Baton* baton) { void Backup::Work_BeginStep(Baton* baton) {
BACKUP_BEGIN(Step); BACKUP_BEGIN(Step);
} }
void Backup::Work_Step(uv_work_t* req) { void Backup::Work_Step(napi_env e, void* data) {
BACKUP_INIT(StepBaton); BACKUP_INIT(StepBaton);
if (backup->_handle) { if (backup->_handle) {
backup->status = sqlite3_backup_step(backup->_handle, baton->pages); backup->status = sqlite3_backup_step(backup->_handle, baton->pages);
...@@ -279,11 +287,12 @@ void Backup::Work_Step(uv_work_t* req) { ...@@ -279,11 +287,12 @@ void Backup::Work_Step(uv_work_t* req) {
} }
} }
void Backup::Work_AfterStep(uv_work_t* req) { void Backup::Work_AfterStep(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
BACKUP_INIT(StepBaton); BACKUP_INIT(StepBaton);
Napi::Env env = backup->Env();
Napi::HandleScope scope(env);
if (backup->status == SQLITE_DONE) { if (backup->status == SQLITE_DONE) {
backup->completed = true; backup->completed = true;
} else if (!backup->_handle) { } else if (!backup->_handle) {
...@@ -295,45 +304,48 @@ void Backup::Work_AfterStep(uv_work_t* req) { ...@@ -295,45 +304,48 @@ void Backup::Work_AfterStep(uv_work_t* req) {
} }
else { else {
// Fire callbacks. // Fire callbacks.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsEmpty() && cb.IsFunction()) {
Local<Value> argv[] = { Nan::Null(), Nan::New(backup->status == SQLITE_DONE) }; Napi::Value argv[] = { env.Null(), Napi::Boolean::New(env, backup->status == SQLITE_DONE) };
TRY_CATCH_CALL(backup->handle(), cb, 2, argv); TRY_CATCH_CALL(backup->Value(), cb, 2, argv);
} }
} }
BACKUP_END(); BACKUP_END();
} }
NAN_METHOD(Backup::Finish) { Napi::Value Backup::Finish(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
Napi::Env env = backup->Env();
OPTIONAL_ARGUMENT_FUNCTION(0, callback); OPTIONAL_ARGUMENT_FUNCTION(0, callback);
Baton* baton = new Baton(backup, callback); Baton* baton = new Baton(backup, callback);
backup->Schedule(Work_BeginFinish, baton); backup->Schedule(Work_BeginFinish, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Backup::Work_BeginFinish(Baton* baton) { void Backup::Work_BeginFinish(Baton* baton) {
BACKUP_BEGIN(Finish); BACKUP_BEGIN(Finish);
} }
void Backup::Work_Finish(uv_work_t* req) { void Backup::Work_Finish(napi_env e, void* data) {
BACKUP_INIT(Baton); BACKUP_INIT(Baton);
backup->FinishSqlite(); backup->FinishSqlite();
} }
void Backup::Work_AfterFinish(uv_work_t* req) { void Backup::Work_AfterFinish(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
BACKUP_INIT(Baton); BACKUP_INIT(Baton);
Napi::Env env = backup->Env();
Napi::HandleScope scope(env);
backup->FinishAll(); backup->FinishAll();
// Fire callback in case there was one. // Fire callback in case there was one.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsEmpty() && cb.IsFunction()) {
TRY_CATCH_CALL(backup->handle(), cb, 0, NULL); TRY_CATCH_CALL(backup->Value(), cb, 0, NULL);
} }
BACKUP_END(); BACKUP_END();
...@@ -362,55 +374,56 @@ void Backup::FinishSqlite() { ...@@ -362,55 +374,56 @@ void Backup::FinishSqlite() {
_destDb = NULL; _destDb = NULL;
} }
NAN_GETTER(Backup::IdleGetter) { Napi::Value Backup::IdleGetter(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
bool idle = backup->inited && !backup->locked && backup->queue.empty(); bool idle = backup->inited && !backup->locked && backup->queue.empty();
info.GetReturnValue().Set(idle); return Napi::Boolean::New(this->Env(), idle);
} }
NAN_GETTER(Backup::CompletedGetter) { Napi::Value Backup::CompletedGetter(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
info.GetReturnValue().Set(backup->completed); return Napi::Boolean::New(this->Env(), backup->completed);
} }
NAN_GETTER(Backup::FailedGetter) { Napi::Value Backup::FailedGetter(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
info.GetReturnValue().Set(backup->failed); return Napi::Boolean::New(this->Env(), backup->failed);
} }
NAN_GETTER(Backup::RemainingGetter) { Napi::Value Backup::RemainingGetter(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
info.GetReturnValue().Set(backup->remaining); return Napi::Number::New(this->Env(), backup->remaining);
} }
NAN_GETTER(Backup::PageCountGetter) { Napi::Value Backup::PageCountGetter(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
info.GetReturnValue().Set(backup->pageCount); return Napi::Number::New(this->Env(), backup->pageCount);
} }
NAN_GETTER(Backup::RetryErrorGetter) { Napi::Value Backup::RetryErrorGetter(const Napi::CallbackInfo& info) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
info.GetReturnValue().Set(Nan::New(backup->retryErrors)); return backup->retryErrors.Value();
} }
NAN_SETTER(Backup::RetryErrorSetter) { void Backup::RetryErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value) {
Backup* backup = Nan::ObjectWrap::Unwrap<Backup>(info.This()); Backup* backup = this;
if (!value->IsArray()) { Napi::Env env = backup->Env();
return Nan::ThrowError("retryErrors must be an array"); if (!value.IsArray()) {
Napi::Error::New(env, "retryErrors must be an array").ThrowAsJavaScriptException();
return;
} }
Local<Array> array = Local<Array>::Cast(value); Napi::Array array = value.As<Napi::Array>();
backup->retryErrors.Reset(array); backup->retryErrors.Reset(array, 1);
} }
void Backup::GetRetryErrors(std::set<int>& retryErrorsSet) { void Backup::GetRetryErrors(std::set<int>& retryErrorsSet) {
retryErrorsSet.clear(); retryErrorsSet.clear();
Local<Array> array = Nan::New(retryErrors); Napi::Array array = retryErrors.Value();
int length = array->Length(); int length = array.Length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Local<Value> code = Nan::Get(array, i).ToLocalChecked(); Napi::Value code = (array).Get(i);
if (code->IsInt32()) { if (code.IsNumber()) {
retryErrorsSet.insert(Nan::To<int32_t>(code).FromJust()); retryErrorsSet.insert(code.As<Napi::Number>().Int32Value());
} }
} }
} }
...@@ -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;
}; };
} }
......
...@@ -6,47 +6,45 @@ ...@@ -6,47 +6,45 @@
using namespace node_sqlite3; using namespace node_sqlite3;
Nan::Persistent<FunctionTemplate> Database::constructor_template; Napi::FunctionReference Database::constructor;
NAN_MODULE_INIT(Database::Init) { Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
Nan::HandleScope scope; Napi::HandleScope scope(env);
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New); Napi::Function t = DefineClass(env, "Database", {
InstanceMethod("close", &Database::Close),
InstanceMethod("exec", &Database::Exec),
InstanceMethod("wait", &Database::Wait),
InstanceMethod("loadExtension", &Database::LoadExtension),
InstanceMethod("serialize", &Database::Serialize),
InstanceMethod("parallelize", &Database::Parallelize),
InstanceMethod("configure", &Database::Configure),
InstanceMethod("interrupt", &Database::Interrupt),
InstanceAccessor("open", &Database::OpenGetter, nullptr)
});
t->InstanceTemplate()->SetInternalFieldCount(1); constructor = Napi::Persistent(t);
t->SetClassName(Nan::New("Database").ToLocalChecked()); constructor.SuppressDestruct();
Nan::SetPrototypeMethod(t, "close", Close); exports.Set("Database", t);
Nan::SetPrototypeMethod(t, "exec", Exec); return exports;
Nan::SetPrototypeMethod(t, "wait", Wait);
Nan::SetPrototypeMethod(t, "loadExtension", LoadExtension);
Nan::SetPrototypeMethod(t, "serialize", Serialize);
Nan::SetPrototypeMethod(t, "parallelize", Parallelize);
Nan::SetPrototypeMethod(t, "configure", Configure);
Nan::SetPrototypeMethod(t, "interrupt", Interrupt);
NODE_SET_GETTER(t, "open", OpenGetter);
constructor_template.Reset(t);
Nan::Set(target, Nan::New("Database").ToLocalChecked(),
Nan::GetFunction(t).ToLocalChecked());
} }
void Database::Process() { void Database::Process() {
Nan::HandleScope scope; Napi::Env env = this->Env();
Napi::HandleScope scope(env);
if (!open && locked && !queue.empty()) { if (!open && locked && !queue.empty()) {
EXCEPTION("Database handle is closed", SQLITE_MISUSE, exception); EXCEPTION(Napi::String::New(env, "Database handle is closed"), SQLITE_MISUSE, exception);
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
bool called = false; bool called = false;
// Call all callbacks with the error object. // Call all callbacks with the error object.
while (!queue.empty()) { while (!queue.empty()) {
Call* call = queue.front(); Call* call = queue.front();
Local<Function> cb = Nan::New(call->baton->callback); Napi::Function cb = call->baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
TRY_CATCH_CALL(this->handle(), cb, 1, argv); TRY_CATCH_CALL(this->Value(), cb, 1, argv);
called = true; called = true;
} }
queue.pop(); queue.pop();
...@@ -59,8 +57,8 @@ void Database::Process() { ...@@ -59,8 +57,8 @@ void Database::Process() {
// When we couldn't call a callback function, emit an error on the // When we couldn't call a callback function, emit an error on the
// Database object. // Database object.
if (!called) { if (!called) {
Local<Value> info[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value info[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(handle(), 2, info); EMIT_EVENT(Value(), 2, info);
} }
return; return;
} }
...@@ -82,18 +80,19 @@ void Database::Process() { ...@@ -82,18 +80,19 @@ void Database::Process() {
} }
void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) { void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) {
Nan::HandleScope scope; Napi::Env env = this->Env();
Napi::HandleScope scope(env);
if (!open && locked) { if (!open && locked) {
EXCEPTION("Database is closed", SQLITE_MISUSE, exception); EXCEPTION(Napi::String::New(env, "Database is closed"), SQLITE_MISUSE, exception);
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
TRY_CATCH_CALL(handle(), cb, 1, argv); TRY_CATCH_CALL(Value(), cb, 1, argv);
} }
else { else {
Local<Value> argv[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value argv[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(handle(), 2, argv); EMIT_EVENT(Value(), 2, argv);
} }
return; return;
} }
...@@ -107,47 +106,51 @@ void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) { ...@@ -107,47 +106,51 @@ void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) {
} }
} }
NAN_METHOD(Database::New) { Database::Database(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Database>(info) {
if (!info.IsConstructCall()) { init();
return Nan::ThrowTypeError("Use the new operator to create new Database objects"); Napi::Env env = info.Env();
if (info.Length() <= 0 || !info[0].IsString()) {
Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException();
return;
} }
std::string filename = info[0].As<Napi::String>();
REQUIRE_ARGUMENT_STRING(0, filename); unsigned int pos = 1;
int pos = 1;
int mode; int mode;
if (info.Length() >= pos && info[pos]->IsInt32()) { if (info.Length() >= pos && info[pos].IsNumber() && OtherIsInt(info[pos].As<Napi::Number>())) {
mode = Nan::To<int>(info[pos++]).FromJust(); mode = info[pos++].As<Napi::Number>().Int32Value();
} else { }
else {
mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
} }
Local<Function> callback; Napi::Function callback;
if (info.Length() >= pos && info[pos]->IsFunction()) { if (info.Length() >= pos && info[pos].IsFunction()) {
callback = Local<Function>::Cast(info[pos++]); callback = info[pos++].As<Napi::Function>();
} }
Database* db = new Database(); info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("filename", info[0].As<Napi::String>(), napi_default));
db->Wrap(info.This()); info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("mode", Napi::Number::New(env, mode), napi_default));
Nan::ForceSet(info.This(), Nan::New("filename").ToLocalChecked(), info[0].As<String>(), ReadOnly);
Nan::ForceSet(info.This(), Nan::New("mode").ToLocalChecked(), Nan::New(mode), ReadOnly);
// Start opening the database. // Start opening the database.
OpenBaton* baton = new OpenBaton(db, callback, *filename, mode); OpenBaton* baton = new OpenBaton(this, callback, filename.c_str(), mode);
Work_BeginOpen(baton); Work_BeginOpen(baton);
info.GetReturnValue().Set(info.This());
} }
void Database::Work_BeginOpen(Baton* baton) { void Database::Work_BeginOpen(Baton* baton) {
int status = uv_queue_work(uv_default_loop(), Napi::Env env = baton->db->Env();
&baton->request, Work_Open, (uv_after_work_cb)Work_AfterOpen); int status = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.Open"),
Work_Open, Work_AfterOpen, baton, &baton->request
);
assert(status == 0); assert(status == 0);
napi_queue_async_work(env, baton->request);
} }
void Database::Work_Open(uv_work_t* req) { void Database::Work_Open(napi_env e, void* data) {
OpenBaton* baton = static_cast<OpenBaton*>(req->data); OpenBaton* baton = static_cast<OpenBaton*>(data);
Database* db = baton->db; Database* db = baton->db;
baton->status = sqlite3_open_v2( baton->status = sqlite3_open_v2(
...@@ -168,54 +171,59 @@ void Database::Work_Open(uv_work_t* req) { ...@@ -168,54 +171,59 @@ void Database::Work_Open(uv_work_t* req) {
} }
} }
void Database::Work_AfterOpen(uv_work_t* req) { void Database::Work_AfterOpen(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope; OpenBaton* baton = static_cast<OpenBaton*>(data);
OpenBaton* baton = static_cast<OpenBaton*>(req->data);
Database* db = baton->db; Database* db = baton->db;
Local<Value> argv[1]; Napi::Env env = db->Env();
Napi::HandleScope scope(env);
Napi::Value argv[1];
if (baton->status != SQLITE_OK) { if (baton->status != SQLITE_OK) {
EXCEPTION(baton->message, baton->status, exception); EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
argv[0] = exception; argv[0] = exception;
} }
else { else {
db->open = true; db->open = true;
argv[0] = Nan::Null(); argv[0] = env.Null();
} }
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
TRY_CATCH_CALL(db->handle(), cb, 1, argv); TRY_CATCH_CALL(db->Value(), cb, 1, argv);
} }
else if (!db->open) { else if (!db->open) {
Local<Value> info[] = { Nan::New("error").ToLocalChecked(), argv[0] }; Napi::Value info[] = { Napi::String::New(env, "error"), argv[0] };
EMIT_EVENT(db->handle(), 2, info); EMIT_EVENT(db->Value(), 2, info);
} }
if (db->open) { if (db->open) {
Local<Value> info[] = { Nan::New("open").ToLocalChecked() }; Napi::Value info[] = { Napi::String::New(env, "open") };
EMIT_EVENT(db->handle(), 1, info); EMIT_EVENT(db->Value(), 1, info);
db->Process(); db->Process();
} }
napi_delete_async_work(e, baton->request);
delete baton; delete baton;
} }
NAN_GETTER(Database::OpenGetter) { Napi::Value Database::OpenGetter(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = this->Env();
info.GetReturnValue().Set(db->open); Database* db = this;
return Napi::Boolean::New(env, db->open);
} }
NAN_METHOD(Database::Close) { Napi::Value Database::Close(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = info.Env();
Database* db = this;
OPTIONAL_ARGUMENT_FUNCTION(0, callback); OPTIONAL_ARGUMENT_FUNCTION(0, callback);
Baton* baton = new Baton(db, callback); Baton* baton = new Baton(db, callback);
db->Schedule(Work_BeginClose, baton, true); db->Schedule(Work_BeginClose, baton, true);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Database::Work_BeginClose(Baton* baton) { void Database::Work_BeginClose(Baton* baton) {
...@@ -227,13 +235,18 @@ void Database::Work_BeginClose(Baton* baton) { ...@@ -227,13 +235,18 @@ void Database::Work_BeginClose(Baton* baton) {
baton->db->RemoveCallbacks(); baton->db->RemoveCallbacks();
baton->db->closing = true; baton->db->closing = true;
int status = uv_queue_work(uv_default_loop(), Napi::Env env = baton->db->Env();
&baton->request, Work_Close, (uv_after_work_cb)Work_AfterClose);
int status = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.Close"),
Work_Close, Work_AfterClose, baton, &baton->request
);
assert(status == 0); assert(status == 0);
napi_queue_async_work(env, baton->request);
} }
void Database::Work_Close(uv_work_t* req) { void Database::Work_Close(napi_env e, void* data) {
Baton* baton = static_cast<Baton*>(req->data); Baton* baton = static_cast<Baton*>(data);
Database* db = baton->db; Database* db = baton->db;
baton->status = sqlite3_close(db->_handle); baton->status = sqlite3_close(db->_handle);
...@@ -246,132 +259,143 @@ void Database::Work_Close(uv_work_t* req) { ...@@ -246,132 +259,143 @@ void Database::Work_Close(uv_work_t* req) {
} }
} }
void Database::Work_AfterClose(uv_work_t* req) { void Database::Work_AfterClose(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope; Baton* baton = static_cast<Baton*>(data);
Baton* baton = static_cast<Baton*>(req->data);
Database* db = baton->db; Database* db = baton->db;
Napi::Env env = db->Env();
Napi::HandleScope scope(env);
db->closing = false; db->closing = false;
Local<Value> argv[1]; Napi::Value argv[1];
if (baton->status != SQLITE_OK) { if (baton->status != SQLITE_OK) {
EXCEPTION(baton->message, baton->status, exception); EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
argv[0] = exception; argv[0] = exception;
} }
else { else {
db->open = false; db->open = false;
// Leave db->locked to indicate that this db object has reached // Leave db->locked to indicate that this db object has reached
// the end of its life. // the end of its life.
argv[0] = Nan::Null(); argv[0] = env.Null();
} }
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
// Fire callbacks. // Fire callbacks.
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
TRY_CATCH_CALL(db->handle(), cb, 1, argv); TRY_CATCH_CALL(db->Value(), cb, 1, argv);
} }
else if (db->open) { else if (db->open) {
Local<Value> info[] = { Nan::New("error").ToLocalChecked(), argv[0] }; Napi::Value info[] = { Napi::String::New(env, "error"), argv[0] };
EMIT_EVENT(db->handle(), 2, info); EMIT_EVENT(db->Value(), 2, info);
} }
if (!db->open) { if (!db->open) {
Local<Value> info[] = { Nan::New("close").ToLocalChecked(), argv[0] }; Napi::Value info[] = { Napi::String::New(env, "close"), argv[0] };
EMIT_EVENT(db->handle(), 1, info); EMIT_EVENT(db->Value(), 1, info);
db->Process(); db->Process();
} }
napi_delete_async_work(e, baton->request);
delete baton; delete baton;
} }
NAN_METHOD(Database::Serialize) { Napi::Value Database::Serialize(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = this->Env();
Database* db = this;
OPTIONAL_ARGUMENT_FUNCTION(0, callback); OPTIONAL_ARGUMENT_FUNCTION(0, callback);
bool before = db->serialize; bool before = db->serialize;
db->serialize = true; db->serialize = true;
if (!callback.IsEmpty() && callback->IsFunction()) { if (!callback.IsEmpty() && callback.IsFunction()) {
TRY_CATCH_CALL(info.This(), callback, 0, NULL); TRY_CATCH_CALL(info.This(), callback, 0, NULL);
db->serialize = before; db->serialize = before;
} }
db->Process(); db->Process();
info.GetReturnValue().Set(info.This()); return info.This();
} }
NAN_METHOD(Database::Parallelize) { Napi::Value Database::Parallelize(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = this->Env();
Database* db = this;
OPTIONAL_ARGUMENT_FUNCTION(0, callback); OPTIONAL_ARGUMENT_FUNCTION(0, callback);
bool before = db->serialize; bool before = db->serialize;
db->serialize = false; db->serialize = false;
if (!callback.IsEmpty() && callback->IsFunction()) { if (!callback.IsEmpty() && callback.IsFunction()) {
TRY_CATCH_CALL(info.This(), callback, 0, NULL); TRY_CATCH_CALL(info.This(), callback, 0, NULL);
db->serialize = before; db->serialize = before;
} }
db->Process(); db->Process();
info.GetReturnValue().Set(info.This()); return info.This();
} }
NAN_METHOD(Database::Configure) { Napi::Value Database::Configure(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = this->Env();
Database* db = this;
REQUIRE_ARGUMENTS(2); REQUIRE_ARGUMENTS(2);
if (Nan::Equals(info[0], Nan::New("trace").ToLocalChecked()).FromJust()) { if (info[0].StrictEquals( Napi::String::New(env, "trace"))) {
Local<Function> handle; Napi::Function handle;
Baton* baton = new Baton(db, handle); Baton* baton = new Baton(db, handle);
db->Schedule(RegisterTraceCallback, baton); db->Schedule(RegisterTraceCallback, baton);
} }
else if (Nan::Equals(info[0], Nan::New("profile").ToLocalChecked()).FromJust()) { else if (info[0].StrictEquals( Napi::String::New(env, "profile"))) {
Local<Function> handle; Napi::Function handle;
Baton* baton = new Baton(db, handle); Baton* baton = new Baton(db, handle);
db->Schedule(RegisterProfileCallback, baton); db->Schedule(RegisterProfileCallback, baton);
} }
else if (Nan::Equals(info[0], Nan::New("busyTimeout").ToLocalChecked()).FromJust()) { else if (info[0].StrictEquals( Napi::String::New(env, "busyTimeout"))) {
if (!info[1]->IsInt32()) { if (!info[1].IsNumber()) {
return Nan::ThrowTypeError("Value must be an integer"); Napi::TypeError::New(env, "Value must be an integer").ThrowAsJavaScriptException();
return env.Null();
} }
Local<Function> handle; Napi::Function handle;
Baton* baton = new Baton(db, handle); Baton* baton = new Baton(db, handle);
baton->status = Nan::To<int>(info[1]).FromJust(); baton->status = info[1].As<Napi::Number>().Int32Value();
db->Schedule(SetBusyTimeout, baton); db->Schedule(SetBusyTimeout, baton);
} }
else { else {
return Nan::ThrowError(Exception::Error(String::Concat( Napi::TypeError::New(env, (StringConcat(
#if V8_MAJOR_VERSION > 6 #if V8_MAJOR_VERSION > 6
info.GetIsolate(), info.GetIsolate(),
#endif #endif
Nan::To<String>(info[0]).ToLocalChecked(), info[0].As<Napi::String>(),
Nan::New(" is not a valid configuration option").ToLocalChecked() Napi::String::New(env, " is not a valid configuration option")
))); )).Utf8Value().c_str()).ThrowAsJavaScriptException();
return env.Null();
} }
db->Process(); db->Process();
info.GetReturnValue().Set(info.This()); return info.This();
} }
NAN_METHOD(Database::Interrupt) { Napi::Value Database::Interrupt(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = this->Env();
Database* db = this;
if (!db->open) { if (!db->open) {
return Nan::ThrowError("Database is not open"); Napi::Error::New(env, "Database is not open").ThrowAsJavaScriptException();
return env.Null();
} }
if (db->closing) { if (db->closing) {
return Nan::ThrowError("Database is closing"); Napi::Error::New(env, "Database is closing").ThrowAsJavaScriptException();
return env.Null();
} }
sqlite3_interrupt(db->_handle); sqlite3_interrupt(db->_handle);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Database::SetBusyTimeout(Baton* baton) { void Database::SetBusyTimeout(Baton* baton) {
...@@ -412,13 +436,14 @@ void Database::TraceCallback(void* db, const char* sql) { ...@@ -412,13 +436,14 @@ void Database::TraceCallback(void* db, const char* sql) {
void Database::TraceCallback(Database* db, std::string* sql) { void Database::TraceCallback(Database* db, std::string* sql) {
// Note: This function is called in the main V8 thread. // Note: This function is called in the main V8 thread.
Nan::HandleScope scope; Napi::Env env = db->Env();
Napi::HandleScope scope(env);
Local<Value> argv[] = { Napi::Value argv[] = {
Nan::New("trace").ToLocalChecked(), Napi::String::New(env, "trace"),
Nan::New(sql->c_str()).ToLocalChecked() Napi::String::New(env, sql->c_str())
}; };
EMIT_EVENT(db->handle(), 2, argv); EMIT_EVENT(db->Value(), 2, argv);
delete sql; delete sql;
} }
...@@ -452,14 +477,15 @@ void Database::ProfileCallback(void* db, const char* sql, sqlite3_uint64 nsecs) ...@@ -452,14 +477,15 @@ void Database::ProfileCallback(void* db, const char* sql, sqlite3_uint64 nsecs)
} }
void Database::ProfileCallback(Database *db, ProfileInfo* info) { void Database::ProfileCallback(Database *db, ProfileInfo* info) {
Nan::HandleScope scope; Napi::Env env = db->Env();
Napi::HandleScope scope(env);
Local<Value> argv[] = { Napi::Value argv[] = {
Nan::New("profile").ToLocalChecked(), Napi::String::New(env, "profile"),
Nan::New(info->sql.c_str()).ToLocalChecked(), Napi::String::New(env, info->sql.c_str()),
Nan::New<Number>((double)info->nsecs / 1000000.0) Napi::Number::New(env, (double)info->nsecs / 1000000.0)
}; };
EMIT_EVENT(db->handle(), 3, argv); EMIT_EVENT(db->Value(), 3, argv);
delete info; delete info;
} }
...@@ -496,28 +522,30 @@ void Database::UpdateCallback(void* db, int type, const char* database, ...@@ -496,28 +522,30 @@ void Database::UpdateCallback(void* db, int type, const char* database,
} }
void Database::UpdateCallback(Database *db, UpdateInfo* info) { void Database::UpdateCallback(Database *db, UpdateInfo* info) {
Nan::HandleScope scope; Napi::Env env = db->Env();
Napi::HandleScope scope(env);
Local<Value> argv[] = {
Nan::New(sqlite_authorizer_string(info->type)).ToLocalChecked(), Napi::Value argv[] = {
Nan::New(info->database.c_str()).ToLocalChecked(), Napi::String::New(env, sqlite_authorizer_string(info->type)),
Nan::New(info->table.c_str()).ToLocalChecked(), Napi::String::New(env, info->database.c_str()),
Nan::New<Number>(info->rowid), Napi::String::New(env, info->table.c_str()),
Napi::Number::New(env, info->rowid),
}; };
EMIT_EVENT(db->handle(), 4, argv); EMIT_EVENT(db->Value(), 4, argv);
delete info; delete info;
} }
NAN_METHOD(Database::Exec) { Napi::Value Database::Exec(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = this->Env();
Database* db = this;
REQUIRE_ARGUMENT_STRING(0, sql); REQUIRE_ARGUMENT_STRING(0, sql);
OPTIONAL_ARGUMENT_FUNCTION(1, callback); OPTIONAL_ARGUMENT_FUNCTION(1, callback);
Baton* baton = new ExecBaton(db, callback, *sql); Baton* baton = new ExecBaton(db, callback, sql.c_str());
db->Schedule(Work_BeginExec, baton, true); db->Schedule(Work_BeginExec, baton, true);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Database::Work_BeginExec(Baton* baton) { void Database::Work_BeginExec(Baton* baton) {
...@@ -525,13 +553,17 @@ void Database::Work_BeginExec(Baton* baton) { ...@@ -525,13 +553,17 @@ void Database::Work_BeginExec(Baton* baton) {
assert(baton->db->open); assert(baton->db->open);
assert(baton->db->_handle); assert(baton->db->_handle);
assert(baton->db->pending == 0); assert(baton->db->pending == 0);
int status = uv_queue_work(uv_default_loop(), Napi::Env env = baton->db->Env();
&baton->request, Work_Exec, (uv_after_work_cb)Work_AfterExec); int status = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.Exec"),
Work_Exec, Work_AfterExec, baton, &baton->request
);
assert(status == 0); assert(status == 0);
napi_queue_async_work(env, baton->request);
} }
void Database::Work_Exec(uv_work_t* req) { void Database::Work_Exec(napi_env e, void* data) {
ExecBaton* baton = static_cast<ExecBaton*>(req->data); ExecBaton* baton = static_cast<ExecBaton*>(data);
char* message = NULL; char* message = NULL;
baton->status = sqlite3_exec( baton->status = sqlite3_exec(
...@@ -548,59 +580,64 @@ void Database::Work_Exec(uv_work_t* req) { ...@@ -548,59 +580,64 @@ void Database::Work_Exec(uv_work_t* req) {
} }
} }
void Database::Work_AfterExec(uv_work_t* req) { void Database::Work_AfterExec(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope; ExecBaton* baton = static_cast<ExecBaton*>(data);
ExecBaton* baton = static_cast<ExecBaton*>(req->data);
Database* db = baton->db; Database* db = baton->db;
Local<Function> cb = Nan::New(baton->callback); Napi::Env env = db->Env();
Napi::HandleScope scope(env);
Napi::Function cb = baton->callback.Value();
if (baton->status != SQLITE_OK) { if (baton->status != SQLITE_OK) {
EXCEPTION(baton->message, baton->status, exception); EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
TRY_CATCH_CALL(db->handle(), cb, 1, argv); TRY_CATCH_CALL(db->Value(), cb, 1, argv);
} }
else { else {
Local<Value> info[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value info[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(db->handle(), 2, info); EMIT_EVENT(db->Value(), 2, info);
} }
} }
else if (!cb.IsEmpty() && cb->IsFunction()) { else if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(db->handle(), cb, 1, argv); TRY_CATCH_CALL(db->Value(), cb, 1, argv);
} }
db->Process(); db->Process();
napi_delete_async_work(e, baton->request);
delete baton; delete baton;
} }
NAN_METHOD(Database::Wait) { Napi::Value Database::Wait(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = info.Env();
Database* db = this;
OPTIONAL_ARGUMENT_FUNCTION(0, callback); OPTIONAL_ARGUMENT_FUNCTION(0, callback);
Baton* baton = new Baton(db, callback); Baton* baton = new Baton(db, callback);
db->Schedule(Work_Wait, baton, true); db->Schedule(Work_Wait, baton, true);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Database::Work_Wait(Baton* baton) { void Database::Work_Wait(Baton* baton) {
Nan::HandleScope scope; Napi::Env env = baton->db->Env();
Napi::HandleScope scope(env);
assert(baton->db->locked); assert(baton->db->locked);
assert(baton->db->open); assert(baton->db->open);
assert(baton->db->_handle); assert(baton->db->_handle);
assert(baton->db->pending == 0); assert(baton->db->pending == 0);
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(baton->db->handle(), cb, 1, argv); TRY_CATCH_CALL(baton->db->Value(), cb, 1, argv);
} }
baton->db->Process(); baton->db->Process();
...@@ -608,16 +645,17 @@ void Database::Work_Wait(Baton* baton) { ...@@ -608,16 +645,17 @@ void Database::Work_Wait(Baton* baton) {
delete baton; delete baton;
} }
NAN_METHOD(Database::LoadExtension) { Napi::Value Database::LoadExtension(const Napi::CallbackInfo& info) {
Database* db = Nan::ObjectWrap::Unwrap<Database>(info.This()); Napi::Env env = this->Env();
Database* db = this;
REQUIRE_ARGUMENT_STRING(0, filename); REQUIRE_ARGUMENT_STRING(0, filename);
OPTIONAL_ARGUMENT_FUNCTION(1, callback); OPTIONAL_ARGUMENT_FUNCTION(1, callback);
Baton* baton = new LoadExtensionBaton(db, callback, *filename); Baton* baton = new LoadExtensionBaton(db, callback, filename.c_str());
db->Schedule(Work_BeginLoadExtension, baton, true); db->Schedule(Work_BeginLoadExtension, baton, true);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Database::Work_BeginLoadExtension(Baton* baton) { void Database::Work_BeginLoadExtension(Baton* baton) {
...@@ -625,13 +663,17 @@ void Database::Work_BeginLoadExtension(Baton* baton) { ...@@ -625,13 +663,17 @@ void Database::Work_BeginLoadExtension(Baton* baton) {
assert(baton->db->open); assert(baton->db->open);
assert(baton->db->_handle); assert(baton->db->_handle);
assert(baton->db->pending == 0); assert(baton->db->pending == 0);
int status = uv_queue_work(uv_default_loop(), Napi::Env env = baton->db->Env();
&baton->request, Work_LoadExtension, reinterpret_cast<uv_after_work_cb>(Work_AfterLoadExtension)); int status = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.LoadExtension"),
Work_LoadExtension, Work_AfterLoadExtension, baton, &baton->request
);
assert(status == 0); assert(status == 0);
napi_queue_async_work(env, baton->request);
} }
void Database::Work_LoadExtension(uv_work_t* req) { void Database::Work_LoadExtension(napi_env e, void* data) {
LoadExtensionBaton* baton = static_cast<LoadExtensionBaton*>(req->data); LoadExtensionBaton* baton = static_cast<LoadExtensionBaton*>(data);
sqlite3_enable_load_extension(baton->db->_handle, 1); sqlite3_enable_load_extension(baton->db->_handle, 1);
...@@ -651,32 +693,36 @@ void Database::Work_LoadExtension(uv_work_t* req) { ...@@ -651,32 +693,36 @@ void Database::Work_LoadExtension(uv_work_t* req) {
} }
} }
void Database::Work_AfterLoadExtension(uv_work_t* req) { void Database::Work_AfterLoadExtension(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope; LoadExtensionBaton* baton = static_cast<LoadExtensionBaton*>(data);
LoadExtensionBaton* baton = static_cast<LoadExtensionBaton*>(req->data);
Database* db = baton->db; Database* db = baton->db;
Local<Function> cb = Nan::New(baton->callback);
Napi::Env env = db->Env();
Napi::HandleScope scope(env);
Napi::Function cb = baton->callback.Value();
if (baton->status != SQLITE_OK) { if (baton->status != SQLITE_OK) {
EXCEPTION(baton->message, baton->status, exception); EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
TRY_CATCH_CALL(db->handle(), cb, 1, argv); TRY_CATCH_CALL(db->Value(), cb, 1, argv);
} }
else { else {
Local<Value> info[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value info[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(db->handle(), 2, info); EMIT_EVENT(db->Value(), 2, info);
} }
} }
else if (!cb.IsEmpty() && cb->IsFunction()) { else if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(db->handle(), cb, 1, argv); TRY_CATCH_CALL(db->Value(), cb, 1, argv);
} }
db->Process(); db->Process();
napi_delete_async_work(e, baton->request);
delete baton; delete baton;
} }
......
...@@ -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);
......
...@@ -3,47 +3,71 @@ ...@@ -3,47 +3,71 @@
const char* sqlite_code_string(int code); const char* sqlite_code_string(int code);
const char* sqlite_authorizer_string(int type); const char* sqlite_authorizer_string(int type);
#include <vector>
// TODO: better way to work around StringConcat?
#include <napi.h>
inline Napi::String StringConcat(Napi::Value str1, Napi::Value str2) {
return Napi::String::New(str1.Env(), str1.As<Napi::String>().Utf8Value() +
str2.As<Napi::String>().Utf8Value() );
}
// A Napi substitute IsInt32()
inline bool OtherIsInt(Napi::Number source) {
double orig_val = source.DoubleValue();
double int_val = (double)source.Int32Value();
if (orig_val == int_val) {
return true;
} else {
return false;
}
}
#define REQUIRE_ARGUMENTS(n) \ #define REQUIRE_ARGUMENTS(n) \
if (info.Length() < (n)) { \ if (info.Length() < (n)) { \
return Nan::ThrowTypeError("Expected " #n "arguments"); \ Napi::TypeError::New(env, "Expected " #n "arguments").ThrowAsJavaScriptException(); \
return env.Null(); \
} }
#define REQUIRE_ARGUMENT_EXTERNAL(i, var) \ #define REQUIRE_ARGUMENT_EXTERNAL(i, var) \
if (info.Length() <= (i) || !info[i]->IsExternal()) { \ if (info.Length() <= (i) || !info[i].IsExternal()) { \
return Nan::ThrowTypeError("Argument " #i " invalid"); \ Napi::TypeError::New(env, "Argument " #i " invalid").ThrowAsJavaScriptException(); \
return env.Null(); \
} \ } \
Local<External> var = Local<External>::Cast(info[i]); Napi::External var = info[i].As<Napi::External>();
#define REQUIRE_ARGUMENT_FUNCTION(i, var) \ #define REQUIRE_ARGUMENT_FUNCTION(i, var) \
if (info.Length() <= (i) || !info[i]->IsFunction()) { \ if (info.Length() <= (i) || !info[i].IsFunction()) { \
return Nan::ThrowTypeError("Argument " #i " must be a function"); \ Napi::TypeError::New(env, "Argument " #i " must be a function").ThrowAsJavaScriptException(); \
return env.Null(); \
} \ } \
Local<Function> var = Local<Function>::Cast(info[i]); Napi::Function var = info[i].As<Napi::Function>();
#define REQUIRE_ARGUMENT_STRING(i, var) \ #define REQUIRE_ARGUMENT_STRING(i, var) \
if (info.Length() <= (i) || !info[i]->IsString()) { \ if (info.Length() <= (i) || !info[i].IsString()) { \
return Nan::ThrowTypeError("Argument " #i " must be a string"); \ Napi::TypeError::New(env, "Argument " #i " must be a string").ThrowAsJavaScriptException(); \
return env.Null(); \
} \ } \
Nan::Utf8String var(info[i]); std::string var = info[i].As<Napi::String>();
#define REQUIRE_ARGUMENT_INTEGER(i, var) \ #define REQUIRE_ARGUMENT_INTEGER(i, var) \
if (info.Length() <= (i) || !info[i]->IsInt32()) { \ if (info.Length() <= (i) || !info[i].IsNumber()) { \
return Nan::ThrowTypeError("Argument " #i " must be an integer"); \ Napi::TypeError::New(env, "Argument " #i " must be an integer").ThrowAsJavaScriptException(); \
return env.Null(); \
} \ } \
int var(Nan::To<int32_t>(info[i]).FromJust()); int var(info[i].As<Napi::Number>().Int32Value());
#define OPTIONAL_ARGUMENT_FUNCTION(i, var) \ #define OPTIONAL_ARGUMENT_FUNCTION(i, var) \
Local<Function> var; \ Napi::Function var; \
if (info.Length() > i && !info[i]->IsUndefined()) { \ if (info.Length() > i && !info[i].IsUndefined()) { \
if (!info[i]->IsFunction()) { \ if (!info[i].IsFunction()) { \
return Nan::ThrowTypeError("Argument " #i " must be a function"); \ Napi::TypeError::New(env, "Argument " #i " must be a function").ThrowAsJavaScriptException(); \
return env.Null(); \
} \ } \
var = Local<Function>::Cast(info[i]); \ var = info[i].As<Napi::Function>(); \
} }
...@@ -52,70 +76,62 @@ const char* sqlite_authorizer_string(int type); ...@@ -52,70 +76,62 @@ const char* sqlite_authorizer_string(int type);
if (info.Length() <= (i)) { \ if (info.Length() <= (i)) { \
var = (default); \ var = (default); \
} \ } \
else if (info[i]->IsInt32()) { \ else if (info[i].IsNumber()) { \
var = Nan::To<int32_t>(info[i]).FromJust(); \ if (OtherIsInt(info[i].As<Number>())) { \
var = info[i].As<Napi::Number>().Int32Value(); \
} \
} \ } \
else { \ else { \
return Nan::ThrowTypeError("Argument " #i " must be an integer"); \ Napi::TypeError::New(env, "Argument " #i " must be an integer").ThrowAsJavaScriptException(); \
return env.Null(); \
} }
#define DEFINE_CONSTANT_INTEGER(target, constant, name) \ #define DEFINE_CONSTANT_INTEGER(target, constant, name) \
Nan::ForceSet(target, \ Napi::PropertyDescriptor::Value(#name, Napi::Number::New(env, constant), \
Nan::New(#name).ToLocalChecked(), \ static_cast<napi_property_attributes>(napi_enumerable | napi_configurable)),
Nan::New<Integer>(constant), \
static_cast<PropertyAttribute>(ReadOnly | DontDelete) \
);
#define DEFINE_CONSTANT_STRING(target, constant, name) \ #define DEFINE_CONSTANT_STRING(target, constant, name) \
Nan::ForceSet(target, \ Napi::PropertyDescriptor::Value(#name, Napi::String::New(env, constant), \
Nan::New(#name).ToLocalChecked(), \ static_cast<napi_property_attributes>(napi_enumerable | napi_configurable)),
Nan::New(constant).ToLocalChecked(), \
static_cast<PropertyAttribute>(ReadOnly | DontDelete) \
);
#define NODE_SET_GETTER(target, name, function) \
Nan::SetAccessor((target)->InstanceTemplate(), \
Nan::New(name).ToLocalChecked(), (function));
#define NODE_SET_SETTER(target, name, getter, setter) \
Nan::SetAccessor((target)->InstanceTemplate(), \
Nan::New(name).ToLocalChecked(), getter, setter);
#define GET_STRING(source, name, property) \
Nan::Utf8String name(Nan::Get(source, \
Nan::New(prop).ToLocalChecked()).ToLocalChecked());
#define GET_INTEGER(source, name, prop) \
int name = Nan::To<int>(Nan::Get(source, \
Nan::New(property).ToLocalChecked()).ToLocalChecked()).FromJust();
#define EXCEPTION(msg, errno, name) \ #define EXCEPTION(msg, errno, name) \
Local<Value> name = Exception::Error(Nan::New( \ Napi::Value name = Napi::Error::New(env, \
std::string(sqlite_code_string(errno)) + \ StringConcat( \
std::string(": ") + std::string(msg) \ StringConcat( \
).ToLocalChecked()); \ Napi::String::New(env, sqlite_code_string(errno)), \
Local<Object> name ##_obj = name.As<Object>(); \ Napi::String::New(env, ": ") \
Nan::Set(name ##_obj, Nan::New("errno").ToLocalChecked(), Nan::New(errno));\ ), \
Nan::Set(name ##_obj, Nan::New("code").ToLocalChecked(), \ (msg) \
Nan::New(sqlite_code_string(errno)).ToLocalChecked()); ).Utf8Value() \
).Value(); \
Napi::Object name ##_obj = name.As<Napi::Object>(); \
(name ##_obj).Set( Napi::String::New(env, "errno"), Napi::Number::New(env, errno)); \
(name ##_obj).Set( Napi::String::New(env, "code"), \
Napi::String::New(env, sqlite_code_string(errno)));
#define EMIT_EVENT(obj, argc, argv) \ #define EMIT_EVENT(obj, argc, argv) \
TRY_CATCH_CALL((obj), \ TRY_CATCH_CALL((obj), \
Nan::Get(obj, \ (obj).Get("emit").As<Napi::Function>(),\
Nan::New("emit").ToLocalChecked()).ToLocalChecked().As<Function>(),\
argc, argv \ argc, argv \
); );
// The Mac OS compiler complains when argv is NULL unless we
// first assign it to a locally defined variable.
#define TRY_CATCH_CALL(context, callback, argc, argv) \ #define TRY_CATCH_CALL(context, callback, argc, argv) \
Nan::MakeCallback((context), (callback), (argc), (argv)) Napi::Value* passed_argv = argv;\
std::vector<napi_value> args;\
if ((argc != 0) && (passed_argv != NULL)) {\
args.assign(passed_argv, passed_argv + argc);\
}\
(callback).MakeCallback(Napi::Value(context), args);
#define WORK_DEFINITION(name) \ #define WORK_DEFINITION(name) \
static NAN_METHOD(name); \ Napi::Value name(const Napi::CallbackInfo& info); \
static void Work_Begin##name(Baton* baton); \ static void Work_Begin##name(Baton* baton); \
static void Work_##name(uv_work_t* req); \ static void Work_##name(napi_env env, void* data); \
static void Work_After##name(uv_work_t* req); static void Work_After##name(napi_env env, napi_status status, void* data);
#define STATEMENT_BEGIN(type) \ #define STATEMENT_BEGIN(type) \
assert(baton); \ assert(baton); \
...@@ -125,13 +141,16 @@ const char* sqlite_authorizer_string(int type); ...@@ -125,13 +141,16 @@ const char* sqlite_authorizer_string(int type);
assert(baton->stmt->prepared); \ assert(baton->stmt->prepared); \
baton->stmt->locked = true; \ baton->stmt->locked = true; \
baton->stmt->db->pending++; \ baton->stmt->db->pending++; \
int status = uv_queue_work(uv_default_loop(), \ Napi::Env env = baton->stmt->Env(); \
&baton->request, \ int status = napi_create_async_work( \
Work_##type, reinterpret_cast<uv_after_work_cb>(Work_After##type)); \ env, NULL, Napi::String::New(env, "sqlite3.Statement."#type), \
assert(status == 0); Work_##type, Work_After##type, baton, &baton->request \
); \
assert(status == 0); \
napi_queue_async_work(env, baton->request);
#define STATEMENT_INIT(type) \ #define STATEMENT_INIT(type) \
type* baton = static_cast<type*>(req->data); \ type* baton = static_cast<type*>(data); \
Statement* stmt = baton->stmt; Statement* stmt = baton->stmt;
#define STATEMENT_END() \ #define STATEMENT_END() \
...@@ -141,6 +160,7 @@ const char* sqlite_authorizer_string(int type); ...@@ -141,6 +160,7 @@ const char* sqlite_authorizer_string(int type);
stmt->db->pending--; \ stmt->db->pending--; \
stmt->Process(); \ stmt->Process(); \
stmt->db->Process(); \ stmt->db->Process(); \
napi_delete_async_work(e, baton->request); \
delete baton; delete baton;
#define BACKUP_BEGIN(type) \ #define BACKUP_BEGIN(type) \
...@@ -151,13 +171,16 @@ const char* sqlite_authorizer_string(int type); ...@@ -151,13 +171,16 @@ const char* sqlite_authorizer_string(int type);
assert(baton->backup->inited); \ assert(baton->backup->inited); \
baton->backup->locked = true; \ baton->backup->locked = true; \
baton->backup->db->pending++; \ baton->backup->db->pending++; \
int status = uv_queue_work(uv_default_loop(), \ Napi::Env env = baton->backup->Env(); \
&baton->request, \ int status = napi_create_async_work( \
Work_##type, reinterpret_cast<uv_after_work_cb>(Work_After##type)); \ env, NULL, Napi::String::New(env, "sqlite3.Backup."#type), \
assert(status == 0); Work_##type, Work_After##type, baton, &baton->request \
); \
assert(status == 0); \
napi_queue_async_work(env, baton->request);
#define BACKUP_INIT(type) \ #define BACKUP_INIT(type) \
type* baton = static_cast<type*>(req->data); \ type* baton = static_cast<type*>(data); \
Backup* backup = baton->backup; Backup* backup = baton->backup;
#define BACKUP_END() \ #define BACKUP_END() \
...@@ -167,6 +190,7 @@ const char* sqlite_authorizer_string(int type); ...@@ -167,6 +190,7 @@ const char* sqlite_authorizer_string(int type);
backup->db->pending--; \ backup->db->pending--; \
backup->Process(); \ backup->Process(); \
backup->db->Process(); \ backup->db->Process(); \
napi_delete_async_work(e, baton->request); \
delete baton; delete baton;
#define DELETE_FIELD(field) \ #define DELETE_FIELD(field) \
......
...@@ -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)
#include <string.h> #include <string.h>
#include <node.h> #include <napi.h>
#include <node_buffer.h> #include <uv.h>
#include <node_version.h>
#include "macros.h" #include "macros.h"
#include "database.h" #include "database.h"
...@@ -9,27 +8,37 @@ ...@@ -9,27 +8,37 @@
using namespace node_sqlite3; using namespace node_sqlite3;
Nan::Persistent<FunctionTemplate> Statement::constructor_template; Napi::FunctionReference Statement::constructor;
NAN_MODULE_INIT(Statement::Init) { Napi::Object Statement::Init(Napi::Env env, Napi::Object exports) {
Nan::HandleScope scope; Napi::HandleScope scope(env);
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New); Napi::Function t = DefineClass(env, "Statement", {
InstanceMethod("bind", &Statement::Bind),
InstanceMethod("get", &Statement::Get),
InstanceMethod("run", &Statement::Run),
InstanceMethod("all", &Statement::All),
InstanceMethod("each", &Statement::Each),
InstanceMethod("reset", &Statement::Reset),
InstanceMethod("finalize", &Statement::Finalize_),
});
t->InstanceTemplate()->SetInternalFieldCount(1); constructor = Napi::Persistent(t);
t->SetClassName(Nan::New("Statement").ToLocalChecked()); constructor.SuppressDestruct();
Nan::SetPrototypeMethod(t, "bind", Bind); exports.Set("Statement", t);
Nan::SetPrototypeMethod(t, "get", Get); return exports;
Nan::SetPrototypeMethod(t, "run", Run); }
Nan::SetPrototypeMethod(t, "all", All);
Nan::SetPrototypeMethod(t, "each", Each); // A Napi InstanceOf for Javascript Objects "Date" and "RegExp"
Nan::SetPrototypeMethod(t, "reset", Reset); bool OtherInstanceOf(Napi::Object source, char* object_type) {
Nan::SetPrototypeMethod(t, "finalize", Finalize); if (object_type == "Date") {
return source.InstanceOf(source.Env().Global().Get("Date").As<Function>());
} else if (object_type == "RegExp") {
return source.InstanceOf(source.Env().Global().Get("RegExp").As<Function>());
}
constructor_template.Reset(t); return false;
Nan::Set(target, Nan::New("Statement").ToLocalChecked(),
Nan::GetFunction(t).ToLocalChecked());
} }
void Statement::Process() { void Statement::Process() {
...@@ -60,67 +69,71 @@ void Statement::Schedule(Work_Callback callback, Baton* baton) { ...@@ -60,67 +69,71 @@ void Statement::Schedule(Work_Callback callback, Baton* baton) {
} }
template <class T> void Statement::Error(T* baton) { template <class T> void Statement::Error(T* baton) {
Nan::HandleScope scope;
Statement* stmt = baton->stmt; Statement* stmt = baton->stmt;
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
// Fail hard on logic errors. // Fail hard on logic errors.
assert(stmt->status != 0); assert(stmt->status != 0);
EXCEPTION(stmt->message, stmt->status, exception); EXCEPTION(Napi::String::New(env, stmt->message.c_str()), stmt->status, exception);
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
TRY_CATCH_CALL(stmt->handle(), cb, 1, argv); TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
} }
else { else {
Local<Value> argv[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value argv[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(stmt->handle(), 2, argv); EMIT_EVENT(stmt->Value(), 2, argv);
} }
} }
// { Database db, String sql, Array params, Function callback } // { Database db, String sql, Array params, Function callback }
NAN_METHOD(Statement::New) { Statement::Statement(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Statement>(info) {
if (!info.IsConstructCall()) { Napi::Env env = info.Env();
return Nan::ThrowTypeError("Use the new operator to create new Statement objects");
}
int length = info.Length(); int length = info.Length();
if (length <= 0 || !Database::HasInstance(info[0])) { if (length <= 0 || !Database::HasInstance(info[0])) {
return Nan::ThrowTypeError("Database object expected"); Napi::TypeError::New(env, "Database object expected").ThrowAsJavaScriptException();
return;
} }
else if (length <= 1 || !info[1]->IsString()) { else if (length <= 1 || !info[1].IsString()) {
return Nan::ThrowTypeError("SQL query expected"); Napi::TypeError::New(env, "SQL query expected").ThrowAsJavaScriptException();
return;
} }
else if (length > 2 && !info[2]->IsUndefined() && !info[2]->IsFunction()) { else if (length > 2 && !info[2].IsUndefined() && !info[2].IsFunction()) {
return Nan::ThrowTypeError("Callback expected"); Napi::TypeError::New(env, "Callback expected").ThrowAsJavaScriptException();
return;
} }
Database* db = Nan::ObjectWrap::Unwrap<Database>(info[0].As<Object>()); Database* db = Napi::ObjectWrap<Database>::Unwrap(info[0].As<Napi::Object>());
Local<String> sql = Local<String>::Cast(info[1]); Napi::String sql = info[1].As<Napi::String>();
Nan::ForceSet(info.This(),Nan::New("sql").ToLocalChecked(), sql, ReadOnly); info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("sql", sql, napi_default));
Statement* stmt = new Statement(db); init(db);
stmt->Wrap(info.This()); Statement* stmt = this;
PrepareBaton* baton = new PrepareBaton(db, Local<Function>::Cast(info[2]), stmt); PrepareBaton* baton = new PrepareBaton(db, info[2].As<Napi::Function>(), stmt);
baton->sql = std::string(*Nan::Utf8String(sql)); baton->sql = std::string(sql.As<Napi::String>().Utf8Value().c_str());
db->Schedule(Work_BeginPrepare, baton); db->Schedule(Work_BeginPrepare, baton);
info.GetReturnValue().Set(info.This());
} }
void Statement::Work_BeginPrepare(Database::Baton* baton) { void Statement::Work_BeginPrepare(Database::Baton* baton) {
assert(baton->db->open); assert(baton->db->open);
baton->db->pending++; baton->db->pending++;
int status = uv_queue_work(uv_default_loop(), Napi::Env env = baton->db->Env();
&baton->request, Work_Prepare, (uv_after_work_cb)Work_AfterPrepare); int status = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Statement.Prepare"),
Work_Prepare, Work_AfterPrepare, baton, &baton->request
);
assert(status == 0); assert(status == 0);
napi_queue_async_work(env, baton->request);
} }
void Statement::Work_Prepare(uv_work_t* req) { void Statement::Work_Prepare(napi_env e, void* data) {
STATEMENT_INIT(PrepareBaton); STATEMENT_INIT(PrepareBaton);
// In case preparing fails, we use a mutex to make sure we get the associated // In case preparing fails, we use a mutex to make sure we get the associated
...@@ -144,21 +157,22 @@ void Statement::Work_Prepare(uv_work_t* req) { ...@@ -144,21 +157,22 @@ void Statement::Work_Prepare(uv_work_t* req) {
sqlite3_mutex_leave(mtx); sqlite3_mutex_leave(mtx);
} }
void Statement::Work_AfterPrepare(uv_work_t* req) { void Statement::Work_AfterPrepare(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
STATEMENT_INIT(PrepareBaton); STATEMENT_INIT(PrepareBaton);
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
if (stmt->status != SQLITE_OK) { if (stmt->status != SQLITE_OK) {
Error(baton); Error(baton);
stmt->Finalize(); stmt->Finalize_();
} }
else { else {
stmt->prepared = true; stmt->prepared = true;
Local<Function> cb = Nan::New(baton->callback); if (!baton->callback.IsEmpty() && baton->callback.Value().IsFunction()) {
if (!cb.IsEmpty() && cb->IsFunction()) { Napi::Function cb = baton->callback.Value();
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(stmt->handle(), cb, 1, argv); TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
} }
} }
...@@ -166,77 +180,88 @@ void Statement::Work_AfterPrepare(uv_work_t* req) { ...@@ -166,77 +180,88 @@ void Statement::Work_AfterPrepare(uv_work_t* req) {
} }
template <class T> Values::Field* template <class T> Values::Field*
Statement::BindParameter(const Local<Value> source, T pos) { Statement::BindParameter(const Napi::Value source, T pos) {
if (source->IsString() || source->IsRegExp()) { if (source.IsString()) {
Nan::Utf8String val(source); std::string val = source.As<Napi::String>().Utf8Value();
return new Values::Text(pos, val.length(), *val); return new Values::Text(pos, val.length(), val.c_str());
}
else if (OtherInstanceOf(source.As<Object>(), "RegExp")) {
std::string val = source.ToString().Utf8Value();
return new Values::Text(pos, val.length(), val.c_str());
} }
else if (source->IsInt32()) { else if (source.IsNumber()) {
return new Values::Integer(pos, Nan::To<int32_t>(source).FromJust()); if (OtherIsInt(source.As<Napi::Number>())) {
return new Values::Integer(pos, source.As<Napi::Number>().Int32Value());
} else {
return new Values::Float(pos, source.As<Napi::Number>().DoubleValue());
} }
else if (source->IsNumber()) {
return new Values::Float(pos, Nan::To<double>(source).FromJust());
} }
else if (source->IsBoolean()) { else if (source.IsBoolean()) {
return new Values::Integer(pos, Nan::To<bool>(source).FromJust() ? 1 : 0); return new Values::Integer(pos, source.As<Napi::Boolean>().Value() ? 1 : 0);
} }
else if (source->IsNull()) { else if (source.IsNull()) {
return new Values::Null(pos); return new Values::Null(pos);
} }
else if (Buffer::HasInstance(source)) { else if (source.IsBuffer()) {
Local<Object> buffer = Nan::To<Object>(source).ToLocalChecked(); Napi::Buffer<char> buffer = source.As<Napi::Buffer<char>>();
return new Values::Blob(pos, Buffer::Length(buffer), Buffer::Data(buffer)); return new Values::Blob(pos, buffer.Length(), buffer.Data());
}
else if (OtherInstanceOf(source.As<Object>(), "Date")) {
return new Values::Float(pos, source.ToNumber().DoubleValue());
} }
else if (source->IsDate()) { else if (source.IsObject()) {
return new Values::Float(pos, Nan::To<double>(source).FromJust()); std::string val = source.ToString().Utf8Value();
return new Values::Text(pos, val.length(), val.c_str());
} }
else { else {
return NULL; return NULL;
} }
} }
template <class T> T* Statement::Bind(Nan::NAN_METHOD_ARGS_TYPE info, int start, int last) { template <class T> T* Statement::Bind(const Napi::CallbackInfo& info, int start, int last) {
Nan::HandleScope scope; Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (last < 0) last = info.Length(); if (last < 0) last = info.Length();
Local<Function> callback; Napi::Function callback;
if (last > start && info[last - 1]->IsFunction()) { if (last > start && info[last - 1].IsFunction()) {
callback = Local<Function>::Cast(info[last - 1]); callback = info[last - 1].As<Napi::Function>();
last--; last--;
} }
T* baton = new T(this, callback); T* baton = new T(this, callback);
if (start < last) { if (start < last) {
if (info[start]->IsArray()) { if (info[start].IsArray()) {
Local<Array> array = Local<Array>::Cast(info[start]); Napi::Array array = info[start].As<Napi::Array>();
int length = array->Length(); int length = array.Length();
// Note: bind parameters start with 1. // Note: bind parameters start with 1.
for (int i = 0, pos = 1; i < length; i++, pos++) { for (int i = 0, pos = 1; i < length; i++, pos++) {
baton->parameters.push_back(BindParameter(Nan::Get(array, i).ToLocalChecked(), pos)); baton->parameters.push_back(BindParameter((array).Get(i), pos));
} }
} }
else if (!info[start]->IsObject() || info[start]->IsRegExp() || info[start]->IsDate() || Buffer::HasInstance(info[start])) { else if (!info[start].IsObject() || OtherInstanceOf(info[start].As<Object>(), "RegExp") || OtherInstanceOf(info[start].As<Object>(), "Date") || info[start].IsBuffer()) {
// Parameters directly in array. // Parameters directly in array.
// Note: bind parameters start with 1. // Note: bind parameters start with 1.
for (int i = start, pos = 1; i < last; i++, pos++) { for (int i = start, pos = 1; i < last; i++, pos++) {
baton->parameters.push_back(BindParameter(info[i], pos)); baton->parameters.push_back(BindParameter(info[i], pos));
} }
} }
else if (info[start]->IsObject()) { else if (info[start].IsObject()) {
Local<Object> object = Local<Object>::Cast(info[start]); Napi::Object object = info[start].As<Napi::Object>();
Local<Array> array = Nan::GetPropertyNames(object).ToLocalChecked(); Napi::Array array = object.GetPropertyNames();
int length = array->Length(); int length = array.Length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Local<Value> name = Nan::Get(array, i).ToLocalChecked(); Napi::Value name = (array).Get(i);
Napi::Number num = name.ToNumber();
if (name->IsInt32()) { if (num.Int32Value() == num.DoubleValue()) {
baton->parameters.push_back( baton->parameters.push_back(
BindParameter(Nan::Get(object, name).ToLocalChecked(), Nan::To<int32_t>(name).FromJust())); BindParameter((object).Get(name), num.Int32Value()));
} }
else { else {
baton->parameters.push_back(BindParameter(Nan::Get(object, name).ToLocalChecked(), baton->parameters.push_back(BindParameter((object).Get(name),
*Nan::Utf8String(name))); name.As<Napi::String>().Utf8Value().c_str()));
} }
} }
} }
...@@ -263,7 +288,7 @@ bool Statement::Bind(const Parameters & parameters) { ...@@ -263,7 +288,7 @@ bool Statement::Bind(const Parameters & parameters) {
Values::Field* field = *it; Values::Field* field = *it;
if (field != NULL) { if (field != NULL) {
int pos; unsigned int pos;
if (field->index > 0) { if (field->index > 0) {
pos = field->index; pos = field->index;
} }
...@@ -305,16 +330,18 @@ bool Statement::Bind(const Parameters & parameters) { ...@@ -305,16 +330,18 @@ bool Statement::Bind(const Parameters & parameters) {
return true; return true;
} }
NAN_METHOD(Statement::Bind) { Napi::Value Statement::Bind(const Napi::CallbackInfo& info) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This()); Napi::Env env = info.Env();
Statement* stmt = this;
Baton* baton = stmt->Bind<Baton>(info); Baton* baton = stmt->Bind<Baton>(info);
if (baton == NULL) { if (baton == NULL) {
return Nan::ThrowTypeError("Data type is not supported"); Napi::TypeError::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
} }
else { else {
stmt->Schedule(Work_BeginBind, baton); stmt->Schedule(Work_BeginBind, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
} }
...@@ -322,7 +349,7 @@ void Statement::Work_BeginBind(Baton* baton) { ...@@ -322,7 +349,7 @@ void Statement::Work_BeginBind(Baton* baton) {
STATEMENT_BEGIN(Bind); STATEMENT_BEGIN(Bind);
} }
void Statement::Work_Bind(uv_work_t* req) { void Statement::Work_Bind(napi_env e, void* data) {
STATEMENT_INIT(Baton); STATEMENT_INIT(Baton);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle); sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
...@@ -331,20 +358,21 @@ void Statement::Work_Bind(uv_work_t* req) { ...@@ -331,20 +358,21 @@ void Statement::Work_Bind(uv_work_t* req) {
sqlite3_mutex_leave(mtx); sqlite3_mutex_leave(mtx);
} }
void Statement::Work_AfterBind(uv_work_t* req) { void Statement::Work_AfterBind(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
STATEMENT_INIT(Baton); STATEMENT_INIT(Baton);
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
if (stmt->status != SQLITE_OK) { if (stmt->status != SQLITE_OK) {
Error(baton); Error(baton);
} }
else { else {
// Fire callbacks. // Fire callbacks.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(stmt->handle(), cb, 1, argv); TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
} }
} }
...@@ -353,16 +381,18 @@ void Statement::Work_AfterBind(uv_work_t* req) { ...@@ -353,16 +381,18 @@ void Statement::Work_AfterBind(uv_work_t* req) {
NAN_METHOD(Statement::Get) { Napi::Value Statement::Get(const Napi::CallbackInfo& info) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This()); Napi::Env env = info.Env();
Statement* stmt = this;
Baton* baton = stmt->Bind<RowBaton>(info); Baton* baton = stmt->Bind<RowBaton>(info);
if (baton == NULL) { if (baton == NULL) {
return Nan::ThrowError("Data type is not supported"); Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
} }
else { else {
stmt->Schedule(Work_BeginGet, baton); stmt->Schedule(Work_BeginGet, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
} }
...@@ -370,7 +400,7 @@ void Statement::Work_BeginGet(Baton* baton) { ...@@ -370,7 +400,7 @@ void Statement::Work_BeginGet(Baton* baton) {
STATEMENT_BEGIN(Get); STATEMENT_BEGIN(Get);
} }
void Statement::Work_Get(uv_work_t* req) { void Statement::Work_Get(napi_env e, void* data) {
STATEMENT_INIT(RowBaton); STATEMENT_INIT(RowBaton);
if (stmt->status != SQLITE_DONE || baton->parameters.size()) { if (stmt->status != SQLITE_DONE || baton->parameters.size()) {
...@@ -394,26 +424,27 @@ void Statement::Work_Get(uv_work_t* req) { ...@@ -394,26 +424,27 @@ void Statement::Work_Get(uv_work_t* req) {
} }
} }
void Statement::Work_AfterGet(uv_work_t* req) { void Statement::Work_AfterGet(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
STATEMENT_INIT(RowBaton); STATEMENT_INIT(RowBaton);
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) { if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) {
Error(baton); Error(baton);
} }
else { else {
// Fire callbacks. // Fire callbacks.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
if (stmt->status == SQLITE_ROW) { if (stmt->status == SQLITE_ROW) {
// Create the result array from the data we acquired. // Create the result array from the data we acquired.
Local<Value> argv[] = { Nan::Null(), RowToJS(&baton->row) }; Napi::Value argv[] = { env.Null(), RowToJS(env, &baton->row) };
TRY_CATCH_CALL(stmt->handle(), cb, 2, argv); TRY_CATCH_CALL(stmt->Value(), cb, 2, argv);
} }
else { else {
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(stmt->handle(), cb, 1, argv); TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
} }
} }
} }
...@@ -421,16 +452,18 @@ void Statement::Work_AfterGet(uv_work_t* req) { ...@@ -421,16 +452,18 @@ void Statement::Work_AfterGet(uv_work_t* req) {
STATEMENT_END(); STATEMENT_END();
} }
NAN_METHOD(Statement::Run) { Napi::Value Statement::Run(const Napi::CallbackInfo& info) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This()); Napi::Env env = info.Env();
Statement* stmt = this;
Baton* baton = stmt->Bind<RunBaton>(info); Baton* baton = stmt->Bind<RunBaton>(info);
if (baton == NULL) { if (baton == NULL) {
return Nan::ThrowError("Data type is not supported"); Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
} }
else { else {
stmt->Schedule(Work_BeginRun, baton); stmt->Schedule(Work_BeginRun, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
} }
...@@ -438,7 +471,7 @@ void Statement::Work_BeginRun(Baton* baton) { ...@@ -438,7 +471,7 @@ void Statement::Work_BeginRun(Baton* baton) {
STATEMENT_BEGIN(Run); STATEMENT_BEGIN(Run);
} }
void Statement::Work_Run(uv_work_t* req) { void Statement::Work_Run(napi_env e, void* data) {
STATEMENT_INIT(RunBaton); STATEMENT_INIT(RunBaton);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle); sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
...@@ -464,39 +497,42 @@ void Statement::Work_Run(uv_work_t* req) { ...@@ -464,39 +497,42 @@ void Statement::Work_Run(uv_work_t* req) {
sqlite3_mutex_leave(mtx); sqlite3_mutex_leave(mtx);
} }
void Statement::Work_AfterRun(uv_work_t* req) { void Statement::Work_AfterRun(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
STATEMENT_INIT(RunBaton); STATEMENT_INIT(RunBaton);
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) { if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) {
Error(baton); Error(baton);
} }
else { else {
// Fire callbacks. // Fire callbacks.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Nan::Set(stmt->handle(), Nan::New("lastID").ToLocalChecked(), Nan::New<Number>(baton->inserted_id)); (stmt->Value()).Set(Napi::String::New(env, "lastID"), Napi::Number::New(env, baton->inserted_id));
Nan::Set(stmt->handle(), Nan::New("changes").ToLocalChecked(), Nan::New(baton->changes)); (stmt->Value()).Set( Napi::String::New(env, "changes"), Napi::Number::New(env, baton->changes));
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(stmt->handle(), cb, 1, argv); TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
} }
} }
STATEMENT_END(); STATEMENT_END();
} }
NAN_METHOD(Statement::All) { Napi::Value Statement::All(const Napi::CallbackInfo& info) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This()); Napi::Env env = info.Env();
Statement* stmt = this;
Baton* baton = stmt->Bind<RowsBaton>(info); Baton* baton = stmt->Bind<RowsBaton>(info);
if (baton == NULL) { if (baton == NULL) {
return Nan::ThrowError("Data type is not supported"); Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
} }
else { else {
stmt->Schedule(Work_BeginAll, baton); stmt->Schedule(Work_BeginAll, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
} }
...@@ -504,7 +540,7 @@ void Statement::Work_BeginAll(Baton* baton) { ...@@ -504,7 +540,7 @@ void Statement::Work_BeginAll(Baton* baton) {
STATEMENT_BEGIN(All); STATEMENT_BEGIN(All);
} }
void Statement::Work_All(uv_work_t* req) { void Statement::Work_All(napi_env e, void* data) {
STATEMENT_INIT(RowsBaton); STATEMENT_INIT(RowsBaton);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle); sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
...@@ -530,38 +566,39 @@ void Statement::Work_All(uv_work_t* req) { ...@@ -530,38 +566,39 @@ void Statement::Work_All(uv_work_t* req) {
sqlite3_mutex_leave(mtx); sqlite3_mutex_leave(mtx);
} }
void Statement::Work_AfterAll(uv_work_t* req) { void Statement::Work_AfterAll(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
STATEMENT_INIT(RowsBaton); STATEMENT_INIT(RowsBaton);
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
if (stmt->status != SQLITE_DONE) { if (stmt->status != SQLITE_DONE) {
Error(baton); Error(baton);
} }
else { else {
// Fire callbacks. // Fire callbacks.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
if (baton->rows.size()) { if (baton->rows.size()) {
// Create the result array from the data we acquired. // Create the result array from the data we acquired.
Local<Array> result(Nan::New<Array>(baton->rows.size())); Napi::Array result(Napi::Array::New(env, baton->rows.size()));
Rows::const_iterator it = baton->rows.begin(); Rows::const_iterator it = baton->rows.begin();
Rows::const_iterator end = baton->rows.end(); Rows::const_iterator end = baton->rows.end();
for (int i = 0; it < end; ++it, i++) { for (int i = 0; it < end; ++it, i++) {
Nan::Set(result, i, RowToJS(*it)); (result).Set(i, RowToJS(env,*it));
delete *it; delete *it;
} }
Local<Value> argv[] = { Nan::Null(), result }; Napi::Value argv[] = { env.Null(), result };
TRY_CATCH_CALL(stmt->handle(), cb, 2, argv); TRY_CATCH_CALL(stmt->Value(), cb, 2, argv);
} }
else { else {
// There were no result rows. // There were no result rows.
Local<Value> argv[] = { Napi::Value argv[] = {
Nan::Null(), env.Null(),
Nan::New<Array>(0) Napi::Array::New(env, 0)
}; };
TRY_CATCH_CALL(stmt->handle(), cb, 2, argv); TRY_CATCH_CALL(stmt->Value(), cb, 2, argv);
} }
} }
} }
...@@ -569,24 +606,26 @@ void Statement::Work_AfterAll(uv_work_t* req) { ...@@ -569,24 +606,26 @@ void Statement::Work_AfterAll(uv_work_t* req) {
STATEMENT_END(); STATEMENT_END();
} }
NAN_METHOD(Statement::Each) { Napi::Value Statement::Each(const Napi::CallbackInfo& info) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This()); Napi::Env env = info.Env();
Statement* stmt = this;
int last = info.Length(); int last = info.Length();
Local<Function> completed; Napi::Function completed;
if (last >= 2 && info[last - 1]->IsFunction() && info[last - 2]->IsFunction()) { if (last >= 2 && info[last - 1].IsFunction() && info[last - 2].IsFunction()) {
completed = Local<Function>::Cast(info[--last]); completed = info[--last].As<Napi::Function>();
} }
EachBaton* baton = stmt->Bind<EachBaton>(info, 0, last); EachBaton* baton = stmt->Bind<EachBaton>(info, 0, last);
if (baton == NULL) { if (baton == NULL) {
return Nan::ThrowError("Data type is not supported"); Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
} }
else { else {
baton->completed.Reset(completed); baton->completed.Reset(completed, 1);
stmt->Schedule(Work_BeginEach, baton); stmt->Schedule(Work_BeginEach, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
} }
...@@ -595,13 +634,13 @@ void Statement::Work_BeginEach(Baton* baton) { ...@@ -595,13 +634,13 @@ void Statement::Work_BeginEach(Baton* baton) {
// the event loop. This prevents dangling events. // the event loop. This prevents dangling events.
EachBaton* each_baton = static_cast<EachBaton*>(baton); EachBaton* each_baton = static_cast<EachBaton*>(baton);
each_baton->async = new Async(each_baton->stmt, reinterpret_cast<uv_async_cb>(AsyncEach)); each_baton->async = new Async(each_baton->stmt, reinterpret_cast<uv_async_cb>(AsyncEach));
each_baton->async->item_cb.Reset(each_baton->callback); each_baton->async->item_cb.Reset(each_baton->callback.Value(), 1);
each_baton->async->completed_cb.Reset(each_baton->completed); each_baton->async->completed_cb.Reset(each_baton->completed.Value(), 1);
STATEMENT_BEGIN(Each); STATEMENT_BEGIN(Each);
} }
void Statement::Work_Each(uv_work_t* req) { void Statement::Work_Each(napi_env e, void* data) {
STATEMENT_INIT(EachBaton); STATEMENT_INIT(EachBaton);
Async* async = baton->async; Async* async = baton->async;
...@@ -651,11 +690,12 @@ void Statement::CloseCallback(uv_handle_t* handle) { ...@@ -651,11 +690,12 @@ void Statement::CloseCallback(uv_handle_t* handle) {
delete async; delete async;
} }
void Statement::AsyncEach(uv_async_t* handle, int status) { void Statement::AsyncEach(uv_async_t* handle) {
Nan::HandleScope scope;
Async* async = static_cast<Async*>(handle->data); Async* async = static_cast<Async*>(handle->data);
Napi::Env env = async->stmt->Env();
Napi::HandleScope scope(env);
while (true) { while (true) {
// Get the contents out of the data cache for us to process in the JS callback. // Get the contents out of the data cache for us to process in the JS callback.
Rows rows; Rows rows;
...@@ -667,41 +707,42 @@ void Statement::AsyncEach(uv_async_t* handle, int status) { ...@@ -667,41 +707,42 @@ void Statement::AsyncEach(uv_async_t* handle, int status) {
break; break;
} }
Local<Function> cb = Nan::New(async->item_cb); Napi::Function cb = async->item_cb.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[2]; Napi::Value argv[2];
argv[0] = Nan::Null(); argv[0] = env.Null();
Rows::const_iterator it = rows.begin(); Rows::const_iterator it = rows.begin();
Rows::const_iterator end = rows.end(); Rows::const_iterator end = rows.end();
for (int i = 0; it < end; ++it, i++) { for (int i = 0; it < end; ++it, i++) {
argv[1] = RowToJS(*it); argv[1] = RowToJS(env,*it);
async->retrieved++; async->retrieved++;
TRY_CATCH_CALL(async->stmt->handle(), cb, 2, argv); TRY_CATCH_CALL(async->stmt->Value(), cb, 2, argv);
delete *it; delete *it;
} }
} }
} }
Local<Function> cb = Nan::New(async->completed_cb); Napi::Function cb = async->completed_cb.Value();
if (async->completed) { if (async->completed) {
if (!cb.IsEmpty() && if (!cb.IsEmpty() &&
cb->IsFunction()) { cb.IsFunction()) {
Local<Value> argv[] = { Napi::Value argv[] = {
Nan::Null(), env.Null(),
Nan::New(async->retrieved) Napi::Number::New(env, async->retrieved)
}; };
TRY_CATCH_CALL(async->stmt->handle(), cb, 2, argv); TRY_CATCH_CALL(async->stmt->Value(), cb, 2, argv);
} }
uv_close(reinterpret_cast<uv_handle_t*>(handle), CloseCallback); uv_close(reinterpret_cast<uv_handle_t*>(handle), CloseCallback);
} }
} }
void Statement::Work_AfterEach(uv_work_t* req) { void Statement::Work_AfterEach(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
STATEMENT_INIT(EachBaton); STATEMENT_INIT(EachBaton);
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
if (stmt->status != SQLITE_DONE) { if (stmt->status != SQLITE_DONE) {
Error(baton); Error(baton);
} }
...@@ -709,74 +750,76 @@ void Statement::Work_AfterEach(uv_work_t* req) { ...@@ -709,74 +750,76 @@ void Statement::Work_AfterEach(uv_work_t* req) {
STATEMENT_END(); STATEMENT_END();
} }
NAN_METHOD(Statement::Reset) { Napi::Value Statement::Reset(const Napi::CallbackInfo& info) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This()); Napi::Env env = info.Env();
Statement* stmt = this;
OPTIONAL_ARGUMENT_FUNCTION(0, callback); OPTIONAL_ARGUMENT_FUNCTION(0, callback);
Baton* baton = new Baton(stmt, callback); Baton* baton = new Baton(stmt, callback);
stmt->Schedule(Work_BeginReset, baton); stmt->Schedule(Work_BeginReset, baton);
info.GetReturnValue().Set(info.This()); return info.This();
} }
void Statement::Work_BeginReset(Baton* baton) { void Statement::Work_BeginReset(Baton* baton) {
STATEMENT_BEGIN(Reset); STATEMENT_BEGIN(Reset);
} }
void Statement::Work_Reset(uv_work_t* req) { void Statement::Work_Reset(napi_env e, void* data) {
STATEMENT_INIT(Baton); STATEMENT_INIT(Baton);
sqlite3_reset(stmt->_handle); sqlite3_reset(stmt->_handle);
stmt->status = SQLITE_OK; stmt->status = SQLITE_OK;
} }
void Statement::Work_AfterReset(uv_work_t* req) { void Statement::Work_AfterReset(napi_env e, napi_status status, void* data) {
Nan::HandleScope scope;
STATEMENT_INIT(Baton); STATEMENT_INIT(Baton);
Napi::Env env = stmt->Env();
Napi::HandleScope scope(env);
// Fire callbacks. // Fire callbacks.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
Local<Value> argv[] = { Nan::Null() }; Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(stmt->handle(), cb, 1, argv); TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
} }
STATEMENT_END(); STATEMENT_END();
} }
Local<Object> Statement::RowToJS(Row* row) { Napi::Value Statement::RowToJS(Napi::Env env, Row* row) {
Nan::EscapableHandleScope scope; Napi::EscapableHandleScope scope(env);
Local<Object> result = Nan::New<Object>(); Napi::Object result = Napi::Object::New(env);
Row::const_iterator it = row->begin(); Row::const_iterator it = row->begin();
Row::const_iterator end = row->end(); Row::const_iterator end = row->end();
for (int i = 0; it < end; ++it, i++) { for (int i = 0; it < end; ++it, i++) {
Values::Field* field = *it; Values::Field* field = *it;
Local<Value> value; Napi::Value value;
switch (field->type) { switch (field->type) {
case SQLITE_INTEGER: { case SQLITE_INTEGER: {
value = Nan::New<Number>(((Values::Integer*)field)->value); value = Napi::Number::New(env, ((Values::Integer*)field)->value);
} break; } break;
case SQLITE_FLOAT: { case SQLITE_FLOAT: {
value = Nan::New<Number>(((Values::Float*)field)->value); value = Napi::Number::New(env, ((Values::Float*)field)->value);
} break; } break;
case SQLITE_TEXT: { case SQLITE_TEXT: {
value = Nan::New<String>(((Values::Text*)field)->value.c_str(), ((Values::Text*)field)->value.size()).ToLocalChecked(); value = Napi::String::New(env, ((Values::Text*)field)->value.c_str(), ((Values::Text*)field)->value.size());
} break; } break;
case SQLITE_BLOB: { case SQLITE_BLOB: {
value = Nan::CopyBuffer(((Values::Blob*)field)->value, ((Values::Blob*)field)->length).ToLocalChecked(); value = Napi::Buffer<char>::Copy(env, ((Values::Blob*)field)->value, ((Values::Blob*)field)->length);
} break; } break;
case SQLITE_NULL: { case SQLITE_NULL: {
value = Nan::Null(); value = env.Null();
} break; } break;
} }
Nan::Set(result, Nan::New(field->name.c_str()).ToLocalChecked(), value); (result).Set(Napi::String::New(env, field->name.c_str()), value);
DELETE_FIELD(field); DELETE_FIELD(field);
} }
...@@ -816,31 +859,33 @@ void Statement::GetRow(Row* row, sqlite3_stmt* stmt) { ...@@ -816,31 +859,33 @@ void Statement::GetRow(Row* row, sqlite3_stmt* stmt) {
} }
} }
NAN_METHOD(Statement::Finalize) { Napi::Value Statement::Finalize_(const Napi::CallbackInfo& info) {
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This()); Napi::Env env = info.Env();
Statement* stmt = this;
OPTIONAL_ARGUMENT_FUNCTION(0, callback); OPTIONAL_ARGUMENT_FUNCTION(0, callback);
Baton* baton = new Baton(stmt, callback); Baton* baton = new Baton(stmt, callback);
stmt->Schedule(Finalize, baton); stmt->Schedule(Finalize_, baton);
info.GetReturnValue().Set(stmt->db->handle()); return stmt->db->Value();
} }
void Statement::Finalize(Baton* baton) { void Statement::Finalize_(Baton* baton) {
Nan::HandleScope scope; Napi::Env env = baton->stmt->Env();
Napi::HandleScope scope(env);
baton->stmt->Finalize(); baton->stmt->Finalize_();
// Fire callback in case there was one. // Fire callback in case there was one.
Local<Function> cb = Nan::New(baton->callback); Napi::Function cb = baton->callback.Value();
if (!cb.IsEmpty() && cb->IsFunction()) { if (!cb.IsUndefined() && cb.IsFunction()) {
TRY_CATCH_CALL(baton->stmt->handle(), cb, 0, NULL); TRY_CATCH_CALL(baton->stmt->Value(), cb, 0, NULL);
} }
delete baton; delete baton;
} }
void Statement::Finalize() { void Statement::Finalize_() {
assert(!finalized); assert(!finalized);
finalized = true; finalized = true;
CleanQueue(); CleanQueue();
...@@ -852,13 +897,14 @@ void Statement::Finalize() { ...@@ -852,13 +897,14 @@ void Statement::Finalize() {
} }
void Statement::CleanQueue() { void Statement::CleanQueue() {
Nan::HandleScope scope; Napi::Env env = this->Env();
Napi::HandleScope scope(env);
if (prepared && !queue.empty()) { if (prepared && !queue.empty()) {
// This statement has already been prepared and is now finalized. // This statement has already been prepared and is now finalized.
// Fire error for all remaining items in the queue. // Fire error for all remaining items in the queue.
EXCEPTION("Statement is already finalized", SQLITE_MISUSE, exception); EXCEPTION(Napi::String::New(env, "Statement is already finalized"), SQLITE_MISUSE, exception);
Local<Value> argv[] = { exception }; Napi::Value argv[] = { exception };
bool called = false; bool called = false;
// Clear out the queue so that this object can get GC'ed. // Clear out the queue so that this object can get GC'ed.
...@@ -866,11 +912,11 @@ void Statement::CleanQueue() { ...@@ -866,11 +912,11 @@ void Statement::CleanQueue() {
Call* call = queue.front(); Call* call = queue.front();
queue.pop(); queue.pop();
Local<Function> cb = Nan::New(call->baton->callback); Napi::Function cb = call->baton->callback.Value();
if (prepared && !cb.IsEmpty() && if (prepared && !cb.IsEmpty() &&
cb->IsFunction()) { cb.IsFunction()) {
TRY_CATCH_CALL(handle(), cb, 1, argv); TRY_CATCH_CALL(Value(), cb, 1, argv);
called = true; called = true;
} }
...@@ -883,8 +929,8 @@ void Statement::CleanQueue() { ...@@ -883,8 +929,8 @@ void Statement::CleanQueue() {
// When we couldn't call a callback function, emit an error on the // When we couldn't call a callback function, emit an error on the
// Statement object. // Statement object.
if (!called) { if (!called) {
Local<Value> info[] = { Nan::New("error").ToLocalChecked(), exception }; Napi::Value info[] = { Napi::String::New(env, "error"), exception };
EMIT_EVENT(handle(), 2, info); EMIT_EVENT(Value(), 2, info);
} }
} }
else while (!queue.empty()) { else while (!queue.empty()) {
......
#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