seed r389 - in trunk: libseed modules/sqlite



Author: racarr
Date: Fri Dec  5 05:10:50 2008
New Revision: 389
URL: http://svn.gnome.org/viewvc/seed?rev=389&view=rev

Log:
I'm very sick so I don't remember all the API changes I just made. But they are all very nice and work. Also we have sqlite bindings now.


Added:
   trunk/modules/sqlite/example.js
Modified:
   trunk/libseed/seed-api.c
   trunk/libseed/seed-engine.c
   trunk/libseed/seed-private.h
   trunk/libseed/seed.h
   trunk/modules/sqlite/Makefile.am
   trunk/modules/sqlite/seed-sqlite.c

Modified: trunk/libseed/seed-api.c
==============================================================================
--- trunk/libseed/seed-api.c	(original)
+++ trunk/libseed/seed-api.c	Fri Dec  5 05:10:50 2008
@@ -1,5 +1,21 @@
 #include "seed-private.h"
 
+JSGlobalContextRef seed_context_create(JSContextGroupRef group,
+									   JSClassRef global_class)
+{
+	return JSGlobalContextCreateInGroup(group, global_class);
+}
+
+JSGlobalContextRef seed_context_ref(JSGlobalContextRef ctx)
+{
+	return JSGlobalContextRetain(ctx); 
+}
+
+void seed_context_unref(JSGlobalContextRef ctx)
+{
+	JSGlobalContextRelease(ctx); 
+}
+
 JSValueRef seed_make_null(JSContextRef ctx)
 {
 	return JSValueMakeNull(ctx);

Modified: trunk/libseed/seed-engine.c
==============================================================================
--- trunk/libseed/seed-engine.c	(original)
+++ trunk/libseed/seed-engine.c	Fri Dec  5 05:10:50 2008
@@ -1324,6 +1324,7 @@
 
 	eng->context = JSGlobalContextCreateInGroup(context_group, NULL);
 	eng->global = JSContextGetGlobalObject(eng->context);
+	eng->group = context_group;
 	
 	gobject_class = JSClassCreate(&gobject_def);
 	JSClassRetain(gobject_class);

Modified: trunk/libseed/seed-private.h
==============================================================================
--- trunk/libseed/seed-private.h	(original)
+++ trunk/libseed/seed-private.h	Fri Dec  5 05:10:50 2008
@@ -36,6 +36,8 @@
 struct _SeedEngine {
 	JSGlobalContextRef context;
 	JSObjectRef global;
+	
+	JSContextGroupRef group;
 };
 
 #include "seed-debug.h"

Modified: trunk/libseed/seed.h
==============================================================================
--- trunk/libseed/seed.h	(original)
+++ trunk/libseed/seed.h	Fri Dec  5 05:10:50 2008
@@ -33,6 +33,8 @@
 typedef gpointer SeedException;
 
 typedef gpointer SeedContext;
+typedef gpointer SeedGlobalContext;
+typedef gpointer SeedContextGroup;
 
 typedef enum {
 	SEED_TYPE_UNDEFINED,
@@ -58,8 +60,10 @@
 typedef struct _SeedScript SeedScript;
 
 typedef struct _SeedEngine {
-	SeedContext context;
+	SeedGlobalContext context;
 	SeedValue global;
+	
+	SeedContextGroup group;
 } SeedEngine;
 
 
@@ -87,6 +91,11 @@
  * seed-api.c
  */
 
+SeedGlobalContext seed_context_create(SeedContextGroup group, 
+								SeedClass global_class);
+SeedGlobalContext seed_context_ref(SeedGlobalContext ctx);
+void seed_context_unref(SeedGlobalContext ctx);
+
 SeedValue seed_make_null(SeedContext ctx);
 
 SeedObject seed_make_object(SeedContext ctx,

Modified: trunk/modules/sqlite/Makefile.am
==============================================================================
--- trunk/modules/sqlite/Makefile.am	(original)
+++ trunk/modules/sqlite/Makefile.am	Fri Dec  5 05:10:50 2008
@@ -7,6 +7,8 @@
 libsqlite_la_LDFLAGS = \
 	`pkg-config --libs seed sqlite3`
 
+EXTRA_DIST = example.js
+
 AM_CPPFLAGS = \
 	-I../../libseed/ \
 	`pkg-config --cflags seed` \

Added: trunk/modules/sqlite/example.js
==============================================================================
--- (empty file)
+++ trunk/modules/sqlite/example.js	Fri Dec  5 05:10:50 2008
@@ -0,0 +1,10 @@
+#!/usr/local/bin/seed
+Seed.import_namespace("sqlite");
+d = new sqlite.Database(Seed.argv[2]);
+
+d.exec("create table t1 (t1key INTEGER PRIMARY KEY,data TEXT,num double,timeEnter DATE);");
+d.exec("insert into t1 (data,num) values ('This is sample data',3);");
+d.exec("insert into t1 (data,num) values ('More sample data',6);");
+d.exec("insert into t1 (data,num) values ('And a little more',9);");
+
+d.exec("select * from t1", function(results){Seed.print(JSON.stringify(results))});

Modified: trunk/modules/sqlite/seed-sqlite.c
==============================================================================
--- trunk/modules/sqlite/seed-sqlite.c	(original)
+++ trunk/modules/sqlite/seed-sqlite.c	Fri Dec  5 05:10:50 2008
@@ -3,6 +3,7 @@
 
 SeedObject namespace_ref;
 SeedClass sqlite_class;
+SeedEngine * eng;
 
 #define MAKE_ERROR_ENUM(name)											\
 	seed_object_set_property(eng->context, namespace_ref, #name,			\
@@ -68,6 +69,8 @@
 
 	rc = sqlite3_open(file, &db);
 	
+	g_free(file);
+	
 	ret = seed_make_object(ctx, sqlite_class, db);
 	seed_object_set_property(ctx, ret, "status",
 							 seed_value_from_int(ctx, rc, exception));
@@ -75,6 +78,81 @@
 	return ret;
 }
 
+static int seed_sqlite_exec_callback(SeedObject function,
+									 int argc,
+									 gchar ** argv,
+									 gchar ** azColName)
+{
+	SeedGlobalContext ctx;
+	SeedObject hash;
+	int i;
+	
+	if (!function)
+		return 0;
+
+	ctx = seed_context_create(eng->group, NULL);
+
+	hash = seed_make_object(ctx, 0, 0);
+	for (i = 0; i < argc; i++)
+	{
+		seed_object_set_property(ctx, hash,
+								azColName[i],
+			seed_value_from_string(ctx, argv[i], 0));
+	}
+	
+	seed_object_call(ctx, function, 0, 1, &hash, 0);
+	
+	seed_context_unref(ctx);
+	
+	return 0;
+}
+
+SeedValue seed_sqlite_exec  (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	gchar * statement;
+	gchar * sqlite_error = 0;
+	sqlite3 * db;
+	int rc;
+	
+	if (argument_count < 1)
+	{
+		seed_make_exception(ctx, exception, "ArgumentError",
+							"sqlite.Database.exec expected 1 or 2 arguments");
+		return seed_make_null(ctx);
+	}
+	
+	statement = seed_value_to_string(ctx, arguments[0], exception);
+	db = seed_object_get_private(this_object);
+
+	g_assert(db);
+
+	rc = sqlite3_exec(db, statement, 
+					  seed_sqlite_exec_callback, 
+					  argument_count == 2 ? arguments[1] : 0, &sqlite_error);
+	g_free(statement);
+	
+	if (rc != SQLITE_OK)
+	{
+		if (sqlite_error)
+		{
+			seed_make_exception(ctx, 
+								exception, 
+								"SqliteError", 
+								sqlite_error);
+			sqlite3_free(sqlite_error);
+		}
+		return seed_make_null(ctx);
+	}
+
+	return seed_value_from_int(ctx, rc, exception);
+	
+}
+
 SeedValue seed_sqlite_close  (SeedContext ctx,
 							  SeedObject function,
 							  SeedObject this_object,
@@ -89,13 +167,16 @@
 
 seed_static_function database_funcs[] = {
 	{"close", seed_sqlite_close, 0},
+	{"exec", seed_sqlite_exec, 0 },
 	{0, 0, 0}
 };
 
-void seed_module_init(SeedEngine * eng)
+void seed_module_init(SeedEngine * local_eng)
 {
 	SeedObject db_constructor;
 	seed_class_definition sqlite_class_def = seed_empty_class;
+	
+	eng = local_eng;
 
 	namespace_ref = seed_make_object(eng->context, 0, 0);
 	



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]