The unified diff between revisions [9af6e795..] and [594720f0..] is displayed below. It can also be downloaded as a raw diff.

This diff has been restricted to the following files: 'mlsqlite/ocaml-sqlite3.c'

#
#
# patch "mlsqlite/ocaml-sqlite3.c"
#  from [7c98e2fdeb9663865485a3f21374dae11f74f46f]
#    to [215cc1e97dd120a746601b54a4ba10fc598ebf98]
#
============================================================
--- mlsqlite/ocaml-sqlite3.c	7c98e2fdeb9663865485a3f21374dae11f74f46f
+++ mlsqlite/ocaml-sqlite3.c	215cc1e97dd120a746601b54a4ba10fc598ebf98
@@ -11,8 +11,7 @@

 #include <sqlite3.h>

-#define TRUE  1
-#define FALSE 0
+#include "ocaml-sqlite3.h"

 /* Not wrapped :
    - user-defined aggregate functions
@@ -27,10 +26,7 @@
 
 /* Error handling */

-static void ml_sqlite3_raise_exn (int, const char *, int) Noreturn;
-#define raise_sqlite3_exn(db)	ml_sqlite3_raise_exn (sqlite3_errcode (Sqlite3_val(db)), sqlite3_errmsg (Sqlite3_val(db)), TRUE)
-
-static void ml_sqlite3_raise_exn (int status, const char *errmsg, int static_errmsg)
+void ml_sqlite3_raise_exn (int status, const char *errmsg, int static_errmsg)
 {
   static value *sqlite3_exn;

@@ -59,62 +55,8 @@ static void ml_sqlite3_raise_exn (int st


 
-/* Memory management of sqlite3* values */
-struct user_function {
-  value fun;
-  struct user_function *next;
-};
-
-struct ml_sqlite3_data {
-  sqlite3 *db;
-  value  callbacks;
-  value  stmt_store;
-  struct user_function *user_functions;
-};
-
-#define Sqlite3_data_val(v)	(* ((struct ml_sqlite3_data **) Data_custom_val(v)))
-
-#if defined(__GNUC__) && (__GNUC__ >= 3)
-# define Pure	__attribute__ ((pure))
-#else
-# define Pure
-#endif
-
-static sqlite3 *	Sqlite3_val       (value) Pure;
-static sqlite3_stmt *	Sqlite3_stmt_val  (value) Pure;
-static sqlite3_value *	Sqlite3_value_val (value) Pure;
-
-static inline sqlite3 *
-Sqlite3_val (value v)
-{
-  struct ml_sqlite3_data *data = Sqlite3_data_val (v);
-  if (data->db == NULL)
-    ml_sqlite3_raise_exn (SQLITE_MISUSE, "closed db", TRUE);
-  return data->db;
-}
-
-static inline sqlite3_stmt *
-Sqlite3_stmt_val (value v)
-{
-  sqlite3_stmt *stmt = * ((sqlite3_stmt **) Field (v, 0));
-  if (stmt == NULL)
-    ml_sqlite3_raise_exn (SQLITE_MISUSE, "invalid statement", TRUE);
-  return stmt;
-}
-
-static inline sqlite3_value *
-Sqlite3_value_val (value v)
-{
-  sqlite3_value *val = * ((sqlite3_value **) v);
-  if (val == NULL)
-    ml_sqlite3_raise_exn (SQLITE_MISUSE, "invalid value", TRUE);
-  return val;
-}
-
-
-
-/* 0 -> trace
- * 1 -> busy
+/* 0 -> busy
+ * 1 -> trace
  * 2 -> progress
  */
 #define NUM_CALLBACKS 3
@@ -231,7 +173,11 @@ ml_sqlite3_complete (value sql)
 CAMLprim value
 ml_sqlite3_complete (value sql)
 {
+#ifdef HAVE_SQLITE3_COMPLETE
   return Val_bool (sqlite3_complete (String_val (sql)));
+#else
+  caml_failwith ("sqlite3_complete unavailable");
+#endif
 }

 CAMLprim value
@@ -258,18 +204,20 @@ ml_sqlite3_total_changes (value db)
   return Val_long (sqlite3_total_changes (Sqlite3_val (db)));
 }

-#if 0
 CAMLprim value
 ml_sqlite3_get_autocommit (value db)
 {
+#ifdef HAVE_SQLITE3_GET_AUTOCOMMIT
   return Val_bool (sqlite3_get_autocommit (Sqlite3_val (db)));
-}
+#else
+  caml_failwith ("sqlite3_get_autocommit unavailable");
 #endif
+}

 CAMLprim value
 ml_sqlite3_sleep (value ms)
 {
-#if 0
+#if HAVE_SQLITE3_SLEEP
   return Val_int (sqlite3_sleep (Int_val (ms)));
 #else
   caml_failwith ("sqlite3_sleep unavailable");
@@ -279,8 +227,8 @@ ml_sqlite3_sleep (value ms)
 
 /* callbacks */

-#define MLTAG_RETRY 0xc96e9e91L
-#define MLTAG_FAIL  0x5ced03bdL
+#define MLTAG_RETRY -915497327L
+#define MLTAG_FAIL  1559036861L

 static int
 ml_sqlite3_busy_handler_cb (void *data, int num)
@@ -345,6 +293,7 @@ ml_sqlite3_trace_unset (value db)
   return Val_unit;
 }

+#ifdef HAVE_SQLITE3_PROGRESS_HANDLER
 static int
 ml_sqlite3_progress_handler_cb (void *data)
 {
@@ -353,37 +302,42 @@ ml_sqlite3_progress_handler_cb (void *da
  res = caml_callback_exn (Field (db->callbacks, 2), Val_unit);
  return Is_exception_result(res);
 }
+#endif

 CAMLprim value
 ml_sqlite3_progress_handler (value db, value delay, value cb)
 {
+#ifdef HAVE_SQLITE3_PROGRESS_HANDLER
   struct ml_sqlite3_data *db_data = Sqlite3_data_val(db);
   sqlite3_progress_handler (Sqlite3_val (db), Int_val (delay),
 			    ml_sqlite3_progress_handler_cb, db_data);
   Store_field (db_data->callbacks, 2, cb);
+#endif
   return Val_unit;
 }

 CAMLprim value
 ml_sqlite3_progress_handler_unset (value db)
 {
+#ifdef HAVE_SQLITE3_PROGRESS_HANDLER
   struct ml_sqlite3_data *db_data = Sqlite3_data_val(db);
   sqlite3_progress_handler (Sqlite3_val(db), 0, NULL, NULL);
   Store_field (db_data->callbacks, 2, Val_unit);
+#endif
   return Val_unit;
 }


 
-#define MLTAG_INTEGER 0x2ddf233dL
-#define MLTAG_FLOAT   0x0109faf9L
-#define MLTAG_TEXT    0x6f75295bL
-#define MLTAG_BLOB    0x57b40abbL
-#define MLTAG_NULL    0x679ecd0fL
+#define MLTAG_INTEGER  769598269L
+#define MLTAG_FLOAT     17431289L
+#define MLTAG_TEXT    1869949275L
+#define MLTAG_BLOB    1471417019L
+#define MLTAG_NULL    1738460431L

-#define MLTAG_INT     0x006f519f
-#define MLTAG_INT64   0x781dd39b
-#define MLTAG_VALUE   0x5f4d6ea3
+#define MLTAG_INT        7295391L
+#define MLTAG_INT64   2015220635L
+#define MLTAG_VALUE   1598910115L

 static value
 convert_sqlite3_type (int t)
@@ -408,26 +362,16 @@ convert_sqlite3_type (int t)
 
 /* Prepared statements */

-static int
-ml_sqlite3_finalize_stmt (value s, sqlite3 **db)
+CAMLprim value
+ml_sqlite3_finalize_noerr (value s)
 {
-  int status = SQLITE_OK;
   sqlite3_stmt **p_stmt = (sqlite3_stmt **) Field (s, 0);

   if (*p_stmt != NULL)
     {
-      if (db != NULL)
-	*db = sqlite3_db_handle (*p_stmt);
-      status = sqlite3_finalize (*p_stmt);
+      sqlite3_finalize (*p_stmt);
       *p_stmt = NULL;
     }
-  return status;
-}
-
-CAMLprim value
-ml_sqlite3_finalize_noerr (value v)
-{
-  ml_sqlite3_finalize_stmt (v, NULL);
   return Val_unit;
 }

@@ -508,11 +452,8 @@ ml_sqlite3_reset (value stmt)
 CAMLprim value
 ml_sqlite3_reset (value stmt)
 {
-  sqlite3_stmt *s = * ((sqlite3_stmt **) Field (stmt, 0));
-  if (s == NULL)
-    ml_sqlite3_recompile (stmt, NULL);
-  else
-    sqlite3_reset (s);
+  sqlite3_stmt *s = Sqlite3_stmt_val (stmt);
+  sqlite3_reset (s);
   return Val_unit;
 }

@@ -520,11 +461,11 @@ ml_sqlite3_expired (value stmt)
 ml_sqlite3_expired (value stmt)
 {
   sqlite3_stmt *s = * ((sqlite3_stmt **) Field (stmt, 0));
-  return Val_bool (s ? sqlite3_expired (s) : TRUE);
+  return Val_bool (s == NULL);
 }

-#define MLTAG_ROW	0x007cfbf5L
-#define MLTAG_DONE	0x5a5d7105L
+#define MLTAG_ROW	   8190965L
+#define MLTAG_DONE	1516073221L

 CAMLprim value
 ml_sqlite3_step (value stmt)
@@ -545,12 +486,14 @@ ml_sqlite3_step (value stmt)
     default: /* either BUSY, ERROR or MISUSE */
       {
 	sqlite3 *db;
-	if (sqlite3_expired (s))
+	if (status == SQLITE_ERROR)
+	  status = sqlite3_reset (s);
+	if (status == SQLITE_SCHEMA)
 	  {
 	    s = ml_sqlite3_recompile (stmt, s);
 	    goto again;
 	  }
-	status = ml_sqlite3_finalize_stmt (stmt, &db);
+	db = sqlite3_db_handle (s);
 	ml_sqlite3_raise_exn (status, sqlite3_errmsg (db), TRUE);
       }
     }
@@ -594,7 +537,7 @@ ml_sqlite3_bind (value s, value idx, val
 				      SQLITE_TRANSIENT);
 	  break;
 	case MLTAG_VALUE:
-#if 0
+#if HAVE_SQLITE3_BIND_VALUE
 	  status = sqlite3_bind_value (stmt, i, Sqlite3_value_val (val)); break;
 #else
 	  caml_failwith ("sqlite3_bind_value unavailable");
@@ -631,14 +574,23 @@ ml_sqlite3_clear_bindings (value s)
 CAMLprim value
 ml_sqlite3_clear_bindings (value s)
 {
-#if 0
+#if HAVE_SQLITE3_CLEAR_BINDINGS
   int status;
   status = sqlite3_clear_bindings (Sqlite3_stmt_val (s));
   if (status != SQLITE_OK)
     ml_sqlite3_raise_exn (status, "clear_bindings failed", TRUE);
   return Val_unit;
 #else
-  caml_failwith ("sqlite3_clear_bindings unavailable");
+  sqlite3_stmt *stmt = Sqlite3_stmt_val (s);
+  int i, n, status;
+  n = sqlite3_bind_parameter_count(stmt);
+  for (i = 1; i <= n; i++)
+    {
+      status = sqlite3_bind_null(stmt, i);
+      if (status != SQLITE_OK)
+	ml_sqlite3_raise_exn (status, "clear_bindings failed", TRUE);
+    }
+  return Val_unit;
 #endif
 }