-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
157 lines (131 loc) · 3.87 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
var EventEmitter = require('events').EventEmitter
var sqlite3 = require('sqlite3')
var inherits = require('inherits')
var Readable = require('stream').Readable
var adapter = exports
adapter.name = 'sqlite3'
adapter.verbose = sqlite3.verbose;
adapter.createQuery = function (text, values, callback) {
if (text instanceof SQLite3Query) return text
return new SQLite3Query(text, values, callback)
}
adapter.createConnection = function (opts, callback) {
var filename;
if (opts.host) {
filename = opts.host + (opts.database || '')
} else {
filename = opts.database
}
if (!filename || filename == '/:memory') filename = ':memory:'
var mode = Object.keys(opts).map(function (k) {
return (k == k.toUpperCase() && sqlite3[k]) | 0;
}).reduce(function (mode, flag) {
return mode | flag;
}, 0)
if (!mode) mode = (sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE)
var db = new sqlite3.Database(filename, mode)
db.serialize()
return new SQLite3Connection(db, callback)
}
inherits(SQLite3Connection, EventEmitter)
function SQLite3Connection(db, callback) {
EventEmitter.call(this)
this._db = db
var self = this
this._db.on('open', function () { self.emit('open') })
this._db.on('close', function () { self.emit('end') })
this._db.on('error', function (err) { self.emit('error', err) })
if (callback) {
self.once('error', callback)
self.once('open', function handleOpenEvent () {
self.removeListener('error', callback)
callback(null, self)
})
}
}
SQLite3Connection.prototype.adapter = adapter
SQLite3Connection.prototype.query = function (text, values, callback) {
var query = adapter.createQuery(text, values, callback)
this.emit('query', query)
if (query.text.match(/^\s*(insert|update|replace)\s+/i)) {
runQuery(this._db, query);
} else {
eachQuery(this._db, query);
}
return query
}
function runQuery (db, query) {
db.run(query.text, query.values, function (err) {
query.complete(err,
(this ? this.changes : 0),
(this ? this.lastID : -1))
})
}
function eachQuery (db, query) {
db.each(query.text, query.values, onRow, function (err, count) {
query.complete(err, count);
})
function onRow (err, row) {
query.onRow(err, row)
}
}
SQLite3Connection.prototype.end = function (callback) {
if (callback) this.on('end', callback)
this._db.close()
}
inherits(SQLite3Query, Readable)
function SQLite3Query(text, values, callback) {
Readable.call(this, {objectMode: true})
this.text = text
this._fields = null
this._result = { rows: [] }
this._errored = false
if (typeof values == 'function') {
callback = values
values = []
}
this.values = values || []
if (this.callback = callback) {
this.on('error', function(error) {
this.callback(error);
})
this.on('data', function (row) {
this._result.rows.push(row)
})
}
}
SQLite3Query.prototype._read = function () {}
SQLite3Query.prototype.onRow = function (err, row) {
if (this._errored) return
this._gotData = true
if (err) {
this._errored = true
this.emit('close')
this.emit('error', err)
return;
}
if (!this._result.fields) {
this._result.fields = Object.keys(row).map(function (name) {
return { name: name }
})
this.emit('fields', this._result.fields)
}
this.push(row)
}
SQLite3Query.prototype.complete = function (err, count, lastId, affectedRows) {
this.push(null)
if (this._errored) return // we've emitted an error from the row callback
if (!err && !this._gotData) this.emit('fields', [])
this.emit('close')
if (err) return this.emit('error', err)
this._result.rowCount = count
this._result.lastInsertId = lastId
if (affectedRows) {
this._result.affectedRows = affectedRows
this._result.changedRows = affectedRows
}
if (this.callback) {
this.callback(null, this._result)
}
}