/* * Baltisot * Copyright (C) 1999-2007 Nicolas "Pixel" Noble * * 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; either version 2 of the License, or * (at your option) any later version. * * 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; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* $Id: SQL.cc,v 1.12 2007-05-30 11:57:10 pixel Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef HAVE_MYSQL #ifdef _WIN32 #include #endif #include "SQL.h" SQLConnection::SQLConnection(const String & host, const String & user, const String & passwd, const String & db, int port, const String & socket, unsigned long cflags) throw (GeneralException) : res(0) { mysql_init(&con); const char * phost = ((ugly_string) host).p; const char * puser = ((ugly_string) user).p; const char * ppasswd = ((ugly_string) passwd).p; const char * pdb = ((ugly_string) db).p; const char * psocket = ((ugly_string) socket).p; phost = *phost ? phost : 0; psocket = *psocket ? psocket : 0; if (!mysql_real_connect(&con, phost, puser, ppasswd, pdb, port, psocket, cflags)) { throw GeneralException("Could not connect to MySQL host " + host); } } SQLConnection::~SQLConnection() { mysql_close(&con); } int SQLConnection::query(const String & q) { int r; if (res) { mysql_free_result(res); } res = 0; if ((r = mysql_real_query(&con, ((ugly_string)q).p, q.strlen()))) { return r; } res = mysql_store_result(&con); if (res) { nr = mysql_num_rows(res); nf = mysql_num_fields(res); fields = mysql_fetch_fields(res); na = mysql_affected_rows(&con); ii = mysql_insert_id(&con); } else { nr = 0; nf = 0; fields = 0; na = 0; ii = -1; } return r; } int SQLConnection::numrows() { return nr; } int SQLConnection::numfields() { return nf; } AssocArray SQLConnection::fetchrow() { AssocArray r; MYSQL_ROW row; int i; row = mysql_fetch_row(res); for (i = 0; i < nf; i++) { r[fields[i].name] = row[i]; } return r; } int SQLConnection::numaffectedrows() { return na; } int SQLConnection::insertid() { return ii; } //int SQLConnection::errno() { // return mysql_errno(&con); //} String SQLConnection::error() { return mysql_error(&con); } #endif