[mw-devel] MW3 r1118 - branches/nomud/src

welshbyte at sucs.org welshbyte at sucs.org
Mon Dec 7 17:56:30 GMT 2009


Author: welshbyte
Date: 2009-12-07 17:56:29 +0000 (Mon, 07 Dec 2009)
New Revision: 1118

Modified:
   branches/nomud/src/sqlite.c
   branches/nomud/src/sqlite.h
Log:
Add functions for storing and retrieving room data in sqlite


Modified: branches/nomud/src/sqlite.c
===================================================================
--- branches/nomud/src/sqlite.c	2009-12-06 17:57:58 UTC (rev 1117)
+++ branches/nomud/src/sqlite.c	2009-12-07 17:56:29 UTC (rev 1118)
@@ -354,3 +354,72 @@
 	}
 	db_free(old);
 }
+
+
+char * db_room_get(int room, const char *opt)
+{
+	char path[PATHSIZE];
+	struct db_result *old;
+	char *value;
+	char *out;
+
+	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
+
+	char *query = sqlite3_mprintf("SELECT * from "USERDB_ROOMS" WHERE room=%d AND opt=%Q", room, opt);
+	old  = db_query(path, query, 1);
+	sqlite3_free(query);
+
+	if (old == NULL) return NULL;
+	if (db_numrows(old) < 1) return NULL;
+
+	if ((value = db_item(old, 0, "arg"))==NULL)
+	{
+		db_free(old);
+		return NULL;
+	}
+	out = strdup(value);
+	db_free(old);
+	return out;
+}
+
+void db_room_set(int room, const char *opt, const char *arg)
+{
+	char path[PATHSIZE];
+	struct db_result *old;
+
+	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
+
+	char *query = sqlite3_mprintf("SELECT * from "USERDB_ROOMS" WHERE room=%d AND opt=%Q", room, opt);
+	old  = db_query(path, query, 1);
+	sqlite3_free(query);
+
+	/* set a new value */
+	struct db_result *res;
+	if (arg==NULL || *arg==0) {
+		char *query = sqlite3_mprintf("DELETE FROM "USERDB_ROOMS" WHERE room=%d AND opt=%Q", room, opt);
+		db_free(db_query(path, query, 1));
+		sqlite3_free(query);
+	}else
+	if (old != NULL && old->rows > 0) {
+		char *query = sqlite3_mprintf("UPDATE "USERDB_ROOMS" SET arg=%Q WHERE room=%d AND opt=%Q", arg, room, opt);
+		db_free(db_query(path, query, 1));
+		sqlite3_free(query);
+	} else {
+		char *query = sqlite3_mprintf("INSERT INTO "USERDB_ROOMS" (room, opt, arg) VALUES (%d,%Q,%Q)", room, opt, arg);
+		res = db_query(path, query, 1);
+		if (res == NULL) {
+			/* it failed, try to create the table */
+			char *create = "CREATE TABLE "USERDB_ROOMS" (room integer, opt text, arg text)";
+			res = db_query(path, create, 0);
+			sqlite3_free(create);
+			if (res != NULL) {
+				/* it worked, resubmit the insert */
+				db_free(res);
+				res = db_query(path, query, 0);
+			}
+		}
+		db_free(res);
+		sqlite3_free(query);
+	}
+	db_free(old);
+}

Modified: branches/nomud/src/sqlite.h
===================================================================
--- branches/nomud/src/sqlite.h	2009-12-06 17:57:58 UTC (rev 1117)
+++ branches/nomud/src/sqlite.h	2009-12-07 17:56:29 UTC (rev 1118)
@@ -28,4 +28,5 @@
 char * db_item(struct db_result *result, int row, const char *field);
 char * userdb_get(const char *table, const char *user, const char *opt);
 void userdb_set(const char *table, const char *user, const char *opt, const char *arg);
-
+char * db_room_get(int room, const char *opt);
+void db_room_set(int room, const char *opt, const char *arg);




More information about the mw-devel mailing list