Diff for /mqtt/src/pubmqtt.c between versions 1.1.2.7 and 1.1.2.8

version 1.1.2.7, 2011/11/24 16:00:31 version 1.1.2.8, 2011/11/25 15:29:30
Line 85  mqtt_rtlm_init_session(sl_config *cfg, sqlite3 *sql, c Line 85  mqtt_rtlm_init_session(sl_config *cfg, sqlite3 *sql, c
   
         str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online"));          str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online"));
         if (!str) {          if (!str) {
                mqtt_rtlm_log("Error:: not found topics table name");                mqtt_rtlm_log("Error:: not found online table name");
                 return -1;                  return -1;
         }          }
         snprintf(szStmt, sizeof szStmt, "INSERT INTO %s (Username, RemoteHost, RemotePort) "          snprintf(szStmt, sizeof szStmt, "INSERT INTO %s (Username, RemoteHost, RemotePort) "
Line 128  mqtt_rtlm_fini_session(sl_config *cfg, sqlite3 *sql, c Line 128  mqtt_rtlm_fini_session(sl_config *cfg, sqlite3 *sql, c
   
         str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online"));          str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online"));
         if (!str) {          if (!str) {
                mqtt_rtlm_log("Error:: not found topics table name");                mqtt_rtlm_log("Error:: not found online table name");
                 return -1;                  return -1;
         }          }
         snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE Username = '%s' AND RemoteHost LIKE '%s';",           snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE Username = '%s' AND RemoteHost LIKE '%s';", 
Line 171  mqtt_rtlm_chk_session(sl_config *cfg, sqlite3 *sql, co Line 171  mqtt_rtlm_chk_session(sl_config *cfg, sqlite3 *sql, co
   
         str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online"));          str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online"));
         if (!str) {          if (!str) {
                mqtt_rtlm_log("Error:: not found topics table name");                mqtt_rtlm_log("Error:: not found online table name");
                 return -1;                  return -1;
         }          }
         snprintf(szStmt, sizeof szStmt, "SELECT RemoteHost, RemotePort FROM %s WHERE "          snprintf(szStmt, sizeof szStmt, "SELECT RemoteHost, RemotePort FROM %s WHERE "
Line 185  mqtt_rtlm_chk_session(sl_config *cfg, sqlite3 *sql, co Line 185  mqtt_rtlm_chk_session(sl_config *cfg, sqlite3 *sql, co
                 ret = sqlite3_changes(sql);                  ret = sqlite3_changes(sql);
         else          else
                 ret = 0;                  ret = 0;
           sqlite3_finalize(stmt);
   
           return ret;
   }
   
   /*
    * mqtt_rtlm_write_topic() Publish topic
    *
    * @cfg = loaded config
    * @sql = SQL handle
    * @topic = topic
    * @txt = text
    * @user = username
    * @host = hostname
    * @retain = !=0 retain message to database
    * return: -1 error, 0 no publish or >0 published ok
    */
   int
   mqtt_rtlm_write_topic(sl_config *cfg, sqlite3 *sql, const char *topic, const char *txt, 
                   const char *user, const char *host, char retain)
   {
           int ret = 0;
           char *str, szStmt[BUFSIZ] = { 0 };
           sqlite3_stmt *stmt;
   
           if (!cfg || !sql || !topic)
                   return -1;
   
           str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_topics"));
           if (!str) {
                   mqtt_rtlm_log("Error:: not found topics table name");
                   return -1;
           }
           snprintf(szStmt, sizeof szStmt, "INSERT INTO %s (Retain, Topic, Value, PubUser, PubDate, PubHost) "
                           "VALUES (%d, '%s', '%s', '%s', datetime('now', 'localtime'), '%s');", str, 
                           retain, topic, txt, user, host);
   
           if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) {
                   MQTT_RTLM_LOG(sql);
                   return -1;
           }
           if ((ret = sqlite3_step(stmt)) == SQLITE_DONE)
                   ret = sqlite3_changes(sql);
           else {
                   if (ret > SQLITE_OK && ret < SQLITE_ROW)
                           MQTT_RTLM_LOG(sql);
                   ret = 0;
           }
           sqlite3_finalize(stmt);
   
           return ret;
   }
   
   /*
    * mqtt_rtlm_delete_topic() Delete topic
    *
    * @cfg = loaded config
    * @sql = SQL handle
    * @topic = topic
    * @user = username
    * @host = hostname
    * @retain = -1 no matter
    * return: -1 error, 0 no changes or >0 deleted rows
    */
   int
   mqtt_rtlm_delete_topic(sl_config *cfg, sqlite3 *sql, const char *topic, 
                   const char *user, const char *host, char retain)
   {
           int ret = 0;
           char *str, *rtn, szStmt[BUFSIZ] = { 0 };
           sqlite3_stmt *stmt;
   
           if (!cfg || !sql || !topic)
                   return -1;
   
           str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_topics"));
           if (!str) {
                   mqtt_rtlm_log("Error:: not found topics table name");
                   return -1;
           }
           switch (retain) {
                   case -1:
                           rtn = "";
                           break;
                   case 0:
                           rtn = "AND Retain = 0";
                           break;
                   default:
                           rtn = "AND Retain != 0";
                           break;
           }
           snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE Topic LIKE '%s' AND "
                           "PubUser LIKE '%s' AND PubHost LIKE '%s' %s;", str, 
                           topic, user, host, rtn);
   
           if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) {
                   MQTT_RTLM_LOG(sql);
                   return -1;
           }
           if ((ret = sqlite3_step(stmt)) == SQLITE_DONE)
                   ret = sqlite3_changes(sql);
           else {
                   if (ret > SQLITE_OK && ret < SQLITE_ROW)
                           MQTT_RTLM_LOG(sql);
                   ret = 0;
           }
         sqlite3_finalize(stmt);          sqlite3_finalize(stmt);
   
         return ret;          return ret;

Removed from v.1.1.2.7  
changed lines
  Added in v.1.1.2.8


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>