]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mysql_selector.c
stat_client_write(): add help text for new 'itemnum' parameter
[paraslash.git] / mysql_selector.c
index 85d16ee200dc9c7bf032aac790894dabee5a710f..a53632c524c8be7bdc05d91d2365deb9ff00289e 100644 (file)
@@ -38,7 +38,6 @@ extern struct misc_meta_data *mmd;
 
 static void *mysql_ptr = NULL;
 
 
 static void *mysql_ptr = NULL;
 
-
 static int com_cam(int, int, char **);
 static int com_cdb(int, int, char **);
 static int com_cs(int, int, char **);
 static int com_cam(int, int, char **);
 static int com_cdb(int, int, char **);
 static int com_cs(int, int, char **);
@@ -93,7 +92,7 @@ static struct server_command cmds[] = {
 
 "\tCreate database name containing the initial columns for basic\n"
 "\tinteroperation with server. This command has to be used only once\n"
 
 "\tCreate database name containing the initial columns for basic\n"
 "\tinteroperation with server. This command has to be used only once\n"
-"\twhen you use the mysql database tool for the very first time.\n"
+"\twhen you use the mysql audio file selector for the very first time.\n"
 "\n"
 "\tThe optional name defaults to 'paraslash' if not given.\n"
 
 "\n"
 "\tThe optional name defaults to 'paraslash' if not given.\n"
 
@@ -212,7 +211,7 @@ static struct server_command cmds[] = {
 .help =
 
 "\tIf pattern was not given, print list of all audio files known\n"
 .help =
 
 "\tIf pattern was not given, print list of all audio files known\n"
-"\tto the mysql database tool. See the documentation of mysql\n"
+"\tto the mysql selector. See the documentation of mysql\n"
 "\tfor the definition of LIKE patterns.\n"
 
 },
 "\tfor the definition of LIKE patterns.\n"
 
 },
@@ -600,7 +599,7 @@ static struct para_macro macro_list[] = {
        }
 };
 
        }
 };
 
-static int real_query(char *query)
+static int real_query(const char *query)
 {
        if (!mysql_ptr)
                return -E_NOTCONN;
 {
        if (!mysql_ptr)
                return -E_NOTCONN;
@@ -617,7 +616,7 @@ static int real_query(char *query)
  * Use open connection given by mysql_ptr to query server. Returns a
  * result pointer on succes and NULL on errors
  */
  * Use open connection given by mysql_ptr to query server. Returns a
  * result pointer on succes and NULL on errors
  */
-static struct MYSQL_RES *get_result(char *query)
+static struct MYSQL_RES *get_result(const char *query)
 {
        void *result;
 
 {
        void *result;
 
@@ -810,7 +809,7 @@ static int com_verb(int fd, int argc, char *argv[])
 
        if (argc < 1)
                return -E_MYSQL_SYNTAX;
 
        if (argc < 1)
                return -E_MYSQL_SYNTAX;
-        result = get_result(argv[1]);
+       result = get_result(argv[1]);
        if (!result)
                /* return success, because it's ok to have no results */
                return 1;
        if (!result)
                /* return success, because it's ok to have no results */
                return 1;
@@ -837,7 +836,7 @@ static void *get_all_attributes(void)
                mysql_free_result(result);
                return NULL;
        }
                mysql_free_result(result);
                return NULL;
        }
-        mysql_data_seek(result, 4); /* skip Lastplayed, Numplayed... */
+       mysql_data_seek(result, 4); /* skip Lastplayed, Numplayed... */
        return result;
 }
 
        return result;
 }
 
@@ -871,9 +870,9 @@ static int com_hist(int fd, int argc, char *argv[]) {
        q = make_message("select name, to_days(now()) - to_days(lastplayed) from "
                "data%s%s%s order by lastplayed",
                (argc < 1)? "" : " where ",
        q = make_message("select name, to_days(now()) - to_days(lastplayed) from "
                "data%s%s%s order by lastplayed",
                (argc < 1)? "" : " where ",
-               (argc < 1)? "" : argv[1], 
+               (argc < 1)? "" : argv[1],
                (argc < 1)? "" : " = '1'");
                (argc < 1)? "" : " = '1'");
-        result = get_result(q);
+       result = get_result(q);
        free(q);
        if (!result)
                return -E_NORESULT;
        free(q);
        if (!result)
                return -E_NORESULT;
@@ -951,7 +950,7 @@ static int com_mbox(int fd, int argc, char *argv[])
        }
        mysql_free_result(result);
        ret = -E_NORESULT;
        }
        mysql_free_result(result);
        ret = -E_NORESULT;
-        result = get_result(query);
+       result = get_result(query);
        if (!result)
                goto out;
        ret = -E_EMPTY_RESULT;
        if (!result)
                goto out;
        ret = -E_EMPTY_RESULT;
@@ -998,7 +997,7 @@ static char *get_atts(char *name, int verbose)
        num_fields = mysql_num_fields(result);
        if (num_fields < 5)
                goto out;
        num_fields = mysql_num_fields(result);
        if (num_fields < 5)
                goto out;
-        mysql_data_seek(result2, 4); /* skip Lastplayed, Numplayed... */
+       mysql_data_seek(result2, 4); /* skip Lastplayed, Numplayed... */
        row = mysql_fetch_row(result);
        ret = -E_NOROW;
        if (!row)
        row = mysql_fetch_row(result);
        ret = -E_NOROW;
        if (!row)
@@ -1010,11 +1009,11 @@ static char *get_atts(char *name, int verbose)
                        goto out;
                if (atts && (verbose || is_set))
                        atts = para_strcat(atts, verbose? "," : " ");
                        goto out;
                if (atts && (verbose || is_set))
                        atts = para_strcat(atts, verbose? "," : " ");
-                if (is_set || verbose)
+               if (is_set || verbose)
                        atts = para_strcat(atts, row2[0]);
                if (verbose)
                        atts = para_strcat(atts, is_set? "=\"1\"" : "=\"0\"");
                        atts = para_strcat(atts, row2[0]);
                if (verbose)
                        atts = para_strcat(atts, is_set? "=\"1\"" : "=\"0\"");
-        }
+       }
        ret = 1;
 out:
        if (result2)
        ret = 1;
 out:
        if (result2)
@@ -1048,7 +1047,7 @@ static char *get_meta(char *name, int verbose)
                goto out;
        q = make_message(verbose? verbose_fmt : fmt, ebn);
        free(ebn);
                goto out;
        q = make_message(verbose? verbose_fmt : fmt, ebn);
        free(ebn);
-        result = get_result(q);
+       result = get_result(q);
        free(q);
        if (!result)
                goto out;
        free(q);
        if (!result)
                goto out;
@@ -1187,7 +1186,7 @@ static char *get_query(char *streamname, char *filename, int with_path)
                        free(tmp2);
                        continue;
                }
                        free(tmp2);
                        continue;
                }
-               if (!strcmp(command, "score:"))
+               if (!score && !strcmp(command, "score:"))
                        score = s_a_r_list(macro_list, arg);
        }
        if (!score) {
                        score = s_a_r_list(macro_list, arg);
        }
        if (!score) {
@@ -1207,10 +1206,10 @@ static char *get_query(char *streamname, char *filename, int with_path)
                goto write_query;
        }
        select_clause = para_strdup(with_path?
                goto write_query;
        }
        select_clause = para_strdup(with_path?
-                "select concat(dir.dir, '/', dir.name) from data, dir "
-                "where dir.name = data.name "
-                :
-                "select name from data where name is not NULL");
+               "select concat(dir.dir, '/', dir.name) from data, dir "
+               "where dir.name = data.name "
+               :
+               "select name from data where name is not NULL");
        order = make_message("order by -(%s)", score);
        free(score);
        if (accept_opts && deny_opts) {
        order = make_message("order by -(%s)", score);
        free(score);
        if (accept_opts && deny_opts) {
@@ -1248,7 +1247,7 @@ out:
  * This is called from server and from some commands. Name must not be NULL
  * Never returns NULL.
  */
  * This is called from server and from some commands. Name must not be NULL
  * Never returns NULL.
  */
-static char *get_dbinfo(char *name)
+static char *get_selector_info(char *name)
 {
        char *meta = NULL, *atts = NULL, *info, *dir = NULL, *query, *stream = NULL;
        void *result = NULL;
 {
        char *meta = NULL, *atts = NULL, *info, *dir = NULL, *query, *stream = NULL;
        void *result = NULL;
@@ -1338,7 +1337,7 @@ out:
                free(name);
        return ret;
 }
                free(name);
        return ret;
 }
-static int change_stream(char *stream)
+static int change_stream(const char *stream)
 {
        char *query;
        int ret;
 {
        char *query;
        int ret;
@@ -1517,7 +1516,7 @@ static int com_set(__unused int fd, int argc, char *argv[])
        char *q, *ebn;
        long unsigned id;
        int i, ret;
        char *q, *ebn;
        long unsigned id;
        int i, ret;
-       char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id";
+       const char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id";
 
        if (argc < 2)
                return -E_MYSQL_SYNTAX;
 
        if (argc < 2)
                return -E_MYSQL_SYNTAX;
@@ -1696,7 +1695,7 @@ static int com_ls(int fd, int argc, char *argv[])
                        argv[1]);
        else
                q = para_strdup("select name from data");
                        argv[1]);
        else
                q = para_strdup("select name from data");
-        result = get_result(q);
+       result = get_result(q);
        free(q);
        if (!result)
                return -E_NORESULT;
        free(q);
        if (!result)
                return -E_NORESULT;
@@ -1761,7 +1760,7 @@ static int get_numplayed(char *name)
        char *buf = make_message(fmt, name);
        int ret = -E_NORESULT;
 
        char *buf = make_message(fmt, name);
        int ret = -E_NORESULT;
 
-        result = get_result(buf);
+       result = get_result(buf);
        free(buf);
        if (!result)
                goto out;
        free(buf);
        if (!result)
                goto out;
@@ -1807,14 +1806,14 @@ out:
 static void update_mmd(char *info)
 {
        PARA_DEBUG_LOG("%s", "updating shared memory area\n");
 static void update_mmd(char *info)
 {
        PARA_DEBUG_LOG("%s", "updating shared memory area\n");
-       strncpy(mmd->dbinfo, info, MMD_INFO_SIZE - 1);
-       mmd->dbinfo[MMD_INFO_SIZE - 1] = '\0';
+       strncpy(mmd->selector_info, info, MMD_INFO_SIZE - 1);
+       mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
 }
 
 static void update_audio_file_server_handler(char *name)
 {
        char *info;
 }
 
 static void update_audio_file_server_handler(char *name)
 {
        char *info;
-       info = get_dbinfo(name);
+       info = get_selector_info(name);
        update_mmd(info);
        free(info);
        update_audio_file(name);
        update_mmd(info);
        free(info);
        update_audio_file(name);
@@ -1827,14 +1826,14 @@ static int com_us(__unused int fd, int argc, char *argv[])
        return update_audio_file(argv[1]);
 }
 
        return update_audio_file(argv[1]);
 }
 
-static void refresh_mmd_dbinfo(void)
+static void refresh_selector_info(void)
 {
        char *name = get_current_audio_file();
        char *info;
 
        if (!name)
                return;
 {
        char *name = get_current_audio_file();
        char *info;
 
        if (!name)
                return;
-       info = get_dbinfo(name);
+       info = get_selector_info(name);
        free(name);
        mmd_lock();
        update_mmd(info);
        free(name);
        mmd_lock();
        update_mmd(info);
@@ -1880,7 +1879,7 @@ static int com_ps(__unused int fd, int argc, char *argv[])
        else
                i = match < num_rows - 1? match + 1 : 0;
        ret = -E_NOROW;
        else
                i = match < num_rows - 1? match + 1 : 0;
        ret = -E_NOROW;
-        mysql_data_seek(result, i);
+       mysql_data_seek(result, i);
        row = mysql_fetch_row(result);
        if (!row || !row[0])
                goto out;
        row = mysql_fetch_row(result);
        if (!row || !row[0])
                goto out;
@@ -1901,7 +1900,7 @@ static int com_ps(__unused int fd, int argc, char *argv[])
                "'current_stream'", row[0]);
        ret = real_query(query);
        free(query);
                "'current_stream'", row[0]);
        ret = real_query(query);
        free(query);
-       refresh_mmd_dbinfo();
+       refresh_selector_info();
 out:
        free(stream);
        if (result)
 out:
        free(stream);
        if (result)
@@ -2009,7 +2008,7 @@ static int com_cs(int fd, int argc, char *argv[])
                ret = change_stream(argv[1]);
                if (ret < 0)
                        goto out;
                ret = change_stream(argv[1]);
                if (ret < 0)
                        goto out;
-               refresh_mmd_dbinfo();
+               refresh_selector_info();
        }
        if (csp) {
                mmd_lock();
        }
        if (csp) {
                mmd_lock();
@@ -2148,7 +2147,7 @@ no_more_atts:
                for (; argv[i] && ret >= 0; i++)
                        ret = update_atts(fd, argv[i], atts);
        }
                for (; argv[i] && ret >= 0; i++)
                        ret = update_atts(fd, argv[i], atts);
        }
-       refresh_mmd_dbinfo();
+       refresh_selector_info();
 out:
        return ret;
 }
 out:
        return ret;
 }
@@ -2427,7 +2426,7 @@ static int init_mysql_server(void)
 /* mmd lock must be held */
 static void write_msg2mmd(int success)
 {
 /* mmd lock must be held */
 static void write_msg2mmd(int success)
 {
-       sprintf(mmd->dbinfo, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
+       sprintf(mmd->selector_info, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
                success < 0? PARA_STRERROR(-success) :
                        "successfully connected to mysql server",
                success < 0? "" : mysql_get_server_info(mysql_ptr));
                success < 0? PARA_STRERROR(-success) :
                        "successfully connected to mysql server",
                success < 0? "" : mysql_get_server_info(mysql_ptr));
@@ -2436,17 +2435,9 @@ static void write_msg2mmd(int success)
 /* create database */
 static int com_cdb(int fd, int argc, char *argv[])
 {
 /* create database */
 static int com_cdb(int fd, int argc, char *argv[])
 {
-       char *query, *name;
+       char *query;
        int ret;
 
        int ret;
 
-       if (argc < 1)
-               name = "paraslash";
-       else {
-               ret = -E_NAMETOOLONG;
-               name = argv[1];
-               if (strlen(name) > MAXLINE)
-                       goto out;
-       }
        if (mysql_ptr) {
                PARA_INFO_LOG("%s", "closing database\n");
                mysql_close(mysql_ptr);
        if (mysql_ptr) {
                PARA_INFO_LOG("%s", "closing database\n");
                mysql_close(mysql_ptr);
@@ -2456,14 +2447,15 @@ static int com_cdb(int fd, int argc, char *argv[])
        ret = -E_MYSQL_INIT;
        if (init_mysql_server() < 0 || !mysql_ptr)
                goto out;
        ret = -E_MYSQL_INIT;
        if (init_mysql_server() < 0 || !mysql_ptr)
                goto out;
-       query = make_message("create database %s", name);
+       conf.mysql_database_arg = para_strdup((argc < 1)?
+               "paraslash" : argv[1]);
+       query = make_message("create database %s", conf.mysql_database_arg);
        ret = real_query(query);
        free(query);
        if (ret < 0)
                goto out;
        /* reconnect with database just created */
        mysql_close(mysql_ptr);
        ret = real_query(query);
        free(query);
        if (ret < 0)
                goto out;
        /* reconnect with database just created */
        mysql_close(mysql_ptr);
-       conf.mysql_database_arg = para_strdup(name);
        ret = -E_MYSQL_INIT;
        if (init_mysql_server() < 0 || !mysql_ptr)
                goto out;
        ret = -E_MYSQL_INIT;
        if (init_mysql_server() < 0 || !mysql_ptr)
                goto out;
@@ -2494,7 +2486,8 @@ static int com_cdb(int fd, int argc, char *argv[])
        if (real_query("insert into streams (name, def) values "
                        "('current_stream', '(none)')") < 0)
                goto out;
        if (real_query("insert into streams (name, def) values "
                        "('current_stream', '(none)')") < 0)
                goto out;
-       ret = send_va_buffer(fd, "successfully created database %s\n", name);
+       ret = send_va_buffer(fd, "successfully created database %s\n",
+               conf.mysql_database_arg);
 out:
        return ret;
 }
 out:
        return ret;
 }
@@ -2509,12 +2502,13 @@ static void shutdown_connection(void)
 }
 
 /**
 }
 
 /**
- * the init function of the mysql-based database tool
+ * the init function of the mysql-based audio file selector
  *
  * Check the command line options and initialize all function pointers of \a db.
  *
  * Check the command line options and initialize all function pointers of \a db.
- * Connect to the mysql server and initialize the dbinfo string.
+ * Connect to the mysql server and initialize the info string.
  *
  *
- * \sa struct audio_file_selector, misc_meta_data::dbinfo, random_selector.c
+ * \sa struct audio_file_selector, misc_meta_data::selector_info,
+ * random_selector.c
  */
 int mysql_selector_init(struct audio_file_selector *db)
 {
  */
 int mysql_selector_init(struct audio_file_selector *db)
 {