summaryrefslogtreecommitdiffstats
path: root/src/rgw/driver/dbstore/sqlite/statement.cc
blob: dcf7dba9c50bdc597afecb60ee6df5c8a995dd13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2022 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */

#include "common/dout.h"
#include "error.h"
#include "statement.h"

#define dout_subsys ceph_subsys_rgw_dbstore

namespace rgw::dbstore::sqlite {

// owning pointer to arbitrary memory allocated and returned by sqlite3
struct sqlite_deleter {
  template <typename T>
  void operator()(T* p) { ::sqlite3_free(p); }
};
template <typename T>
using sqlite_ptr = std::unique_ptr<T, sqlite_deleter>;


stmt_ptr prepare_statement(const DoutPrefixProvider* dpp,
                           sqlite3* db, std::string_view sql)
{
  sqlite3_stmt* stmt = nullptr;
  int result = ::sqlite3_prepare_v2(db, sql.data(), sql.size(), &stmt, nullptr);
  auto ec = std::error_code{result, sqlite::error_category()};
  if (ec != sqlite::errc::ok) {
    const char* errmsg = ::sqlite3_errmsg(db);
    ldpp_dout(dpp, 1) << "preparation failed: " << errmsg
        << " (" << ec << ")\nstatement: " << sql << dendl;
    throw sqlite::error(errmsg, ec);
  }
  return stmt_ptr{stmt};
}

static int bind_index(const DoutPrefixProvider* dpp,
                      const stmt_binding& stmt, const char* name)
{
  const int index = ::sqlite3_bind_parameter_index(stmt.get(), name);
  if (index <= 0) {
    ldpp_dout(dpp, 1) << "binding failed on parameter name="
        << name << dendl;
    sqlite3* db = ::sqlite3_db_handle(stmt.get());
    throw sqlite::error(db);
  }
  return index;
}

void bind_text(const DoutPrefixProvider* dpp, const stmt_binding& stmt,
               const char* name, std::string_view value)
{
  const int index = bind_index(dpp, stmt, name);

  int result = ::sqlite3_bind_text(stmt.get(), index, value.data(),
                                   value.size(), SQLITE_STATIC);
  auto ec = std::error_code{result, sqlite::error_category()};
  if (ec != sqlite::errc::ok) {
    ldpp_dout(dpp, 1) << "binding failed on parameter name="
        << name << " value=" << value << dendl;
    sqlite3* db = ::sqlite3_db_handle(stmt.get());
    throw sqlite::error(db, ec);
  }
}

void bind_int(const DoutPrefixProvider* dpp, const stmt_binding& stmt,
              const char* name, int value)
{
  const int index = bind_index(dpp, stmt, name);

  int result = ::sqlite3_bind_int(stmt.get(), index, value);
  auto ec = std::error_code{result, sqlite::error_category()};
  if (ec != sqlite::errc::ok) {
    ldpp_dout(dpp, 1) << "binding failed on parameter name="
        << name << " value=" << value << dendl;
    sqlite3* db = ::sqlite3_db_handle(stmt.get());
    throw sqlite::error(db, ec);
  }
}

void eval0(const DoutPrefixProvider* dpp, const stmt_execution& stmt)
{
  sqlite_ptr<char> sql;
  if (dpp->get_cct()->_conf->subsys.should_gather<dout_subsys, 20>()) {
    sql.reset(::sqlite3_expanded_sql(stmt.get()));
  }

  const int result = ::sqlite3_step(stmt.get());
  auto ec = std::error_code{result, sqlite::error_category()};
  sqlite3* db = ::sqlite3_db_handle(stmt.get());

  if (ec != sqlite::errc::done) {
    const char* errmsg = ::sqlite3_errmsg(db);
    ldpp_dout(dpp, 20) << "evaluation failed: " << errmsg
        << " (" << ec << ")\nstatement: " << sql.get() << dendl;
    throw sqlite::error(errmsg, ec);
  }
  ldpp_dout(dpp, 20) << "evaluation succeeded: " << sql.get() << dendl;
}

void eval1(const DoutPrefixProvider* dpp, const stmt_execution& stmt)
{
  sqlite_ptr<char> sql;
  if (dpp->get_cct()->_conf->subsys.should_gather<dout_subsys, 20>()) {
    sql.reset(::sqlite3_expanded_sql(stmt.get()));
  }

  const int result = ::sqlite3_step(stmt.get());
  auto ec = std::error_code{result, sqlite::error_category()};
  if (ec != sqlite::errc::row) {
    sqlite3* db = ::sqlite3_db_handle(stmt.get());
    const char* errmsg = ::sqlite3_errmsg(db);
    ldpp_dout(dpp, 1) << "evaluation failed: " << errmsg << " (" << ec
        << ")\nstatement: " << sql.get() << dendl;
    throw sqlite::error(errmsg, ec);
  }
  ldpp_dout(dpp, 20) << "evaluation succeeded: " << sql.get() << dendl;
}

int column_int(const stmt_execution& stmt, int column)
{
  return ::sqlite3_column_int(stmt.get(), column);
}

std::string column_text(const stmt_execution& stmt, int column)
{
  const unsigned char* text = ::sqlite3_column_text(stmt.get(), column);
  // may be NULL
  if (text) {
    const std::size_t size = ::sqlite3_column_bytes(stmt.get(), column);
    return {reinterpret_cast<const char*>(text), size};
  } else {
    return {};
  }
}

auto read_text_rows(const DoutPrefixProvider* dpp,
                    const stmt_execution& stmt,
                    std::span<std::string> entries)
  -> std::span<std::string>
{
  sqlite_ptr<char> sql;
  if (dpp->get_cct()->_conf->subsys.should_gather<dout_subsys, 20>()) {
    sql.reset(::sqlite3_expanded_sql(stmt.get()));
  }

  std::size_t count = 0;
  while (count < entries.size()) {
    const int result = ::sqlite3_step(stmt.get());
    auto ec = std::error_code{result, sqlite::error_category()};
    if (ec == sqlite::errc::done) {
      break;
    }
    if (ec != sqlite::errc::row) {
      sqlite3* db = ::sqlite3_db_handle(stmt.get());
      const char* errmsg = ::sqlite3_errmsg(db);
      ldpp_dout(dpp, 1) << "evaluation failed: " << errmsg << " (" << ec
          << ")\nstatement: " << sql.get() << dendl;
      throw sqlite::error(errmsg, ec);
    }
    entries[count] = column_text(stmt, 0);
    ++count;
  }
  ldpp_dout(dpp, 20) << "statement evaluation produced " << count
      << " results: " << sql.get() << dendl;

  return entries.first(count);
}

void execute(const DoutPrefixProvider* dpp, sqlite3* db, const char* query,
             sqlite3_callback callback, void* arg)
{
  char* errmsg = nullptr;
  const int result = ::sqlite3_exec(db, query, callback, arg, &errmsg);
  auto ec = std::error_code{result, sqlite::error_category()};
  auto ptr = sqlite_ptr<char>{errmsg}; // free on destruction
  if (ec != sqlite::errc::ok) {
    ldpp_dout(dpp, 1) << "query execution failed: " << errmsg << " (" << ec
        << ")\nquery: " << query << dendl;
    throw sqlite::error(errmsg, ec);
  }
  ldpp_dout(dpp, 20) << "query execution succeeded: " << query << dendl;
}

} // namespace rgw::dbstore::sqlite