Navigation
Home
gpl
sqlmeta
v1.0b
dbconnection.cpp








































dbconnection.cpp
   
   /*
    * DBConnection.cpp
    * Part of SQLMeta, a language to use sql-queries in html pages.
    *
    * Copyright (C) 2001  Daan Vreeken
    *
    * 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.
    *
    */
   
   #include "sqlmeta.h"
   
   
   #define NeedReconnect		0
   #define ChangedUser		1
   #define ChangedDB		2
   
   
   
   
   DBConnection::DBConnection(MetaParser *MyParent, DBInfo *MyInfo)
   {
   	Parent=MyParent;
   
   	Info=MyInfo;
   	Connected=0;
   	RowNr=0;
   }
   
   
   
   int DBConnection::TryConnect(void)
   {
   	if (Info->Host==NULL)
   	{
   		Parent->Error(ErrSQLConnect,"No hostname specified");
   		return 0;
   	}
   	if (Info->User==NULL)
   	{
   		Parent->Error(ErrSQLConnect,"No username specified");
   		return 0;
   	}
   	if (Info->Password==NULL)
   	{
   		Parent->Error(ErrSQLConnect,"No password specified");
   		return 0;
   	}
   	if (Info->DB==NULL)
   	{
   		Parent->Error(ErrSQLConnect,"No database specified");
   		return 0;
   	}
   
   	Connection=(MYSQL *)malloc(sizeof(MYSQL));
   	if (Connection==NULL)
   	{
   		Parent->Error(ErrMalloc,"DBConnection::TryConnect");
   		return 0;
   	}
   
   	mysql_init(Connection);
   
   	if (!mysql_real_connect(Connection,Info->Host,Info->User,Info->Password,Info->DB,0,NULL,0))
   	{
   		ShowError(ErrSQLConnect);
   		return 0;
   	}
   
   	Connected=1;
   	return 1;
   }
   
   
   
   int DBConnection::Query(char *Query)
   {
   	if (Connection==NULL)
   	{
   		ShowError(ErrSQLConnectFirst);
   		return 0;
   	}
   
   	if (mysql_query(Connection,Query))
   	{
   		ShowError(ErrSQLQuery);
   		return 0;
   	}
   	return 1;
   }
   
   
   
   int DBConnection::GetResult(void)
   {
   	if (Connection==NULL)
   	{
   		ShowError(ErrSQLConnectFirst);
   		return 0;
   	}
   
   	Result=mysql_use_result(Connection);
   	if (Result==NULL)
   	{
   		ShowError(ErrSQLNoResult);
   		return 0;
   	}
   
   	Fields=mysql_num_fields(Result);
   	FieldName=mysql_fetch_fields(Result);
   
   	return 1;
   }
   
   
   
   int DBConnection::NextRow(void)
   {
   	Row=mysql_fetch_row(Result);
   	RowNr++;
   
   	return (Row!=NULL);
   }
   
   
   
   void DBConnection::FreeResult(void)
   {
   	mysql_free_result(Result);
   	Result=NULL;
   }
   
   
   
   void DBConnection::ShowError(char *Err)
   {
   	Parent->Error(Err,mysql_error(Connection));
   }
   
   
   
   
   DBConnection::~DBConnection(void)
   {
   	if (Connected)
   	{
   		if (Result)
   			FreeResult();
   
   		mysql_close(Connection);
   		free(Connection);
   	}
   }
   
   
   
   
   

syntax highlighted by Code2HTML, v. 0.9.1


Email me with questions/comments : Daan <Daan @ pa4dan . nl>