Skip to content

Commit b461df6

Browse files
committed
feat sqlite: use common Query
Tests: протестировано CI commit_hash:5a5e5d3320329f7498ec6d7b7481d865f6bac554
1 parent eebb6d0 commit b461df6

16 files changed

Lines changed: 41 additions & 81 deletions

.mapping.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4205,7 +4205,6 @@
42054205
"sqlite/src/storages/sqlite/infra/strategy/read_only.cpp":"taxi/uservices/userver/sqlite/src/storages/sqlite/infra/strategy/read_only.cpp",
42064206
"sqlite/src/storages/sqlite/infra/strategy/read_write.cpp":"taxi/uservices/userver/sqlite/src/storages/sqlite/infra/strategy/read_write.cpp",
42074207
"sqlite/src/storages/sqlite/options.cpp":"taxi/uservices/userver/sqlite/src/storages/sqlite/options.cpp",
4208-
"sqlite/src/storages/sqlite/query.cpp":"taxi/uservices/userver/sqlite/src/storages/sqlite/query.cpp",
42094208
"sqlite/src/storages/sqlite/result_set.cpp":"taxi/uservices/userver/sqlite/src/storages/sqlite/result_set.cpp",
42104209
"sqlite/src/storages/sqlite/savepoint.cpp":"taxi/uservices/userver/sqlite/src/storages/sqlite/savepoint.cpp",
42114210
"sqlite/src/storages/sqlite/tests/common_result_set_test.cpp":"taxi/uservices/userver/sqlite/src/storages/sqlite/tests/common_result_set_test.cpp",

sqlite/include/userver/storages/sqlite/client.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ ResultSet Client::Execute(OperationType operation_type, const Query& query, cons
131131
auto connection = GetConnection(operation_type);
132132
AccountQueryExecute(connection);
133133
try {
134-
auto params_binder = impl::BindHelper::UpdateParamsBindings(query.GetStatement(), *connection, args...);
134+
auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection, args...);
135135
return DoExecute(params_binder, connection);
136136
} catch (const std::exception& err) {
137137
AccountQueryFailed(connection);
@@ -144,7 +144,7 @@ ResultSet Client::ExecuteDecompose(OperationType operation_type, const Query& qu
144144
auto connection = GetConnection(operation_type);
145145
AccountQueryExecute(connection);
146146
try {
147-
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query.GetStatement(), *connection, row);
147+
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection, row);
148148
return DoExecute(params_binder, connection);
149149
} catch (const std::exception& err) {
150150
AccountQueryFailed(connection);
@@ -158,7 +158,7 @@ void Client::ExecuteMany(OperationType operation_type, const Query& query, const
158158
for (const auto& row : params) {
159159
AccountQueryExecute(connection);
160160
try {
161-
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query.GetStatement(), *connection, row);
161+
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection, row);
162162
DoExecute(params_binder, connection);
163163
} catch (const std::exception& err) {
164164
AccountQueryFailed(connection);
@@ -173,7 +173,7 @@ Client::GetCursor(OperationType operation_type, std::size_t batch_size, const Qu
173173
auto connection = GetConnection(operation_type);
174174
AccountQueryExecute(connection);
175175
try {
176-
auto params_binder = impl::BindHelper::UpdateParamsBindings(query.GetStatement(), *connection, args...);
176+
auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection, args...);
177177
return CursorResultSet<T>{DoExecute(params_binder, connection), batch_size};
178178
} catch (const std::exception& err) {
179179
AccountQueryFailed(connection);

sqlite/include/userver/storages/sqlite/impl/binder_help.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ namespace storages::sqlite::impl {
1212
class BindHelper final {
1313
public:
1414
template <typename... Args>
15-
static io::ParamsBinder
16-
UpdateParamsBindings(const std::string& query, infra::ConnectionPtr& conn, const Args&... args) {
15+
static io::ParamsBinder UpdateParamsBindings(const Query& query, infra::ConnectionPtr& conn, const Args&... args) {
1716
return io::ParamsBinder::BindParams(query, conn, args...);
1817
}
1918

2019
template <typename T>
21-
static io::ParamsBinder
22-
UpdateRowAsParamsBindings(const std::string& query, infra::ConnectionPtr& conn, const T& row) {
20+
static io::ParamsBinder UpdateRowAsParamsBindings(const Query& query, infra::ConnectionPtr& conn, const T& row) {
2321
static_assert(
2422
std::is_aggregate_v<T> || boost::pfr::tuple_size_v<T> > 0, "T must be an aggregate type or tuple-like type"
2523
);

sqlite/include/userver/storages/sqlite/impl/io/params_binder.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace storages::sqlite::impl::io {
1313

1414
class ParamsBinder final : public ParamsBinderBase {
1515
public:
16-
explicit ParamsBinder(const std::string& query, infra::ConnectionPtr& conn);
16+
explicit ParamsBinder(const Query& query, infra::ConnectionPtr& conn);
1717
~ParamsBinder();
1818

1919
ParamsBinder(const ParamsBinder& other) = delete;
@@ -25,7 +25,7 @@ class ParamsBinder final : public ParamsBinderBase {
2525
}
2626

2727
template <typename... Args>
28-
static ParamsBinder BindParams(const std::string& query, infra::ConnectionPtr& conn, const Args&... args) {
28+
static ParamsBinder BindParams(const Query& query, infra::ConnectionPtr& conn, const Args&... args) {
2929
constexpr auto kParamsCount = sizeof...(Args);
3030
ParamsBinder binder{query, conn};
3131

sqlite/include/userver/storages/sqlite/impl/io/params_binder_base.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
#include <userver/storages/sqlite/impl/binder_fwd.hpp>
44
#include <userver/storages/sqlite/infra/connection_ptr.hpp>
5+
#include <userver/storages/sqlite/query.hpp>
56

67
USERVER_NAMESPACE_BEGIN
78

89
namespace storages::sqlite::impl::io {
910

1011
class ParamsBinderBase {
1112
public:
12-
explicit ParamsBinderBase(const std::string& query, infra::ConnectionPtr& conn);
13+
explicit ParamsBinderBase(const Query& query, infra::ConnectionPtr& conn);
1314

1415
ParamsBinderBase(const ParamsBinderBase& other) = delete;
1516
ParamsBinderBase(ParamsBinderBase&& other) noexcept;

sqlite/include/userver/storages/sqlite/impl/statement.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace storages::sqlite::impl {
1515

1616
class Statement final : public StatementBase {
1717
public:
18-
Statement(const NativeHandler& db_handler, const std::string& statement);
18+
Statement(const NativeHandler& db_handler, std::string_view statement);
1919
~Statement() override;
2020

2121
Statement(const Statement& other) = delete;
@@ -74,7 +74,7 @@ class Statement final : public StatementBase {
7474

7575
using NativeStatementPtr = std::unique_ptr<sqlite3_stmt, SQLiteStatementDeleter>;
7676

77-
NativeStatementPtr prepareStatement(const std::string& statement_str);
77+
NativeStatementPtr prepareStatement(std::string_view statement_str);
7878

7979
const NativeHandler& db_handler_;
8080
NativeStatementPtr prepare_statement_;

sqlite/include/userver/storages/sqlite/impl/statements_cache.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class StatementsCache final {
2424
StatementsCache& operator=(StatementsCache&&) noexcept = delete;
2525
StatementsCache& operator=(const StatementsCache&) = delete;
2626

27-
std::shared_ptr<Statement> PrepareStatement(const std::string& statement);
27+
std::shared_ptr<Statement> PrepareStatement(std::string_view statement);
2828

2929
private:
3030
const NativeHandler& db_handler_;

sqlite/include/userver/storages/sqlite/query.hpp

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,13 @@
22

33
/// @file userver/storages/sqlite/query.hpp
44

5-
#include <optional>
6-
#include <string>
7-
8-
#include <userver/utils/strong_typedef.hpp>
5+
#include <userver/storages/query.hpp>
96

107
USERVER_NAMESPACE_BEGIN
118

129
namespace storages::sqlite {
1310

14-
// TODO: Can use Query from #include <userver/storages/query.hpp>?
15-
16-
/// @brief Query class, which driver executes.
17-
class Query {
18-
public:
19-
/// @brief Strong typedef for query name, one can use named queries to get
20-
/// better logging experience
21-
using Name = utils::StrongTypedef<struct NameTag, std::string>;
22-
23-
/// @brief Query constructor
24-
Query(const char* statement, std::optional<Name> = std::nullopt);
25-
26-
/// @brief Query constructor
27-
Query(std::string statement, std::optional<Name> = std::nullopt);
28-
29-
/// @brief Get query statement
30-
const std::string& GetStatement() const;
31-
32-
/// @brief Get query name
33-
const std::optional<Name>& GetName() const;
34-
35-
private:
36-
std::string statement_;
37-
std::optional<Name> name_;
38-
};
11+
using storages::Query;
3912

4013
} // namespace storages::sqlite
4114

sqlite/include/userver/storages/sqlite/savepoint.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ ResultSet Savepoint::Execute(const Query& query, const Args&... args) const {
113113
AssertValid();
114114
AccountQueryExecute();
115115
try {
116-
auto params_binder = impl::BindHelper::UpdateParamsBindings(query.GetStatement(), *connection_, args...);
116+
auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection_, args...);
117117
return DoExecute(params_binder);
118118
} catch (const std::exception& err) {
119119
AccountQueryFailed();
@@ -126,7 +126,7 @@ ResultSet Savepoint::ExecuteDecompose(const Query& query, const T& row) const {
126126
AssertValid();
127127
AccountQueryExecute();
128128
try {
129-
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query.GetStatement(), *connection_, row);
129+
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection_, row);
130130
return DoExecute(params_binder);
131131
} catch (const std::exception& err) {
132132
AccountQueryFailed();
@@ -140,7 +140,7 @@ void Savepoint::ExecuteMany(const Query& query, const Container& params) const {
140140
for (const auto& row : params) {
141141
AccountQueryExecute();
142142
try {
143-
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query.GetStatement(), *connection_, row);
143+
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection_, row);
144144
DoExecute(params_binder);
145145
} catch (const std::exception& err) {
146146
AccountQueryFailed();
@@ -154,7 +154,7 @@ CursorResultSet<T> Savepoint::GetCursor(std::size_t batch_size, const Query& que
154154
AssertValid();
155155
AccountQueryExecute();
156156
try {
157-
auto params_binder = impl::BindHelper::UpdateParamsBindings(query.GetStatement(), *connection_, args...);
157+
auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection_, args...);
158158
return CursorResultSet<T>{DoExecute(params_binder, connection_), batch_size};
159159
} catch (const std::exception& err) {
160160
AccountQueryFailed();

sqlite/include/userver/storages/sqlite/transaction.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ ResultSet Transaction::Execute(const Query& query, const Args&... args) const {
112112
AssertValid();
113113
AccountQueryExecute();
114114
try {
115-
auto params_binder = impl::BindHelper::UpdateParamsBindings(query.GetStatement(), *connection_, args...);
115+
auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection_, args...);
116116
return DoExecute(params_binder);
117117
} catch (const std::exception& err) {
118118
AccountQueryFailed();
@@ -125,7 +125,7 @@ ResultSet Transaction::ExecuteDecompose(const Query& query, const T& row) const
125125
AssertValid();
126126
AccountQueryExecute();
127127
try {
128-
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query.GetStatement(), *connection_, row);
128+
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection_, row);
129129
return DoExecute(params_binder);
130130
} catch (const std::exception& err) {
131131
AccountQueryFailed();
@@ -139,7 +139,7 @@ void Transaction::ExecuteMany(const Query& query, const Container& params) const
139139
AccountQueryExecute();
140140
for (const auto& row : params) {
141141
try {
142-
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query.GetStatement(), *connection_, row);
142+
auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection_, row);
143143
DoExecute(params_binder);
144144
} catch (const std::exception& err) {
145145
AccountQueryFailed();
@@ -153,7 +153,7 @@ CursorResultSet<T> Transaction::GetCursor(std::size_t batch_size, const Query& q
153153
AssertValid();
154154
AccountQueryExecute();
155155
try {
156-
auto params_binder = impl::BindHelper::UpdateParamsBindings(query.GetStatement(), *connection_, args...);
156+
auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection_, args...);
157157
return CursorResultSet<T>{DoExecute(params_binder, connection_), batch_size};
158158
} catch (const std::exception& err) {
159159
AccountQueryFailed();

0 commit comments

Comments
 (0)