added value class
This commit is contained in:
@@ -37,6 +37,8 @@ set(SQL_SOURCES
|
||||
sql/any_type_to_string_visitor.cpp
|
||||
sql/field.cpp
|
||||
sql/table_definition.cpp
|
||||
sql/value.cpp
|
||||
sql/entity_query_builder.cpp
|
||||
)
|
||||
|
||||
set(SQL_HEADER
|
||||
@@ -92,6 +94,7 @@ set(SQL_HEADER
|
||||
../include/matador/sql/entity_query_builder.hpp
|
||||
../include/matador/sql/table_definition.hpp
|
||||
../include/matador/sql/has_many_to_many_relation.hpp
|
||||
../include/matador/sql/value.hpp
|
||||
)
|
||||
|
||||
set(UTILS_HEADER
|
||||
|
||||
@@ -110,6 +110,10 @@ void column_definition::type(data_type_t type)
|
||||
|
||||
std::string column_definition::str() const
|
||||
{
|
||||
if (std::holds_alternative<std::string>(value_)) {
|
||||
return std::get<std::string>(value_);
|
||||
}
|
||||
|
||||
any_type_to_visitor<std::string> visitor;
|
||||
std::visit(visitor, const_cast<any_type &>(value_));
|
||||
return visitor.result;
|
||||
|
||||
@@ -6,6 +6,21 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
void data_type_traits<nullptr_t>::read_value(query_result_reader &reader, const char *id, size_t index, nullptr_t &/*value*/)
|
||||
{
|
||||
// reader.read_value(id, index, value);
|
||||
}
|
||||
|
||||
void data_type_traits<nullptr_t>::bind_value(parameter_binder &binder, size_t index, nullptr_t &/*value*/)
|
||||
{
|
||||
// binder.bind(index, value);
|
||||
}
|
||||
|
||||
void data_type_traits<nullptr_t>::bind_result_value(result_parameter_binder &binder, size_t index, nullptr_t &/*value*/)
|
||||
{
|
||||
// binder.bind_result_value(index, value);
|
||||
}
|
||||
|
||||
void data_type_traits<char>::read_value(query_result_reader &reader, const char *id, size_t index, char &value)
|
||||
{
|
||||
reader.read_value(id, index, value);
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ std::string dialect::prepare_identifier_string(const std::string &col) const
|
||||
|
||||
for (auto &part : parts) {
|
||||
escape_quotes_in_identifier(part);
|
||||
// quote_identifier(part);
|
||||
quote_identifier(part);
|
||||
}
|
||||
return utils::join(parts, ".");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "matador/sql/entity_query_builder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
void entity_query_builder::on_primary_key(const char *id, std::string &, size_t)
|
||||
{
|
||||
push(id);
|
||||
if (!is_root_entity()) {
|
||||
auto b = pk_.is_varchar();
|
||||
std::cout << "is matching primary key: " << std::boolalpha << b << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void entity_query_builder::on_revision(const char *id, unsigned long long &/*rev*/)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
|
||||
void entity_query_builder::push(const std::string &column_name)
|
||||
{
|
||||
char str[4];
|
||||
snprintf(str, 4, "c%02d", ++column_index);
|
||||
entity_query_data_.columns.emplace_back(table_info_stack_.top().name, column_name, str);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool entity_query_builder::is_root_entity() const {
|
||||
return table_info_stack_.size() == 1;
|
||||
}
|
||||
|
||||
void entity_query_builder::append_join(const column &left, const column &right)
|
||||
{
|
||||
entity_query_data_.joins.push_back({
|
||||
{ left.table },
|
||||
make_condition(left == right)
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+10
-16
@@ -7,24 +7,20 @@ namespace matador::sql {
|
||||
field::field(std::string name)
|
||||
: name_(std::move(name))
|
||||
, value_(nullptr)
|
||||
, type_(data_type_t::type_unknown)
|
||||
{}
|
||||
|
||||
field::field(std::string name, data_type_t data_type, size_t size, int index)
|
||||
: name_(std::move(name))
|
||||
, size_(size)
|
||||
, index_(index)
|
||||
, type_(data_type) {}
|
||||
, value_(data_type, size) {}
|
||||
|
||||
field::field(field &&x) noexcept
|
||||
: name_(std::move(x.name_))
|
||||
, index_(x.index_)
|
||||
, value_(std::move(x.value_))
|
||||
, type_(x.type_)
|
||||
{
|
||||
x.value_ = nullptr;
|
||||
x.index_ = -1;
|
||||
x.type_ = data_type_t::type_unknown;
|
||||
}
|
||||
|
||||
field &field::operator=(field &&x) noexcept
|
||||
@@ -32,10 +28,8 @@ field &field::operator=(field &&x) noexcept
|
||||
name_ = std::move(x.name_);
|
||||
index_ = x.index_;
|
||||
value_ = std::move(x.value_);
|
||||
type_ = x.type_;
|
||||
x.index_ = -1;
|
||||
x.value_ = nullptr;
|
||||
x.type_ = data_type_t::type_unknown;
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -47,7 +41,7 @@ const std::string &field::name() const
|
||||
|
||||
size_t field::size() const
|
||||
{
|
||||
return size_;
|
||||
return value_.size();
|
||||
}
|
||||
|
||||
int field::index() const
|
||||
@@ -68,42 +62,42 @@ std::string field::str() const
|
||||
|
||||
bool field::is_integer() const
|
||||
{
|
||||
return type_ >= data_type_t::type_char && type_ <= data_type_t::type_unsigned_long_long;
|
||||
return value_.is_integer();
|
||||
}
|
||||
|
||||
bool field::is_floating_point() const
|
||||
{
|
||||
return type_ == data_type_t::type_float || type_ == data_type_t::type_double;
|
||||
return value_.is_floating_point();
|
||||
}
|
||||
|
||||
bool field::is_bool() const
|
||||
{
|
||||
return type_ == data_type_t::type_bool;
|
||||
return value_.is_bool();
|
||||
}
|
||||
|
||||
bool field::is_string() const
|
||||
{
|
||||
return type_ == data_type_t::type_text;
|
||||
return value_.is_string();
|
||||
}
|
||||
|
||||
bool field::is_varchar() const
|
||||
{
|
||||
return type_ == data_type_t::type_varchar;
|
||||
return value_.is_varchar();
|
||||
}
|
||||
|
||||
bool field::is_blob() const
|
||||
{
|
||||
return type_ == data_type_t::type_blob;
|
||||
return value_.is_blob();
|
||||
}
|
||||
|
||||
bool field::is_null() const
|
||||
{
|
||||
return type_ == data_type_t::type_null;
|
||||
return value_.is_null();
|
||||
}
|
||||
|
||||
bool field::is_unknown() const
|
||||
{
|
||||
return type_ == data_type_t::type_unknown;
|
||||
return value_.is_unknown();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "matador/sql/query_result_impl.hpp"
|
||||
#include "matador/sql/query_result_reader.hpp"
|
||||
#include "matador/sql/value.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
@@ -39,9 +40,9 @@ void query_result_impl::on_attribute(const char *id, std::string &value, const u
|
||||
}
|
||||
|
||||
void
|
||||
query_result_impl::on_attribute(const char *id, any_type &value, data_type_t type, const utils::field_attributes &attr)
|
||||
query_result_impl::on_attribute(const char *id, value &val, const utils::field_attributes &attr)
|
||||
{
|
||||
reader_->read_value(id, column_index_++, value, type, attr.size());
|
||||
reader_->read_value(id, column_index_++, val, attr.size());
|
||||
}
|
||||
|
||||
const std::vector<column_definition>& query_result_impl::prototype() const
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "matador/sql/query_result_reader.hpp"
|
||||
#include "matador/sql/to_value.hpp"
|
||||
#include "matador/sql/value.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
@@ -104,82 +105,82 @@ void query_result_reader::read_value(const char *id, size_t index, utils::blob &
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
void convert(const char *valstr, sql::any_type &value)
|
||||
void convert(const char *valstr, value &val)
|
||||
{
|
||||
Type val{};
|
||||
sql::to_value(val, valstr);
|
||||
value = val;
|
||||
Type local_val{};
|
||||
sql::to_value(local_val, valstr);
|
||||
val = local_val;
|
||||
}
|
||||
|
||||
void query_result_reader::read_value(const char *id, size_t index, any_type &value, data_type_t type, size_t size)
|
||||
void query_result_reader::read_value(const char *id, size_t index, value &val, size_t size)
|
||||
{
|
||||
switch (type) {
|
||||
switch (val.type()) {
|
||||
case sql::data_type_t::type_char:
|
||||
convert<char>(column(index), value);
|
||||
convert<char>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_short:
|
||||
convert<short>(column(index), value);
|
||||
convert<short>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_int:
|
||||
convert<int>(column(index), value);
|
||||
convert<int>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_long:
|
||||
convert<long>(column(index), value);
|
||||
convert<long>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_long_long:
|
||||
convert<long long>(column(index), value);
|
||||
convert<long long>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_unsigned_char:
|
||||
convert<unsigned char>(column(index), value);
|
||||
convert<unsigned char>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_unsigned_short:
|
||||
convert<unsigned short>(column(index), value);
|
||||
convert<unsigned short>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_unsigned_int:
|
||||
convert<unsigned int>(column(index), value);
|
||||
convert<unsigned int>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_unsigned_long:
|
||||
convert<unsigned long>(column(index), value);
|
||||
convert<unsigned long>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_unsigned_long_long:
|
||||
convert<unsigned long long>(column(index), value);
|
||||
convert<unsigned long long>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_float:
|
||||
convert<float>(column(index), value);
|
||||
convert<float>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_double:
|
||||
convert<double>(column(index), value);
|
||||
convert<double>(column(index), val);
|
||||
break;
|
||||
case sql::data_type_t::type_bool: {
|
||||
int val{};
|
||||
sql::to_value(val, column(index));
|
||||
value = val > 0;
|
||||
int local_val{};
|
||||
sql::to_value(local_val, column(index));
|
||||
val = local_val > 0;
|
||||
break;
|
||||
}
|
||||
case sql::data_type_t::type_text:
|
||||
case sql::data_type_t::type_varchar: {
|
||||
value = std::string{column(index)};
|
||||
val = std::string{column(index)};
|
||||
break;
|
||||
}
|
||||
case sql::data_type_t::type_char_pointer: {
|
||||
value = column(index);
|
||||
val = column(index);
|
||||
break;
|
||||
}
|
||||
case sql::data_type_t::type_time:
|
||||
case sql::data_type_t::type_date: {
|
||||
value = std::string{column(index)};
|
||||
val = std::string{column(index)};
|
||||
break;
|
||||
}
|
||||
case sql::data_type_t::type_null: {
|
||||
value = nullptr_t{};
|
||||
val = nullptr_t{};
|
||||
break;
|
||||
}
|
||||
case sql::data_type_t::type_blob: {
|
||||
value = utils::blob{};
|
||||
val = utils::blob{};
|
||||
break;
|
||||
}
|
||||
case sql::data_type_t::type_unknown: {
|
||||
value = std::string(column(index));
|
||||
val = std::string(column(index));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "matador/sql/value.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
namespace detail {
|
||||
|
||||
size_t determine_size(const std::string &val)
|
||||
{
|
||||
return val.size();
|
||||
}
|
||||
|
||||
size_t determine_size(const char *val)
|
||||
{
|
||||
return strlen(val);
|
||||
}
|
||||
|
||||
size_t determine_size(const utils::blob &val)
|
||||
{
|
||||
return val.size();
|
||||
}
|
||||
|
||||
}
|
||||
value::value(data_type_t data_type, size_t size)
|
||||
: size_(size)
|
||||
, type_(data_type) {}
|
||||
|
||||
value::value(value &&x) noexcept
|
||||
: value_(std::move(x.value_))
|
||||
, type_(x.type_)
|
||||
{
|
||||
x.value_ = nullptr;
|
||||
x.type_ = data_type_t::type_unknown;
|
||||
}
|
||||
|
||||
value &value::operator=(value &&x) noexcept
|
||||
{
|
||||
value_ = std::move(x.value_);
|
||||
type_ = x.type_;
|
||||
x.value_ = nullptr;
|
||||
x.type_ = data_type_t::type_unknown;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string value::str() const
|
||||
{
|
||||
return as<std::string>().value();
|
||||
}
|
||||
|
||||
size_t value::size() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
data_type_t value::type() const
|
||||
{
|
||||
return type_;
|
||||
}
|
||||
|
||||
bool value::is_integer() const
|
||||
{
|
||||
return type_ >= data_type_t::type_char && type_ <= data_type_t::type_unsigned_long_long;
|
||||
}
|
||||
|
||||
bool value::is_floating_point() const
|
||||
{
|
||||
return type_ == data_type_t::type_float || type_ == data_type_t::type_double;
|
||||
}
|
||||
|
||||
bool value::is_bool() const
|
||||
{
|
||||
return type_ == data_type_t::type_bool;
|
||||
}
|
||||
|
||||
bool value::is_string() const
|
||||
{
|
||||
return type_ == data_type_t::type_text;
|
||||
}
|
||||
|
||||
bool value::is_varchar() const
|
||||
{
|
||||
return type_ == data_type_t::type_varchar || type_ == data_type_t::type_char_pointer;
|
||||
}
|
||||
|
||||
bool value::is_blob() const
|
||||
{
|
||||
return type_ == data_type_t::type_blob;
|
||||
}
|
||||
|
||||
bool value::is_null() const
|
||||
{
|
||||
return type_ == data_type_t::type_null;
|
||||
}
|
||||
|
||||
bool value::is_unknown() const
|
||||
{
|
||||
return type_ == data_type_t::type_unknown;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user