Skip to content

Commit

Permalink
working select of many types
Browse files Browse the repository at this point in the history
  • Loading branch information
joeferner committed Dec 22, 2011
1 parent 52d9ec8 commit 2230bd8
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 15 deletions.
93 changes: 91 additions & 2 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,31 @@ void Connection::CreateColumnsFromResultSet(oracle::occi::ResultSet* rs, std::ve
int type = metadata.getInt(oracle::occi::MetaData::ATTR_DATA_TYPE);
switch(type) {
case oracle::occi::OCCI_TYPECODE_NUMBER:
case oracle::occi::OCCI_TYPECODE_FLOAT:
case oracle::occi::OCCI_TYPECODE_DOUBLE:
case oracle::occi::OCCI_TYPECODE_REAL:
case oracle::occi::OCCI_TYPECODE_DECIMAL:
case oracle::occi::OCCI_TYPECODE_INTEGER:
case oracle::occi::OCCI_TYPECODE_SMALLINT:
col->type = VALUE_TYPE_NUMBER;
break;
case oracle::occi::OCCI_TYPECODE_VARCHAR2:
case oracle::occi::OCCI_TYPECODE_VARCHAR:
case oracle::occi::OCCI_TYPECODE_CHAR:
col->type = VALUE_TYPE_STRING;
break;
case oracle::occi::OCCI_TYPECODE_CLOB:
col->type = VALUE_TYPE_CLOB;
break;
case oracle::occi::OCCI_TYPECODE_DATE:
col->type = VALUE_TYPE_DATE;
break;
case OCI_TYPECODE_TIMESTAMP:
col->type = VALUE_TYPE_TIMESTAMP;
break;
case oracle::occi::OCCI_TYPECODE_BLOB:
col->type = VALUE_TYPE_BLOB;
break;
default:
std::ostringstream message;
message << "CreateColumnsFromResultSet: Unhandled oracle data type: " << type;
Expand All @@ -142,9 +161,21 @@ row_t* Connection::CreateRowFromCurrentResultSetRow(oracle::occi::ResultSet* rs,
case VALUE_TYPE_NUMBER:
row->values.push_back(new oracle::occi::Number(rs->getNumber(colIndex)));
break;
case VALUE_TYPE_DATE:
row->values.push_back(new oracle::occi::Date(rs->getDate(colIndex)));
break;
case VALUE_TYPE_TIMESTAMP:
row->values.push_back(new oracle::occi::Timestamp(rs->getTimestamp(colIndex)));
break;
case VALUE_TYPE_CLOB:
row->values.push_back(new oracle::occi::Clob(rs->getClob(colIndex)));
break;
case VALUE_TYPE_BLOB:
row->values.push_back(new oracle::occi::Blob(rs->getBlob(colIndex)));
break;
default:
std::ostringstream message;
message << "Unhandled type: " << col->type;
message << "CreateRowFromCurrentResultSetRow: Unhandled type: " << col->type;
throw NodeOracleException(message.str());
break;
}
Expand Down Expand Up @@ -196,6 +227,40 @@ void Connection::EIO_Execute(eio_req* req) {
}
}

void CallDateMethod(v8::Local<v8::Date> date, const char* methodName, int val) {
Handle<Value> args[1];
args[0] = Number::New(val);
Local<Function>::Cast(date->Get(String::New(methodName)))->Call(date, 1, args);
}

Local<Date> OracleDateToV8Date(oracle::occi::Date* d) {
int year;
unsigned int month, day, hour, min, sec;
d->getDate(year, month, day, hour, min, sec);
Local<Date> date = Date::Cast(*Date::New(0.0));
CallDateMethod(date, "setSeconds", sec);
CallDateMethod(date, "setMinutes", min);
CallDateMethod(date, "setHours", hour);
CallDateMethod(date, "setDate", day);
CallDateMethod(date, "setMonth", month - 1);
CallDateMethod(date, "setFullYear", year);
return date;
}

Local<Date> OracleTimestampToV8Date(oracle::occi::Timestamp* d) {
int year;
unsigned int month, day;
d->getDate(year, month, day);
Local<Date> date = Date::Cast(*Date::New(0.0));
CallDateMethod(date, "setSeconds", 0);
CallDateMethod(date, "setMinutes", 0);
CallDateMethod(date, "setHours", 0);
CallDateMethod(date, "setDate", day);
CallDateMethod(date, "setMonth", month - 1);
CallDateMethod(date, "setFullYear", year);
return date;
}

Local<Object> Connection::CreateV8ObjectFromRow(ExecuteBaton* baton, row_t* currentRow) {
Local<Object> obj = Object::New();
uint32_t colIndex = 0;
Expand All @@ -217,9 +282,33 @@ Local<Object> Connection::CreateV8ObjectFromRow(ExecuteBaton* baton, row_t* curr
delete v;
}
break;
case VALUE_TYPE_DATE:
{
oracle::occi::Date* v = (oracle::occi::Date*)val;
obj->Set(String::New(col->name.c_str()), OracleDateToV8Date(v));
}
break;
case VALUE_TYPE_TIMESTAMP:
{
oracle::occi::Timestamp* v = (oracle::occi::Timestamp*)val;
obj->Set(String::New(col->name.c_str()), OracleTimestampToV8Date(v));
}
break;
case VALUE_TYPE_CLOB:
{
oracle::occi::Clob* v = (oracle::occi::Clob*)val;
obj->Set(String::New(col->name.c_str()), Null()); // TODO: handle clobs
}
break;
case VALUE_TYPE_BLOB:
{
oracle::occi::Blob* v = (oracle::occi::Blob*)val;
obj->Set(String::New(col->name.c_str()), Null()); // TODO: handle blobs
}
break;
default:
std::ostringstream message;
message << "Unhandled type: " << col->type;
message << "CreateV8ObjectFromRow: Unhandled type: " << col->type;
throw NodeOracleException(message.str());
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <node.h>
#include <unistd.h>
#include <occi.h>
#include <oro.h>
#include "utils.h"
#include "nodeOracleException.h"
#include "executeBaton.h"
Expand Down
13 changes: 8 additions & 5 deletions src/executeBaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ class Connection;
#include <stdlib.h>

enum {
VALUE_TYPE_NULL,
VALUE_TYPE_OUTPUT,
VALUE_TYPE_STRING,
VALUE_TYPE_NUMBER,
VALUE_TYPE_DATE
VALUE_TYPE_NULL = 1,
VALUE_TYPE_OUTPUT = 2,
VALUE_TYPE_STRING = 3,
VALUE_TYPE_NUMBER = 4,
VALUE_TYPE_DATE = 5,
VALUE_TYPE_TIMESTAMP = 6,
VALUE_TYPE_CLOB = 7,
VALUE_TYPE_BLOB = 8
};

struct column_t {
Expand Down
22 changes: 14 additions & 8 deletions tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
ttimestamp TIMESTAMP,
tclob CLOB,
tnclob NCLOB,
tblob BLOB,
tbfile BFILE,
txmltype XMLType);
tblob BLOB);
CREATE SEQUENCE datatype_test_seq START WITH 1 INCREMENT BY 1 NOMAXVALUE;
CREATE TRIGGER datatype_test_pk_trigger BEFORE INSERT ON datatype_test FOR EACH row
BEGIN
Expand Down Expand Up @@ -100,8 +98,8 @@ exports['IntegrationTest'] = nodeunit.testCase({
var date2 = new Date(2011, 11, 1, 1, 2, 3);
self.connection.execute(
"INSERT INTO datatype_test "
+ "(tvarchar2, tnvarchar2, tchar, tnchar, tnumber, tdate, ttimestamp, tclob, tnclob, tblob, txmltype) VALUES "
+ "(:1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11) RETURNING id INTO :12",
+ "(tvarchar2, tnvarchar2, tchar, tnchar, tnumber, tdate, ttimestamp, tclob, tnclob, tblob) VALUES "
+ "(:1, :2, :3, :4, :5, :6, :7, :8, :9, :10) RETURNING id INTO :11",
[
"tvarchar2 value",
"tnvarchar2 value",
Expand All @@ -113,7 +111,6 @@ exports['IntegrationTest'] = nodeunit.testCase({
"tclob value",
"tnclob value",
null, //new Buffer("tblob value"),
"<xmlData></xmlData>",
new oracle.OutParam()
],
function(err, results) {
Expand All @@ -122,9 +119,18 @@ exports['IntegrationTest'] = nodeunit.testCase({

self.connection.execute("SELECT * FROM datatype_test", [], function(err, results) {
if(err) { console.error(err); return; }
console.log(results);
test.equal(results.length, 1);
test.equal(results[0]['NAME'], "Bill O'Neil");
test.equal(results[0]['TVARCHAR2'], "tvarchar2 value");
test.equal(results[0]['TNVARCHAR2'], "tnvarchar2 value");
test.equal(results[0]['TCHAR'], "tchar value ");
test.equal(results[0]['TNCHAR'], "tnchar value ");
test.equal(results[0]['TNUMBER'], 42.5);
test.equal(results[0]['TDATE'].getTime(), date1.getTime());
var date2Timestamp = new Date(2011, 11, 1, 0, 0, 0); // same as date2 but without time
test.equal(results[0]['TTIMESTAMP'].getTime(), date2Timestamp.getTime());
// todo: test.equal(results[0]['TCLOB'], "tclob value");
// todo: test.equal(results[0]['TNCLOB'], "tnclob value");
// todo: test.equal(results[0]['TBLOB'], null);
test.done();
});
});
Expand Down

0 comments on commit 2230bd8

Please sign in to comment.