--- mqtt/src/pubmqtt.c 2011/11/24 15:34:18 1.1.2.6 +++ mqtt/src/pubmqtt.c 2011/11/28 22:31:19 1.1.2.13 @@ -1,6 +1,9 @@ #include "global.h" +extern const char sql_schema[]; + + /* * mqtt_db_log() Log database connection message * @@ -36,18 +39,25 @@ mqtt_rtlm_open(sl_config *cfg) if (!cfg) return NULL; + sqlite3_config(SQLITE_CONFIG_SERIALIZED); + str = (const char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("name")); if (!str) { mqtt_rtlm_log("Error:: Unknown database name ...\n"); return NULL; } - if (sqlite3_open_v2(str, &sql, SQLITE_OPEN_READWRITE, NULL)) { + if (sqlite3_open_v2(str, &sql, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) { MQTT_RTLM_LOG(sql); sqlite3_close(sql); return NULL; } + if (sqlite3_exec(sql, sql_schema, NULL, NULL, NULL)) { + MQTT_RTLM_LOG(sql); + sqlite3_close(sql); + return NULL; + } return sql; } @@ -85,7 +95,7 @@ mqtt_rtlm_init_session(sl_config *cfg, sqlite3 *sql, c str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online")); if (!str) { - mqtt_rtlm_log("Error:: not found topics table name"); + mqtt_rtlm_log("Error:: not found online table name"); return -1; } snprintf(szStmt, sizeof szStmt, "INSERT INTO %s (Username, RemoteHost, RemotePort) " @@ -128,7 +138,7 @@ mqtt_rtlm_fini_session(sl_config *cfg, sqlite3 *sql, c str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online")); if (!str) { - mqtt_rtlm_log("Error:: not found topics table name"); + mqtt_rtlm_log("Error:: not found online table name"); return -1; } snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE Username = '%s' AND RemoteHost LIKE '%s';", @@ -171,7 +181,7 @@ mqtt_rtlm_chk_session(sl_config *cfg, sqlite3 *sql, co str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_online")); if (!str) { - mqtt_rtlm_log("Error:: not found topics table name"); + mqtt_rtlm_log("Error:: not found online table name"); return -1; } snprintf(szStmt, sizeof szStmt, "SELECT RemoteHost, RemotePort FROM %s WHERE " @@ -181,9 +191,187 @@ mqtt_rtlm_chk_session(sl_config *cfg, sqlite3 *sql, co MQTT_RTLM_LOG(sql); return -1; } - while (sqlite3_step(stmt) == SQLITE_ROW); - ret = sqlite3_changes(sql); + if (sqlite3_step(stmt) == SQLITE_ROW) + ret = sqlite3_changes(sql); + else + 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); + + return ret; +} + +/* + * mqtt_rtlm_read_topic() Get topic + * + * @cfg = loaded config + * @sql = SQL handle + * @topic = topic + * @retain = retain 0 get only dynamic, >0 get only retained and -1 no matter + * return: NULL error or not found and !=NULL allocated subscribe topics + */ +mqtt_subscr_t * +mqtt_rtlm_read_topic(sl_config *cfg, sqlite3 *sql, const char *topic, char retain) +{ + int rowz = 0; + char *str, szStr[STRSIZ], szStmt[BUFSIZ] = { 0 }; + sqlite3_stmt *stmt; + register int j; + mqtt_subscr_t *s = NULL; + + if (!cfg || !sql || !topic) + return NULL; + + switch (retain) { + case -1: + memset(szStr, 0, sizeof szStr); + break; + case 0: + snprintf(szStr, sizeof szStr, "AND Retain = 0"); + break; + default: + snprintf(szStr, sizeof szStr, "AND Retain > 0"); + break; + } + + str = (char*) cfg_GetAttribute(cfg, CFG("mqtt_pub"), CFG("tbl_topics")); + if (!str) { + mqtt_rtlm_log("Error:: not found topics table name"); + return NULL; + } + snprintf(szStmt, sizeof szStmt, "SELECT Retain, Topic, Value FROM %s WHERE Topic LIKE '%s' %s;", + str, topic, szStr); + + if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { + MQTT_RTLM_LOG(sql); + return NULL; + } + + /* calculate count of rows and allocate subscribe items */ + while (sqlite3_step(stmt) == SQLITE_ROW) + rowz++; + if (!(s = malloc((rowz + 1) * sizeof(mqtt_subscr_t)))) { + mqtt_rtlm_log("Error:: System #%d - %s", errno, strerror(errno)); + goto end; + } else + memset(s, 0, (rowz + 1) * sizeof(mqtt_subscr_t)); + sqlite3_reset(stmt); + + /* fill with data */ + for (j = 0; j < rowz && sqlite3_step(stmt) == SQLITE_ROW; j++) { + s[j].sub_ret = (char) sqlite3_column_int(stmt, 0); + s[j].sub_topic._base = strdup(sqlite3_column_text(stmt, 1)); + s[j].sub_topic._size = strlen(s[j].sub_topic._base); + s[j].sub_value._base = strdup(sqlite3_column_text(stmt, 2)); + s[j].sub_value._size = strlen(s[j].sub_value._base); + } +end: + sqlite3_finalize(stmt); + + return s; }