//************************************************* // Testcase for using MYSQL C API // // Simple create table, insert data, update and // select (with bind) //************************************************* #include #include using std::cout; using std::endl; using std::cin; const char* user = "USER"; const char* passwd = "PASSWORD"; const char* dbase = "DATABASE"; const char* server = "10.68.3.129"; #define TABLENAME "foo_dfr_blob" #define CREATETABLE "CREATE TABLE " TABLENAME " (col1 mediumblob) " #define DROPTABLE "DROP TABLE " TABLENAME #define SELECTSTMT "SELECT * FROM " TABLENAME #define INSERTSTMT "INSERT INTO " TABLENAME" VALUES (?)" inline void printMsg(const char* str) { cout << endl; cout << "--------------------------------------------------" << endl; cout << " " << str << endl; cout << "--------------------------------------------------" << endl << endl; } inline void doFatalError(const char* str) { cout << "FATAL ERROR : " << str << endl; exit(1); } inline void doFetchError(MYSQL* pMysql) { cout << "MYSQL ERROR # " << mysql_errno(pMysql) << " : " << mysql_error(pMysql) << endl; } void doExecuteDirect(MYSQL* pMysql, const char * sql) { cout << "EXECUTING SQL >> " << sql << endl; if (mysql_real_query(pMysql, sql, strlen(sql))) { doFetchError(pMysql); } } void doBindInsert(MYSQL* pMysql) { printMsg("BIND INSERT"); MYSQL_STMT* stmt; MYSQL_BIND bind[1]; unsigned long blobLen; char blobVal[150000]; stmt = mysql_prepare(pMysql, INSERTSTMT, strlen(INSERTSTMT)); if(!stmt) { doFetchError(pMysql); return; } bind[0].buffer_type = MYSQL_TYPE_MEDIUM_BLOB; bind[0].buffer = (char*)&blobVal; bind[0].is_null = 0; bind[0].buffer_length = sizeof(blobVal); bind[0].length = &blobLen; printf("Size of Blob input %u\n", sizeof(blobVal)); if (mysql_bind_param(stmt, bind)) { doFetchError(pMysql); return; } // Set the data blobVal[0] = 'A'; for (int i=1; i < 150000; ++i) { blobVal[i] = 'B'; } blobLen = sizeof(blobVal); if(mysql_execute(stmt)) { doFetchError(pMysql); return; } if(mysql_stmt_close(stmt)) { doFetchError(pMysql); return; } } void doBoundSelect(MYSQL* mysql) { printMsg("Bound Select"); MYSQL_STMT *stmt; MYSQL_BIND* bind; MYSQL_RES *prepare_meta_result; unsigned long length[1]; int param_count, row_count; char* blob_data; my_bool is_null[1]; blob_data = new char[128000]; bind = new MYSQL_BIND[1]; /* Prepare a SELECT query to fetch data from test_table */ stmt = mysql_prepare(mysql, SELECTSTMT, strlen(SELECTSTMT)); if (!stmt) { fprintf(stderr, " mysql_prepare(), SELECT failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(0); } fprintf(stdout, " prepare, SELECT successful\n"); /* Get the parameter count from the statement */ param_count= mysql_param_count(stmt); fprintf(stdout, " total parameters in SELECT: %d\n", param_count); if (param_count != 0) /* validate parameter count */ { fprintf(stderr, " invalid parameter count returned by MySQL\n"); exit(0); } /* Fetch result set meta information */ prepare_meta_result = mysql_get_metadata(stmt); if (!prepare_meta_result) { fprintf(stderr, " mysql_prepare_result(), returned no meta information\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Get max blob size in the query */ MYSQL_FIELD* field; field= mysql_fetch_fields(prepare_meta_result); fprintf(stdout, "\n max blob size in SELECT statement: %lu\n\n", field->max_length); mysql_free_result(prepare_meta_result); /* Execute the SELECT query */ if (mysql_execute(stmt)) { fprintf(stderr, " mysql_execute(), failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Get max blob size in the query */ prepare_meta_result = mysql_get_metadata(stmt); field= mysql_fetch_fields(prepare_meta_result); fprintf(stdout, "\n max blob size in SELECT statement: %lu\n\n", field->max_length); mysql_free_result(prepare_meta_result); // Now buffer all results to client if (mysql_stmt_store_result(stmt)) { fprintf(stderr, " mysql_stmt_store_result() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Get max blob size in the query */ prepare_meta_result = mysql_get_metadata(stmt); field= mysql_fetch_fields(prepare_meta_result); fprintf(stdout, "\n max blob size in SELECT statement: %lu\n\n", field->max_length); bind[0].buffer_type= MYSQL_TYPE_MEDIUM_BLOB; bind[0].buffer= (char *)blob_data; bind[0].buffer_length= 128000; bind[0].is_null= &is_null[0]; bind[0].length= &length[0]; /* Bind the result buffers */ if (mysql_bind_result(stmt, bind)) { fprintf(stderr, " mysql_bind_result() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Fetch all rows */ row_count= 0; fprintf(stdout, "Fetching results ...\n"); while (!mysql_fetch(stmt)) { row_count++; fprintf(stdout, " row %d\n", row_count); /* column 1 */ fprintf(stdout, " column1 (blob) : "); if (is_null[0]) fprintf(stdout, " NULL\n"); else fprintf(stdout, " strlen(%u), fetched size(%ld)\n", strlen(blob_data), length[0]); fprintf(stdout, "\n"); } /* Validate rows fetched */ fprintf(stdout, " total rows fetched: %d\n", row_count); /* Free the prepared result metadata */ mysql_free_result(prepare_meta_result); /* Close the statement */ if (mysql_stmt_close(stmt)) { fprintf(stderr, " failed while closing the statement\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } } int main() { printMsg("STARTING"); // Initialization MYSQL* myconn = mysql_init(NULL); if (!myconn) doFatalError("Could not get MYSQL*"); // Set the options. if (mysql_options(myconn, MYSQL_OPT_CONNECT_TIMEOUT, "30")) { doFetchError(myconn); } else { cout << "Connection timeout set to 30" << endl; } if (!mysql_real_connect(myconn, server, user, passwd, dbase, 0, NULL, 0)) { doFetchError(myconn); doFatalError("Failed to connect to server"); } // First create table printMsg("CREATE TABLE"); doExecuteDirect(myconn, CREATETABLE); doBindInsert(myconn); // Select with binding doBoundSelect(myconn); // Drop the table. cout << "Ready to drop table. Press any key to continue..." << endl; cin.get(); doExecuteDirect(myconn, DROPTABLE); // Close the connection mysql_close(myconn); return 0; }