changed: testing result status in do_query() in PostgreSQLConnector
renamed: some fields in QueryResult and PostgreSQLQueryResult git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1142 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
parent
1f9e4ee70a
commit
8c5eaacadb
|
@ -93,31 +93,32 @@ bool PostgreSQLConnector::do_query(const char * query_str, PostgreSQLQueryResult
|
|||
{
|
||||
if( pg_conn && psql_result )
|
||||
{
|
||||
psql_result->clear();
|
||||
|
||||
if( log_queries && logger )
|
||||
{
|
||||
(*logger) << PT::Logger::log1 << "Db: executing query: " << query_str << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
|
||||
psql_result->result = PQexec(pg_conn, query_str);
|
||||
psql_result->psql_result = PQexec(pg_conn, query_str);
|
||||
|
||||
if( !psql_result->result )
|
||||
if( !psql_result->psql_result )
|
||||
{
|
||||
if( PQstatus(pg_conn) != CONNECTION_OK )
|
||||
{
|
||||
assert_connection();
|
||||
psql_result->result = PQexec(pg_conn, query_str);
|
||||
psql_result->psql_result = PQexec(pg_conn, query_str);
|
||||
}
|
||||
}
|
||||
|
||||
if( psql_result->result )
|
||||
if( psql_result->psql_result )
|
||||
{
|
||||
psql_result->status = PQresultStatus(psql_result->result);
|
||||
psql_result->result_rows = static_cast<size_t>(PQntuples(psql_result->result));
|
||||
psql_result->psql_status = PQresultStatus(psql_result->psql_result);
|
||||
psql_result->result_rows = static_cast<size_t>(PQntuples(psql_result->psql_result));
|
||||
}
|
||||
|
||||
if( !psql_result->result || psql_result->status == PGRES_FATAL_ERROR )
|
||||
if( !psql_result->psql_result || psql_result->psql_status == PGRES_FATAL_ERROR )
|
||||
{
|
||||
psql_result->was_error = true;
|
||||
const char * err_msg = PQerrorMessage(pg_conn);
|
||||
|
||||
if( err_msg )
|
||||
|
@ -133,9 +134,13 @@ bool PostgreSQLConnector::do_query(const char * query_str, PostgreSQLQueryResult
|
|||
(*logger) << PT::Logger::log1 << "Db: " << err_msg << PT::Logger::logend << PT::Logger::logsave;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
psql_result->status = true;
|
||||
}
|
||||
}
|
||||
|
||||
return (psql_result && psql_result->result != nullptr);
|
||||
return (pg_conn && psql_result && psql_result->psql_result != nullptr && psql_result->status);
|
||||
}
|
||||
|
||||
|
||||
|
@ -210,7 +215,8 @@ bool PostgreSQLConnector::query_select(const char * query_str, QueryResult & que
|
|||
|
||||
if( psql_result )
|
||||
{
|
||||
result = (do_query(query_str, psql_result) && psql_result->status == PGRES_TUPLES_OK);
|
||||
result = (do_query(query_str, psql_result) && psql_result->psql_status == PGRES_TUPLES_OK);
|
||||
psql_result->status = result;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -224,7 +230,8 @@ bool PostgreSQLConnector::query_update(const char * query_str, QueryResult & que
|
|||
|
||||
if( psql_result )
|
||||
{
|
||||
result = (do_query(query_str, psql_result) && psql_result->status == PGRES_COMMAND_OK);
|
||||
result = (do_query(query_str, psql_result) && psql_result->psql_status == PGRES_COMMAND_OK);
|
||||
psql_result->status = result;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -238,7 +245,8 @@ bool PostgreSQLConnector::query_insert(const char * query_str, QueryResult & que
|
|||
|
||||
if( psql_result )
|
||||
{
|
||||
result = (do_query(query_str, psql_result) && psql_result->status == PGRES_COMMAND_OK);
|
||||
result = (do_query(query_str, psql_result) && psql_result->psql_status == PGRES_COMMAND_OK);
|
||||
psql_result->status = result;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -252,7 +260,8 @@ bool PostgreSQLConnector::query_remove(const char * query_str, QueryResult & que
|
|||
|
||||
if( psql_result )
|
||||
{
|
||||
result = (do_query(query_str, psql_result) && psql_result->status == PGRES_COMMAND_OK);
|
||||
result = (do_query(query_str, psql_result) && psql_result->psql_status == PGRES_COMMAND_OK);
|
||||
psql_result->status = result;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -42,20 +42,20 @@ namespace morm
|
|||
|
||||
PostgreSQLQueryResult::PostgreSQLQueryResult()
|
||||
{
|
||||
result = nullptr;
|
||||
status = PGRES_EMPTY_QUERY;
|
||||
psql_result = nullptr;
|
||||
psql_status = PGRES_EMPTY_QUERY;
|
||||
}
|
||||
|
||||
|
||||
void PostgreSQLQueryResult::clear()
|
||||
{
|
||||
if( result )
|
||||
if( psql_result )
|
||||
{
|
||||
PQclear(result);
|
||||
PQclear(psql_result);
|
||||
}
|
||||
|
||||
result = nullptr;
|
||||
status = PGRES_EMPTY_QUERY;
|
||||
psql_result = nullptr;
|
||||
psql_status = PGRES_EMPTY_QUERY;
|
||||
|
||||
QueryResult::clear();
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ void PostgreSQLQueryResult::clear()
|
|||
|
||||
bool PostgreSQLQueryResult::has_db_result()
|
||||
{
|
||||
return result != nullptr;
|
||||
return psql_result != nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
@ -71,15 +71,15 @@ const char * PostgreSQLQueryResult::get_field_string_value(const char * column_n
|
|||
{
|
||||
const char * value_str = nullptr;
|
||||
|
||||
if( result )
|
||||
if( psql_result )
|
||||
{
|
||||
int col_index = PQfnumber(result, column_name);
|
||||
int col_index = PQfnumber(psql_result, column_name);
|
||||
|
||||
if( col_index != -1 )
|
||||
{
|
||||
if( cur_row < result_rows )
|
||||
{
|
||||
value_str = PQgetvalue(result, cur_row, col_index);
|
||||
value_str = PQgetvalue(psql_result, cur_row, col_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -93,9 +93,9 @@ int PostgreSQLQueryResult::get_column_index(const char * column_name)
|
|||
{
|
||||
int col_index = -1;
|
||||
|
||||
if( result )
|
||||
if( psql_result )
|
||||
{
|
||||
col_index = PQfnumber(result, column_name);
|
||||
col_index = PQfnumber(psql_result, column_name);
|
||||
// returns -1 if there is no such a column
|
||||
}
|
||||
|
||||
|
@ -108,10 +108,10 @@ const char * PostgreSQLQueryResult::get_value_from_result(int row, int col)
|
|||
{
|
||||
const char * value_str = nullptr;
|
||||
|
||||
if( result )
|
||||
if( psql_result )
|
||||
{
|
||||
value_str = PQgetvalue(result, row, col);
|
||||
// can return a null pointer if there is no such an item in the last result
|
||||
value_str = PQgetvalue(psql_result, row, col);
|
||||
// can return a null pointer if there is no such an item in the last psql_result
|
||||
}
|
||||
|
||||
return value_str;
|
||||
|
@ -122,9 +122,9 @@ int PostgreSQLQueryResult::get_value_length(int row, int col)
|
|||
{
|
||||
int len = 0;
|
||||
|
||||
if( result )
|
||||
if( psql_result )
|
||||
{
|
||||
len = PQgetlength(result, row, col);
|
||||
len = PQgetlength(psql_result, row, col);
|
||||
}
|
||||
|
||||
return len;
|
||||
|
@ -135,9 +135,9 @@ bool PostgreSQLQueryResult::is_null(int row, int col)
|
|||
{
|
||||
bool is_null = false;
|
||||
|
||||
if( result )
|
||||
if( psql_result )
|
||||
{
|
||||
is_null = (PQgetisnull(result, row, col) == 1);
|
||||
is_null = (PQgetisnull(psql_result, row, col) == 1);
|
||||
}
|
||||
|
||||
return is_null;
|
||||
|
|
|
@ -46,8 +46,8 @@ namespace morm
|
|||
|
||||
struct PostgreSQLQueryResult : public QueryResult
|
||||
{
|
||||
PGresult * result; // can be null
|
||||
ExecStatusType status;
|
||||
PGresult * psql_result; // can be null
|
||||
ExecStatusType psql_status;
|
||||
|
||||
|
||||
PostgreSQLQueryResult();
|
||||
|
|
|
@ -57,7 +57,7 @@ void QueryResult::clear()
|
|||
{
|
||||
result_rows = 0;
|
||||
cur_row = 0;
|
||||
was_error = false;
|
||||
status = false;
|
||||
error_msg.clear();
|
||||
|
||||
temp_column_name.clear();
|
||||
|
|
|
@ -43,9 +43,9 @@ namespace morm
|
|||
|
||||
struct QueryResult
|
||||
{
|
||||
bool status;
|
||||
size_t result_rows; // how many rows in the result query
|
||||
size_t cur_row; // used for reading
|
||||
bool was_error;
|
||||
std::wstring error_msg;
|
||||
|
||||
std::string temp_column_name;
|
||||
|
|
Loading…
Reference in New Issue