4 // Provides the core of C# bindings
5 // to the MySQL library libMySQL.dll
8 // Brad Merrill <zbrad@cybercom.net>
9 // Daniel Morgan <danmorg@sc.rr.com>
11 // (C)Copyright 2002 Brad Merril
12 // (C)Copyright 2002 Daniel Morgan
14 // http://www.cybercom.net/~zbrad/DotNet/MySql/
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
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
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
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
37 // This is so we can retrieve Field data from MySQL.
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.
44 // TODO: handle the name of the MySQL client library for
45 // different platforms because it is named differently
47 // We maybe using a config file for this.
50 namespace Mono.Data.MySql {
52 using System.Security;
53 using System.Runtime.InteropServices;
57 /// MySql P/Invoke implementation
62 /// This is an incomplete implementation of the mysql C api,
63 /// but sufficient to run the Test sample application.
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";
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);
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,
97 uint port, [In] string socketName, uint flags
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);
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);
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);
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);
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);
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);
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);
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);
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);
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();
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);
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);
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);
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"/>.
195 /// A IntPtr result set for success. IntPtr.Zero if an error
198 //[SuppressUnmanagedCodeSecurity]
199 [DllImport("libmySQL",
200 EntryPoint="mysql_list_processes", ExactSpelling=true)]
201 public static extern IntPtr ListProcesses(IntPtr db);
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.
212 /// Calling <see cref="ListTables"/> is similar to executing
213 /// the query "SHOW tables [LIKE wild]".
216 /// You must free the result set with <see cref="FreeResult"/>.
219 //[SuppressUnmanagedCodeSecurity]
220 [DllImport("libmySQL",
221 EntryPoint="mysql_list_tables", ExactSpelling=true)]
222 public static extern IntPtr ListTables(IntPtr db, [In] string wild);
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.
232 /// A string that describes the error. An empty string if no error occurred.
234 [DllImport("libmySQL",
235 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
236 EntryPoint="mysql_error", ExactSpelling=true)]
237 public static extern string Error(IntPtr db);
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"/>.
247 /// Note that this function is NOT invoked automatically by the client
251 [DllImport("libmySQL",
252 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
253 EntryPoint="mysql_thread_end", ExactSpelling=true)]
254 public static extern void ThreadEnd();
258 /// This function needs to be called for each created thread
259 /// to initialize thread specific variables.
262 /// This is automatically called by <see cref="Init"/>.
265 [DllImport("libmySQL",
266 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
267 EntryPoint="mysql_thread_init", ExactSpelling=true)]
268 public static extern void ThreadInit();