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
d1a750dc
Commit
d1a750dc
authored
Jan 25, 2011
by
Will White
Committed by
Konstantin Käfer
Feb 14, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add blob read/write support.
parent
46eb3b2f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
0 deletions
+127
-0
statement.cc
src/statement.cc
+28
-0
statement.h
src/statement.h
+3
-0
principia_mathematica.jpg
tests/fixtures/principia_mathematica.jpg
+0
-0
test-blob.js
tests/test-blob.js
+96
-0
No files found.
src/statement.cc
View file @
d1a750dc
...
...
@@ -147,6 +147,10 @@ int Statement::EIO_BindArray(eio_req *req) {
rc
=
sqlite3_bind_text
(
sto
->
stmt_
,
index
,
(
char
*
)(
pair
->
value
),
pair
->
value_size
,
SQLITE_TRANSIENT
);
break
;
case
VALUE_BLOB
:
rc
=
sqlite3_bind_blob
(
sto
->
stmt_
,
index
,
(
char
*
)(
pair
->
value
),
pair
->
value_size
,
SQLITE_TRANSIENT
);
break
;
case
VALUE_NULL
:
rc
=
sqlite3_bind_null
(
sto
->
stmt_
,
index
);
break
;
...
...
@@ -388,6 +392,12 @@ Handle<Value> Statement::Bind(const Arguments& args) {
pair
->
value
=
value
;
pair
->
value_size
=
text
.
length
();
}
else
if
(
Buffer
::
HasInstance
(
args
[
1
]))
{
pair
->
value_type
=
VALUE_BLOB
;
Buffer
*
buffer
=
Buffer
::
Unwrap
<
Buffer
>
(
args
[
1
]
->
ToObject
());
pair
->
value
=
buffer
->
data
();
pair
->
value_size
=
buffer
->
length
();
}
else
if
(
args
[
1
]
->
IsNull
()
||
args
[
1
]
->
IsUndefined
())
{
pair
->
value_type
=
VALUE_NULL
;
pair
->
value
=
NULL
;
...
...
@@ -525,6 +535,14 @@ int Statement::EIO_AfterStep(eio_req *req) {
}
break
;
case
SQLITE_BLOB
:
{
node
::
Buffer
*
buf
=
Buffer
::
New
(
cell
->
size
);
memcpy
(
buf
->
data
(),
cell
->
value
,
cell
->
size
);
row
->
Set
(
String
::
New
(
sto
->
column_names_
[
i
]),
buf
->
handle_
);
free
(
cell
->
value
);
}
break
;
case
SQLITE_NULL
:
row
->
Set
(
String
::
New
(
sto
->
column_names_
[
i
]),
Local
<
Value
>::
New
(
Null
()));
...
...
@@ -644,6 +662,16 @@ int Statement::EIO_Step(eio_req *req) {
}
break
;
case
SQLITE_BLOB
:
{
const
void
*
blob
=
sqlite3_column_blob
(
stmt
,
i
);
int
size
=
sqlite3_column_bytes
(
stmt
,
i
);
cell
->
size
=
size
;
unsigned
char
*
pzBlob
=
(
unsigned
char
*
)
malloc
(
size
);
memcpy
(
pzBlob
,
blob
,
size
);
cell
->
value
=
pzBlob
;
}
break
;
case
SQLITE_NULL
:
cell
->
value
=
NULL
;
break
;
...
...
src/statement.h
View file @
d1a750dc
...
...
@@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <node_events.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <node_buffer.h>
extern
"C"
{
#include <mpool.h>
...
...
@@ -34,6 +35,7 @@ struct cell_node {
void
*
value
;
int
type
;
struct
cell_node
*
next
;
int
size
;
};
struct
row_node
{
...
...
@@ -122,6 +124,7 @@ enum BindKeyType {
enum
BindValueType
{
VALUE_INT
,
VALUE_DOUBLE
,
VALUE_BLOB
,
VALUE_STRING
,
VALUE_NULL
};
...
...
tests/fixtures/principia_mathematica.jpg
0 → 100644
View file @
d1a750dc
47.4 KB
tests/test-blob.js
0 → 100644
View file @
d1a750dc
require
.
paths
.
push
(
__dirname
+
'/..'
);
sys
=
require
(
'sys'
);
fs
=
require
(
'fs'
);
path
=
require
(
'path'
);
TestSuite
=
require
(
'async-testing/async_testing'
).
TestSuite
;
sqlite
=
require
(
'sqlite'
);
var
db
=
new
sqlite
.
Database
();
var
name
=
"Reading and writing blobs"
;
var
suite
=
exports
[
name
]
=
new
TestSuite
(
name
);
var
fixture_path
=
path
.
join
(
__dirname
,
'fixtures/principia_mathematica.jpg'
);
function
createTestTable
(
db
,
callback
)
{
db
.
prepare
(
'CREATE TABLE [images] (name text,image blob);'
,
function
(
error
,
createStatement
)
{
if
(
error
)
throw
error
;
createStatement
.
step
(
function
(
error
,
row
)
{
if
(
error
)
throw
error
;
callback
();
});
});
}
var
tests
=
[
{
"Blob read/write"
:
function
(
assert
,
finished
)
{
var
that
=
this
;
this
.
db
.
open
(
':memory:'
,
function
()
{
createTestTable
(
that
.
db
,
function
()
{
that
.
db
.
prepare
(
'INSERT INTO images (name, image) VALUES ("principia_mathematica", ?);'
,
function
(
err
,
statement
)
{
if
(
err
)
throw
err
;
fs
.
readFile
(
fixture_path
,
function
(
err
,
data
)
{
if
(
err
)
throw
err
;
assert
.
equal
(
data
.
length
,
48538
);
statement
.
bind
(
1
,
data
,
function
(
err
)
{
if
(
err
)
throw
err
;
statement
.
step
(
function
(
err
,
row
)
{
if
(
err
)
throw
err
;
that
.
db
.
prepare
(
'SELECT image FROM images WHERE name = "principia_mathematica";'
,
function
(
err
,
statement
)
{
if
(
err
)
throw
err
;
statement
.
step
(
function
(
err
,
row
)
{
if
(
err
)
throw
err
;
assert
.
equal
(
row
.
image
.
length
,
48538
);
finished
();
});
}
);
});
});
});
}
);
});
});
}
},
{
"Close database after blob query"
:
function
(
assert
,
finished
)
{
this
.
db
.
close
(
function
(
err
)
{
if
(
err
)
throw
err
;
finished
();
});
}
}
];
for
(
var
i
=
0
,
il
=
tests
.
length
;
i
<
il
;
i
++
)
{
suite
.
addTests
(
tests
[
i
]);
}
var
currentTest
=
0
;
var
testCount
=
tests
.
length
;
suite
.
db
=
new
sqlite
.
Database
();
suite
.
setup
(
function
(
finished
,
test
)
{
this
.
db
=
suite
.
db
;
finished
();
});
suite
.
teardown
(
function
(
finished
)
{
finished
();
++
currentTest
==
testCount
;
});
if
(
module
==
require
.
main
)
{
suite
.
runTests
();
}
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