Permalink
Browse files

fixed issue #2 and issue #3

  • Loading branch information...
amitkr committed Apr 11, 2012
1 parent 8c6f1b9 commit 8c144304c3f1794e409879eec2b52c677e517dec
Showing with 123 additions and 90 deletions.
  1. +6 −0 ChangeLog
  2. +8 −0 lib/nodejs-db/connection.cxx
  3. +18 −14 lib/nodejs-db/query.cxx
  4. +1 −1 lib/nodejs-db/query.h
  5. +1 −1 lib/nodejs-db/result.h
  6. +53 −32 src/result.cxx
  7. +5 −5 src/result.h
  8. +3 −3 tests/db_conf.json
  9. +28 −34 tests/tests.js
View
@@ -11,3 +11,9 @@
0.0.4
* Removed esql dependency.
* Fixed arguments for query in test.
+
+0.0.5
+ * Fixed issue #2 query returns 0 instead of database name from
+ sysdatabases
+ * Fixed issue #3 query.select() with array of strings as arg produces
+ wrong (quoted) select fields
@@ -55,6 +55,14 @@ bool nodejs_db::Connection::isAlive(bool ping) {
return this->alive;
}
+/**
+ * \fn std::string nodejs_db::Connection::escapeName
+ * escape the given string.
+ * e.g. first.second is escaped as 'first'.'second' is escapeChar is "'"
+ *
+ * \param[in] string String to be escaped
+ * \exception Exception&
+ */
std::string nodejs_db::Connection::escapeName(const std::string& string) const throw(Exception&) {
std::string escaped;
if (string.find_first_of('.') != string.npos) {
View
@@ -560,7 +560,7 @@ v8::Handle<v8::Value> nodejs_db::Query::Set(const v8::Arguments& args) {
THROW_EXCEPTION("Non empty objects should be used for values in set");
}
- for (uint32_t j = 0, limitj = valueProperties->Length(); j < limitj; j++) {
+ for (uint32_t j = 0, max_j = valueProperties->Length(); j < max_j; j++) {
v8::Local<v8::Value> propertyName = valueProperties->Get(j);
v8::String::Utf8Value fieldName(propertyName);
v8::Local<v8::Value> currentValue = values->Get(propertyName);
@@ -703,7 +703,7 @@ nodejs_db::Query::eioExecute(eio_req* eioRequest) {
while (request->result->hasNext()) {
unsigned long* columnLengths = request->result->columnLengths();
assert(columnLengths);
- std::vector<std::string*> *currentRow = request->result->next();
+ std::vector<std::string> *currentRow = request->result->next();
assert(currentRow);
if (!currentRow) {
@@ -724,7 +724,7 @@ nodejs_db::Query::eioExecute(eio_req* eioRequest) {
throw nodejs_db::Exception("Could not create buffer for column lengths");
}
- row->columns = new std::vector<std::string*>(size_t(request->columnCount));
+ row->columns = new std::vector<std::string>(size_t(request->columnCount));
if (row->columns == NULL) {
throw nodejs_db::Exception("Could not create buffer for columns");
}
@@ -737,7 +737,7 @@ nodejs_db::Query::eioExecute(eio_req* eioRequest) {
std::cout
<< "Column: " << i
<< " Length: " << columnLengths[i]
- << " Value: " << *(currentRow->at(i))
+ << " Value: " << currentRow->at(i)
<< std::endl;
#endif
row->columnLengths[i] = columnLengths[i];
@@ -921,6 +921,7 @@ void nodejs_db::Query::executeAsync(execute_request_t* request) {
#ifdef DEBUG
std::cout << ", columnCount " << request->columnCount << std::endl;
#endif
+ /* setup the columns */
for (uint16_t i = 0; i < request->columnCount; i++) {
nodejs_db::Result::Column *currentColumn = request->result->column(i);
@@ -939,6 +940,7 @@ void nodejs_db::Query::executeAsync(execute_request_t* request) {
columns->Set(i, column);
}
+ /* setup rows */
row_t row;
uint64_t index = 0;
@@ -950,12 +952,13 @@ void nodejs_db::Query::executeAsync(execute_request_t* request) {
std::cout << "Row: " << index;
#endif
- row.columnLengths = (unsigned long*) request->result->columnLengths();
+ row.columnLengths = static_cast<unsigned long*>(request->result->columnLengths());
row.columns = request->result->next();
#ifdef DEBUG
if (row.columns) {
- std::cout << ", Columns: " << row.columns->size()
+ std::cout
+ << ", Columns: " << row.columns->size()
<< std::endl;
}
#endif
@@ -1198,7 +1201,7 @@ std::string nodejs_db::Query::fieldName(v8::Local<v8::Value> v) const throw(node
throw nodejs_db::Exception("Non empty objects should be used for value aliasing in select");
}
- for (uint32_t j = 0, limitj = valueProperties->Length(); j < limitj; j++) {
+ for (uint32_t j = 0, max_j = valueProperties->Length(); j < max_j; j++) {
v8::Local<v8::Value> propertyName = valueProperties->Get(j);
v8::String::Utf8Value fn(propertyName); // fieldName
@@ -1250,7 +1253,7 @@ std::string nodejs_db::Query::fieldName(v8::Local<v8::Value> v) const throw(node
}
} else if (v->IsString()) {
v8::String::Utf8Value fn(v->ToString());
- buffer += this->connection->escapeName(*fn);
+ buffer += *fn;
} else {
throw nodejs_db::Exception("Incorrect value type provided as field for select");
}
@@ -1330,15 +1333,16 @@ nodejs_db::Query::row(
row_t* currentRow
) const {
DEBUG_LOG_FUNC;
- v8::Local<v8::Object> row = v8::Object::New();
#ifdef DEBUG
std::cout
- << std::endl
- ;
+ << std::endl
+ ;
#endif
- for (uint16_t j = 0, limitj = result->columnCount(); j < limitj; j++) {
+ v8::Local<v8::Object> row = v8::Object::New();
+
+ for (uint16_t j = 0, max_j = result->columnCount(); j < max_j; ++j) {
nodejs_db::Result::Column* currentColumn = result->column(j);
v8::Local<v8::Value> v;
@@ -1348,8 +1352,8 @@ nodejs_db::Query::row(
<< ", Name: " << currentColumn->getName().c_str();
#endif
- if (currentRow->columns->at(j) != NULL) {
- const char* currentValue = currentRow->columns->at(j)->c_str();
+ if (currentRow->columns->at(j).c_str() != NULL) {
+ const char* currentValue = currentRow->columns->at(j).c_str();
unsigned long currentLength = currentRow->columnLengths[j];
#ifdef DEBUG
std::cout
View
@@ -26,7 +26,7 @@ class Query : public EventEmitter {
protected:
struct row_t {
- std::vector<std::string*>* columns;
+ std::vector<std::string>* columns;
unsigned long* columnLengths;
};
struct execute_request_t {
View
@@ -39,7 +39,7 @@ class Result {
virtual ~Result();
virtual void release() throw();
virtual bool hasNext() const throw(Exception&) = 0;
- virtual std::vector<std::string*>* next() throw(Exception&) = 0;
+ virtual std::vector<std::string>* next() throw(Exception&) = 0;
virtual unsigned long* columnLengths() throw(Exception&) = 0;
virtual uint64_t index() const throw(std::out_of_range&) = 0;
virtual Column* column(uint16_t i) const throw(std::out_of_range&) = 0;
View
@@ -78,90 +78,111 @@ nodejs_db_informix::Result::Column::Column(
if (this->typeName == "blob") {
this->binary = 0;
this->type = BLOB;
- } else
+ }
+ else
if (this->typeName == "boolean") {
this->binary = 0;
this->type = BOOL;
- } else
+ }
+ else
if (this->typeName == "byte") {
this->binary = 0;
this->type = INT;
- } else
+ }
+ else
if (this->typeName == "char") {
this->binary = 0;
- this->type = INT;
- } else
+ this->type = STRING;
+ }
+ else
if (this->typeName == "clob") {
this->binary = 0;
this->type = TEXT;
- } else
+ }
+ else
if (this->typeName == "date") {
this->binary = 0;
this->type = DATE;
- } else
+ }
+ else
if (this->typeName == "datetime") {
this->binary = 0;
this->type = DATETIME;
- } else
+ }
+ else
if (this->typeName == "decimal") {
this->binary = 0;
this->type = NUMBER;
- } else
+ }
+ else
if (this->typeName == "float") {
this->binary = 0;
this->type = NUMBER;
- } else
+ }
+ else
if (this->typeName == "int8") {
this->binary = 0;
this->type = INT;
- } else
+ }
+ else
if (this->typeName == "integer") {
this->binary = 0;
this->type = INT;
- } else
+ }
+ else
if (this->typeName == "interval") {
this->binary = 0;
this->type = INTERVAL;
- } else
+ }
+ else
if (this->typeName == "lvarchar") {
this->binary = 0;
this->type = STRING;
- } else
+ }
+ else
if (this->typeName == "money") {
this->binary = 0;
this->type = MONEY;
- } else
+ }
+ else
if (this->typeName == "serial") {
this->binary = 0;
this->type = INT;
- } else
+ }
+ else
if (this->typeName == "smallfloat") {
this->binary = 0;
this->type = NUMBER;
- } else
+ }
+ else
if (this->typeName == "smallint") {
this->binary = 0;
this->type = NUMBER;
- } else
+ }
+ else
if (this->typeName == "text") {
this->binary = 0;
this->type = TEXT;
- } else
+ }
+ else
if (this->typeName == "varchar") {
this->binary = 0;
this->type = STRING;
- } else {
+ }
+ else {
// sub-columns
if (ti->IsRow()) {
this->typeName = "row";
this->type = ROW;
this->binary = 0;
}
+ else
if (ti->IsCollection()) {
this->typeName = "collection";
this->type = COLLECTION;
this->binary = 0;
}
+ else
if (ti->IsConstructed()) {
this->typeName = "constructed";
this->type = CONSTRUCTED;
@@ -279,7 +300,7 @@ bool nodejs_db_informix::Result::hasNext() const throw() {
return (this->nextRow != NULL);
}
-std::vector<std::string*>*
+std::vector<std::string>*
nodejs_db_informix::Result::next() throw(nodejs_db::Exception&) {
if (this->nextRow == NULL) {
return NULL;
@@ -296,36 +317,36 @@ unsigned long* nodejs_db_informix::Result::columnLengths() throw(nodejs_db::Exce
return this->colLengths;
}
-std::vector<std::string*>*
+std::vector<std::string>*
nodejs_db_informix::Result::row() throw(nodejs_db::Exception&) {
- std::vector<std::string*> *r = new std::vector<std::string*>();
+ std::vector<std::string> *row = new std::vector<std::string>();
ITValue *v = this->resultSet->Fetch();
if (v == NULL) {
// throw nodejs_db::Exception("Cannot fetch ITValue for next row");
return NULL;
}
- ITRow *row;
- if (v->QueryInterface(ITRowIID, (void**) &row) == IT_QUERYINTERFACE_FAILED) {
+ ITRow *r; //ITRow is an abstract class
+ if (v->QueryInterface(ITRowIID, (void**) &r) == IT_QUERYINTERFACE_FAILED) {
throw nodejs_db::Exception("Couldn't fetch ITRow for next row");
} else {
- long nCols = row->NumColumns();
- this->colLengths = new unsigned long[nCols];
- for (long i = 0; i < nCols; ++i) {
- ITValue *cv = row->Column(i);
+ long nc = r->NumColumns(); // number of columns
+ this->colLengths = new unsigned long[nc];
+ for (long i = 0; i < nc; ++i) {
+ ITValue *cv = r->Column(i); // column value
if (cv->IsNull()) {
// append null
- r->push_back(new std::string("null"));
+ row->push_back(std::string("null"));
this->colLengths[i] = strlen("null");
} else {
- r->push_back(new std::string(cv->Printable().Data()));
+ row->push_back(std::string(cv->Printable().Data()));
this->colLengths[i] = cv->Printable().Length();
}
}
}
- return r;
+ return row;
}
uint64_t nodejs_db_informix::Result::index() const throw(std::out_of_range&) {
View
@@ -23,7 +23,7 @@ class Result : public nodejs_db::Result {
nodejs_db::Result::Column::type_t getType() const;
friend std::ostream& operator<< (std::ostream &o, const Column &c) {
- o << "Column { name: " << c.name
+ o << "Column { name: " << c.name
<< ", typeName: " << c.typeName
<< ", type: " << c.type
<< ", binary: " << c.binary
@@ -45,7 +45,7 @@ class Result : public nodejs_db::Result {
~Result();
void release() throw();
bool hasNext() const throw();
- std::vector<std::string*>* next() throw(nodejs_db::Exception&);
+ std::vector<std::string>* next() throw(nodejs_db::Exception&);
unsigned long* columnLengths() throw(nodejs_db::Exception&);
uint64_t index() const throw(std::out_of_range&);
Column* column(uint16_t i) const throw(std::out_of_range&);
@@ -65,13 +65,13 @@ class Result : public nodejs_db::Result {
uint64_t rowNumber;
bool empty;
- std::vector<std::string*>* row() throw(nodejs_db::Exception&);
+ std::vector<std::string>* row() throw(nodejs_db::Exception&);
void free() throw();
private:
ITSet* resultSet;
- std::vector<std::string*>* previousRow;
- std::vector<std::string*>* nextRow;
+ std::vector<std::string>* previousRow;
+ std::vector<std::string>* nextRow;
};
}
View
@@ -1,6 +1,6 @@
{
- "host": "informix",
- "user": "amitkr",
+ "host": "",
+ "user": "",
"password": "",
- "database": "superstores_demo"
+ "database": "sysmaster"
}
Oops, something went wrong.

0 comments on commit 8c14430

Please sign in to comment.