| 1 |
#include <log4cxx/logger.h> |
| 2 |
#include <YSQLConnection.h> |
| 3 |
|
| 4 |
using namespace log4cxx; |
| 5 |
using namespace yngn; |
| 6 |
using namespace yngn::sql; |
| 7 |
using namespace yngn::YException; |
| 8 |
|
| 9 |
TSQLServer *YSQLConnection::sqlServer = 0; |
| 10 |
YSQLConnection *YSQLConnection::sqlConnection = 0; |
| 11 |
static LoggerPtr logger = Logger::getLogger(_T("yngn.sql.YSQLConnection")); |
| 12 |
|
| 13 |
/** |
| 14 |
* Private constructor for the YSQLConnection. |
| 15 |
* @param URL Path to the database to be used. |
| 16 |
* @param user Name of the user to open the database connection. |
| 17 |
* @param psw Password of the user to open the database connection. |
| 18 |
*/ |
| 19 |
YSQLConnection::YSQLConnection(char *URL, char *user, char *psw) throw (YSQLNotConnectedException) { |
| 20 |
YSQLConnection::sqlServer = TSQLServer::Connect(URL, user, psw); |
| 21 |
if (YSQLConnection::sqlServer == 0) throw YSQLNotConnectedException("Connection to database failed"); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get an instance of YSQLConnection opening a connection to the database. |
| 26 |
* @param URL Path to the database to be used. |
| 27 |
* @param user Name of the user to open the database connection. |
| 28 |
* @param psw Password of the user to open the database connection. |
| 29 |
*/ |
| 30 |
TSQLServer* YSQLConnection::getConnection(char *URL, char *user, char *psw) throw (YSQLNotConnectedException) { |
| 31 |
if (YSQLConnection::sqlConnection == 0){ |
| 32 |
YSQLConnection::sqlConnection = new YSQLConnection(URL, user, psw); |
| 33 |
} |
| 34 |
return sqlServer; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get an instance of YSQLConnection opening a connection to the database. |
| 39 |
* @param URL Path to the database to be used. |
| 40 |
* @param fileName Name of the file containing user/password to use for connection. |
| 41 |
|
| 42 |
TSQLServer* YSQLConnection::getConnection(char *URL, char *fileName) throw (YSQLNotConnectedException) { |
| 43 |
if (YSQLConnection::sqlConnection == 0){ |
| 44 |
FILE * pFile; |
| 45 |
long lSize; |
| 46 |
char * buffer; |
| 47 |
pFile = fopen (fileName, "r" ); |
| 48 |
// obtain file size. |
| 49 |
fseek (pFile , 0 , SEEK_END); |
| 50 |
lSize = ftell (pFile); |
| 51 |
rewind (pFile); |
| 52 |
// allocate memory to contain the whole file. |
| 53 |
buffer = (char*) malloc (lSize); |
| 54 |
if (buffer == NULL) throw YSQLNotConnectedException("Connection file empty"); |
| 55 |
// copy the file into the buffer. |
| 56 |
fread (buffer,1,lSize,pFile); |
| 57 |
// the whole file is loaded in the buffer. |
| 58 |
// terminate |
| 59 |
fclose (pFile); |
| 60 |
YSQLConnection::sqlConnection = new YSQLConnection(URL, user, psw); |
| 61 |
} |
| 62 |
return sqlServer; |
| 63 |
} |
| 64 |
*/ |
| 65 |
|
| 66 |
/** |
| 67 |
* Get an instance of YSQLConnection if a connection has already been opened connection to the database. |
| 68 |
*/ |
| 69 |
TSQLServer* YSQLConnection::getConnection(void) throw (YSQLNotConnectedException) { |
| 70 |
if (sqlConnection == NULL) throw YSQLNotConnectedException("Please connect to the database specifing the required parameters"); |
| 71 |
return sqlServer; |
| 72 |
} |
| 73 |
|