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
87e12892
Commit
87e12892
authored
Feb 23, 2011
by
Konstantin Käfer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Statement#lastID and Statement#changes on .run() queries
parent
9f6496b9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
3 deletions
+49
-3
statement.cc
src/statement.cc
+10
-3
statement.h
src/statement.h
+7
-0
affected.test.js
test/affected.test.js
+32
-0
No files found.
src/statement.cc
View file @
87e12892
...
@@ -434,7 +434,7 @@ Handle<Value> Statement::Run(const Arguments& args) {
...
@@ -434,7 +434,7 @@ Handle<Value> Statement::Run(const Arguments& args) {
HandleScope
scope
;
HandleScope
scope
;
Statement
*
stmt
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
Statement
*
stmt
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
Baton
*
baton
=
stmt
->
Bind
<
Baton
>
(
args
);
Baton
*
baton
=
stmt
->
Bind
<
Run
Baton
>
(
args
);
if
(
baton
==
NULL
)
{
if
(
baton
==
NULL
)
{
return
ThrowException
(
Exception
::
Error
(
String
::
New
(
"Data type is not supported"
)));
return
ThrowException
(
Exception
::
Error
(
String
::
New
(
"Data type is not supported"
)));
}
}
...
@@ -449,7 +449,7 @@ void Statement::EIO_BeginRun(Baton* baton) {
...
@@ -449,7 +449,7 @@ void Statement::EIO_BeginRun(Baton* baton) {
}
}
int
Statement
::
EIO_Run
(
eio_req
*
req
)
{
int
Statement
::
EIO_Run
(
eio_req
*
req
)
{
STATEMENT_INIT
(
Baton
);
STATEMENT_INIT
(
Run
Baton
);
sqlite3_mutex
*
mtx
=
sqlite3_db_mutex
(
stmt
->
db
->
handle
);
sqlite3_mutex
*
mtx
=
sqlite3_db_mutex
(
stmt
->
db
->
handle
);
sqlite3_mutex_enter
(
mtx
);
sqlite3_mutex_enter
(
mtx
);
...
@@ -465,6 +465,10 @@ int Statement::EIO_Run(eio_req *req) {
...
@@ -465,6 +465,10 @@ int Statement::EIO_Run(eio_req *req) {
if
(
!
(
stmt
->
status
==
SQLITE_ROW
||
stmt
->
status
==
SQLITE_DONE
))
{
if
(
!
(
stmt
->
status
==
SQLITE_ROW
||
stmt
->
status
==
SQLITE_DONE
))
{
stmt
->
message
=
std
::
string
(
sqlite3_errmsg
(
stmt
->
db
->
handle
));
stmt
->
message
=
std
::
string
(
sqlite3_errmsg
(
stmt
->
db
->
handle
));
}
}
else
{
baton
->
inserted_id
=
sqlite3_last_insert_rowid
(
stmt
->
db
->
handle
);
baton
->
changes
=
sqlite3_changes
(
stmt
->
db
->
handle
);
}
}
}
sqlite3_mutex_leave
(
mtx
);
sqlite3_mutex_leave
(
mtx
);
...
@@ -474,7 +478,7 @@ int Statement::EIO_Run(eio_req *req) {
...
@@ -474,7 +478,7 @@ int Statement::EIO_Run(eio_req *req) {
int
Statement
::
EIO_AfterRun
(
eio_req
*
req
)
{
int
Statement
::
EIO_AfterRun
(
eio_req
*
req
)
{
HandleScope
scope
;
HandleScope
scope
;
STATEMENT_INIT
(
Baton
);
STATEMENT_INIT
(
Run
Baton
);
if
(
stmt
->
status
!=
SQLITE_ROW
&&
stmt
->
status
!=
SQLITE_DONE
)
{
if
(
stmt
->
status
!=
SQLITE_ROW
&&
stmt
->
status
!=
SQLITE_DONE
)
{
Error
(
baton
);
Error
(
baton
);
...
@@ -482,6 +486,9 @@ int Statement::EIO_AfterRun(eio_req *req) {
...
@@ -482,6 +486,9 @@ int Statement::EIO_AfterRun(eio_req *req) {
else
{
else
{
// Fire callbacks.
// Fire callbacks.
if
(
!
baton
->
callback
.
IsEmpty
()
&&
baton
->
callback
->
IsFunction
())
{
if
(
!
baton
->
callback
.
IsEmpty
()
&&
baton
->
callback
->
IsFunction
())
{
stmt
->
handle_
->
Set
(
String
::
NewSymbol
(
"lastID"
),
Local
<
Integer
>
(
Integer
::
New
(
baton
->
inserted_id
)));
stmt
->
handle_
->
Set
(
String
::
NewSymbol
(
"changes"
),
Local
<
Integer
>
(
Integer
::
New
(
baton
->
changes
)));
Local
<
Value
>
argv
[]
=
{
Local
<
Value
>::
New
(
Null
())
};
Local
<
Value
>
argv
[]
=
{
Local
<
Value
>::
New
(
Null
())
};
TRY_CATCH_CALL
(
stmt
->
handle_
,
baton
->
callback
,
1
,
argv
);
TRY_CATCH_CALL
(
stmt
->
handle_
,
baton
->
callback
,
1
,
argv
);
}
}
...
...
src/statement.h
View file @
87e12892
...
@@ -101,6 +101,13 @@ public:
...
@@ -101,6 +101,13 @@ public:
Data
::
Row
row
;
Data
::
Row
row
;
};
};
static
struct
RunBaton
:
Baton
{
RunBaton
(
Statement
*
stmt_
,
Handle
<
Function
>
cb_
)
:
Baton
(
stmt_
,
cb_
),
inserted_id
(
0
),
changes
(
0
)
{}
sqlite3_int64
inserted_id
;
int
changes
;
};
static
struct
RowsBaton
:
Baton
{
static
struct
RowsBaton
:
Baton
{
RowsBaton
(
Statement
*
stmt_
,
Handle
<
Function
>
cb_
)
:
RowsBaton
(
Statement
*
stmt_
,
Handle
<
Function
>
cb_
)
:
Baton
(
stmt_
,
cb_
)
{}
Baton
(
stmt_
,
cb_
)
{}
...
...
test/affected.test.js
0 → 100644
View file @
87e12892
var
sqlite3
=
require
(
'sqlite3'
);
var
assert
=
require
(
'assert'
);
exports
[
'test row changes and lastID'
]
=
function
(
beforeExit
)
{
var
db
=
new
sqlite3
.
Database
(
':memory:'
);
var
finished
=
false
;
db
.
serialize
(
function
()
{
db
.
run
(
"CREATE TABLE foo (id INT, txt TEXT)"
);
var
stmt
=
db
.
prepare
(
"INSERT INTO foo VALUES(?, ?)"
);
var
j
=
1
;
for
(
var
i
=
0
;
i
<
1000
;
i
++
)
{
stmt
.
run
(
i
,
"demo"
,
function
(
err
)
{
if
(
err
)
throw
err
;
// Relies on SQLite's row numbering to be gapless and starting
// from 1.
assert
.
equal
(
j
++
,
this
.
lastID
);
});
}
db
.
run
(
"UPDATE foo SET id = id + 1 WHERE id % 2 = 0"
,
function
(
err
)
{
if
(
err
)
throw
err
;
assert
.
equal
(
500
,
this
.
changes
);
finished
=
true
;
});
});
beforeExit
(
function
()
{
assert
.
ok
(
finished
);
})
}
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