-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathreader.cpp
More file actions
139 lines (114 loc) · 4.25 KB
/
reader.cpp
File metadata and controls
139 lines (114 loc) · 4.25 KB
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
#include <string.h>
#include "connection.h"
#include "executeBaton.h"
#include "reader.h"
using namespace std;
Persistent<FunctionTemplate> Reader::constructorTemplate;
void Reader::Init(Handle<Object> target) {
UNI_SCOPE(scope);
Local<FunctionTemplate> t = NanNew<FunctionTemplate>(New);
uni::Reset(constructorTemplate, t);
uni::Deref(constructorTemplate)->InstanceTemplate()->SetInternalFieldCount(1);
uni::Deref(constructorTemplate)->SetClassName(NanNew<String>("Reader"));
NODE_SET_PROTOTYPE_METHOD(uni::Deref(constructorTemplate), "nextRows", NextRows);
target->Set(NanNew<String>("Reader"), uni::Deref(constructorTemplate)->GetFunction());
}
Reader::Reader(): ObjectWrap() {
}
Reader::~Reader() {
delete m_baton;
m_baton = NULL;
}
uni::CallbackType Reader::New(const uni::FunctionCallbackInfo& args) {
UNI_SCOPE(scope);
Reader* reader = new Reader();
reader->Wrap(args.This());
UNI_RETURN(scope, args, args.This());
}
void Reader::setBaton(ReaderBaton* baton) {
m_baton = baton;
}
uni::CallbackType Reader::NextRows(const uni::FunctionCallbackInfo& args) {
UNI_SCOPE(scope);
Reader* reader = ObjectWrap::Unwrap<Reader>(args.This());
ReaderBaton* baton = reader->m_baton;
if (baton->error) {
Local<String> message = NanNew<String>(baton->error->c_str());
UNI_THROW(Exception::Error(message));
}
if (baton->busy) {
UNI_THROW(Exception::Error(NanNew<String>("invalid state: reader is busy with another nextRows call")));
}
baton->busy = true;
if (args.Length() > 1) {
REQ_INT_ARG(0, count);
REQ_FUN_ARG(1, callback);
baton->count = count;
uni::Reset(baton->callback, callback);
} else {
REQ_FUN_ARG(0, callback);
baton->count = baton->connection->getPrefetchRowCount();
uni::Reset(baton->callback, callback);
}
if (baton->count <= 0) baton->count = 1;
uv_work_t* req = new uv_work_t();
req->data = baton;
uv_queue_work(uv_default_loop(), req, EIO_NextRows, (uv_after_work_cb)EIO_AfterNextRows);
baton->connection->Ref();
UNI_RETURN(scope, args, NanUndefined());
}
void Reader::EIO_NextRows(uv_work_t* req) {
ReaderBaton* baton = static_cast<ReaderBaton*>(req->data);
baton->rows = new vector<row_t*>();
if (baton->done) return;
if (!baton->connection->getConnection()) {
baton->error = new std::string("Connection already closed");
return;
}
if (!baton->rs) {
try {
baton->stmt = baton->connection->getConnection()->createStatement(baton->sql);
baton->stmt->setAutoCommit(baton->connection->getAutoCommit());
baton->stmt->setPrefetchRowCount(baton->count);
Connection::SetValuesOnStatement(baton->stmt, baton);
if (baton->error) return;
int status = baton->stmt->execute();
if (status != oracle::occi::Statement::RESULT_SET_AVAILABLE) {
baton->error = new std::string("Connection already closed");
return;
}
baton->rs = baton->stmt->getResultSet();
} catch (oracle::occi::SQLException &ex) {
baton->error = new string(ex.getMessage());
return;
}
Connection::CreateColumnsFromResultSet(baton->rs, baton, baton->columns);
if (baton->error) return;
}
for (int i = 0; i < baton->count && baton->rs->next(); i++) {
row_t* row = Connection::CreateRowFromCurrentResultSetRow(baton->rs, baton, baton->columns);
if (baton->error) return;
baton->rows->push_back(row);
}
if (baton->rows->size() < (size_t)baton->count) baton->done = true;
}
void Reader::EIO_AfterNextRows(uv_work_t* req, int status) {
UNI_SCOPE(scope);
ReaderBaton* baton = static_cast<ReaderBaton*>(req->data);
baton->busy = false;
baton->connection->Unref();
// transfer callback to local and dispose persistent handle
Local<Function> cb = uni::HandleToLocal(uni::Deref(baton->callback));
NanDisposePersistent(baton->callback);
Handle<Value> argv[2];
Connection::handleResult(baton, argv);
baton->ResetRows();
if (baton->done || baton->error) {
// free occi resources so that we don't run out of cursors if gc is not fast enough
// reader destructor will delete the baton and everything else.
baton->ResetStatement();
}
delete req;
// invoke callback at the very end because callback may re-enter nextRows.
NanMakeCallback(NanGetCurrentContext()->Global(), cb, 2, argv);
}