rename compile to build and added sql_hash to query_context
This commit is contained in:
parent
57246d8742
commit
d59b41d06c
|
|
@ -29,9 +29,9 @@ struct value_visitor;
|
||||||
|
|
||||||
class query_builder final : public query_part_visitor {
|
class query_builder final : public query_part_visitor {
|
||||||
public:
|
public:
|
||||||
sql::query_context compile(const query_data &data,
|
sql::query_context build(const query_data &data,
|
||||||
const sql::dialect &d,
|
const sql::dialect &d,
|
||||||
std::optional<std::reference_wrapper<const sql::connection_impl>> conn);
|
std::optional<std::reference_wrapper<const sql::connection_impl>> conn);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void visit(internal::query_alter_part& part) override;
|
void visit(internal::query_alter_part& part) override;
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ enum class return_mode {
|
||||||
|
|
||||||
struct query_context {
|
struct query_context {
|
||||||
std::string sql;
|
std::string sql;
|
||||||
|
size_t sql_hash{};
|
||||||
sql_command command{};
|
sql_command command{};
|
||||||
std::string command_name{};
|
std::string command_name{};
|
||||||
std::string schema_name{};
|
std::string schema_name{};
|
||||||
|
|
|
||||||
|
|
@ -9,22 +9,22 @@ namespace matador::query {
|
||||||
|
|
||||||
utils::result<sql::execute_result, utils::error> executable_query::execute(const sql::executor &exec) const {
|
utils::result<sql::execute_result, utils::error> executable_query::execute(const sql::executor &exec) const {
|
||||||
query_builder compiler;
|
query_builder compiler;
|
||||||
return exec.execute(compiler.compile(*context_, exec.dialect(), std::nullopt));
|
return exec.execute(compiler.build(*context_, exec.dialect(), std::nullopt));
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<sql::statement, utils::error> executable_query::prepare(sql::executor &exec) const {
|
utils::result<sql::statement, utils::error> executable_query::prepare(sql::executor &exec) const {
|
||||||
query_builder compiler;
|
query_builder compiler;
|
||||||
return exec.prepare(compiler.compile(*context_, exec.dialect(), std::nullopt));
|
return exec.prepare(compiler.build(*context_, exec.dialect(), std::nullopt));
|
||||||
}
|
}
|
||||||
|
|
||||||
sql::query_context executable_query::compile( const sql::executor& exec ) const {
|
sql::query_context executable_query::compile( const sql::executor& exec ) const {
|
||||||
query_builder compiler;
|
query_builder compiler;
|
||||||
return compiler.compile(*context_, exec.dialect(), std::nullopt);
|
return compiler.build(*context_, exec.dialect(), std::nullopt);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string executable_query::str(const sql::executor &exec) const {
|
std::string executable_query::str(const sql::executor &exec) const {
|
||||||
query_builder compiler;
|
query_builder compiler;
|
||||||
return exec.str(compiler.compile(*context_, exec.dialect(), std::nullopt));
|
return exec.str(compiler.build(*context_, exec.dialect(), std::nullopt));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ sql::record *create_prototype(const std::vector<object::attribute> &prototype) {
|
||||||
}
|
}
|
||||||
utils::result<sql::query_result<sql::record>, utils::error> fetchable_query::fetch_all(const sql::executor &exec) const {
|
utils::result<sql::query_result<sql::record>, utils::error> fetchable_query::fetch_all(const sql::executor &exec) const {
|
||||||
query_builder compiler;
|
query_builder compiler;
|
||||||
auto ctx = compiler.compile(*context_, exec.dialect(), std::nullopt);
|
auto ctx = compiler.build(*context_, exec.dialect(), std::nullopt);
|
||||||
ctx.resolver = exec.resolver();
|
ctx.resolver = exec.resolver();
|
||||||
return exec.fetch(ctx)
|
return exec.fetch(ctx)
|
||||||
.and_then([](auto &&res) {
|
.and_then([](auto &&res) {
|
||||||
|
|
@ -38,7 +38,7 @@ utils::result<sql::query_result<sql::record>, utils::error> fetchable_query::fet
|
||||||
|
|
||||||
utils::result<std::optional<sql::record>, utils::error> fetchable_query::fetch_one(const sql::executor &exec) const {
|
utils::result<std::optional<sql::record>, utils::error> fetchable_query::fetch_one(const sql::executor &exec) const {
|
||||||
query_builder compiler;
|
query_builder compiler;
|
||||||
auto ctx = compiler.compile(*context_, exec.dialect(), std::nullopt);
|
auto ctx = compiler.build(*context_, exec.dialect(), std::nullopt);
|
||||||
ctx.resolver = exec.resolver();
|
ctx.resolver = exec.resolver();
|
||||||
auto result = exec.fetch(ctx);
|
auto result = exec.fetch(ctx);
|
||||||
if (!result.is_ok()) {
|
if (!result.is_ok()) {
|
||||||
|
|
@ -64,7 +64,7 @@ std::string fetchable_query::str(const sql::dialect &d) const {
|
||||||
|
|
||||||
sql::query_context fetchable_query::compile(const sql::dialect &d) const {
|
sql::query_context fetchable_query::compile(const sql::dialect &d) const {
|
||||||
query_builder compiler;
|
query_builder compiler;
|
||||||
return compiler.compile(*context_, d, std::nullopt);
|
return compiler.build(*context_, d, std::nullopt);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetchable_query::fetch(const sql::executor &exec, const std::type_index& index) const {
|
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetchable_query::fetch(const sql::executor &exec, const std::type_index& index) const {
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,9 @@
|
||||||
#include "matador/sql/dialect.hpp"
|
#include "matador/sql/dialect.hpp"
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
sql::query_context query_builder::compile(const query_data& data,
|
sql::query_context query_builder::build(const query_data& data,
|
||||||
const sql::dialect& d,
|
const sql::dialect& d,
|
||||||
const std::optional<std::reference_wrapper<const sql::connection_impl>>
|
const std::optional<std::reference_wrapper<const sql::connection_impl>> conn) {
|
||||||
conn) {
|
|
||||||
data_ = &data;
|
data_ = &data;
|
||||||
dialect_ = &d;
|
dialect_ = &d;
|
||||||
connection_ = conn;
|
connection_ = conn;
|
||||||
|
|
@ -33,6 +32,7 @@ sql::query_context query_builder::compile(const query_data& data,
|
||||||
dialect_ = nullptr;
|
dialect_ = nullptr;
|
||||||
data_ = nullptr;
|
data_ = nullptr;
|
||||||
|
|
||||||
|
query_.sql_hash = std::hash<std::string>{}(query_.sql);
|
||||||
return {query_};
|
return {query_};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -151,10 +151,9 @@ statement_cache::statement_cache(utils::message_bus &bus, connection_pool &pool,
|
||||||
utils::result<statement, utils::error> statement_cache::acquire(const query_context& ctx) {
|
utils::result<statement, utils::error> statement_cache::acquire(const query_context& ctx) {
|
||||||
std::unique_lock lock(mutex_);
|
std::unique_lock lock(mutex_);
|
||||||
// hash statement
|
// hash statement
|
||||||
const auto key = std::hash<std::string>{}(ctx.sql);
|
|
||||||
const auto now = std::chrono::steady_clock::now();
|
const auto now = std::chrono::steady_clock::now();
|
||||||
// Found in cache. Move it to of the LRU list
|
// Found in cache. Move it to of the LRU list
|
||||||
if (const auto it = cache_map_.find(key); it != cache_map_.end()) {
|
if (const auto it = cache_map_.find(ctx.sql_hash); it != cache_map_.end()) {
|
||||||
usage_list_.splice(usage_list_.begin(), usage_list_, it->second.position);
|
usage_list_.splice(usage_list_.begin(), usage_list_, it->second.position);
|
||||||
it->second.last_access = now;
|
it->second.last_access = now;
|
||||||
bus_.publish<statement_accessed_event>({ctx.sql, std::chrono::steady_clock::now()});
|
bus_.publish<statement_accessed_event>({ctx.sql, std::chrono::steady_clock::now()});
|
||||||
|
|
@ -181,9 +180,9 @@ utils::result<statement, utils::error> statement_cache::acquire(const query_cont
|
||||||
bus_.publish<statement_evicted_event>({ctx.sql, std::chrono::steady_clock::now()});
|
bus_.publish<statement_evicted_event>({ctx.sql, std::chrono::steady_clock::now()});
|
||||||
}
|
}
|
||||||
|
|
||||||
usage_list_.push_front(key);
|
usage_list_.push_front(ctx.sql_hash);
|
||||||
const auto it = cache_map_.insert({
|
const auto it = cache_map_.insert({
|
||||||
key,
|
ctx.sql_hash,
|
||||||
{statement{
|
{statement{
|
||||||
std::make_shared<internal::statement_cache_proxy>(bus_, std::move(stmt), pool_, id)},
|
std::make_shared<internal::statement_cache_proxy>(bus_, std::move(stmt), pool_, id)},
|
||||||
std::chrono::steady_clock::now(),
|
std::chrono::steady_clock::now(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue