Skip to content

Commit

Permalink
add suport for types: bool, short, unsigned short
Browse files Browse the repository at this point in the history
  • Loading branch information
wsekta committed Jan 31, 2024
1 parent 4d2051d commit abc8fad
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 22 deletions.
3 changes: 0 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
* DB2
* Add update command
* Add support for other types:
* bool
* short (int16_t)
* unsigned short (uint16_t)
* long (int32_t)
* unsigned long (uint32_t)
* std::tm
Expand Down
39 changes: 39 additions & 0 deletions include/orm-cxx/database/binding/ObjectFieldFromValues.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ struct ObjectFieldFromValues<float>
}
};

template <>
struct ObjectFieldFromValues<bool>
{
template <typename T>
static auto get(bool* column, const BindingPayload<T>& model, std::size_t columnIndex, const soci::values& values)
-> void
{
auto fieldNames =
std::format("{}_{}", model.getModelInfo().tableName, model.getModelInfo().columnsInfo[columnIndex].name);
*column = static_cast<bool>(values.get<int>(fieldNames));
}
};

template <>
struct ObjectFieldFromValues<char>
{
Expand All @@ -111,4 +124,30 @@ struct ObjectFieldFromValues<unsigned char>
*column = static_cast<unsigned char>(values.get<int>(fieldNames));
}
};

template <>
struct ObjectFieldFromValues<short>
{
template <typename T>
static auto get(short* column, const BindingPayload<T>& model, std::size_t columnIndex, const soci::values& values)
-> void
{
auto fieldNames =
std::format("{}_{}", model.getModelInfo().tableName, model.getModelInfo().columnsInfo[columnIndex].name);
*column = static_cast<short>(values.get<int>(fieldNames));
}
};

template <>
struct ObjectFieldFromValues<unsigned short>
{
template <typename T>
static auto get(unsigned short* column, const BindingPayload<T>& model, std::size_t columnIndex,
const soci::values& values) -> void
{
auto fieldNames =
std::format("{}_{}", model.getModelInfo().tableName, model.getModelInfo().columnsInfo[columnIndex].name);
*column = static_cast<unsigned short>(values.get<int>(fieldNames));
}
};
} // namespace orm::db::binding
33 changes: 33 additions & 0 deletions include/orm-cxx/database/binding/ObjectFieldToValues.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ struct ObjectFieldToValues<float>
}
};

template <>
struct ObjectFieldToValues<bool>
{
template <typename T>
static auto set(const bool* column, const BindingPayload<T>& model, std::size_t columnIndex, soci::values& values)
-> void
{
values.set(model.getModelInfo().columnsInfo[columnIndex].name, static_cast<int>(*column));
}
};

template <>
struct ObjectFieldToValues<char>
{
Expand All @@ -80,6 +91,28 @@ struct ObjectFieldToValues<unsigned char>
}
};

template <>
struct ObjectFieldToValues<short>
{
template <typename T>
static auto set(const short* column, const BindingPayload<T>& model, std::size_t columnIndex, soci::values& values)
-> void
{
values.set(model.getModelInfo().columnsInfo[columnIndex].name, static_cast<int>(*column));
}
};

template <>
struct ObjectFieldToValues<unsigned short>
{
template <typename T>
static auto set(const unsigned short* column, const BindingPayload<T>& model, std::size_t columnIndex,
soci::values& values) -> void
{
values.set(model.getModelInfo().columnsInfo[columnIndex].name, static_cast<int>(*column));
}
};

template <typename ModelField>
struct ObjectFieldToValues<std::optional<ModelField>>
{
Expand Down
13 changes: 6 additions & 7 deletions src/model/ColumnType.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "orm-cxx/model/ColumnType.hpp"

#include <iostream>
#include <regex>
#include <unordered_map>
#include <iostream>

namespace
{
Expand Down Expand Up @@ -54,27 +54,26 @@ auto toColumnType(const std::string& type) -> std::pair<ColumnType, bool>
{
columnType = ColumnType::UnsignedChar;
}
else if (typeString == "short")
else if (typeString == "short" or typeString == "short int")
{
columnType = ColumnType::Short;
}
else if (typeString == "unsigned short")
else if (typeString == "unsigned short" or typeString == "short unsigned int")
{
columnType = ColumnType::UnsignedShort;
}
else if (typeString == "long long" or typeString == "long long int" or typeString == "__int64")
{
columnType = ColumnType::LongLong;
}
else if (typeString == "unsigned long long"
or typeString == "long long unsigned int"
or typeString == "unsigned __int64")
else if (typeString == "unsigned long long" or typeString == "long long unsigned int" or
typeString == "unsigned __int64")
{
columnType = ColumnType::UnsignedLongLong;
}
else if (typeString == "bool")
{
columnType = ColumnType::Bool;
columnType = ColumnType::Bool;
}
else if (typeString == "int" or typeString == "long")
{
Expand Down
6 changes: 3 additions & 3 deletions tests/ModelsDefinitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ struct ModelWithAllBasicTypes
inline static const std::string table_name = "all_types";

int id;
// bool field1;
bool field1;
char field2;
unsigned char field3;
// short field4;
// unsigned short field5;
short field4;
unsigned short field5;
int field6;
// unsigned int field7;
// long field8;
Expand Down
13 changes: 4 additions & 9 deletions tests/database/SimpleModelTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,7 @@ TEST_P(SimpleModelTest, shouldThrowWhileReadingNullValueToNotNullableField)
TEST_P(SimpleModelTest, shouldExecuteInsertQueryAndSelectQueryWithModelWithAllBasicTypes)
{
createTable<models::ModelWithAllBasicTypes>();
models::ModelWithAllBasicTypes model{1,
// true,
'2', 3,
// 4,
// 5,
6,
models::ModelWithAllBasicTypes model{1, true, '2', 3, 4, 5, 6,
// 7,
// 8,
// 9,
Expand All @@ -131,11 +126,11 @@ TEST_P(SimpleModelTest, shouldExecuteInsertQueryAndSelectQueryWithModelWithAllBa
orm::Query<models::ModelWithAllBasicTypes> queryForOptional;
auto returnedModels = database.select(queryForOptional);

// EXPECT_EQ(model.field1, returnedModels[0].field1);
EXPECT_EQ(model.field1, returnedModels[0].field1);
EXPECT_EQ(model.field2, returnedModels[0].field2);
EXPECT_EQ(model.field3, returnedModels[0].field3);
// EXPECT_EQ(model.field4, returnedModels[0].field4);
// EXPECT_EQ(model.field5, returnedModels[0].field5);
EXPECT_EQ(model.field4, returnedModels[0].field4);
EXPECT_EQ(model.field5, returnedModels[0].field5);
EXPECT_EQ(model.field6, returnedModels[0].field6);
// EXPECT_EQ(model.field7, returnedModels[0].field7);
// EXPECT_EQ(model.field8, returnedModels[0].field8);
Expand Down

0 comments on commit abc8fad

Please sign in to comment.