forked from tokumine/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.js
More file actions
150 lines (129 loc) · 4.29 KB
/
Copy pathtypes.js
File metadata and controls
150 lines (129 loc) · 4.29 KB
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
//maps types from javascript to postgres and vise-versa
//the empty parse function
var noParse = function(val) {
return val;
}
//parses PostgreSQL server formatted date strings into javascript date objects
var parseDate = function(isoDate) {
//TODO this could do w/ a refactor
var dateMatcher = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?/;
var match = dateMatcher.exec(isoDate);
//could not parse date
if(!match) {
return null;
}
var year = match[1];
var month = parseInt(match[2],10)-1;
var day = match[3];
var hour = parseInt(match[4],10);
var min = parseInt(match[5],10);
var seconds = parseInt(match[6], 10);
var miliString = match[7];
var mili = 0;
if(miliString) {
mili = 1000 * parseFloat(miliString);
}
var tZone = /([Z|+\-])(\d{2})?(\d{2})?/.exec(isoDate.split(' ')[1]);
//minutes to adjust for timezone
var tzAdjust = 0;
if(tZone) {
var type = tZone[1];
switch(type) {
case 'Z': break;
case '-':
tzAdjust = -(((parseInt(tZone[2],10)*60)+(parseInt(tZone[3]||0,10))));
break;
case '+':
tzAdjust = (((parseInt(tZone[2],10)*60)+(parseInt(tZone[3]||0,10))));
break;
default:
throw new Error("Unidentifed tZone part " + type);
}
}
var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);
var date = new Date(utcOffset - (tzAdjust * 60* 1000));
return date;
};
var parseBool = function(val) {
return val === 't';
}
var parseIntegerArray = function(val) {
return JSON.parse(val.replace("{","[").replace("}","]"));
};
var parseStringArray = function(val) {
if (!val) return null;
if (val === '{}') return [];
if (val[0] !== '{' || val[val.length-1] !== '}')
throw "Not postgresql array! (" + arrStr + ")";
var x = val.substring(1, val.length - 1);
x = x.match(/(NULL|[^,]+|"((?:.|\n|\r)*?)(?!\\)"|\{((?:.|\n|\r)*?(?!\\)\}) (,|$))/mg);
if (x === null) throw "Not postgre array";
return x.map(function (el) {
if (el === 'NULL') return null;
if (el[0] === '{') return arguments.callee(el);
if (el[0] === '\"') return el.substring(1, el.length - 1).replace('\\\"', '\"');
return el;
});
};
var NUM = '([+-]?\\d+)';
var YEAR = NUM + '\\s+years?';
var MON = NUM + '\\s+mons?';
var DAY = NUM + '\\s+days?';
var TIME = '([+-])?(\\d\\d):(\\d\\d):(\\d\\d)';
var INTERVAL = [YEAR,MON,DAY,TIME].map(function(p){ return "("+p+")?" }).join('\\s*');
var parseInterval = function(val) {
if (!val) return {};
var m = new RegExp(INTERVAL).exec(val);
var i = {};
if (m[2]) i.years = parseInt(m[2]);
if (m[4]) i.months = parseInt(m[4]);
if (m[6]) i.days = parseInt(m[6]);
if (m[9]) i.hours = parseInt(m[9]);
if (m[10]) i.minutes = parseInt(m[10]);
if (m[11]) i.seconds = parseInt(m[11]);
if (m[8] == '-'){
if (i.hours) i.hours *= -1;
if (i.minutes) i.minutes *= -1;
if (i.seconds) i.seconds *= -1;
}
for (field in i){
if (i[field] == 0)
delete i[field];
}
return i;
};
var parseByteA = function(val) {
return new Buffer(val.replace(/\\([0-7]{3})/g, function (full_match, code) {
return String.fromCharCode(parseInt(code, 8));
}).replace(/\\\\/g, "\\"), "binary");
}
var typeParsers = {};
//registers a method used to parse a string representing a particular
//oid type into a javascript type
var registerStringTypeParser = function(oid, converter) {
typeParsers[oid] = converter;
};
//returns a function used to convert a specific type (specified by
//oid) into a result javascript type
var getStringTypeParser = function(oid) {
return typeParsers[oid] || noParse;
};
//default string type parser registrations
registerStringTypeParser(20, parseInt);
registerStringTypeParser(21, parseInt);
registerStringTypeParser(23, parseInt);
registerStringTypeParser(26, parseInt);
registerStringTypeParser(1700, parseFloat);
registerStringTypeParser(700, parseFloat);
registerStringTypeParser(701, parseFloat);
registerStringTypeParser(16, parseBool);
registerStringTypeParser(1114, parseDate);
registerStringTypeParser(1184, parseDate);
registerStringTypeParser(1007, parseIntegerArray);
registerStringTypeParser(1009, parseStringArray);
registerStringTypeParser(1186, parseInterval);
registerStringTypeParser(17, parseByteA);
module.exports = {
registerStringTypeParser: registerStringTypeParser,
getStringTypeParser: getStringTypeParser,
}