-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessdb.js
387 lines (373 loc) · 13.3 KB
/
accessdb.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* ACCESSdb JavaScript Library v0.9.2
*
* Joshua Faulkenberry
* Dual licensed under the MIT and GPL licenses.
*
* Date: 2009-03-14
* Revision: 4
*/
(function() {
//Helper function to handle Date formatting
window.Date.prototype.format = function(format) {
if (format == "@") { return this.getTime(); }
var MONTH_NAMES = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var DAY_NAMES = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
var LZ = function(x) { return (x < 0 || x > 9 ? "" : "0") + x}
var date = this;
format = format + "";
var result = "";
var i_format = 0;
var c = "";
var token = "";
var y = date.getYear() + "";
var M = date.getMonth() + 1;
var d = date.getDate();
var E = date.getDay();
var H = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
var yyyy, yy, MMM, MM, dd, hh, h, mm, ss, ampm, HH, H, KK, K, kk, k;
// Convert real date parts into formatted versions
var value = new Object();
if (y.length < 4) {
y = "" + (y - 0 + 1900);
}
value["y"] = "" + y;
value["yyyy"] = y;
value["yy"] = y.substring(2, 4);
value["M"] = M;
value["MM"] = LZ(M);
value["MMM"] = MONTH_NAMES[M - 1];
value["NNN"] = MONTH_NAMES[M + 11];
value["d"] = d;
value["dd"] = LZ(d);
value["E"] = DAY_NAMES[E + 7];
value["EE"] = DAY_NAMES[E];
value["H"] = H;
value["HH"] = LZ(H);
if (H == 0) {
value["h"] = 12;
}
else if (H > 12) {
value["h"] = H - 12;
}
else {
value["h"] = H;
}
value["hh"] = LZ(value["h"]);
if (H > 11) {
value["K"] = H - 12;
}
else {
value["K"] = H;
}
value["k"] = H + 1;
value["KK"] = LZ(value["K"]);
value["kk"] = LZ(value["k"]);
if (H > 11) {
value["a"] = "PM";
}
else {
value["a"] = "AM";
}
value["m"] = m;
value["mm"] = LZ(m);
value["s"] = s;
value["ss"] = LZ(s);
while (i_format < format.length) {
c = format.charAt(i_format);
token = "";
while ((format.charAt(i_format) == c) && (i_format < format.length)) {
token += format.charAt(i_format++);
}
if (value[token] != null) {
result = result + value[token];
}
else {
result = result + token;
}
}
return result;
}
ACCESSdb = function(dataSrc, options) {
this.options = options || {};
this.options.showErrors = this.options.showErrors || false;
this.options.adOpenDynamic = this.options.adOpenDynamic || 2;
this.options.adLockOptimistic = this.options.adLockOptimistic || 3;
this.options.persist = this.options.persist || false;
this.options.user = this.options.user || "";
this.options.password = this.options.password || "";
this.options.wrkgrpFile = "Jet OLEDB:System Database="+this.options.wrkgrpFile+";" || "";
this.dataSource = dataSrc;
this.provider = "Microsoft.Jet.OLEDB.4.0";
this.conn = new ActiveXObject("ADODB.Connection");
this.query = function(query, options) {
if (!options) {
options = {};
}
var result = true;
var rs = new ActiveXObject("ADODB.Recordset");
try {
rs.open(query, this.conn, this.options.adOpenDynamic, this.options.adLockOptimistic);
}
catch (e) {
if (this.options.showErrors) {
alert("Query " + e.name + "\n\n" + e.description);
}
if (options.errorHandler) {
options.errorHandler(e);
}
result = false;
}
if (rs.Fields.Count) {
if (!rs.bof && !rs.eof) {
if (options.json) {
result = this.outJSON(rs);
}
else if (options.xml) {
result = this.outXML(rs, options.xml);
}
else if (options.table) {
result = this.outTable(rs, options.table);
}
else {
result = eval("(" + this.outJSON(rs) + ")");
}
}
else {
result = false;
}
rs.close();
}
else {
result = false;
}
return result;
};
this.insert = function(table, data, options) {
if (!options) {
options = {};
}
var insList = [];
var insStr = "INSERT INTO " + table + " (";
data = this.translate(data);
if (!data) { return false; }
for (var key in data[0]) {
if (key != "ID") {
insStr += "" + key + ",";
}
}
insStr = insStr.slice(0, insStr.length - 1) + ") VALUES(%%%)\n";
for (var x = 0, row; row = data[x]; x++) {
var rowIns = "";
for (var key in row) {
if (key != "ID") {
var val = row[key];
if (typeof(val) == "number" || typeof(val) == "boolean") {
rowIns += val + ",";
}
else {
val = val.replace(/\"/g, '"');
val = val.replace(/\'/g, ''');
rowIns += "\"" + val + "\",";
}
}
}
insList[insList.length] = insStr.replace("%%%", rowIns.slice(0, rowIns.length - 1));
}
var noerr = true;
for (var x = 0, sql; sql = insList[x]; x++) {
if (!this.query(sql, options)) {
noerr = false;
break;
}
}
return noerr;
};
this.translate = function(data) {
var tranObj;
if (typeof(data) == "string") {
if (data.replace(/^\s*(.*?)\s*$/, "$1").charAt(0) == "<" && data.replace(/^\s*(.*?)\s*$/, "$1").charAt(data.replace(/^\s*(.*?)\s*$/, "$1").length - 1) == ">") {
//Should be XML String
var err = false;
try {
varxmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(data);
}
catch (e) {
tranObj = false;
err = true;
if (this.options.showErrors) {
alert("XML " + e.name + "\n\n" + e.description);
}
}
if (!err) tranObj = this.convertXML(xmlDoc);
}
else if (data.replace(/^\s*(.*?)\s*$/, "$1").charAt(0) == "[" && data.replace(/^\s*(.*?)\s*$/, "$1").charAt(data.replace(/^\s*(.*?)\s*$/, "$1").length - 1) == "]") {
//Should be JSON String
try {
tranObj = eval("(" + data + ")");
}
catch (e) {
tranObj = false;
if (this.options.showErrors) {
alert("JSON " + e.name + "\n\n" + e.description);
}
}
}
}
else if (typeof(data) == "object") {
if (data.nodeName) {
//Should be XML Object
tranObj = this.convertXML(data);
}
else if (data[0]) {
//Should be JSON Object
tranObj = data;
}
}
return tranObj;
};
this.convertXML = function(xmlDoc) {
var jsObj = [];
for (var x = 0, row; row = xmlDoc.getElementsByTagName("record")[x]; x++) {
jsObj[x] = {};
for (var y = 0, col; col = row.childNodes[y]; y++) {
jsObj[x][col.nodeName] = col.text;
}
}
return jsObj;
};
this.outJSON = function(rs) {
var json = "[";
rs.MoveFirst();
while (!rs.eof) {
json += '{';
for (var x = 0; x < rs.Fields.Count; x++) {
json += '"' + rs.Fields(x).Name + '":';
var val = rs.Fields(x).Value;
if (typeof(val) == "string") {
val = val.replace(/\"/g, '"');
val = val.replace(/\'/g, ''');
val = '"' + val + '"';
}
if (typeof(val) == "date") {
val = "new Date(\"" + val + "\")";
}
json += val + ',';
}
json = json.slice(0, json.length - 1);
rs.MoveNext();
json += '},';
}
json = json.slice(0, json.length - 1);
json += ']';
return json;
};
this.outXML = function(rs, options) {
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><recordset>";
rs.MoveFirst();
while (!rs.eof) {
xml += '<record>';
for (var x = 0; x < rs.Fields.Count; x++) {
var val = rs.Fields(x).Value;
if (typeof(val) == "string") {
val = val.replace(/\&/g, '&');
val = val.replace(/\</g, '<');
val = val.replace(/\>/g, '>');
}
else if (typeof(val) == "date" && options.formatDates) {
if (typeof(options.formatDates) == "string") {
val = (new Date((val))).format(options.formatDates);
}
else {
for (var col in options.formatDates) {
if (col == rs.Fields(x).Name) {
val = (new Date((val))).format(options.formatDates[col]);
}
}
}
}
xml += "<" + rs.Fields(x).Name + ">" + val + "</" + rs.Fields(x).Name + ">";
}
xml += '</record>';
rs.MoveNext();
}
xml += '</recordset>';
if (!options.stringOut) {
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xml);
xml = xmlDoc;
}
return xml;
};
this.outTable = function(rs, options) {
var tbl = document.createElement("table");
tbl.cellPadding = 0;
tbl.cellSpacing = 0;
var tbody = document.createElement("tbody");
tbl.appendChild(tbody);
if (options.id) {
tbl.id = options.id;
}
if (options.className) {
tbl.className = options.className;
}
rs.MoveFirst();
if (!options.noHeaders) {
var row = document.createElement("tr");
tbody.appendChild(row);
for (var x = 0; x < rs.Fields.Count; x++) {
var hdr = document.createElement("th");
hdr.innerHTML = rs.Fields(x).Name;
row.appendChild(hdr);
}
}
while (!rs.eof) {
var row = document.createElement("tr");
tbody.appendChild(row);
for (var x = 0; x < rs.Fields.Count; x++) {
var val = rs.Fields(x).Value;
if (typeof(val) == "string") {
val = val.replace(/\&/g, '&');
val = val.replace(/\</g, '<');
val = val.replace(/\>/g, '>');
}
else if (typeof(val) == "date" && options.formatDates) {
if (typeof(options.formatDates) == "string") {
val = (new Date((val))).format(options.formatDates);
}
else {
for (var col in options.formatDates) {
if (col == rs.Fields(x).Name) {
val = (new Date((val))).format(options.formatDates[col]);
}
}
}
}
var cell = document.createElement("td");
cell.innerHTML = val;
row.appendChild(cell);
}
rs.MoveNext();
}
if (options.stringOut) { return tbl.outerHTML; }
return tbl;
};
this.kill = function() {
this.conn.close();
delete this;
};
try {
this.conn.open("Provider = " + this.provider + ";Data Source = " + this.dataSource + ";"+this.options.wrkgrpFile+"Persist Security Info = " + this.options.persist, this.options.user, this.options.password);
}
catch (e) {
if (this.options.showErrors) {
alert("Load DB " + e.name + "\n\n" + e.description);
}
}
};
})();