entity query and record progress

This commit is contained in:
2024-03-04 20:09:39 +01:00
parent 830185c3c5
commit be610ffcad
31 changed files with 294 additions and 50 deletions
+11
View File
@@ -10,6 +10,17 @@
namespace matador::sql {
using any_db_type = std::variant<
long long,
unsigned long long,
double,
bool,
const char*,
std::string,
utils::blob,
placeholder,
nullptr_t>;
using any_type = std::variant<
char, short, int, long, long long,
unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long,
+3 -2
View File
@@ -9,7 +9,6 @@
#include "matador/sql/query_result.hpp"
#include "matador/sql/record.hpp"
#include "matador/sql/statement.hpp"
#include "matador/sql/schema.hpp"
#include "matador/utils/logger.hpp"
@@ -17,6 +16,8 @@
namespace matador::sql {
class schema;
class connection
{
public:
@@ -32,7 +33,7 @@ public:
[[nodiscard]] bool is_open() const;
[[nodiscard]] const connection_info& info() const;
[[nodiscard]] record describe(const std::string &table_name) const;
[[nodiscard]] std::vector<sql::column_definition> describe(const std::string &table_name) const;
[[nodiscard]] bool exists(const std::string &schema_name, const std::string &table_name) const;
[[nodiscard]] bool exists(const std::string &table_name) const;
+1 -1
View File
@@ -26,7 +26,7 @@ public:
virtual std::unique_ptr<query_result_impl> fetch(const std::string &stmt) = 0;
virtual std::unique_ptr<statement_impl> prepare(query_context context) = 0;
virtual record describe(const std::string &table) = 0;
virtual std::vector<sql::column_definition> describe(const std::string &table) = 0;
virtual bool exists(const std::string &schema_name, const std::string &table_name) = 0;
protected:
@@ -0,0 +1,62 @@
#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
#define QUERY_ENTITY_QUERY_BUILDER_HPP
#include "matador/sql/query_context.hpp"
namespace matador::sql {
template < typename PrimaryKeyType >
class entity_query_builder
{
public:
// determine pk
// collect eager relations for joins
template<class EntityType>
query_context build() {
EntityType obj;
matador::utils::access::process(*this, obj);
return {};
}
template < class V >
void on_primary_key(const char *id, V &, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type* = 0)
{
}
void on_primary_key(const char *id, std::string &, size_t)
{
}
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
template<typename Type>
void on_attribute(const char * /*id*/, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class Pointer>
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &/*attr*/)
{
}
template<class Pointer>
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
{
}
template<class ContainerType>
void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
{
}
template<class ContainerType>
void on_has_many(const char *id, ContainerType &c, const utils::foreign_attributes &attr)
{
on_has_many(id, c, "", "", attr);
}
private:
PrimaryKeyType pk_;
};
}
#endif //QUERY_ENTITY_QUERY_BUILDER_HPP
+37
View File
@@ -0,0 +1,37 @@
#ifndef QUERY_FIELD_HPP
#define QUERY_FIELD_HPP
#include "matador/sql/any_type.hpp"
#include "matador/sql/any_type_to_visitor.hpp"
#include <string>
namespace matador::sql {
class field
{
public:
[[nodiscard]] const std::string& name() const;
template<class Type>
Type as() const
{
const Type* ptr= std::get_if<Type>(&value_);
if (ptr) {
return *ptr;
}
any_type_to_visitor<Type> visitor;
std::visit(visitor, const_cast<any_type&>(value_));
return visitor.result;
}
friend std::ostream& operator<<(std::ostream &out, const field &col);
private:
std::string name_;
any_type value_;
};
}
#endif //QUERY_FIELD_HPP
+1 -1
View File
@@ -16,7 +16,7 @@ public:
size_t execute(const std::string &stmt) override;
std::unique_ptr<query_result_impl> fetch(const std::string &stmt) override;
std::unique_ptr<statement_impl> prepare(query_context context) override;
record describe(const std::string &table) override;
std::vector<sql::column_definition> describe(const std::string &table) override;
bool exists(const std::string &schema_name, const std::string &table_name) override;
private:
+1 -1
View File
@@ -11,7 +11,7 @@ struct query_context
std::string sql;
std::string command_name;
sql::table table{""};
record prototype;
std::vector<column_definition> prototype;
std::vector<std::string> result_vars;
std::vector<std::string> bind_vars;
};
+4 -4
View File
@@ -107,13 +107,13 @@ private:
namespace detail {
template < typename Type >
Type* create_prototype(const record &/*prototype*/)
Type* create_prototype(const std::vector<column_definition> &/*prototype*/)
{
return new Type{};
}
template <>
record* create_prototype<record>(const record &prototype);
record* create_prototype<record>(const std::vector<column_definition> &prototype);
}
@@ -128,7 +128,7 @@ public:
explicit query_result(std::unique_ptr<query_result_impl> impl)
: impl_(std::move(impl)) {}
query_result(std::unique_ptr<query_result_impl> impl, record record_prototype)
query_result(std::unique_ptr<query_result_impl> impl, std::vector<column_definition> record_prototype)
: record_prototype_(std::move(record_prototype))
, impl_(std::move(impl)) {}
@@ -151,7 +151,7 @@ private:
}
private:
record record_prototype_;
std::vector<column_definition> record_prototype_;
std::unique_ptr<query_result_impl> impl_;
};
+3 -3
View File
@@ -55,7 +55,7 @@ private:
class query_result_impl
{
public:
query_result_impl(std::unique_ptr<query_result_reader> &&reader, record prototype);
query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<column_definition> prototype);
template<typename ValueType>
void on_primary_key(const char *id, ValueType &value, typename std::enable_if<std::is_integral<ValueType>::value && !std::is_same<bool, ValueType>::value>::type* = 0)
@@ -110,11 +110,11 @@ public:
return true;
}
[[nodiscard]] const record& prototype() const;
[[nodiscard]] const std::vector<column_definition>& prototype() const;
protected:
size_t column_index_ = 0;
record prototype_;
std::vector<column_definition> prototype_;
std::unique_ptr<query_result_reader> reader_;
detail::pk_reader pk_reader_;
};
+23 -1
View File
@@ -31,6 +31,28 @@ public:
return insert(new Type(std::forward<Args>(args)...));
}
template<typename Type, typename PrimaryKeyType>
entity<Type> find(const PrimaryKeyType &pk) {
auto c = pool_.acquire();
if (!c.valid()) {
throw std::logic_error("no database connection available");
}
// collect all columns
// - evaluate fetch::Eager flag for relations
// build pk where condition
// - check if type has pk
// - check type
// pk_condition_builder<Type> builder;
// auto cond = builder.build(pk);
// create query with relations as requested
//
// c->query(*schema_).select<Type>().from("xyz").where().fetch_all();
return {};
}
template<typename Type>
void drop_table();
void drop_table(const std::string &table_name);
@@ -40,7 +62,7 @@ public:
[[nodiscard]] size_t execute(const std::string &sql) const;
statement prepare(query_context q) const;
record describe_table(const std::string &table_name) const;
std::vector<sql::column_definition> describe_table(const std::string &table_name) const;
bool table_exists(const std::string &table_name) const;
[[nodiscard]] const schema& tables() const;