2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/db/detail/RawSqliteDb.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-10-08 17:30:36 +02:00
|
|
|
#include "td/utils/logging.h"
|
2020-07-14 02:37:04 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2022-08-04 13:37:08 +02:00
|
|
|
struct tdsqlite3;
|
|
|
|
struct tdsqlite3_stmt;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
extern int VERBOSITY_NAME(sqlite);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class SqliteStatement {
|
|
|
|
public:
|
|
|
|
SqliteStatement() = default;
|
|
|
|
SqliteStatement(const SqliteStatement &other) = delete;
|
|
|
|
SqliteStatement &operator=(const SqliteStatement &other) = delete;
|
|
|
|
SqliteStatement(SqliteStatement &&other) = default;
|
|
|
|
SqliteStatement &operator=(SqliteStatement &&other) = default;
|
|
|
|
~SqliteStatement();
|
|
|
|
|
|
|
|
Status bind_blob(int id, Slice blob) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status bind_string(int id, Slice str) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status bind_int32(int id, int32 value) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status bind_int64(int id, int64 value) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status bind_null(int id) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status step() TD_WARN_UNUSED_RESULT;
|
|
|
|
Slice view_string(int id) TD_WARN_UNUSED_RESULT;
|
|
|
|
Slice view_blob(int id) TD_WARN_UNUSED_RESULT;
|
|
|
|
int32 view_int32(int id) TD_WARN_UNUSED_RESULT;
|
|
|
|
int64 view_int64(int id) TD_WARN_UNUSED_RESULT;
|
|
|
|
enum class Datatype { Integer, Float, Blob, Null, Text };
|
|
|
|
Datatype view_datatype(int id);
|
|
|
|
|
|
|
|
Result<string> explain();
|
|
|
|
|
|
|
|
bool can_step() const {
|
2020-01-19 01:02:56 +01:00
|
|
|
return state_ != State::Finish;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
bool has_row() const {
|
2020-01-19 01:02:56 +01:00
|
|
|
return state_ == State::GotRow;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
bool empty() const {
|
|
|
|
return !stmt_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
auto guard() {
|
2019-09-28 04:14:21 +02:00
|
|
|
return ScopeExit{} + [this] {
|
|
|
|
this->reset();
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO get row
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class SqliteDb;
|
2022-08-04 13:37:08 +02:00
|
|
|
SqliteStatement(tdsqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class StmtDeleter {
|
|
|
|
public:
|
2022-08-04 13:37:08 +02:00
|
|
|
void operator()(tdsqlite3_stmt *stmt);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2020-01-19 01:02:56 +01:00
|
|
|
enum class State { Start, GotRow, Finish };
|
|
|
|
State state_ = State::Start;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-04 13:37:08 +02:00
|
|
|
std::unique_ptr<tdsqlite3_stmt, StmtDeleter> stmt_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<detail::RawSqliteDb> db_;
|
|
|
|
|
|
|
|
Status last_error();
|
|
|
|
};
|
2018-07-17 23:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|