cpp mysql lmysqlcppconn call procedure

发布时间 2023-12-14 11:34:25作者: FredGrit
mysql> show create table t5;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                             |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t5    | CREATE TABLE `t5` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(40) DEFAULT (uuid()),
  `author` varchar(40) NOT NULL DEFAULT (uuid()),
  `topic` varchar(128) NOT NULL,
  `idx` int NOT NULL DEFAULT '-1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

  

mysql> show create procedure insert_into_t5_sp5;
+--------------------+-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Procedure          | sql_mode                                                                                                              | Create Procedure                                                                                                                                                                                                                                                            | character_set_client | collation_connection | Database Collation |
+--------------------+-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| insert_into_t5_sp5 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`rumination`@`localhost` PROCEDURE `insert_into_t5_sp5`(in min_idx_value int,in max_idx_value int)
begin
while(min_idx_value<max_idx_value)
do
insert into t5(topic ,idx) values( uuid(),min_idx_value*2);
set min_idx_value=min_idx_value+1;
end while;
end | utf8mb4              | utf8mb4_0900_ai_ci   | utf8mb4_0900_ai_ci |
+--------------------+-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

mysql> 

 

 

 

#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <thread>
#include <cppconn/connection.h>
#include <cppconn/driver.h>
#include <cppconn/metadata.h>
#include <cppconn/parameter_metadata.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/resultset_metadata.h>

std::string get_time_now(bool is_exact = true)
{
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    if (is_exact)
    {
        std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
        std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
        std::chrono::microseconds micros = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
        std::chrono::nanoseconds nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
        
        ss << "_" << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
           << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
           << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    }
    return ss.str();
}

void print_time(const int &len)
{
    std::thread t1([](int le)
                   {
        for (int i = 0; i < le; i++)
        {
            std::cout << get_time_now() << "," << i + 1 << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
        std::cout<<"Thread Id:"<<std::this_thread::get_id()<<std::endl; },
                   len);
    t1.join();
    std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ",line:" << __LINE__ << " of " << __FUNCTION__ << std::endl;
}
 
void cpp_call_proc(const int &min_value, const int &max_value)
{
    sql::Driver *driver = get_driver_instance();
    sql::Connection *conn = driver->connect("localhost", "rumination", "Rumination0001!");
    conn->setSchema("db");
    std::cout << std::boolalpha << conn->isValid() << std::endl;

    /*mysql> show create procedure insert_into_t5_sp5;
+--------------------+-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Procedure          | sql_mode                                                                                                              | Create Procedure                                                                                                                                                                                                                                                            | character_set_client | collation_connection | Database Collation |
+--------------------+-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| insert_into_t5_sp5 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`rumination`@`localhost` PROCEDURE `insert_into_t5_sp5`(in min_idx_value int,in max_idx_value int)
begin
while(min_idx_value<max_idx_value)
do
insert into t5(topic ,idx) values( uuid(),min_idx_value*2);
set min_idx_value=min_idx_value+1;
end while;
end | utf8mb4              | utf8mb4_0900_ai_ci   | utf8mb4_0900_ai_ci |
+--------------------+-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)*/
    // insert_into_t5_sp5`(in min_idx_value int,in max_idx_value int)

    // std::auto_ptr<sql::PreparedStatement> pstmt;
    sql::PreparedStatement *pstmt = conn->prepareStatement("CALL insert_into_t5_sp5(?,?)");
    pstmt->setInt(1, min_value);
    pstmt->setInt(2, max_value);
    pstmt->execute();
    std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ",line:" << __LINE__ << " of " << __FUNCTION__ << std::endl;
}

int main(int args, char **argv)
{
    cpp_call_proc(atoi(argv[1]), atoi(argv[2]));
    std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ",line:" << __LINE__ << " of " << __FUNCTION__ << std::endl;
}

 

g++-13 -g -std=c++23 -I. main.cpp -luuid -lpthread -lmysqlcppconn -o h1;

 

./h1 1 10
./h1 1 10;
true
20231214113002_308175336,thread id:140675402647360,line:83 of cpp_call_proc
20231214113002_308275370,thread id:140675402647360,line:89 of main

 

 

mysql> select * from t5;
+----+--------------------------------------+--------------------------------------+--------------------------------------+-----+
| id | name                                 | author                               | topic                                | idx |
+----+--------------------------------------+--------------------------------------+--------------------------------------+-----+
|  1 | 10e18c20-9a31-11ee-9410-080027b9fde5 | 10e18c23-9a31-11ee-9410-080027b9fde5 | 10e18c0c-9a31-11ee-9410-080027b9fde5 |   2 |
|  2 | 10e3729c-9a31-11ee-9410-080027b9fde5 | 10e3729e-9a31-11ee-9410-080027b9fde5 | 10e37288-9a31-11ee-9410-080027b9fde5 |   4 |
|  3 | 10e5cf45-9a31-11ee-9410-080027b9fde5 | 10e5cf47-9a31-11ee-9410-080027b9fde5 | 10e5cf20-9a31-11ee-9410-080027b9fde5 |   6 |
|  4 | 10e71cd9-9a31-11ee-9410-080027b9fde5 | 10e71cdc-9a31-11ee-9410-080027b9fde5 | 10e71cc7-9a31-11ee-9410-080027b9fde5 |   8 |
|  5 | 10e7f05c-9a31-11ee-9410-080027b9fde5 | 10e7f05f-9a31-11ee-9410-080027b9fde5 | 10e7f049-9a31-11ee-9410-080027b9fde5 |  10 |
|  6 | 10e8f69b-9a31-11ee-9410-080027b9fde5 | 10e8f69d-9a31-11ee-9410-080027b9fde5 | 10e8f691-9a31-11ee-9410-080027b9fde5 |  12 |
|  7 | 10ea06a2-9a31-11ee-9410-080027b9fde5 | 10ea06a4-9a31-11ee-9410-080027b9fde5 | 10ea0696-9a31-11ee-9410-080027b9fde5 |  14 |
|  8 | 10eadb59-9a31-11ee-9410-080027b9fde5 | 10eadb5c-9a31-11ee-9410-080027b9fde5 | 10eadb45-9a31-11ee-9410-080027b9fde5 |  16 |
|  9 | 10eb94ee-9a31-11ee-9410-080027b9fde5 | 10eb94f0-9a31-11ee-9410-080027b9fde5 | 10eb94da-9a31-11ee-9410-080027b9fde5 |  18 |
+----+--------------------------------------+--------------------------------------+--------------------------------------+-----+
9 rows in set (0.00 sec)