D:/Zythum/DinoKod/DataBase/MySQL.cpp

00001 //---------------------------------------------------------------------------------------------
00002 //      This file is a part of "DinoKod".
00003 //      Copyright © 2003 Dino Productions. All Rights Reserved.
00004 //      
00005 //      File                    : MySQL.cpp
00006 //      Author                  : Sebastien LEIX        sebastien.leix@wanadoo.fr
00007 //      Date                    : 06/05/2002
00008 //      Modification    :
00009 //
00010 //---------------------------------------------------------------------------------------------
00011 #include <winsock2.h>
00012 #include "Common/Error.h"
00013 #include "Common/Console.h"
00014 
00015 #include "DataBase/MySQL.h"
00016 
00017 //---------------------------------------------------------------------------------------------------------------------
00018 KMySQL::KMySQL()
00019 {
00020         m_pMySQL        = NULL;
00021         m_nFields       = 0;
00022         m_nRows         = 0;
00023         m_pResult       = NULL;
00024 }
00025 
00026 //---------------------------------------------------------------------------------------------------------------------
00027 KMySQL::~KMySQL()
00028 {
00029         FlushResult();
00030 }
00031 
00032 //---------------------------------------------------------------------------------------------------------------------
00033 bool KMySQL::Init()
00034 {
00035         if( m_pMySQL )
00036         {
00037                 KError::Error( NULL, "KMySQL::Init() : MySQL already initialized" );
00038                 return false;
00039         }
00040 
00041         m_pMySQL = mysql_init( NULL );
00042         if( !m_pMySQL )
00043         {
00044                 KError::Error( NULL, "KMySQL::Init() : Cannot init mysql" );
00045                 return false;
00046         }
00047 
00048         return true;
00049 }
00050 
00051 //---------------------------------------------------------------------------------------------------------------------
00052 bool KMySQL::End()
00053 {
00054         if( m_pMySQL )
00055                 if( !Disconnect() )
00056                         return false;
00057 
00058         return true;
00059 }
00060 
00061 //---------------------------------------------------------------------------------------------------------------------
00062 bool KMySQL::Connect( char* pHost, char* pLogin, char* pPassword, char* pDataBase )
00063 {
00064         if( !mysql_real_connect( m_pMySQL, pHost, pLogin, pPassword, pDataBase, 0, NULL, 0 ) )
00065                 return false;
00066 
00067         return true;
00068 }
00069 
00070 //---------------------------------------------------------------------------------------------------------------------
00071 bool KMySQL::Disconnect()
00072 {
00073         if( !m_pMySQL )
00074         {
00075                 KError::Error( NULL, "KMySQL::Disconnect() : MySQL not initialized" );
00076                 return false;
00077         }
00078 
00079         mysql_close( m_pMySQL );
00080         m_pMySQL = NULL;
00081 
00082         return true;
00083 }
00084 
00085 //---------------------------------------------------------------------------------------------------------------------
00086 bool KMySQL::Query( char* pQuery, s32* pnFields, s32* pnRows, char**** pResult )
00087 {
00088         *pnFields       = 0;
00089         *pnRows         = 0;
00090         *pResult        = NULL;
00091 
00092         if( !m_pMySQL )
00093         {
00094                 KError::Error( NULL, "KMySQL::Query() : MySQL not initialized" );
00095                 return false;
00096         }
00097 
00098         if( mysql_query( m_pMySQL, pQuery ) )
00099                 return false;
00100 
00101         MYSQL_RES*      pMySQLResult;
00102         
00103         pMySQLResult = mysql_store_result( m_pMySQL );
00104         if( !pMySQLResult )
00105                 return false;
00106 
00107         // Creation de la table de resultat
00108         MYSQL_ROW       Row;
00109         s32                     r, f;
00110 
00111         *pnFields       = mysql_num_fields( pMySQLResult );
00112         *pnRows         = (s32)mysql_num_rows( pMySQLResult );
00113 
00114         FlushResult();
00115         
00116         if( !(*pnFields) || !(*pnRows) )
00117                 return false;
00118 
00119         AllocResult( *pnFields, *pnRows );
00120 
00121         r = 0;
00122         while( Row = mysql_fetch_row( pMySQLResult ) )
00123         {
00124                 for( f = 0; f < *pnFields; f ++ )
00125                 {
00126                         m_pResult[f][r] = strdup( Row[f] ? Row[f] : "" );
00127                 }
00128                 r ++;
00129         }
00130 
00131         *pResult = m_pResult;
00132 
00133         return true;
00134 }
00135 
00136 //---------------------------------------------------------------------------------------------------------------------
00137 void KMySQL::AllocResult( s32 nFields, s32 nRows )
00138 {
00139         KASSERT( !m_pResult );
00140 
00141         m_nFields       = nFields;
00142         m_nRows         = nRows;
00143 
00144         m_pResult = new char**[nFields];
00145         for( s32 f = 0; f < nFields; f ++ )
00146         {
00147                 m_pResult[f] = new char*[nRows];
00148                 for( s32 r = 0; r < m_nRows; r ++ )
00149                         m_pResult[f][r] = NULL;
00150         }
00151 }
00152 
00153 //---------------------------------------------------------------------------------------------------------------------
00154 void KMySQL::FlushResult()
00155 {
00156         if( !m_pResult )
00157                 return;
00158 
00159         for( s32 f = 0; f < m_nFields; f ++ )
00160         {
00161                 for( s32 r = 0; r < m_nRows; r ++ )
00162                         if( m_pResult[f][r] )
00163                                 Freep( m_pResult[f][r] );
00164 
00165                 Deletev( m_pResult[f] );
00166         }
00167         Deletev( m_pResult );
00168 
00169         m_nFields       = 0;
00170         m_nRows         = 0;
00171 }

Generated on Sun Mar 25 20:02:11 2007 for Zythum Project by  doxygen 1.5.1-p1