Skip to content

Commit

Permalink
added sqlite database for data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jankammerath committed Dec 31, 2017
1 parent 337e075 commit 61f1ae0
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
data/
*.tar.gz
data/epg
data/logo
*.tar.gz
Binary file modified bin/iptvx
Binary file not shown.
Binary file added data/db
Binary file not shown.
1 change: 1 addition & 0 deletions src/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct channel{
GString* logoFile;
GString* logoUrl;
GArray* programmeList;
long lastUpdated;
} typedef channel;

#endif
125 changes: 125 additions & 0 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <sqlite3.h>
#include <string.h>
#include <glib.h>
#include "channel.h"

Expand All @@ -39,6 +40,111 @@ void iptvx_db_init(char* filename){
}
}

/*
Checks if a programme already exists in the database and returns it's id
@param chanid id of the channel the programme is on
@param prog programme struct with the actual data
@return db id of the programme or -1 if not exists
*/
long iptvx_db_get_programme_id(int chanid, programme* prog){
long result = -1;

/* prepare the sql statement */
char* sql = sqlite3_mprintf("SELECT programmeid FROM programme "
"WHERE programmetitle = '%q' "
"AND programmestart = %lld "
"AND programmestop = %lld "
"AND programmechannelid = %lld",
prog->title->str, prog->start,
prog->stop, chanid);

sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
while (sqlite3_step(stmt) != SQLITE_DONE) {
int i;
int column_count = sqlite3_column_count(stmt);
for(i = 0; i < column_count; i++){
result = sqlite3_column_int64(stmt, i);
}
}

return result;
}

/*
Gets the id of a category by it's name.
If it doesn't exist, it'll be created
@param category_name name of the category to get id for
@return id of the category
*/
long iptvx_db_get_category_id(char* category_name){
long result = -1;

if(category_name != NULL && strlen(category_name) > 0){
/* prepare the sql statement */
char* sql = sqlite3_mprintf("SELECT categoryid FROM category "
"WHERE categoryname = '%q'", category_name);

sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
while (sqlite3_step(stmt) != SQLITE_DONE) {
int i;
int column_count = sqlite3_column_count(stmt);
for(i = 0; i < column_count; i++){
result = sqlite3_column_int64(stmt, i);
}
}

if(result == -1){
char* insert_sql = sqlite3_mprintf("INSERT INTO category "
"(categoryname) VALUES ('%q')", category_name);
sqlite3_exec(db,insert_sql,NULL,NULL,NULL);
sqlite3_free(insert_sql);
result = sqlite3_last_insert_rowid(db);
}
}

return result;
}

/*
Inserts a channel's programme into the db if it does not yet exist
@param chanid db id of the channel to add programme to
@param prog programme struct with the programme
*/
void iptvx_db_insert_programme(int chanid, programme* prog){
/* check if the programme is already in the database */
long exist_prog_id = iptvx_db_get_programme_id(chanid,prog);

/* get the category id from the database */
long exist_category_id = iptvx_db_get_category_id(prog->category->str);

if(exist_prog_id >= 0){
/* programme already exists in db, just update it */
char* update_sql = sqlite3_mprintf("UPDATE programme "
"SET programmedescription = '%q', "
"programmecategoryid = %lld, "
"programmeproductiondate = %lld "
"WHERE programmeid = %lld",
prog->description->str, exist_category_id,
prog->productionDate, exist_prog_id);
sqlite3_exec(db,update_sql,NULL,NULL,NULL);
sqlite3_free(update_sql);
}else{
/* the programme is not in the database, insert it */
char* insert_sql = sqlite3_mprintf(
"INSERT INTO programme (programmetitle, programmedescription, "
"programmecategoryid, programmestart, programmestop, "
"programmeproductiondate, programmechannelid) VALUES "
"('%q','%q',%lld,%lld,%lld,%lld,%lld)",
prog->title->str, prog->description->str,
exist_category_id, prog->start, prog->stop,
prog->productionDate, chanid);
sqlite3_exec(db,insert_sql,NULL,NULL,NULL);
sqlite3_free(insert_sql);
}
}

/*
Inserts a channel into the db if it doesn't exist
@param chan the channel struct
Expand Down Expand Up @@ -78,6 +184,25 @@ void iptvx_db_insert_channel(channel* chan){
sqlite3_free(insert_sql);
}

/* get the id of the channel */
int channelid = -1;
char* get_id_sql = sqlite3_mprintf("SELECT channelid FROM channel "
"WHERE channelname = '%q'",chan->name->str);
sqlite3_stmt *getid_stmt;
sqlite3_prepare_v2(db,get_id_sql, -1, &getid_stmt, NULL);
while(sqlite3_step(getid_stmt) != SQLITE_DONE){
channelid = sqlite3_column_int64(getid_stmt,0);
}

/* make sure the channel exists and has an id */
if(channelid != -1){
int p = 0;
for(p = 0;p < chan->programmeList->len; p++){
programme* prog = &g_array_index(chan->programmeList,programme,p);
iptvx_db_insert_programme(channelid,prog);
}
}

/* finalise statement */
sqlite3_finalize(stmt);
sqlite3_free(sql);
Expand Down

0 comments on commit 61f1ae0

Please sign in to comment.