From e4bcc27463598290143f6803f05a3f6aaa368c4b Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Thu, 15 Aug 2013 09:32:24 +0200 Subject: [PATCH] is_null() throws for invalid rows result_row objects can be constructed with raw result rows representing the end of the result set. Field values must not be accessed for such rows. Thus, is_null() now throws an exception when called for a field of an invalid row. --- include/sqlpp11/boolean.h | 9 ++++++++- include/sqlpp11/numeric.h | 9 ++++++++- include/sqlpp11/text.h | 9 ++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/sqlpp11/boolean.h b/include/sqlpp11/boolean.h index a1a36947..85658aeb 100644 --- a/include/sqlpp11/boolean.h +++ b/include/sqlpp11/boolean.h @@ -95,13 +95,20 @@ namespace sqlpp bool _is_trivial() const { return value() == false; } - bool is_null() const { return _is_null; } + bool is_null() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + return _is_null; + } + bool value() const { if (not _is_valid) throw exception("accessing value in non-existing row"); return _value; } + operator bool() const { return value(); } private: diff --git a/include/sqlpp11/numeric.h b/include/sqlpp11/numeric.h index cfbed5d3..1c9af404 100644 --- a/include/sqlpp11/numeric.h +++ b/include/sqlpp11/numeric.h @@ -72,13 +72,20 @@ namespace sqlpp bool _is_trivial() const { return value() == 0; } - bool is_null() const { return _is_null; } + bool is_null() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + return _is_null; + } + int64_t value() const { if (not _is_valid) throw exception("accessing value in non-existing row"); return _value; } + operator int64_t() const { return value(); } private: diff --git a/include/sqlpp11/text.h b/include/sqlpp11/text.h index 6eeb64ab..0a906486 100644 --- a/include/sqlpp11/text.h +++ b/include/sqlpp11/text.h @@ -86,13 +86,20 @@ namespace sqlpp bool operator==(const std::string& rhs) const { return value() == rhs; } bool operator!=(const std::string& rhs) const { return not operator==(rhs); } - bool is_null() const { return _is_null; } + bool is_null() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + return _is_null; + } + std::string value() const { if (not _is_valid) throw exception("accessing value in non-existing row"); return _value; } + operator std::string() const { return value(); } private: