In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / Mono.Data.Sqlite / Mono.Data.Sqlite_2.0 / SQLite3_UTF16.cs
1 //
2 // Mono.Data.Sqlite.SQLite3_UTF16.cs
3 //
4 // Author(s):
5 //   Robert Simpson (robert@blackcastlesoft.com)
6 //
7 // Adapted and modified for the Mono Project by
8 //   Marek Habersack (grendello@gmail.com)
9 //
10 //
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2007 Marek Habersack
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 /********************************************************
35  * ADO.NET 2.0 Data Provider for SQLite Version 3.X
36  * Written by Robert Simpson (robert@blackcastlesoft.com)
37  * 
38  * Released to the public domain, use at your own risk!
39  ********************************************************/
40 #if NET_2_0
41 namespace Mono.Data.Sqlite
42 {
43   using System;
44   using System.Runtime.InteropServices;
45
46   /// <summary>
47   /// Alternate Sqlite3 object, overriding many text behaviors to support UTF-16 (Unicode)
48   /// </summary>
49   internal class Sqlite3_UTF16 : Sqlite3
50   {
51     internal Sqlite3_UTF16(SqliteDateFormats fmt)
52       : base(fmt)
53     {
54     }
55
56     /// <summary>
57     /// Overrides SqliteConvert.ToString() to marshal UTF-16 strings instead of UTF-8
58     /// </summary>
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)
63     {
64       return Marshal.PtrToStringUni(b);
65     }
66
67     internal override string Version
68     {
69       get
70       {
71         return base.ToString(UnsafeNativeMethods.sqlite3_libversion());
72       }
73     }
74
75     internal override void Open(string strFilename)
76     {
77       if (_sql != IntPtr.Zero) return;
78       int n = UnsafeNativeMethods.sqlite3_open16(strFilename, out _sql);
79       if (n > 0) throw new SqliteException(n, SqliteLastError());
80
81       _functionsArray = SqliteFunction.BindFunctions(this);
82     }
83
84     internal override string SqliteLastError()
85     {
86       return ToString(UnsafeNativeMethods.sqlite3_errmsg16(_sql));
87     }
88
89     internal override void Bind_DateTime(SqliteStatement stmt, int index, DateTime dt)
90     {
91       Bind_Text(stmt, index, ToString(dt));
92     }
93
94     internal override string Bind_ParamName(SqliteStatement stmt, int index)
95     {
96       return base.ToString(UnsafeNativeMethods.sqlite3_bind_parameter_name(stmt._sqlite_stmt, index));
97     }
98
99     internal override void Bind_Text(SqliteStatement stmt, int index, string value)
100     {
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());
103     }
104
105     internal override string ColumnName(SqliteStatement stmt, int index)
106     {
107       return ToString(UnsafeNativeMethods.sqlite3_column_name16(stmt._sqlite_stmt, index));
108     }
109
110     internal override DateTime GetDateTime(SqliteStatement stmt, int index)
111     {
112       return ToDateTime(GetText(stmt, index));
113     }
114     internal override string GetText(SqliteStatement stmt, int index)
115     {
116             return ToString (UnsafeNativeMethods.sqlite3_column_text16(stmt._sqlite_stmt, index));
117     }
118
119     internal override string ColumnOriginalName(SqliteStatement stmt, int index)
120     {
121       return ToString(UnsafeNativeMethods.sqlite3_column_origin_name16(stmt._sqlite_stmt, index));
122     }
123
124     internal override string ColumnDatabaseName(SqliteStatement stmt, int index)
125     {
126       return ToString(UnsafeNativeMethods.sqlite3_column_database_name16(stmt._sqlite_stmt, index));
127     }
128
129     internal override string ColumnTableName(SqliteStatement stmt, int index)
130     {
131       return ToString(UnsafeNativeMethods.sqlite3_column_table_name16(stmt._sqlite_stmt, index));
132     }
133
134     internal override IntPtr CreateFunction(string strFunction, int nArgs, SqliteCallback func, SqliteCallback funcstep, SqliteCallback funcfinal)
135     {
136             // FIXME: the function interface below is not supported in the mainstream version of sqlite. Need to rewrite the C# API to
137             // use the mainstream sqlite. The cookie needs to be allocated in C#
138       IntPtr nCookie;
139
140       int n = UnsafeNativeMethods.sqlite3_create_function16(_sql, strFunction, nArgs, 4, func, funcstep, funcfinal, out nCookie);
141       if (n > 0) throw new SqliteException(n, SqliteLastError());
142
143       return nCookie;
144     }
145
146     internal override IntPtr CreateCollation(string strCollation, SqliteCollation func)
147     {
148             // FIXME: the function interface below is not supported in the mainstream version of sqlite. Need to rewrite the C# API to
149             // use the mainstream sqlite. The cookie needs to be allocated in C#
150       IntPtr nCookie;
151
152       int n = UnsafeNativeMethods.sqlite3_create_collation16(_sql, strCollation, 4, 0, func, out nCookie);
153       if (n > 0) throw new SqliteException(n, SqliteLastError());
154
155       return nCookie;
156     }
157
158     internal override string GetParamValueText(IntPtr ptr)
159     {
160       return ToString(UnsafeNativeMethods.sqlite3_value_text16(ptr));
161     }
162
163     internal override void ReturnError(IntPtr context, string value)
164     {
165       UnsafeNativeMethods.sqlite3_result_error16(context, value, value.Length);
166     }
167
168     internal override void ReturnText(IntPtr context, string value)
169     {
170       UnsafeNativeMethods.sqlite3_result_text16(context, value, value.Length, (IntPtr)(-1));
171     }
172   }
173 }
174 #endif