Blame SOURCES/0002-ras-record-make-the-code-more-generic.patch

97e186
From 002238dff53b284c9455554f146176ee8de2de4a Mon Sep 17 00:00:00 2001
97e186
From: Mauro Carvalho Chehab <mchehab@redhat.com>
97e186
Date: Fri, 31 May 2013 12:41:01 -0300
97e186
Subject: [PATCH 02/32] ras-record: make the code more generic
97e186
97e186
Now that we're ready to add more tables to the database, make
97e186
the code that creates and inserts data into the table more
97e186
generic.
97e186
97e186
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
97e186
---
97e186
 ras-record.c |  173 +++++++++++++++++++++++++++++++++++++---------------------
97e186
 1 files changed, 110 insertions(+), 63 deletions(-)
97e186
97e186
diff --git a/ras-record.c b/ras-record.c
97e186
index 8995c9e..3af0791 100644
97e186
--- a/ras-record.c
97e186
+++ b/ras-record.c
97e186
@@ -28,80 +28,128 @@
97e186
 #include "ras-mc-handler.h"
97e186
 #include "ras-logger.h"
97e186
 
97e186
+/* #define DEBUG_SQL 1 */
97e186
+
97e186
 #define SQLITE_RAS_DB RASSTATEDIR "/" RAS_DB_FNAME
97e186
 
97e186
-const char *mc_event_db = " mc_event ";
97e186
-const char *mc_event_db_create_fields = "("
97e186
-		"id INTEGER PRIMARY KEY"
97e186
-		", timestamp TEXT"
97e186
-		", err_count INTEGER"
97e186
-		", err_type TEXT"
97e186
-		", err_msg TEXT"		/* 5 */
97e186
-		", label TEXT"
97e186
-		", mc INTEGER"
97e186
-		", top_layer INTEGER"
97e186
-		", middle_layer INTEGER"
97e186
-		", lower_layer INTEGER"		/* 10 */
97e186
-		", address INTEGER"
97e186
-		", grain INTEGER"
97e186
-		", syndrome INTEGER"
97e186
-		", driver_detail TEXT"		/* 14 */
97e186
-	")";
97e186
-
97e186
-const char *mc_event_db_fields = "("
97e186
-		"id"
97e186
-		", timestamp"
97e186
-		", err_count"
97e186
-		", err_type"
97e186
-		", err_msg"			/* 5 */
97e186
-		", label"
97e186
-		", mc"
97e186
-		", top_layer"
97e186
-		", middle_layer"
97e186
-		", lower_layer"			/* 10 */
97e186
-		", address"
97e186
-		", grain"
97e186
-		", syndrome"
97e186
-		", driver_detail"		/* 14 */
97e186
-	")";
97e186
-
97e186
-#define NUM_MC_EVENT_DB_VALUES	14
97e186
-
97e186
-const char *createdb = "CREATE TABLE IF NOT EXISTS";
97e186
+
97e186
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x)))
97e186
+
97e186
+struct db_fields {
97e186
+	char *name;
97e186
+	char *type;
97e186
+};
97e186
+
97e186
+struct db_table_descriptor {
97e186
+	char			*name;
97e186
+	const struct db_fields	*fields;
97e186
+	size_t			num_fields;
97e186
+};
97e186
+
97e186
+static const struct db_fields mc_event_fields[] = {
97e186
+		{ .name="id",			.type="INTEGER PRIMARY KEY" },
97e186
+		{ .name="timestamp",		.type="TEXT" },
97e186
+		{ .name="err_count",		.type="INTEGER" },
97e186
+		{ .name="err_type",		.type="TEXT" },
97e186
+		{ .name="err_msg",		.type="TEXT" },
97e186
+		{ .name="label",		.type="TEXT" },
97e186
+		{ .name="mc",			.type="INTEGER" },
97e186
+		{ .name="top_layer",		.type="INTEGER" },
97e186
+		{ .name="middle_layer",		.type="INTEGER" },
97e186
+		{ .name="lower_layer",		.type="INTEGER" },
97e186
+		{ .name="address",		.type="INTEGER" },
97e186
+		{ .name="grain",		.type="INTEGER" },
97e186
+		{ .name="syndrome",		.type="INTEGER" },
97e186
+		{ .name="driver_detail",	.type="TEXT" },
97e186
+};
97e186
+
97e186
+static const struct db_table_descriptor mc_event_tab = {
97e186
+	.name = "mc_event",
97e186
+	.fields = mc_event_fields,
97e186
+	.num_fields = ARRAY_SIZE(mc_event_fields),
97e186
+};
97e186
+
97e186
 const char *insertdb = "INSERT INTO";
97e186
 const char *valuesdb = " VALUES ";
97e186
 
97e186
-static int ras_mc_prepare_stmt(struct sqlite3_priv *priv)
97e186
+static int ras_mc_prepare_stmt(struct sqlite3_priv *priv,
97e186
+			       sqlite3_stmt **stmt,
97e186
+			       const struct db_table_descriptor *db_tab)
97e186
+
97e186
 {
97e186
 	int i, rc;
97e186
-	char sql[1024];
97e186
+	char sql[1024], *p = sql, *end = sql + sizeof(sql);
97e186
+	const struct db_fields *field;
97e186
+
97e186
+	p += snprintf(p, end - p, "INSERT INTO %s (",
97e186
+		      db_tab->name);
97e186
+
97e186
+	for (i = 0; i < db_tab->num_fields; i++) {
97e186
+		field = &db_tab->fields[i];
97e186
+		p += snprintf(p, end - p, "%s", field->name);
97e186
+
97e186
+		if (i < db_tab->num_fields - 1)
97e186
+			p += snprintf(p, end - p, ", ");
97e186
+	}
97e186
 
97e186
-	strcpy(sql, insertdb);
97e186
-	strcat(sql, mc_event_db);
97e186
-	strcat(sql, mc_event_db_fields);
97e186
-	strcat(sql, valuesdb);
97e186
+	p += snprintf(p, end - p, ") VALUES ( NULL, ");
97e186
 
97e186
-	strcat(sql, "(NULL, ");	/* Auto-increment field */
97e186
-	for (i = 1; i < NUM_MC_EVENT_DB_VALUES; i++) {
97e186
-		if (i < NUM_MC_EVENT_DB_VALUES - 1)
97e186
+	for (i = 1; i < db_tab->num_fields; i++) {
97e186
+		if (i <  db_tab->num_fields - 1)
97e186
 			strcat(sql, "?, ");
97e186
 		else
97e186
 			strcat(sql, "?)");
97e186
 	}
97e186
 
97e186
-	rc = sqlite3_prepare_v2(priv->db, sql, -1, &priv->stmt, NULL);
97e186
+#ifdef DEBUG_SQL
97e186
+	log(TERM, LOG_INFO, "SQL: %s\n", sql);
97e186
+#endif
97e186
+
97e186
+	rc = sqlite3_prepare_v2(priv->db, sql, -1, stmt, NULL);
97e186
 	if (rc != SQLITE_OK)
97e186
-		log(TERM, LOG_ERR, "Failed to prepare insert db on %s: error = %s\n",
97e186
-		       SQLITE_RAS_DB, sqlite3_errmsg(priv->db));
97e186
+		log(TERM, LOG_ERR,
97e186
+		    "Failed to prepare insert db at table %s (db %s): error = %s\n",
97e186
+		    db_tab->name, SQLITE_RAS_DB, sqlite3_errmsg(priv->db));
97e186
 
97e186
 	return rc;
97e186
 }
97e186
 
97e186
+static int ras_mc_create_table(struct sqlite3_priv *priv,
97e186
+			       const struct db_table_descriptor *db_tab)
97e186
+{
97e186
+	const struct db_fields *field;
97e186
+	char sql[1024], *p = sql, *end = sql + sizeof(sql);
97e186
+	int i,rc;
97e186
+
97e186
+	p += snprintf(p, end - p, "CREATE TABLE IF NOT EXISTS %s (",
97e186
+		      db_tab->name);
97e186
+
97e186
+	for (i = 0; i < db_tab->num_fields; i++) {
97e186
+		field = &db_tab->fields[i];
97e186
+		p += snprintf(p, end - p, "%s %s", field->name, field->type);
97e186
+
97e186
+		if (i < db_tab->num_fields - 1)
97e186
+			p += snprintf(p, end - p, ", ");
97e186
+	}
97e186
+	p += snprintf(p, end - p, ")");
97e186
+
97e186
+#ifdef DEBUG_SQL
97e186
+	log(TERM, LOG_INFO, "SQL: %s\n", sql);
97e186
+#endif
97e186
+
97e186
+	rc = sqlite3_exec(priv->db, sql, NULL, NULL, NULL);
97e186
+	if (rc != SQLITE_OK) {
97e186
+		log(TERM, LOG_ERR,
97e186
+		    "Failed to create table %s on %s: error = %d\n",
97e186
+		    db_tab->name, SQLITE_RAS_DB, rc);
97e186
+	}
97e186
+	return rc;
97e186
+}
97e186
+
97e186
 int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras)
97e186
 {
97e186
 	int rc;
97e186
 	sqlite3 *db;
97e186
-	char sql[1024];
97e186
 	struct sqlite3_priv *priv;
97e186
 
97e186
 	printf("Calling %s()\n", __FUNCTION__);
97e186
@@ -137,27 +185,26 @@ int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras)
97e186
 		free(priv);
97e186
 		return -1;
97e186
 	}
97e186
+	priv->db = db;
97e186
 
97e186
-	strcpy(sql, createdb);
97e186
-	strcat(sql, mc_event_db);
97e186
-	strcat(sql, mc_event_db_create_fields);
97e186
-	rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
97e186
+	rc = ras_mc_create_table(priv, &mc_event_tab);
97e186
 	if (rc != SQLITE_OK) {
97e186
-		log(TERM, LOG_ERR,
97e186
-		    "cpu %u: Failed to create db on %s: error = %d\n",
97e186
-		    cpu, SQLITE_RAS_DB, rc);
97e186
+		sqlite3_close(db);
97e186
 		free(priv);
97e186
 		return -1;
97e186
 	}
97e186
 
97e186
-	priv->db = db;
97e186
-	ras->db_priv = priv;
97e186
-
97e186
-	rc = ras_mc_prepare_stmt(priv);
97e186
-	if (rc == SQLITE_OK)
97e186
+	rc = ras_mc_prepare_stmt(priv, &priv->stmt, &mc_event_tab);
97e186
+	if (rc == SQLITE_OK) {
97e186
 		log(TERM, LOG_INFO,
97e186
 		    "cpu %u: Recording events at %s\n",
97e186
 		    cpu, SQLITE_RAS_DB);
97e186
+		ras->db_priv = priv;
97e186
+	} else {
97e186
+		sqlite3_close(db);
97e186
+		free(priv);
97e186
+		return -1;
97e186
+	}
97e186
 
97e186
 	return 0;
97e186
 }
97e186
-- 
97e186
1.7.1
97e186