mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-12-02 11:47:56 +08:00
Add support in the SqlBinder class and the Session class (#644)
This commit is contained in:
parent
7dd2d6123b
commit
8b7ffb28d7
@ -414,6 +414,7 @@ set(DROGON_UTIL_HEADERS
|
|||||||
lib/inc/drogon/utils/Utilities.h
|
lib/inc/drogon/utils/Utilities.h
|
||||||
lib/inc/drogon/utils/any.h
|
lib/inc/drogon/utils/any.h
|
||||||
lib/inc/drogon/utils/string_view.h
|
lib/inc/drogon/utils/string_view.h
|
||||||
|
lib/inc/drogon/utils/optional.h
|
||||||
lib/inc/drogon/utils/HttpConstraint.h
|
lib/inc/drogon/utils/HttpConstraint.h
|
||||||
lib/inc/drogon/utils/OStringStream.h)
|
lib/inc/drogon/utils/OStringStream.h)
|
||||||
install(FILES ${DROGON_UTIL_HEADERS}
|
install(FILES ${DROGON_UTIL_HEADERS}
|
||||||
|
@ -16,16 +16,16 @@ void TimeFilter::doFilter(const HttpRequestPtr &req,
|
|||||||
cb(resp);
|
cb(resp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (req->session()->find(VDate))
|
auto lastDate = req->session()->getOptional<trantor::Date>(VDate);
|
||||||
|
if (lastDate)
|
||||||
{
|
{
|
||||||
auto lastDate = req->session()->get<trantor::Date>(VDate);
|
LOG_TRACE << "last:" << lastDate.value().toFormattedString(false);
|
||||||
LOG_TRACE << "last:" << lastDate.toFormattedString(false);
|
|
||||||
req->session()->modify<trantor::Date>(VDate,
|
req->session()->modify<trantor::Date>(VDate,
|
||||||
[now](trantor::Date &vdate) {
|
[now](trantor::Date &vdate) {
|
||||||
vdate = now;
|
vdate = now;
|
||||||
});
|
});
|
||||||
LOG_TRACE << "update visitDate";
|
LOG_TRACE << "update visitDate";
|
||||||
if (now > lastDate.after(10))
|
if (now > lastDate.value().after(10))
|
||||||
{
|
{
|
||||||
// 10 sec later can visit again;
|
// 10 sec later can visit again;
|
||||||
ccb();
|
ccb();
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <drogon/utils/any.h>
|
#include <drogon/utils/any.h>
|
||||||
|
#include <drogon/utils/optional.h>
|
||||||
#include <trantor/utils/Logger.h>
|
#include <trantor/utils/Logger.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -59,6 +60,35 @@ class Session
|
|||||||
}
|
}
|
||||||
return T();
|
return T();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the data identified by the key parameter and return an
|
||||||
|
* optional object that wraps the data.
|
||||||
|
*
|
||||||
|
* @tparam T
|
||||||
|
* @param key
|
||||||
|
* @return optional<T>
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
optional<T> getOptional(const std::string &key) const
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lck(mutex_);
|
||||||
|
auto it = sessionMap_.find(key);
|
||||||
|
if (it != sessionMap_.end())
|
||||||
|
{
|
||||||
|
if (typeid(T) == it->second.type())
|
||||||
|
{
|
||||||
|
return optional<T>{*(any_cast<T>(&(it->second)))};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG_ERROR << "Bad type";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return optional<T>{};
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @brief Modify or visit the data identified by the key parameter.
|
* @brief Modify or visit the data identified by the key parameter.
|
||||||
*
|
*
|
||||||
|
29
lib/inc/drogon/utils/optional.h
Normal file
29
lib/inc/drogon/utils/optional.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* optional.h
|
||||||
|
* An Tao
|
||||||
|
*
|
||||||
|
* Copyright 2018, An Tao. All rights reserved.
|
||||||
|
* https://github.com/an-tao/drogon
|
||||||
|
* Use of this source code is governed by a MIT license
|
||||||
|
* that can be found in the License file.
|
||||||
|
*
|
||||||
|
* Drogon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#if __cplusplus >= 201703L || (defined _MSC_VER && _MSC_VER > 1900)
|
||||||
|
#include <optional>
|
||||||
|
#else
|
||||||
|
#include <boost/optional.hpp>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace drogon
|
||||||
|
{
|
||||||
|
#if __cplusplus >= 201703L || (defined _MSC_VER && _MSC_VER > 1900)
|
||||||
|
using std::optional;
|
||||||
|
#else
|
||||||
|
using boost::optional;
|
||||||
|
#endif
|
||||||
|
} // namespace drogon
|
@ -20,6 +20,7 @@
|
|||||||
#include <drogon/orm/Row.h>
|
#include <drogon/orm/Row.h>
|
||||||
#include <drogon/orm/RowIterator.h>
|
#include <drogon/orm/RowIterator.h>
|
||||||
#include <drogon/utils/string_view.h>
|
#include <drogon/utils/string_view.h>
|
||||||
|
#include <drogon/utils/optional.h>
|
||||||
#include <trantor/utils/Logger.h>
|
#include <trantor/utils/Logger.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -436,7 +437,24 @@ class SqlBinder
|
|||||||
mode_ = mode;
|
mode_ = mode;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
template <typename T>
|
||||||
|
self &operator<<(const optional<T> ¶meter)
|
||||||
|
{
|
||||||
|
if (parameter)
|
||||||
|
{
|
||||||
|
return *this << parameter.value();
|
||||||
|
}
|
||||||
|
return *this << nullptr;
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
self &operator<<(optional<T> &¶meter)
|
||||||
|
{
|
||||||
|
if (parameter)
|
||||||
|
{
|
||||||
|
return *this << std::move(parameter.value());
|
||||||
|
}
|
||||||
|
return *this << nullptr;
|
||||||
|
}
|
||||||
void exec() noexcept(false);
|
void exec() noexcept(false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user