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
6d53991a
Commit
6d53991a
authored
Sep 22, 2010
by
Orlando Vazquez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Database#executeScript and clean up sqlite.js
parent
fa95fda3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
64 deletions
+84
-64
README.md
README.md
+19
-2
sqlite.js
sqlite.js
+64
-62
common.js
tests/lib/common.js
+1
-0
No files found.
README.md
View file @
6d53991a
...
@@ -56,7 +56,7 @@ The low-level bindings directly interface with the SQLite C API. The API
...
@@ -56,7 +56,7 @@ The low-level bindings directly interface with the SQLite C API. The API
approximately matches the SQLite3 API when it makes sense.
approximately matches the SQLite3 API when it makes sense.
var sys = require('sys'),
var sys = require('sys'),
sqlite = require('sqlite
/sqlite3_bindings
');
sqlite = require('sqlite');
var db = new sqlite.Database();
var db = new sqlite.Database();
...
@@ -99,7 +99,7 @@ approximately matches the SQLite3 API when it makes sense.
...
@@ -99,7 +99,7 @@ approximately matches the SQLite3 API when it makes sense.
To create a new database object:
To create a new database object:
var db = sqlite
_bindings
.Database();
var db = sqlite.Database();
### database.open(filename, function (error) {})
### database.open(filename, function (error) {})
...
@@ -113,6 +113,23 @@ A filename of ":memory:" may be used to create an in-memory database.
...
@@ -113,6 +113,23 @@ A filename of ":memory:" may be used to create an in-memory database.
Close the database handle.
Close the database handle.
### database.executeScript(SQL, function (error) {});
db.executeScript
( "CREATE TABLE table1 (id, name);"
+ "CREATE TABLE table2 (id, age);"
+ "INSERT INTO table1 (1, 'Mister Shake');"
+ "INSER INTO table2 (1, 34);"
, function (error) {
if (error) throw error;
// ...
});
Execute multiple semi-colon separated SQL statements. Statements must take no
placeholders. Each statements will be executed with a single step() and then
reset. This is ideally suited to executing DDL statements that take no
arguments and return no results.
### database.prepare(SQL, [options,] function (error, statement) {})
### database.prepare(SQL, [options,] function (error, statement) {})
Create a prepared statement from an SQL string. Prepared statements can be
Create a prepared statement from an SQL string. Prepared statements can be
...
...
sqlite.js
View file @
6d53991a
...
@@ -18,14 +18,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
...
@@ -18,14 +18,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
var
sys
=
require
(
"sys"
);
var
sys
=
require
(
"sys"
);
var
sqlite
=
require
(
"./sqlite3_bindings"
);
var
sqlite
=
require
(
"./sqlite3_bindings"
);
// load numeric constants from sqlite3_bindings
for
(
prop
in
sqlite
)
{
var
obj
=
sqlite
[
prop
];
if
((
obj
===
+
obj
)
||
(
toString
.
call
(
obj
)
===
'[object Number]'
)
)
{
exports
[
prop
]
=
sqlite
[
prop
];
}
}
var
Database
=
exports
.
Database
=
function
()
{
var
Database
=
exports
.
Database
=
function
()
{
var
self
=
this
;
var
self
=
this
;
...
@@ -40,28 +32,48 @@ Database.prototype = {
...
@@ -40,28 +32,48 @@ Database.prototype = {
constructor
:
Database
,
constructor
:
Database
,
};
};
function
_queryDone
(
db
,
statement
)
{
Database
.
prototype
.
query
=
function
(
sql
,
bindings
,
rowCallback
)
{
if
(
statement
.
tail
)
{
var
self
=
this
;
statement
.
finalize
(
function
()
{
db
.
prepareAndStep
(
statement
.
tail
,
onPrepare
);
if
(
typeof
(
bindings
)
==
"function"
)
{
})
;
rowCallback
=
bindings
;
return
;
bindings
=
undefined
;
}
}
statement
.
finalize
(
function
()
{
this
.
prepare
(
sql
,
function
(
error
,
statement
)
{
// if there are any queries queued, let them know it's safe to go
function
next
()
{
db
.
emit
(
"ready"
);
_doStep
(
self
,
statement
,
rowCallback
);
}
if
(
error
)
{
return
rowCallback
(
error
);
}
if
(
statement
)
{
if
(
Array
.
isArray
(
bindings
))
{
statement
.
bindArray
(
bindings
,
next
);
}
else
if
(
typeof
(
bindings
)
===
'object'
)
{
statement
.
bindObject
(
bindings
,
next
);
}
else
{
next
();
}
}
else
{
rowCallback
();
}
});
});
}
}
function
_doStep
(
db
,
statement
,
rowCallback
)
{
function
_doStep
(
db
,
statement
,
rowCallback
)
{
statement
.
step
(
function
(
error
,
row
)
{
statement
.
step
(
function
(
error
,
row
)
{
if
(
error
)
if
(
error
)
{
return
rowCallback
(
error
);
return
rowCallback
(
error
);
}
if
(
!
row
)
{
if
(
!
row
)
{
rowCallback
();
rowCallback
();
_queryDone
(
db
,
statement
);
statement
.
finalize
(
function
(){}
);
return
;
return
;
}
}
rowCallback
(
undefined
,
row
);
rowCallback
(
undefined
,
row
);
...
@@ -70,48 +82,38 @@ function _doStep(db, statement, rowCallback) {
...
@@ -70,48 +82,38 @@ function _doStep(db, statement, rowCallback) {
});
});
}
}
function
_onPrepare
(
db
,
statement
,
bindings
,
rowCallback
)
{
// Execute SQL statements separated by semi-colons.
function
next
()
{
// SQL must contain no placeholders. Results are discarded.
_doStep
(
db
,
statement
,
rowCallback
);
Database
.
prototype
.
executeScript
=
function
(
script
,
callback
)
{
}
if
(
Array
.
isArray
(
bindings
))
{
statement
.
bindArray
(
bindings
,
next
);
}
else
if
(
typeof
(
bindings
)
===
'object'
)
{
statement
.
bindObject
(
bindings
,
next
);
}
}
Database
.
prototype
.
query
=
function
(
sql
,
bindings
,
rowCallback
,
prepareMode
)
{
var
self
=
this
;
if
(
typeof
(
bindings
)
==
"function"
)
{
prepareMode
=
rowCallback
||
sqlite
.
EXEC_EMPTY
;
rowCallback
=
bindings
;
bindings
=
[];
}
if
(
typeof
(
prepareMode
)
==
"undefined"
)
prepareMode
=
sqlite
.
EXEC_EMPTY
;
this
.
prepareAndStep
(
sql
,
function
(
error
,
statement
)
{
if
(
error
)
return
rowCallback
(
error
);
if
(
statement
)
{
_onPrepare
(
self
,
statement
,
bindings
,
rowCallback
);
}
else
{
rowCallback
();
}
},
prepareMode
);
}
Database
.
prototype
.
insert
=
function
(
sql
,
insertCallback
)
{
var
self
=
this
;
var
self
=
this
;
this
.
prepareAndStep
(
sql
,
function
(
error
,
info
)
{
(
function
stepOverSQL
(
sql
)
{
if
(
error
)
self
.
prepare
(
sql
,
function
(
error
,
statement
)
{
return
insertCallback
(
error
);
if
(
error
)
{
return
callback
(
error
);
insertCallback
(
undefined
,
(
info
?
info
.
last_inserted_id
:
0
));
}
},
sqlite
.
EXEC_LAST_INSERT_ID
);
statement
.
step
(
function
(
error
,
row
)
{
var
tail
;
if
(
error
)
{
callback
(
error
);
return
;
}
if
(
!
row
)
{
statement
.
finalize
(
function
(){});
tail
=
statement
.
tail
;
if
(
typeof
tail
==
"string"
)
{
tail
=
tail
.
trim
();
}
if
(
tail
)
{
stepOverSQL
(
tail
);
}
else
{
callback
();
}
}
});
});
})(
script
);
}
}
tests/lib/common.js
View file @
6d53991a
...
@@ -34,6 +34,7 @@ exports.insertMany = function (db, table, fields, rows, callback) {
...
@@ -34,6 +34,7 @@ exports.insertMany = function (db, table, fields, rows, callback) {
}
}
db
.
prepare
(
sql
,
function
(
error
,
stmt
)
{
db
.
prepare
(
sql
,
function
(
error
,
stmt
)
{
if
(
error
)
return
callback
(
error
);
statement
=
stmt
;
statement
=
stmt
;
doStep
(
--
i
);
doStep
(
--
i
);
});
});
...
...
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