diff -Nru 3-3/src/Makefile.in 3-3.sqlite/src/Makefile.in
--- 3-3/src/Makefile.in	2003-09-28 06:08:29.000000000 +0200
+++ 3-3.sqlite/src/Makefile.in	2004-03-28 17:02:18.541126536 +0200
@@ -67,7 +67,7 @@
 #
 CFLAGS= @EXTRA_CFLAGS@ $(OPTIMIZE) $(DEBUG) $(WARN) $(MPATH) $(PROFIL)
 #
-LIBS=@LIBS@ @PKGLIBS@
+LIBS=@LIBS@ @PKGLIBS@ -lsqlite
 #
 LDFLAGS=$(PROFIL) @LDFLAGS@
 #
@@ -83,7 +83,7 @@
       lex.c main.c mapping.c md5.c mempools.c mregex.c mstrings.c object.c \
       otable.c\
       parser.c parse.c pkg-alists.c pkg-mccp.c pkg-mysql.c pkg-pcre.c \
-      pkg-pgsql.c pkg-tls.c \
+      pkg-pgsql.c pkg-tls.c pkg-sqlite.c \
       ptmalloc.c port.c ptrtable.c \
       random.c regexp.c simulate.c simul_efun.c stdstrings.c \
       strfuns.c structs.c sprintf.c swap.c wiz_list.c xalloc.c 
@@ -94,7 +94,7 @@
       lex.o main.o mapping.o md5.o mempools.o mregex.o mstrings.o object.o \
       otable.o \
       parser.o parse.o pkg-alists.o pkg-mccp.o pkg-mysql.o pkg-pcre.o \
-      pkg-pgsql.o pkg-tls.o \
+      pkg-pgsql.o pkg-tls.o pkg-sqlite.o \
       ptmalloc.o port.o ptrtable.o \
       random.o regexp.o simulate.o simul_efun.o stdstrings.o \
       strfuns.o structs.o sprintf.o swap.o wiz_list.o xalloc.o @ALLOCA@ 
diff -Nru 3-3/src/config.h.in 3-3.sqlite/src/config.h.in
--- 3-3/src/config.h.in	2003-09-28 06:39:40.000000000 +0200
+++ 3-3.sqlite/src/config.h.in	2004-03-28 17:02:18.542126384 +0200
@@ -7,6 +7,8 @@
 #ifndef CONFIG_H__
 #define CONFIG_H__ 1
 
+#define USE_SQLITE
+
 /* ----------- Commandline Argument Defaults ----------
  * These options provide default settings for those options which can
  * also be set on the commandline.
diff -Nru 3-3/src/func_spec 3-3.sqlite/src/func_spec
--- 3-3/src/func_spec	2003-11-22 06:43:46.000000000 +0100
+++ 3-3.sqlite/src/func_spec	2004-03-28 17:02:18.543126232 +0200
@@ -698,6 +698,16 @@
 
 #endif /* USE_PGSQL */
 
+
+#ifdef USE_SQLITE
+
+int      sl_open(string);
+mixed    sl_exec(string);
+int      sl_insert_id();
+void     sl_close();
+
+#endif /* USE_SQLITE */
+
 #ifdef USE_TLS
 
 int     tls_query_connection_state(object default: F_THIS_OBJECT);
diff -Nru 3-3/src/lex.c 3-3.sqlite/src/lex.c
--- 3-3/src/lex.c	2004-03-28 17:00:14.063050080 +0200
+++ 3-3.sqlite/src/lex.c	2004-03-28 17:02:18.546125776 +0200
@@ -808,6 +808,9 @@
 #ifdef USE_MYSQL
     add_permanent_define("__MYSQL__", -1, string_copy("1"), MY_FALSE);
 #endif
+#ifdef USE_SQLITE
+    add_permanent_define("__SQLITE__", -1, string_copy("1"), MY_FALSE);
+#endif
 #ifdef USE_PGSQL
     add_permanent_define("__PGSQL__", -1, string_copy("1"), MY_FALSE);
 #endif
diff -Nru 3-3/src/object.c 3-3.sqlite/src/object.c
--- 3-3/src/object.c	2003-11-22 06:43:46.000000000 +0100
+++ 3-3.sqlite/src/object.c	2004-03-28 17:02:18.551125016 +0200
@@ -42,6 +42,7 @@
  *       wiz_list_t    * user;
  *       wiz_list_t    * eff_user;
  *       int             extra_num_variables;  (ifdef DEBUG)
+ *       int             open_sqlite_db (ifdef USE_SQLITE)
  *       svalue_t      * variables;
  *       unsigned long   ticks, gigaticks;
  *   }
diff -Nru 3-3/src/object.h 3-3.sqlite/src/object.h
--- 3-3/src/object.h	2003-04-14 05:12:51.000000000 +0200
+++ 3-3.sqlite/src/object.h	2004-03-28 17:02:18.551125016 +0200
@@ -49,6 +49,9 @@
     int extra_num_variables;
     /* amylaar : used to determine where to check ref counts at all... */
 #endif
+#ifdef USE_SQLITE
+    int open_sqlite_db;   /* does this object have an open sqlite db */
+#endif 
     svalue_t *variables;
       /* All variables to this object: an array of svalues, allocated
        * in a separate block.
diff -Nru 3-3/src/pkg-sqlite.c 3-3.sqlite/src/pkg-sqlite.c
--- 3-3/src/pkg-sqlite.c	1970-01-01 01:00:00.000000000 +0100
+++ 3-3.sqlite/src/pkg-sqlite.c	2004-03-28 17:02:42.996408768 +0200
@@ -0,0 +1,268 @@
+
+#include "config.h"
+#ifdef USE_SQLITE
+  
+#include <sqlite.h>
+#include "typedefs.h"
+  
+#include "my-alloca.h"
+#include <errno.h>
+#include <stddef.h>
+#include <time.h>
+#include <unistd.h>
+#include <stdio.h>
+#include "array.h"
+#include "interpret.h"
+#include "mstrings.h"
+#include "simulate.h"
+#include "svalue.h"
+#include "xalloc.h"
+#include "object.h"
+#include "stdstrings.h"
+
+typedef struct sqlite_rows_s sqlite_rows_t;
+typedef struct sqlite_dbs_s sqlite_dbs_t;
+
+/* Since we don't know the number of rows while we retrieve the
+ * rows from a query we save the data in a single-linked list first
+ * and move them into an array after the retrieval has finished 
+ */ 
+struct sqlite_rows_s 
+{
+   vector_t * row;
+   sqlite_rows_t * last;
+};
+
+/* Database connections should be bound to the object which opens 
+ * database file. We will store all database connections in a 
+ * linked list. 
+ */ 
+struct sqlite_dbs_s 
+{
+   sqlite * db;
+   object_t * obj;
+   sqlite_dbs_t * next;
+   sqlite_dbs_t * prev;
+};
+
+/* The list of database connections.
+ */ 
+static sqlite_dbs_t *head = NULL;
+  
+// HELPER FUNS
+sqlite_dbs_t *find_db (object_t * obj) 
+{
+   sqlite_dbs_t *tmp=NULL;
+   if (!head) return NULL;
+   else tmp = head;
+
+   while (tmp)
+   {
+      if (tmp->obj==obj) return tmp;
+      tmp=tmp->prev;
+   }
+   return NULL;
+}
+
+sqlite_dbs_t * new_db()
+{
+   sqlite_dbs_t *tmp;
+   tmp = pxalloc (sizeof (*tmp));
+   tmp->db=NULL;
+   tmp->obj=NULL;
+   tmp->next=NULL;
+   if (head) {
+   	head->next=tmp;
+   	tmp->prev=head;
+   	head=tmp;
+   } else {
+   	tmp->prev=NULL;
+   	head=tmp;
+   }
+   return tmp;
+}
+
+void remove_db(sqlite_dbs_t *db)
+{
+   if (db==head)
+   {
+   	if (head->prev) 
+   	{
+   		head->prev->next=NULL;
+   		head=head->prev;
+   	} else {
+   		head=NULL;
+   	}
+   } else {
+   	if (db->next) db->next->prev=db->prev;
+   	if (db->prev) db->prev->next=db->next;
+   }
+   pfree(db);
+   db=NULL;
+}
+
+// EFUNS GO FROM HERE
+svalue_t * 
+f_sl_open (svalue_t *sp) 
+{
+   string_t *file;
+   sqlite *db;
+   sqlite_dbs_t *tmp;
+   
+   file = check_valid_path(sp->u.str, current_object, STR_SQLITE_OPEN , MY_TRUE);
+   if (!file)
+   {
+      error ("file access error\n");
+      return sp;
+   }
+   
+   tmp = find_db (current_object);
+   if (tmp)
+   {
+      error ("DB already open.\n");
+      return sp;
+   }
+   db = sqlite_open (get_txt(file), 0, NULL);
+   if (!db)
+   {
+      error ("SQLITE: Could not open Database.\n");
+      return sp;
+   }
+
+   /* create a new chain link and hang on the old chain */
+   tmp=new_db();
+   tmp->db = db;
+   tmp->obj = current_object;
+   current_object->open_sqlite_db=1;
+   free_string_svalue (sp);
+   put_number (sp, 1);
+   return sp;
+}
+
+svalue_t * 
+f_sl_exec (svalue_t * sp) 
+{
+   sqlite_vm * vm;
+   const char *tail = 0;
+   char *zErrMsg = NULL;
+   char *errmsg = NULL;
+   char *sql;
+   const char **cols = 0;
+   const char **value = 0;
+   int i, pN = 0;
+   int rows = 0;
+   sqlite_rows_t *plast = NULL, *pthis = NULL;
+   sqlite_dbs_t *db=NULL;
+   vector_t * v = NULL;
+   sql = get_txt (sp->u.str);
+   
+   db = find_db (current_object);
+   if (!db) 
+   {
+      error ("No DB open.\n");
+      return sp;
+   }
+  
+   // compile the query / create vm
+   if (sqlite_compile (db->db, sql, &tail, &vm, &zErrMsg))
+   {
+      errmsg = pxalloc (strlen (zErrMsg) + 15);
+      sprintf (errmsg, "sl_exec: %s\n", zErrMsg);
+      free (zErrMsg);
+      error (errmsg);
+      pfree (errmsg);
+      return sp;
+   }
+  
+   // get all rows 
+   while (SQLITE_ROW == sqlite_step (vm, &pN, &value, &cols))
+   {
+      ++rows;
+      pthis = pxalloc (sizeof (*pthis));
+      if (plast)
+         pthis->last = plast;
+      else
+         pthis->last = NULL;
+      pthis->row = allocate_array (pN);
+      if (!pthis->row)
+      {
+         error("Out of memory.\n");
+         return sp;
+      }
+      for (i = 0; i < pN; i++)
+         if (value[i])
+            put_c_string (pthis->row->item + i, (const char *) value[i]);
+         else
+            put_number (pthis->row->item + i, 0);
+      plast = pthis;
+   }
+
+   sqlite_finalize (vm, NULL);
+   
+   free_string_svalue (sp);
+   
+   if (rows)
+   {
+      v = allocate_array (rows);
+      if (!v)
+      {
+         error("Out of memory.\n");
+         return sp;
+      }
+      while (rows--)
+      {
+         put_array (v->item + rows, pthis->row);
+         pthis = plast->last;
+    //         pfree (plast);
+         plast = pthis;
+      }
+      put_array (sp, v);
+   }
+   else
+   {
+      put_number (sp, 0);   
+   }
+  
+   return sp;
+}
+
+int
+sl_close (object_t *ob)
+{
+   sqlite_dbs_t *db=NULL;
+   db = find_db(ob);
+   if (!db) return 0;
+   sqlite_close(db->db);
+   ob->open_sqlite_db=0;
+   remove_db(db);
+   return 1;
+}
+
+svalue_t *
+f_sl_insert_id (svalue_t * sp)
+{
+  sqlite_dbs_t *db=NULL;
+  int id;
+  db=find_db(current_object);
+  if (!db)
+  {
+      error ("No DB open.\n");
+      return sp;
+   }
+   id=sqlite_last_insert_rowid(db->db);
+   sp++;
+   put_number(sp,id);
+   return sp;
+}
+
+svalue_t * 
+f_sl_close (svalue_t * sp) 
+{
+   if (!sl_close(current_object)) 
+   {
+      error ("No DB open.\n");
+   }
+   return sp;
+}
+
+#endif /* USE_SQLITE */
diff -Nru 3-3/src/pkg-sqlite.h 3-3.sqlite/src/pkg-sqlite.h
--- 3-3/src/pkg-sqlite.h	1970-01-01 01:00:00.000000000 +0100
+++ 3-3.sqlite/src/pkg-sqlite.h	2004-03-28 17:02:18.553124712 +0200
@@ -0,0 +1,2 @@
+
+int sl_close (object_t *ob);
diff -Nru 3-3/src/simulate.c 3-3.sqlite/src/simulate.c
--- 3-3/src/simulate.c	2004-03-28 17:00:14.068049320 +0200
+++ 3-3.sqlite/src/simulate.c	2004-03-28 17:02:53.389828728 +0200
@@ -53,6 +53,9 @@
 #include "mstrings.h"
 #include "object.h"
 #include "otable.h"
+#ifdef USE_SQLITE
+#include "pkg-sqlite.h"
+#endif
 #ifdef USE_TLS
 #include "pkg-tls.h"
 #endif
@@ -1905,6 +1908,9 @@
     ob->load_name = new_tabled(name);  /* but here it is */
     ob->prog = prog;
     ob->ticks = ob->gigaticks = 0;
+#ifdef USE_SQLITE
+    ob->open_sqlite_db=0;
+#endif
     ob->next_all = obj_list;
     ob->prev_all = NULL;
     if (obj_list)
@@ -2147,6 +2153,9 @@
     if (!current_object)
         fatal("clone_object() from no current_object !\n");
 #endif
+#ifdef USE_SQLITE
+    new_ob->open_sqlite_db=0;
+#endif
     new_ob->next_all = obj_list;
     new_ob->prev_all = NULL;
     if (obj_list)
@@ -2382,6 +2391,10 @@
 #ifdef CHECK_OBJECT_REF
     xallocate(shadow, sizeof(*shadow), "destructed object shadow");
 #endif /* CHECK_OBJECT_REF */
+
+#ifdef USE_SQLITE
+    if (ob->open_sqlite_db) sl_close(ob);
+#endif
     ob->time_reset = 0;
 
     /* We need the object in memory */
diff -Nru 3-3/src/string_spec 3-3.sqlite/src/string_spec
--- 3-3/src/string_spec	2003-11-22 06:43:46.000000000 +0100
+++ 3-3.sqlite/src/string_spec	2004-03-28 17:02:18.557124104 +0200
@@ -211,5 +211,8 @@
 PG_RESET_FAILED "Reset failed, connection aborted."
 SUCCESS         "success"
 #endif
+#ifdef USE_SQLITE
+SQLITE_OPEN	"sl_open"
+#endif
 
 /***************************************************************************/
