/**********************************************************
Server Weave 0.1 Dev C++ source file
http://www.serverweave.com
Filepath: includes/mysql/MySQLConnector.cpp
Lines of code: 135
***********************************************************/
MySQLConnector::MySQLConnector(string host,string user,string pass)
{
m_host=host;
m_user=user;
m_pass=pass;
m_error="";
CON=NULL;
m_connectionid_str="NULL";
}
MySQLConnector::MySQLConnector()
{
m_host=Core->GetSetting("mysql_server");
m_user="root";
m_pass=Core->GetSetting("mysql_rootpass");
m_error="";
CON=NULL;
m_connectionid_str="NULL";
}
void MySQLConnector::SetName(string name)
{
m_connectionid_str=name;
}
uli MySQLConnector::m_connectionid=0;
void MySQLConnector::Debug(int level,string str)
{
Core->Debug(level,"MySQL[" + m_connectionid_str + "] " + str);
}
bool MySQLConnector::Connect(string host,string user,string pass)
{
if(CON!=NULL)
{
delete CON;
CON=NULL;
}
if(m_connectionid_str=="NULL")
{
m_connectionid++;
m_connectionid_str=BitStream::Int2Str(m_connectionid);
}
CON = new Connection(false);
bool ret=CON->connect(0,host.c_str(),user.c_str(),pass.c_str());
if(!ret)
{
string errstr=CON->error();
m_error=errstr;
Debug(1,"Failed to connect to DB (srv: " + host + " usr: " + user + " ) - " + errstr);
}
else Debug(2,"Successfully connected to DB (srv: [" + host + "] usr: [" + user +"])");
return ret;
}
bool MySQLConnector::Connect()
{
return Connect(m_host,m_user,m_pass);
}
//TODO: look into reversing the return value and the "goterror" bool
UseQueryResult MySQLConnector::DoQuery(string querystr,bool & goterror)
{
UseQueryResult res;
if(!IsConnected())
{
goterror=true;
Debug(1,"Unable to perform query: " + querystr + " - Not connected");
return res;
}
Query thequery = CON->query(querystr);
res = thequery.use();
Debug(4,"Performing SQL query: " + querystr);
if(CON->errnum())
{
goterror=true;
string err=CON->error();
m_error=err;
Debug(1,"ERROR: " + err);
}
else m_MySqlAffectedRows=thequery.affected_rows();
thequery.reset();
return res;
}
bool MySQLConnector::CreateDB(string dbname)
{
if(!IsConnected())
{
Debug(1,"Error creating DB: " + dbname + ", Not connected");
return false;
}
bool ret= CON->create_db(dbname);
if(!ret)
{
string errstr=CON->error();
m_error=errstr;
Debug(1,"Failed to create DB " + dbname + ": " + errstr);
}
else Debug(2,"created db " + dbname);
return ret;
}
bool MySQLConnector::SelectDB(string dbname)
{
if(!IsConnected())
{
Debug(1,"Error selecting DB: " + dbname + ", Not connected");
return false;
}
bool ret= CON->select_db(dbname);
if(!ret)
{
string errstr=CON->error();
m_error=errstr;
Debug(1,"Failed to select DB " + dbname + ": " + errstr);
}
else Debug(2,"Selecting DB " + dbname);
return ret;
}
bool MySQLConnector::SelectDB()
{
return SelectDB(Core->GetSetting("mysql_db"));
}
uli MySQLConnector::AffectedRows()
{
return m_MySqlAffectedRows;
}
void MySQLConnector::Disconnect()
{
CON->disconnect();
delete CON;
CON=NULL;
}
string MySQLConnector::GetError()
{
string ret =m_error;
m_error="";
return ret;
}
bool MySQLConnector::IsConnected()
{
return CON!=NULL; //TODO: check actual connection state - this will fix queries crashing if connection gets dropped
}