/* Marcion
Copyright (C) 2009 - 2011 Milan Konvicka
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program */
#include "cmysql.h"
MYSQL * CMySql::mysql(0);
MYSQL * CMySql::r_mysql(0);
MYSQL * CMySql::m_init(0);
MYSQL * CMySql::m_r_init(0);
QString const CMySql::delimiter(";;;***;;;\n");
QString CMySql::last_error=QString();
CMySql::CMySql(Dbase dbase)
:command(),result(0),dbase(dbase)
{
}
CMySql::CMySql(QString const & command,Dbase dbase)
:command(command),result(0),dbase(dbase)
{
}
CMySql::~CMySql()
{
if(result)
mysql_free_result(result);
}
int CMySql::fileToBlob(char const * cmd_begin,char const * cmd_end,QString const & filename)
{
FILE * f;
f=fopen(filename.toUtf8(),"rb");
if(!f)
return -3;
if(fseek(f,0,SEEK_END)!=0)
{
fclose(f);
return -4;
}
long int const fsize=ftell(f);
size_t const cmdlen=strlen(cmd_begin),
cmd2len=strlen(cmd_end);
char * to=new char((fsize*2)+1+cmdlen+cmd2len),
*file_to=new char((fsize*2)+1),
*end;
if(!(to&&file_to))
{
fclose(f);
return -1;
}
size_t const real_size=fread(file_to,fsize,1,f);
if(real_size!=(size_t)fsize)
{
fclose(f);
return -2;
}
fclose(f);
end=stpcpy(to,cmd_begin);
unsigned long l2=mysql_real_escape_string(p_mysql(),end,file_to,real_size);
end+=l2;
end=stpcpy(end,cmd_begin);
mysql_real_query(p_mysql(),to,cmd2len+cmdlen+l2);
int e=mysql_errno(p_mysql());
if(e==0)
result=mysql_store_result(p_mysql());
else
{
error=QString::number(e)+": "+QString(mysql_error(p_mysql()));
return -5;
}
return 0;
}
bool CMySql::exec(QString const & command)
{
mysql_query(p_mysql(), command.toUtf8().data());
int e=mysql_errno(p_mysql());
if(e==0)
result=mysql_store_result(p_mysql());
else
{
error=QString::number(e)+": "+QString(mysql_error(p_mysql()));
return false;
}
return true;
}
bool CMySql::exec()
{
return exec(command);
}
bool CMySql::first()
{
return next();
}
bool CMySql::next()
{
if((row=mysql_fetch_row(result)))
return true;
return false;
}
bool CMySql::isNULL(int index) const
{
return !row[index];
}
QString CMySql::value(int index) const
{
return QString::fromUtf8(row[index]);
}
char * CMySql::data(int index) const
{
return row[index];
}
QString CMySql::escapedValue(int index) const
{
return CTranslit::escaped(value(index));
}
QString CMySql::lastError() const
{
return error;
}
unsigned int CMySql::size() const
{
return mysql_num_rows(result);
}
unsigned int CMySql::fieldCount() const
{
return mysql_num_fields(result);
}
bool CMySql::connect(QString const & host,
QString const & user,
QString const & password,
QString const & db,
unsigned int port)
{
CMySql::m_r_init = mysql_init(0);
mysql_options(CMySql::m_r_init, MYSQL_READ_DEFAULT_GROUP, "client");
CMySql::r_mysql=mysql_real_connect(m_r_init,
host.toUtf8().data(),
user.toUtf8().data(),
password.toUtf8().data(),
db.toUtf8().data(),
port,NULL,0);
int e=mysql_errno(m_r_init);
if(e!=0)
{
last_error=QString::number(e)+": "+QString(mysql_error(m_r_init));
CMySql::close();
return false;
}
return true;
}
void CMySql::close()
{
//mysql_close(CMySql::r_mysql);
mysql_close(CMySql::m_r_init);
}
bool CMySql::connect_local(
QString const & host,
QString const & user,
QString const & password,
QString const & db,
unsigned int port)
{
CMySql::mysql=mysql_real_connect(m_init, host.toUtf8().constData(),user.toUtf8().constData(),password.toUtf8().constData(), db.toUtf8().constData(), port,NULL,0);
OSTREAM << "mysql_real_connect(" << host << "," << user << "," << password << "," << db << ",NULL,0) ";
int e=mysql_errno(m_init);
if(e!=0)
{
OSTREAM << "failed\n";
last_error=QString::number(e)+": "+QString(mysql_error(m_init));
return false;
}
OSTREAM << "OK\n";
OSTREAM << "MySql server: " << QString(mysql_get_server_info(mysql)) << "\n";
OSTREAM.flush();
//printf("MySql host: %s\n",mysql_get_host_info(m_init));
return true;
}
bool CMySql::connect_local()
{
CMySql::mysql=mysql_real_connect(m_init, "127.0.0.1","marcion","marcion", "marcion", 0,NULL,0);
int e=mysql_errno(m_init);
if(e!=0)
{
OSTREAM << "mysql_real_connect() failed\n";
OSTREAM.flush();
last_error=QString::number(e)+": "+QString(mysql_error(m_init));
return false;
}
OSTREAM << "mysql_real_connect() OK\n";
OSTREAM.flush();
return true;
}
/*bool CMySql::setVar(QString const & name,QString const & value)
{
QString cmd("set session "+name+"='"+value+"'");
CMySql q(cmd);
return q.exec();
}*/
QString CMySql::last_Error()
{
return last_error;
}
bool CMySql::hasResult() const
{
return result!=0;
}
QString CMySql::fieldName() const
{
MYSQL_FIELD *field;
field = mysql_fetch_field(result);
if(field)
return QString(field->name);
else
return QString();
}
QString CMySql::realEscape(const char * from,unsigned long length) const
{
char * to=new char((length*2)+1);
/*unsigned long l2=*/mysql_real_escape_string(p_mysql(),to,from,length);
QString r(to);
delete to;
return r;
}