Skip to content

Commit bbb647f

Browse files
committed
Allowing empty result sets
1 parent b9c64a5 commit bbb647f

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

lib/node-db

src/connection.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,5 @@ node_db::Result* node_db_mysql::Connection::query(const std::string& query) cons
8383
throw node_db::Exception(mysql_error(this->connection));
8484
}
8585

86-
MYSQL_RES* result = mysql_store_result(this->connection);
87-
if (result == NULL) {
88-
throw node_db::Exception("Could not fetch result of query");
89-
}
90-
91-
return new node_db_mysql::Result(this->connection, result);
86+
return new node_db_mysql::Result(this->connection);
9287
}

src/result.cc

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,40 @@ node_db::Result::Column::type_t node_db_mysql::Result::Column::getType() const {
6767
return this->type;
6868
}
6969

70-
node_db_mysql::Result::Result(MYSQL* connection, MYSQL_RES* result) throw(node_db::Exception&)
70+
node_db_mysql::Result::Result(MYSQL* connection) throw(node_db::Exception&)
7171
: columns(NULL),
7272
totalColumns(0),
7373
rowNumber(0),
74+
empty(true),
7475
connection(connection),
75-
result(result),
7676
previousRow(NULL),
7777
nextRow(NULL) {
78-
if (this->result == NULL) {
79-
throw node_db::Exception("Invalid result");
80-
}
8178

82-
MYSQL_FIELD* fields = mysql_fetch_fields(this->result);
83-
if (fields == NULL) {
84-
throw node_db::Exception("Could not buffer columns");
85-
}
79+
this->result = mysql_store_result(this->connection);
80+
if (result == NULL && mysql_field_count(this->connection) != 0) {
81+
throw node_db::Exception(mysql_error(this->connection));
82+
} else if (result != NULL) {
83+
this->empty = false;
8684

87-
this->totalColumns = mysql_num_fields(this->result);
88-
if (this->totalColumns > 0) {
89-
this->columns = new Column*[this->totalColumns];
90-
if (this->columns == NULL) {
91-
throw node_db::Exception("Could not allocate storage for columns");
85+
MYSQL_FIELD* fields = mysql_fetch_fields(this->result);
86+
if (fields == NULL) {
87+
throw node_db::Exception("Could not buffer columns");
9288
}
9389

94-
for (uint16_t i = 0; i < this->totalColumns; i++) {
95-
this->columns[i] = new Column(fields[i]);
90+
this->totalColumns = mysql_num_fields(this->result);
91+
if (this->totalColumns > 0) {
92+
this->columns = new Column*[this->totalColumns];
93+
if (this->columns == NULL) {
94+
throw node_db::Exception("Could not allocate storage for columns");
95+
}
96+
97+
for (uint16_t i = 0; i < this->totalColumns; i++) {
98+
this->columns[i] = new Column(fields[i]);
99+
}
96100
}
97-
}
98101

99-
this->nextRow = this->row();
102+
this->nextRow = this->row();
103+
}
100104
}
101105

102106
node_db_mysql::Result::~Result() {
@@ -175,3 +179,7 @@ uint64_t node_db_mysql::Result::count() const throw(node_db::Exception&) {
175179
bool node_db_mysql::Result::isBuffered() const throw() {
176180
return (!this->result->handle || this->result->handle->status != MYSQL_STATUS_USE_RESULT);
177181
}
182+
183+
bool node_db_mysql::Result::isEmpty() const throw() {
184+
return this->empty;
185+
}

src/result.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Result : public node_db::Result {
2525
bool binary;
2626
};
2727

28-
explicit Result(MYSQL* connection, MYSQL_RES* result) throw(node_db::Exception&);
28+
explicit Result(MYSQL* connection) throw(node_db::Exception&);
2929
~Result();
3030
bool hasNext() const;
3131
char** next() throw(node_db::Exception&);
@@ -38,11 +38,13 @@ class Result : public node_db::Result {
3838
uint16_t warningCount() const;
3939
uint64_t count() const throw(node_db::Exception&);
4040
bool isBuffered() const throw();
41+
bool isEmpty() const throw();
4142

4243
protected:
4344
Column** columns;
4445
uint16_t totalColumns;
4546
uint64_t rowNumber;
47+
bool empty;
4648

4749
char** row() throw(node_db::Exception&);
4850

0 commit comments

Comments
 (0)