added connection impl class
This commit is contained in:
+11
-4
@@ -2,22 +2,29 @@ set(SQL_SOURCES
|
||||
sql/dialect.cpp
|
||||
sql/query_builder.cpp
|
||||
sql/column.cpp
|
||||
sql/key_value_pair.cpp
|
||||
sql/key_value_pair.cpp
|
||||
sql/basic_condition.cpp
|
||||
sql/connection.cpp
|
||||
sql/connection_intermediates.cpp)
|
||||
sql/connection_intermediates.cpp
|
||||
sql/record.cpp
|
||||
sql/connection_info.cpp
|
||||
sql/connection_impl.cpp)
|
||||
|
||||
set(SQL_HEADER
|
||||
../include/matador/sql/dialect.hpp
|
||||
../include/matador/sql/query_builder.hpp
|
||||
../include/matador/sql/column.hpp
|
||||
../include/matador/sql/types.hpp
|
||||
../include/matador/sql/key_value_pair.hpp
|
||||
../include/matador/sql/key_value_pair.hpp
|
||||
../include/matador/sql/basic_condition.hpp
|
||||
../include/matador/sql/condition.hpp
|
||||
../include/matador/sql/connection.hpp
|
||||
../include/matador/sql/connection_intermediates.hpp
|
||||
../include/matador/sql/result.hpp)
|
||||
../include/matador/sql/result.hpp
|
||||
../include/matador/sql/record.hpp
|
||||
../include/matador/sql/query_result.hpp
|
||||
../include/matador/sql/connection_impl.hpp
|
||||
../include/matador/sql/connection_info.hpp)
|
||||
|
||||
set(UTILS_HEADER
|
||||
../include/matador/utils/field_attributes.hpp
|
||||
|
||||
@@ -36,7 +36,7 @@ query_delete_intermediate connection::remove()
|
||||
return query_delete_intermediate{*this, query_.remove()};
|
||||
}
|
||||
|
||||
result connection::fetch(const std::string &sql)
|
||||
query_result<record> connection::fetch(const std::string &sql)
|
||||
{
|
||||
return {sql};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <utility>
|
||||
|
||||
#include "matador/sql/connection_impl.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
connection_impl::connection_impl(connection_info info)
|
||||
: info_(std::move(info)){}
|
||||
|
||||
const connection_info &connection_impl::info() const {
|
||||
return info_;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
|
||||
#include <regex>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
connection_info connection_info::parse(const std::string &info, unsigned short default_port, const std::string &default_driver) {
|
||||
static const std::regex DNS_RGX (R"((\w+):\/\/((([\w]+)(:([\w!]+))?)@)?((((([\w.\(\)\\]+)([:]([\d]+))?)?)\/)?(([\w.]+)( \((.*)\))?)))");
|
||||
|
||||
std::smatch what;
|
||||
|
||||
if (!std::regex_match(info, what, DNS_RGX)) {
|
||||
throw std::logic_error("connect invalid dns: " + info);
|
||||
}
|
||||
|
||||
connection_info ci{};
|
||||
ci.type = what[1].str();
|
||||
ci.user = what[4].str();
|
||||
ci.password = what[6].str();
|
||||
|
||||
// get connection part
|
||||
ci.hostname = what[11].str();
|
||||
if (what[13].matched) {
|
||||
char *end;
|
||||
ci.port = static_cast<unsigned short>(std::strtoul(what[13].str().c_str(), &end, 10));
|
||||
} else {
|
||||
ci.port = default_port;
|
||||
}
|
||||
|
||||
// Should we just ignore the "instance" part?
|
||||
// const std::string instance = what[5].str();
|
||||
ci.database = what[15].str();
|
||||
|
||||
if (what[17].matched) {
|
||||
ci.driver = what[17].str();
|
||||
} else {
|
||||
ci.driver = default_driver;
|
||||
}
|
||||
|
||||
return ci;
|
||||
}
|
||||
|
||||
std::string connection_info::to_string(const connection_info &ci) {
|
||||
std::string dns{ci.type};
|
||||
dns += "://";
|
||||
|
||||
if (!ci.user.empty()) {
|
||||
dns += ci.user;
|
||||
if (!ci.password.empty()) {
|
||||
dns += ":" + ci.password;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ci.hostname.empty()) {
|
||||
dns += "@" + ci.hostname;
|
||||
if (ci.port > 0) {
|
||||
dns += ":" + std::to_string(ci.port);
|
||||
}
|
||||
}
|
||||
|
||||
if (!dns.empty()) {
|
||||
dns += "/";
|
||||
}
|
||||
dns += ci.database;
|
||||
|
||||
if (!ci.driver.empty()) {
|
||||
dns += " (" + ci.driver +")";
|
||||
}
|
||||
return dns;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,19 +13,14 @@ query_builder &query_intermediate::query()
|
||||
return query_;
|
||||
}
|
||||
|
||||
result query_select_finish::fetch_all()
|
||||
query_result<record> query_select_finish::fetch_all()
|
||||
{
|
||||
return db().fetch(query().compile());
|
||||
}
|
||||
|
||||
result query_select_finish::fetch_one()
|
||||
record query_select_finish::fetch_one()
|
||||
{
|
||||
return db().fetch(query().compile());
|
||||
}
|
||||
|
||||
result query_select_finish::fetch_value()
|
||||
{
|
||||
return db().fetch(query().compile());
|
||||
return db().fetch(query().compile()).begin();
|
||||
}
|
||||
|
||||
query_intermediate::query_intermediate(connection &db, query_builder &query)
|
||||
@@ -106,17 +101,12 @@ query_execute_finish query_into_intermediate::values(std::initializer_list<any_t
|
||||
return {db(), query().values(values)};
|
||||
}
|
||||
|
||||
void query_create_drop_finish::execute()
|
||||
{
|
||||
db().execute(query().compile());
|
||||
}
|
||||
|
||||
query_create_drop_finish query_create_intermediate::table(const std::string &table, std::initializer_list<column> columns)
|
||||
query_execute_finish query_create_intermediate::table(const std::string &table, std::initializer_list<column> columns)
|
||||
{
|
||||
return {db(), query().table(table, columns)};
|
||||
}
|
||||
|
||||
query_create_drop_finish query_drop_intermediate::table(const std::string &table)
|
||||
query_execute_finish query_drop_intermediate::table(const std::string &table)
|
||||
{
|
||||
return {db(), query().table(table)};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
record::record(std::initializer_list<column> columns)
|
||||
: columns_(columns) {
|
||||
size_t index{0};
|
||||
for(auto &col : columns_) {
|
||||
columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index++});
|
||||
}
|
||||
}
|
||||
|
||||
const column &record::at(const std::string &name) const
|
||||
{
|
||||
return columns_by_name_.at(name).first;
|
||||
}
|
||||
|
||||
const column &record::at(size_t index)
|
||||
{
|
||||
return columns_.at(index);
|
||||
}
|
||||
|
||||
record::iterator record::find(const std::string &column_name)
|
||||
{
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
record::const_iterator record::find(const std::string &column_name) const {
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
void record::append(column col)
|
||||
{
|
||||
auto &ref = columns_.emplace_back(std::move(col));
|
||||
columns_by_name_.emplace(ref.name(), column_index_pair{std::ref(ref), columns_.size()-1});
|
||||
}
|
||||
|
||||
record::iterator record::begin()
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
record::const_iterator record::begin() const
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
record::const_iterator record::cbegin() const
|
||||
{
|
||||
return columns_.cbegin();
|
||||
}
|
||||
|
||||
record::iterator record::end()
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
record::const_iterator record::end() const
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
record::const_iterator record::cend() const
|
||||
{
|
||||
return columns_.cend();
|
||||
}
|
||||
|
||||
size_t record::size() const
|
||||
{
|
||||
return columns_.size();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user