-
Notifications
You must be signed in to change notification settings - Fork 2
/
statement.js
134 lines (121 loc) · 3.56 KB
/
statement.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
// @flow
import { Observable } from "rxjs/Observable";
import type { Statement } from "sqlite3";
import { reports, error } from "./event";
/**
* Statement Wrapper Object that adds [RxJS 5](https://github.com/ReactiveX/RxJS) API to [sqlite3](https://github.com/mapbox/node-sqlite3/)
*/
export default class RxStatement {
stmt: Statement;
constructor(stmt: Statement) {
this.stmt = stmt;
}
/**
* make new error event stream.
*/
errors(): Observable<{ error: Error }> {
return Observable.create(subs => {
let fn = err => subs.next(error(this, err));
let t = "error";
this.stmt.on(t, fn);
return () => {
this.stmt.removeListener(t, fn);
};
});
}
/**
* Binds parameters and executes the statement.
* @param params see {@link https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback|passing bind parameters}
*/
run<T>(...params: any[]): Observable<T> {
return toObservable(this, this.stmt.run, params);
}
/**
* Binds parameters and executes the statement and retrieves a row.
* @param params see {@link https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback|passing bind parameters}
*/
get<T>(...params: any[]): Observable<T> {
return toObservable(this, this.stmt.get, params);
}
/**
* Binds parameters and executes the statement and retrieves all rows at one time.
* @param params see {@link https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback|passing bind parameters}
*/
all<T>(...params: any[]): Observable<T[]> {
return toObservable(this, this.stmt.all, params);
}
/**
* Binds parameters and executes the statement and retrieves all rows.
* @param params see {@link https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback|passing bind parameters}
*/
each<T>(...params: any[]): Observable<T> {
return Observable.create(subs => {
this.stmt.each.apply(this.stmt, [
...params,
(err, row) => {
if (reports(this.stmt, subs, err)) {
subs.next(row);
}
},
err => {
if (reports(this.stmt, subs, err)) {
subs.complete();
}
}
]);
return () => {
this.stmt.finalize();
};
});
}
/**
* Binds parameters to the prepared statement
* @param params see {@link https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback|passing bind parameters}
*/
bind(...params: any[]): Observable<RxStatement> {
return Observable.create(subs => {
this.stmt.bind.apply(this.stmt, [...params, toOnceCallback(this, subs)]);
});
}
/**
*
*/
reset(): Observable<RxStatement> {
return Observable.create(subs => {
this.stmt.reset(toOnceCallback(this, subs));
});
}
finalize(): Observable<RxStatement> {
return Observable.create(subs => {
this.stmt.finalize(toOnceCallback(this, subs));
});
}
}
function toOnceCallback(rxs: RxStatement, subs) {
return err => {
if (reports(rxs.stmt, subs, err)) {
subs.next(rxs);
subs.complete();
}
};
}
function toObservable(rxs: RxStatement, stmtFn, params = []) {
return Observable.create(subs => {
stmtFn.apply(rxs.stmt, [
...params,
(err, vals) => {
if (reports(rxs.stmt, subs, err)) {
subs.next(vals);
subs.complete();
}
}
]);
return () => {
rxs.stmt.finalize(err => {
if (err) {
rxs.stmt.emit("error", err);
}
});
};
});
}