| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH | ||
| 2 | // | ||
| 3 | // SPDX-License-Identifier: MIT | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstdint> | ||
| 8 | #include <string> | ||
| 9 | #include <optional> | ||
| 10 | #include <IBlobOutput.h> | ||
| 11 | |||
| 12 | /// This interface is representing a "prepared/compiled" database-statement. The life-cycle of a database-statement is: | ||
| 13 | /// - an instance is created (-> IDbConnection::PrepareStatement) | ||
| 14 | /// - if the statement is using literals which are to be replaced with parameters, then the appropriate Bind-methods are to be called | ||
| 15 | /// - now the statement is sent to execution (-> IDbConnection::StepStatement) | ||
| 16 | /// - and results may then be retrieved by calling into the appropriate "GetResult"-methods. | ||
| 17 | /// This interface is intended to abstract different databases. | ||
| 18 | class IDbStatement | ||
| 19 | { | ||
| 20 | public: | ||
| 21 | virtual void Reset() = 0; | ||
| 22 | |||
| 23 | virtual void BindNull(int index) = 0; | ||
| 24 | virtual void BindInt32(int index, std::int32_t value) = 0; | ||
| 25 | virtual void BindInt64(int index, std::int64_t value) = 0; | ||
| 26 | virtual void BindDouble(int index, double value) = 0; | ||
| 27 | |||
| 28 | /// Bind a string (in UTF8). The string is copied (called "transient binding" in SQLite, c.f. https://www3.sqlite.org/c3ref/bind_blob.html). | ||
| 29 | /// So, the string only must be valid for the duration of the execution of this method. However, there is of course a performance penalty | ||
| 30 | /// here. | ||
| 31 | /// | ||
| 32 | /// \param index Index of the parameter to bind. | ||
| 33 | /// \param value The null-terminated string to be bound (in UTF8 encoding). | ||
| 34 | virtual void BindString(int index, const char* value) = 0; | ||
| 35 | |||
| 36 | virtual void BindStringView(int index, const std::string_view& value) = 0; | ||
| 37 | |||
| 38 | /// Bind a "static" BLOB. Static means that the pointer MUST remain valid until either the prepared statement is | ||
| 39 | /// finalized or the same SQL parameter is bound to something else. | ||
| 40 | /// | ||
| 41 | /// \param index Index of the parameter to bind. | ||
| 42 | /// \param data The pointer to the data. | ||
| 43 | /// \param size The size of the data (in bytes). | ||
| 44 | virtual void BindBlob_Static(int index, const void* data, size_t size) = 0; | ||
| 45 | |||
| 46 | /// Gets the column of the result as an int32. This will coerce/convert that data into the desired type 'int32' if necessary. | ||
| 47 | /// In particular, a DB-NULL is mapped to '0'. | ||
| 48 | /// \param column The column. | ||
| 49 | /// \returns The value of the specified column. | ||
| 50 | virtual std::int32_t GetResultInt32(int column) = 0; | ||
| 51 | |||
| 52 | /// Gets the column of the result as an int32. This will convert that data into the desired type 'int32' if necessary. | ||
| 53 | /// However, a DB-NULL is NOT mapped to '0', instead an empty result is returned. | ||
| 54 | /// \param column The column. | ||
| 55 | /// \returns If it exists and is valid, the value of the specified column; otherwise an empty value. | ||
| 56 | virtual std::optional<std::int32_t> GetResultInt32OrNull(int column) = 0; | ||
| 57 | |||
| 58 | virtual std::uint32_t GetResultUInt32(int column) = 0; | ||
| 59 | virtual std::uint8_t GetResultUInt8(int column) = 0; | ||
| 60 | virtual std::int64_t GetResultInt64(int column) = 0; | ||
| 61 | virtual double GetResultDouble(int column) = 0; | ||
| 62 | |||
| 63 | virtual std::optional<double> GetResultDoubleOrNull(int column) = 0; | ||
| 64 | virtual std::string GetResultString(int column) = 0; | ||
| 65 | virtual void GetResultBlob(int column, imgdoc2::IBlobOutput* blobOutput) = 0; | ||
| 66 | |||
| 67 | 221044 | virtual ~IDbStatement() = default; | |
| 68 | |||
| 69 | public: | ||
| 70 | 1434 | void BindString(int index, const std::string& value) { this->BindString(index, value.c_str()); } | |
| 71 | |||
| 72 | public: | ||
| 73 | // no copy and no move (-> https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c21-if-you-define-or-delete-any-copy-move-or-destructor-function-define-or-delete-them-all ) | ||
| 74 | 110522 | IDbStatement() = default; | |
| 75 | IDbStatement(const IDbStatement&) = delete; // copy constructor | ||
| 76 | IDbStatement& operator=(const IDbStatement&) = delete; // copy assignment | ||
| 77 | IDbStatement(IDbStatement&&) = delete; // move constructor | ||
| 78 | IDbStatement& operator=(IDbStatement&&) = delete; // move assignment | ||
| 79 | }; | ||
| 80 |