00001
00002
00003
00004
00005
00006
00007
00008 #include "openMyDB.h"
00009 #include "ocString.h"
00010 #include <cstdlib>
00011
00012
00013 #include "openLogger.h"
00014
00015
00016
00017
00018 bool mySqlDB::error( int state )
00019 {
00020 bool bRet = true;
00021
00022 m_strErrors = mysql_error(&mysql);
00023
00024
00025 bRet = state != 0 || m_strErrors.length() > 0;
00026 return bRet;
00027 }
00028
00029
00030
00031
00032
00033
00034
00035 mySqlDB::mySqlDB( string strConnection )
00036 :openDB(strConnection)
00037 ,port(0)
00038 ,pchSocket(NULL)
00039 ,connection(NULL)
00040 ,transactioning(false)
00041 {
00042 m_bGood = true;
00043 m_provider = MySQL;
00044 parseConnection(strConnection);
00045
00046 mysql_init(&mysql);
00047
00048 connection = mysql_real_connect( &mysql,
00049 host.c_str(),
00050 uid.c_str(),
00051 pwd.c_str(),
00052 db.c_str(),
00053 port,
00054 pchSocket,
00055 flags );
00056 if( connection == NULL )
00057 {
00058 m_bGood = false;
00059 m_strErrors = "No Real Connection Possible at present: ";
00060 m_strErrors += mysql_error(&mysql);
00061 }
00062 }
00063
00064 bool mySqlDB::parseConnection( string & strConnection )
00065 {
00066 bool bRet=false;
00067 host = "";
00068 uid = "";
00069 pwd = "";
00070 db = "";
00071 port = 0;
00072 socket = "";
00073 pchSocket = NULL;
00074 flags = 0;
00075
00076 ocString connStr = strConnection;
00077 connStr.parseInit();
00078 for( string element=connStr.parse(";");
00079 !(element.length()==0 && connStr.endOfParse() );
00080 element=connStr.parse(";") )
00081 {
00082 ocString apair(element);
00083 string name = apair.parse("=");
00084 string value = apair.parse("=");
00085
00086
00087
00088
00089 transform(name.begin(),name.end(),name.begin(),(int(*)(int)) tolower );
00090 if( name == "host" )
00091 {
00092 host = value;
00093 }
00094 else if ( name == "uid" )
00095 {
00096 uid = value;
00097 }
00098 else if( name == "pwd" )
00099 {
00100 pwd = value;
00101 }
00102 else if( name == "db" )
00103 {
00104 db = value;
00105 }
00106 else if( name == "port" )
00107 {
00108 port = atol(value.c_str());
00109 }
00110 else if( name == "socket" )
00111 {
00112 socket = value;
00113 if( socket.length() )
00114 {
00115 pchSocket = socket.c_str();
00116 }
00117 }
00118 else if( name == "socket" )
00119 {
00120 socket = value;
00121 }
00122 else if( name == "flags" )
00123 {
00124 flags = atol(value.c_str());
00125 }
00126 }
00127
00128
00129 if( db.length() && uid.length() )
00130 {
00131 bRet = true;
00132 }
00133
00134 return bRet;
00135 }
00136 dbProvider mySqlDB::getProvider( void )
00137 {
00138 return m_provider;
00139 }
00140
00141 mySqlDB::~mySqlDB()
00142 {
00143 mysql_close( connection );
00144 }
00145
00146
00147 openCMD * mySqlDB::createCommand()
00148 {
00149 return new mySqlCMD( *this );
00150 }
00151
00152 openRS * mySqlDB::createRecordset()
00153 {
00154 return new mySqlRS( *this );
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164 mySqlCMD::mySqlCMD( openDB & idb ):openCMD(idb),state(0),rows(0)
00165 {
00166
00167 poDB = & dynamic_cast<mySqlDB &>(rdb);
00168 }
00169
00170 mySqlCMD::~mySqlCMD()
00171 {
00172 ;
00173 }
00174
00175 bool mySqlCMD::beginTransaction()
00176 {
00177
00178 if( !m_bTransactioning )
00179 {
00180 m_bTransactioning = true;
00181 }
00182 return m_bTransactioning;
00183 }
00184
00185 bool mySqlCMD::commit()
00186 {
00187
00188 if( m_bTransactioning )
00189 {
00190 m_bTransactioning = false;
00191 }
00192 return !m_bTransactioning;
00193 }
00194
00195 bool mySqlCMD::rollback()
00196 {
00197
00198 if( m_bTransactioning )
00199 {
00200 m_bTransactioning = false;
00201 }
00202 return !m_bTransactioning;
00203 }
00204
00205 bool mySqlCMD::execute( string sql )
00206 {
00207 bool bRet = false;
00208 if( poDB )
00209 {
00210 if( poDB->isGood() )
00211 {
00212 writelog2("mySqlCMD::execute: ", sql );
00213 state = mysql_real_query( poDB->getConnection(),
00214 sql.c_str(),
00215 sql.length() );
00216 bRet = poDB->error(state) == false;
00217 }
00218 if( bRet )
00219 {
00220
00221 rows = mysql_affected_rows(poDB->getConnection());
00222 }
00223 }
00224 return bRet;
00225 }
00226 int mySqlCMD::resultId( void )
00227 {
00228 return (int) mysql_insert_id(poDB->getConnection());
00229 }
00230 long long mySqlCMD::resultKey( const string & , const string & )
00231 {
00232 return (int) mysql_insert_id(poDB->getConnection());
00233 }
00234
00235
00236
00237
00238 mySqlRS::mySqlRS( openDB & idb )
00239 :openRS(idb),state(0),rows(0),result(NULL)
00240 {
00241
00242 poDB = dynamic_cast<mySqlDB *>(&idb);
00243 }
00244
00245 mySqlRS::~mySqlRS()
00246 {
00247 close();
00248 }
00249
00250 bool mySqlRS::open( string sql )
00251 {
00252
00253 close();
00254 m_iFields = 0;
00255 if( poDB && poDB->isGood() )
00256 {
00257
00258 writelog2("mySqlRS::open: ", sql );
00259 state = mysql_real_query(poDB->getConnection(),sql.c_str(),sql.length());
00260 writelog2("mySqlRS::open mysql_real_query state is: ", state);
00261 m_bOpen = poDB->error(state) == false;
00262 writelog2("mySqlRS::open opened? ", m_bOpen);
00263 if( m_bOpen )
00264 {
00265
00266 m_iFields = mysql_field_count(poDB->getConnection());
00267 writelog2("mySqlRS::open field count: ", m_iFields );
00268
00269
00270 result = mysql_store_result(poDB->getConnection());
00271
00272 writelog2("mySqlRS::open result: ", result );
00273
00274 if( result )
00275 {
00276
00277 m_iRecords = mysql_num_rows( result );
00278
00279 MYSQL_FIELD * pFields = mysql_fetch_fields( result );
00280
00281
00282 for( int icol = 0 ; icol < m_iFields; icol ++ )
00283 {
00284 mysqlColInfo * pInfo = new mysqlColInfo;
00285
00286 if(pInfo)
00287 {
00288
00289 colInfo.push_back(pInfo);
00290
00291
00292 pInfo->mySqlType = pFields[icol].type;
00293 pInfo->length = pFields[icol].length;
00294 pInfo->decimals = pFields[icol].decimals;
00295 pInfo->ColName = pFields[icol].name;
00296
00297
00298 my_mapItem * pItem = colMap.data[pInfo->mySqlType];
00299
00300
00301 if( pItem )
00302 {
00303
00304 basicField * pField = pItem->createField( pInfo );
00305 if( pField )
00306 {
00307
00308 ordinals.push_back(pField);
00309
00310 associations.insert(make_pair(pInfo->ColName,pField));
00311
00312 }
00313 }
00314 else
00315 {
00316 poDB->errorString() += "\r\nUnable to find type in map\r\n";
00317
00318 }
00319 }
00320 }
00321
00322 m_bOpen = next();
00323 }
00324 }
00325 }
00326
00327 return m_bOpen;
00328 }
00329
00330
00331 string & mySqlRS::getErrors( void )
00332 {
00333 string temp = poDB->errorString();
00334 poDB->error();
00335 poDB->errorString() += temp;
00336 return poDB->errorString();
00337 }
00338
00339 bool mySqlRS::beginTransaction()
00340 {
00341
00342 if( !m_bTransactioning )
00343 {
00344 m_bTransactioning = true;
00345 }
00346 return m_bTransactioning;
00347 }
00348
00349 bool mySqlRS::commit()
00350 {
00351
00352 if( m_bTransactioning )
00353 {
00354 m_bTransactioning = false;
00355 }
00356 return !m_bTransactioning;
00357 }
00358
00359 bool mySqlRS::close( void )
00360 {
00361 writelog("mySqlRS::close: " );
00362 unsigned int i;
00363
00364 for( i=0;i<ordinals.size();i++)
00365 {
00366 delete ordinals[i];
00367 }
00368 ordinals.clear();
00369 associations.clear();
00370 for( i=0;i<colInfo.size();i++)
00371 {
00372
00373 delete colInfo[i];
00374 }
00375 colInfo.clear();
00376 if( result )
00377 {
00378 mysql_free_result( result );
00379 result = NULL;
00380 }
00381 m_bOpen=false;
00382 return !m_bOpen;
00383 }
00384
00385 bool mySqlRS::next( int )
00386 {
00387
00388 bool ret = false;
00389 writelog("Getting another row..");
00390 MYSQL_ROW row = mysql_fetch_row(result);
00391 writelog2("Got row.", row );
00392 if( row )
00393 {
00394 ret = true;
00395 for( int icol = 0 ; icol < m_iFields; icol ++ )
00396 {
00397 writelog2("Moving Col", icol );
00398
00399 mysqlColInfo * pInfo = colInfo[icol];
00400
00401 my_mapItem * pItem = colMap.data[pInfo->mySqlType];
00402
00403 basicField * pField = ordinals[icol];
00404
00405 if(pInfo&&pItem&&pField)
00406 {
00407
00408 pItem->setField( pField, row[icol] );
00409
00410 }
00411 }
00412 }
00413 else
00414 {
00415 close();
00416 poDB->error();
00417 }
00418 return ret;
00419 }
00420
00421 bool mySqlRS::previous( int )
00422 {
00423
00424 return false;
00425 }