Skip to content

Commit

Permalink
refactor: Change order of arguments of psQuery (#114)
Browse files Browse the repository at this point in the history
* refactor: Change order of arguments of psQuery
  • Loading branch information
MrWarlockX authored and smradCZ committed Jun 18, 2019
1 parent 1048b66 commit 73bb8f9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,24 @@ while (preparedStatement.fetch())

Invokes psQuery with param setter only.
```c++
psParamQuery("INSERT INTO ... (col1, col2, ...) VALUES (?, ?, ...)", connection, [&](T1 &col1, T2& col2, ...) -> bool {
psParamQuery(connection, "INSERT INTO ... (col1, col2, ...) VALUES (?, ?, ...)", [&](T1 &col1, T2& col2, ...) -> bool {
col1 = ...;
col2 = ...;
return true; // Or false, if we want to stop
});
```
**psResultQuery**
```c++
psResultQuery("SELECT ... FROM ...", connection, <Callable>);
psResultQuery(connection, "SELECT ... FROM ...", <Callable>);
```
Where callable can be C function, lambda, or member function, however in the last case you need to use
wrapper, for example wrapMember function (located in *superior_mysqlpp/extras/member_wrapper.hpp*).
```c++
psResultQuery("SELECT ... FROM ...", connection, [&](int arg1, int arg2){});
psResultQuery(connection, "SELECT ... FROM ...", [&](int arg1, int arg2){});
```
```c++
void processRow(int arg1, int arg2) {}
psResultQuery("SELECT ... FROM ...", connection, &processRow);
psResultQuery(connection, "SELECT ... FROM ...", &processRow);
```
```c++
class ProcessingClass {
Expand All @@ -291,7 +291,7 @@ public:
};

ProcessingClass pc;
psResultQuery("SELECT ... FROM ...", connection, wrapMember(&pc, &ProcessingClass::processRow));
psResultQuery(connection, "SELECT ... FROM ...", wrapMember(&pc, &ProcessingClass::processRow));
```
This method doesn't throw exceptions, however query execution and row fetching can still fail,
resulting in exception.
Expand All @@ -316,8 +316,8 @@ an *UnexpectedRowCountError* exception is thrown.
int myData = 0;
psQuery(
"SELECT ?",
connection,
"SELECT ?",
[&](int &value) -> bool {
value = myData;
return (myData++) < 5; // Return true, if data are set, false otherwise (no more input data available)
Expand Down
12 changes: 6 additions & 6 deletions include/superior_mysqlpp/extras/prepared_statement_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace SuperiorMySqlpp
typename ResultCallable,
typename ParamCallable,
typename ConnType>
void psQuery(const std::string &query, ConnType &&connection, ParamCallable &&paramsSetter, ResultCallable &&processingFunction)
void psQuery(ConnType &&connection, const std::string &query, ParamCallable &&paramsSetter, ResultCallable &&processingFunction)
{
static constexpr bool hasParamCallback = !std::is_same<ParamCallable, EmptyParamCallback>::value;
static constexpr bool hasResultCallback = !std::is_same<ResultCallable, EmptyResultCallback>::value;
Expand Down Expand Up @@ -173,7 +173,7 @@ namespace SuperiorMySqlpp
typename ConnType>
[[deprecated("psReadQuery is deprecated, use psResultQuery")]] void psReadQuery(const std::string &query, ConnType &&connection, Callable &&processingFunction)
{
psQuery(query, connection, EmptyParamCallback {}, processingFunction);
psQuery(connection, query, EmptyParamCallback {}, processingFunction);
}

/**
Expand All @@ -193,9 +193,9 @@ namespace SuperiorMySqlpp
bool ignoreNullable=detail::PreparedStatementsDefault::getIgnoreNullable(),
typename Callable,
typename ConnType>
void psResultQuery(const std::string &query, ConnType &&connection, Callable &&processingFunction)
void psResultQuery(ConnType &&connection, const std::string &query, Callable &&processingFunction)
{
psQuery(query, connection, EmptyParamCallback {}, processingFunction);
psQuery(connection, query, EmptyParamCallback {}, processingFunction);
}

/**
Expand All @@ -214,9 +214,9 @@ namespace SuperiorMySqlpp
bool ignoreNullable=detail::PreparedStatementsDefault::getIgnoreNullable(),
typename Callable,
typename ConnType>
void psParamQuery(const std::string &query, ConnType &&connection, Callable &&paramsSetter)
void psParamQuery(ConnType &&connection, const std::string &query, Callable &&paramsSetter)
{
psQuery(query, connection, paramsSetter, EmptyResultCallback {});
psQuery(connection, query, paramsSetter, EmptyResultCallback {});
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/_debian-common/changelog
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
libsuperiormysqlpp (0.4.1) UNRELEASED; urgency=medium
libsuperiormysqlpp (0.5.0) UNRELEASED; urgency=medium

[ Peter Opatril ]
* refactor: Logging system (DRY)
* feat: Default logger logs errors

[ Michal Merc ]
* feat: Implement psQuery/psParamQuery/psResultQuery convenience functions
* refactor: Change order of arguments for psQuery/psParamQuery/psResultQuery functions before release

-- Peter Opatril <[email protected]> Fri, 24 May 2019 14:07:13 +0000

Expand Down
10 changes: 5 additions & 5 deletions tests/db_access/prepared_statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,8 @@ go_bandit([](){
});

it("can work with psResultQuery (valid types, passing query string)", [&](){
psResultQuery("SELECT `id`, `blob`, `binary`, `varbinary` FROM `test_superior_sqlpp`.`binary_data` ORDER BY `id` LIMIT 1",
connection, [&](int id, const BlobData &blob, const BlobData &binary, const BlobData &varbinary) {
psResultQuery(connection, "SELECT `id`, `blob`, `binary`, `varbinary` FROM `test_superior_sqlpp`.`binary_data` ORDER BY `id` LIMIT 1",
[&](int id, const BlobData &blob, const BlobData &binary, const BlobData &varbinary) {
AssertThat(id, Equals(42));
AssertThat(blob.size(), Equals(5u));
AssertThat(binary.size(), Equals(10u));
Expand All @@ -616,8 +616,8 @@ go_bandit([](){
});

it("can work with prepared statement helper functions - psResultQuery (invalid data types)", [&](){
AssertThrows(PreparedStatementTypeError, psResultQuery("SELECT `id`, `blob`, `binary`, `varbinary` FROM `test_superior_sqlpp`.`binary_data` ORDER BY `id` LIMIT 1",
connection, [&](int , int, const BlobData &, const BlobData &) {}));
AssertThrows(PreparedStatementTypeError, psResultQuery(connection, "SELECT `id`, `blob`, `binary`, `varbinary` FROM `test_superior_sqlpp`.`binary_data` ORDER BY `id` LIMIT 1",
[&](int , int, const BlobData &, const BlobData &) {}));
});

it("can work with psQuery helper function", [&](){
Expand All @@ -630,8 +630,8 @@ go_bandit([](){

// Select ids, where foreign key is equal to some value (?)
psQuery(
"SELECT `id`, `f_id` FROM `psquery_test` WHERE `f_id` = ?",
connection,
"SELECT `id`, `f_id` FROM `psquery_test` WHERE `f_id` = ?",
[&](int &f_id) -> bool {
if (foreignKeysIterator == std::end(foreignKeys))
{
Expand Down

0 comments on commit 73bb8f9

Please sign in to comment.