Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .clangd
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Diagnostics:
ClangTidy:
Remove: bugprone-unused-return-value
Style:
AngledHeaders: ["boost/.*"]
108 changes: 68 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,33 @@
cmake_minimum_required(VERSION 3.8...3.22)

# Project
project(boost_mysql VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX)
project(mycosql LANGUAGES CXX)

# Library
add_library(boost_mysql INTERFACE)
add_library(Boost::mysql ALIAS boost_mysql)
add_library(boost_mycosql
src/character_set.cpp
src/column_type.cpp
src/connection.cpp
src/date.cpp
src/datetime.cpp
src/error_categories.cpp
src/escape_string.cpp
src/execution_state_impl.cpp
src/field.cpp
src/field_kind.cpp
src/field_view.cpp
src/format_sql.cpp
src/is_fatal_error.cpp
src/meta_check_context.cpp
src/pipeline.cpp
src/results_impl.cpp
src/resultset.cpp
src/row_impl.cpp
src/server_error_to_string.cpp
src/static_execution_state_impl.cpp
src/static_results_impl.cpp
)
add_library(Boost::mycosql ALIAS boost_mycosql)

# Dependencies. If non-Boost dependencies are not found, we just bail out
find_package(Threads)
Expand All @@ -29,9 +51,10 @@ endif()
# This is generated by boostdep.
# Note that Boost::pfr is not listed because it's a peer dependency
target_link_libraries(
boost_mysql
INTERFACE
Boost::asio
boost_mycosql
PUBLIC
Boost::corosio
Boost::corosio_openssl
Boost::assert
Boost::charconv
Boost::compat
Expand All @@ -52,45 +75,50 @@ target_link_libraries(
)

# Includes & features
target_include_directories(boost_mysql INTERFACE include)
target_compile_features(boost_mysql INTERFACE cxx_std_11)
target_include_directories(boost_mycosql PUBLIC include)
target_include_directories(boost_mycosql PRIVATE src)
target_compile_features(boost_mycosql PUBLIC cxx_std_20)

# Don't run integration testing unless explicitly requested, since these require a running MySQL server
option(BOOST_MYSQL_INTEGRATION_TESTS OFF "Whether to build and run integration tests or not")
mark_as_advanced(BOOST_MYSQL_INTEGRATION_TESTS)
# TODO: remove
add_executable(mycosql_example example/1_tutorial/1_sync.cpp)
target_link_libraries(mycosql_example PRIVATE Boost::mycosql)

# List of server features that the CI DB server does not support.
# Disables running some integration tests and examples
set(BOOST_MYSQL_DISABLED_SERVER_FEATURES "" CACHE STRING
"A CMake list of server features not supported by the CI server, for integration tests"
)
mark_as_advanced(BOOST_MYSQL_DISABLED_SERVER_FEATURES)
# # Don't run integration testing unless explicitly requested, since these require a running MySQL server
# option(BOOST_MYSQL_INTEGRATION_TESTS OFF "Whether to build and run integration tests or not")
# mark_as_advanced(BOOST_MYSQL_INTEGRATION_TESTS)

# # List of server features that the CI DB server does not support.
# # Disables running some integration tests and examples
# set(BOOST_MYSQL_DISABLED_SERVER_FEATURES "" CACHE STRING
# "A CMake list of server features not supported by the CI server, for integration tests"
# )
# mark_as_advanced(BOOST_MYSQL_DISABLED_SERVER_FEATURES)

# Don't build benchmarks unless explicitly requested, since these require the official
# MySQL and MariaDB client libraries
option(BOOST_MYSQL_BENCH OFF "Whether to build the benchmarks")
mark_as_advanced(BOOST_MYSQL_BENCH)
# # Don't build benchmarks unless explicitly requested, since these require the official
# # MySQL and MariaDB client libraries
# option(BOOST_MYSQL_BENCH OFF "Whether to build the benchmarks")
# mark_as_advanced(BOOST_MYSQL_BENCH)

# Examples and tests
if(BUILD_TESTING)
# Contains some functions to share code between examples and tests
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake)
# # Examples and tests
# if(BUILD_TESTING)
# # Contains some functions to share code between examples and tests
# include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake)

# Custom target tests; required by the Boost superproject
if(NOT TARGET tests)
add_custom_target(tests)
endif()
# # Custom target tests; required by the Boost superproject
# if(NOT TARGET tests)
# add_custom_target(tests)
# endif()

# Tests
add_subdirectory(test)
# # Tests
# add_subdirectory(test)

# All examples require a real server to run
if (BOOST_MYSQL_INTEGRATION_TESTS)
add_subdirectory(example)
endif()
endif()
# # All examples require a real server to run
# if (BOOST_MYSQL_INTEGRATION_TESTS)
# add_subdirectory(example)
# endif()
# endif()

if (BOOST_MYSQL_BENCH)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake)
add_subdirectory(bench)
endif()
# if (BOOST_MYSQL_BENCH)
# include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake)
# add_subdirectory(bench)
# endif()
93 changes: 35 additions & 58 deletions example/1_tutorial/1_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,78 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

//[example_tutorial_sync

/**
* Creates a connection, establishes a session and
* runs a simple "Hello world!" query.
*
* This example uses synchronous functions and handles errors using exceptions.
*/

#include <boost/mysql/any_address.hpp>
#include <boost/mysql/any_connection.hpp>
#include <boost/mysql/connect_params.hpp>
#include <boost/mysql/error_with_diagnostics.hpp>
#include <boost/mysql/diagnostics.hpp>
#include <boost/mysql/results.hpp>

#include <boost/asio/io_context.hpp>
#include <boost/capy/ex/run_async.hpp>
#include <boost/capy/ex/this_coro.hpp>
#include <boost/capy/io_task.hpp>
#include <boost/corosio/io_context.hpp>

#include <iostream>

//[tutorial_sync_namespaces
namespace mysql = boost::mysql;
namespace asio = boost::asio;
//]
namespace corosio = boost::corosio;
namespace capy = boost::capy;

void main_impl(int argc, char** argv)
static void check_err(capy::io_result<mysql::diagnostics> result)
{
if (argc != 4)
if (result.ec)
{
std::cerr << "Usage: " << argv[0] << " <username> <password> <server-hostname>\n";
std::cerr << "Error connecting: " << result.ec << ": " << result.get<1>().server_message()
<< std::endl;
exit(1);
}
}

const char* hostname = argv[3];
const char* username = argv[1];
const char* password = argv[2];

//[tutorial_sync_connection
// The execution context, required to run I/O operations.
asio::io_context ctx;
capy::io_task<> main_impl()
{
// if (argc != 4)
// {
// std::cerr << "Usage: " << argv[0] << " <username> <password> <server-hostname>\n";
// exit(1);
// }

// Represents a connection to the MySQL server.
mysql::any_connection conn(ctx);
//]
mysql::any_connection conn(co_await capy::this_coro::executor);

//[tutorial_sync_main
//[tutorial_sync_connect
// The hostname, username and password to use
mysql::connect_params params;
params.server_address.emplace_host_and_port(hostname);
params.username = username;
params.password = password;
mysql::connect_params params{
.server_address = mysql::host_and_port("localhost"),
.username = "root",
.password = "",
};

// Connect to the server
conn.connect(params);
//]
check_err(co_await conn.connect(params));

//[tutorial_sync_query
// Issue the SQL query to the server
const char* sql = "SELECT 'Hello world!'";
mysql::results result;
conn.execute(sql, result);
//]
check_err(co_await conn.execute(sql, result));

//[tutorial_sync_results
// Print the first field in the first row
std::cout << result.rows().at(0).at(0) << std::endl;
//]

//[tutorial_sync_close
// Close the connection
conn.close();
//]
//]
// // Close the connection
// conn.close();

co_return {};
}

int main(int argc, char** argv)
{
try
{
main_impl(argc, argv);
}
catch (const mysql::error_with_diagnostics& err)
{
// Some errors include additional diagnostics, like server-provided error messages.
// Security note: diagnostics::server_message may contain user-supplied values (e.g. the
// field value that caused the error) and is encoded using to the connection's character set
// (UTF-8 by default). Treat is as untrusted input.
std::cerr << "Error: " << err.what() << '\n'
<< "Server diagnostics: " << err.get_diagnostics().server_message() << std::endl;
return 1;
}
catch (const std::exception& err)
{
std::cerr << "Error: " << err.what() << std::endl;
return 1;
}
corosio::io_context ctx;
capy::run_async(ctx.get_executor())(main_impl());
ctx.run();
}

//]
1 change: 0 additions & 1 deletion include/boost/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#include <boost/mysql/statement.hpp>
#include <boost/mysql/static_execution_state.hpp>
#include <boost/mysql/static_results.hpp>
#include <boost/mysql/string_view.hpp>
#include <boost/mysql/tcp.hpp>
#include <boost/mysql/tcp_ssl.hpp>
#include <boost/mysql/throw_on_error.hpp>
Expand Down
8 changes: 5 additions & 3 deletions include/boost/mysql/any_address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#define BOOST_MYSQL_ANY_ADDRESS_HPP

#include <boost/mysql/defaults.hpp>
#include <boost/mysql/string_view.hpp>

#include <boost/mysql/detail/access.hpp>

#include <boost/assert.hpp>

#include <string>
#include <string_view>

namespace boost {
namespace mysql {
Expand Down Expand Up @@ -187,7 +189,7 @@ class any_address
* \par Exception safety
* No-throw guarantee.
*/
string_view hostname() const noexcept
std::string_view hostname() const noexcept
{
BOOST_ASSERT(type() == address_type::host_and_port);
return impl_.address;
Expand Down Expand Up @@ -219,7 +221,7 @@ class any_address
* \par Exception safety
* No-throw guarantee.
*/
string_view unix_socket_path() const noexcept
std::string_view unix_socket_path() const noexcept
{
BOOST_ASSERT(type() == address_type::unix_path);
return impl_.address;
Expand Down
Loading
Loading