1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
5 * Ceph - scalable distributed file system
7 * Copyright (C) 2022 Red Hat, Inc.
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.
24 class DoutPrefixProvider
;
26 namespace rgw::dbstore::sqlite
{
28 // owning sqlite3_stmt pointer
30 void operator()(sqlite3_stmt
* p
) const { ::sqlite3_finalize(p
); }
32 using stmt_ptr
= std::unique_ptr
<sqlite3_stmt
, stmt_deleter
>;
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
); }
38 using stmt_binding
= std::unique_ptr
<sqlite3_stmt
, stmt_binding_deleter
>;
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
); }
44 using stmt_execution
= std::unique_ptr
<sqlite3_stmt
, stmt_execution_deleter
>;
47 // prepare the sql statement or throw on error
48 stmt_ptr
prepare_statement(const DoutPrefixProvider
* dpp
,
49 sqlite3
* db
, std::string_view sql
);
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
);
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
);
59 // evaluate a prepared statement, expecting no result rows
60 void eval0(const DoutPrefixProvider
* dpp
, const stmt_execution
& stmt
);
62 // evaluate a prepared statement, expecting a single result row
63 void eval1(const DoutPrefixProvider
* dpp
, const stmt_execution
& stmt
);
65 // return the given column as an integer
66 int column_int(const stmt_execution
& stmt
, int column
);
68 // return the given column as text, or an empty string on NULL
69 std::string
column_text(const stmt_execution
& stmt
, int column
);
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
>;
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
);
83 } // namespace rgw::dbstore::sqlite