I having been using Poco::Data and have run into an issue when selecting/querying for data and the data in the column is NULL.
I have the following database schema:
- Code: Select all
TABLE: person
| COLUMN | DATA_TYPE |
--------------------------------
| name | TEXT |
| age | INT |
| account_balance | DECIMAL |
Now all of these columns are optional and are able to have a value of NULL.
So, i do a simple insert:
- Code: Select all
struct Person {
std::string name;
int age;
double accountBalance;
}
bool persistPerson() {
try {
Poco::Data::Session databaseSession;
// connect to the database...
databaseSession.begin();
Poco::Data::Statement insert(databaseSession);
insert << "INSERT INTO person VALUES(DEFAULT, DEFAULT, DEFAULT)";
insert.execute();
databaseSession.commit();
} catch (Poco::Exception& ex) {
std::string error = ex.displayText();
databaseSession.rollback();
return false;
}
return true;
}
Now when i try to do a query:
- Code: Select all
std::list<Person>* queryAllPersons() {
std::list<Person>* results = new std::list<Person>;
try {
Poco::Data::Session databaseSession;
// connect to the database...
Person person_result;
Poco::Data::Statement select(databaseSession);
select << "SELECT * FROM person",
::Poco::Data::into(person_result.name),
::Poco::Data::into(person_result.age),
::Poco::Data::into(person_result.account_balance),
::Poco::Data::range(0, 1);
while (!select.done()) {
select.execute();
// copy the result into the list
results->push_back(person_result);
}
} catch (Poco::Exception& ex) {
std::cout << ex.displayText() << std::endl;
results.clear();
}
return results;
}
I get the following results:
- Code: Select all
person.name = ""
person.age = 0
person.account_balance = 0.0
It seems every column that was NULL gets the default value for that data type put into it. I found in the documentation that you can use the:
- Code: Select all
template < typename T > Extraction < T > * into(
T & t,
const T & def
);
That works well for unsigned int's: into(my_int, -1);
But doesn't work so well for signed integers, doubles, or strings. (In my world "", DBL_MAX, INT_MAX, etc are valid values).
Is there a better way to detect NULL values coming out of the database?
-Nick





