|
8 | 8 | #include <memory> |
9 | 9 | #include <vector> |
10 | 10 |
|
| 11 | +#if __has_include(<optional>) |
| 12 | +#include <optional> |
| 13 | +#endif |
| 14 | + |
11 | 15 | #ifdef _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT |
12 | 16 | #include <boost/optional.hpp> |
13 | 17 | #endif |
@@ -237,6 +241,11 @@ namespace sqlite { |
237 | 241 | friend database_binder& operator <<(database_binder& db, const std::u16string& txt); |
238 | 242 |
|
239 | 243 |
|
| 244 | +#if __has_include(<optional>) |
| 245 | + template <typename OptionalT> friend database_binder& operator <<(database_binder& db, const std::optional<OptionalT>& val); |
| 246 | + template <typename OptionalT> friend void get_col_from_db(database_binder& db, int inx, std::optional<OptionalT>& o); |
| 247 | +#endif |
| 248 | + |
240 | 249 | #ifdef _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT |
241 | 250 | template <typename BoostOptionalT> friend database_binder& operator <<(database_binder& db, const boost::optional<BoostOptionalT>& val); |
242 | 251 | template <typename BoostOptionalT> friend void get_col_from_db(database_binder& db, int inx, boost::optional<BoostOptionalT>& o); |
@@ -545,7 +554,33 @@ namespace sqlite { |
545 | 554 | ++db._inx; |
546 | 555 | return db; |
547 | 556 | } |
548 | | - // boost::optinal support for NULL values |
| 557 | + // std::optional support for NULL values |
| 558 | +#if __has_include(<optional>) |
| 559 | + template <typename OptionalT> inline database_binder& operator <<(database_binder& db, const std::optional<OptionalT>& val) { |
| 560 | + if(val) { |
| 561 | + return operator << (std::move(db), std::move(*val)); |
| 562 | + } |
| 563 | + int hresult; |
| 564 | + if((hresult = sqlite3_bind_null(db._stmt.get(), db._inx)) != SQLITE_OK) { |
| 565 | + exceptions::throw_sqlite_error(hresult); |
| 566 | + } |
| 567 | + |
| 568 | + ++db._inx; |
| 569 | + return db; |
| 570 | + } |
| 571 | + |
| 572 | + template <typename OptionalT> inline void get_col_from_db(database_binder& db, int inx, std::optional<OptionalT>& o) { |
| 573 | + if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) { |
| 574 | + o.reset(); |
| 575 | + } else { |
| 576 | + OptionalT v; |
| 577 | + get_col_from_db(db, inx, v); |
| 578 | + o = std::move(v); |
| 579 | + } |
| 580 | + } |
| 581 | +#endif |
| 582 | + |
| 583 | + // boost::optional support for NULL values |
549 | 584 | #ifdef _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT |
550 | 585 | template <typename BoostOptionalT> inline database_binder& operator <<(database_binder& db, const boost::optional<BoostOptionalT>& val) { |
551 | 586 | if(val) { |
@@ -574,7 +609,7 @@ namespace sqlite { |
574 | 609 | // Some ppl are lazy so we have a operator for proper prep. statemant handling. |
575 | 610 | void inline operator++(database_binder& db, int) { db.execute(); db.reset(); } |
576 | 611 |
|
577 | | - // Convert the rValue binder to a reference and call first op<<, its needed for the call that creates the binder (be carfull of recursion here!) |
| 612 | + // Convert the rValue binder to a reference and call first op<<, its needed for the call that creates the binder (be carefull of recursion here!) |
578 | 613 | template<typename T> database_binder& operator << (database_binder&& db, const T& val) { return db << val; } |
579 | 614 |
|
580 | 615 | } |
0 commit comments