From a04bb0245c391fb7341e116cccd00e6c2604a78e Mon Sep 17 00:00:00 2001 From: self-consstency Date: Tue, 15 Apr 2025 15:15:01 +0000 Subject: [PATCH 1/6] feat: add a demo of C++ using mysql connector --- .github/workflows/basic-workflow.yml | 8 + .github/workflows/cpp.yml | 24 ++ c_cplusplus/mysql-connector-cpp/README-CN.md | 206 +++++++++++++++++ c_cplusplus/mysql-connector-cpp/README.md | 212 ++++++++++++++++++ c_cplusplus/mysql-connector-cpp/run.sh | 24 ++ .../src/mysql_connector_test.cpp | 82 +++++++ 6 files changed, 556 insertions(+) create mode 100644 .github/workflows/cpp.yml create mode 100644 c_cplusplus/mysql-connector-cpp/README-CN.md create mode 100644 c_cplusplus/mysql-connector-cpp/README.md create mode 100755 c_cplusplus/mysql-connector-cpp/run.sh create mode 100644 c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp diff --git a/.github/workflows/basic-workflow.yml b/.github/workflows/basic-workflow.yml index 04293d7..58bf16b 100644 --- a/.github/workflows/basic-workflow.yml +++ b/.github/workflows/basic-workflow.yml @@ -54,6 +54,14 @@ jobs: with: ruby-version: 3.1 bundler-cache: true + - name: Setup C++ env + if: ${{ inputs.language == 'cpp' }} + uses: aminya/setup-cpp@v1 + with: + compiler: gcc + run: | + sudo apt-get update + sudo apt-get install -y libmysqlcppconn-dev - name: Start OceanBase container if: ${{ inputs.with_oceanbase_container }} uses: oceanbase/setup-oceanbase-ce@v1 diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml new file mode 100644 index 0000000..8d8672f --- /dev/null +++ b/.github/workflows/cpp.yml @@ -0,0 +1,24 @@ +name: C++ CI + +on: + push: + paths: + - '.github/workflows/cpp.yml' + - 'c_cplusplus/**' + pull_request: + paths: + - '.github/workflows/cpp.yml' + - 'c_cplusplus/**' + +jobs: + ci: + strategy: + matrix: + module: + - name: 'mysql-connector-cpp' + with_oceanbase_container: true + uses: ./.github/workflows/basic-workflow.yml + with: + language: 'cpp' + module: ${{ matrix.module.name }} + with_oceanbase_container: ${{ matrix.module.with_oceanbase_container }} \ No newline at end of file diff --git a/c_cplusplus/mysql-connector-cpp/README-CN.md b/c_cplusplus/mysql-connector-cpp/README-CN.md new file mode 100644 index 0000000..9f4872f --- /dev/null +++ b/c_cplusplus/mysql-connector-cpp/README-CN.md @@ -0,0 +1,206 @@ +# 使用 C++ 连接 OceanBase (mysql-connector-cpp) + +[English](./README.md) | 简体中文 + +## 简介 + +本示例演示了如何使用 MySQL Connector/C++ 连接 OceanBase 数据库。示例展示了基本的数据库操作,包括: +- 创建连接 +- 创建表 +- 插入数据 +- 查询数据 +- 错误处理 + +## 环境要求 + +- **OceanBase 数据库**: + - 可以使用 OceanBase Docker 镜像进行测试: + ```bash + docker pull oceanbase/oceanbase-ce + docker run -p 2881:2881 --name oceanbase-ce -d oceanbase/oceanbase-ce + ``` + +- **开发环境**: + - 支持 C++11 的 C++ 编译器 + - MySQL Connector/C++ (libmysqlcppconn) + +## 安装 + +首先,在本地机器上安装 MySQL Connector/C++ 包。请参考[官方文档](https://dev.mysql.com/doc/dev/connector-cpp/latest/)获取针对您特定操作系统的安装说明。 + +## 快速开始 + +1. **克隆仓库**: + ```bash + git clone https://github.com/oceanbase/ob-samples.git + cd ob-samples/c_cplusplus/mysql-connector-cpp + ``` + +2. **编译并运行示例**: + ```bash + ./run.sh + ``` + + 或者使用自定义连接参数: + ```bash + ./run.sh [url] [username] [password] [database] + ``` + +## 连接参数 + +示例接受以下可选参数(所有参数都是可选的,如果不提供将使用默认值): +- `url`: 数据库服务器 URL(默认:tcp://127.0.0.1:2881) +- `username`: 数据库用户名(默认:root@test) +- `password`: 数据库密码(默认:"") +- `database`: 数据库名称(默认:test) + +示例: +```bash +# 使用默认参数 +./run.sh + +# 使用自定义参数 +./run.sh tcp://127.0.0.1:2881 root@test "" test +``` + +**注意**:参数必须按顺序从左到右提供,不能跳过中间参数。 + +## 示例代码 + +示例 [mysql_connector_test](./src/mysql_connector_test.cpp) 演示了基本的数据库操作: + +```cpp +/* Standard C++ includes */ +#include +#include + +#include + +#define DEFAULT_URI "tcp://127.0.0.1:2881" +#define EXAMPLE_USER "root@test" +#define EXAMPLE_PASSWORD "" +#define EXAMPLE_DB "test" + +using namespace std; + +int main(int argc, char *argv[]) { + try { + const char *url = (argc > 1 ? argv[1] : DEFAULT_URI); + const string user(argc >= 3 ? argv[2] : EXAMPLE_USER); + const string password(argc >= 4 ? argv[3] : EXAMPLE_PASSWORD); + const string database(argc >= 5 ? argv[4] : EXAMPLE_DB); + cout << "url: " << url << endl; + cout << "user: " << user << endl; + cout << "password: " << password << endl; + cout << "database: " << database << endl; + sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance(); + + cout << "Connected to OceanBase server..." << endl; + unique_ptr< sql::Connection > con{driver->connect(url, user, password)}; + + + con->setSchema(database); + + unique_ptr< sql::Statement > stmt{con->createStatement()}; + + // Drop table if exists + cout << "Dropping table if exists..." << endl; + stmt->execute("DROP TABLE IF EXISTS `t_test`"); + + // Create table + cout << "Creating table..." << endl; + stmt->execute("CREATE TABLE `t_test` (" + " `id` int(10) NOT NULL AUTO_INCREMENT," + " `name` varchar(20) DEFAULT NULL," + " PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE = utf8_bin"); + + // Insert data + cout << "Inserting data..." << endl; + stmt->execute("INSERT INTO `t_test` VALUES (default, 'Hello OceanBase')"); + + // Query data + cout << "Querying data..." << endl; + std::unique_ptr< sql::ResultSet > res{stmt->executeQuery("SELECT * FROM `t_test`")}; + + cout << "Query results:" << endl; + while (res->next()) { + cout << "id: " << res->getInt("id") + << ", name: " << res->getString("name") << endl; + } + + cout << "Done!" << endl; + } + catch (sql::SQLException &e) + { + /* + The JDBC API throws three different exceptions: + + - sql::MethodNotImplementedException (derived from sql::SQLException) + - sql::InvalidArgumentException (derived from sql::SQLException) + - sql::SQLException (derived from std::runtime_error) + */ + + cout << "# ERR: SQLException in " << __FILE__; + cout << "(" << "EXAMPLE_FUNCTION" << ") on line " << __LINE__ << endl; + + /* Use what() (derived from std::runtime_error) to fetch the error message */ + + cout << "# ERR: " << e.what(); + cout << " (MySQL error code: " << e.getErrorCode(); + cout << ", SQLState: " << e.getSQLState() << " )" << endl; + + return EXIT_FAILURE; + } + + + return EXIT_SUCCESS; +} +``` + +## 示例输出 + +使用默认参数运行示例时,您将看到如下输出: + +```bash +$ ./run.sh +Connected to OceanBase server... +Dropping table if exists... +Creating table... +Inserting data... +Querying data... +Query results: +id: 1, name: Hello OceanBase +Done! +``` + +## 编译选项 + +示例可以使用提供的 `run.sh` 脚本或手动编译: + +```bash +# 使用 run.sh +./run.sh + +# 手动编译 +g++ -std=c++11 -I/usr/include/mysql-cppconn-8 src/mysql_connector_test.cpp -o mysql_connector_test -lmysqlcppconn +``` + +**注意**:您可能需要将 `/usr/include/mysql-cppconn-8`(在 [run.sh](./run.sh) 中)替换为您实际的 MySQL Connector/C++ 安装路径。 + +## 故障排除 + +1. **编译错误**: + - 确保已正确安装 MySQL Connector/C++ 开发包 + - 检查 `run.sh` 中的头文件包含路径是否正确 + - 确保编译器支持 C++11 标准 + +2. **连接错误**: + - 确认 OceanBase 数据库服务是否正常运行 + - 检查数据库连接参数是否正确 + - 确认网络连接是否正常,端口是否开放 + +3. **运行时错误**: + - 查看具体的错误信息,了解问题原因 + - 确认数据库用户是否有足够的权限执行操作 + - 确保数据库 schema 和表已正确创建 diff --git a/c_cplusplus/mysql-connector-cpp/README.md b/c_cplusplus/mysql-connector-cpp/README.md new file mode 100644 index 0000000..9550608 --- /dev/null +++ b/c_cplusplus/mysql-connector-cpp/README.md @@ -0,0 +1,212 @@ +# Connect OceanBase with C++ (mysql-connector-cpp) + +English | [简体中文](./README-CN.md) + +## Introduction + +This example demonstrates how to connect to OceanBase database using MySQL Connector/C++. The example shows basic database operations including: +- Creating a connection +- Creating a table +- Inserting data +- Querying data +- Error handling + +## Prerequisites + +- **OceanBase Database**: + - You can use OceanBase Docker image for testing: + ```bash + docker pull oceanbase/oceanbase-ce + docker run -p 2881:2881 --name oceanbase-ce -d oceanbase/oceanbase-ce + ``` + +- **Development Environment**: + - C++ compiler with C++11 support + - MySQL Connector/C++ (libmysqlcppconn) + +## Installation + +First, install the MySQL Connector/C++ package onto your local machine. Refer to the [official documentation](https://dev.mysql.com/doc/dev/connector-cpp/latest/) for installation instructions for your specific operating system. + +## Quick Start + +1. **Clone the repository**: + ```bash + git clone https://github.com/oceanbase/ob-samples.git + cd ob-samples/c_cplusplus/mysql-connector-cpp + ``` + +2. **Compile and run the example**: + ```bash + ./run.sh + ``` + + Or with custom connection parameters: + ```bash + ./run.sh [url] [username] [password] [database] + ``` + +## Connection Parameters + +The example accepts the following optional parameters (all parameters are optional and will use default values if not provided): +- `url`: Database server URL (default: tcp://127.0.0.1:2881) +- `username`: Database username (default: root@test) +- `password`: Database password (default: "") +- `database`: Database name (default: test) + +Example: +```bash +# Use default parameters +./run.sh + +# Use custom parameters +./run.sh tcp://127.0.0.1:2881 root@test "" test +``` + +**Note**: Parameters must be provided in order, from left to right. You cannot skip parameters in the middle. + +## Example Code + +The example [mysql_connector_test](./src/mysql_connector_test.cpp) demonstrates basic database operations: + +```cpp +/* Standard C++ includes */ +#include +#include + +#include + +#define DEFAULT_URI "tcp://127.0.0.1:2881" +#define EXAMPLE_USER "root@test" +#define EXAMPLE_PASSWORD "" +#define EXAMPLE_DB "test" + +using namespace std; + +int main(int argc, char *argv[]) { + try { + const char *url = (argc > 1 ? argv[1] : DEFAULT_URI); + const string user(argc >= 3 ? argv[2] : EXAMPLE_USER); + const string password(argc >= 4 ? argv[3] : EXAMPLE_PASSWORD); + const string database(argc >= 5 ? argv[4] : EXAMPLE_DB); + cout << "url: " << url << endl; + cout << "user: " << user << endl; + cout << "password: " << password << endl; + cout << "database: " << database << endl; + sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance(); + + cout << "Connected to OceanBase server..." << endl; + unique_ptr< sql::Connection > con{driver->connect(url, user, password)}; + + + con->setSchema(database); + + unique_ptr< sql::Statement > stmt{con->createStatement()}; + + // Drop table if exists + cout << "Dropping table if exists..." << endl; + stmt->execute("DROP TABLE IF EXISTS `t_test`"); + + // Create table + cout << "Creating table..." << endl; + stmt->execute("CREATE TABLE `t_test` (" + " `id` int(10) NOT NULL AUTO_INCREMENT," + " `name` varchar(20) DEFAULT NULL," + " PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE = utf8_bin"); + + // Insert data + cout << "Inserting data..." << endl; + stmt->execute("INSERT INTO `t_test` VALUES (default, 'Hello OceanBase')"); + + // Query data + cout << "Querying data..." << endl; + std::unique_ptr< sql::ResultSet > res{stmt->executeQuery("SELECT * FROM `t_test`")}; + + cout << "Query results:" << endl; + while (res->next()) { + cout << "id: " << res->getInt("id") + << ", name: " << res->getString("name") << endl; + } + + cout << "Done!" << endl; + } + catch (sql::SQLException &e) + { + /* + The JDBC API throws three different exceptions: + + - sql::MethodNotImplementedException (derived from sql::SQLException) + - sql::InvalidArgumentException (derived from sql::SQLException) + - sql::SQLException (derived from std::runtime_error) + */ + + cout << "# ERR: SQLException in " << __FILE__; + cout << "(" << "EXAMPLE_FUNCTION" << ") on line " << __LINE__ << endl; + + /* Use what() (derived from std::runtime_error) to fetch the error message */ + + cout << "# ERR: " << e.what(); + cout << " (MySQL error code: " << e.getErrorCode(); + cout << ", SQLState: " << e.getSQLState() << " )" << endl; + + return EXIT_FAILURE; + } + + + return EXIT_SUCCESS; +} +``` + +## Example Output + +When you run the example with default parameters, you should see output as follows: + +```bash +$ ./run.sh +Connected to OceanBase server... +Dropping table if exists... +Creating table... +Inserting data... +Querying data... +Query results: +id: 1, name: Hello OceanBase +Done! +``` + +## Build Options + +The example can be built using the provided `run.sh` script or manually: + +```bash +# Using run.sh +./run.sh + +# Manual build +g++ -std=c++11 -I/usr/include/mysql-cppconn-8 src/mysql_connector_test.cpp -o mysql_connector_test -lmysqlcppconn +``` + +**Note**: You may need to replace `/usr/include/mysql-cppconn-8` (in [run.sh](./run.sh)) with your actual MySQL Connector/C++ installation path. + +## Troubleshooting + +1. **Compilation Errors**: + - Ensure MySQL Connector/C++ development package is properly installed + - Check if the include path in `run.sh` is correct + - Verify that your compiler supports C++11 standard + +2. **Connection Errors**: + - Verify that the OceanBase database service is running + - Check if the database connection parameters are correct + - Ensure network connectivity and port accessibility + +3. **Runtime Errors**: + - Check the specific error message for the root cause + - Verify that the database user has sufficient permissions + - Ensure the database schema and tables are properly created + + + + + + diff --git a/c_cplusplus/mysql-connector-cpp/run.sh b/c_cplusplus/mysql-connector-cpp/run.sh new file mode 100755 index 0000000..f306908 --- /dev/null +++ b/c_cplusplus/mysql-connector-cpp/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Usage: ./run.sh [url] [username] [password] [database] +# url: database server URL (default: tcp://127.0.0.1:2881) +# username: database username (default: root@test) +# password: database password (default: "") +# database: database name (default: test) + +# Note: Parameters must be provided in order, from left to right. +# You cannot skip parameters in the middle. +# For example: +# Valid: ./run.sh tcp://127.0.0.1:2881 root@test "" test +# Valid: ./run.sh tcp://127.0.0.1:2881 +# Invalid: ./run.sh tcp://127.0.0.1:2881 test (skipping username and password) +# All parameters are optional and will use default values if not provided + + + +# Compile the program +# Note: You may need to modify the include path (-I) based on your MySQL Connector/C++ installation +g++ -std=c++11 -I/usr/include/mysql-cppconn-8 src/mysql_connector_test.cpp -o mysql_connector_test -lmysqlcppconn + +# Run the test +./mysql_connector_test "$@" \ No newline at end of file diff --git a/c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp b/c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp new file mode 100644 index 0000000..f6c578a --- /dev/null +++ b/c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp @@ -0,0 +1,82 @@ +/* Standard C++ includes */ +#include +#include + +#include + +#define DEFAULT_URI "tcp://127.0.0.1:2881" +#define EXAMPLE_USER "root@test" +#define EXAMPLE_PASSWORD "" +#define EXAMPLE_DB "test" + +using namespace std; + +int main(int argc, char *argv[]) { + try { + const char *url = (argc > 1 ? argv[1] : DEFAULT_URI); + const string user(argc >= 3 ? argv[2] : EXAMPLE_USER); + const string password(argc >= 4 ? argv[3] : EXAMPLE_PASSWORD); + const string database(argc >= 5 ? argv[4] : EXAMPLE_DB); + sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance(); + + cout << "Connected to OceanBase server..." << endl; + unique_ptr< sql::Connection > con{driver->connect(url, user, password)}; + + + con->setSchema(database); + + unique_ptr< sql::Statement > stmt{con->createStatement()}; + + // Drop table if exists + cout << "Dropping table if exists..." << endl; + stmt->execute("DROP TABLE IF EXISTS `t_test`"); + + // Create table + cout << "Creating table..." << endl; + stmt->execute("CREATE TABLE `t_test` (" + " `id` int(10) NOT NULL AUTO_INCREMENT," + " `name` varchar(20) DEFAULT NULL," + " PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE = utf8_bin"); + + // Insert data + cout << "Inserting data..." << endl; + stmt->execute("INSERT INTO `t_test` VALUES (default, 'Hello OceanBase')"); + + // Query data + cout << "Querying data..." << endl; + std::unique_ptr< sql::ResultSet > res{stmt->executeQuery("SELECT * FROM `t_test`")}; + + cout << "Query results:" << endl; + while (res->next()) { + cout << "id: " << res->getInt("id") + << ", name: " << res->getString("name") << endl; + } + + cout << "Done!" << endl; + } + catch (sql::SQLException &e) + { + /* + The JDBC API throws three different exceptions: + + - sql::MethodNotImplementedException (derived from sql::SQLException) + - sql::InvalidArgumentException (derived from sql::SQLException) + - sql::SQLException (derived from std::runtime_error) + */ + + cout << "# ERR: SQLException in " << __FILE__; + cout << "(" << "EXAMPLE_FUNCTION" << ") on line " << __LINE__ << endl; + + /* Use what() (derived from std::runtime_error) to fetch the error message */ + + cout << "# ERR: " << e.what(); + cout << " (MySQL error code: " << e.getErrorCode(); + cout << ", SQLState: " << e.getSQLState() << " )" << endl; + + return EXIT_FAILURE; + } + + + return EXIT_SUCCESS; +} \ No newline at end of file From 539b17ff7b6df1ac7e5e62a13c4d194fd4352c71 Mon Sep 17 00:00:00 2001 From: self-consstency Date: Tue, 15 Apr 2025 15:42:09 +0000 Subject: [PATCH 2/6] fix: update basic-workflow.yml to support C++ samples --- .github/workflows/basic-workflow.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/basic-workflow.yml b/.github/workflows/basic-workflow.yml index 58bf16b..5c6a956 100644 --- a/.github/workflows/basic-workflow.yml +++ b/.github/workflows/basic-workflow.yml @@ -54,11 +54,13 @@ jobs: with: ruby-version: 3.1 bundler-cache: true - - name: Setup C++ env + - name: Setup C++ compiler if: ${{ inputs.language == 'cpp' }} uses: aminya/setup-cpp@v1 with: - compiler: gcc + compiler: gcc + - name: Install MySQL Connector/C++ + if: ${{ inputs.language == 'cpp' }} run: | sudo apt-get update sudo apt-get install -y libmysqlcppconn-dev @@ -70,5 +72,9 @@ jobs: image_tag: ${{ inputs.oceanbase_image_tag || 'latest' }} - name: Run sample for ${{ inputs.module }} run: | - cd ${{ inputs.language }}/${{ inputs.module }} || exit + if [ "${{ inputs.language }}" = "cpp" ]; then + cd c_cplusplus/${{ inputs.module }} || exit + else + cd ${{ inputs.language }}/${{ inputs.module }} || exit + fi sh run.sh From 923e7d6bb0264466c0583ec72b261e242b9e4191 Mon Sep 17 00:00:00 2001 From: self-consstency Date: Thu, 17 Apr 2025 06:02:53 +0000 Subject: [PATCH 3/6] fix: update run.sh to search feasible include path for mysql connector --- c_cplusplus/mysql-connector-cpp/run.sh | 48 +++++++++++++++++--------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/c_cplusplus/mysql-connector-cpp/run.sh b/c_cplusplus/mysql-connector-cpp/run.sh index f306908..24491e7 100755 --- a/c_cplusplus/mysql-connector-cpp/run.sh +++ b/c_cplusplus/mysql-connector-cpp/run.sh @@ -1,24 +1,40 @@ #!/bin/bash -# Usage: ./run.sh [url] [username] [password] [database] -# url: database server URL (default: tcp://127.0.0.1:2881) -# username: database username (default: root@test) -# password: database password (default: "") -# database: database name (default: test) +# environment info +echo "=== Environment Info ===" +echo "C++ compiler:" +g++ --version +echo "MySQL Connector/C++:" +find /usr -name "jdbc.h" 2>/dev/null -# Note: Parameters must be provided in order, from left to right. -# You cannot skip parameters in the middle. -# For example: -# Valid: ./run.sh tcp://127.0.0.1:2881 root@test "" test -# Valid: ./run.sh tcp://127.0.0.1:2881 -# Invalid: ./run.sh tcp://127.0.0.1:2881 test (skipping username and password) -# All parameters are optional and will use default values if not provided +# set include path +INCLUDE_PATH="/usr/include/mysql-cppconn-8" +# check if the path exists +if [ ! -d "$INCLUDE_PATH/mysql" ]; then + echo "Warning: Default include path not found, searching alternatives..." + # search alternatives + ALTERNATIVE_PATH=$(find /usr -name "jdbc.h" 2>/dev/null | grep "mysql/jdbc.h" | xargs dirname | xargs dirname) + if [ -n "$ALTERNATIVE_PATH" ]; then + INCLUDE_PATH="$ALTERNATIVE_PATH" + echo "Using alternative path: $INCLUDE_PATH" + else + echo "Error: Could not find MySQL Connector/C++ include path" + exit 1 + fi +fi +# compile +echo "=== Compiling ===" +echo "Using include path: $INCLUDE_PATH" +g++ -std=c++11 -I$INCLUDE_PATH src/mysql_connector_test.cpp -o mysql_connector_test -lmysqlcppconn -# Compile the program -# Note: You may need to modify the include path (-I) based on your MySQL Connector/C++ installation -g++ -std=c++11 -I/usr/include/mysql-cppconn-8 src/mysql_connector_test.cpp -o mysql_connector_test -lmysqlcppconn +# check compile result +if [ $? -ne 0 ]; then + echo "Compilation failed" + exit 1 +fi -# Run the test +# run +echo "=== Running ===" ./mysql_connector_test "$@" \ No newline at end of file From a16802ad7efc6f9b5228751b5adc8a2c70f6ffa6 Mon Sep 17 00:00:00 2001 From: self-consstency Date: Thu, 17 Apr 2025 06:16:10 +0000 Subject: [PATCH 4/6] fix: update run.sh to search feasible include path --- c_cplusplus/mysql-connector-cpp/run.sh | 56 +++++++++++++++++--------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/c_cplusplus/mysql-connector-cpp/run.sh b/c_cplusplus/mysql-connector-cpp/run.sh index 24491e7..d6916c1 100755 --- a/c_cplusplus/mysql-connector-cpp/run.sh +++ b/c_cplusplus/mysql-connector-cpp/run.sh @@ -1,32 +1,50 @@ #!/bin/bash -# environment info +# show environment info echo "=== Environment Info ===" echo "C++ compiler:" g++ --version -echo "MySQL Connector/C++:" -find /usr -name "jdbc.h" 2>/dev/null - -# set include path -INCLUDE_PATH="/usr/include/mysql-cppconn-8" - -# check if the path exists -if [ ! -d "$INCLUDE_PATH/mysql" ]; then - echo "Warning: Default include path not found, searching alternatives..." - # search alternatives - ALTERNATIVE_PATH=$(find /usr -name "jdbc.h" 2>/dev/null | grep "mysql/jdbc.h" | xargs dirname | xargs dirname) - if [ -n "$ALTERNATIVE_PATH" ]; then - INCLUDE_PATH="$ALTERNATIVE_PATH" - echo "Using alternative path: $INCLUDE_PATH" - else - echo "Error: Could not find MySQL Connector/C++ include path" - exit 1 + +echo "=== Package Info ===" +echo "Checking installed packages..." +dpkg -l | grep mysql +dpkg -L libmysqlcppconn-dev + +echo "=== File Search ===" +echo "Searching for MySQL files..." +find /usr -name "*mysql*" 2>/dev/null +find /usr -name "*.h" | grep -i mysql 2>/dev/null + +# set possible paths +POSSIBLE_PATHS=( + "/usr/include/mysql-cppconn-8" + "/usr/include/mysql" + "/usr/include/mysql-cppconn-8/mysql" + "/usr/include/mysql-cppconn-8/jdbc" +) + +# check each possible path +for path in "${POSSIBLE_PATHS[@]}"; do + if [ -d "$path" ]; then + echo "Found directory: $path" + ls -la $path + if [ -f "$path/mysql/jdbc.h" ] || [ -f "$path/jdbc.h" ]; then + INCLUDE_PATH=$path + echo "Using include path: $INCLUDE_PATH" + break + fi fi +done + +if [ -z "$INCLUDE_PATH" ]; then + echo "Error: Could not find MySQL Connector/C++ include path" + echo "Please check if libmysqlcppconn-dev is properly installed" + echo "You can install it with: sudo apt-get install libmysqlcppconn-dev" + exit 1 fi # compile echo "=== Compiling ===" -echo "Using include path: $INCLUDE_PATH" g++ -std=c++11 -I$INCLUDE_PATH src/mysql_connector_test.cpp -o mysql_connector_test -lmysqlcppconn # check compile result From e15d5da68335495df59502802acbf91fc68acdfc Mon Sep 17 00:00:00 2001 From: self-consstency Date: Thu, 17 Apr 2025 06:50:11 +0000 Subject: [PATCH 5/6] fix: update mysql connector c++ headers --- c_cplusplus/mysql-connector-cpp/run.sh | 39 +++++-------------- .../src/mysql_connector_test.cpp | 13 +++++-- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/c_cplusplus/mysql-connector-cpp/run.sh b/c_cplusplus/mysql-connector-cpp/run.sh index d6916c1..41dfa7b 100755 --- a/c_cplusplus/mysql-connector-cpp/run.sh +++ b/c_cplusplus/mysql-connector-cpp/run.sh @@ -1,43 +1,24 @@ #!/bin/bash -# show environment info -echo "=== Environment Info ===" -echo "C++ compiler:" -g++ --version - -echo "=== Package Info ===" -echo "Checking installed packages..." -dpkg -l | grep mysql -dpkg -L libmysqlcppconn-dev - -echo "=== File Search ===" -echo "Searching for MySQL files..." -find /usr -name "*mysql*" 2>/dev/null -find /usr -name "*.h" | grep -i mysql 2>/dev/null - -# set possible paths +# find mysql connector c++ headers +echo "Searching for MySQL Connector/C++ headers..." POSSIBLE_PATHS=( - "/usr/include/mysql-cppconn-8" - "/usr/include/mysql" - "/usr/include/mysql-cppconn-8/mysql" "/usr/include/mysql-cppconn-8/jdbc" + "/usr/include/mysql-cppconn-8" + "/usr/include/" ) -# check each possible path +# find actual header file location for path in "${POSSIBLE_PATHS[@]}"; do - if [ -d "$path" ]; then - echo "Found directory: $path" - ls -la $path - if [ -f "$path/mysql/jdbc.h" ] || [ -f "$path/jdbc.h" ]; then - INCLUDE_PATH=$path - echo "Using include path: $INCLUDE_PATH" - break - fi + if [ -f "$path/cppconn/driver.h" ] || [ -f "$path/driver.h" ]; then + INCLUDE_PATH=$path + echo "Found headers in: $INCLUDE_PATH" + break fi done if [ -z "$INCLUDE_PATH" ]; then - echo "Error: Could not find MySQL Connector/C++ include path" + echo "Error: Could not find MySQL Connector/C++ headers" echo "Please check if libmysqlcppconn-dev is properly installed" echo "You can install it with: sudo apt-get install libmysqlcppconn-dev" exit 1 diff --git a/c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp b/c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp index f6c578a..9a8f2d6 100644 --- a/c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp +++ b/c_cplusplus/mysql-connector-cpp/src/mysql_connector_test.cpp @@ -2,7 +2,12 @@ #include #include -#include +// MySQL Connector/C++ headers +#include +#include +#include +#include +#include #define DEFAULT_URI "tcp://127.0.0.1:2881" #define EXAMPLE_USER "root@test" @@ -17,12 +22,12 @@ int main(int argc, char *argv[]) { const string user(argc >= 3 ? argv[2] : EXAMPLE_USER); const string password(argc >= 4 ? argv[3] : EXAMPLE_PASSWORD); const string database(argc >= 5 ? argv[4] : EXAMPLE_DB); - sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance(); + + sql::Driver *driver = get_driver_instance(); cout << "Connected to OceanBase server..." << endl; unique_ptr< sql::Connection > con{driver->connect(url, user, password)}; - con->setSchema(database); unique_ptr< sql::Statement > stmt{con->createStatement()}; @@ -45,7 +50,7 @@ int main(int argc, char *argv[]) { // Query data cout << "Querying data..." << endl; - std::unique_ptr< sql::ResultSet > res{stmt->executeQuery("SELECT * FROM `t_test`")}; + unique_ptr< sql::ResultSet > res{stmt->executeQuery("SELECT * FROM `t_test`")}; cout << "Query results:" << endl; while (res->next()) { From 2fa4810bc81045a37b590d43cbdf2b2aca32c230 Mon Sep 17 00:00:00 2001 From: self-consstency Date: Thu, 17 Apr 2025 07:10:17 +0000 Subject: [PATCH 6/6] fix: update run.sh to support different shell --- c_cplusplus/mysql-connector-cpp/run.sh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/c_cplusplus/mysql-connector-cpp/run.sh b/c_cplusplus/mysql-connector-cpp/run.sh index 41dfa7b..83b60b2 100755 --- a/c_cplusplus/mysql-connector-cpp/run.sh +++ b/c_cplusplus/mysql-connector-cpp/run.sh @@ -1,15 +1,11 @@ -#!/bin/bash +#!/bin/sh # find mysql connector c++ headers echo "Searching for MySQL Connector/C++ headers..." -POSSIBLE_PATHS=( - "/usr/include/mysql-cppconn-8/jdbc" - "/usr/include/mysql-cppconn-8" - "/usr/include/" -) +POSSIBLE_PATHS="/usr/include/mysql-cppconn-8/jdbc /usr/include/mysql-cppconn-8 /usr/include/" # find actual header file location -for path in "${POSSIBLE_PATHS[@]}"; do +for path in $POSSIBLE_PATHS; do if [ -f "$path/cppconn/driver.h" ] || [ -f "$path/driver.h" ]; then INCLUDE_PATH=$path echo "Found headers in: $INCLUDE_PATH"