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
04b72695
Commit
04b72695
authored
Mar 07, 2011
by
Konstantin Käfer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add #map() function that returns a hash instead of an array
parent
c7f3a8bd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
113 additions
and
4 deletions
+113
-4
sqlite3.js
lib/sqlite3.js
+39
-4
map.test.js
test/map.test.js
+74
-0
No files found.
lib/sqlite3.js
View file @
04b72695
...
...
@@ -29,7 +29,7 @@ Database.prototype.run = function(sql) {
var
statement
=
new
Statement
(
this
,
sql
,
errorCallback
(
params
));
statement
.
run
.
apply
(
statement
,
params
).
finalize
();
return
this
;
}
}
;
// Database#get(sql, [bind1, bind2, ...], [callback])
Database
.
prototype
.
get
=
function
(
sql
)
{
...
...
@@ -37,7 +37,7 @@ Database.prototype.get = function(sql) {
var
statement
=
new
Statement
(
this
,
sql
,
errorCallback
(
params
));
statement
.
get
.
apply
(
statement
,
params
).
finalize
();
return
this
;
}
}
;
// Database#all(sql, [bind1, bind2, ...], [callback])
Database
.
prototype
.
all
=
function
(
sql
)
{
...
...
@@ -45,7 +45,7 @@ Database.prototype.all = function(sql) {
var
statement
=
new
Statement
(
this
,
sql
,
errorCallback
(
params
));
statement
.
all
.
apply
(
statement
,
params
).
finalize
();
return
this
;
}
}
;
// Database#each(sql, [bind1, bind2, ...], [callback], [complete])
Database
.
prototype
.
each
=
function
(
sql
)
{
...
...
@@ -53,7 +53,40 @@ Database.prototype.each = function(sql) {
var
statement
=
new
Statement
(
this
,
sql
,
errorCallback
(
params
));
statement
.
each
.
apply
(
statement
,
params
).
finalize
();
return
this
;
}
};
Database
.
prototype
.
map
=
function
(
sql
)
{
var
params
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
);
var
statement
=
new
Statement
(
this
,
sql
,
errorCallback
(
params
));
statement
.
map
.
apply
(
statement
,
params
).
finalize
();
return
this
;
};
Statement
.
prototype
.
map
=
function
()
{
var
params
=
Array
.
prototype
.
slice
.
call
(
arguments
);
var
callback
=
params
.
pop
();
params
.
push
(
function
(
err
,
rows
)
{
if
(
err
)
return
callback
(
err
);
var
result
=
{};
if
(
rows
.
length
)
{
var
keys
=
Object
.
keys
(
rows
[
0
]),
key
=
keys
[
0
];
if
(
keys
.
length
>
2
)
{
// Value is an object
for
(
var
i
=
0
;
i
<
rows
.
length
;
i
++
)
{
result
[
rows
[
i
][
key
]]
=
rows
[
i
];
}
}
else
{
var
value
=
keys
[
1
];
// Value is a plain value
for
(
var
i
=
0
;
i
<
rows
.
length
;
i
++
)
{
result
[
rows
[
i
][
key
]]
=
rows
[
i
][
value
];
}
}
}
callback
(
err
,
result
);
});
return
this
.
all
.
apply
(
this
,
params
);
};
var
isVerbose
=
false
;
...
...
@@ -66,6 +99,7 @@ sqlite3.verbose = function() {
trace
.
extendTrace
(
Database
.
prototype
,
'run'
);
trace
.
extendTrace
(
Database
.
prototype
,
'all'
);
trace
.
extendTrace
(
Database
.
prototype
,
'each'
);
trace
.
extendTrace
(
Database
.
prototype
,
'map'
);
trace
.
extendTrace
(
Database
.
prototype
,
'exec'
);
trace
.
extendTrace
(
Database
.
prototype
,
'close'
);
trace
.
extendTrace
(
Statement
.
prototype
,
'bind'
);
...
...
@@ -73,6 +107,7 @@ sqlite3.verbose = function() {
trace
.
extendTrace
(
Statement
.
prototype
,
'run'
);
trace
.
extendTrace
(
Statement
.
prototype
,
'all'
);
trace
.
extendTrace
(
Statement
.
prototype
,
'each'
);
trace
.
extendTrace
(
Statement
.
prototype
,
'map'
);
trace
.
extendTrace
(
Statement
.
prototype
,
'reset'
);
trace
.
extendTrace
(
Statement
.
prototype
,
'finalize'
);
isVerbose
=
true
;
...
...
test/map.test.js
0 → 100644
View file @
04b72695
var
sqlite3
=
require
(
'sqlite3'
);
var
assert
=
require
(
'assert'
);
if
(
process
.
setMaxListeners
)
process
.
setMaxListeners
(
0
);
exports
[
'test Database#map() with two columns'
]
=
function
(
beforeExit
)
{
var
db
=
new
sqlite3
.
Database
(
':memory:'
);
var
count
=
10
;
var
inserted
=
0
;
var
retrieved
=
false
;
db
.
serialize
(
function
()
{
db
.
run
(
"CREATE TABLE foo (id INT, value TEXT)"
);
var
stmt
=
db
.
prepare
(
"INSERT INTO foo VALUES(?, ?)"
);
for
(
var
i
=
5
;
i
<
count
;
i
++
)
{
stmt
.
run
(
i
,
'Value for '
+
i
,
function
(
err
)
{
if
(
err
)
throw
err
;
inserted
++
;
});
}
stmt
.
finalize
();
db
.
map
(
"SELECT * FROM foo"
,
function
(
err
,
map
)
{
retrieved
=
true
;
assert
.
deepEqual
(
map
,
{
5
:
'Value for 5'
,
6
:
'Value for 6'
,
7
:
'Value for 7'
,
8
:
'Value for 8'
,
9
:
'Value for 9'
});
});
});
beforeExit
(
function
()
{
assert
.
equal
(
inserted
,
5
);
assert
.
ok
(
retrieved
);
});
};
exports
[
'test Database#map() with three columns'
]
=
function
(
beforeExit
)
{
var
db
=
new
sqlite3
.
Database
(
':memory:'
);
var
count
=
10
;
var
inserted
=
0
;
var
retrieved
=
false
;
db
.
serialize
(
function
()
{
db
.
run
(
"CREATE TABLE foo (id INT, value TEXT, other TEXT)"
);
var
stmt
=
db
.
prepare
(
"INSERT INTO foo VALUES(?, ?, ?)"
);
for
(
var
i
=
5
;
i
<
count
;
i
++
)
{
stmt
.
run
(
i
,
'Value for '
+
i
,
null
,
function
(
err
)
{
if
(
err
)
throw
err
;
inserted
++
;
});
}
stmt
.
finalize
();
db
.
map
(
"SELECT * FROM foo"
,
function
(
err
,
map
)
{
retrieved
=
true
;
assert
.
deepEqual
(
map
,
{
5
:
{
id
:
5
,
value
:
'Value for 5'
,
other
:
null
},
6
:
{
id
:
6
,
value
:
'Value for 6'
,
other
:
null
},
7
:
{
id
:
7
,
value
:
'Value for 7'
,
other
:
null
},
8
:
{
id
:
8
,
value
:
'Value for 8'
,
other
:
null
},
9
:
{
id
:
9
,
value
:
'Value for 9'
,
other
:
null
}
});
});
});
beforeExit
(
function
()
{
assert
.
equal
(
inserted
,
5
);
assert
.
ok
(
retrieved
);
});
};
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