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
2cbaf25a
Commit
2cbaf25a
authored
Sep 28, 2010
by
Orlando Vazquez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a Database#execute that, given some SQL, fetches all rows.
parent
82c2a674
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
10 deletions
+82
-10
README.md
README.md
+15
-1
sqlite.js
sqlite.js
+48
-0
statement.cc
src/statement.cc
+19
-9
No files found.
README.md
View file @
2cbaf25a
...
@@ -20,9 +20,17 @@ currently made.
...
@@ -20,9 +20,17 @@ currently made.
db.open("aquateen.db", function (error) {
db.open("aquateen.db", function (error) {
if (error) {
if (error) {
console.log("
Purple Alert! Aqua Teen Database unabled to be opened!
"));
console.log("
Tonight. You.
"));
throw error;
throw error;
}
}
db.execute
( "INSERT INTO aqua_teens (name) VALUES (?)"
, ['meaty meaty moo']
, function (error, rows) {
if (error) throw error;
console.log("Aqua teen added.");
}
);
var sql = 'SELECT name FROM dudes WHERE type = ? AND age > ?';
var sql = 'SELECT name FROM dudes WHERE type = ? AND age > ?';
db.prepare(sql, function (error, statement) {
db.prepare(sql, function (error, statement) {
...
@@ -59,6 +67,12 @@ A filename of ":memory:" may be used to create an in-memory database.
...
@@ -59,6 +67,12 @@ A filename of ":memory:" may be used to create an in-memory database.
Close the database handle.
Close the database handle.
### database.execute(sql[, bindings], function (error, rows) {})
Execute a SQL query,
`sql`
with optional bindings
`bindings`
on the currently
opened database. The callback will be executed once with all the rows returned
for the query. This is much faster than
`database.query`
since there are less roundtrips into the thread-pool.
### database.query(sql, [bindings,] function (error, row) {})
### database.query(sql, [bindings,] function (error, row) {})
Execute a SQL query,
`sql`
, with optional bindings
`bindings`
on the currently
Execute a SQL query,
`sql`
, with optional bindings
`bindings`
on the currently
...
...
sqlite.js
View file @
2cbaf25a
...
@@ -82,6 +82,54 @@ function _doStep(db, statement, rowCallback) {
...
@@ -82,6 +82,54 @@ function _doStep(db, statement, rowCallback) {
});
});
}
}
// Execute a single SQL query with the given optional parameters. Calls
// `callback` with all rows or an error on query completion.
Database
.
prototype
.
execute
=
function
(
sql
/* , bindings, callback */
)
{
var
self
=
this
;
var
bindings
,
callback
;
var
n
=
arguments
.
length
;
switch
(
n
)
{
case
3
:
callback
=
arguments
[
2
];
bindings
=
arguments
[
1
];
break
;
case
2
:
callback
=
arguments
[
1
];
break
;
default
:
throw
new
Error
(
"Invalid number of arguments ("
+
n
+
")"
);
}
self
.
prepare
(
sql
,
function
(
error
,
statement
)
{
if
(
error
)
{
return
callback
(
error
);
}
if
(
bindings
)
{
statement
.
bind
(
bindings
,
function
(
error
)
{
if
(
error
)
{
return
callback
(
new
Error
(
"Binding error: "
+
error
.
toString
()));
}
fetchAll
(
statement
);
});
}
else
{
fetchAll
(
statement
);
}
function
fetchAll
(
statement
)
{
statement
.
fetchAll
(
function
(
error
,
rows
)
{
if
(
error
)
{
return
callback
(
error
);
}
statement
.
finalize
(
function
()
{
callback
(
undefined
,
rows
);
});
});
}
});
}
// Execute SQL statements separated by semi-colons.
// Execute SQL statements separated by semi-colons.
// SQL must contain no placeholders. Results are discarded.
// SQL must contain no placeholders. Results are discarded.
Database
.
prototype
.
executeScript
=
function
(
script
,
callback
)
{
Database
.
prototype
.
executeScript
=
function
(
script
,
callback
)
{
...
...
src/statement.cc
View file @
2cbaf25a
...
@@ -820,18 +820,28 @@ int Statement::EIO_FetchAll(eio_req *req) {
...
@@ -820,18 +820,28 @@ int Statement::EIO_FetchAll(eio_req *req) {
assert
(
stmt
);
assert
(
stmt
);
int
ret
;
int
ret
;
/* open the pool */
fetchall_req
->
pool
=
mpool_open
(
MPOOL_FLAG_USE_MAP_ANON
int
rc
=
sqlite3_step
(
stmt
);
,
0
,
NULL
if
(
rc
==
SQLITE_ROW
)
{
,
&
ret
);
/* open the pool */
if
(
fetchall_req
->
pool
==
NULL
)
{
fetchall_req
->
pool
=
mpool_open
(
MPOOL_FLAG_USE_MAP_ANON
req
->
result
=
-
1
;
,
0
,
NULL
,
&
ret
);
if
(
fetchall_req
->
pool
==
NULL
)
{
req
->
result
=
-
1
;
fetchall_req
->
rows
=
NULL
;
fetchall_req
->
error
=
(
char
*
)
mpool_strerror
(
ret
);
return
0
;
}
}
else
{
fetchall_req
->
pool
=
NULL
;
fetchall_req
->
rows
=
NULL
;
fetchall_req
->
rows
=
NULL
;
fetchall_req
->
error
=
(
char
*
)
mpool_strerror
(
ret
);
return
0
;
}
}
// We're going to be traversing a linked list in two dimensions.
// We're going to be traversing a linked list in two dimensions.
struct
row_node
*
cur
=
NULL
struct
row_node
*
cur
=
NULL
,
*
prev
=
NULL
,
*
prev
=
NULL
...
...
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