00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "mysqlxx.hpp"
00030
00031
00032
00033 MySqlContext::MySqlContext( const string &dbName,
00034 const string &user,
00035 const string &passwd,
00036 const string &host,
00037 const int port,
00038 const string &unixSocket,
00039 const unsigned long clientFlags )
00040 : handle( 0 ),
00041 _dbName( dbName ),
00042 _user( user ),
00043 _passwd( passwd ),
00044 _host( host ),
00045 _port( port ),
00046 _unixSocket( unixSocket ),
00047 _clientFlags( clientFlags )
00048 {
00049 if( !_dbName.empty() )
00050 {
00051 try
00052 {
00053 open();
00054 }
00055 catch( SqlDatabaseOpenException &e )
00056 {
00057 throw e;
00058 }
00059 }
00060 }
00061
00062 MySqlContext::~MySqlContext()
00063 {
00064 close();
00065 }
00066
00067 void MySqlContext::close()
00068 {
00069 if( handle )
00070 {
00071 mysql_close( handle );
00072 handle = 0;
00073 }
00074 }
00075
00076 string MySqlContext::escapeString( const string ¶m ) const
00077 {
00078 char escapedSql[ param.length() * 2 + 1 ];
00079 mysql_real_escape_string( handle, escapedSql, param.c_str(), param.length() );
00080 return escapedSql;
00081 }
00082
00083 string MySqlContext::version() const
00084 {
00085 string v = "MySQL client library ";
00086 v += mysql_get_client_info();
00087 v += "\nMySQL server ";
00088 v += mysql_get_server_info( handle );
00089 return v;
00090 }
00091
00092
00093
00094 void MySqlContext::open()
00095 {
00096 if( handle == 0 )
00097 {
00098 if( !( handle = mysql_init( 0 ) ) )
00099 {
00100 throw SqlDatabaseOpenException( "insufficient memory to initialize context" );
00101 }
00102
00103 bool success = mysql_real_connect( handle, _host.c_str(), _user.c_str(),
00104 _passwd.c_str(), _dbName.c_str(),
00105 _port, 0, _clientFlags );
00106
00107 if( !success )
00108 {
00109 string ex = "Failed to connect to database: ";
00110 ex += mysql_error( handle );
00111 close();
00112 throw SqlDatabaseOpenException( ex );
00113 }
00114 }
00115 }
00116
00117 SqlResult MySqlContext::query( const string &sql ) const
00118 {
00119 MYSQL_RES *rawResult = 0;
00120 SqlResult result;
00121
00122 if( mysql_real_query( handle, sql.c_str(), sql.length() ) != 0 )
00123 {
00124 string ex = "Bad SQL statement: ";
00125 ex += sql;
00126 ex += "\nMySQL message: ";
00127 ex += mysql_error( handle );
00128 throw SqlDatabaseBadStatementException( ex );
00129 }
00130
00131 do
00132 {
00133 if( ( rawResult = mysql_store_result( handle ) ) )
00134 {
00135 MYSQL_ROW rawRow;
00136 unsigned int numFields = mysql_num_fields( rawResult );
00137
00138 while( ( rawRow = mysql_fetch_row( rawResult ) ) )
00139 {
00140 SqlRow *row = new SqlRow( numFields );
00141 result.add( row );
00142
00143 for( int i = 0; i < row->columns(); ++i )
00144 {
00145 row->add( rawRow[ i ] );
00146 }
00147 }
00148 }
00149
00150 mysql_free_result( rawResult );
00151 } while( mysql_next_result( handle ) > 0 );
00152
00153 return result;
00154 }