2002-08-23 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / Mono.Data.MySql / Mono.Data.MySql / MySql.cs
1 // 
2 // MySql.cs
3 //
4 // Provides the core of C# bindings 
5 // to the MySQL library libMySQL.dll
6 //
7 // Author:
8 //    Brad Merrill <zbrad@cybercom.net>
9 //    Daniel Morgan <danmorg@sc.rr.com>
10 //
11 // (C)Copyright 2002 Brad Merril
12 // (C)Copyright 2002 Daniel Morgan
13 //
14 // http://www.cybercom.net/~zbrad/DotNet/MySql/
15 //
16 // Mono has gotten permission from Brad Merrill to include in 
17 // the Mono Class Library
18 // his C# bindings to MySQL under the X11 License
19 //
20 // Mono can be found at http://www.go-mono.com/
21 // The X11/MIT License can be found 
22 // at http://www.opensource.org/licenses/mit-license.html
23 //
24
25 // Things to do:
26 // =============
27 // FIXME: mysql_thread_init() and mysql_thread_end()
28 //        for some reason can not be loaded from the libMySQL.dll
29 //        using Mono.  Until this is fixed, there will be a
30 //        resource leak.
31 //
32 // TODO: method IntPtr PtrToStructure(IntPtr, Type) needs to
33 //       be implemented in assembly corlib.dll 
34 //       in class System.Runtime.InteropServices.Marshal
35 //       which requires also an internal call in the runtime
36 //       for it too.
37 //       This is so we can retrieve Field data from MySQL.
38 //
39 // TODO: more functions in the MySQL C Client API
40 //       (libmysqlclient.so on linux and libmySQL.dll on cygwin)
41 //       need to be added here as C# pinvoke methods.
42 //       Other data structures may need to be added as well.
43 //
44 // TODO: handle the name of the MySQL client library for
45 //       different platforms because it is named differently
46 //       on each platform.
47 //       We maybe using a config file for this.
48 //
49
50 namespace Mono.Data.MySql {
51         using System;
52         using System.Security;
53         using System.Runtime.InteropServices;
54
55         ///<remarks>
56         ///<para>
57         /// MySql P/Invoke implementation
58         /// Brad Merrill
59         /// 3-Mar-2002
60         ///</para>
61         ///<para>
62         /// This is an incomplete implementation of the mysql C api,
63         /// but sufficient to run the Test sample application.
64         ///</para>
65         ///</remarks>
66         internal sealed class MySql {
67                 ///<value>protocol version</value>
68                 public static readonly uint ProtocolVersion = 10;
69                 ///<value>server version</value>
70                 public static readonly string ServerVersion = "3.23.49";
71                 ///<value>server suffix</value>
72                 public static readonly string ServerSuffix = "";
73                 ///<value>server version as int</value>
74                 public static readonly uint VersionId = 32349;
75                 ///<value>server port number</value>
76                 public static readonly uint Port = 3306;
77                 ///<value>unix named socket</value>
78                 public static readonly string UnixAddr = "/tmp/mysql.sock";
79                 ///<value>character set</value>
80                 public static readonly string CharSet = "latin1";
81
82                 ///<summary>Gets or initializes a `MySql' structure</summary>
83                 ///<returns>An initialized `MYSQL*' handle.  IntPtr.Zero if there was insufficient memory to allocate a new object.</returns>
84                 [SuppressUnmanagedCodeSecurity]
85                 [DllImport("libmySQL", EntryPoint="mysql_init", ExactSpelling=true)]
86                 public static extern IntPtr Init(IntPtr db);
87
88                 ///<summary>Connects to a MySql server</summary>
89                 ///<returns><paramref name="db"/> value on success, else returns IntPtr.Zero</returns>
90                 [SuppressUnmanagedCodeSecurity]
91                 [DllImport("libmySQL",
92                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
93                          EntryPoint="mysql_connect", ExactSpelling=true)]
94                 public static extern IntPtr Connect(IntPtr db,
95                         [In] string host, [In] string user, [In] string passwd,
96                         [In] string dbname,
97                         uint port, [In] string socketName, uint flags
98                         );
99
100                 ///<summary>Selects a database</summary>
101                 ///<returns>Zero for success.  Non-zero if an error occurred.</returns>
102                 //[SuppressUnmanagedCodeSecurity]
103                 [DllImport("libmySQL",
104                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
105                          EntryPoint="mysql_select_db", ExactSpelling=true)]
106                 public static extern int SelectDb(IntPtr conn, [In] string db);
107
108                 ///<summary>Closes a server connection</summary>
109                 //[SuppressUnmanagedCodeSecurity]
110                 [DllImport("libmySQL", EntryPoint="mysql_close", ExactSpelling=true)]
111                 public static extern void Close(IntPtr db);
112
113                 ///<summary>Executes a SQL query specified as a string</summary>
114                 ///<returns>number of rows changed, -1 if zero</returns>
115                 //[SuppressUnmanagedCodeSecurity]
116                 [DllImport("libmySQL",
117                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
118                          EntryPoint="mysql_query", ExactSpelling=true)]
119                 public static extern int Query(IntPtr conn, [In] string query);
120
121                 ///<summary>Retrieves a complete result set to the client</summary>
122                 ///<returns>An IntPtr result structure with the results. IntPtr.Zero if an error occurred.</returns>
123                 //[SuppressUnmanagedCodeSecurity]
124                 [DllImport("libmySQL",
125                          EntryPoint="mysql_store_result", ExactSpelling=true)]
126                 public static extern IntPtr StoreResult(IntPtr conn);
127
128                 ///<returns>Returns the number of rows in a result set</returns>
129                 //[SuppressUnmanagedCodeSecurity]
130                 [DllImport("libmySQL",
131                          EntryPoint="mysql_num_rows", ExactSpelling=true)]
132                 public static extern int NumRows(IntPtr r);
133
134                 ///<returns>Returns the number of columns in a result set</returns>
135                 //[SuppressUnmanagedCodeSecurity]
136                 [DllImport("libmySQL",
137                          EntryPoint="mysql_num_fields", ExactSpelling=true)]
138                 public static extern int NumFields(IntPtr r);
139
140                 ///<returns>Returns an IntPtr to all field structures</returns>
141                 //[SuppressUnmanagedCodeSecurity]
142                 [DllImport("libmySQL",
143                          EntryPoint="mysql_fetch_field", ExactSpelling=true)]
144                 public static extern IntPtr FetchField(IntPtr r);
145
146                 ///<summary>Retrieves the next row of a result set</summary>
147                 ///<returns>An IntPtr structure for the next row. IntPtr.Zero if there are no more rows to retrieve or if an error occurred.</returns>
148                 //[SuppressUnmanagedCodeSecurity]
149                 [DllImport("libmySQL",
150                          EntryPoint="mysql_fetch_row", ExactSpelling=true)]
151                 public static extern IntPtr FetchRow(IntPtr r);
152
153                 ///<summary>Frees the memory allocated for a result set by <see cref="StoreResult"/></summary>
154                 //[SuppressUnmanagedCodeSecurity]
155                 [DllImport("libmySQL",
156                          EntryPoint="mysql_free_result", ExactSpelling=true)]
157                 public static extern void FreeResult(IntPtr r);
158
159                 ///<returns>Returns a string that represents the client library version</returns>
160                 //[SuppressUnmanagedCodeSecurity]
161                 [DllImport("libmySQL",
162                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
163                          EntryPoint="mysql_get_client_info", ExactSpelling=true)]
164                 public static extern string GetClientInfo();
165
166                 ///<returns></returns>
167                 //[SuppressUnmanagedCodeSecurity]
168                 [DllImport("libmySQL",
169                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
170                          EntryPoint="mysql_get_host_info", ExactSpelling=true)]
171                 public static extern string GetHostInfo(IntPtr db);
172
173                 ///<returns>A string describing the type of connection in use, including the server host name.</returns>
174                 //[SuppressUnmanagedCodeSecurity]
175                 [DllImport("libmySQL",
176                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
177                          EntryPoint="mysql_get_server_info", ExactSpelling=true)]
178                 public static extern string GetServerInfo(IntPtr db);
179
180                 ///<returns>A string describing the server status. null if an error occurred.</returns>
181                 //[SuppressUnmanagedCodeSecurity]
182                 [DllImport("libmySQL",
183                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
184                          EntryPoint="mysql_stat", ExactSpelling=true)]
185                 public static extern string Stat(IntPtr db);
186
187                 ///<summary>
188                 /// Returns a result set describing the current server
189                 /// threads.  This is the same kind of information as that
190                 /// reported by `mysqladmin processlist' or a `SHOW PROCESSLIST'
191                 /// query.  You must free the result set with
192                 /// <see cref="FreeResult"/>.
193                 ///</summary> 
194                 ///<returns>
195                 /// A IntPtr result set for success.  IntPtr.Zero if an error
196                 /// occurred.
197                 ///</returns> 
198                 //[SuppressUnmanagedCodeSecurity]
199                 [DllImport("libmySQL",
200                          EntryPoint="mysql_list_processes", ExactSpelling=true)]
201                 public static extern IntPtr ListProcesses(IntPtr db);
202
203                 ///<summary>
204                 ///<para>
205                 /// Returns a result set consisting of table names in
206                 /// the current database that match the simple regular expression
207                 /// specified by the <paramref name="wild"/> parameter.
208                 /// <paramref name="wild"/>may contain the wild-card characters
209                 /// "%" or "_", or may be a null pointer to match all tables.
210                 ///</para>
211                 ///<para>
212                 /// Calling <see cref="ListTables"/> is similar to executing
213                 /// the query "SHOW tables [LIKE wild]".
214                 ///</para>
215                 ///<para>
216                 /// You must free the result set with <see cref="FreeResult"/>.
217                 ///</para>
218                 ///</summary>
219                 //[SuppressUnmanagedCodeSecurity]
220                 [DllImport("libmySQL",
221                          EntryPoint="mysql_list_tables", ExactSpelling=true)]
222                 public static extern IntPtr ListTables(IntPtr db, [In] string wild);
223
224                 ///<summary>
225                 /// For the connection specified by <paramref name="db"/>,
226                 /// <see cref="Error"/> returns the
227                 /// error message for the most recently invoked API function
228                 /// that can succeed or fail.  An empty string ("") is
229                 /// returned if no error occurred.
230                 ///</summary>
231                 ///<returns>
232                 /// A string that describes the error.  An empty string if no error occurred.
233                 ///</returns>
234                 [DllImport("libmySQL",
235                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
236                          EntryPoint="mysql_error", ExactSpelling=true)]
237                 public static extern string Error(IntPtr db);
238
239                 ///<summary>
240                 ///<para>
241                 /// This function needs to be called before exiting
242                 /// to free memory allocated explicitly by
243                 /// <see cref="ThreadInit"/> or implicitly by
244                 /// <see cref="Init"/>.
245                 ///</para>
246                 ///<para>
247                 /// Note that this function is NOT invoked automatically by the client
248                 /// library!
249                 ///</para>
250                 ///</summary>
251                 [DllImport("libmySQL",
252                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
253                          EntryPoint="mysql_thread_end", ExactSpelling=true)]
254                 public static extern void ThreadEnd();
255
256                 ///<summary>
257                 ///<para>
258                 /// This function needs to be called for each created thread
259                 /// to initialize thread specific variables.
260                 ///</para>
261                 ///<para>
262                 /// This is automatically called by <see cref="Init"/>.
263                 ///</para>
264                 ///</summary>
265                 [DllImport("libmySQL",
266                          CharSet=System.Runtime.InteropServices.CharSet.Ansi,
267                          EntryPoint="mysql_thread_init", ExactSpelling=true)]
268                 public static extern void ThreadInit();
269         }
270 }