Skip to content

Commit 2230bd8

Browse files
committed
working select of many types
1 parent 52d9ec8 commit 2230bd8

File tree

4 files changed

+114
-15
lines changed

4 files changed

+114
-15
lines changed

src/connection.cpp

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,31 @@ void Connection::CreateColumnsFromResultSet(oracle::occi::ResultSet* rs, std::ve
114114
int type = metadata.getInt(oracle::occi::MetaData::ATTR_DATA_TYPE);
115115
switch(type) {
116116
case oracle::occi::OCCI_TYPECODE_NUMBER:
117+
case oracle::occi::OCCI_TYPECODE_FLOAT:
118+
case oracle::occi::OCCI_TYPECODE_DOUBLE:
119+
case oracle::occi::OCCI_TYPECODE_REAL:
120+
case oracle::occi::OCCI_TYPECODE_DECIMAL:
121+
case oracle::occi::OCCI_TYPECODE_INTEGER:
122+
case oracle::occi::OCCI_TYPECODE_SMALLINT:
117123
col->type = VALUE_TYPE_NUMBER;
118124
break;
119125
case oracle::occi::OCCI_TYPECODE_VARCHAR2:
120126
case oracle::occi::OCCI_TYPECODE_VARCHAR:
127+
case oracle::occi::OCCI_TYPECODE_CHAR:
121128
col->type = VALUE_TYPE_STRING;
122129
break;
130+
case oracle::occi::OCCI_TYPECODE_CLOB:
131+
col->type = VALUE_TYPE_CLOB;
132+
break;
133+
case oracle::occi::OCCI_TYPECODE_DATE:
134+
col->type = VALUE_TYPE_DATE;
135+
break;
136+
case OCI_TYPECODE_TIMESTAMP:
137+
col->type = VALUE_TYPE_TIMESTAMP;
138+
break;
139+
case oracle::occi::OCCI_TYPECODE_BLOB:
140+
col->type = VALUE_TYPE_BLOB;
141+
break;
123142
default:
124143
std::ostringstream message;
125144
message << "CreateColumnsFromResultSet: Unhandled oracle data type: " << type;
@@ -142,9 +161,21 @@ row_t* Connection::CreateRowFromCurrentResultSetRow(oracle::occi::ResultSet* rs,
142161
case VALUE_TYPE_NUMBER:
143162
row->values.push_back(new oracle::occi::Number(rs->getNumber(colIndex)));
144163
break;
164+
case VALUE_TYPE_DATE:
165+
row->values.push_back(new oracle::occi::Date(rs->getDate(colIndex)));
166+
break;
167+
case VALUE_TYPE_TIMESTAMP:
168+
row->values.push_back(new oracle::occi::Timestamp(rs->getTimestamp(colIndex)));
169+
break;
170+
case VALUE_TYPE_CLOB:
171+
row->values.push_back(new oracle::occi::Clob(rs->getClob(colIndex)));
172+
break;
173+
case VALUE_TYPE_BLOB:
174+
row->values.push_back(new oracle::occi::Blob(rs->getBlob(colIndex)));
175+
break;
145176
default:
146177
std::ostringstream message;
147-
message << "Unhandled type: " << col->type;
178+
message << "CreateRowFromCurrentResultSetRow: Unhandled type: " << col->type;
148179
throw NodeOracleException(message.str());
149180
break;
150181
}
@@ -196,6 +227,40 @@ void Connection::EIO_Execute(eio_req* req) {
196227
}
197228
}
198229

230+
void CallDateMethod(v8::Local<v8::Date> date, const char* methodName, int val) {
231+
Handle<Value> args[1];
232+
args[0] = Number::New(val);
233+
Local<Function>::Cast(date->Get(String::New(methodName)))->Call(date, 1, args);
234+
}
235+
236+
Local<Date> OracleDateToV8Date(oracle::occi::Date* d) {
237+
int year;
238+
unsigned int month, day, hour, min, sec;
239+
d->getDate(year, month, day, hour, min, sec);
240+
Local<Date> date = Date::Cast(*Date::New(0.0));
241+
CallDateMethod(date, "setSeconds", sec);
242+
CallDateMethod(date, "setMinutes", min);
243+
CallDateMethod(date, "setHours", hour);
244+
CallDateMethod(date, "setDate", day);
245+
CallDateMethod(date, "setMonth", month - 1);
246+
CallDateMethod(date, "setFullYear", year);
247+
return date;
248+
}
249+
250+
Local<Date> OracleTimestampToV8Date(oracle::occi::Timestamp* d) {
251+
int year;
252+
unsigned int month, day;
253+
d->getDate(year, month, day);
254+
Local<Date> date = Date::Cast(*Date::New(0.0));
255+
CallDateMethod(date, "setSeconds", 0);
256+
CallDateMethod(date, "setMinutes", 0);
257+
CallDateMethod(date, "setHours", 0);
258+
CallDateMethod(date, "setDate", day);
259+
CallDateMethod(date, "setMonth", month - 1);
260+
CallDateMethod(date, "setFullYear", year);
261+
return date;
262+
}
263+
199264
Local<Object> Connection::CreateV8ObjectFromRow(ExecuteBaton* baton, row_t* currentRow) {
200265
Local<Object> obj = Object::New();
201266
uint32_t colIndex = 0;
@@ -217,9 +282,33 @@ Local<Object> Connection::CreateV8ObjectFromRow(ExecuteBaton* baton, row_t* curr
217282
delete v;
218283
}
219284
break;
285+
case VALUE_TYPE_DATE:
286+
{
287+
oracle::occi::Date* v = (oracle::occi::Date*)val;
288+
obj->Set(String::New(col->name.c_str()), OracleDateToV8Date(v));
289+
}
290+
break;
291+
case VALUE_TYPE_TIMESTAMP:
292+
{
293+
oracle::occi::Timestamp* v = (oracle::occi::Timestamp*)val;
294+
obj->Set(String::New(col->name.c_str()), OracleTimestampToV8Date(v));
295+
}
296+
break;
297+
case VALUE_TYPE_CLOB:
298+
{
299+
oracle::occi::Clob* v = (oracle::occi::Clob*)val;
300+
obj->Set(String::New(col->name.c_str()), Null()); // TODO: handle clobs
301+
}
302+
break;
303+
case VALUE_TYPE_BLOB:
304+
{
305+
oracle::occi::Blob* v = (oracle::occi::Blob*)val;
306+
obj->Set(String::New(col->name.c_str()), Null()); // TODO: handle blobs
307+
}
308+
break;
220309
default:
221310
std::ostringstream message;
222-
message << "Unhandled type: " << col->type;
311+
message << "CreateV8ObjectFromRow: Unhandled type: " << col->type;
223312
throw NodeOracleException(message.str());
224313
break;
225314
}

src/connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <node.h>
77
#include <unistd.h>
88
#include <occi.h>
9+
#include <oro.h>
910
#include "utils.h"
1011
#include "nodeOracleException.h"
1112
#include "executeBaton.h"

src/executeBaton.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ class Connection;
1414
#include <stdlib.h>
1515

1616
enum {
17-
VALUE_TYPE_NULL,
18-
VALUE_TYPE_OUTPUT,
19-
VALUE_TYPE_STRING,
20-
VALUE_TYPE_NUMBER,
21-
VALUE_TYPE_DATE
17+
VALUE_TYPE_NULL = 1,
18+
VALUE_TYPE_OUTPUT = 2,
19+
VALUE_TYPE_STRING = 3,
20+
VALUE_TYPE_NUMBER = 4,
21+
VALUE_TYPE_DATE = 5,
22+
VALUE_TYPE_TIMESTAMP = 6,
23+
VALUE_TYPE_CLOB = 7,
24+
VALUE_TYPE_BLOB = 8
2225
};
2326

2427
struct column_t {

tests/integration.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
ttimestamp TIMESTAMP,
2727
tclob CLOB,
2828
tnclob NCLOB,
29-
tblob BLOB,
30-
tbfile BFILE,
31-
txmltype XMLType);
29+
tblob BLOB);
3230
CREATE SEQUENCE datatype_test_seq START WITH 1 INCREMENT BY 1 NOMAXVALUE;
3331
CREATE TRIGGER datatype_test_pk_trigger BEFORE INSERT ON datatype_test FOR EACH row
3432
BEGIN
@@ -100,8 +98,8 @@ exports['IntegrationTest'] = nodeunit.testCase({
10098
var date2 = new Date(2011, 11, 1, 1, 2, 3);
10199
self.connection.execute(
102100
"INSERT INTO datatype_test "
103-
+ "(tvarchar2, tnvarchar2, tchar, tnchar, tnumber, tdate, ttimestamp, tclob, tnclob, tblob, txmltype) VALUES "
104-
+ "(:1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11) RETURNING id INTO :12",
101+
+ "(tvarchar2, tnvarchar2, tchar, tnchar, tnumber, tdate, ttimestamp, tclob, tnclob, tblob) VALUES "
102+
+ "(:1, :2, :3, :4, :5, :6, :7, :8, :9, :10) RETURNING id INTO :11",
105103
[
106104
"tvarchar2 value",
107105
"tnvarchar2 value",
@@ -113,7 +111,6 @@ exports['IntegrationTest'] = nodeunit.testCase({
113111
"tclob value",
114112
"tnclob value",
115113
null, //new Buffer("tblob value"),
116-
"<xmlData></xmlData>",
117114
new oracle.OutParam()
118115
],
119116
function(err, results) {
@@ -122,9 +119,18 @@ exports['IntegrationTest'] = nodeunit.testCase({
122119

123120
self.connection.execute("SELECT * FROM datatype_test", [], function(err, results) {
124121
if(err) { console.error(err); return; }
125-
console.log(results);
126122
test.equal(results.length, 1);
127-
test.equal(results[0]['NAME'], "Bill O'Neil");
123+
test.equal(results[0]['TVARCHAR2'], "tvarchar2 value");
124+
test.equal(results[0]['TNVARCHAR2'], "tnvarchar2 value");
125+
test.equal(results[0]['TCHAR'], "tchar value ");
126+
test.equal(results[0]['TNCHAR'], "tnchar value ");
127+
test.equal(results[0]['TNUMBER'], 42.5);
128+
test.equal(results[0]['TDATE'].getTime(), date1.getTime());
129+
var date2Timestamp = new Date(2011, 11, 1, 0, 0, 0); // same as date2 but without time
130+
test.equal(results[0]['TTIMESTAMP'].getTime(), date2Timestamp.getTime());
131+
// todo: test.equal(results[0]['TCLOB'], "tclob value");
132+
// todo: test.equal(results[0]['TNCLOB'], "tnclob value");
133+
// todo: test.equal(results[0]['TBLOB'], null);
128134
test.done();
129135
});
130136
});

0 commit comments

Comments
 (0)