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
779ee011
Commit
779ee011
authored
Dec 12, 2009
by
Eric Fredricksen
Committed by
Eric Fredricksen
Dec 12, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reimplement bindings as a thin wrapper
parent
a3401c87
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
225 additions
and
119 deletions
+225
-119
sqlite.js
sqlite.js
+45
-0
sqlite3_bindings.cc
sqlite3_bindings.cc
+180
-119
No files found.
sqlite.js
View file @
779ee011
...
@@ -33,6 +33,51 @@ exports.openDatabaseSync = function (name, version, displayName,
...
@@ -33,6 +33,51 @@ exports.openDatabaseSync = function (name, version, displayName,
}
}
DatabaseSync
.
prototype
.
performQuery
=
function
(
sql
,
bindings
,
callback
,
errback
)
{
var
all
=
[];
// bindings param is optional
if
(
typeof
bindings
==
'function'
)
{
errback
=
callback
;
callback
=
bindings
;
bindings
=
null
;
}
var
stmt
=
this
.
prepare
(
sql
);
while
(
stmt
)
{
if
(
bindings
)
{
if
(
Object
.
prototype
.
toString
.
call
(
bindings
)
===
"[object Array]"
)
{
for
(
var
i
=
0
;
i
<
stmt
.
bindParameterCount
();
++
i
)
stmt
.
bind
(
i
+
1
,
bindings
.
shift
());
}
else
{
for
(
var
key
in
bindings
)
if
(
bindings
.
hasOwnProperty
(
key
))
stmt
.
bind
(
key
,
bindings
[
key
]);
}
}
var
rows
=
[];
while
(
true
)
{
var
row
=
stmt
.
step
();
if
(
!
row
)
break
;
rows
.
push
(
row
);
}
rows
.
rowsAffected
=
this
.
changes
();
rows
.
insertId
=
this
.
lastInsertRowid
();
all
.
push
(
rows
);
stmt
.
finalize
();
stmt
=
this
.
prepare
(
stmt
.
tail
);
}
return
all
;
}
DatabaseSync
.
prototype
.
query
=
function
(
sql
,
bindings
,
callback
)
{
DatabaseSync
.
prototype
.
query
=
function
(
sql
,
bindings
,
callback
)
{
// TODO: error callback
// TODO: error callback
if
(
typeof
(
bindings
)
==
"function"
)
{
if
(
typeof
(
bindings
)
==
"function"
)
{
...
...
sqlite3_bindings.cc
View file @
779ee011
...
@@ -33,13 +33,20 @@ public:
...
@@ -33,13 +33,20 @@ public:
t
->
Inherit
(
EventEmitter
::
constructor_template
);
t
->
Inherit
(
EventEmitter
::
constructor_template
);
t
->
InstanceTemplate
()
->
SetInternalFieldCount
(
1
);
t
->
InstanceTemplate
()
->
SetInternalFieldCount
(
1
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"
performQuery"
,
PerformQuery
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"
changes"
,
Changes
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"close"
,
Close
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"close"
,
Close
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"lastInsertRowid"
,
LastInsertRowid
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"prepare"
,
Prepare
);
target
->
Set
(
v8
::
String
::
NewSymbol
(
"DatabaseSync"
),
t
->
GetFunction
());
target
->
Set
(
v8
::
String
::
NewSymbol
(
"DatabaseSync"
),
t
->
GetFunction
());
Statement
::
Init
(
target
);
}
}
protected
:
protected
:
Sqlite3Db
()
:
db_
(
NULL
)
{
}
Sqlite3Db
(
sqlite3
*
db
)
:
db_
(
db
)
{
Sqlite3Db
(
sqlite3
*
db
)
:
db_
(
db
)
{
}
}
...
@@ -49,11 +56,10 @@ protected:
...
@@ -49,11 +56,10 @@ protected:
sqlite3
*
db_
;
sqlite3
*
db_
;
operator
sqlite3
*
()
{
return
db_
;
}
operator
sqlite3
*
()
const
{
return
db_
;
}
protected
:
protected
:
static
Handle
<
Value
>
New
(
const
Arguments
&
args
)
static
Handle
<
Value
>
New
(
const
Arguments
&
args
)
{
{
HandleScope
scope
;
HandleScope
scope
;
if
(
args
.
Length
()
==
0
||
!
args
[
0
]
->
IsString
())
{
if
(
args
.
Length
()
==
0
||
!
args
[
0
]
->
IsString
())
{
...
@@ -74,155 +80,208 @@ protected:
...
@@ -74,155 +80,208 @@ protected:
}
}
static
Handle
<
Value
>
PerformQuery
(
const
Arguments
&
args
)
#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"))); \
}
static
Handle
<
Value
>
Changes
(
const
Arguments
&
args
)
{
HandleScope
scope
;
HandleScope
scope
;
Sqlite3Db
*
db
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
Sqlite3Db
*
db
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
Local
<
Number
>
result
=
Integer
::
New
(
sqlite3_changes
(
*
db
));
return
scope
.
Close
(
result
);
}
if
(
args
.
Length
()
==
0
||
!
args
[
0
]
->
IsString
())
{
static
Handle
<
Value
>
Close
(
const
Arguments
&
args
)
{
return
ThrowException
(
Exception
::
TypeError
(
HandleScope
scope
;
String
::
New
(
"First argument must be a string"
)));
Sqlite3Db
*
db
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
CHECK
(
sqlite3_close
(
*
db
));
db
->
db_
=
NULL
;
return
Undefined
();
}
static
Handle
<
Value
>
LastInsertRowid
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Sqlite3Db
*
db
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
Local
<
Number
>
result
=
Integer
::
New
(
sqlite3_last_insert_rowid
(
*
db
));
return
scope
.
Close
(
result
);
}
static
Handle
<
Value
>
Open
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Sqlite3Db
*
db
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
REQ_STR_ARG
(
0
,
filename
);
Close
(
args
);
// ignores args anyway, except This
CHECK
(
sqlite3_open
(
*
filename
,
&
db
->
db_
));
return
args
.
This
();
}
static
Handle
<
Value
>
Prepare
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Sqlite3Db
*
db
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
REQ_STR_ARG
(
0
,
sql
);
sqlite3_stmt
*
stmt
=
NULL
;
const
char
*
tail
=
NULL
;
CHECK
(
sqlite3_prepare_v2
(
*
db
,
*
sql
,
-
1
,
&
stmt
,
&
tail
));
if
(
!
stmt
)
return
Null
();
Handle
<
Value
>
arg
=
External
::
New
(
stmt
);
Local
<
Object
>
statement
(
Statement
::
constructor_template
->
GetFunction
()
->
NewInstance
(
1
,
&
arg
));
if
(
tail
)
statement
->
Set
(
String
::
New
(
"tail"
),
String
::
New
(
tail
));
return
scope
.
Close
(
statement
);
}
}
String
::
Utf8Value
usql
(
args
[
0
]
->
ToString
());
class
Statement
:
public
EventEmitter
const
char
*
sql
(
*
usql
);
{
public
:
static
Persistent
<
FunctionTemplate
>
constructor_template
;
int
param
=
0
;
static
void
Init
(
v8
::
Handle
<
Object
>
target
)
{
HandleScope
scope
;
Local
<
Array
>
all
(
Array
::
New
(
0
));
Local
<
FunctionTemplate
>
t
=
FunctionTemplate
::
New
(
New
);
constructor_template
=
Persistent
<
FunctionTemplate
>::
New
(
t
);
t
->
Inherit
(
EventEmitter
::
constructor_template
);
t
->
InstanceTemplate
()
->
SetInternalFieldCount
(
1
);
for
(;;)
{
NODE_SET_PROTOTYPE_METHOD
(
t
,
"bind"
,
Bind
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"finalize"
,
Finalize
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"bindParameterCount"
,
BindParameterCount
);
NODE_SET_PROTOTYPE_METHOD
(
t
,
"step"
,
Step
);
sqlite3_stmt
*
stmt
;
//target->Set(v8::String::NewSymbol("SQLStatement"), t->GetFunction());
int
rc
=
sqlite3_prepare_v2
(
*
db
,
sql
,
-
1
,
&
stmt
,
&
sql
);
}
if
(
rc
!=
SQLITE_OK
)
{
return
ThrowException
(
Exception
::
Error
(
String
::
New
(
static
Handle
<
Value
>
New
(
const
Arguments
&
args
)
{
sqlite3_errmsg
(
*
db
))));
HandleScope
scope
;
}
int
I
=
0
;
if
(
!
stmt
)
break
;
REQ_EXT_ARG
(
0
,
stmt
);
Statement
statement
(
stmt
);
(
new
Statement
((
sqlite3_stmt
*
)
stmt
->
Value
()))
->
Wrap
(
args
.
This
());
return
args
.
This
();
if
(
args
.
Length
()
>
1
)
{
}
if
(
args
[
1
]
->
IsArray
())
{
Local
<
Array
>
a
(
Array
::
Cast
(
*
args
[
1
]));
protected
:
int
start
=
param
;
Statement
(
sqlite3_stmt
*
stmt
)
:
stmt_
(
stmt
)
{}
int
stop
=
start
+
sqlite3_bind_parameter_count
(
statement
);
for
(;
param
<
a
->
Length
()
&&
param
<
stop
;
++
param
)
{
~
Statement
()
{
if
(
stmt_
)
sqlite3_finalize
(
stmt_
);
}
Local
<
Value
>
v
=
a
->
Get
(
Integer
::
New
(
param
));
statement
.
Bind
(
param
+
1
-
start
,
v
);
sqlite3_stmt
*
stmt_
;
}
}
else
if
(
args
[
1
]
->
IsObject
())
{
operator
sqlite3_stmt
*
()
const
{
return
stmt_
;
}
Local
<
Array
>
keys
(
args
[
1
]
->
ToObject
()
->
GetPropertyNames
());
for
(
int
k
=
0
;
k
<
keys
->
Length
();
++
k
)
{
static
Handle
<
Value
>
Bind
(
const
Arguments
&
args
)
{
Local
<
Value
>
key
(
keys
->
Get
(
Integer
::
New
(
k
)));
HandleScope
scope
;
statement
.
Bind
(
key
,
args
[
1
]
->
ToObject
()
->
Get
(
key
));
Statement
*
stmt
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
}
}
else
if
(
args
[
1
]
->
IsUndefined
()
||
args
[
1
]
->
IsNull
())
{
REQ_ARGS
(
2
);
// That's okay
if
(
!
args
[
0
]
->
IsString
()
&&
!
args
[
0
]
->
IsInt32
())
return
ThrowException
(
Exception
::
TypeError
(
String
::
New
(
"First argument must be a string or integer"
)));
int
index
=
args
[
0
]
->
IsString
()
?
sqlite3_bind_parameter_index
(
*
stmt
,
*
String
::
Utf8Value
(
args
[
0
]))
:
args
[
0
]
->
Int32Value
();
if
(
args
[
1
]
->
IsInt32
())
{
sqlite3_bind_int
(
*
stmt
,
index
,
args
[
1
]
->
Int32Value
());
}
else
if
(
args
[
1
]
->
IsNumber
())
{
sqlite3_bind_double
(
*
stmt
,
index
,
args
[
1
]
->
NumberValue
());
}
else
if
(
args
[
1
]
->
IsString
())
{
String
::
Utf8Value
text
(
args
[
1
]);
sqlite3_bind_text
(
*
stmt
,
index
,
*
text
,
text
.
length
(),
SQLITE_TRANSIENT
);
}
else
{
}
else
{
return
ThrowException
(
Exception
::
TypeError
(
return
ThrowException
(
Exception
::
TypeError
(
String
::
New
(
"Second argument invalid
"
)));
String
::
New
(
"Unable to bind value of this type
"
)));
}
}
return
args
.
This
();
}
}
Local
<
Array
>
rows
(
Array
::
New
(
0
));
static
Handle
<
Value
>
Finalize
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Statement
*
stmt
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
SCHECK
(
sqlite3_finalize
(
*
stmt
));
stmt
->
stmt_
=
NULL
;
return
Undefined
();
}
for
(
int
r
=
0
;
;
++
r
)
{
static
Handle
<
Value
>
BindParameterCount
(
const
Arguments
&
args
)
{
int
rc
=
sqlite3_step
(
statement
);
HandleScope
scope
;
Statement
*
stmt
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
Local
<
Number
>
result
=
Integer
::
New
(
sqlite3_bind_parameter_count
(
*
stmt
));
return
scope
.
Close
(
result
);
}
static
Handle
<
Value
>
Step
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Statement
*
stmt
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
int
rc
=
sqlite3_step
(
*
stmt
);
if
(
rc
==
SQLITE_ROW
)
{
if
(
rc
==
SQLITE_ROW
)
{
Local
<
Object
>
row
=
Object
::
New
();
Local
<
Object
>
row
=
Object
::
New
();
for
(
int
c
=
0
;
c
<
sqlite3_column_count
(
statemen
t
);
++
c
)
{
for
(
int
c
=
0
;
c
<
sqlite3_column_count
(
*
stm
t
);
++
c
)
{
Handle
<
Value
>
value
;
Handle
<
Value
>
value
;
switch
(
sqlite3_column_type
(
statemen
t
,
c
))
{
switch
(
sqlite3_column_type
(
*
stm
t
,
c
))
{
case
SQLITE_INTEGER
:
case
SQLITE_INTEGER
:
value
=
Integer
::
New
(
sqlite3_column_int
(
statemen
t
,
c
));
value
=
Integer
::
New
(
sqlite3_column_int
(
*
stm
t
,
c
));
break
;
break
;
case
SQLITE_FLOAT
:
case
SQLITE_FLOAT
:
value
=
Number
::
New
(
sqlite3_column_double
(
statemen
t
,
c
));
value
=
Number
::
New
(
sqlite3_column_double
(
*
stm
t
,
c
));
break
;
break
;
case
SQLITE_TEXT
:
case
SQLITE_TEXT
:
value
=
String
::
New
((
const
char
*
)
sqlite3_column_text
(
statemen
t
,
c
));
value
=
String
::
New
((
const
char
*
)
sqlite3_column_text
(
*
stm
t
,
c
));
break
;
break
;
case
SQLITE_NULL
:
case
SQLITE_NULL
:
default
:
// We don't handle any other types just now
default
:
// We don't handle any other types just now
value
=
Undefined
();
value
=
Undefined
();
break
;
break
;
}
}
row
->
Set
(
String
::
NewSymbol
(
sqlite3_column_name
(
statemen
t
,
c
)),
row
->
Set
(
String
::
NewSymbol
(
sqlite3_column_name
(
*
stm
t
,
c
)),
value
);
value
);
}
}
rows
->
Set
(
Integer
::
New
(
rows
->
Length
()),
row
)
;
return
row
;
}
else
if
(
rc
==
SQLITE_DONE
)
{
}
else
if
(
rc
==
SQLITE_DONE
)
{
break
;
return
Null
();
}
else
{
return
ThrowException
(
Exception
::
Error
(
v8
::
String
::
New
(
sqlite3_errmsg
(
*
db
))));
}
}
rows
->
Set
(
String
::
New
(
"rowsAffected"
),
Integer
::
New
(
sqlite3_changes
(
*
db
)));
rows
->
Set
(
String
::
New
(
"insertId"
),
Integer
::
New
(
sqlite3_last_insert_rowid
(
*
db
)));
all
->
Set
(
Integer
::
New
(
all
->
Length
()),
rows
);
sqlite3_finalize
(
stmt
);
}
return
all
;
}
static
Handle
<
Value
>
Close
(
const
Arguments
&
args
)
{
Sqlite3Db
*
db
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
HandleScope
scope
;
db
->
Close
();
return
Undefined
();
}
void
Close
()
{
sqlite3_close
(
db_
);
db_
=
NULL
;
Detach
();
}
class
Statement
:
public
EventEmitter
{
public
:
Statement
(
sqlite3_stmt
*
stmt
)
:
stmt_
(
stmt
)
{}
~
Statement
()
{
sqlite3_finalize
(
stmt_
);
}
operator
sqlite3_stmt
*
()
{
return
stmt_
;
}
bool
Bind
(
int
index
,
Handle
<
Value
>
value
)
{
HandleScope
scope
;
if
(
value
->
IsInt32
())
{
sqlite3_bind_int
(
stmt_
,
index
,
value
->
Int32Value
());
}
else
if
(
value
->
IsNumber
())
{
sqlite3_bind_double
(
stmt_
,
index
,
value
->
NumberValue
());
}
else
if
(
value
->
IsString
())
{
String
::
Utf8Value
text
(
value
);
sqlite3_bind_text
(
stmt_
,
index
,
*
text
,
text
.
length
(),
SQLITE_TRANSIENT
);
}
else
{
}
else
{
return
false
;
return
ThrowException
(
Exception
::
Error
(
String
::
New
(
}
sqlite3_errmsg
(
sqlite3_db_handle
(
*
stmt
)))));
return
true
;
}
}
bool
Bind
(
Handle
<
Value
>
key
,
Handle
<
Value
>
value
)
{
HandleScope
scope
;
String
::
Utf8Value
skey
(
key
);
//string x = ":" + key
int
index
=
sqlite3_bind_parameter_index
(
stmt_
,
*
skey
);
Bind
(
index
,
value
);
}
}
Handle
<
Object
>
Cast
()
/*
{
Handle<Object> Cast()
{
HandleScope scope;
HandleScope scope;
Local<ObjectTemplate> t(ObjectTemplate::New());
Local<ObjectTemplate> t(ObjectTemplate::New());
t->SetInternalFieldCount(1);
t->SetInternalFieldCount(1);
...
@@ -231,15 +290,17 @@ protected:
...
@@ -231,15 +290,17 @@ protected:
//Wrap(thus);
//Wrap(thus);
return thus;
return thus;
}
}
*/
protected
:
sqlite3_stmt
*
stmt_
;
};
};
};
};
Persistent
<
FunctionTemplate
>
Sqlite3Db
::
Statement
::
constructor_template
;
extern
"C"
void
init
(
v8
::
Handle
<
Object
>
target
)
extern
"C"
void
init
(
v8
::
Handle
<
Object
>
target
)
{
{
Sqlite3Db
::
Init
(
target
);
Sqlite3Db
::
Init
(
target
);
...
...
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