|
@@ -474,112 +474,6 @@ void sql_close_database(void)
|
|
|
error_report("Error %d while closing the SQLite database, %s", rc, sqlite3_errstr(rc));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-// Expect a UUID in column 0
|
|
|
-// Return 0 and store in store_uuid
|
|
|
-// Return 1 if it fails (store_uuid is left unchanged)
|
|
|
-static int expect_uuid_column0(sqlite3_stmt *res, uuid_t *store_uuid)
|
|
|
-{
|
|
|
- int rc = sqlite3_step_monitored(res);
|
|
|
- if (likely(rc == SQLITE_ROW)) {
|
|
|
- uuid_copy(*store_uuid, *((uuid_t *)sqlite3_column_blob(res, 0)));
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-// Do a database lookup to find the uuid of a dimension
|
|
|
-// Return 0 if uuid is found and store it in *store_uuid
|
|
|
-// 1 if no uuid is found (needs to be created)
|
|
|
-//
|
|
|
-
|
|
|
-#define SQL_FIND_DIMENSION_UUID \
|
|
|
- "SELECT dim_id FROM dimension WHERE chart_id=@chart AND id=@id AND name=@name AND LENGTH(dim_id)=16;"
|
|
|
-int find_dimension_uuid(RRDSET *st, RRDDIM *rd, uuid_t *store_uuid)
|
|
|
-{
|
|
|
- static __thread sqlite3_stmt *res = NULL;
|
|
|
- int rc;
|
|
|
- int status = 1;
|
|
|
-
|
|
|
- if (unlikely(!db_meta) && default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
|
|
|
- return 1;
|
|
|
-
|
|
|
- if (unlikely(!res)) {
|
|
|
- rc = prepare_statement(db_meta, SQL_FIND_DIMENSION_UUID, &res);
|
|
|
- if (rc != SQLITE_OK) {
|
|
|
- error_report("Failed to prepare statement to lookup dimension UUID in the database");
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- rc = sqlite3_bind_blob(res, 1, &st->chart_uuid, sizeof(st->chart_uuid), SQLITE_STATIC);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- goto skip;
|
|
|
-
|
|
|
- rc = sqlite3_bind_text(res, 2, rrddim_id(rd), -1, SQLITE_STATIC);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- goto skip;
|
|
|
-
|
|
|
- rc = sqlite3_bind_text(res, 3, rrddim_name(rd), -1, SQLITE_STATIC);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- goto skip;
|
|
|
-
|
|
|
- status = expect_uuid_column0(res, store_uuid);
|
|
|
-
|
|
|
-skip:
|
|
|
- rc = sqlite3_reset(res);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- error_report("Failed to reset statement find dimension uuid, rc = %d", rc);
|
|
|
- return status;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-/*
|
|
|
- * Do a database lookup to find the UUID of a chart
|
|
|
- *
|
|
|
- */
|
|
|
-
|
|
|
-#define SQL_FIND_CHART_UUID "SELECT chart_id FROM chart WHERE host_id = @host AND type=@type AND id=@id AND chart_id IS NOT NULL;"
|
|
|
-
|
|
|
-int find_chart_uuid(RRDHOST *host, const char *type, const char *id, uuid_t *store_uuid)
|
|
|
-{
|
|
|
- static __thread sqlite3_stmt *res = NULL;
|
|
|
- int rc;
|
|
|
- int status = 1;
|
|
|
-
|
|
|
- if (unlikely(!db_meta) && default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
|
|
|
- return 1;
|
|
|
-
|
|
|
- if (unlikely(!res)) {
|
|
|
- rc = prepare_statement(db_meta, SQL_FIND_CHART_UUID, &res);
|
|
|
- if (rc != SQLITE_OK) {
|
|
|
- error_report("Failed to prepare statement to lookup chart UUID in the database");
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- goto skip;
|
|
|
-
|
|
|
- rc = sqlite3_bind_text(res, 2, type, -1, SQLITE_STATIC);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- goto skip;
|
|
|
-
|
|
|
- rc = sqlite3_bind_text(res, 3, id, -1, SQLITE_STATIC);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- goto skip;
|
|
|
-
|
|
|
- status = expect_uuid_column0(res, store_uuid);
|
|
|
-
|
|
|
-skip:
|
|
|
- rc = sqlite3_reset(res);
|
|
|
- if (unlikely(rc != SQLITE_OK))
|
|
|
- error_report("Failed to reset statement when searching for a chart UUID, rc = %d", rc);
|
|
|
- return status;
|
|
|
-}
|
|
|
-
|
|
|
int exec_statement_with_uuid(const char *sql, uuid_t *uuid)
|
|
|
{
|
|
|
int rc, result = 1;
|