| 1 | // |
| 2 | // Binder.cpp |
| 3 | // |
| 4 | // Library: SQL/SQLite |
| 5 | // Package: SQLite |
| 6 | // Module: Binder |
| 7 | // |
| 8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/SQL/SQLite/Binder.h" |
| 16 | #include "Poco/SQL/SQLite/Utility.h" |
| 17 | #include "Poco/SQL/Date.h" |
| 18 | #include "Poco/SQL/Time.h" |
| 19 | #include "Poco/Exception.h" |
| 20 | #include "Poco/DateTimeFormatter.h" |
| 21 | #include "Poco/DateTimeFormat.h" |
| 22 | #include <cstdlib> |
| 23 | |
| 24 | |
| 25 | using Poco::DateTimeFormatter; |
| 26 | using Poco::DateTimeFormat; |
| 27 | |
| 28 | |
| 29 | namespace Poco { |
| 30 | namespace SQL { |
| 31 | namespace SQLite { |
| 32 | |
| 33 | |
| 34 | Binder::Binder(sqlite3_stmt* pStmt): |
| 35 | _pStmt(pStmt) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | |
| 40 | Binder::~Binder() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void Binder::bind(std::size_t pos, const Poco::Int32 &val, Direction /*dir*/, const WhenNullCb& /*nullCb*/) |
| 46 | { |
| 47 | int rc = sqlite3_bind_int(_pStmt, (int) pos, val); |
| 48 | checkReturn(rc); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void Binder::bind(std::size_t pos, const Poco::Int64 &val, Direction /*dir*/, const WhenNullCb& /*nullCb*/) |
| 53 | { |
| 54 | int rc = sqlite3_bind_int64(_pStmt, (int) pos, val); |
| 55 | checkReturn(rc); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | #ifndef POCO_LONG_IS_64_BIT |
| 60 | void Binder::bind(std::size_t pos, const long &val, Direction /*dir*/, const WhenNullCb& /*nullCb*/) |
| 61 | { |
| 62 | long tmp = static_cast<long>(val); |
| 63 | int rc = sqlite3_bind_int(_pStmt, (int) pos, tmp); |
| 64 | checkReturn(rc); |
| 65 | } |
| 66 | |
| 67 | void Binder::bind(std::size_t pos, const unsigned long &val, Direction /*dir*/, const WhenNullCb& /*nullCb*/) |
| 68 | { |
| 69 | long tmp = static_cast<long>(val); |
| 70 | int rc = sqlite3_bind_int(_pStmt, (int) pos, tmp); |
| 71 | checkReturn(rc); |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | |
| 76 | void Binder::bind(std::size_t pos, const double &val, Direction /*dir*/, const WhenNullCb& /*nullCb*/) |
| 77 | { |
| 78 | int rc = sqlite3_bind_double(_pStmt, (int) pos, val); |
| 79 | checkReturn(rc); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void Binder::bind(std::size_t pos, const std::string& val, Direction /*dir*/, const WhenNullCb& /*nullCb*/) |
| 84 | { |
| 85 | int rc = sqlite3_bind_text(_pStmt, (int) pos, val.c_str(), (int) val.size()*sizeof(char), SQLITE_TRANSIENT); |
| 86 | checkReturn(rc); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void Binder::bind(std::size_t pos, const Date& val, Direction dir, const WhenNullCb& nullCb) |
| 91 | { |
| 92 | DateTime dt(val.year(), val.month(), val.day()); |
| 93 | std::string str(DateTimeFormatter::format(dt, Utility::SQLITE_DATE_FORMAT)); |
| 94 | bind(pos, str, dir, nullCb); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void Binder::bind(std::size_t pos, const Time& val, Direction dir, const WhenNullCb& nullCb) |
| 99 | { |
| 100 | DateTime dt; |
| 101 | dt.assign(dt.year(), dt.month(), dt.day(), val.hour(), val.minute(), val.second()); |
| 102 | std::string str(DateTimeFormatter::format(dt, Utility::SQLITE_TIME_FORMAT)); |
| 103 | bind(pos, str, dir, nullCb); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void Binder::bind(std::size_t pos, const DateTime& val, Direction dir, const WhenNullCb& nullCb) |
| 108 | { |
| 109 | std::string dt(DateTimeFormatter::format(val, DateTimeFormat::ISO8601_FORMAT)); |
| 110 | bind(pos, dt, dir, nullCb); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | void Binder::bind(std::size_t pos, const NullData&, Direction, const std::type_info& /*bindType*/) |
| 115 | { |
| 116 | sqlite3_bind_null(_pStmt, static_cast<int>(pos)); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void Binder::checkReturn(int rc) |
| 121 | { |
| 122 | if (rc != SQLITE_OK) |
| 123 | Utility::throwException(sqlite3_db_handle(_pStmt), rc); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | } } } // namespace Poco::SQL::SQLite |
| 128 | |