# HG changeset patch # User melunko # Date 1176930081 -3600 # Node ID 0200f97ac28942448bbf10dca24fa02f1a5caac5 # Parent 25f194cfa60b2fde6779650ad56c879daa723624 [svn r572] Fixed add scheduling call. Now we query the station information before adding the schedule, and handle the mysqlquery auto-increment. Added the function gmyth_uri_to_string(). Added the function gmyth_query_process_statement_with_increment() diff -r 25f194cfa60b -r 0200f97ac289 gmyth/src/gmyth_query.c --- a/gmyth/src/gmyth_query.c Wed Apr 18 16:21:38 2007 +0100 +++ b/gmyth/src/gmyth_query.c Wed Apr 18 22:01:21 2007 +0100 @@ -233,4 +233,25 @@ return mysql_store_result (gmyth_query->conn); } +MYSQL_RES* +gmyth_query_process_statement_with_increment (GMythQuery *gmyth_query, char *stmt_str, gulong *id) +{ + assert(gmyth_query); + + gmyth_debug ("[%s] Running mysql query %s", __FUNCTION__, stmt_str); + if (gmyth_query == NULL) + return NULL; + + /* the statement failed */ + if (mysql_query (gmyth_query->conn, stmt_str) != 0) { + gmyth_query_print_error (gmyth_query->conn, "Could not execute statement"); + return NULL; + } + + *id = (my_ulonglong) mysql_insert_id (gmyth_query->conn); + + /* the statement succeeded; determine whether it returned data */ + return mysql_store_result (gmyth_query->conn); +} + diff -r 25f194cfa60b -r 0200f97ac289 gmyth/src/gmyth_query.h --- a/gmyth/src/gmyth_query.h Wed Apr 18 16:21:38 2007 +0100 +++ b/gmyth/src/gmyth_query.h Wed Apr 18 22:01:21 2007 +0100 @@ -73,6 +73,8 @@ GMythQuery* gmyth_query_new (void); MYSQL_RES * gmyth_query_process_statement (GMythQuery *gmyth_query, gchar *stmt_str); +MYSQL_RES* +gmyth_query_process_statement_with_increment (GMythQuery *gmyth_query, char *stmt_str, gulong *id); gboolean gmyth_query_connect (GMythQuery *gmyth_query, GMythBackendInfo *backend_info); diff -r 25f194cfa60b -r 0200f97ac289 gmyth/src/gmyth_scheduler.c --- a/gmyth/src/gmyth_scheduler.c Wed Apr 18 16:21:38 2007 +0100 +++ b/gmyth/src/gmyth_scheduler.c Wed Apr 18 22:01:21 2007 +0100 @@ -47,7 +47,7 @@ static void gmyth_scheduler_finalize (GObject *object); static gint get_record_id_from_database (GMythScheduler *scheduler); -static void update_backend (GMythScheduler *scheduler, gint record_id); +static gboolean update_backend (GMythScheduler *scheduler, gint record_id); G_DEFINE_TYPE(GMythScheduler, gmyth_scheduler, G_TYPE_OBJECT) @@ -326,6 +326,23 @@ return (*recorded_list == NULL) ? 0 : g_list_length (*recorded_list); } +static void +_set_value (GMythQuery *myth_query, char* field, gchar* value, gint rec_id) +{ + gchar* query = g_strdup_printf ("UPDATE record SET recordid = %d, %s = \"%s\" WHERE recordid = %d;", rec_id, field, value, rec_id); + + gmyth_query_process_statement (myth_query, query); + g_free (query); +} + +static void +_set_int_value (GMythQuery *myth_query, char* field, gint value, gint rec_id) +{ + gchar *str_value = g_strdup_printf ("%d", value); + _set_value (myth_query, field, str_value, rec_id); + g_free (str_value); +} + /** Requests the Mysql database in the backend to add a new schedule. * * @param scheduler the GMythScheduler instance. @@ -338,11 +355,10 @@ gmyth_scheduler_add_schedule (GMythScheduler *scheduler, ScheduleInfo *schedule_info) { - //GTimeVal *start_tm; - //GTimeVal *end_tm; - MYSQL_RES *msql_res; - GString *query_str = g_string_new (""); + gchar *query_str = "INSERT record (recordid) VALUE (0);"; + gchar *station = NULL; + gulong rec_id; assert(scheduler); @@ -350,60 +366,67 @@ g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__); return FALSE; } - - //TODO: verify if this funtion realy does what it should do! - g_string_printf (query_str, "REPLACE INTO record " - "(recordid, type, chanid, starttime, " - "startdate, endtime, enddate, title," - "profile, recpriority, maxnewest, inactive, " - "maxepisodes, autoexpire, startoffset, endoffset, " - "recgroup, dupmethod, dupin, station, " - "autocommflag, findday, findtime, findid, " - "search, autotranscode, transcoder, tsdefault, " - "autouserjob1, autouserjob2, autouserjob3, autouserjob4) " - " values ( %d, 1, %d, \"%s\"," //recordid, type, chanid, starttime - " \"%s\", \"%s\", \"%s\", \"%s\"," //startdate, endtime, enddate, title - "DEFAULT, 0, 0, 0, " //profile, recpriority, maxnewest, inactive - "0, 1, 0, 0, " //maxepisodes, autoexpire, startoffset, endoffset - "DEFAULT, 6, 15, %d, " //recgroup, dupmethod, dupin, station - "1, %d, \"%s\", %d, " //autocommflag, findday, findtime, findid - "5, 0, 29, 1, " //search, autotranscode, transcoder, tsdefault - "0, 0, 0, 0 );", //autouserjob1, autouserjob2, autouserjob3, autouserjob4 - schedule_info->record_id, /* record id */ - schedule_info->channel_id, /* channel id */ - gmyth_util_time_to_string_only_time( schedule_info->start_time ), /* start time */ - gmyth_util_time_to_string_only_date( schedule_info->start_time ), /* start date */ - gmyth_util_time_to_string_only_time( schedule_info->end_time ), /* end time */ - gmyth_util_time_to_string_only_date( schedule_info->end_time ), /* end date */ - schedule_info->title->str, /* title */ - schedule_info->channel_id,/* station */ - (gmyth_util_time_val_to_date( schedule_info->start_time ))->tm_wday, //findday - gmyth_util_time_to_string_only_time( schedule_info->start_time ), //findtime - (gint)(schedule_info->start_time->tv_sec/60/60/24 + 719528)//findid - ); + + msql_res = gmyth_query_process_statement_with_increment (scheduler->msqlquery, query_str, &rec_id); + mysql_free_result (msql_res); - gmyth_debug ( "Sending query to MySQL = %s.", query_str->str ); + // Retrieves the station info + query_str = g_strdup_printf ("SELECT callsign FROM channel WHERE chanid = \"%d\";", schedule_info->channel_id); + msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str); + if (msql_res == NULL) { + g_warning ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__); + return FALSE; + } else { + MYSQL_ROW row; + if ((row = mysql_fetch_row (msql_res)) != NULL) { + station = g_strdup (row[0]); + } + } + mysql_free_result (msql_res); + g_free (query_str); - msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str); - - /* FIXME: currently no way to detect db error in UPDATE, REPLACES! - if (msql_res == NULL) { - g_warning ("DB retrieval of recording list failed"); - return -1; - }*/ - - /* TODO: verify record_id = -1 semantics */ - if (schedule_info->record_id <= 0) - schedule_info->record_id = get_record_id_from_database(scheduler); - + // _set_value (field, value, id); + _set_int_value (scheduler->msqlquery, "chanid", schedule_info->channel_id, rec_id); + _set_value (scheduler->msqlquery, "station", station, rec_id); + _set_value (scheduler->msqlquery, "title", schedule_info->title->str, rec_id); + /// subtitle, description + _set_value (scheduler->msqlquery, "starttime", gmyth_util_time_to_string_only_time( schedule_info->start_time), rec_id); + _set_value (scheduler->msqlquery, "startdate", gmyth_util_time_to_string_only_date( schedule_info->start_time), rec_id); + _set_value (scheduler->msqlquery, "endtime", gmyth_util_time_to_string_only_time( schedule_info->end_time), rec_id); + _set_value (scheduler->msqlquery, "enddate", gmyth_util_time_to_string_only_date( schedule_info->end_time), rec_id); + /// category, series id, program id + //_set_value (scheduler->msqlquery, "findday", (gmyth_util_time_val_to_date( schedule_info->start_time ))->tm_wday, rec_id); + //_set_value (scheduler->msqlquery, "findtime", gmyth_util_time_to_string_only_time( schedule_info->start_time), rec_id); + //_set_int_value (scheduler->msqlquery, "findid", (gint)(schedule_info->start_time->tv_sec/60/60/24 + 719528), rec_id); + _set_value (scheduler->msqlquery, "parentid", "0", rec_id); + _set_value (scheduler->msqlquery, "search", "0", rec_id); + _set_value (scheduler->msqlquery, "type", "1", rec_id); + _set_value (scheduler->msqlquery, "recpriority", "0", rec_id); + _set_value (scheduler->msqlquery, "startoffset", "0", rec_id); + _set_value (scheduler->msqlquery, "endoffset", "0", rec_id); + _set_value (scheduler->msqlquery, "dupmethod", "6", rec_id); // ? + _set_value (scheduler->msqlquery, "dupin", "15", rec_id); // ? + + _set_value (scheduler->msqlquery, "prefinput", "0", rec_id); + _set_value (scheduler->msqlquery, "inactive", "0", rec_id); + _set_value (scheduler->msqlquery, "profile", "Default", rec_id); + _set_value (scheduler->msqlquery, "recgroup", "Default", rec_id); + _set_value (scheduler->msqlquery, "storagegroup", "Default", rec_id); + _set_value (scheduler->msqlquery, "playgroup", "Default", rec_id); + _set_value (scheduler->msqlquery, "autoexpire", "1", rec_id); + _set_value (scheduler->msqlquery, "maxepisodes", "0", rec_id); + _set_value (scheduler->msqlquery, "maxnewest", "0", rec_id); + _set_value (scheduler->msqlquery, "autocommflag", "1", rec_id); + _set_value (scheduler->msqlquery, "autotranscode", "0", rec_id); + _set_value (scheduler->msqlquery, "transcoder", "0", rec_id); + + _set_value (scheduler->msqlquery, "autouserjob1", "0", rec_id); + _set_value (scheduler->msqlquery, "autouserjob2", "0", rec_id); + _set_value (scheduler->msqlquery, "autouserjob3", "0", rec_id); + _set_value (scheduler->msqlquery, "autouserjob4", "0", rec_id); + /* Notify the backend of changes */ - update_backend(scheduler, schedule_info->record_id); - - /* free allocated memory */ - mysql_free_result (msql_res); - g_string_free(query_str, TRUE); - - return 1; + return update_backend(scheduler, rec_id); } /** Requests the Mysql database in the backend to remove an existing schedule. @@ -432,12 +455,12 @@ msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str); - update_backend(scheduler, record_id);// Notify the backend of the changes mysql_free_result (msql_res); g_string_free(query_str, TRUE); - - return TRUE; + + // Notify the backend of the changes + return update_backend(scheduler, record_id); } /** Requests the Mysql database in the backend to remove an existing recorded item. @@ -468,13 +491,12 @@ // FIXME: Mythtv implementation runs also: DELETE FROM oldfind WHERE recordid = x msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str); - - update_backend(scheduler, record_id);// Notify the backend of the changes mysql_free_result (msql_res); g_string_free(query_str, TRUE); - - return TRUE; + + // Notify the backend of the changes + return update_backend(scheduler, record_id); } /** Retrieves an existing recorded item information from database. The information @@ -604,12 +626,13 @@ * * @param record_id the id of the modified recording. */ -static void +static gboolean update_backend(GMythScheduler *scheduler, gint record_id)//fixme: put void and discovery record_id inside { GMythSocket *socket; GMythStringList *strlist = gmyth_string_list_new (); GString *datastr = g_string_new ("RESCHEDULE_RECORDINGS "); + gboolean ret = FALSE; g_string_append_printf (datastr, "%d", record_id); gmyth_string_list_append_string (strlist, datastr); @@ -617,13 +640,15 @@ socket = gmyth_socket_new (); if (gmyth_socket_connect_to_backend (socket, scheduler->backend_info->hostname, scheduler->backend_info->port, TRUE)) { - gmyth_socket_sendreceive_stringlist (socket, strlist); + ret = (gmyth_socket_sendreceive_stringlist (socket, strlist) > 0); } else { g_warning ("[%s] Connection to backend failed!", __FUNCTION__); } g_string_free(datastr, TRUE); g_object_unref(strlist); + + return ret; } void diff -r 25f194cfa60b -r 0200f97ac289 gmyth/src/gmyth_uri.c --- a/gmyth/src/gmyth_uri.c Wed Apr 18 16:21:38 2007 +0100 +++ b/gmyth/src/gmyth_uri.c Wed Apr 18 22:01:21 2007 +0100 @@ -519,3 +519,12 @@ return ret; } + +char* +gmyth_uri_to_string (const GMythURI* uri) +{ + g_return_val_if_fail (uri != NULL, NULL); + g_return_val_if_fail (uri->uri != NULL, NULL); + + return g_strdup (uri->uri->str); +} diff -r 25f194cfa60b -r 0200f97ac289 gmyth/src/gmyth_uri.h --- a/gmyth/src/gmyth_uri.h Wed Apr 18 16:21:38 2007 +0100 +++ b/gmyth/src/gmyth_uri.h Wed Apr 18 22:01:21 2007 +0100 @@ -110,6 +110,9 @@ gint gmyth_uri_get_channel_num( GMythURI* uri ); gchar* gmyth_uri_get_channel_name( GMythURI* uri ); +char* gmyth_uri_to_string (const GMythURI* uri); + + gboolean gmyth_uri_is_local_file( const GMythURI* uri ); #define gmyth_uri_get_host(urip) ( urip->host != NULL ? urip->host->str : "" )