]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1708] moved rawStatement to MySqlConnectionTest
authorAndrei Pavel <andrei@isc.org>
Fri, 19 Feb 2021 13:36:01 +0000 (15:36 +0200)
committerAndrei Pavel <andrei@isc.org>
Fri, 19 Feb 2021 13:47:52 +0000 (15:47 +0200)
src/lib/mysql/mysql_connection.cc
src/lib/mysql/mysql_connection.h
src/lib/mysql/tests/mysql_connection_unittest.cc

index c70d9f0e28bfb5a5f622a9ac468fc9410759be30..4784aaefd3f9f3797ba68c62b62ce3f382946401 100644 (file)
@@ -437,31 +437,5 @@ MySqlConnection::rollback() {
     }
 }
 
-std::vector<std::vector<std::string>>
-MySqlConnection::rawStatement(std::string const& statement) const {
-    // Execute a SQL statement.
-    if (mysql_query(mysql_, statement.c_str())) {
-        isc_throw(DbOperationError, statement << ": " << mysql_error(mysql_));
-    }
-
-    // Get a result set.
-    MySqlResult result(mysql_use_result(mysql_));
-
-    // Fetch a result set.
-    std::vector<std::vector<std::string>> output;
-    size_t r(0);
-    MYSQL_ROW row;
-    size_t const column_count(mysql_num_fields(result.result_));
-    while ((row = mysql_fetch_row(result.result_)) != NULL) {
-        output.push_back(std::vector<std::string>());
-        for (size_t i = 0; i < column_count; ++i) {
-            output[r].push_back(row[i]);
-        }
-        ++r;
-    }
-
-    return output;
-}
-
-} // namespace isc::db
+} // namespace db
 } // namespace isc
index 6583898fcfcce9d43a045fcb2cd77fd2949a100d..4a5bed9b2062e0dd45a993565bb5380d2667fa88 100644 (file)
@@ -67,24 +67,10 @@ private:
     MYSQL_STMT*     statement_;     ///< Statement for which results are freed
 };
 
-/// @brief RAII wrapper oer MYSQL_RES obtained from MySQL library functions like
-/// mysql_use_result().
-struct MySqlResult {
-    MySqlResult(MYSQL_RES* result) : result_(result) {
-    }
-
-    ~MySqlResult() {
-        mysql_free_result(result_);
-    }
-
-    MYSQL_RES* const result_;
-};
-
 /// @brief MySQL Selection Statements
 ///
 /// Each statement is associated with an index, which is used to reference the
 /// associated prepared statement.
-
 struct TaggedStatement {
     uint32_t index;
     const char* text;
@@ -578,21 +564,6 @@ public:
         return (static_cast<uint64_t>(mysql_stmt_affected_rows(statements_[index])));
     }
 
-    /// @brief Run a raw, unprepared statement.
-    ///
-    /// This is useful when running statements that can't be parametrized with a
-    /// question mark in place of a binded variable e.g. "SHOW GLOBAL VARIABLES"
-    /// and thus cannot be prepared beforehand. All the results are string, the
-    /// output should be the same as that which one would see in a mysql command
-    /// line client.
-    ///
-    /// @param statement the statement in string form
-    /// @throw DbOperationError if the statement could not be run
-    /// @return the list of rows, each row consisting of a list of values for
-    ///     each column
-    std::vector<std::vector<std::string>>
-    rawStatement(std::string const& statement) const;
-
     /// @brief Commit Transactions
     ///
     /// Commits all pending database operations. On databases that don't
index cbef0d06e443d86e00649e7fc0358ac8e724bcb7..7e1627d1ab0e09b6d2d140b38d2a10c443d2d6cf 100644 (file)
@@ -17,6 +17,19 @@ using namespace isc::db::test;
 
 namespace {
 
+/// @brief RAII wrapper over MYSQL_RES obtained from MySQL library functions like
+/// mysql_use_result().
+struct MySqlResult {
+    MySqlResult(MYSQL_RES* result) : result_(result) {
+    }
+
+    ~MySqlResult() {
+        mysql_free_result(result_);
+    }
+
+    MYSQL_RES* const result_;
+};
+
 /// @brief Test fixture class for @c MySqlConnection class.
 class MySqlConnectionTest : public ::testing::Test {
 public:
@@ -234,6 +247,45 @@ public:
         }
     }
 
+    /// @brief Run a raw, unprepared statement and return the result.
+    ///
+    /// This is useful when running statements that can't be parametrized with a
+    /// question mark in place of a bound variable e.g. "SHOW GLOBAL VARIABLES"
+    /// and thus cannot be prepared beforehand. All the results are string, the
+    /// output should be the same as that which one would see in a mysql command
+    /// line client.
+    ///
+    /// @param statement the statement in string form
+    /// @throw DbOperationError if the statement could not be run
+    /// @return the list of rows, each row consisting of a list of values for
+    ///     each column
+    std::vector<std::vector<std::string>>
+    rawStatement(std::string const& statement) const {
+        // Execute a SQL statement.
+        if (mysql_query(conn_.mysql_, statement.c_str())) {
+            isc_throw(DbOperationError,
+                      statement << ": " << mysql_error(conn_.mysql_));
+        }
+
+        // Get a result set.
+        MySqlResult result(mysql_use_result(conn_.mysql_));
+
+        // Fetch a result set.
+        std::vector<std::vector<std::string>> output;
+        size_t r(0);
+        MYSQL_ROW row;
+        size_t const column_count(mysql_num_fields(result.result_));
+        while ((row = mysql_fetch_row(result.result_)) != NULL) {
+            output.push_back(std::vector<std::string>());
+            for (size_t i = 0; i < column_count; ++i) {
+                output[r].push_back(row[i]);
+            }
+            ++r;
+        }
+
+        return output;
+    }
+
     /// @brief Get pxc_strict_mode global variable from the database.
     /// For Percona, they can be: DISABLED, PERMISSIVE, ENFORCING, MASTER.
     std::string showPxcStrictMode() {
@@ -244,7 +296,7 @@ public:
             // this returned row the lambda provided as 4th argument should be
             // executed.
             std::vector<std::vector<std::string>> const result(
-                conn_.rawStatement("SHOW GLOBAL VARIABLES LIKE 'pxc_strict_mode'"));
+                rawStatement("SHOW GLOBAL VARIABLES LIKE 'pxc_strict_mode'"));
             if (result.size() < 1 || result[0].size() < 2) {
                 // Not Percona
                 return "";