mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-06 01:19:58 -06:00
Added support for JSON fields (#48)
This commit is contained in:
committed by
GitHub
parent
8b6b6c5a59
commit
26869c83f1
@@ -6,6 +6,7 @@
|
||||
#include "sqlgen/ForeignKey.hpp"
|
||||
#include "sqlgen/Iterator.hpp"
|
||||
#include "sqlgen/IteratorBase.hpp"
|
||||
#include "sqlgen/JSON.hpp"
|
||||
#include "sqlgen/Literal.hpp"
|
||||
#include "sqlgen/Pattern.hpp"
|
||||
#include "sqlgen/PrimaryKey.hpp"
|
||||
|
||||
109
include/sqlgen/JSON.hpp
Normal file
109
include/sqlgen/JSON.hpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#ifndef SQLGEN_JSON_HPP_
|
||||
#define SQLGEN_JSON_HPP_
|
||||
|
||||
#include <rfl/json.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
#include "transpilation/is_nullable.hpp"
|
||||
|
||||
namespace sqlgen {
|
||||
|
||||
template <class T>
|
||||
class JSON {
|
||||
public:
|
||||
using ReflectionType = T;
|
||||
|
||||
JSON() : value_(T()) {}
|
||||
|
||||
JSON(const T& _value) : value_(_value) {}
|
||||
|
||||
JSON(JSON&& _other) noexcept = default;
|
||||
|
||||
JSON(const JSON& _other) = default;
|
||||
|
||||
template <class U>
|
||||
JSON(const JSON<U>& _other) : value_(_other.get()) {}
|
||||
|
||||
template <class U>
|
||||
JSON(JSON<U>&& _other) : value_(_other.get()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
JSON(const U& _value) : value_(_value) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
JSON(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
JSON(const JSON<U>& _other) : value_(_other.value()) {}
|
||||
|
||||
~JSON() = default;
|
||||
|
||||
/// Returns the underlying object.
|
||||
T& get() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const T& get() const { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
T& operator()() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const T& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const T& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const U& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
JSON& operator=(const JSON& _other) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
JSON& operator=(JSON&& _other) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(const JSON<U>& _other) {
|
||||
value_ = _other.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(JSON<U>&& _other) {
|
||||
value_ = std::move(_other.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Necessary for the automated transpilation to work.
|
||||
const T& reflection() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(const T& _value) { value_ = _value; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
T& value() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const T& value() const { return value_; }
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
T value_;
|
||||
};
|
||||
|
||||
} // namespace sqlgen
|
||||
|
||||
#endif
|
||||
@@ -11,9 +11,10 @@ namespace sqlgen::dynamic {
|
||||
using Type =
|
||||
rfl::TaggedUnion<"type", types::Unknown, types::Boolean, types::Dynamic,
|
||||
types::Float32, types::Float64, types::Int8, types::Int16,
|
||||
types::Int32, types::Int64, types::UInt8, types::UInt16,
|
||||
types::UInt32, types::UInt64, types::Text, types::Date,
|
||||
types::Timestamp, types::TimestampWithTZ, types::VarChar>;
|
||||
types::Int32, types::Int64, types::JSON, types::UInt8,
|
||||
types::UInt16, types::UInt32, types::UInt64, types::Text,
|
||||
types::Date, types::Timestamp, types::TimestampWithTZ,
|
||||
types::VarChar>;
|
||||
|
||||
} // namespace sqlgen::dynamic
|
||||
|
||||
|
||||
@@ -58,6 +58,10 @@ struct Int64 {
|
||||
Properties properties;
|
||||
};
|
||||
|
||||
struct JSON {
|
||||
Properties properties;
|
||||
};
|
||||
|
||||
struct UInt8 {
|
||||
Properties properties;
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "Parser_base.hpp"
|
||||
#include "Parser_default.hpp"
|
||||
#include "Parser_foreign_key.hpp"
|
||||
#include "Parser_json.hpp"
|
||||
#include "Parser_optional.hpp"
|
||||
#include "Parser_primary_key.hpp"
|
||||
#include "Parser_shared_ptr.hpp"
|
||||
|
||||
34
include/sqlgen/parsing/Parser_json.hpp
Normal file
34
include/sqlgen/parsing/Parser_json.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef SQLGEN_PARSING_PARSER_JSON_HPP_
|
||||
#define SQLGEN_PARSING_PARSER_JSON_HPP_
|
||||
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../JSON.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../dynamic/Type.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace sqlgen::parsing {
|
||||
|
||||
template <class T>
|
||||
struct Parser<JSON<T>> {
|
||||
static Result<JSON<T>> read(const std::optional<std::string>& _str) noexcept {
|
||||
if (!_str) {
|
||||
return error("NULL value encounted: JSON value cannot be NULL.");
|
||||
}
|
||||
return rfl::json::read<T>(*_str).transform(
|
||||
[](auto&& _t) { return JSON<T>(std::move(_t)); });
|
||||
}
|
||||
|
||||
static std::optional<std::string> write(const JSON<T>& _j) noexcept {
|
||||
return rfl::json::write(_j.value());
|
||||
}
|
||||
|
||||
static dynamic::Type to_type() noexcept { return dynamic::types::JSON{}; }
|
||||
};
|
||||
|
||||
} // namespace sqlgen::parsing
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user