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
e3e2d829
Commit
e3e2d829
authored
Dec 12, 2009
by
Eric Fredricksen
Committed by
Eric Fredricksen
Dec 12, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix events.
parent
f384e879
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
53 deletions
+80
-53
sqlite.js
sqlite.js
+4
-0
sqlite3_bindings.cc
sqlite3_bindings.cc
+51
-52
test.js
test.js
+25
-1
No files found.
sqlite.js
View file @
e3e2d829
...
...
@@ -24,6 +24,10 @@ process.mixin(exports, bindings);
// Conform somewhat to http://dev.w3.org/html5/webdatabase/#sql
exports
.
SQLITE_DELETE
=
9
;
exports
.
SQLITE_INSERT
=
18
;
exports
.
SQLITE_UPDATE
=
23
;
exports
.
openDatabaseSync
=
function
(
name
,
version
,
displayName
,
estimatedSize
,
creationCallback
)
{
...
...
sqlite3_bindings.cc
View file @
e3e2d829
...
...
@@ -21,6 +21,45 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
using
namespace
v8
;
using
namespace
node
;
#define CHECK(rc) { if ((rc) != SQLITE_OK) \
return ThrowException(Exception::Error(String::New( \
sqlite3_errmsg(*db)))); }
#define SCHECK(rc) { if ((rc) != SQLITE_OK) \
return ThrowException(Exception::Error(String::New( \
sqlite3_errmsg(sqlite3_db_handle(*stmt))))); }
#define REQ_ARGS(N) \
if (args.Length() < (N)) \
return ThrowException(Exception::TypeError( \
String::New("Expected " #N "arguments")));
#define REQ_STR_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsString()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be a string"))); \
String::Utf8Value VAR(args[I]->ToString());
#define REQ_EXT_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsExternal()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " invalid"))); \
Local<External> VAR = Local<External>::Cast(args[I]);
#define OPT_INT_ARG(I, VAR, DEFAULT) \
int VAR; \
if (args.Length() <= (I)) { \
VAR = (DEFAULT); \
} else if (args[I]->IsInt32()) { \
VAR = args[I]->Int32Value(); \
} else { \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be an integer"))); \
}
class
Sqlite3Db
:
public
EventEmitter
{
public
:
...
...
@@ -44,9 +83,6 @@ public:
}
protected
:
Sqlite3Db
()
:
db_
(
NULL
)
{
}
Sqlite3Db
(
sqlite3
*
db
)
:
db_
(
db
)
{
}
...
...
@@ -61,61 +97,22 @@ protected:
protected
:
static
Handle
<
Value
>
New
(
const
Arguments
&
args
)
{
HandleScope
scope
;
if
(
args
.
Length
()
==
0
||
!
args
[
0
]
->
IsString
())
{
return
ThrowException
(
Exception
::
TypeError
(
String
::
New
(
"First argument must be a string"
)));
}
String
::
Utf8Value
filename
(
args
[
0
]
->
ToString
());
REQ_STR_ARG
(
0
,
filename
);
sqlite3
*
db
;
int
rc
=
sqlite3_open
(
*
filename
,
&
db
);
if
(
rc
)
{
Local
<
String
>
err
=
v8
::
String
::
New
(
sqlite3_errmsg
(
db
));
sqlite3_close
(
db
);
return
ThrowException
(
Exception
::
Error
(
err
));
}
(
new
Sqlite3Db
(
db
))
->
Wrap
(
args
.
This
());
return
args
.
This
();
}
#define CHECK(rc) { if ((rc) != SQLITE_OK) \
return ThrowException(Exception::Error(String::New( \
sqlite3_errmsg(*db)))); }
#define SCHECK(rc) { if ((rc) != SQLITE_OK) \
return ThrowException(Exception::Error(String::New( \
sqlite3_errmsg(sqlite3_db_handle(*stmt))))); }
if
(
rc
)
return
ThrowException
(
Exception
::
Error
(
String
::
New
(
"Error opening database"
)));
Sqlite3Db
*
dbo
=
new
Sqlite3Db
(
db
);
dbo
->
Wrap
(
args
.
This
());
#define REQ_ARGS(N) \
if (args.Length() < (N)) \
return ThrowException(Exception::TypeError( \
String::New("Expected " #N "arguments"))); \
sqlite3_commit_hook
(
db
,
CommitHook
,
dbo
);
sqlite3_rollback_hook
(
db
,
RollbackHook
,
dbo
);
sqlite3_update_hook
(
db
,
UpdateHook
,
dbo
);
#define REQ_STR_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsString()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be a string"))); \
String::Utf8Value VAR(args[I]->ToString());
#define REQ_EXT_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsExternal()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " invalid"))); \
Local<External> VAR = Local<External>::Cast(args[I]);
#define OPT_INT_ARG(I, VAR, DEFAULT) \
int VAR; \
if (args.Length() <= (I)) { \
VAR = (DEFAULT); \
} else if (args[I]->IsInt32()) { \
VAR = args[I]->Int32Value(); \
} else { \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be an integer"))); \
return
args
.
This
();
}
//
// JS DatabaseSync bindings
//
...
...
@@ -165,6 +162,7 @@ protected:
db
->
Emit
(
"update"
,
4
,
args
);
}
/*
static Handle<Value> Open(const Arguments& args) {
HandleScope scope;
Sqlite3Db* db = ObjectWrap::Unwrap<Sqlite3Db>(args.This());
...
...
@@ -178,6 +176,7 @@ protected:
return args.This();
}
*/
static
Handle
<
Value
>
Prepare
(
const
Arguments
&
args
)
{
HandleScope
scope
;
...
...
test.js
View file @
e3e2d829
...
...
@@ -16,6 +16,26 @@ function asserteq(v1, v2) {
var
db
=
sqlite
.
openDatabaseSync
(
'test.db'
);
var
commits
=
0
;
var
rollbacks
=
0
;
var
updates
=
0
;
db
.
addListener
(
"commit"
,
function
()
{
commits
++
;
});
db
.
addListener
(
"rollback"
,
function
()
{
sys
.
puts
(
"ROLLBACK"
);
rollbacks
++
;
});
db
.
addListener
(
"update"
,
function
(
operation
,
database
,
table
,
rowid
)
{
//sys.puts("update " + operation + " " + database + "." + table + " " + rowid);
updates
++
;
});
db
.
query
(
"CREATE TABLE egg (a,y,e)"
);
db
.
query
(
"INSERT INTO egg (a) VALUES (1)"
,
function
()
{
process
.
assert
(
this
.
insertId
==
1
);
...
...
@@ -46,7 +66,6 @@ db.query("SELECT e FROM egg WHERE a = ?", [5], function (es) {
});
db
.
transaction
(
function
(
tx
)
{
tx
.
executeSql
(
"CREATE TABLE tex (t,e,x)"
);
var
i
=
tx
.
executeSql
(
"INSERT INTO tex (t,e,x) VALUES (?,?,?)"
,
...
...
@@ -89,6 +108,11 @@ try {
}
sys
.
puts
(
"commits: "
+
commits
);
sys
.
puts
(
"rollbacks: "
+
rollbacks
);
sys
.
puts
(
"updates: "
+
updates
);
db
.
close
();
sys
.
puts
(
"OK
\
n"
);
...
...
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