Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support optional with mysql and add sqlite ci #94

Merged
merged 7 commits into from
Apr 26, 2023
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml → .github/workflows/ci-mysql.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: CI-MySQL

on: [ push, pull_request ]

Expand All @@ -9,6 +9,7 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
configuration: [ Debug, Release ]
os: [ macos-latest, ubuntu-latest, windows-latest ]
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/ci-sqlite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI-SQLite

on: [ push, pull_request ]

jobs:
build-and-test:

name: ${{ matrix.os }} (${{ matrix.configuration }})
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
configuration: [ Debug, Release ]
os: [ macos-latest, ubuntu-latest, windows-latest ]

steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Configure cmake
run: cmake -B${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{ matrix.configuration }} -DENABLE_SQLITE3=ON

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{ matrix.configuration }}

- name: Test
working-directory: ${{github.workspace}}/build
env:
CTEST_OUTPUT_ON_FAILURE: 1
run: ctest -C ${{ matrix.configuration }} -j `nproc` -V
15 changes: 9 additions & 6 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#include <iostream>

#include "../include/connection_pool.hpp"
#include "../include/dbng.hpp"
#include "connection_pool.hpp"
#include "dbng.hpp"

using namespace ormpp;
const char *password = "";
Expand All @@ -40,7 +40,10 @@ int main() {
{
dbng<mysql> mysql;
if (mysql.connect(ip, "root", password, db)) {
std::cout << "connect success" << std::endl;
mysql.create_datatable<person>(ormpp_auto_key{"id"});
mysql.insert<person>({0, "purecpp"});
mysql.insert<person>({0, "purecpp", 6});
mysql.insert<person>({0, "purecpp", 6, "123456"});
}
else {
std::cout << "connect fail" << std::endl;
Expand All @@ -66,9 +69,9 @@ int main() {
sqlite.create_datatable<person>(ormpp_auto_key{"id"});
sqlite.create_datatable<student>(ormpp_auto_key{"id"});

sqlite.insert<person>({-1, "purecpp"});
sqlite.insert<person>({-1, "purecpp", 666});
sqlite.insert<person>({-1, "purecpp", 666, "123456"});
sqlite.insert<person>({0, "purecpp"});
sqlite.insert<person>({0, "purecpp", 6});
sqlite.insert<person>({0, "purecpp", 6, "123456"});
#endif

return 0;
Expand Down
21 changes: 16 additions & 5 deletions include/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class mysql {
param_binds[index].buffer_length = 65536;
}
else {
static_assert(!sizeof(V), "not support");
static_assert(!sizeof(V), "this type has not supported yet");
}
index++;
});
Expand All @@ -254,7 +254,7 @@ class mysql {
index++;
}
else {
static_assert(!sizeof(U), "not support");
static_assert(!sizeof(U), "this type has not supported yet");
}
},
std::make_index_sequence<SIZE>{});
Expand Down Expand Up @@ -310,7 +310,7 @@ class mysql {
it++;
}
else {
static_assert(!sizeof(V), "not support");
static_assert(!sizeof(V), "this type has not supported yet");
}
++column;
});
Expand All @@ -321,7 +321,7 @@ class mysql {
it++;
}
else {
static_assert(!sizeof(W), "not support");
static_assert(!sizeof(W), "this type has not supported yet");
}
++column;
},
Expand Down Expand Up @@ -602,7 +602,15 @@ class mysql {
MYSQL_BIND param = {};

using U = std::remove_const_t<std::remove_reference_t<T>>;
if constexpr (std::is_arithmetic_v<U>) {
if constexpr (is_optional_v<U>::value) {
if (value.has_value()) {
return set_param_bind(param_binds, std::move(value.value()));
}
else {
param.buffer_type = MYSQL_TYPE_NULL;
}
}
else if constexpr (std::is_arithmetic_v<U>) {
param.buffer_type =
(enum_field_types)ormpp_mysql::type_to_id(identity<U>{});
param.buffer = const_cast<void *>(static_cast<const void *>(&value));
Expand All @@ -622,6 +630,9 @@ class mysql {
param.buffer = (void *)(value.data());
param.buffer_length = (unsigned long)value.size();
}
else {
static_assert(!sizeof(U), "this type has not supported yet");
}
param_binds.push_back(param);
}

Expand Down
4 changes: 2 additions & 2 deletions include/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class sqlite {
sqlite3_bind_text(stmt_, i, value, sizeof(U), nullptr);
}
else {
static_assert(!sizeof(T), "this type has not supported yet");
static_assert(!sizeof(U), "this type has not supported yet");
}
}

Expand Down Expand Up @@ -409,7 +409,7 @@ class sqlite {
memcpy(value, sqlite3_column_text(stmt_, i), sizeof(U));
}
else {
static_assert(!sizeof(T), "this type has not supported yet");
static_assert(!sizeof(U), "this type has not supported yet");
}
}

Expand Down
4 changes: 2 additions & 2 deletions include/type_mapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ inline constexpr auto type_to_name(identity<int64_t>) noexcept {
}
inline auto type_to_name(identity<std::string>) noexcept { return "TEXT"sv; }
template <size_t N>
inline constexpr auto type_to_name(identity<std::array<char, N>>) noexcept {
inline auto type_to_name(identity<std::array<char, N>>) noexcept {
std::string s = "varchar(" + std::to_string(N) + ")";
return s;
}
Expand Down Expand Up @@ -162,7 +162,7 @@ inline constexpr auto type_to_name(identity<int64_t>) noexcept {
}
inline auto type_to_name(identity<std::string>) noexcept { return "text"sv; }
template <size_t N>
inline constexpr auto type_to_name(identity<std::array<char, N>>) noexcept {
inline auto type_to_name(identity<std::array<char, N>>) noexcept {
std::string s = "varchar(" + std::to_string(N) + ")";
return s;
}
Expand Down
28 changes: 19 additions & 9 deletions include/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

namespace ormpp {

template <typename T>
struct is_optional_v : std::false_type {};

template <typename T>
struct is_optional_v<std::optional<T>> : std::true_type {};

template <typename... Args>
struct value_of;

Expand Down Expand Up @@ -86,13 +92,18 @@ inline constexpr auto get_type_names(DBType type) {
}
#ifdef ORMPP_ENABLE_MYSQL
else if (type == DBType::mysql) {
s = ormpp_mysql::type_to_name(identity<U>{});
if constexpr (is_optional_v<U>::value) {
s = ormpp_mysql::type_to_name(identity<typename U::value_type>{});
}
else {
s = ormpp_mysql::type_to_name(identity<U>{});
}
}
#endif
#ifdef ORMPP_ENABLE_SQLITE3
else if (type == DBType::sqlite) {
if constexpr (is_optional_v<U>::value) {
s = ormpp_sqlite::type_to_name(identity<U::value_type>{});
s = ormpp_sqlite::type_to_name(identity<typename U::value_type>{});
}
else {
s = ormpp_sqlite::type_to_name(identity<U>{});
Expand All @@ -101,7 +112,12 @@ inline constexpr auto get_type_names(DBType type) {
#endif
#ifdef ORMPP_ENABLE_PG
else if (type == DBType::postgresql) {
s = ormpp_postgresql::type_to_name(identity<U>{});
if constexpr (is_optional_v<U>::value) {
s = ormpp_postgresql::type_to_name(identity<typename U::value_type>{});
}
else {
s = ormpp_postgresql::type_to_name(identity<U>{});
}
}
#endif

Expand Down Expand Up @@ -185,12 +201,6 @@ template <class T>
constexpr bool is_char_array_v = std::is_array_v<T>
&&std::is_same_v<char, std::remove_pointer_t<std::decay_t<T>>>;

template <typename T>
struct is_optional_v : std::false_type {};

template <typename T>
struct is_optional_v<std::optional<T>> : std::true_type {};

template <size_t N>
inline constexpr size_t char_array_size(char (&)[N]) {
return N;
Expand Down
29 changes: 14 additions & 15 deletions tests/test_ormpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ TEST_CASE("orm_connect") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
REQUIRE(sqlite.disconnect());
#endif
}
Expand All @@ -280,7 +280,7 @@ TEST_CASE("orm_create_table") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
REQUIRE(sqlite.create_datatable<person>());
REQUIRE(sqlite.create_datatable<person>(key));
REQUIRE(sqlite.create_datatable<person>(not_null));
Expand Down Expand Up @@ -333,7 +333,7 @@ TEST_CASE("orm_insert_query") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
auto vv2 = sqlite.query(FID(simple::id), "<", "5");
#endif

Expand All @@ -348,8 +348,8 @@ TEST_CASE("orm_insert_query") {
#endif

#ifdef ORMPP_ENABLE_SQLITE3
CHECK(sqlite.delete_records<student>());
REQUIRE(sqlite.create_datatable<student>(auto_key));
CHECK(sqlite.delete_records<student>());
CHECK(sqlite.insert(s) == 1);
auto result3 = sqlite.query<student>();
CHECK(result3.size() == 1);
Expand All @@ -364,8 +364,8 @@ TEST_CASE("orm_insert_query") {
// key
{
#ifdef ORMPP_ENABLE_MYSQL
// CHECK(mysql.delete_records<student>());
REQUIRE(mysql.create_datatable<student>(auto_key, not_null));
CHECK(mysql.delete_records<student>());
CHECK(mysql.insert(s) == 1);
auto result1 = mysql.query<student>("limit 3");
CHECK(result1.size() == 1);
Expand All @@ -382,7 +382,6 @@ TEST_CASE("orm_insert_query") {
CHECK(mysql.insert(s) == 1);
auto result11 = mysql.query<student>();
CHECK(result11.size() == 5);
// CHECK(mysql.insert(s) < 0);
CHECK(mysql.delete_records<student>());
CHECK(mysql.insert(v) == 2);
auto result44 = mysql.query<student>();
Expand All @@ -403,7 +402,7 @@ TEST_CASE("orm_insert_query") {
#endif

#ifdef ORMPP_ENABLE_SQLITE3
REQUIRE(sqlite.create_datatable<student>(key));
REQUIRE(sqlite.create_datatable<student>(auto_key));
CHECK(sqlite.insert(s) == 1);
auto result3 = sqlite.query<student>();
CHECK(result3.size() == 4);
Expand Down Expand Up @@ -453,7 +452,7 @@ TEST_CASE("orm_update") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
CHECK(sqlite.delete_records<student>());
REQUIRE(sqlite.create_datatable<student>(key));
CHECK(sqlite.insert(v) == 3);
Expand Down Expand Up @@ -491,7 +490,7 @@ TEST_CASE("orm_multi_update") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
CHECK(sqlite.delete_records<student>());
REQUIRE(sqlite.create_datatable<student>(key));
CHECK(sqlite.insert(v) == 3);
Expand Down Expand Up @@ -548,7 +547,7 @@ TEST_CASE("orm_delete") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
REQUIRE(sqlite.create_datatable<student>(key));
sqlite.delete_records<student>();
CHECK(sqlite.insert(v) == 3);
Expand Down Expand Up @@ -581,9 +580,9 @@ TEST_CASE("orm_query") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
sqlite.delete_records<simple>();
REQUIRE(sqlite.connect(db));
REQUIRE(sqlite.create_datatable<simple>(key));
sqlite.delete_records<simple>();
CHECK(sqlite.insert(v) == 3);
auto result2 = sqlite.query<simple>();
CHECK(result2.size() == 3);
Expand Down Expand Up @@ -647,7 +646,7 @@ TEST_CASE("orm_query_some") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
CHECK(sqlite.insert(v) == 3);
REQUIRE(sqlite.create_datatable<student>(key));
auto result2 = sqlite.query<std::tuple<int, std::string, double>>(
Expand Down Expand Up @@ -707,7 +706,7 @@ TEST_CASE("orm_query_multi_table") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
sqlite.delete_records<student>();
sqlite.delete_records<person>();
REQUIRE(sqlite.create_datatable<student>(key));
Expand Down Expand Up @@ -771,7 +770,7 @@ TEST_CASE("orm_transaction") {

#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
REQUIRE(sqlite.connect("test.db"));
REQUIRE(sqlite.connect(db));
REQUIRE(sqlite.create_datatable<student>(key));
REQUIRE(sqlite.begin());
for (int i = 0; i < 10; ++i) {
Expand Down