diff options
Diffstat (limited to 'src/dbi.c')
-rw-r--r-- | src/dbi.c | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -456,3 +456,64 @@ int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id, return 0; } + +int og_dbi_get_os_id(const struct og_dbi *dbi, const char *os_name, uint32_t *os_id) +{ + const char *msglog; + dbi_result result; + + if (strlen(os_name) == 0) + return 0; + + result = dbi_conn_queryf(dbi->conn, + "SELECT idnombreso FROM nombresos " + "WHERE nombreso = '%s'", os_name); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query OS id (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (dbi_result_next_row(result)) { + *os_id = dbi_result_get_uint(result, "idnombreso"); + dbi_result_free(result); + return 0; + } + + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO nombresos (nombreso) " + "VALUES ('%s') ", os_name); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to insert OS id (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "SELECT idnombreso FROM nombresos " + "WHERE nombreso = '%s'", os_name); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query OS id (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (!dbi_result_next_row(result)) { + dbi_result_free(result); + syslog(LOG_ERR, "failed to query OS id (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + *os_id = dbi_result_get_uint(result, "idnombreso"); + dbi_result_free(result); + + return 0; +} |