added blob type (progress)
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "matador/sql/placeholder.hpp"
|
||||
|
||||
#include "matador/utils/types.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
@@ -15,6 +17,7 @@ using any_type = std::variant<
|
||||
bool,
|
||||
const char*,
|
||||
std::string,
|
||||
utils::blob,
|
||||
placeholder,
|
||||
nullptr_t>;
|
||||
|
||||
|
||||
@@ -1,111 +1,14 @@
|
||||
#ifndef QUERY_ANY_TYPE_TO_VISITOR_HPP
|
||||
#define QUERY_ANY_TYPE_TO_VISITOR_HPP
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include "matador/sql/convert.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
struct placeholder;
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
void convert(DestType &dest, SourceType source, typename std::enable_if<std::is_same<DestType, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = source;
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
void convert(DestType &dest, SourceType source, typename std::enable_if<std::is_integral<DestType>::value && std::is_arithmetic<SourceType>::value && !std::is_same<DestType, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = static_cast<DestType>(source);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
void convert(DestType &dest, SourceType source, typename std::enable_if<std::is_floating_point<DestType>::value && std::is_arithmetic<SourceType>::value && !std::is_same<DestType, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = static_cast<DestType>(source);
|
||||
}
|
||||
|
||||
void convert(std::string &dest, bool source);
|
||||
|
||||
template < typename SourceType >
|
||||
void convert(std::string &dest, SourceType source, typename std::enable_if<std::is_integral<SourceType>::value && !std::is_same<bool, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
std::array<char, 128> 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<std::is_floating_point<SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
std::array<char, 128> 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<std::is_integral<DestType>::value && std::is_unsigned<DestType>::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<std::is_integral<DestType>::value && std::is_unsigned<DestType>::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<std::is_integral<DestType>::value && std::is_signed<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = to_long_long(source.c_str());
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
void convert(DestType &dest, const char *source, typename std::enable_if<std::is_integral<DestType>::value && std::is_signed<DestType>::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<std::is_floating_point<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = to_double(source.c_str());
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
void convert(DestType &dest, const char *source, typename std::enable_if<std::is_floating_point<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = to_double(source);
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
void convert(DestType &dest, bool source, typename std::enable_if<std::is_floating_point<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = static_cast<DestType>(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{};
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#ifndef QUERY_CONVERT_HPP
|
||||
#define QUERY_CONVERT_HPP
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
void convert(DestType &dest, SourceType source, typename std::enable_if<std::is_same<DestType, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = source;
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
void convert(DestType &dest, SourceType source, typename std::enable_if<std::is_integral<DestType>::value && std::is_arithmetic<SourceType>::value && !std::is_same<DestType, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = static_cast<DestType>(source);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
void convert(DestType &dest, SourceType source, typename std::enable_if<std::is_floating_point<DestType>::value && std::is_arithmetic<SourceType>::value && !std::is_same<DestType, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = static_cast<DestType>(source);
|
||||
}
|
||||
|
||||
void convert(std::string &dest, bool source);
|
||||
|
||||
template < typename SourceType >
|
||||
void convert(std::string &dest, SourceType source, typename std::enable_if<std::is_integral<SourceType>::value && !std::is_same<bool, SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
std::array<char, 128> 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<std::is_floating_point<SourceType>::value>::type* = nullptr)
|
||||
{
|
||||
std::array<char, 128> 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<std::is_integral<DestType>::value && std::is_unsigned<DestType>::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<std::is_integral<DestType>::value && std::is_unsigned<DestType>::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<std::is_integral<DestType>::value && std::is_signed<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = to_long_long(source.c_str());
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
void convert(DestType &dest, const char *source, typename std::enable_if<std::is_integral<DestType>::value && std::is_signed<DestType>::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<std::is_floating_point<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = to_double(source.c_str());
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
void convert(DestType &dest, const char *source, typename std::enable_if<std::is_floating_point<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = to_double(source);
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
void convert(DestType &dest, bool source, typename std::enable_if<std::is_floating_point<DestType>::value>::type* = nullptr)
|
||||
{
|
||||
dest = static_cast<DestType>(source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_CONVERT_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 <cstdint>
|
||||
#include <string>
|
||||
|
||||
@@ -205,6 +207,15 @@ template <> struct data_type_traits<std::string, void>
|
||||
inline static any_type create_value(std::string &value) { return value; }
|
||||
};
|
||||
|
||||
template <> struct data_type_traits<utils::blob, void>
|
||||
{
|
||||
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<matador::date>
|
||||
//{
|
||||
// inline static database_type_t type(std::size_t /*size*/) { return database_type_t::type_date; }
|
||||
@@ -242,4 +253,4 @@ struct data_type_traits<EnumType, typename std::enable_if<std::is_enum<EnumType>
|
||||
/// @endcond
|
||||
|
||||
}
|
||||
#endif //QUERY_TYPES_HPP
|
||||
#endif //QUERY_DATA_TYPE_TRAITS_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 <cstdint>
|
||||
#include <functional>
|
||||
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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 <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
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<std::string> query_parts_;
|
||||
std::vector<query_part> query_parts_;
|
||||
|
||||
detail::any_type_to_string_visitor value_to_string_;
|
||||
|
||||
|
||||
@@ -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 <memory>
|
||||
#include <string>
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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 <string>
|
||||
|
||||
|
||||
@@ -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 <memory>
|
||||
|
||||
|
||||
@@ -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 <vector>
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef QUERY_UTILS_TYPES_HPP
|
||||
#define QUERY_UTILS_TYPES_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
using byte = unsigned char;
|
||||
using blob = std::vector<byte>;
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_UTILS_TYPES_HPP
|
||||
Reference in New Issue
Block a user