/
connection.js
51 lines (42 loc) · 1.54 KB
/
connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var sqlite = require('sqlite')
knit.engine.sqlite.Connection = (function() {
var _ = knit._util,
C = function(sqlite_connection_string) {
this._sqlite_connection_string = sqlite_connection_string
},
p = C.prototype
p.open = function() { this._conn = sqlite.openDatabaseSync(this._sqlite_connection_string) }
p.close = function() { this._conn.close() }
p.execute = function(statement) { this._conn.query(statement.sql, statement.values) }
function queryAsync(self, statement, rowCallback) {
self._conn.query(statement.sql, statement.values, function(rows) {
_.each(_.map(rows, function(row){return row}), rowCallback)
rowCallback(null)
})
}
function querySync(self, statement) {
//need to read these into a regular array for some reason...
return _.map(self._conn.query(statement.sql, statement.values), function(row){return row})
}
p.query = function(statement, rowCallback) {
if (rowCallback) {
return queryAsync(this, statement, rowCallback)
} else {
return querySync(this, statement)
}
}
p.listTables = function() {
var result = this._conn.query(
"select name from sqlite_master " +
"where type='table' " +
"order by name"
)
return _.map(result, function(row){return row.name})
}
p.columnInformation = function(tableName) {
return _.map(this.query({sql:"pragma table_info(" + tableName + ")"}), function(columnInfo){
return {name:columnInfo.name, type:columnInfo.type, pk:columnInfo.pk}
})
}
return C
})()