New test.
[mono.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / Sqlite.cs
index 656298d04817398896e9651420b77948e198f185..b9f01377b030679ca9e524c5bbf62f09ef2fed2c 100644 (file)
@@ -3,7 +3,10 @@
 //
 // Provides C# bindings to the library sqlite.dll
 //
-// Author(s): Everaldo Canuto  <everaldo_canuto@yahoo.com.br>
+//             Everaldo Canuto  <everaldo_canuto@yahoo.com.br>
+//                     Chris Turchin <chris@turchin.net>
+//                     Jeroen Zwartepoorte <jeroen@xs4all.nl>
+//                     Thomas Zoechling <thomas.zoechling@gmx.at>
 //
 // Copyright (C) 2004  Everaldo Canuto
 //
@@ -30,6 +33,7 @@
 using System;
 using System.Security;
 using System.Runtime.InteropServices;
+using System.Text;
 
 namespace Mono.Data.SqliteClient
 {
@@ -120,7 +124,7 @@ namespace Mono.Data.SqliteClient
                internal static extern void sqliteFree (IntPtr ptr);
                
                [DllImport ("sqlite")]
-               internal static extern SqliteError sqlite_compile (IntPtr sqlite_handle, string zSql, out IntPtr pzTail, out IntPtr pVm, out IntPtr errstr);
+               internal static extern SqliteError sqlite_compile (IntPtr sqlite_handle, IntPtr zSql, out IntPtr pzTail, out IntPtr pVm, out IntPtr errstr);
 
                [DllImport ("sqlite")]
                internal static extern SqliteError sqlite_step (IntPtr pVm, out int pN, out IntPtr pazValue, out IntPtr pazColName);
@@ -129,54 +133,153 @@ namespace Mono.Data.SqliteClient
                internal static extern SqliteError sqlite_finalize (IntPtr pVm, out IntPtr pzErrMsg);
 
                [DllImport ("sqlite")]
-                internal static extern SqliteError sqlite_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
+               internal static extern SqliteError sqlite_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
                
-               [DllImport("sqlite3")]
-               internal static extern int sqlite3_open (string dbname, out IntPtr handle);
+               [DllImport ("sqlite")]
+               internal static extern void sqlite_busy_timeout (IntPtr handle, int ms);
+
+               [DllImport("sqlite3", CharSet = CharSet.Unicode)]
+               internal static extern int sqlite3_open16 (string dbname, out IntPtr handle);
 
                [DllImport("sqlite3")]
                internal static extern void sqlite3_close (IntPtr sqlite_handle);
 
                [DllImport("sqlite3")]
-               internal static extern string sqlite3_errmsg (IntPtr sqlite_handle);
+               internal static extern IntPtr sqlite3_errmsg16 (IntPtr sqlite_handle);
 
                [DllImport("sqlite3")]
                internal static extern int sqlite3_changes (IntPtr handle);
 
                [DllImport("sqlite3")]
-               internal static extern int sqlite3_last_insert_rowid (IntPtr sqlite_handle);
+               internal static extern long sqlite3_last_insert_rowid (IntPtr sqlite_handle);
 
                [DllImport ("sqlite3")]
-               internal static extern void sqlite3Free (IntPtr ptr);
-               
-               [DllImport ("sqlite3")]
-               internal static extern SqliteError sqlite3_prepare (IntPtr sqlite_handle, string zSql, int zSqllen, out IntPtr pVm, out IntPtr pzTail);
+               internal static extern SqliteError sqlite3_prepare16 (IntPtr sqlite_handle, IntPtr zSql, int zSqllen, out IntPtr pVm, out IntPtr pzTail);
 
                [DllImport ("sqlite3")]
                internal static extern SqliteError sqlite3_step (IntPtr pVm);
 
                [DllImport ("sqlite3")]
-               internal static extern SqliteError sqlite3_finalize (IntPtr pVm, out IntPtr pzErrMsg);
+               internal static extern SqliteError sqlite3_finalize (IntPtr pVm);
 
                [DllImport ("sqlite3")]
-                internal static extern SqliteError sqlite3_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
+               internal static extern SqliteError sqlite3_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
        
                [DllImport ("sqlite3")]
-               internal static extern IntPtr sqlite3_column_name (IntPtr pVm, int col);
+               internal static extern IntPtr sqlite3_column_name16 (IntPtr pVm, int col);
+               
                [DllImport ("sqlite3")]
-               internal static extern IntPtr sqlite3_column_text (IntPtr pVm, int col);
+               internal static extern IntPtr sqlite3_column_text16 (IntPtr pVm, int col);
+               
                [DllImport ("sqlite3")]
                internal static extern IntPtr sqlite3_column_blob (IntPtr pVm, int col);
+               
                [DllImport ("sqlite3")]
-               internal static extern int sqlite3_column_bytes (IntPtr pVm, int col);
+               internal static extern int sqlite3_column_bytes16 (IntPtr pVm, int col);
+               
                [DllImport ("sqlite3")]
                internal static extern int sqlite3_column_count (IntPtr pVm);
+               
                [DllImport ("sqlite3")]
                internal static extern int sqlite3_column_type (IntPtr pVm, int col);
+               
                [DllImport ("sqlite3")]
                internal static extern Int64 sqlite3_column_int64 (IntPtr pVm, int col);
+               
                [DllImport ("sqlite3")]
                internal static extern double sqlite3_column_double (IntPtr pVm, int col);
+               
+               [DllImport ("sqlite3")]
+               internal static extern IntPtr sqlite3_column_decltype16 (IntPtr pVm, int col);
+
+               [DllImport ("sqlite3")]
+               internal static extern int sqlite3_bind_parameter_count (IntPtr pStmt);
+
+               [DllImport ("sqlite3")]
+               internal static extern IntPtr sqlite3_bind_parameter_name (IntPtr pStmt, int n); // UTF-8 encoded return
+
+               [DllImport ("sqlite3")]
+               internal static extern SqliteError sqlite3_bind_blob (IntPtr pStmt, int n, byte[] blob, int length, IntPtr freetype);
+
+               [DllImport ("sqlite3")]
+               internal static extern SqliteError sqlite3_bind_double (IntPtr pStmt, int n, double value);
+
+               [DllImport ("sqlite3")]
+               internal static extern SqliteError sqlite3_bind_int (IntPtr pStmt, int n, int value);
+
+               [DllImport ("sqlite3")]
+               internal static extern SqliteError sqlite3_bind_int64 (IntPtr pStmt, int n, long value);
+
+               [DllImport ("sqlite3")]
+               internal static extern SqliteError sqlite3_bind_null (IntPtr pStmt, int n);
+
+               [DllImport ("sqlite3", CharSet = CharSet.Unicode)]
+               internal static extern SqliteError sqlite3_bind_text16 (IntPtr pStmt, int n, string value, int length, IntPtr freetype);
+               
+               [DllImport ("sqlite3")]
+               internal static extern void sqlite3_busy_timeout (IntPtr handle, int ms);
+
                #endregion
+               
+               // These are adapted from Mono.Unix.  When encoding is null,
+               // use Ansi encoding, which is a superset of the default
+               // expected encoding (ISO-8859-1).
+
+               public static IntPtr StringToHeap (string s, Encoding encoding)
+               {
+                       if (encoding == null)
+                               return Marshal.StringToHGlobalAnsi (s);
+                               
+                       int min_byte_count = encoding.GetMaxByteCount(1);
+                       char[] copy = s.ToCharArray ();
+                       byte[] marshal = new byte [encoding.GetByteCount (copy) + min_byte_count];
+
+                       int bytes_copied = encoding.GetBytes (copy, 0, copy.Length, marshal, 0);
+
+                       if (bytes_copied != (marshal.Length-min_byte_count))
+                               throw new NotSupportedException ("encoding.GetBytes() doesn't equal encoding.GetByteCount()!");
+
+                       IntPtr mem = Marshal.AllocHGlobal (marshal.Length);
+                       if (mem == IntPtr.Zero)
+                               throw new OutOfMemoryException ();
+
+                       bool copied = false;
+                       try {
+                               Marshal.Copy (marshal, 0, mem, marshal.Length);
+                               copied = true;
+                       }
+                       finally {
+                               if (!copied)
+                                       Marshal.FreeHGlobal (mem);
+                       }
+
+                       return mem;
+               }
+
+               public static unsafe string HeapToString (IntPtr p, Encoding encoding)
+               {
+                       if (encoding == null)
+                               return Marshal.PtrToStringAnsi (p);
+               
+                       if (p == IntPtr.Zero)
+                               return null;
+                               
+                       // This assumes a single byte terminates the string.
+
+                       int len = 0;
+                       while (Marshal.ReadByte (p, len) != 0)
+                               checked {++len;}
+
+                       string s = new string ((sbyte*) p, 0, len, encoding);
+                       len = s.Length;
+                       while (len > 0 && s [len-1] == 0)
+                               --len;
+                       if (len == s.Length) 
+                               return s;
+                       return s.Substring (0, len);
+               }
+
+
+
        }
 }