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
59c42a4b
Commit
59c42a4b
authored
Mar 04, 2010
by
Orlando Vazquez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
start getting bind working through libeio (wip)
parent
e869be39
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
138 additions
and
24 deletions
+138
-24
mydatabase.sql
mydatabase.sql
+9
-0
sqlite3_bindings.cc
sqlite3_bindings.cc
+118
-19
test2.js
test2.js
+11
-5
No files found.
mydatabase.sql
0 → 100644
View file @
59c42a4b
-- Used to create mydatabase.db
CREATE
TABlE
foo
(
bar
TEXT
,
baz
INT
);
INSERT
INTO
foo
(
bar
,
baz
)
VALUES
(
'hello'
,
420
);
INSERT
INTO
foo
(
bar
,
baz
)
VALUES
(
'world'
,
42
);
sqlite3_bindings.cc
View file @
59c42a4b
...
...
@@ -131,9 +131,6 @@ protected:
HandleScope
scope
;
struct
open_request
*
open_req
=
(
struct
open_request
*
)(
req
->
data
);
printf
(
"EIO_AfterOpen; rc = %d
\n
"
,
(
int
)
req
->
result
);
printf
(
"result was %d
\n
"
,
(
int
)
req
->
result
);
Local
<
Value
>
argv
[
1
];
bool
err
=
false
;
if
(
req
->
result
)
{
...
...
@@ -159,15 +156,11 @@ protected:
static
int
EIO_Open
(
eio_req
*
req
)
{
struct
open_request
*
open_req
=
(
struct
open_request
*
)(
req
->
data
);
printf
(
"The filename was %s
\n
"
,
open_req
->
filename
);
sqlite3
**
dbptr
=
open_req
->
dbo
->
GetDBPtr
();
printf
(
"before assn %p
\n
"
,
*
dbptr
);
int
rc
=
sqlite3_open
(
open_req
->
filename
,
dbptr
);
req
->
result
=
rc
;
printf
(
"after assn %p
\n
"
,
*
dbptr
);
// XXX try pulling the sqlite5 handle lazily (ie don't store in struct)
sqlite3
*
db
=
*
dbptr
;
sqlite3_commit_hook
(
db
,
CommitHook
,
open_req
->
dbo
);
sqlite3_rollback_hook
(
db
,
RollbackHook
,
open_req
->
dbo
);
...
...
@@ -182,8 +175,6 @@ protected:
Sqlite3Db
*
dbo
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
sqlite3
*
db
=
*
dbo
;
printf
(
"print it %p
\n
"
,
db
);
return
Undefined
();
}
...
...
@@ -195,8 +186,6 @@ protected:
Sqlite3Db
*
dbo
=
ObjectWrap
::
Unwrap
<
Sqlite3Db
>
(
args
.
This
());
printf
(
"way before addr %p
\n
"
,
((
sqlite3
*
)
*
dbo
));
struct
open_request
*
open_req
=
(
struct
open_request
*
)
calloc
(
1
,
sizeof
(
struct
open_request
)
+
filename
.
length
());
...
...
@@ -285,8 +274,6 @@ protected:
struct
prepare_request
*
prep_req
=
(
struct
prepare_request
*
)(
req
->
data
);
HandleScope
scope
;
printf
(
"EIO_AfterPrepare %s
\n
"
,
prep_req
->
sql
);
Local
<
Value
>
argv
[
2
];
bool
err
=
false
;
if
(
req
->
result
)
{
...
...
@@ -300,8 +287,8 @@ protected:
if
(
prep_req
->
tail
)
statement
->
Set
(
String
::
New
(
"tail"
),
String
::
New
(
prep_req
->
tail
));
argv
[
1
]
=
Local
<
Value
>::
New
(
Undefined
());
argv
[
0
]
=
scope
.
Close
(
statement
);
argv
[
0
]
=
Local
<
Value
>::
New
(
Undefined
());
argv
[
1
]
=
scope
.
Close
(
statement
);
}
TryCatch
try_catch
;
...
...
@@ -322,8 +309,6 @@ protected:
static
int
EIO_Prepare
(
eio_req
*
req
)
{
struct
prepare_request
*
prep_req
=
(
struct
prepare_request
*
)(
req
->
data
);
printf
(
"EIO_Prepare %s
\n
"
,
static_cast
<
struct
prepare_request
*>
(
req
->
data
)
->
sql
);
prep_req
->
stmt
=
NULL
;
prep_req
->
tail
=
NULL
;
...
...
@@ -331,7 +316,8 @@ protected:
int
rc
=
sqlite3_prepare_v2
(
db
,
prep_req
->
sql
,
-
1
,
&
(
prep_req
->
stmt
),
&
(
prep_req
->
tail
));
printf
(
"sqlite3_prepare called
\n
"
);
printf
(
"rc = %d, stmt = %p
\n
"
,
rc
,
prep_req
->
stmt
);
rc
=
rc
||
!
prep_req
->
stmt
;
req
->
result
=
rc
;
...
...
@@ -412,8 +398,122 @@ protected:
// JS prepared statement bindings
//
// indicate the key type (integer index or name string)
enum
BindKeyType
{
KEY_INT
,
KEY_STRING
};
// indicate the parameter type
enum
BindValueType
{
VALUE_INT
,
VALUE_NUMBER
,
VALUE_TEXT
,
VALUE_UNDEFINED
};
struct
bind_request
{
Persistent
<
Function
>
cb
;
Statement
*
sto
;
enum
BindKeyType
key_type
;
enum
BindValueType
value_type
;
void
*
key
;
// pointer to char * or int
void
*
value
;
// pointer to char * or int
};
static
int
EIO_AfterBind
(
eio_req
*
req
)
{
ev_unref
(
EV_DEFAULT_UC
);
HandleScope
scope
;
struct
bind_request
*
bind_req
=
(
struct
bind_request
*
)(
req
->
data
);
printf
(
"EIO_AfterBind %d
\n
"
,
(
int
)
req
->
result
);
Statement
*
sto
=
bind_req
->
sto
;
TryCatch
try_catch
;
bind_req
->
cb
->
Call
(
Context
::
GetCurrent
()
->
Global
(),
0
,
NULL
);
if
(
try_catch
.
HasCaught
())
{
FatalException
(
try_catch
);
}
bind_req
->
cb
.
Dispose
();
sto
->
Unref
();
free
(
bind_req
->
key
);
free
(
bind_req
->
value
);
free
(
bind_req
);
return
0
;
}
static
int
EIO_Bind
(
eio_req
*
req
)
{
struct
bind_request
*
bind_req
=
(
struct
bind_request
*
)(
req
->
data
);
printf
(
"EIO_Bind
\n
"
);
if
(
bind_req
->
key_type
==
KEY_INT
)
{
enum
BindKeyType
key_type
=
bind_req
->
key_type
;
int
*
index
=
(
int
*
)(
bind_req
->
key
);
int
*
value
=
(
int
*
)(
bind_req
->
value
);
printf
(
"index was %d value was %d
\n
"
,
*
index
,
*
value
);
}
printf
(
"outside
\n
"
);
req
->
result
=
420
;
return
0
;
}
static
Handle
<
Value
>
Bind
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Statement
*
sto
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
REQ_ARGS
(
2
);
REQ_FUN_ARG
(
2
,
cb
);
if
(
!
args
[
0
]
->
IsString
()
&&
!
args
[
0
]
->
IsInt32
())
return
ThrowException
(
Exception
::
TypeError
(
String
::
New
(
"First argument must be a string or integer"
)));
struct
bind_request
*
bind_req
=
(
struct
bind_request
*
)
calloc
(
1
,
sizeof
(
struct
bind_request
));
if
(
args
[
0
]
->
IsString
())
{
// bind_req->key_type = STRING;
// Local<String> key = String::Utf8Value(args[0]);
// char *value = (char *) malloc();
// bind_rq->key_value = value;
}
else
{
bind_req
->
key_type
=
KEY_INT
;
int
*
index
=
(
int
*
)
malloc
(
sizeof
(
int
));
*
index
=
args
[
0
]
->
Int32Value
();
int
*
value
=
(
int
*
)
malloc
(
sizeof
(
int
));
*
value
=
args
[
1
]
->
Int32Value
();
printf
(
"creating key/val %d %d
\n
"
,
*
index
,
*
value
);
// don't forget to `free` this
bind_req
->
key
=
index
;
bind_req
->
value
=
value
;
}
printf
(
"done binding
\n
"
);
bind_req
->
cb
=
Persistent
<
Function
>::
New
(
cb
);
bind_req
->
sto
=
sto
;
eio_custom
(
EIO_Bind
,
EIO_PRI_DEFAULT
,
EIO_AfterBind
,
bind_req
);
ev_ref
(
EV_DEFAULT_UC
);
sto
->
Ref
();
return
Undefined
();
}
static
Handle
<
Value
>
Bind2
(
const
Arguments
&
args
)
{
HandleScope
scope
;
Statement
*
stmt
=
ObjectWrap
::
Unwrap
<
Statement
>
(
args
.
This
());
REQ_ARGS
(
2
);
...
...
@@ -524,7 +624,6 @@ protected:
Persistent
<
FunctionTemplate
>
Sqlite3Db
::
Statement
::
constructor_template
;
extern
"C"
void
init
(
v8
::
Handle
<
Object
>
target
)
{
Sqlite3Db
::
Init
(
target
);
}
...
...
test2.js
View file @
59c42a4b
...
...
@@ -18,20 +18,26 @@ throws(function () {
db
.
open
(
"foo.db"
);
});
db
.
open
(
"my.db"
,
function
(
err
)
{
db
.
open
(
"my
database
.db"
,
function
(
err
)
{
puts
(
inspect
(
arguments
));
if
(
err
)
{
puts
(
"There was an error"
);
throw
err
;
}
puts
(
"open callback"
);
db
.
printIt
();
db
.
prepare
(
"SELECT foo FROM bar"
,
function
(
statement
)
{
puts
(
"prepare callback"
);
});
db
.
prepare
(
"SELECT foo FROM bar; SELECT bar FROM baz"
,
function
(
statement
)
{
db
.
prepare
(
"SELECT bar FROM foo; SELECT baz FROM foo;"
,
function
(
error
,
statement
)
{
puts
(
inspect
(
arguments
));
puts
(
"prepare callback"
);
db
.
prepare
(
"SELECT bar FROM foo WHERE bar = 'hello'"
,
function
(
error
,
statement
)
{
puts
(
"prepare callback"
);
statement
.
bind
(
0
,
666
,
function
()
{
puts
(
"bind callback"
);
});
});
});
});
puts
(
"done"
);
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