#include "connection.h"
Connection::Connection(char *host, char *db) {
#if defined(HAS_MSQL)
connection = -1;
#elif defined(HAS_MYSQL)
connection = (MYSQL *)NULL;
#else
#error No database linked.
#endif
Connect(host, db, (char *)NULL, (char *)NULL);
}
Connection::Connection(char *host, char *db, char *uid, char *pw) {
#if defined(HAS_MSQL)
connection = -1;
#elif defined(HAS_MYSQL)
connection = (MYSQL *)NULL;
#else
#error No database linked.
#endif
Connect(host, db, uid, pw);
}
void Connection::Connect(char *host, char *db, char *uid, char *pw) {
int state;
if( IsConnected() ) {
throw "Connection has already been established.";
}
#if defined(HAS_MSQL)
connection = msqlConnect(host);
state = msqlSelectDB(connection, db);
#elif defined (HAS_MYSQL)
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, host,
uid, pw,
db, 0, 0);
#else
#error No database linked.
#endif
if( !IsConnected() ) {
throw GetError();
}
if( state < 0 ) {
throw GetError();
}
}