Skip to content

Commit

Permalink
added: added crypto currency symbol to the coin_history table
Browse files Browse the repository at this point in the history
  • Loading branch information
rishithaminol committed Mar 21, 2018
1 parent 4285350 commit 9d650f1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmarketcap_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ USE cmarketcap_db;
CREATE TABLE `coin_history` (
`index_` int(11) NOT NULL,
`coin_key` varchar(60) NOT NULL,
`coin_symbol` varchar(20) DEFAULT NULL,
`min_0` int(11) NOT NULL DEFAULT '0',
`min_5` int(11) NOT NULL DEFAULT '0',
`min_10` int(11) NOT NULL DEFAULT '0',
Expand Down
4 changes: 2 additions & 2 deletions httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *
int i = 1;
while (t != NULL) {
if (full_rank > 0)
sprintf(tempstr, "#%d-%s\n", t->col1_rank, t->coin_id);
sprintf(tempstr, "#%d-(%s)%s\n", t->col1_rank, t->coin_symbol, t->coin_id);
else
sprintf(tempstr, "#%d-%s: %d (%d)\n", i++, t->coin_id, t->col1_rank, t->col2_rank);
sprintf(tempstr, "#%d-(%s)%s: %d (%d)\n", i++, t->coin_symbol, t->coin_id, t->col1_rank, t->col2_rank);
write(sockfd, tempstr, strlen(tempstr));
t = t->next;
}
Expand Down
35 changes: 23 additions & 12 deletions mysql_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pthread_mutex_t mysql_db_access = PTHREAD_MUTEX_INITIALIZER;
#define LOCK_DB_ACCESS pthread_mutex_lock(&mysql_db_access)
#define UNLOCK_DB_ACCESS pthread_mutex_unlock(&mysql_db_access)

static struct coin_status *mk_coin_status(const char *coin_id, const char *col1,
int col1_rank, const char *col2, int col2_rank);
static struct coin_status *mk_coin_status(const char *coin_id, const char *coin_symbol,
const char *col1, int col1_rank, const char *col2, int col2_rank);
static struct coin_status_base *init_coin_status_base();
static void append_coin_status(struct coin_status_base *sb, struct coin_status *st);

Expand All @@ -30,6 +30,7 @@ void free_coin_status_base(struct coin_status_base *sb)

while (stat1 != NULL) {
free(stat1->coin_id);
free(stat1->coin_symbol);
free(stat1->col1);
free(stat1->col2);

Expand Down Expand Up @@ -58,14 +59,15 @@ void print_coin_status_base(struct coin_status_base *sb)
}
}

static struct coin_status *mk_coin_status(const char *coin_id, const char *col1,
int col1_rank, const char *col2, int col2_rank)
static struct coin_status *mk_coin_status(const char *coin_id, const char *coin_symbol,
const char *col1, int col1_rank, const char *col2, int col2_rank)
{
struct coin_status *coin_stat;

coin_stat = (struct coin_status *)malloc(sizeof(struct coin_status));

coin_stat->coin_id = (coin_id != NULL) ? strdup(coin_id) : NULL;
coin_stat->coin_symbol = (coin_symbol != NULL) ? strdup(coin_symbol) : NULL;
coin_stat->col1 = (col1 != NULL) ? strdup(col1) : NULL;
coin_stat->col1_rank = col1_rank;
coin_stat->col2 = (col2 != NULL) ? strdup(col2) : NULL;
Expand Down Expand Up @@ -143,7 +145,8 @@ void init_coin_history_table(MYSQL *db, struct coin_entry_base *coin_base)

t = coin_base->first;
while (t != NULL) {
sprintf(sql, "INSERT INTO `coin_history` (coin_key) VALUES ('%s')", t->id);
sprintf(sql, "INSERT INTO `coin_history` (coin_key, coin_symbol) "
"VALUES ('%s', '%s')", t->id, t->symbol);

if (mysql_query(db, sql) != 0) {
CM_ERROR("%s\n", mysql_error(db));
Expand Down Expand Up @@ -194,8 +197,10 @@ void cm_update_table(MYSQL *db, struct coin_entry_base *coin_base)
sprintf(sql, "SELECT `min_0` FROM `coin_history` WHERE coin_key='%s';", t->id);

if (mysql_query(db, sql) != 0) { /* New coin arrived */
sprintf(sql2,
"INSERT INTO `coin_history` (coin_key) VALUES ('%s');", t->id);
sprintf(sql2, "INSERT INTO `coin_history` (coin_key, coin_symbol) "
"VALUES ('%s', '%s')", t->id, t->symbol);

DEBUG_MSG("New coin arrived\n");

if (mysql_query(db, sql2) != 0)
CM_ERROR("%s\n", mysql_error(db));
Expand Down Expand Up @@ -264,6 +269,7 @@ struct coin_status_base *fetch_entire_rank(MYSQL *db)
char sql[255];
/* @brief return row values */
char *ret_coin_key;
char *ret_coin_symbol;
int ret_col_rank;

MYSQL_RES *result;
Expand All @@ -273,7 +279,7 @@ struct coin_status_base *fetch_entire_rank(MYSQL *db)

LOCK_DB_ACCESS;

sprintf(sql, "SELECT coin_key, min_0 "
sprintf(sql, "SELECT coin_key, coin_symbol, min_0 "
"FROM coin_history "
"WHERE min_0 > 0 "
"ORDER BY min_0;");
Expand All @@ -291,7 +297,7 @@ struct coin_status_base *fetch_entire_rank(MYSQL *db)
return NULL;
}

if (mysql_num_fields(result) != 2) { /* this should return 2 columns */
if (mysql_num_fields(result) != 3) { /* this should return 3 columns */
CM_ERROR("%s\n", mysql_error(db));
free_coin_status_base(coin_stat_base);
mysql_free_result(result);
Expand All @@ -303,9 +309,11 @@ struct coin_status_base *fetch_entire_rank(MYSQL *db)
struct coin_status *s;

ret_coin_key = row[0]; /* char */
ret_col_rank = atoi(row[1]); /* int */
ret_coin_symbol = row[1];
ret_col_rank = atoi(row[2]); /* int */

s = mk_coin_status((const char *)ret_coin_key,
(const char *)ret_coin_symbol,
NULL,
ret_col_rank,
NULL,
Expand Down Expand Up @@ -338,6 +346,7 @@ struct coin_status_base *fetch_duration(MYSQL *db, const char *col1,
int ret_col1;
int ret_col2;
char *ret_coin_key;
char *ret_coin_symbol;

MYSQL_RES *result;
MYSQL_ROW row;
Expand All @@ -346,7 +355,7 @@ struct coin_status_base *fetch_duration(MYSQL *db, const char *col1,

LOCK_DB_ACCESS;

sprintf(sql, "SELECT %s, %s, coin_key "
sprintf(sql, "SELECT %s, %s, coin_key, coin_symbol "
"FROM coin_history "
"WHERE %s < %s AND %s > 0 "
"ORDER BY %s;", col1, col2, col1, col2, col1, col1);
Expand All @@ -364,7 +373,7 @@ struct coin_status_base *fetch_duration(MYSQL *db, const char *col1,
return NULL;
}

if (mysql_num_fields(result) != 3) { /* this should return 3 columns */
if (mysql_num_fields(result) != 4) { /* this should return 3 columns */
CM_ERROR("%s\n", mysql_error(db));
free_coin_status_base(coin_stat_base);
mysql_free_result(result);
Expand All @@ -378,8 +387,10 @@ struct coin_status_base *fetch_duration(MYSQL *db, const char *col1,
ret_col1 = atoi(row[0]);
ret_col2 = atoi(row[1]);
ret_coin_key = row[2];
ret_coin_symbol = row[3];

s = mk_coin_status((const char *)ret_coin_key,
(const char *)ret_coin_symbol,
(const char *)col1,
ret_col1,
(const char *)col2,
Expand Down
1 change: 1 addition & 0 deletions mysql_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

struct coin_status {
char *coin_id;
char *coin_symbol;
char *col1;
int col1_rank;
char *col2;
Expand Down

0 comments on commit 9d650f1

Please sign in to comment.