mirror of
https://github.com/oatpp/oatpp-postgresql.git
synced 2025-02-17 12:09:32 +08:00
Code cleanup.
This commit is contained in:
parent
732883231b
commit
460bfc4b03
@ -41,7 +41,6 @@ std::shared_ptr<Connection> ConnectionProvider::get() {
|
||||
"Error. Can't connect. " + errMsg);
|
||||
}
|
||||
|
||||
OATPP_LOGI("ConnectionProvider", "get()");
|
||||
return std::make_shared<ConnectionImpl>(handle);
|
||||
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ data::share::StringTemplate Executor::parseQueryTemplate(const oatpp::String& na
|
||||
auto extra = std::make_shared<ql_template::Parser::TemplateExtra>();
|
||||
extra->templateName = name;
|
||||
|
||||
ql_template::TemplateValueProvider valueProvider(¶msTypeMap);
|
||||
ql_template::TemplateValueProvider valueProvider;
|
||||
extra->preparedTemplate = t.format(&valueProvider);
|
||||
|
||||
extra->paramTypes = getParamTypes(t, paramsTypeMap);
|
||||
@ -140,26 +140,7 @@ data::share::StringTemplate Executor::parseQueryTemplate(const oatpp::String& na
|
||||
}
|
||||
|
||||
std::shared_ptr<orm::Connection> Executor::getConnection() {
|
||||
|
||||
// oatpp::String dbHost = "localhost";
|
||||
// oatpp::String dbUser = "postgres";
|
||||
// oatpp::String dbPassword = "db-pass";
|
||||
// oatpp::String dbName = "postgres";
|
||||
//
|
||||
// oatpp::data::stream::ChunkedBuffer stream;
|
||||
// stream << "host=" << dbHost << " user=" << dbUser << " password=" << dbPassword << " dbname=" << dbName;
|
||||
// auto connStr = stream.toString();
|
||||
//
|
||||
// auto handle = PQconnectdb(connStr->c_str());
|
||||
//
|
||||
// if(PQstatus(handle) == CONNECTION_BAD) {
|
||||
// OATPP_LOGD("Database", "Connection to database failed: %s\n", PQerrorMessage(handle));
|
||||
// PQfinish(handle);
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
return m_connectionProvider->get();
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<orm::QueryResult> Executor::execute(const StringTemplate& queryTemplate,
|
||||
|
@ -26,72 +26,10 @@
|
||||
|
||||
namespace oatpp { namespace postgresql { namespace ql_template {
|
||||
|
||||
TemplateValueProvider::TemplateValueProvider(const orm::Executor::ParamsTypeMap* paramsTypeMap)
|
||||
: m_paramsTypeMap(paramsTypeMap)
|
||||
{
|
||||
m_typeNames.resize(data::mapping::type::ClassId::getClassCount(), nullptr);
|
||||
|
||||
setTypeName(data::mapping::type::__class::String::CLASS_ID, "text");
|
||||
setTypeName(data::mapping::type::__class::Any::CLASS_ID, nullptr);
|
||||
|
||||
setTypeName(data::mapping::type::__class::Int8::CLASS_ID, "int2");
|
||||
setTypeName(data::mapping::type::__class::UInt8::CLASS_ID, "int2");
|
||||
|
||||
setTypeName(data::mapping::type::__class::Int16::CLASS_ID, "int2");
|
||||
setTypeName(data::mapping::type::__class::UInt16::CLASS_ID, "int4");
|
||||
|
||||
setTypeName(data::mapping::type::__class::Int32::CLASS_ID, "int4");
|
||||
setTypeName(data::mapping::type::__class::UInt32::CLASS_ID, "int8");
|
||||
|
||||
setTypeName(data::mapping::type::__class::Int64::CLASS_ID, "int8");
|
||||
setTypeName(data::mapping::type::__class::UInt64::CLASS_ID, nullptr);
|
||||
|
||||
setTypeName(data::mapping::type::__class::Float32::CLASS_ID, "float4");
|
||||
setTypeName(data::mapping::type::__class::Float64::CLASS_ID, "float8");
|
||||
setTypeName(data::mapping::type::__class::Boolean::CLASS_ID, "bool");
|
||||
|
||||
setTypeName(data::mapping::type::__class::AbstractObject::CLASS_ID, nullptr);
|
||||
setTypeName(data::mapping::type::__class::AbstractEnum::CLASS_ID, nullptr);
|
||||
|
||||
setTypeName(data::mapping::type::__class::AbstractVector::CLASS_ID, nullptr);
|
||||
setTypeName(data::mapping::type::__class::AbstractList::CLASS_ID, nullptr);
|
||||
setTypeName(data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, nullptr);
|
||||
|
||||
setTypeName(data::mapping::type::__class::AbstractPairList::CLASS_ID, nullptr);
|
||||
setTypeName(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, nullptr);
|
||||
|
||||
}
|
||||
|
||||
void TemplateValueProvider::setTypeName(const data::mapping::type::ClassId& classId, const oatpp::String& name) {
|
||||
const v_uint32 id = classId.id;
|
||||
if(id < m_typeNames.size()) {
|
||||
m_typeNames[id] = name;
|
||||
} else {
|
||||
throw std::runtime_error("[oatpp::postgresql::ql_template::TemplateValueProvider::setTypeName()]: "
|
||||
"Error. Unknown classId. Class-Name=" + std::string(classId.name));
|
||||
}
|
||||
}
|
||||
|
||||
oatpp::String TemplateValueProvider::getValue(const data::share::StringTemplate::Variable& variable, v_uint32 index) {
|
||||
|
||||
auto typeIt = m_paramsTypeMap->find(variable.name);
|
||||
if(typeIt == m_paramsTypeMap->end()) {
|
||||
throw std::runtime_error("[oatpp::postgresql::Executor::PGTemplateValueProvider::getValue()]: Error. "
|
||||
"Type info not found for variable " + variable.name->std_str());
|
||||
}
|
||||
|
||||
auto typeName = m_typeNames[typeIt->second->classId.id];
|
||||
|
||||
if(!typeName) {
|
||||
throw std::runtime_error("[oatpp::postgresql::Executor::PGTemplateValueProvider::getValue()]: Error. "
|
||||
"Unsupported type - " + std::string(typeIt->second->classId.name));
|
||||
}
|
||||
|
||||
m_buffStream.setCurrentPosition(0);
|
||||
m_buffStream << "$" << (index + 1);// << "::" << typeName;
|
||||
|
||||
m_buffStream << "$" << (index + 1);
|
||||
return m_buffStream.toString();
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -32,17 +32,9 @@ namespace oatpp { namespace postgresql { namespace ql_template {
|
||||
|
||||
class TemplateValueProvider : public data::share::StringTemplate::ValueProvider {
|
||||
private:
|
||||
void setTypeName(const data::mapping::type::ClassId& classId, const oatpp::String& name);
|
||||
private:
|
||||
const orm::Executor::ParamsTypeMap* m_paramsTypeMap;
|
||||
std::vector<oatpp::String> m_typeNames;
|
||||
data::stream::BufferOutputStream m_buffStream;
|
||||
public:
|
||||
|
||||
TemplateValueProvider(const orm::Executor::ParamsTypeMap* paramsTypeMap);
|
||||
|
||||
oatpp::String getValue(const data::share::StringTemplate::Variable& variable, v_uint32 index) override;
|
||||
|
||||
};
|
||||
|
||||
}}}
|
||||
|
@ -119,9 +119,9 @@ public:
|
||||
//client.insertFloats(0.32, 0.64, connection);
|
||||
//client.insertFloats(-0.32, -0.64, connection);
|
||||
|
||||
//client.insertStrs("Hello", "World", "Oat++");
|
||||
//client.insertStrs("Hello", "World", "oatpp");
|
||||
//client.insertStrs("Yeah", "Ops", "!!!");
|
||||
// client.insertStrs("Hello", "World", "Oat++");
|
||||
// client.insertStrs("Hello", "World", "oatpp");
|
||||
// client.insertStrs("Yeah", "Ops", "!!!");
|
||||
|
||||
{
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user