2010-09-18 02:51:12 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2010-2014, Tomasz Sowa
|
2010-09-18 02:51:12 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-09-18 02:51:12 +02:00
|
|
|
*/
|
|
|
|
|
2013-10-10 13:59:21 +02:00
|
|
|
#include <unistd.h>
|
2010-09-18 02:51:12 +02:00
|
|
|
#include "dbconn.h"
|
|
|
|
#include "core/log.h"
|
|
|
|
#include "core/error.h"
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DbConn::DbConn()
|
|
|
|
{
|
|
|
|
pg_conn = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DbConn::~DbConn()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PGconn * DbConn::GetPgConn()
|
|
|
|
{
|
|
|
|
return pg_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-09 22:44:56 +02:00
|
|
|
void DbConn::SetConnParam(const std::wstring & d, const std::wstring & u, const std::wstring & p)
|
2010-09-18 02:51:12 +02:00
|
|
|
{
|
|
|
|
db_database = d;
|
|
|
|
db_user = u;
|
|
|
|
db_pass = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbConn::Connect()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
|
|
|
conn_info.Clear();
|
|
|
|
conn_info.SetExtented(false);
|
|
|
|
|
|
|
|
conn_info << R("dbname=") << db_database
|
|
|
|
<< R(" user=") << db_user
|
|
|
|
<< R(" password=") << db_pass;
|
|
|
|
|
|
|
|
pg_conn = PQconnectdb(conn_info.CStr());
|
|
|
|
|
|
|
|
// warning! pg_conn can be not null but there cannnot be a connection established
|
|
|
|
// use PQstatus(pg_conn) to check whether the connection works fine
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbConn::LogConnectionSocket()
|
|
|
|
{
|
|
|
|
log << log2 << "Db: connection to the database works fine" << logend;
|
|
|
|
log << log3 << "Db: connection socket: " << PQsocket(pg_conn) << logend;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbConn::WaitForConnection()
|
|
|
|
{
|
|
|
|
if( !pg_conn || PQstatus(pg_conn) != CONNECTION_OK )
|
|
|
|
{
|
2013-01-19 11:10:17 +01:00
|
|
|
log << log3 << "Db: waiting for the db to be ready...." << logend << logsave;
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
while( !AssertConnection(false, false) )
|
|
|
|
sleep(5);
|
|
|
|
|
|
|
|
LogConnectionSocket();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbConn::Close()
|
|
|
|
{
|
|
|
|
if( pg_conn )
|
|
|
|
{
|
|
|
|
PQfinish(pg_conn);
|
|
|
|
pg_conn = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool DbConn::AssertConnection(bool put_log, bool throw_if_no_connection)
|
|
|
|
{
|
|
|
|
bool was_connection = true;
|
|
|
|
|
|
|
|
|
|
|
|
if( !pg_conn )
|
|
|
|
{
|
|
|
|
was_connection = false;
|
|
|
|
Connect();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( PQstatus(pg_conn) != CONNECTION_OK )
|
|
|
|
{
|
|
|
|
if( put_log )
|
|
|
|
log << log2 << "Db: connection to the database is lost, trying to recover" << logend;
|
|
|
|
|
|
|
|
was_connection = false;
|
|
|
|
PQreset(pg_conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( pg_conn && PQstatus(pg_conn) == CONNECTION_OK )
|
|
|
|
{
|
|
|
|
if( !was_connection )
|
|
|
|
{
|
|
|
|
if( put_log )
|
|
|
|
LogConnectionSocket();
|
|
|
|
|
|
|
|
SetDbParameters();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( put_log )
|
|
|
|
log << log1 << "Db: connection to db server cannot be established" << logend;
|
|
|
|
|
|
|
|
if( throw_if_no_connection )
|
|
|
|
throw Error(WINIX_ERR_DB_FATAL_ERROR_DURING_CONNECTING);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbConn::SetDbParameters()
|
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
if( PQsetClientEncoding(pg_conn, "UTF8") == -1 )
|
2010-09-18 02:51:12 +02:00
|
|
|
log << log1 << "Db: Can't set the proper client encoding" << logend;
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|