From ad44a39533318c79672d436e89afe3cd518e0283 Mon Sep 17 00:00:00 2001 From: Petr Kubiznak Date: Thu, 7 Apr 2011 19:10:29 +0200 Subject: [PATCH] sql_connector: Function getTop10(...) implemented. Type of column "skore" in database changed to INTEGER. --- sqlconn/sql_connector.cpp | 62 +++++++++++++++++++++++++++++++++++------------ sqlconn/sql_connector.h | 8 +++++- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/sqlconn/sql_connector.cpp b/sqlconn/sql_connector.cpp index daebb80..5199662 100644 --- a/sqlconn/sql_connector.cpp +++ b/sqlconn/sql_connector.cpp @@ -1,10 +1,17 @@ #include "sql_connector.h" #include #include -#include +#include +#include #define TABLEDATA_BUFSIZE 255 +#define TABLE_NAME_I 0 +#define TABLE_COL_NAME_I 1 +#define TABLE_COL_SCORE_I 2 +#define TABLE_COL_DATE_I 3 +#define TABLE_COL_DIFFICULTY_I 4 + Sql_connector::Sql_connector(void) { int i; @@ -24,11 +31,11 @@ Sql_connector::Sql_connector(void) c_data->table_data[i] = (char *)malloc( sizeof(char) * TABLEDATA_BUFSIZE+1 ); } - snprintf(c_data->table_data[0], TABLEDATA_BUFSIZE, "api_miny_score"); - snprintf(c_data->table_data[1], TABLEDATA_BUFSIZE, "jmeno"); - snprintf(c_data->table_data[2], TABLEDATA_BUFSIZE, "skore"); - snprintf(c_data->table_data[3], TABLEDATA_BUFSIZE, "date"); - snprintf(c_data->table_data[4], TABLEDATA_BUFSIZE, "difficulty"); + snprintf(c_data->table_data[TABLE_NAME_I], TABLEDATA_BUFSIZE, "api_miny_score"); + snprintf(c_data->table_data[TABLE_COL_NAME_I], TABLEDATA_BUFSIZE, "jmeno"); + snprintf(c_data->table_data[TABLE_COL_SCORE_I], TABLEDATA_BUFSIZE, "skore"); + snprintf(c_data->table_data[TABLE_COL_DATE_I], TABLEDATA_BUFSIZE, "date"); + snprintf(c_data->table_data[TABLE_COL_DIFFICULTY_I], TABLEDATA_BUFSIZE, "difficulty"); fprintf(stderr, "malloc ok\n"); @@ -83,8 +90,8 @@ int Sql_connector::disconnect(void) int Sql_connector::insertScore(const char * name, int score,byte difficulty) { char * query; - int len = 0,i; - int h,m,s; + int len = 0; + int i; time_t t; tm * ptm; @@ -95,10 +102,6 @@ int Sql_connector::insertScore(const char * name, int score,byte difficulty) ptm->tm_mon++; (++ptm->tm_hour) %= 24; - s = score % 60; - m = (score / 60) % 60; - h = score / 3600; - for (i=0;i<=c_data->db_cols;i++) { len += strlen( c_data->table_data[i]); @@ -107,9 +110,9 @@ int Sql_connector::insertScore(const char * name, int score,byte difficulty) len += 100; query = (char * ) malloc(sizeof(char) * len); fprintf(stderr, "%i\n",ptm->tm_isdst); - sprintf(query,"INSERT INTO %s (%s,%s,%s,%s) VALUES ( '%s','%i:%i:%i','%i-%i-%i %i:%i:%i','%i' );", + sprintf(query,"INSERT INTO %s (%s,%s,%s,%s) VALUES ( '%s',%i,'%i-%i-%i %i:%i:%i','%i' );", c_data->table_data[0],c_data->table_data[1],c_data->table_data[2],c_data->table_data[3],c_data->table_data[4], - name,h,m,s,ptm->tm_year,ptm->tm_mon,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec,difficulty); + name,score,ptm->tm_year,ptm->tm_mon,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec,difficulty); if( (len = mysql_query(conn,query)) ) { fprintf(stderr, "error: %u %s\r\n",mysql_errno(conn),mysql_error(conn)); @@ -118,5 +121,34 @@ int Sql_connector::insertScore(const char * name, int score,byte difficulty) free(query); return len; - +} + +bool Sql_connector::getTop10(byte difficulty, std::list &recList) { + std::ostringstream os; + MYSQL_RES *sqlResult; + MYSQL_ROW sqlRow; + ScoreRecord scoreItem; + + //e.g. "SELECT jmeno,skore FROM api_miny_score WHERE difficulty=0 ORDER BY skore ASC LIMIT 0,10" + os << "SELECT " << c_data->table_data[TABLE_COL_NAME_I] << "," << + c_data->table_data[TABLE_COL_SCORE_I] << " FROM " << c_data->table_data[TABLE_NAME_I] << + " WHERE " << c_data->table_data[TABLE_COL_DIFFICULTY_I] << "=" << (int)difficulty << + " ORDER BY " << c_data->table_data[TABLE_COL_SCORE_I] << " ASC LIMIT 0,10;"; + + if(mysql_query(conn, os.str().c_str())) { + fprintf(stderr, "error: %u %s\r\n",mysql_errno(conn),mysql_error(conn)); + return false; + } + sqlResult = mysql_use_result(conn); + + recList.clear(); + while((sqlRow = mysql_fetch_row(sqlResult))) { + strncpy(scoreItem.name, sqlRow[0], sizeof(scoreItem.name)); + scoreItem.name[sizeof(scoreItem.name)]='\0'; + scoreItem.time = (int)strtol(sqlRow[1], NULL, 10); + recList.push_back(scoreItem); + } + + mysql_free_result(sqlResult); + return true; } diff --git a/sqlconn/sql_connector.h b/sqlconn/sql_connector.h index fb48a1e..fb87d33 100644 --- a/sqlconn/sql_connector.h +++ b/sqlconn/sql_connector.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include "../ui/qt/ScoreRecord.h" #define CONN_CONFIG_FILE "conn_config" @@ -39,7 +41,11 @@ class Sql_connector int connect(void); int disconnect(void); int insertScore(const char * name,int score,byte difficulty); - int getTop10(void); + /** Loads top 10 results (of given level) from database. + * @param difficulty Game level for which to load data. + * @param recList List to store results. + * @return True on success, false if loading fails. */ + bool getTop10(byte difficulty, std::list &recList); void writeConfig(void); void loadConfig(void); -- 2.11.4.GIT