diff --git a/include/matador/sql/any_type.hpp b/include/matador/sql/any_type.hpp index d667149..e74069b 100644 --- a/include/matador/sql/any_type.hpp +++ b/include/matador/sql/any_type.hpp @@ -3,6 +3,8 @@ #include "matador/sql/placeholder.hpp" +#include "matador/utils/types.hpp" + #include #include @@ -15,6 +17,7 @@ using any_type = std::variant< bool, const char*, std::string, + utils::blob, placeholder, nullptr_t>; diff --git a/include/matador/sql/any_type_to_visitor.hpp b/include/matador/sql/any_type_to_visitor.hpp index c520026..925cd89 100644 --- a/include/matador/sql/any_type_to_visitor.hpp +++ b/include/matador/sql/any_type_to_visitor.hpp @@ -1,111 +1,14 @@ #ifndef QUERY_ANY_TYPE_TO_VISITOR_HPP #define QUERY_ANY_TYPE_TO_VISITOR_HPP -#include -#include -#include -#include +#include "matador/sql/convert.hpp" + #include -#include namespace matador::sql { struct placeholder; -template < typename DestType, typename SourceType > -void convert(DestType &dest, SourceType source, typename std::enable_if::value>::type* = nullptr) -{ - dest = source; -} - -template < typename DestType, typename SourceType > -void convert(DestType &dest, SourceType source, typename std::enable_if::value && std::is_arithmetic::value && !std::is_same::value>::type* = nullptr) -{ - dest = static_cast(source); -} - -template < typename DestType, typename SourceType > -void convert(DestType &dest, SourceType source, typename std::enable_if::value && std::is_arithmetic::value && !std::is_same::value>::type* = nullptr) -{ - dest = static_cast(source); -} - -void convert(std::string &dest, bool source); - -template < typename SourceType > -void convert(std::string &dest, SourceType source, typename std::enable_if::value && !std::is_same::value>::type* = nullptr) -{ - std::array buffer{}; - auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), source, 10); - if (ec == std::errc{}) { - dest.assign(buffer.data(), ptr); - } else { - throw std::logic_error("couldn't convert value to std::string"); - } -} - -template < typename SourceType > -void convert(std::string &dest, SourceType source, typename std::enable_if::value>::type* = nullptr) -{ - std::array buffer{}; - auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), source); - if (ec == std::errc{}) { - dest.assign(buffer.data(), ptr); - } else { - throw std::logic_error("couldn't convert value to std::string"); - } -} - -void convert(std::string &dest, const char* source); - -unsigned long long to_unsigned_long_long(const char *source); - -template < typename DestType > -void convert(DestType &dest, const std::string &source, typename std::enable_if::value && std::is_unsigned::value>::type* = nullptr) -{ - dest = to_unsigned_long_long(source.c_str()); -} - -template < typename DestType > -void convert(DestType &dest, const char *source, typename std::enable_if::value && std::is_unsigned::value>::type* = nullptr) -{ - dest = to_unsigned_long_long(source); -} - -long long to_long_long(const char *source); - -template < typename DestType > -void convert(DestType &dest, const std::string &source, typename std::enable_if::value && std::is_signed::value>::type* = nullptr) -{ - dest = to_long_long(source.c_str()); -} - -template < typename DestType > -void convert(DestType &dest, const char *source, typename std::enable_if::value && std::is_signed::value>::type* = nullptr) -{ - dest = to_long_long(source); -} - -long double to_double(const char *source); - -template < typename DestType > -void convert(DestType &dest, const std::string &source, typename std::enable_if::value>::type* = nullptr) -{ - dest = to_double(source.c_str()); -} - -template < typename DestType > -void convert(DestType &dest, const char *source, typename std::enable_if::value>::type* = nullptr) -{ - dest = to_double(source); -} - -template < typename DestType > -void convert(DestType &dest, bool source, typename std::enable_if::value>::type* = nullptr) -{ - dest = static_cast(source); -} - template < typename Type > struct any_type_to_visitor { @@ -124,6 +27,7 @@ struct any_type_to_visitor void operator()(double &x) { convert(result, x); } void operator()(const char *x) { convert(result, x); } void operator()(std::string &x) { convert(result, x); } + void operator()(utils::blob &x) { convert(result, x); } void operator()(placeholder &/*x*/) {} Type result{}; diff --git a/include/matador/sql/column.hpp b/include/matador/sql/column.hpp index d2d15ad..dfcbf29 100644 --- a/include/matador/sql/column.hpp +++ b/include/matador/sql/column.hpp @@ -3,7 +3,7 @@ #include "matador/sql/any_type.hpp" #include "matador/sql/any_type_to_visitor.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include "matador/utils/field_attributes.hpp" diff --git a/include/matador/sql/column_generator.hpp b/include/matador/sql/column_generator.hpp index 667cdc3..287b1e2 100644 --- a/include/matador/sql/column_generator.hpp +++ b/include/matador/sql/column_generator.hpp @@ -2,7 +2,7 @@ #define QUERY_COLUMN_GENERATOR_HPP #include "matador/sql/column.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include "matador/utils/access.hpp" #include "matador/utils/field_attributes.hpp" diff --git a/include/matador/sql/convert.hpp b/include/matador/sql/convert.hpp new file mode 100644 index 0000000..6756a7c --- /dev/null +++ b/include/matador/sql/convert.hpp @@ -0,0 +1,108 @@ +#ifndef QUERY_CONVERT_HPP +#define QUERY_CONVERT_HPP + +#include +#include +#include +#include +#include + +namespace matador::sql { + +template < typename DestType, typename SourceType > +void convert(DestType &dest, SourceType source, typename std::enable_if::value>::type* = nullptr) +{ + dest = source; +} + +template < typename DestType, typename SourceType > +void convert(DestType &dest, SourceType source, typename std::enable_if::value && std::is_arithmetic::value && !std::is_same::value>::type* = nullptr) +{ + dest = static_cast(source); +} + +template < typename DestType, typename SourceType > +void convert(DestType &dest, SourceType source, typename std::enable_if::value && std::is_arithmetic::value && !std::is_same::value>::type* = nullptr) +{ + dest = static_cast(source); +} + +void convert(std::string &dest, bool source); + +template < typename SourceType > +void convert(std::string &dest, SourceType source, typename std::enable_if::value && !std::is_same::value>::type* = nullptr) +{ + std::array buffer{}; + auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), source, 10); + if (ec == std::errc{}) { + dest.assign(buffer.data(), ptr); + } else { + throw std::logic_error("couldn't convert value to std::string"); + } +} + +template < typename SourceType > +void convert(std::string &dest, SourceType source, typename std::enable_if::value>::type* = nullptr) +{ + std::array buffer{}; + auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), source); + if (ec == std::errc{}) { + dest.assign(buffer.data(), ptr); + } else { + throw std::logic_error("couldn't convert value to std::string"); + } +} + +void convert(std::string &dest, const char* source); + +unsigned long long to_unsigned_long_long(const char *source); + +template < typename DestType > +void convert(DestType &dest, const std::string &source, typename std::enable_if::value && std::is_unsigned::value>::type* = nullptr) +{ + dest = to_unsigned_long_long(source.c_str()); +} + +template < typename DestType > +void convert(DestType &dest, const char *source, typename std::enable_if::value && std::is_unsigned::value>::type* = nullptr) +{ + dest = to_unsigned_long_long(source); +} + +long long to_long_long(const char *source); + +template < typename DestType > +void convert(DestType &dest, const std::string &source, typename std::enable_if::value && std::is_signed::value>::type* = nullptr) +{ + dest = to_long_long(source.c_str()); +} + +template < typename DestType > +void convert(DestType &dest, const char *source, typename std::enable_if::value && std::is_signed::value>::type* = nullptr) +{ + dest = to_long_long(source); +} + +long double to_double(const char *source); + +template < typename DestType > +void convert(DestType &dest, const std::string &source, typename std::enable_if::value>::type* = nullptr) +{ + dest = to_double(source.c_str()); +} + +template < typename DestType > +void convert(DestType &dest, const char *source, typename std::enable_if::value>::type* = nullptr) +{ + dest = to_double(source); +} + +template < typename DestType > +void convert(DestType &dest, bool source, typename std::enable_if::value>::type* = nullptr) +{ + dest = static_cast(source); +} + +} + +#endif //QUERY_CONVERT_HPP diff --git a/include/matador/sql/types.hpp b/include/matador/sql/data_type_traits.hpp similarity index 94% rename from include/matador/sql/types.hpp rename to include/matador/sql/data_type_traits.hpp index 56d5873..d18ef19 100644 --- a/include/matador/sql/types.hpp +++ b/include/matador/sql/data_type_traits.hpp @@ -1,8 +1,10 @@ -#ifndef QUERY_TYPES_HPP -#define QUERY_TYPES_HPP +#ifndef QUERY_DATA_TYPE_TRAITS_HPP +#define QUERY_DATA_TYPE_TRAITS_HPP #include "matador/sql/any_type.hpp" +#include "matador/utils/types.hpp" + #include #include @@ -205,6 +207,15 @@ template <> struct data_type_traits inline static any_type create_value(std::string &value) { return value; } }; +template <> struct data_type_traits +{ + inline static data_type_t builtin_type(std::size_t size) { return data_type_t::type_blob; } + static void read_value(query_result_reader &reader, const char *id, size_t index, utils::blob &value); + static void bind_value(parameter_binder &binder, size_t index, utils::blob &value); + static void bind_result_value(result_parameter_binder &binder, size_t index, utils::blob &value); + inline static any_type create_value(utils::blob &value) { return value; } +}; + //template <> struct data_type_traits //{ // inline static database_type_t type(std::size_t /*size*/) { return database_type_t::type_date; } @@ -242,4 +253,4 @@ struct data_type_traits /// @endcond } -#endif //QUERY_TYPES_HPP +#endif //QUERY_DATA_TYPE_TRAITS_HPP diff --git a/include/matador/sql/dialect.hpp b/include/matador/sql/dialect.hpp index 37d91e1..69917f7 100644 --- a/include/matador/sql/dialect.hpp +++ b/include/matador/sql/dialect.hpp @@ -1,7 +1,7 @@ #ifndef QUERY_DIALECT_HPP #define QUERY_DIALECT_HPP -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include #include @@ -26,11 +26,13 @@ public: SELECT, TABLE, VALUES, + INSERT_VALUES, COLUMNS, COLUMN, FROM, INTO, WHERE, + WHERE_CLAUSE, AND, OR, LIKE, @@ -43,6 +45,7 @@ public: OFFSET, DISTINCT, SET, + UPDATE_VALUES, NOT_NULL, PRIMARY_KEY, BEGIN, diff --git a/include/matador/sql/object_parameter_binder.hpp b/include/matador/sql/object_parameter_binder.hpp index d7b9879..8b704f3 100644 --- a/include/matador/sql/object_parameter_binder.hpp +++ b/include/matador/sql/object_parameter_binder.hpp @@ -2,7 +2,7 @@ #define QUERY_OBJECT_PARAMETER_BINDER_HPP #include "matador/sql/parameter_binder.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include "matador/utils/access.hpp" #include "matador/utils/cascade_type.hpp" diff --git a/include/matador/sql/parameter_binder.hpp b/include/matador/sql/parameter_binder.hpp index ff387a0..0e275f3 100644 --- a/include/matador/sql/parameter_binder.hpp +++ b/include/matador/sql/parameter_binder.hpp @@ -28,6 +28,7 @@ public: virtual void bind(size_t pos, const char *, size_t size) = 0; virtual void bind(size_t pos, const std::string&) = 0; virtual void bind(size_t pos, const std::string &x, size_t size) = 0; + virtual void bind(size_t pos, const utils::blob &) = 0; // virtual void bind(size_t pos, const matador::time&) = 0; // virtual void bind(size_t pos, const matador::date&) = 0; }; diff --git a/include/matador/sql/query_builder.hpp b/include/matador/sql/query_builder.hpp index e7f585b..2cb3610 100644 --- a/include/matador/sql/query_builder.hpp +++ b/include/matador/sql/query_builder.hpp @@ -3,6 +3,7 @@ #include "matador/sql/basic_condition.hpp" #include "matador/sql/column.hpp" +#include "matador/sql/dialect.hpp" #include "matador/sql/key_value_pair.hpp" #include "matador/sql/query_context.hpp" #include "matador/sql/record.hpp" @@ -10,12 +11,11 @@ #include #include #include +#include #include namespace matador::sql { -class dialect; - namespace detail { struct any_type_to_string_visitor { @@ -99,6 +99,15 @@ private: REMOVE /**< Remove query command */ }; + struct query_part + { + query_part(dialect::token_t t, std::string p) + : token(t), part(std::move(p)) {} + + dialect::token_t token; + std::string part; + }; + public: explicit query_builder(const dialect &d); @@ -142,7 +151,7 @@ private: command_t command_{command_t::UNKNOWN}; state_t state_{state_t::QUERY_INIT}; - std::vector query_parts_; + std::vector query_parts_; detail::any_type_to_string_visitor value_to_string_; diff --git a/include/matador/sql/query_result_impl.hpp b/include/matador/sql/query_result_impl.hpp index 7314578..8a1edb7 100644 --- a/include/matador/sql/query_result_impl.hpp +++ b/include/matador/sql/query_result_impl.hpp @@ -7,7 +7,7 @@ #include "matador/sql/any_type.hpp" #include "matador/sql/query_result_reader.hpp" #include "matador/sql/record.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include #include diff --git a/include/matador/sql/query_result_reader.hpp b/include/matador/sql/query_result_reader.hpp index 931e22b..915bfde 100644 --- a/include/matador/sql/query_result_reader.hpp +++ b/include/matador/sql/query_result_reader.hpp @@ -2,7 +2,7 @@ #define QUERY_QUERY_RESULT_READER_HPP #include "matador/sql/any_type.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" namespace matador::sql { @@ -33,6 +33,7 @@ public: virtual void read_value(const char *id, size_t index, char *value, size_t s); virtual void read_value(const char *id, size_t index, std::string &value); virtual void read_value(const char *id, size_t index, std::string &value, size_t s); + virtual void read_value(const char *id, size_t index, utils::blob &value); virtual void read_value(const char *id, size_t index, any_type &value, data_type_t type, size_t size); }; diff --git a/include/matador/sql/result_parameter_binder.hpp b/include/matador/sql/result_parameter_binder.hpp index 998654f..f88e5fc 100644 --- a/include/matador/sql/result_parameter_binder.hpp +++ b/include/matador/sql/result_parameter_binder.hpp @@ -6,7 +6,7 @@ #include "matador/utils/field_attributes.hpp" #include "matador/sql/any_type.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include diff --git a/include/matador/sql/statement_impl.hpp b/include/matador/sql/statement_impl.hpp index c56d191..bf1de73 100644 --- a/include/matador/sql/statement_impl.hpp +++ b/include/matador/sql/statement_impl.hpp @@ -4,7 +4,7 @@ #include "matador/sql/query_context.hpp" #include "matador/sql/query_result_impl.hpp" #include "matador/sql/parameter_binder.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include diff --git a/include/matador/sql/value_extractor.hpp b/include/matador/sql/value_extractor.hpp index 6562c30..82db268 100644 --- a/include/matador/sql/value_extractor.hpp +++ b/include/matador/sql/value_extractor.hpp @@ -2,7 +2,7 @@ #define QUERY_VALUE_EXTRACTOR_HPP #include "matador/sql/fk_value_extractor.hpp" -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include diff --git a/include/matador/utils/types.hpp b/include/matador/utils/types.hpp new file mode 100644 index 0000000..4a239a3 --- /dev/null +++ b/include/matador/utils/types.hpp @@ -0,0 +1,13 @@ +#ifndef QUERY_UTILS_TYPES_HPP +#define QUERY_UTILS_TYPES_HPP + +#include + +namespace matador::utils { + +using byte = unsigned char; +using blob = std::vector; + +} + +#endif //QUERY_UTILS_TYPES_HPP diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 00bc96f..ebb18aa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,7 +17,6 @@ set(SQL_SOURCES sql/key_value_generator.cpp sql/fk_value_extractor.cpp sql/table_repository.cpp - sql/any_type_to_visitor.cpp sql/query_result.cpp sql/query_result_reader.cpp sql/statement_cache.cpp @@ -25,15 +24,17 @@ set(SQL_SOURCES sql/dialect_builder.cpp sql/object_parameter_binder.cpp sql/placeholder_generator.cpp - sql/types.cpp + sql/data_type_traits.cpp sql/result_parameter_binder.cpp + sql/statement.cpp + sql/convert.cpp ) set(SQL_HEADER ../include/matador/sql/dialect.hpp ../include/matador/sql/query_builder.hpp ../include/matador/sql/column.hpp - ../include/matador/sql/types.hpp + ../include/matador/sql/data_type_traits.hpp ../include/matador/sql/key_value_pair.hpp ../include/matador/sql/basic_condition.hpp ../include/matador/sql/condition.hpp @@ -62,12 +63,12 @@ set(SQL_HEADER ../include/matador/sql/statement.hpp ../include/matador/sql/statement_impl.hpp ../include/matador/sql/query_context.hpp - sql/statement.cpp ../include/matador/sql/parameter_binder.hpp ../include/matador/sql/dialect_builder.hpp ../include/matador/sql/object_parameter_binder.hpp ../include/matador/sql/placeholder_generator.hpp ../include/matador/sql/result_parameter_binder.hpp + ../include/matador/sql/convert.hpp ) set(UTILS_HEADER @@ -80,7 +81,8 @@ set(UTILS_HEADER ../include/matador/utils/identifier.hpp ../include/matador/utils/cascade_type.hpp ../include/matador/utils/logger.hpp - ../include/matador/utils/enum_mapper.hpp) + ../include/matador/utils/enum_mapper.hpp + ../include/matador/utils/types.hpp) set(UTILS_SOURCES utils/field_attributes.cpp diff --git a/src/sql/any_type_to_visitor.cpp b/src/sql/convert.cpp similarity index 95% rename from src/sql/any_type_to_visitor.cpp rename to src/sql/convert.cpp index 9a79a23..d4997b6 100644 --- a/src/sql/any_type_to_visitor.cpp +++ b/src/sql/convert.cpp @@ -1,4 +1,6 @@ -#include "matador/sql/any_type_to_visitor.hpp" +#include "matador/sql/convert.hpp" + +#include namespace matador::sql { diff --git a/src/sql/types.cpp b/src/sql/data_type_traits.cpp similarity index 94% rename from src/sql/types.cpp rename to src/sql/data_type_traits.cpp index 4449864..69f134f 100644 --- a/src/sql/types.cpp +++ b/src/sql/data_type_traits.cpp @@ -1,4 +1,4 @@ -#include "matador/sql/types.hpp" +#include "matador/sql/data_type_traits.hpp" #include "matador/sql/parameter_binder.hpp" #include "matador/sql/query_result_reader.hpp" @@ -245,4 +245,19 @@ void data_type_traits::bind_result_value(result_parameter_bin { binder.bind_result_value(index, value, size); } + +void data_type_traits::read_value(query_result_reader &reader, const char *id, size_t index, utils::blob &value) +{ + +} + +void data_type_traits::bind_value(parameter_binder &binder, size_t index, utils::blob &value) +{ + +} + +void data_type_traits::bind_result_value(result_parameter_binder &binder, size_t index, utils::blob &value) +{ + +} } \ No newline at end of file diff --git a/src/sql/query_builder.cpp b/src/sql/query_builder.cpp index b6690bd..63837d3 100644 --- a/src/sql/query_builder.cpp +++ b/src/sql/query_builder.cpp @@ -94,7 +94,7 @@ query_builder &query_builder::create() { initialize(command_t::CREATE, state_t::QUERY_CREATE); - query_parts_.emplace_back(dialect_.token_at(dialect::token_t::CREATE)); + query_parts_.emplace_back(dialect::token_t::CREATE, dialect_.token_at(dialect::token_t::CREATE)); return *this; } @@ -103,7 +103,7 @@ query_builder &query_builder::drop() { initialize(command_t::DROP, state_t::QUERY_DROP); - query_parts_.emplace_back(dialect_.token_at(dialect::token_t::DROP)); + query_parts_.emplace_back(dialect::token_t::DROP, dialect_.token_at(dialect::token_t::DROP)); return *this; } @@ -117,7 +117,7 @@ query_builder &query_builder::select(const std::vector &columns) { initialize(command_t::SELECT, state_t::QUERY_SELECT); - query_parts_.emplace_back(dialect_.token_at(dialect::token_t::SELECT) + " "); + query_parts_.emplace_back(dialect::token_t::SELECT, dialect_.token_at(dialect::token_t::SELECT) + " "); query_.prototype.clear(); @@ -141,7 +141,7 @@ query_builder &query_builder::select(const std::vector &columns) } } - query_parts_.emplace_back(result); + query_parts_.emplace_back(dialect::token_t::COLUMNS, result); return *this; } @@ -149,7 +149,7 @@ query_builder &query_builder::insert() { initialize(command_t::INSERT, state_t::QUERY_INSERT); - query_parts_.emplace_back(dialect_.token_at(dialect::token_t::INSERT)); + query_parts_.emplace_back(dialect::token_t::INSERT, dialect_.token_at(dialect::token_t::INSERT)); return *this; } @@ -159,7 +159,7 @@ query_builder &query_builder::update(const std::string &table) initialize(command_t::UPDATE, state_t::QUERY_UPDATE); query_.table_name = table; - query_parts_.emplace_back(dialect_.token_at(dialect::token_t::UPDATE) + " " + dialect_.prepare_identifier(table)); + query_parts_.emplace_back(dialect::token_t::UPDATE, dialect_.token_at(dialect::token_t::UPDATE) + " " + dialect_.prepare_identifier(table)); return *this; } @@ -168,7 +168,7 @@ query_builder &query_builder::remove() { initialize(command_t::REMOVE, state_t::QUERY_DELETE); - query_parts_.emplace_back(dialect_.token_at(dialect::token_t::REMOVE)); + query_parts_.emplace_back(dialect::token_t::REMOVE, dialect_.token_at(dialect::token_t::REMOVE)); return *this; } @@ -197,7 +197,7 @@ query_builder &query_builder::table(const std::string &table, const std::vector< { transition_to(state_t::QUERY_TABLE_CREATE); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table) + " "); + query_parts_.emplace_back(dialect::token_t::TABLE, " " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table) + " "); query_.table_name = table; std::string result = "("; @@ -228,7 +228,7 @@ query_builder &query_builder::table(const std::string &table, const std::vector< } result += ")"; - query_parts_.emplace_back(result); + query_parts_.emplace_back(dialect::token_t::COLUMNS, result); return *this; } @@ -236,7 +236,7 @@ query_builder &query_builder::table(const std::string &table) { transition_to(state_t::QUERY_TABLE_DROP); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table)); + query_parts_.emplace_back(dialect::token_t::TABLE, " " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table)); query_.table_name = table; return *this; @@ -251,7 +251,7 @@ query_builder &query_builder::into(const std::string &table, const std::vector &values) { transition_to(state_t::QUERY_VALUES); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::VALUES) + " "); + query_parts_.emplace_back(dialect::token_t::VALUES, " " + dialect_.token_at(dialect::token_t::VALUES) + " "); std::string result{"("}; if (values.size() < 2) { @@ -307,7 +307,7 @@ query_builder &query_builder::values(const std::vector &values) } result += (")"); - query_parts_.emplace_back(result); + query_parts_.emplace_back(dialect::token_t::INSERT_VALUES, result); return *this; } @@ -316,11 +316,11 @@ query_builder &query_builder::from(const std::string &table, const std::string & transition_to(state_t::QUERY_FROM); if (dialect_.default_schema_name().empty()) { - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::FROM) + + query_parts_.emplace_back(dialect::token_t::FROM, " " + dialect_.token_at(dialect::token_t::FROM) + " " + dialect_.prepare_identifier(table) + (as.empty() ? "" : " " + as)); } else { - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::FROM) + + query_parts_.emplace_back(dialect::token_t::FROM, " " + dialect_.token_at(dialect::token_t::FROM) + " " + dialect_.prepare_identifier(dialect_.default_schema_name()) + "." + dialect_.prepare_identifier(table) + (as.empty() ? "" : " " + as)); @@ -349,7 +349,7 @@ query_builder &query_builder::set(const std::vector &key_values) { transition_to(state_t::QUERY_SET); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::SET) + " "); + query_parts_.emplace_back(dialect::token_t::SET, " " + dialect_.token_at(dialect::token_t::SET) + " "); std::string result; if (key_values.size() < 2) { @@ -374,7 +374,7 @@ query_builder &query_builder::set(const std::vector &key_values) } } - query_parts_.emplace_back(result); + query_parts_.emplace_back(dialect::token_t::UPDATE_VALUES, result); return *this; } @@ -382,8 +382,8 @@ query_builder &query_builder::where(const basic_condition &cond) { transition_to(state_t::QUERY_WHERE); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::WHERE) + " "); - query_parts_.emplace_back(cond.evaluate(const_cast(dialect_), query_)); + query_parts_.emplace_back(dialect::token_t::WHERE, " " + dialect_.token_at(dialect::token_t::WHERE) + " "); + query_parts_.emplace_back(dialect::token_t::WHERE_CLAUSE, cond.evaluate(const_cast(dialect_), query_)); return *this; } @@ -392,7 +392,7 @@ query_builder &query_builder::order_by(const std::string &column) { transition_to(state_t::QUERY_ORDER_BY); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::ORDER_BY) + " " + dialect_.prepare_identifier(column)); + query_parts_.emplace_back(dialect::token_t::ORDER_BY, " " + dialect_.token_at(dialect::token_t::ORDER_BY) + " " + dialect_.prepare_identifier(column)); return *this; } @@ -401,7 +401,7 @@ query_builder &query_builder::group_by(const std::string &column) { transition_to(state_t::QUERY_GROUP_BY); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::GROUP_BY) + " " + dialect_.prepare_identifier(column)); + query_parts_.emplace_back(dialect::token_t::GROUP_BY, " " + dialect_.token_at(dialect::token_t::GROUP_BY) + " " + dialect_.prepare_identifier(column)); return *this; } @@ -410,7 +410,7 @@ query_builder &query_builder::asc() { transition_to(state_t::QUERY_ORDER_DIRECTION); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::ASC)); + query_parts_.emplace_back(dialect::token_t::ASC, " " + dialect_.token_at(dialect::token_t::ASC)); return *this; } @@ -419,7 +419,7 @@ query_builder &query_builder::desc() { transition_to(state_t::QUERY_ORDER_DIRECTION); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::DESC)); + query_parts_.emplace_back(dialect::token_t::DESC, " " + dialect_.token_at(dialect::token_t::DESC)); return *this; } @@ -428,7 +428,7 @@ query_builder &query_builder::offset(size_t count) { transition_to(state_t::QUERY_OFFSET); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::OFFSET) + " " + std::to_string(count)); + query_parts_.emplace_back(dialect::token_t::OFFSET, " " + dialect_.token_at(dialect::token_t::OFFSET) + " " + std::to_string(count)); return *this; } @@ -437,7 +437,7 @@ query_builder &query_builder::limit(size_t count) { transition_to(state_t::QUERY_LIMIT); - query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::LIMIT) + " " + std::to_string(count)); + query_parts_.emplace_back(dialect::token_t::LIMIT, " " + dialect_.token_at(dialect::token_t::LIMIT) + " " + std::to_string(count)); return *this; } @@ -445,7 +445,7 @@ query_builder &query_builder::limit(size_t count) query_context query_builder::compile() { for (const auto &part: query_parts_) { - query_.sql.append(part); + query_.sql.append(part.part); } query_.command_name = command_strings_[command_]; return query_; diff --git a/src/sql/query_result_reader.cpp b/src/sql/query_result_reader.cpp index 507ef78..371c244 100644 --- a/src/sql/query_result_reader.cpp +++ b/src/sql/query_result_reader.cpp @@ -98,6 +98,11 @@ void query_result_reader::read_value(const char *id, size_t index, std::string & value.assign(column(index)); } +void query_result_reader::read_value(const char *id, size_t index, utils::blob &value) +{ + +} + template < typename Type > void convert(const char *valstr, sql::any_type &value) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 376a072..9844638 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -32,7 +32,9 @@ add_executable(tests QueryBuilderTest.cpp models/location.hpp TypeTraitsTest.cpp Databases.hpp - models/optional.hpp) + models/optional.hpp + ConvertTest.cpp) + target_link_libraries(tests PRIVATE Catch2::Catch2WithMain matador diff --git a/test/ConvertTest.cpp b/test/ConvertTest.cpp new file mode 100644 index 0000000..0b72330 --- /dev/null +++ b/test/ConvertTest.cpp @@ -0,0 +1,9 @@ +#include + +#include "matador/sql/convert.hpp" + +using namespace matador::sql; + +TEST_CASE("Test convert function", "[convert]") { + +} \ No newline at end of file diff --git a/test/models/person.hpp b/test/models/person.hpp index 613cb68..103e2e5 100644 --- a/test/models/person.hpp +++ b/test/models/person.hpp @@ -3,6 +3,7 @@ #include "matador/utils/access.hpp" #include "matador/utils/field_attributes.hpp" +#include "matador/utils/types.hpp" #include @@ -13,6 +14,7 @@ struct person unsigned long id{}; std::string name; unsigned int age{}; + utils::blob image; template void process(Operator &op) { @@ -21,6 +23,7 @@ struct person field::primary_key(op, "id", id); field::attribute(op, "name", name, 255); field::attribute(op, "age", age); + field::attribute(op, "image", image); } };