]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/driver/dbstore/sqlite/statement.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rgw / driver / dbstore / sqlite / statement.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 /*
5 * Ceph - scalable distributed file system
6 *
7 * Copyright (C) 2022 Red Hat, Inc.
8 *
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
13 *
14 */
15
16 #pragma once
17
18 #include <memory>
19 #include <span>
20 #include <string>
21
22 #include <sqlite3.h>
23
24 class DoutPrefixProvider;
25
26 namespace rgw::dbstore::sqlite {
27
28 // owning sqlite3_stmt pointer
29 struct stmt_deleter {
30 void operator()(sqlite3_stmt* p) const { ::sqlite3_finalize(p); }
31 };
32 using stmt_ptr = std::unique_ptr<sqlite3_stmt, stmt_deleter>;
33
34 // non-owning sqlite3_stmt pointer that clears binding state on destruction
35 struct stmt_binding_deleter {
36 void operator()(sqlite3_stmt* p) const { ::sqlite3_clear_bindings(p); }
37 };
38 using stmt_binding = std::unique_ptr<sqlite3_stmt, stmt_binding_deleter>;
39
40 // non-owning sqlite3_stmt pointer that clears execution state on destruction
41 struct stmt_execution_deleter {
42 void operator()(sqlite3_stmt* p) const { ::sqlite3_reset(p); }
43 };
44 using stmt_execution = std::unique_ptr<sqlite3_stmt, stmt_execution_deleter>;
45
46
47 // prepare the sql statement or throw on error
48 stmt_ptr prepare_statement(const DoutPrefixProvider* dpp,
49 sqlite3* db, std::string_view sql);
50
51 // bind an input string for the given parameter name
52 void bind_text(const DoutPrefixProvider* dpp, const stmt_binding& stmt,
53 const char* name, std::string_view value);
54
55 // bind an input integer for the given parameter name
56 void bind_int(const DoutPrefixProvider* dpp, const stmt_binding& stmt,
57 const char* name, int value);
58
59 // evaluate a prepared statement, expecting no result rows
60 void eval0(const DoutPrefixProvider* dpp, const stmt_execution& stmt);
61
62 // evaluate a prepared statement, expecting a single result row
63 void eval1(const DoutPrefixProvider* dpp, const stmt_execution& stmt);
64
65 // return the given column as an integer
66 int column_int(const stmt_execution& stmt, int column);
67
68 // return the given column as text, or an empty string on NULL
69 std::string column_text(const stmt_execution& stmt, int column);
70
71 // read the text column from each result row into the given entries, and return
72 // the sub-span of entries that contain results
73 auto read_text_rows(const DoutPrefixProvider* dpp,
74 const stmt_execution& stmt,
75 std::span<std::string> entries)
76 -> std::span<std::string>;
77
78 // execute a raw query without preparing a statement. the optional callback
79 // can be used to read results
80 void execute(const DoutPrefixProvider* dpp, sqlite3* db, const char* query,
81 sqlite3_callback callback, void* arg);
82
83 } // namespace rgw::dbstore::sqlite