]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
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 <sqlite3.h> | |
20 | ||
21 | #include <fmt/format.h> | |
22 | ||
23 | #include "sqlite/statement.h" | |
24 | ||
25 | class DoutPrefixProvider; | |
26 | ||
27 | namespace rgw::dbstore::sqlite { | |
28 | ||
29 | // owning sqlite3 pointer | |
30 | struct db_deleter { | |
31 | void operator()(sqlite3* p) const { ::sqlite3_close(p); } | |
32 | }; | |
33 | using db_ptr = std::unique_ptr<sqlite3, db_deleter>; | |
34 | ||
35 | ||
36 | // open the database file or throw on error | |
37 | db_ptr open_database(const char* filename, int flags); | |
38 | ||
39 | ||
40 | struct Connection { | |
41 | db_ptr db; | |
42 | // map of statements, prepared on first use | |
43 | std::map<std::string_view, stmt_ptr> statements; | |
44 | ||
45 | explicit Connection(db_ptr db) : db(std::move(db)) {} | |
46 | }; | |
47 | ||
48 | // sqlite connection factory for ConnectionPool | |
49 | class ConnectionFactory { | |
50 | std::string uri; | |
51 | int flags; | |
52 | public: | |
53 | ConnectionFactory(std::string uri, int flags) | |
54 | : uri(std::move(uri)), flags(flags) {} | |
55 | ||
56 | auto operator()(const DoutPrefixProvider* dpp) | |
57 | -> std::unique_ptr<Connection> | |
58 | { | |
59 | auto db = open_database(uri.c_str(), flags); | |
60 | return std::make_unique<Connection>(std::move(db)); | |
61 | } | |
62 | }; | |
63 | ||
64 | } // namespace rgw::dbstore::sqlite |