-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbRowParser.js
81 lines (61 loc) · 1.85 KB
/
DbRowParser.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
"use strict"
const util = require('util');
const events = require('events');
const DbRowParser = function(options) {
if (!(this instanceof DbRowParser)) {
return new DbRowParser(options);
}
events.EventEmitter.call(this);
this._key = options.key || 0;
if (!options.properties) {
this._parser = new SingleValueParser(options);
} else if (Array.isArray(options.properties)) {
this._parser = new ObjectParser(options);
} else {
this._parser = new ArrayParser(options);
}
this._previousKey = null;
this._currentObj = null;
}
util.inherits(DbRowParser, events.EventEmitter);
module.exports = DbRowParser;
// HACK: circular reference. Must be after module.exports.
const SingleValueParser = require("./SingleValueParser");
const ArrayParser = require("./ArrayParser");
const ObjectParser = require("./ObjectParser");
DbRowParser.prototype.done = function() {
this.end();
this._currentObj = null;
this._key = null;
}
DbRowParser.prototype.end = function() {
if (this._currentObj == null) {
return;
}
this.emit("new-object", this._currentObj);
}
DbRowParser.prototype.parse = function(row) {
let key = row[this._key];
if (key === this._previousKey) {
this._parser.buildChildObject(this._currentObj, row);
return this._currentObj;
}
if (key == null) {
this.end();
this._currentObj = null;
return;
}
if (key !== this._previousKey) {
this.end();
this._currentObj = null;
}
this._previousKey = key;
this._currentObj = this._parser.buildObject(row);
return this._currentObj;
}
DbRowParser.assertIsParser = function(pName, parser) {
if (!(parser instanceof DbRowParser)) {
let t = typeof parser;
throw new Error(`Unknown Parser for property "${pName}": ${t}`);
}
}