Skip to content

Commit 755f027

Browse files
committed
added support for commit and rollback
1 parent 05b7187 commit 755f027

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ oracle.connect({ "hostname": "localhost", "user": "test", "password": "test" },
2525
// results.updateCount = 1
2626
// results.returnParam = the id of the person just inserted
2727
});
28+
29+
connection.setAutoCommit(true);
30+
31+
connection.commit(function(err) {
32+
// transaction committed
33+
});
34+
35+
connection.rollback(function(err) {
36+
// transaction rolledback
37+
});
38+
39+
connection.close(); // call this when you are done with the connection
2840
});
2941
```
3042

src/commitBaton.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
#ifndef _commit_baton_h_
3+
#define _commit_baton_h_
4+
5+
#include "connection.h"
6+
7+
class CommitBaton {
8+
public:
9+
CommitBaton(Connection* connection, v8::Handle<v8::Function>* callback) {
10+
this->connection = connection;
11+
this->callback = Persistent<Function>::New(*callback);
12+
}
13+
~CommitBaton() {
14+
callback.Dispose();
15+
}
16+
17+
Connection *connection;
18+
v8::Persistent<v8::Function> callback;
19+
};
20+
21+
#endif

src/connection.cpp

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
#include "connection.h"
3+
#include "executeBaton.h"
4+
#include "commitBaton.h"
5+
#include "rollbackBaton.h"
36
#include "outParam.h"
47
#include <vector>
58

@@ -15,6 +18,9 @@ void Connection::Init(Handle<Object> target) {
1518

1619
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "execute", Execute);
1720
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "close", Close);
21+
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "setAutoCommit", SetAutoCommit);
22+
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "commit", Commit);
23+
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "rollback", Rollback);
1824

1925
target->Set(String::NewSymbol("Connection"), constructorTemplate->GetFunction());
2026
}
@@ -69,6 +75,61 @@ Handle<Value> Connection::Close(const Arguments& args) {
6975
return Undefined();
7076
}
7177

78+
Handle<Value> Connection::Commit(const Arguments& args) {
79+
Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());
80+
81+
REQ_FUN_ARG(0, callback);
82+
83+
CommitBaton* baton;
84+
try {
85+
baton = new CommitBaton(connection, &callback);
86+
} catch(NodeOracleException &ex) {
87+
Handle<Value> argv[2];
88+
argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
89+
argv[1] = Undefined();
90+
callback->Call(Context::GetCurrent()->Global(), 2, argv);
91+
return Undefined();
92+
}
93+
94+
eio_custom(EIO_Commit, EIO_PRI_DEFAULT, EIO_AfterCommit, baton);
95+
ev_ref(EV_DEFAULT_UC);
96+
97+
connection->Ref();
98+
99+
return Undefined();
100+
}
101+
102+
Handle<Value> Connection::Rollback(const Arguments& args) {
103+
Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());
104+
105+
REQ_FUN_ARG(0, callback);
106+
107+
RollbackBaton* baton;
108+
try {
109+
baton = new RollbackBaton(connection, &callback);
110+
} catch(NodeOracleException &ex) {
111+
Handle<Value> argv[2];
112+
argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
113+
argv[1] = Undefined();
114+
callback->Call(Context::GetCurrent()->Global(), 2, argv);
115+
return Undefined();
116+
}
117+
118+
eio_custom(EIO_Rollback, EIO_PRI_DEFAULT, EIO_AfterRollback, baton);
119+
ev_ref(EV_DEFAULT_UC);
120+
121+
connection->Ref();
122+
123+
return Undefined();
124+
}
125+
126+
Handle<Value> Connection::SetAutoCommit(const Arguments& args) {
127+
Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());
128+
REQ_BOOL_ARG(0, autoCommit);
129+
connection->m_autoCommit = autoCommit;
130+
return Undefined();
131+
}
132+
72133
void Connection::closeConnection() {
73134
if(m_environment && m_connection) {
74135
m_environment->terminateConnection(m_connection);
@@ -187,6 +248,44 @@ row_t* Connection::CreateRowFromCurrentResultSetRow(oracle::occi::ResultSet* rs,
187248
return row;
188249
}
189250

251+
void Connection::EIO_Commit(eio_req* req) {
252+
CommitBaton* baton = static_cast<CommitBaton*>(req->data);
253+
254+
baton->connection->m_connection->commit();
255+
}
256+
257+
int Connection::EIO_AfterCommit(eio_req* req) {
258+
CommitBaton* baton = static_cast<CommitBaton*>(req->data);
259+
ev_unref(EV_DEFAULT_UC);
260+
baton->connection->Unref();
261+
262+
Handle<Value> argv[2];
263+
argv[0] = Undefined();
264+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
265+
266+
delete baton;
267+
return 0;
268+
}
269+
270+
void Connection::EIO_Rollback(eio_req* req) {
271+
RollbackBaton* baton = static_cast<RollbackBaton*>(req->data);
272+
273+
baton->connection->m_connection->rollback();
274+
}
275+
276+
int Connection::EIO_AfterRollback(eio_req* req) {
277+
RollbackBaton* baton = static_cast<RollbackBaton*>(req->data);
278+
ev_unref(EV_DEFAULT_UC);
279+
baton->connection->Unref();
280+
281+
Handle<Value> argv[2];
282+
argv[0] = Undefined();
283+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
284+
285+
delete baton;
286+
return 0;
287+
}
288+
190289
void Connection::EIO_Execute(eio_req* req) {
191290
ExecuteBaton* baton = static_cast<ExecuteBaton*>(req->data);
192291

@@ -198,6 +297,7 @@ void Connection::EIO_Execute(eio_req* req) {
198297
try {
199298
//printf("%s\n", baton->sql.c_str());
200299
stmt = baton->connection->m_connection->createStatement(baton->sql);
300+
stmt->setAutoCommit(baton->connection->m_autoCommit);
201301
int outputParam = SetValuesOnStatement(stmt, baton->values);
202302

203303
int status = stmt->execute();
@@ -303,13 +403,13 @@ Local<Object> Connection::CreateV8ObjectFromRow(ExecuteBaton* baton, row_t* curr
303403
break;
304404
case VALUE_TYPE_CLOB:
305405
{
306-
oracle::occi::Clob* v = (oracle::occi::Clob*)val;
406+
//oracle::occi::Clob* v = (oracle::occi::Clob*)val;
307407
obj->Set(String::New(col->name.c_str()), Null()); // TODO: handle clobs
308408
}
309409
break;
310410
case VALUE_TYPE_BLOB:
311411
{
312-
oracle::occi::Blob* v = (oracle::occi::Blob*)val;
412+
//oracle::occi::Blob* v = (oracle::occi::Blob*)val;
313413
obj->Set(String::New(col->name.c_str()), Null()); // TODO: handle blobs
314414
}
315415
break;

src/connection.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ class Connection : ObjectWrap {
2020
static Handle<Value> New(const Arguments& args);
2121
static Handle<Value> Execute(const Arguments& args);
2222
static Handle<Value> Close(const Arguments& args);
23+
static Handle<Value> Commit(const Arguments& args);
24+
static Handle<Value> Rollback(const Arguments& args);
25+
static Handle<Value> SetAutoCommit(const Arguments& args);
2326
static Persistent<FunctionTemplate> constructorTemplate;
2427
static void EIO_Execute(eio_req* req);
2528
static int EIO_AfterExecute(eio_req* req);
29+
static void EIO_Commit(eio_req* req);
30+
static int EIO_AfterCommit(eio_req* req);
31+
static void EIO_Rollback(eio_req* req);
32+
static int EIO_AfterRollback(eio_req* req);
2633
void closeConnection();
2734

2835
Connection();
@@ -40,6 +47,7 @@ class Connection : ObjectWrap {
4047

4148
oracle::occi::Connection* m_connection;
4249
oracle::occi::Environment* m_environment;
50+
bool m_autoCommit;
4351
};
4452

4553
#endif

src/rollbackBaton.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
#ifndef _rollback_baton_h_
3+
#define _rollback_baton_h_
4+
5+
#include "connection.h"
6+
7+
class RollbackBaton {
8+
public:
9+
RollbackBaton(Connection* connection, v8::Handle<v8::Function>* callback) {
10+
this->connection = connection;
11+
this->callback = Persistent<Function>::New(*callback);
12+
}
13+
~RollbackBaton() {
14+
callback.Dispose();
15+
}
16+
17+
Connection *connection;
18+
v8::Persistent<v8::Function> callback;
19+
};
20+
21+
#endif

src/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
#include <stdio.h>
88
#include <stdlib.h>
99

10+
#define REQ_BOOL_ARG(I, VAR) \
11+
if (args.Length() <= (I) || !args[I]->IsBoolean()) \
12+
return ThrowException(Exception::TypeError(String::New("Argument " #I " must be a bool"))); \
13+
bool VAR = args[I]->IsTrue();
14+
1015
#define REQ_STRING_ARG(I, VAR) \
1116
if (args.Length() <= (I) || !args[I]->IsString()) \
1217
return ThrowException(Exception::TypeError(String::New("Argument " #I " must be a string"))); \

0 commit comments

Comments
 (0)