This repository has been archived on 2020-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
tdlib-fork/tddb/td/db/SqliteStatement.h
levlam 0b6c1d226f Update clag-format to the latest (190926/r372920) version.
GitOrigin-RevId: 7861ae8ad28eb1f6a06ff3c6f56eff3f67b1d24c
2019-09-28 05:14:21 +03:00

84 lines
2.1 KiB
C++

//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
//
// 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
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/db/detail/RawSqliteDb.h"
#include <memory>
struct sqlite3;
struct sqlite3_stmt;
namespace td {
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 {
return state_ != Finish;
}
bool has_row() const {
return state_ == GotRow;
}
bool empty() const {
return !stmt_;
}
void reset();
auto guard() {
return ScopeExit{} + [this] {
this->reset();
};
}
// TODO get row
private:
friend class SqliteDb;
SqliteStatement(sqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db);
class StmtDeleter {
public:
void operator()(sqlite3_stmt *stmt);
};
enum { Start, GotRow, Finish } state_ = Start;
std::unique_ptr<sqlite3_stmt, StmtDeleter> stmt_;
std::shared_ptr<detail::RawSqliteDb> db_;
Status last_error();
};
} // namespace td