/* myselp tpc-B SQL select with prepared statement Programma per effettuare selezioni sulle tabelle standard (TPC-B) con SQL Prepared Il suo utilizzo consente di effettuare prove parametriche sull'utilizzo di MySQL in parallelo da parte di piu' utenti. */ #include /* #include */ #include "mysql.h" int Bid; int Aid; double Abalance; char username[40]; char passwd[40]; char hostname[40]; MYSQL m; MYSQL_RES *results; MYSQL_ROW record; MYSQL_STMT *stmt; MYSQL_BIND bind_in[1]; MYSQL_BIND bind_out[1]; MYSQL_RES *prepare_meta_result; #define SELECT_ACC "SELECT Abalance FROM B_ACCOUNTS WHERE Aid = ?" int param_count, column_count, row_count; unsigned long length[1]; my_bool is_null[1]; int i; int ntimes; int scale; main(argc, argv) int argc; char **argv; { if (argc != 5) { if (argc != 6) { printf("select TPC-B for MySQL database: version 1.0.0 - SQL\n"); printf("Usage: mysel user passwd hostname times [scale] \n"); exit(2); } } strcpy(username, argv[1]); strcpy(passwd, argv[2]); strcpy(hostname, argv[3]); sscanf(argv[4], "%d", &ntimes); if (argc == 5) { scale=1; } else { sscanf(argv[5], "%d", &scale); } if ( ntimes < 1 ) { printf("Usage: mysubmit user passwd hostname times log [scale] \n"); printf("times must be more than 0\n"); exit(2); } if ( scale < 1 ) { printf("Usage: mysubmit user passwd hostname times log [scale] \n"); printf("scale must be more than 0\n"); exit(2); } srand(getpid()); mysql_init(&m); if (!mysql_real_connect(&m, hostname, username, passwd, "bench", 0,NULL,0)) { fprintf(stderr, "Error in CONNECT: %s\n", mysql_error(&m)); exit(1); } stmt = mysql_stmt_init(&m); if (!stmt) { fprintf(stderr, "Error in STMT INIT\n"); exit(0); } if (mysql_stmt_prepare(stmt, SELECT_ACC, strlen(SELECT_ACC))) { fprintf(stderr, "Error in PREPARE: %s\n", mysql_stmt_error(stmt)); exit(0); } param_count= mysql_stmt_param_count(stmt); if (param_count != 1) { fprintf(stderr, "Invalid parameter count\n"); exit(0); } prepare_meta_result = mysql_stmt_result_metadata(stmt); if (!prepare_meta_result) { fprintf(stderr, "Error in META: %s\n", mysql_stmt_error(stmt)); exit(0); } column_count= mysql_num_fields(prepare_meta_result); if (column_count != 1) { fprintf(stderr, "Invalid column count\n"); exit(0); } memset(bind_in, 0, sizeof(bind_in)); bind_in[0].buffer_type= MYSQL_TYPE_LONG; bind_in[0].buffer= (char *)&Aid; bind_in[0].is_null= 0; bind_in[0].length= 0; if (mysql_stmt_bind_param(stmt, bind_in)) { fprintf(stderr, "Error in BIND PARAM: %s\n", mysql_stmt_error(stmt)); exit(0); } memset(bind_out, 0, sizeof(bind_out)); bind_out[0].buffer_type= MYSQL_TYPE_LONG; bind_out[0].buffer= (char *)&Abalance; bind_out[0].is_null= &is_null[0]; bind_out[0].length= &length[0]; if (mysql_stmt_bind_result(stmt, bind_out)) { fprintf(stderr, "Error in BIND RESULT: %s\n", mysql_stmt_error(stmt)); exit(0); } while (ntimes--) { Bid = rand(); Bid = Bid - Bid/scale*scale; Aid = rand(); Aid = Aid - Aid/100000*100000; Aid = Aid + 100000*Bid; if (mysql_stmt_execute(stmt)) { fprintf(stderr, "Error in SEL: %s\n", mysql_stmt_error(stmt)); exit(0); } /* affected_rows= mysql_stmt_affected_rows(stmt); if (affected_rows != 1) { fprintf(stderr, "Invalid number of affected rows\n"); exit(0); } */ if (mysql_stmt_store_result(stmt)) { fprintf(stderr, "Error in STORE: %s\n", mysql_stmt_error(stmt)); exit(0); } row_count= 0; while (!mysql_stmt_fetch(stmt)) { row_count++; } mysql_free_result(prepare_meta_result); } if (mysql_stmt_close(stmt)) { fprintf(stderr, "Error in CLOSE STMT: %s\n", mysql_stmt_error(stmt)); exit(0); } mysql_close(&m); exit(0); }