-
Notifications
You must be signed in to change notification settings - Fork 53
/
access_mdb_sample.js
109 lines (104 loc) · 3.62 KB
/
access_mdb_sample.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
var win32ole = require('win32ole');
win32ole.print('access_mdb_sample\n');
console.log(win32ole.version());
var path = require('path');
var cwd = path.join(win32ole.MODULEDIRNAME, '..');
var fs = require('fs');
var tmpdir = path.join(cwd, 'test/tmp');
if(!fs.existsSync(tmpdir)) fs.mkdirSync(tmpdir);
var outfile = path.join(tmpdir, 'access_mdb_sample.mdb');
var display_or_edit_all = function(rs, ed){
var getRSvalue = function(rs, fieldname){
return rs.Fields(fieldname).Value;
}
rs.MoveFirst();
/*
In ParseUnaryExpression() < v8/src/parser.cc >
v8::Object::ToBoolean() is called directly for unary operator '!'
instead of v8::Object::valueOf()
so NamedPropertyHandler will not be called
Local<Boolean> ToBoolean(); // How to fake ? override v8::Value::ToBoolean
*/
//while(rs.Eof != true){ // It works. (without unary operator !)
//while(!rs.Eof){ // It does not work.
while(!rs.Eof._){ // *** It works. oops!
win32ole.print('id: ');
win32ole.print(getRSvalue(rs, 'id'));
win32ole.print(', c1: ');
win32ole.print(getRSvalue(rs, 'c1'));
win32ole.print(', c2: ');
win32ole.print(getRSvalue(rs, 'c2'));
win32ole.print(', c3: ');
win32ole.print(getRSvalue(rs, 'c3'));
win32ole.print('\n');
if(ed){
rs.Edit();
var id = getRSvalue(rs, 'id');
rs.Fields('c2').Value = id * 1000;
rs.Update(); // rs.CancelUpdate();
}
rs.MoveNext();
}
};
var adox_sample = function(filename){
if(fs.existsSync(filename)) fs.unlinkSync(filename);
console.log('creating mdb (by ADOX): "' + filename + '" ...');
var dsn = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + filename + ';';
// dsn += ('Locale Identifier=' + CAT_LOCALE_(language));
var db = win32ole.client.Dispatch('ADOX.Catalog');
db.Create(dsn);
var sql_create_table = 'create table testtbl (id autoincrement primary key,';
sql_create_table += ' c1 varchar(255), c2 integer, c3 varchar(255));';
var cn = db.ActiveConnection._; // ***
cn.Execute(sql_create_table);
for(var i = 11; i < 13; ++i){
var sql_insert = "insert into testtbl (c1, c2, c3) values";
sql_insert += " ('a(', " + i + ", ')z');";
cn.Execute(sql_insert);
}
console.log('open RS');
var rs = win32ole.client.Dispatch('ADODB.Recordset');
rs.ActiveConnection = cn;
var sql_select = 'select * from testtbl;';
rs.Open(sql_select, cn, 1, 3); // adOpenKeyset, adLockOptimistic
display_or_edit_all(rs, false);
rs.Close();
rs = null;
console.log('RS released');
cn.Close();
cn = null;
// db.Finalize(); and db.Close() db.Disconnect() db.Release() is wrong
db = null;
console.log('disconnected (ADOX)');
};
var ole_automation_sample = function(filename){
console.log('open mdb (by OLE Automation): "' + filename + '" ...');
var mdb = win32ole.client.Dispatch('Access.Application');
mdb.Visible = true;
// mdb.NewCurrentDatabase(filename); // to create new mdb
mdb.OpenCurrentDatabase(filename); // to open exists mdb
var cdb = mdb.CurrentDb();
for(var i = 101; i < 103; ++i){
var sql_insert = "insert into testtbl (c1, c2, c3) values";
sql_insert += " ('p(', " + i + ", ')q');";
cdb.Execute(sql_insert);
}
var sql_select = 'select * from testtbl;';
var rs = cdb.OpenRecordset(sql_select);
display_or_edit_all(rs, true);
display_or_edit_all(rs, false);
rs.Close();
cdb.Close();
mdb.CloseCurrentDatabase();
mdb.Quit(1); // 1: acQuitSaveAll (default), 2: acQuitSaveNone
cdb = null;
mdb = null;
console.log('disconnected (OLE Automation)');
};
try{
adox_sample(outfile);
win32ole.sleep(2000, true, true);
ole_automation_sample(outfile);
}catch(e){
console.log('*** exception cached ***\n' + e);
}