| 1 | // |
| 2 | // Extractor.cpp |
| 3 | // |
| 4 | // Library: SQL/SQLite |
| 5 | // Package: SQLite |
| 6 | // Module: Extractor |
| 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/Extractor.h" |
| 16 | #include "Poco/SQL/SQLite/Utility.h" |
| 17 | #include "Poco/SQL/Date.h" |
| 18 | #include "Poco/SQL/Time.h" |
| 19 | #include "Poco/SQL/LOB.h" |
| 20 | #include "Poco/SQL/SQLException.h" |
| 21 | #include "Poco/DateTimeParser.h" |
| 22 | #include "Poco/Exception.h" |
| 23 | #if defined(POCO_UNBUNDLED) |
| 24 | #include <sqlite3.h> |
| 25 | #else |
| 26 | #include "sqlite3.h" |
| 27 | #endif |
| 28 | #include <cstdlib> |
| 29 | |
| 30 | |
| 31 | using Poco::DateTimeParser; |
| 32 | |
| 33 | |
| 34 | namespace Poco { |
| 35 | namespace SQL { |
| 36 | namespace SQLite { |
| 37 | |
| 38 | |
| 39 | Extractor::(sqlite3_stmt* pStmt): |
| 40 | _pStmt(pStmt) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | Extractor::() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | |
| 50 | bool Extractor::(std::size_t pos, Poco::Int32& val) |
| 51 | { |
| 52 | if (isNull(pos)) return false; |
| 53 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | bool Extractor::(std::size_t pos, Poco::Int64& val) |
| 59 | { |
| 60 | if (isNull(pos)) return false; |
| 61 | val = sqlite3_column_int64(_pStmt, (int) pos); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | #ifndef POCO_LONG_IS_64_BIT |
| 67 | bool Extractor::extract(std::size_t pos, long& val) |
| 68 | { |
| 69 | if (isNull(pos)) return false; |
| 70 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | bool Extractor::extract(std::size_t pos, unsigned long& val) |
| 76 | { |
| 77 | if (isNull(pos)) return false; |
| 78 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 79 | return true; |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | |
| 84 | bool Extractor::(std::size_t pos, double& val) |
| 85 | { |
| 86 | if (isNull(pos)) return false; |
| 87 | val = sqlite3_column_double(_pStmt, (int) pos); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | bool Extractor::(std::size_t pos, std::string& val) |
| 93 | { |
| 94 | if (isNull(pos)) return false; |
| 95 | const char *pBuf = reinterpret_cast<const char*>(sqlite3_column_text(_pStmt, (int) pos)); |
| 96 | if (!pBuf) |
| 97 | val.clear(); |
| 98 | else |
| 99 | val.assign(pBuf); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | bool Extractor::(std::size_t pos, Poco::Int8& val) |
| 105 | { |
| 106 | if (isNull(pos)) return false; |
| 107 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | bool Extractor::(std::size_t pos, Poco::UInt8& val) |
| 113 | { |
| 114 | if (isNull(pos)) return false; |
| 115 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | bool Extractor::(std::size_t pos, Poco::Int16& val) |
| 121 | { |
| 122 | if (isNull(pos)) return false; |
| 123 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | bool Extractor::(std::size_t pos, Poco::UInt16& val) |
| 129 | { |
| 130 | if (isNull(pos)) return false; |
| 131 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | bool Extractor::(std::size_t pos, Poco::UInt32& val) |
| 137 | { |
| 138 | if (isNull(pos)) return false; |
| 139 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | bool Extractor::(std::size_t pos, Poco::UInt64& val) |
| 145 | { |
| 146 | if (isNull(pos)) return false; |
| 147 | val = sqlite3_column_int64(_pStmt, (int) pos); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | bool Extractor::(std::size_t pos, bool& val) |
| 153 | { |
| 154 | if (isNull(pos)) return false; |
| 155 | val = (0 != sqlite3_column_int(_pStmt, (int) pos)); |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | bool Extractor::(std::size_t pos, float& val) |
| 161 | { |
| 162 | if (isNull(pos)) return false; |
| 163 | val = static_cast<float>(sqlite3_column_double(_pStmt, (int) pos)); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | bool Extractor::(std::size_t pos, char& val) |
| 169 | { |
| 170 | if (isNull(pos)) return false; |
| 171 | val = sqlite3_column_int(_pStmt, (int) pos); |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | bool Extractor::(std::size_t pos, Date& val) |
| 177 | { |
| 178 | if (isNull(pos)) return false; |
| 179 | std::string str; |
| 180 | extract(pos, str); |
| 181 | int tzd; |
| 182 | DateTime dt = DateTimeParser::parse(Utility::SQLITE_DATE_FORMAT, str, tzd); |
| 183 | val = dt; |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | bool Extractor::(std::size_t pos, Time& val) |
| 189 | { |
| 190 | if (isNull(pos)) return false; |
| 191 | std::string str; |
| 192 | extract(pos, str); |
| 193 | int tzd; |
| 194 | DateTime dt = DateTimeParser::parse(Utility::SQLITE_TIME_FORMAT, str, tzd); |
| 195 | val = dt; |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | bool Extractor::(std::size_t pos, DateTime& val) |
| 201 | { |
| 202 | if (isNull(pos)) return false; |
| 203 | std::string dt; |
| 204 | extract(pos, dt); |
| 205 | int tzd; |
| 206 | DateTimeParser::parse(dt, val, tzd); |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | bool Extractor::(std::size_t pos, Poco::Any& val) |
| 212 | { |
| 213 | return extractImpl(pos, val); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | bool Extractor::(std::size_t pos, Poco::DynamicAny& val) |
| 218 | { |
| 219 | return extractImpl(pos, val); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | bool Extractor::(std::size_t pos, std::size_t) |
| 224 | { |
| 225 | if (pos >= _nulls.size()) |
| 226 | _nulls.resize(pos + 1); |
| 227 | |
| 228 | if (!_nulls[pos].first) |
| 229 | { |
| 230 | _nulls[pos].first = true; |
| 231 | _nulls[pos].second = (SQLITE_NULL == sqlite3_column_type(_pStmt, static_cast<int>(pos))); |
| 232 | } |
| 233 | |
| 234 | return _nulls[pos].second; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | } } } // namespace Poco::SQL::SQLite |
| 239 | |