progress on record fetch
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
#define QUERY_COLUMN_GENERATOR_HPP
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/table_repository.hpp"
|
||||
#include "matador/sql/types.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
@@ -13,6 +12,8 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class table_repository;
|
||||
|
||||
class fk_column_generator
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -29,17 +29,23 @@ public:
|
||||
[[nodiscard]] bool is_open() const;
|
||||
[[nodiscard]] const connection_info& info() const;
|
||||
|
||||
[[nodiscard]] record describe(const std::string &table_name) const;
|
||||
|
||||
template<class Type>
|
||||
query_result<Type> fetch(const std::string &sql)
|
||||
{
|
||||
return query_result<Type>(connection_->execute(sql));
|
||||
return query_result<Type>(connection_->fetch(sql));
|
||||
}
|
||||
query_result<record> fetch(const std::string &sql);
|
||||
std::pair<size_t, std::string> execute(const std::string &sql);
|
||||
[[nodiscard]] query_result<record> fetch(const std::string &sql) const;
|
||||
[[nodiscard]] std::pair<size_t, std::string> execute(const std::string &sql) const;
|
||||
|
||||
private:
|
||||
friend class session;
|
||||
|
||||
[[nodiscard]] std::unique_ptr<query_result_impl> call_fetch(const std::string &sql) const;
|
||||
|
||||
private:
|
||||
connection_info connection_info_;
|
||||
bool is_open_{false};
|
||||
std::unique_ptr<connection_impl> connection_;
|
||||
};
|
||||
|
||||
|
||||
@@ -54,6 +54,13 @@ enum class join_type_t {
|
||||
INNER, OUTER, LEFT, RIGHT
|
||||
};
|
||||
|
||||
struct query
|
||||
{
|
||||
std::string sql;
|
||||
record prototype;
|
||||
std::vector<any_type> host_vars;
|
||||
};
|
||||
|
||||
class query_builder
|
||||
{
|
||||
private:
|
||||
@@ -123,8 +130,6 @@ public:
|
||||
|
||||
std::string compile();
|
||||
|
||||
[[nodiscard]] const record& prototype() const;
|
||||
|
||||
private:
|
||||
void transition_to(state_t next);
|
||||
void initialize(command_t cmd, state_t state);
|
||||
@@ -139,7 +144,7 @@ private:
|
||||
|
||||
detail::any_type_to_string_visitor value_to_string_;
|
||||
|
||||
record prototype_;
|
||||
query query_;
|
||||
|
||||
using query_state_set = std::unordered_set<state_t>;
|
||||
using query_state_transition_map = std::unordered_map<state_t, query_state_set>;
|
||||
|
||||
@@ -47,6 +47,12 @@ protected:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
public:
|
||||
template < class Type >
|
||||
query_result<Type> fetch_all()
|
||||
{
|
||||
return query_result<Type>(fetch());
|
||||
}
|
||||
|
||||
query_result<record> fetch_all();
|
||||
record fetch_one();
|
||||
template<typename Type>
|
||||
@@ -55,6 +61,9 @@ public:
|
||||
auto result = fetch_all();
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<query_result_impl> fetch();
|
||||
};
|
||||
|
||||
class query_limit_intermediate : public query_select_finish
|
||||
@@ -124,7 +133,6 @@ public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
query_from_intermediate from(const std::string &table, const std::string &as = "");
|
||||
|
||||
};
|
||||
|
||||
class query_into_intermediate : public query_intermediate
|
||||
@@ -149,7 +157,7 @@ public:
|
||||
template<class Type>
|
||||
query_execute_finish table(const std::string &table_name)
|
||||
{
|
||||
const auto &info = repository_.attach<Type>(table_name, record{column_generator::generate<Type>(repository_)});
|
||||
const auto &info = repository_.attach<Type>(table_name/*, record{column_generator::generate<Type>(repository_)}*/);
|
||||
return {db(), query().table(table_name, info.prototype.columns())};
|
||||
}
|
||||
|
||||
|
||||
@@ -115,6 +115,7 @@ public:
|
||||
public:
|
||||
explicit query_result(std::unique_ptr<query_result_impl> impl)
|
||||
: impl_(std::move(impl)) {}
|
||||
|
||||
query_result(std::unique_ptr<query_result_impl> impl, creator_func creator)
|
||||
: creator_(creator)
|
||||
, impl_(std::move(impl)) {}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include "matador/sql/any_type.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
#include "matador/sql/types.hpp"
|
||||
|
||||
#include <string>
|
||||
@@ -16,6 +17,8 @@ class query_result_impl
|
||||
public:
|
||||
virtual ~query_result_impl() = default;
|
||||
|
||||
virtual size_t column_count() const = 0;
|
||||
|
||||
virtual void read_value(const char *id, size_t index, char &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, short &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, int &value) = 0;
|
||||
@@ -79,8 +82,14 @@ public:
|
||||
[[nodiscard]] virtual const char* column(size_t index) const = 0;
|
||||
[[nodiscard]] virtual bool fetch() = 0;
|
||||
|
||||
const record& prototype() const;
|
||||
|
||||
protected:
|
||||
explicit query_result_impl(record prototype);
|
||||
|
||||
protected:
|
||||
size_t column_index_ = 0;
|
||||
record prototype_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ public:
|
||||
query_update_intermediate update(const std::string &table);
|
||||
query_delete_intermediate remove();
|
||||
|
||||
query_result<record> fetch(const std::string &sql);
|
||||
std::pair<size_t, std::string> execute(const std::string &sql);
|
||||
[[nodiscard]] query_result<record> fetch(const std::string &sql) const;
|
||||
[[nodiscard]] std::pair<size_t, std::string> execute(const std::string &sql) const;
|
||||
|
||||
template<typename Type>
|
||||
void attach(const std::string &table_name)
|
||||
@@ -40,6 +40,11 @@ public:
|
||||
|
||||
[[nodiscard]] const table_repository& tables() const;
|
||||
|
||||
private:
|
||||
friend class query_select_finish;
|
||||
|
||||
[[nodiscard]] std::unique_ptr<query_result_impl> call_fetch(const std::string &sql) const;
|
||||
|
||||
private:
|
||||
connection_pool<connection> &pool_;
|
||||
query_builder query_;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef QUERY_TABLE_REPOSITORY_HPP
|
||||
#define QUERY_TABLE_REPOSITORY_HPP
|
||||
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include <optional>
|
||||
@@ -20,12 +21,11 @@ class table_repository
|
||||
{
|
||||
public:
|
||||
template<typename Type>
|
||||
const table_info& attach(const std::string &table_name, const record &proto)
|
||||
const table_info& attach(const std::string &table_name)
|
||||
{
|
||||
return attach(std::type_index(typeid(Type)), table_name, proto);
|
||||
return attach(std::type_index(typeid(Type)), table_info{table_name, record{column_generator::generate<Type>(*this)}});
|
||||
}
|
||||
|
||||
const table_info& attach(std::type_index ti, const std::string &table_name, const record &proto);
|
||||
const table_info& attach(std::type_index ti, const table_info& table);
|
||||
|
||||
template<typename Type>
|
||||
|
||||
Reference in New Issue
Block a user