2 // Mono.Data.Sqlite.SQLite3_UTF16.cs
5 // Robert Simpson (robert@blackcastlesoft.com)
7 // Adapted and modified for the Mono Project by
8 // Marek Habersack (grendello@gmail.com)
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2007 Marek Habersack
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 /********************************************************
35 * ADO.NET 2.0 Data Provider for SQLite Version 3.X
36 * Written by Robert Simpson (robert@blackcastlesoft.com)
38 * Released to the public domain, use at your own risk!
39 ********************************************************/
41 namespace Mono.Data.Sqlite
44 using System.Runtime.InteropServices;
47 /// Alternate Sqlite3 object, overriding many text behaviors to support UTF-16 (Unicode)
49 internal class Sqlite3_UTF16 : Sqlite3
51 internal Sqlite3_UTF16(SqliteDateFormats fmt)
57 /// Overrides SqliteConvert.ToString() to marshal UTF-16 strings instead of UTF-8
59 /// <param name="b">A pointer to a UTF-16 string</param>
60 /// <param name="nbytelen">The length (IN BYTES) of the string</param>
61 /// <returns>A .NET string</returns>
62 public override string ToString(IntPtr b)
64 return Marshal.PtrToStringUni(b);
67 internal override string Version
71 return base.ToString(UnsafeNativeMethods.sqlite3_libversion());
75 internal override void Open(string strFilename)
77 if (_sql != IntPtr.Zero) return;
78 int n = UnsafeNativeMethods.sqlite3_open16(strFilename, out _sql);
79 if (n > 0) throw new SqliteException(n, SqliteLastError());
81 _functionsArray = SqliteFunction.BindFunctions(this);
84 internal override string SqliteLastError()
86 return ToString(UnsafeNativeMethods.sqlite3_errmsg16(_sql));
89 internal override void Bind_DateTime(SqliteStatement stmt, int index, DateTime dt)
91 Bind_Text(stmt, index, ToString(dt));
94 internal override string Bind_ParamName(SqliteStatement stmt, int index)
96 return base.ToString(UnsafeNativeMethods.sqlite3_bind_parameter_name(stmt._sqlite_stmt, index));
99 internal override void Bind_Text(SqliteStatement stmt, int index, string value)
101 int n = UnsafeNativeMethods.sqlite3_bind_text16(stmt._sqlite_stmt, index, value, value.Length * 2, -1);
102 if (n > 0) throw new SqliteException(n, SqliteLastError());
105 internal override string ColumnName(SqliteStatement stmt, int index)
107 return ToString(UnsafeNativeMethods.sqlite3_column_name16(stmt._sqlite_stmt, index));
110 internal override DateTime GetDateTime(SqliteStatement stmt, int index)
112 return ToDateTime(GetText(stmt, index));
114 internal override string GetText(SqliteStatement stmt, int index)
116 return ToString (UnsafeNativeMethods.sqlite3_column_text16(stmt._sqlite_stmt, index));
119 internal override string ColumnOriginalName(SqliteStatement stmt, int index)
121 return ToString(UnsafeNativeMethods.sqlite3_column_origin_name16(stmt._sqlite_stmt, index));
124 internal override string ColumnDatabaseName(SqliteStatement stmt, int index)
126 return ToString(UnsafeNativeMethods.sqlite3_column_database_name16(stmt._sqlite_stmt, index));
129 internal override string ColumnTableName(SqliteStatement stmt, int index)
131 return ToString(UnsafeNativeMethods.sqlite3_column_table_name16(stmt._sqlite_stmt, index));
134 internal override void CreateFunction(string strFunction, int nArgs, SqliteCallback func, SqliteCallback funcstep, SqliteFinalCallback funcfinal)
136 int n = UnsafeNativeMethods.sqlite3_create_function16(_sql, strFunction, nArgs, 4, IntPtr.Zero, func, funcstep, funcfinal);
137 if (n > 0) throw new SqliteException(n, SqliteLastError());
140 internal override void CreateCollation(string strCollation, SqliteCollation func)
142 int n = UnsafeNativeMethods.sqlite3_create_collation16(_sql, strCollation, 4, IntPtr.Zero, func);
143 if (n > 0) throw new SqliteException(n, SqliteLastError());
146 internal override string GetParamValueText(IntPtr ptr)
148 return ToString(UnsafeNativeMethods.sqlite3_value_text16(ptr));
151 internal override void ReturnError(IntPtr context, string value)
153 UnsafeNativeMethods.sqlite3_result_error16(context, value, value.Length);
156 internal override void ReturnText(IntPtr context, string value)
158 UnsafeNativeMethods.sqlite3_result_text16(context, value, value.Length, (IntPtr)(-1));