#include #include #include #include #include "d_sql.h" // ------------------------------------------------------------------------- d_sql::d_sql( ) { use_query = false; this->init(); } // ------------------------------------------------------------------------- d_sql::d_sql( string host, string user, string pwd, string db, unsigned int port = 0 ) { this->init(); // copy parameters to global vars d_host = host; d_user = user; d_pwd = pwd; d_db = db; d_port = port; // connect to the sql server this->conn(); } // ------------------------------------------------------------------------- void d_sql::init( void ) { if( mysql_init( &dbase ) == NULL ) { cout << "error on SQL library init -- exiting\n"; exit( 1 ); } this->reset(); } // ------------------------------------------------------------------------- void d_sql::reset( void ) { this->d_port = 0; this->state = 0; this->head.clear(); this->head2.clear(); this->data.clear(); } // ------------------------------------------------------------------------- // actually connects to the sql server void d_sql::conn() { if( mysql_real_connect( &dbase, d_host.c_str(), d_user.c_str(), d_pwd.c_str(), d_db.c_str(), d_port, // default port is 0 NULL, 0 ) == NULL ) { cout << "
** SQL error on real_connect: server can't be reached
\n"; this->state = 0; } else { //this->state_conn = true; this->state = 1; } //this->clear(); } // ------------------------------------------------------------------------- void d_sql::close() { // disconnect if not already if( this->state == 1 ) { mysql_close( &dbase ); this->state = 0; } } // ------------------------------------------------------------------------- void d_sql::query( string q ) { char ss[1024]; strcpy( ss, q.c_str() ); this->query( ss ); } // ------------------------------------------------------------------------- void d_sql::query( char * q ) { unsigned int i, j, c = 0; if( this->state == 0 ) { cout << "
** cannot query: not connected to SQL server
"; return; } // is the look-up successful? if( mysql_real_query( &dbase, q, 0 ) != 0 ) { cout << "
** SQL query error
" << endl; this->data.clear(); return; } // so our program doesn't crash upon exiting use_query = true; result = mysql_store_result( &dbase ); //w = mysql_num_fields( result ); //h = mysql_num_rows( result ); this->data.clear(); //this->data.resize( (this->w * this->h) + this->w ); this->data.resize( mysql_num_fields( result ) * mysql_num_rows( result ) ); this->head.resize( mysql_num_fields( result ) ); this->head2.resize( mysql_num_fields( result ) ); for( j = 0; j < mysql_num_rows( result ); j++ ) { row = mysql_fetch_row(result); for( i = 0; i < mysql_num_fields( result ); i++ ) { //this->data[c + mysql_num_fields( result )] = row[i]; this->data[c] = row[i]; c++; } } unsigned int num_fields; //unsigned int i; MYSQL_FIELD *fields; num_fields = mysql_num_fields(result); fields = mysql_fetch_fields(result); for(i = 0; i < num_fields; i++) { //printf("Field %u is %s\n", i, fields[i].name); head[i] = fields[i].name; } } // ------------------------------------------------------------------------- //ostream &operator<<( ostream &out, const d_sql &L ) //{ // //out << L.data_html; // return( out ); //} // ------------------------------------------------------------------------- d_sql::~d_sql() { // free allocated resources if( use_query == true ) mysql_free_result( this->result ); // close connection this->close(); } // -------------------------------------------------------------------------