added sql function and alias
This commit is contained in:
@@ -12,15 +12,11 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
namespace detail {
|
||||
|
||||
|
||||
}
|
||||
class column {
|
||||
public:
|
||||
explicit column(std::string name)
|
||||
: name_(std::move(name))
|
||||
, attributes_(utils::null_attributes) {}
|
||||
column(sql_function_t func, std::string name);
|
||||
column(const char *name, std::string alias = ""); // NOLINT(*-explicit-constructor)
|
||||
column(std::string name, std::string alias = ""); // NOLINT(*-explicit-constructor)
|
||||
|
||||
column(const column&) = default;
|
||||
column& operator=(const column&) = default;
|
||||
@@ -50,9 +46,12 @@ public:
|
||||
[[nodiscard]] size_t index() const;
|
||||
[[nodiscard]] const utils::field_attributes& attributes() const;
|
||||
[[nodiscard]] data_type_t type() const;
|
||||
[[nodiscard]] const std::string& alias() const;
|
||||
[[nodiscard]] const std::string& ref_table() const;
|
||||
[[nodiscard]] const std::string& ref_column() const;
|
||||
|
||||
void alias(const std::string &as);
|
||||
|
||||
template< typename Type >
|
||||
[[nodiscard]] bool is_type_of() const {
|
||||
return std::holds_alternative<Type>(value_);
|
||||
@@ -94,6 +93,9 @@ public:
|
||||
return visitor.result;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_function() const;
|
||||
[[nodiscard]] sql_function_t function() const;
|
||||
|
||||
private:
|
||||
template<class Operator>
|
||||
void process(Operator &op)
|
||||
@@ -113,6 +115,8 @@ private:
|
||||
utils::field_attributes attributes_;
|
||||
data_type_t type_{data_type_t::type_unknown};
|
||||
any_type value_;
|
||||
sql_function_t function_{sql_function_t::NONE};
|
||||
std::string alias_;
|
||||
std::string ref_table_;
|
||||
std::string ref_column_;
|
||||
};
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
#include <typeindex>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
@@ -30,14 +30,13 @@ public:
|
||||
[[nodiscard]] const connection_info& info() const;
|
||||
|
||||
[[nodiscard]] record describe(const std::string &table_name) const;
|
||||
bool exists(const std::string &table_name) const;
|
||||
[[nodiscard]] bool exists(const std::string &table_name) const;
|
||||
|
||||
template<class Type>
|
||||
query_result<Type> fetch(const std::string &sql)
|
||||
{
|
||||
return query_result<Type>(connection_->fetch(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:
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class column;
|
||||
|
||||
class dialect
|
||||
{
|
||||
public:
|
||||
@@ -61,6 +63,7 @@ public:
|
||||
|
||||
using token_to_string_map = std::unordered_map<token_t, std::string>;
|
||||
using data_type_to_string_map = std::unordered_map<data_type_t, std::string>;
|
||||
using sql_func_to_string_map = std::unordered_map<sql_function_t, std::string>;
|
||||
|
||||
public:
|
||||
dialect() = default;
|
||||
@@ -79,7 +82,7 @@ public:
|
||||
* @param str The identifier string to be prepared
|
||||
* @return The prepared string
|
||||
*/
|
||||
std::string prepare_identifier(const std::string &str) const;
|
||||
std::string prepare_identifier(const column &col) const;
|
||||
|
||||
/**
|
||||
* Prepare string literal
|
||||
@@ -196,6 +199,15 @@ private:
|
||||
{data_type_t::type_null, "NULL"},
|
||||
{data_type_t::type_unknown, "UNKNOWN"}
|
||||
};
|
||||
|
||||
sql_func_to_string_map sql_func_map_ {
|
||||
{sql_function_t::NONE, "NONE" },
|
||||
{sql_function_t::COUNT, "COUNT" },
|
||||
{sql_function_t::AVG, "AVG" },
|
||||
{sql_function_t::SUM, "SUM" },
|
||||
{sql_function_t::MIN, "MIN" },
|
||||
{sql_function_t::MAX, "MAX" },
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -50,6 +50,10 @@ struct any_type_to_string_visitor
|
||||
|
||||
}
|
||||
|
||||
column alias(const std::string &column, const std::string &as);
|
||||
column alias(column &&col, const std::string &as);
|
||||
column count(const std::string &column);
|
||||
|
||||
enum class join_type_t {
|
||||
INNER, OUTER, LEFT, RIGHT
|
||||
};
|
||||
@@ -103,8 +107,8 @@ public:
|
||||
|
||||
query_builder& create();
|
||||
query_builder& drop();
|
||||
query_builder& select(std::initializer_list<std::string> column_names);
|
||||
query_builder& select(const std::vector<std::string> &column_names);
|
||||
query_builder& select(std::initializer_list<column> columns);
|
||||
query_builder& select(const std::vector<column> &columns);
|
||||
query_builder& insert();
|
||||
query_builder& update(const std::string &table);
|
||||
query_builder& remove();
|
||||
|
||||
@@ -136,7 +136,7 @@ protected:
|
||||
class query_select_intermediate : public query_start_intermediate
|
||||
{
|
||||
public:
|
||||
query_select_intermediate(session &s, std::vector<std::string> column_names);
|
||||
query_select_intermediate(session &s, const std::vector<column>& columns);
|
||||
|
||||
query_from_intermediate from(const std::string &table, const std::string &as = "");
|
||||
};
|
||||
|
||||
@@ -105,6 +105,19 @@ private:
|
||||
query_result<Type> *result_{nullptr};
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template < typename Type >
|
||||
Type* create_prototype(const record &/*prototype*/)
|
||||
{
|
||||
return new Type{};
|
||||
}
|
||||
|
||||
template <>
|
||||
record* create_prototype<record>(const record &prototype);
|
||||
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
class query_result
|
||||
{
|
||||
@@ -116,8 +129,8 @@ 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)
|
||||
query_result(std::unique_ptr<query_result_impl> impl, record record_prototype)
|
||||
: record_prototype_(std::move(record_prototype))
|
||||
, impl_(std::move(impl)) {}
|
||||
|
||||
iterator begin() { return std::move(++iterator(this)); }
|
||||
@@ -126,7 +139,7 @@ public:
|
||||
private:
|
||||
friend class query_result_iterator<Type>;
|
||||
|
||||
Type* create() { return creator_(); }
|
||||
Type* create() { return detail::create_prototype<Type>(record_prototype_); }
|
||||
|
||||
void bind(const Type &obj)
|
||||
{
|
||||
@@ -139,7 +152,7 @@ private:
|
||||
}
|
||||
|
||||
private:
|
||||
creator_func creator_ = []{ return new Type; };
|
||||
record record_prototype_;
|
||||
std::unique_ptr<query_result_impl> impl_;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef QUERY_SESSION_HPP
|
||||
#define QUERY_SESSION_HPP
|
||||
|
||||
#include "matador/sql/column_name_generator.hpp"
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
@@ -24,9 +24,9 @@ public:
|
||||
template < class Type >
|
||||
query_select_intermediate select()
|
||||
{
|
||||
return query_select_intermediate{*this, column_name_generator::generate<Type>()};
|
||||
return query_select_intermediate{*this, column_generator::generate<Type>(table_repository_)};
|
||||
}
|
||||
query_select_intermediate select(std::initializer_list<std::string> column_names);
|
||||
query_select_intermediate select(std::initializer_list<column> columns);
|
||||
query_insert_intermediate insert();
|
||||
query_update_intermediate update(const std::string &table);
|
||||
query_delete_intermediate remove();
|
||||
|
||||
@@ -53,6 +53,16 @@ enum struct database_type_t : uint8_t {
|
||||
type_unknown /*!< Data type unknown */
|
||||
};
|
||||
|
||||
enum class sql_function_t {
|
||||
NONE,
|
||||
COUNT,
|
||||
AVG,
|
||||
SUM,
|
||||
MIN,
|
||||
MAX
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @tparam T The type of the traits
|
||||
* @brief Type traits for database types
|
||||
|
||||
Reference in New Issue
Block a user