[mw-devel] MARVIN r90 - branches/smonkey

welshbyte at sucs.org welshbyte at sucs.org
Thu Dec 21 00:37:28 GMT 2006


Author: welshbyte
Date: 2006-12-21 00:37:28 +0000 (Thu, 21 Dec 2006)
New Revision: 90

Modified:
   branches/smonkey/sqlite.c
   branches/smonkey/sqlite.h
Log:
Exposed sqlite.c's structs so that we stop casting pointers to ints.
Made marvin buildable again.


Modified: branches/smonkey/sqlite.c
===================================================================
--- branches/smonkey/sqlite.c	2006-12-16 23:00:11 UTC (rev 89)
+++ branches/smonkey/sqlite.c	2006-12-21 00:37:28 UTC (rev 90)
@@ -1,24 +1,8 @@
+#include "sqlite.h"
 #include "marvin.h"
 #include <sqlite3.h>
-#include <jsapi.h>
 
-/* functions to access and interact with SQLite
- * convert pointers to int so they can safely be handed into slang
- */
-struct db_data {
-	char **field;
-	int row;
-	struct db_data *next;
-};
-
-struct db_result {
-	int rows;
-	int cols;
-	char **colNames;
-	struct db_data *data;
-};
-
-int
+sqlite3*
 db_open(char *dbname)
 {
 	sqlite3 *db;
@@ -26,16 +10,18 @@
 
 	if (error != SQLITE_OK) {
 		printf("Error opening db %s: %s\n", dbname, sqlite3_errmsg(db));
-		return 0;
+		return NULL;
 	}
-	return (int)db;
+	return db;
 }
 
-int db_callback(void *ptr, int ncols, char **row, char **cols)
+int
+db_callback(void *ptr, int ncols, char **row, char **cols)
 {
 	struct db_result *query = ptr;
 	struct db_data *ndata;
-	int i,j;
+	int i;
+	unsigned int j;
 
 	if (query == NULL) return 1;
 
@@ -69,48 +55,52 @@
 	return 0;
 }
 
-int db_query(int *db, char *query)
+struct db_result*
+db_query(char *dbname, char *query)
 {
-	sqlite3 *sdb = (sqlite3 *)*db;
 	struct db_result *new;
 	int ret;
 	char *error = NULL;
+	sqlite3 *db;
 
-	if (sdb == NULL) return 0;
+	if (dbname == NULL) return 0;
 
+	db = db_open(dbname);
+	if (!db) {
+		return NULL;
+	}
+
 	new = malloc(sizeof(struct db_result));
 	new->rows = -1;
 	new->cols = -1;
 	new->colNames = NULL;
 	new->data = NULL;
 
-	ret = sqlite3_exec(sdb, query, db_callback, new, &error);
+	ret = sqlite3_exec(db, query, db_callback, new, &error);
 
 	if (ret != SQLITE_OK) {
 		printf("Error %s (%d) on query %s\n", error, ret, query);
-		free(error);
-		return 0;
+		sqlite3_free(error);
+		return NULL;
 	}
-	return (int)new;
+
+	return new;
 }
 
 /* how many rows in that result */
-int db_numrows(int *result_handle)
+int db_numrows(struct db_result *result)
 {
-	struct db_result *result = (struct db_result *)*result_handle;
-
 	if (result == NULL) return 0;
 	return result->rows;
 }
 
 /* fetch a row, return it as a struct */
-void db_getrow(int *result_handle, int *row)
+void db_getrow(struct db_result *result, int *row)
 {
-	struct db_result *result = (struct db_result *)*result_handle;
 	struct db_data *ndata;
 	char **field_names;
-        unsigned char *field_types;
-        void **field_values;
+	unsigned char *field_types;
+	void **field_values;
 	int i;
 
 	if (result == NULL) return;
@@ -136,9 +126,8 @@
 	/* SLstruct_create_struct(result->cols, field_names, field_types, field_values); */
 }
 
-void db_free(int *result_handle)
+void db_free(struct db_result *result)
 {
-	struct db_result *result = (struct db_result *)*result_handle;
 	struct db_data *ndata, *nnext;
 	int i;
 
@@ -169,10 +158,9 @@
 	free(result);
 }
 
-void db_close(int *db)
+void db_close(sqlite3 *db)
 {
-	sqlite3 *sdb = (sqlite3 *)*db;
-	printf("Closing database %p\n", sdb);
-	sqlite3_close(sdb);
+	printf("Closing database %p\n", (void *)db);
+	sqlite3_close(db);
 }
 

Modified: branches/smonkey/sqlite.h
===================================================================
--- branches/smonkey/sqlite.h	2006-12-16 23:00:11 UTC (rev 89)
+++ branches/smonkey/sqlite.h	2006-12-21 00:37:28 UTC (rev 90)
@@ -1,8 +1,18 @@
-/* sqlite.c */
-int db_open(char *dbname);
-int db_callback(void *ptr, int rows, char **row, char **cols);
-int db_query(int *db, char *query);
-int db_numrows(int *result_handle);
-void db_getrow(int *result_handle, int *row);
-void db_close(int *db);
-void db_free(int *result_handle);
+/* Functions to access and interact with SQLite */
+
+struct db_data {
+	char **field;
+	int row;
+	struct db_data *next;
+};
+
+struct db_result {
+	int rows;
+	int cols;
+	char **colNames;
+	struct db_data *data;
+};
+
+struct db_result *db_query(char *dbname, char *query);
+void db_getrow(struct db_result *result, int *row);
+void db_free(struct db_result *result);





More information about the mw-devel mailing list