added support for PT::Logger to DbConnector and PostgreSQLConnector
git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1097 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
parent
fd1cc7debe
commit
cc2230001c
|
@ -12,8 +12,8 @@ baseexpression.o: ../../pikotools/convert/misc.h
|
|||
baseexpression.o: ../../pikotools/membuffer/membuffer.h
|
||||
baseexpression.o: ../../pikotools/textstream/types.h morm_types.h model.h
|
||||
baseexpression.o: modelconnector.h dbconnector.h dbexpression.h
|
||||
baseexpression.o: flatexpression.h flatconnector.h
|
||||
baseexpression.o: ../../pikotools/utf8/utf8.h
|
||||
baseexpression.o: ../../pikotools/logger/logger.h flatexpression.h
|
||||
baseexpression.o: flatconnector.h ../../pikotools/utf8/utf8.h
|
||||
dbconnector.o: dbconnector.h dbexpression.h baseexpression.h
|
||||
dbconnector.o: ../../pikotools/textstream/textstream.h
|
||||
dbconnector.o: ../../pikotools/space/space.h
|
||||
|
@ -23,9 +23,9 @@ dbconnector.o: ../../pikotools/convert/inttostr.h
|
|||
dbconnector.o: ../../pikotools/convert/strtoint.h
|
||||
dbconnector.o: ../../pikotools/convert/text.h ../../pikotools/convert/misc.h
|
||||
dbconnector.o: ../../pikotools/membuffer/membuffer.h
|
||||
dbconnector.o: ../../pikotools/textstream/types.h morm_types.h model.h
|
||||
dbconnector.o: modelconnector.h flatexpression.h flatconnector.h
|
||||
dbconnector.o: ../../pikotools/utf8/utf8.h
|
||||
dbconnector.o: ../../pikotools/textstream/types.h morm_types.h
|
||||
dbconnector.o: ../../pikotools/logger/logger.h model.h modelconnector.h
|
||||
dbconnector.o: flatexpression.h flatconnector.h ../../pikotools/utf8/utf8.h
|
||||
dbexpression.o: dbexpression.h baseexpression.h
|
||||
dbexpression.o: ../../pikotools/textstream/textstream.h
|
||||
dbexpression.o: ../../pikotools/space/space.h
|
||||
|
@ -49,6 +49,7 @@ flatconnector.o: ../../pikotools/convert/misc.h
|
|||
flatconnector.o: ../../pikotools/membuffer/membuffer.h
|
||||
flatconnector.o: ../../pikotools/textstream/types.h morm_types.h model.h
|
||||
flatconnector.o: modelconnector.h dbconnector.h dbexpression.h
|
||||
flatconnector.o: ../../pikotools/logger/logger.h
|
||||
flatexpression.o: flatexpression.h baseexpression.h
|
||||
flatexpression.o: ../../pikotools/textstream/textstream.h
|
||||
flatexpression.o: ../../pikotools/space/space.h
|
||||
|
@ -93,8 +94,8 @@ model.o: ../../pikotools/convert/inttostr.h
|
|||
model.o: ../../pikotools/convert/strtoint.h ../../pikotools/convert/text.h
|
||||
model.o: ../../pikotools/convert/misc.h ../../pikotools/membuffer/membuffer.h
|
||||
model.o: ../../pikotools/textstream/types.h modelconnector.h baseexpression.h
|
||||
model.o: morm_types.h dbconnector.h dbexpression.h flatexpression.h
|
||||
model.o: flatconnector.h
|
||||
model.o: morm_types.h dbconnector.h dbexpression.h
|
||||
model.o: ../../pikotools/logger/logger.h flatexpression.h flatconnector.h
|
||||
modelconnector.o: modelconnector.h baseexpression.h
|
||||
modelconnector.o: ../../pikotools/textstream/textstream.h
|
||||
modelconnector.o: ../../pikotools/space/space.h
|
||||
|
@ -107,7 +108,8 @@ modelconnector.o: ../../pikotools/convert/text.h
|
|||
modelconnector.o: ../../pikotools/convert/misc.h
|
||||
modelconnector.o: ../../pikotools/membuffer/membuffer.h
|
||||
modelconnector.o: ../../pikotools/textstream/types.h morm_types.h
|
||||
modelconnector.o: dbconnector.h dbexpression.h flatexpression.h
|
||||
modelconnector.o: dbconnector.h dbexpression.h
|
||||
modelconnector.o: ../../pikotools/logger/logger.h flatexpression.h
|
||||
modelconnector.o: flatconnector.h model.h
|
||||
postgresqlconnector.o: postgresqlconnector.h dbconnector.h dbexpression.h
|
||||
postgresqlconnector.o: baseexpression.h
|
||||
|
@ -122,6 +124,7 @@ postgresqlconnector.o: ../../pikotools/convert/text.h
|
|||
postgresqlconnector.o: ../../pikotools/convert/misc.h
|
||||
postgresqlconnector.o: ../../pikotools/membuffer/membuffer.h
|
||||
postgresqlconnector.o: ../../pikotools/textstream/types.h morm_types.h
|
||||
postgresqlconnector.o: ../../pikotools/logger/logger.h
|
||||
postgresqlconnector.o: ../../pikotools/utf8/utf8.h postgresqlexpression.h
|
||||
postgresqlconnector.o: ../../pikotools/convert/strtoint.h
|
||||
postgresqlexpression.o: postgresqlexpression.h dbexpression.h
|
||||
|
|
|
@ -45,12 +45,14 @@ DbConnector::DbConnector()
|
|||
{
|
||||
db_expression = nullptr;
|
||||
expression_allocated = false;
|
||||
logger = nullptr;
|
||||
}
|
||||
|
||||
DbConnector::DbConnector(const DbConnector &)
|
||||
{
|
||||
db_expression = nullptr;
|
||||
expression_allocated = false;
|
||||
logger = nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
@ -60,6 +62,18 @@ DbConnector::~DbConnector()
|
|||
}
|
||||
|
||||
|
||||
void DbConnector::set_logger(PT::Logger * logger)
|
||||
{
|
||||
this->logger = logger;
|
||||
}
|
||||
|
||||
void DbConnector::set_logger(PT::Logger & logger)
|
||||
{
|
||||
this->logger = &logger;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbConnector::clear_last_query_result()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
|
||||
#include "dbexpression.h"
|
||||
#include "textstream/textstream.h"
|
||||
#include "logger/logger.h"
|
||||
|
||||
|
||||
namespace morm
|
||||
|
@ -53,6 +54,9 @@ public:
|
|||
DbConnector(const DbConnector &);
|
||||
virtual ~DbConnector();
|
||||
|
||||
virtual void set_logger(PT::Logger * logger);
|
||||
virtual void set_logger(PT::Logger & logger);
|
||||
|
||||
DbExpression * get_expression();
|
||||
|
||||
virtual void clear_last_query_result();
|
||||
|
@ -159,6 +163,7 @@ protected:
|
|||
DbExpression * db_expression;
|
||||
bool expression_allocated;
|
||||
std::wstring last_query_error_msg;
|
||||
PT::Logger * logger;
|
||||
|
||||
virtual void allocate_default_expression() = 0;
|
||||
virtual void allocate_default_expression_if_needed();
|
||||
|
@ -171,7 +176,6 @@ protected:
|
|||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ ModelConnector::ModelConnector()
|
|||
model_connector_mode = MORM_MODEL_CONNECTOR_MODE_NONE;
|
||||
flat_connector = nullptr;
|
||||
db_connector = nullptr;
|
||||
|
||||
logger = nullptr;
|
||||
|
||||
//expression_callback = nullptr;
|
||||
//db_connector_callback = nullptr;
|
||||
|
@ -61,6 +61,16 @@ ModelConnector::~ModelConnector()
|
|||
}
|
||||
|
||||
|
||||
void ModelConnector::set_logger(PT::Logger * logger)
|
||||
{
|
||||
this->logger = logger;
|
||||
}
|
||||
|
||||
void ModelConnector::set_logger(PT::Logger & logger)
|
||||
{
|
||||
this->logger = &logger;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ModelConnector::set_db_expression_output_type(int output_type)
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
#include "dbconnector.h"
|
||||
#include "flatexpression.h"
|
||||
#include "flatconnector.h"
|
||||
#include "logger/logger.h"
|
||||
|
||||
|
||||
namespace morm
|
||||
|
@ -58,6 +59,9 @@ public:
|
|||
ModelConnector();
|
||||
virtual ~ModelConnector();
|
||||
|
||||
virtual void set_logger(PT::Logger * logger);
|
||||
virtual void set_logger(PT::Logger & logger);
|
||||
|
||||
virtual int get_connector_mode();
|
||||
|
||||
// FIX ME
|
||||
|
@ -106,6 +110,8 @@ protected:
|
|||
|
||||
int model_connector_mode;
|
||||
|
||||
PT::Logger * logger;
|
||||
|
||||
FlatConnector * flat_connector;
|
||||
DbConnector * db_connector;
|
||||
|
||||
|
|
|
@ -145,6 +145,14 @@ std::wstring PostgreSQLConnector::get_last_query_error_msg()
|
|||
}
|
||||
|
||||
|
||||
void PostgreSQLConnector::set_last_query_error_msg()
|
||||
{
|
||||
if( pg_conn )
|
||||
{
|
||||
set_last_query_error_msg(PQerrorMessage(pg_conn));
|
||||
}
|
||||
}
|
||||
|
||||
void PostgreSQLConnector::set_last_query_error_msg(const char * error_msg)
|
||||
{
|
||||
if( !query_results.empty() )
|
||||
|
@ -153,6 +161,10 @@ void PostgreSQLConnector::set_last_query_error_msg(const char * error_msg)
|
|||
{
|
||||
PT::UTF8ToWide(error_msg, query_results.back().error_msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
query_results.back().error_msg.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,6 +177,10 @@ void PostgreSQLConnector::set_last_query_error_msg(const wchar_t * error_msg)
|
|||
{
|
||||
query_results.back().error_msg = error_msg;
|
||||
}
|
||||
else
|
||||
{
|
||||
query_results.back().error_msg.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -206,12 +222,12 @@ void PostgreSQLConnector::set_log_queries(bool log_queries)
|
|||
|
||||
bool PostgreSQLConnector::query(const char * query_str)
|
||||
{
|
||||
// if( pg_conn )
|
||||
if( pg_conn )
|
||||
{
|
||||
// if( log_queries )
|
||||
// {
|
||||
// log << log1 << "Db: executing query: " << q << logend;
|
||||
// }
|
||||
if( log_queries && logger )
|
||||
{
|
||||
(*logger) << PT::Logger::log1 << "Db: executing query: " << query_str << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
|
||||
query_results.push_back(QueryResult());
|
||||
QueryResult & last_res = query_results.back();
|
||||
|
@ -232,10 +248,14 @@ bool PostgreSQLConnector::query(const char * query_str)
|
|||
last_res.status = PQresultStatus(last_res.result);
|
||||
last_res.result_rows = static_cast<size_t>(PQntuples(last_res.result));
|
||||
}
|
||||
else
|
||||
|
||||
if( !last_res.result || last_res.status == PGRES_FATAL_ERROR )
|
||||
{
|
||||
// log << log1 << "Db: Problem with this query: \"" << q << '\"' << logend;
|
||||
// log << log1 << "Db: " << PQerrorMessage(pg_conn) << logend;
|
||||
if( logger )
|
||||
{
|
||||
(*logger) << PT::Logger::log1 << "Db: Problem with this query: \"" << query_str << '\"' << PT::Logger::logend << PT::Logger::logsave;
|
||||
(*logger) << PT::Logger::log1 << "Db: " << PQerrorMessage(pg_conn) << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -263,7 +283,11 @@ const char * PostgreSQLConnector::query_last_sequence(const wchar_t * sequence_t
|
|||
}
|
||||
else
|
||||
{
|
||||
//log << log1 << "Db: error (currval) for table: " << table << ", " << PQerrorMessage(db_conn->GetPgConn()) << logend;
|
||||
if( pg_conn && logger )
|
||||
{
|
||||
(*logger) << PT::Logger::log1 << "Db: error (currval) for table: " << sequence_table_name << ", "
|
||||
<< PQerrorMessage(pg_conn) << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -293,7 +317,7 @@ bool PostgreSQLConnector::query_select(const char * query_str)
|
|||
if( !result )
|
||||
{
|
||||
set_was_error_in_last_query(true);
|
||||
set_last_query_error_msg(PQerrorMessage(pg_conn));
|
||||
set_last_query_error_msg();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -306,7 +330,7 @@ bool PostgreSQLConnector::query_update(const char * query_str)
|
|||
if( !result )
|
||||
{
|
||||
set_was_error_in_last_query(true);
|
||||
set_last_query_error_msg(PQerrorMessage(pg_conn));
|
||||
set_last_query_error_msg();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -319,7 +343,7 @@ bool PostgreSQLConnector::query_insert(const char * query_str)
|
|||
if( !result )
|
||||
{
|
||||
set_was_error_in_last_query(true);
|
||||
set_last_query_error_msg(PQerrorMessage(pg_conn));
|
||||
set_last_query_error_msg();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -332,7 +356,7 @@ bool PostgreSQLConnector::query_remove(const char * query_str)
|
|||
if( !result )
|
||||
{
|
||||
set_was_error_in_last_query(true);
|
||||
set_last_query_error_msg(PQerrorMessage(pg_conn));
|
||||
set_last_query_error_msg();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -868,13 +892,10 @@ void PostgreSQLConnector::connect()
|
|||
|
||||
void PostgreSQLConnector::log_connection_socket()
|
||||
{
|
||||
if( pg_conn )
|
||||
if( pg_conn && logger )
|
||||
{
|
||||
//log << log2 << "Db: connection to the database works fine" << logend;
|
||||
//log << log3 << "Db: connection socket: " << PQsocket(pg_conn) << logend;
|
||||
|
||||
//std::cout << "Db: connection to the database works fine" << std::endl;
|
||||
//std::cout << "Db: connection socket: " << PQsocket(pg_conn) << std::endl;
|
||||
(*logger) << PT::Logger::log2 << "Db: connection to the database works fine" << PT::Logger::logend << PT::Logger::logsave;
|
||||
(*logger) << PT::Logger::log3 << "Db: connection socket: " << PQsocket(pg_conn) << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -882,15 +903,17 @@ void PostgreSQLConnector::wait_for_connection()
|
|||
{
|
||||
if( !pg_conn || PQstatus(pg_conn) != CONNECTION_OK )
|
||||
{
|
||||
//log << log3 << "Db: waiting for the db to be ready...." << logend << logsave;
|
||||
//std::cout << "Db: waiting for the db to be ready...." << std::endl;
|
||||
if( logger )
|
||||
{
|
||||
(*logger) << PT::Logger::log3 << "Db: waiting for the db to be ready...." << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
|
||||
while( !assert_connection(false) )
|
||||
{
|
||||
sleep(5);
|
||||
}
|
||||
|
||||
//LogConnectionSocket();
|
||||
log_connection_socket();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -911,10 +934,9 @@ bool was_connection = true;
|
|||
else
|
||||
if( PQstatus(pg_conn) != CONNECTION_OK )
|
||||
{
|
||||
if( put_log )
|
||||
if( put_log && logger )
|
||||
{
|
||||
//log << log2 << "Db: connection to the database is lost, trying to recover" << logend;
|
||||
//std::cout << "Db: connection to the database is lost, trying to recover" << std::endl;
|
||||
(*logger) << PT::Logger::log2 << "Db: connection to the database is lost, trying to recover" << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
|
||||
was_connection = false;
|
||||
|
@ -926,8 +948,8 @@ bool was_connection = true;
|
|||
{
|
||||
if( !was_connection )
|
||||
{
|
||||
// if( put_log )
|
||||
// LogConnectionSocket();
|
||||
if( put_log )
|
||||
log_connection_socket();
|
||||
|
||||
set_db_parameters();
|
||||
}
|
||||
|
@ -936,10 +958,9 @@ bool was_connection = true;
|
|||
}
|
||||
else
|
||||
{
|
||||
if( put_log )
|
||||
if( put_log && logger )
|
||||
{
|
||||
//log << log1 << "Db: connection to db server cannot be established" << logend;
|
||||
//std::cout << "Db: connection to db server cannot be established" << std::endl;
|
||||
(*logger) << PT::Logger::log1 << "Db: connection to db server cannot be established" << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
|
||||
// if( throw_if_no_connection )
|
||||
|
@ -954,10 +975,15 @@ bool was_connection = true;
|
|||
|
||||
void PostgreSQLConnector::set_db_parameters()
|
||||
{
|
||||
if( PQsetClientEncoding(pg_conn, "UTF8") == -1 )
|
||||
if( pg_conn )
|
||||
{
|
||||
//log << log1 << "Db: Can't set the proper client encoding" << logend;
|
||||
//std::cout << "Db: Can't set the proper client encoding" << std::endl;
|
||||
if( PQsetClientEncoding(pg_conn, "UTF8") == -1 )
|
||||
{
|
||||
if( logger )
|
||||
{
|
||||
(*logger) << PT::Logger::log1 << "Db: Can't set the proper client encoding" << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -164,9 +164,7 @@ protected:
|
|||
void overwrite(PT::TextStream & stream);
|
||||
virtual const char * get_field_string_value(const char * column_name);
|
||||
virtual const char * get_field_string_value(const wchar_t * field_name);
|
||||
|
||||
|
||||
|
||||
virtual void set_last_query_error_msg();
|
||||
const char * query_last_sequence(const wchar_t * sequence_table_name);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue