Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sqlite3
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
node-sqlite3
Commits
a76fbf83
Commit
a76fbf83
authored
Mar 18, 2011
by
Konstantin Käfer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add extension loading support
parent
7ffb96ea
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
1 deletions
+103
-1
database.cc
src/database.cc
+73
-0
database.h
src/database.h
+12
-1
extension.test.js
test/extension.test.js
+18
-0
No files found.
src/database.cc
View file @
a76fbf83
...
...
@@ -23,6 +23,7 @@ void Database::Init(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD
(
constructor_template
,
"close"
,
Close
);
NODE_SET_PROTOTYPE_METHOD
(
constructor_template
,
"exec"
,
Exec
);
NODE_SET_PROTOTYPE_METHOD
(
constructor_template
,
"loadExtension"
,
LoadExtension
);
NODE_SET_PROTOTYPE_METHOD
(
constructor_template
,
"serialize"
,
Serialize
);
NODE_SET_PROTOTYPE_METHOD
(
constructor_template
,
"parallelize"
,
Parallelize
);
NODE_SET_PROTOTYPE_METHOD
(
constructor_template
,
"configure"
,
Configure
);
...
...
@@ -437,6 +438,78 @@ int Database::EIO_AfterExec(eio_req *req) {
return
0
;
}
Handle
<
Value
>
Database
::
LoadExtension
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Database
*
db
=
ObjectWrap
::
Unwrap
<
Database
>
(
args
.
This
());
REQUIRE_ARGUMENT_STRING
(
0
,
filename
);
OPTIONAL_ARGUMENT_FUNCTION
(
1
,
callback
);
Baton
*
baton
=
new
LoadExtensionBaton
(
db
,
callback
,
*
filename
);
db
->
Schedule
(
EIO_BeginLoadExtension
,
baton
,
true
);
return
args
.
This
();
}
void
Database
::
EIO_BeginLoadExtension
(
Baton
*
baton
)
{
assert
(
baton
->
db
->
locked
);
assert
(
baton
->
db
->
open
);
assert
(
baton
->
db
->
handle
);
assert
(
baton
->
db
->
pending
==
0
);
eio_custom
(
EIO_LoadExtension
,
EIO_PRI_DEFAULT
,
EIO_AfterLoadExtension
,
baton
);
}
int
Database
::
EIO_LoadExtension
(
eio_req
*
req
)
{
LoadExtensionBaton
*
baton
=
static_cast
<
LoadExtensionBaton
*>
(
req
->
data
);
sqlite3_enable_load_extension
(
baton
->
db
->
handle
,
1
);
char
*
message
=
NULL
;
baton
->
status
=
sqlite3_load_extension
(
baton
->
db
->
handle
,
baton
->
filename
.
c_str
(),
0
,
&
message
);
sqlite3_enable_load_extension
(
baton
->
db
->
handle
,
0
);
if
(
baton
->
status
!=
SQLITE_OK
&&
message
!=
NULL
)
{
baton
->
message
=
std
::
string
(
message
);
sqlite3_free
(
message
);
}
return
0
;
}
int
Database
::
EIO_AfterLoadExtension
(
eio_req
*
req
)
{
HandleScope
scope
;
LoadExtensionBaton
*
baton
=
static_cast
<
LoadExtensionBaton
*>
(
req
->
data
);
Database
*
db
=
baton
->
db
;
if
(
baton
->
status
!=
SQLITE_OK
)
{
EXCEPTION
(
String
::
New
(
baton
->
message
.
c_str
()),
baton
->
status
,
exception
);
if
(
!
baton
->
callback
.
IsEmpty
()
&&
baton
->
callback
->
IsFunction
())
{
Local
<
Value
>
argv
[]
=
{
exception
};
TRY_CATCH_CALL
(
db
->
handle_
,
baton
->
callback
,
1
,
argv
);
}
else
{
Local
<
Value
>
args
[]
=
{
String
::
NewSymbol
(
"error"
),
exception
};
EMIT_EVENT
(
db
->
handle_
,
2
,
args
);
}
}
else
if
(
!
baton
->
callback
.
IsEmpty
()
&&
baton
->
callback
->
IsFunction
())
{
Local
<
Value
>
argv
[]
=
{
Local
<
Value
>::
New
(
Null
())
};
TRY_CATCH_CALL
(
db
->
handle_
,
baton
->
callback
,
1
,
argv
);
}
db
->
Process
();
delete
baton
;
return
0
;
}
/**
* Override this so that we can properly close the database when this object
* gets garbage collected.
...
...
src/database.h
View file @
a76fbf83
...
...
@@ -61,6 +61,12 @@ public:
Baton
(
db_
,
cb_
),
sql
(
sql_
)
{}
};
struct
LoadExtensionBaton
:
Baton
{
std
::
string
filename
;
LoadExtensionBaton
(
Database
*
db_
,
Handle
<
Function
>
cb_
,
const
char
*
filename_
)
:
Baton
(
db_
,
cb_
),
filename
(
filename_
)
{}
};
typedef
void
(
*
EIO_Callback
)(
Baton
*
baton
);
struct
Call
{
...
...
@@ -70,7 +76,7 @@ public:
bool
exclusive
;
Baton
*
baton
;
};
typedef
void
(
*
Async_Callback
)(
EV_P_
ev_async
*
w
,
int
revents
);
...
...
@@ -161,6 +167,11 @@ protected:
static
int
EIO_Close
(
eio_req
*
req
);
static
int
EIO_AfterClose
(
eio_req
*
req
);
static
Handle
<
Value
>
LoadExtension
(
const
Arguments
&
args
);
static
void
EIO_BeginLoadExtension
(
Baton
*
baton
);
static
int
EIO_LoadExtension
(
eio_req
*
req
);
static
int
EIO_AfterLoadExtension
(
eio_req
*
req
);
static
Handle
<
Value
>
Serialize
(
const
Arguments
&
args
);
static
Handle
<
Value
>
Parallelize
(
const
Arguments
&
args
);
...
...
test/extension.test.js
0 → 100644
View file @
a76fbf83
var
sqlite3
=
require
(
'sqlite3'
);
var
assert
=
require
(
'assert'
);
if
(
process
.
setMaxListeners
)
process
.
setMaxListeners
(
0
);
exports
[
'test loadExtension'
]
=
function
(
beforeExit
)
{
var
db
=
new
sqlite3
.
Database
(
':memory:'
);
var
completed
=
false
;
db
.
loadExtension
(
'/usr/local/lib/libspatialite.dylib'
,
function
(
err
)
{
if
(
err
)
throw
err
;
completed
=
true
;
});
beforeExit
(
function
()
{
assert
.
ok
(
completed
);
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment