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
28 // TODO: more functions in the MySQL C Client API
29 // (libmysqlclient.so on linux and libmySQL.dll on cygwin)
30 // need to be added here as C# pinvoke methods.
31 // Other data structures may need to be added as well.
34 namespace Mono.Data.MySql {
36 using System.Security;
37 using System.Runtime.InteropServices;
41 /// MySql P/Invoke implementation
46 /// This is an incomplete implementation of the mysql C api,
47 /// but sufficient to run the Test sample application.
50 internal sealed class MySql {
51 ///<value>protocol version</value>
52 public static readonly uint ProtocolVersion = 10;
53 ///<value>server version</value>
54 public static readonly string ServerVersion = "3.23.49";
55 ///<value>server suffix</value>
56 public static readonly string ServerSuffix = "";
57 ///<value>server version as int</value>
58 public static readonly uint VersionId = 32349;
59 ///<value>server port number</value>
60 public static readonly uint Port = 3306;
61 ///<value>unix named socket</value>
62 public static readonly string UnixAddr = "/tmp/mysql.sock";
63 ///<value>character set</value>
64 public static readonly string CharSet = "latin1";
66 ///<summary>Gets or initializes a `MySql' structure</summary>
67 ///<returns>An initialized `MYSQL*' handle. IntPtr.Zero if there was insufficient memory to allocate a new object.</returns>
68 [SuppressUnmanagedCodeSecurity]
69 [DllImport("libmySQL", EntryPoint="mysql_init", ExactSpelling=true)]
70 public static extern IntPtr Init(IntPtr db);
72 ///<summary>Connects to a MySql server</summary>
73 ///<returns><paramref name="db"/> value on success, else returns IntPtr.Zero</returns>
74 [SuppressUnmanagedCodeSecurity]
75 [DllImport("libmySQL",
76 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
77 EntryPoint="mysql_connect", ExactSpelling=true)]
78 public static extern IntPtr Connect(IntPtr db,
79 [In] string host, [In] string user, [In] string passwd,
81 uint port, [In] string socketName, uint flags
84 ///<summary>Selects a database</summary>
85 ///<returns>Zero for success. Non-zero if an error occurred.</returns>
86 //[SuppressUnmanagedCodeSecurity]
87 [DllImport("libmySQL",
88 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
89 EntryPoint="mysql_select_db", ExactSpelling=true)]
90 public static extern int SelectDb(IntPtr conn, [In] string db);
92 ///<summary>Closes a server connection</summary>
93 //[SuppressUnmanagedCodeSecurity]
94 [DllImport("libmySQL", EntryPoint="mysql_close", ExactSpelling=true)]
95 public static extern void Close(IntPtr db);
97 ///<summary>Executes a SQL query specified as a string</summary>
98 ///<returns>number of rows changed, -1 if zero</returns>
99 //[SuppressUnmanagedCodeSecurity]
100 [DllImport("libmySQL",
101 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
102 EntryPoint="mysql_query", ExactSpelling=true)]
103 public static extern int Query(IntPtr conn, [In] string query);
105 ///<summary>Retrieves a complete result set to the client</summary>
106 ///<returns>An IntPtr result structure with the results. IntPtr.Zero if an error occurred.</returns>
107 //[SuppressUnmanagedCodeSecurity]
108 [DllImport("libmySQL",
109 EntryPoint="mysql_store_result", ExactSpelling=true)]
110 public static extern IntPtr StoreResult(IntPtr conn);
112 ///<returns>Returns the number of rows in a result set</returns>
113 //[SuppressUnmanagedCodeSecurity]
114 [DllImport("libmySQL",
115 EntryPoint="mysql_num_rows", ExactSpelling=true)]
116 public static extern int NumRows(IntPtr r);
118 ///<returns>Returns the number of columns in a result set</returns>
119 //[SuppressUnmanagedCodeSecurity]
120 [DllImport("libmySQL",
121 EntryPoint="mysql_num_fields", ExactSpelling=true)]
122 public static extern int NumFields(IntPtr r);
124 ///<returns>Returns an IntPtr to all field structures</returns>
125 //[SuppressUnmanagedCodeSecurity]
126 [DllImport("libmySQL",
127 EntryPoint="mysql_fetch_field", ExactSpelling=true)]
128 public static extern IntPtr FetchField(IntPtr r);
130 ///<summary>Retrieves the next row of a result set</summary>
131 ///<returns>An IntPtr structure for the next row. IntPtr.Zero if there are no more rows to retrieve or if an error occurred.</returns>
132 //[SuppressUnmanagedCodeSecurity]
133 [DllImport("libmySQL",
134 EntryPoint="mysql_fetch_row", ExactSpelling=true)]
135 public static extern IntPtr FetchRow(IntPtr r);
137 ///<summary>Frees the memory allocated for a result set by <see cref="StoreResult"/></summary>
138 //[SuppressUnmanagedCodeSecurity]
139 [DllImport("libmySQL",
140 EntryPoint="mysql_free_result", ExactSpelling=true)]
141 public static extern void FreeResult(IntPtr r);
143 ///<returns>Returns a string that represents the client library version</returns>
144 //[SuppressUnmanagedCodeSecurity]
145 [DllImport("libmySQL",
146 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
147 EntryPoint="mysql_get_client_info", ExactSpelling=true)]
148 public static extern string GetClientInfo();
150 ///<returns></returns>
151 //[SuppressUnmanagedCodeSecurity]
152 [DllImport("libmySQL",
153 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
154 EntryPoint="mysql_get_host_info", ExactSpelling=true)]
155 public static extern string GetHostInfo(IntPtr db);
157 ///<returns>A string describing the type of connection in use, including the server host name.</returns>
158 //[SuppressUnmanagedCodeSecurity]
159 [DllImport("libmySQL",
160 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
161 EntryPoint="mysql_get_server_info", ExactSpelling=true)]
162 public static extern string GetServerInfo(IntPtr db);
164 ///<returns>A string describing the server status. null if an error occurred.</returns>
165 //[SuppressUnmanagedCodeSecurity]
166 [DllImport("libmySQL",
167 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
168 EntryPoint="mysql_stat", ExactSpelling=true)]
169 public static extern string Stat(IntPtr db);
172 /// Returns a result set describing the current server
173 /// threads. This is the same kind of information as that
174 /// reported by `mysqladmin processlist' or a `SHOW PROCESSLIST'
175 /// query. You must free the result set with
176 /// <see cref="FreeResult"/>.
179 /// A IntPtr result set for success. IntPtr.Zero if an error
182 //[SuppressUnmanagedCodeSecurity]
183 [DllImport("libmySQL",
184 EntryPoint="mysql_list_processes", ExactSpelling=true)]
185 public static extern IntPtr ListProcesses(IntPtr db);
189 /// Returns a result set consisting of table names in
190 /// the current database that match the simple regular expression
191 /// specified by the <paramref name="wild"/> parameter.
192 /// <paramref name="wild"/>may contain the wild-card characters
193 /// "%" or "_", or may be a null pointer to match all tables.
196 /// Calling <see cref="ListTables"/> is similar to executing
197 /// the query "SHOW tables [LIKE wild]".
200 /// You must free the result set with <see cref="FreeResult"/>.
203 //[SuppressUnmanagedCodeSecurity]
204 [DllImport("libmySQL",
205 EntryPoint="mysql_list_tables", ExactSpelling=true)]
206 public static extern IntPtr ListTables(IntPtr db, [In] string wild);
209 /// For the connection specified by <paramref name="db"/>,
210 /// <see cref="Error"/> returns the
211 /// error message for the most recently invoked API function
212 /// that can succeed or fail. An empty string ("") is
213 /// returned if no error occurred.
216 /// A string that describes the error. An empty string if no error occurred.
218 [DllImport("libmySQL",
219 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
220 EntryPoint="mysql_error", ExactSpelling=true)]
221 public static extern string Error(IntPtr db);
225 /// This function needs to be called before exiting
226 /// to free memory allocated explicitly by
227 /// <see cref="ThreadInit"/> or implicitly by
228 /// <see cref="Init"/>.
231 /// Note that this function is NOT invoked automatically by the client
235 [DllImport("libmySQL",
236 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
237 EntryPoint="my_thread_end", ExactSpelling=true)]
238 public static extern void ThreadEnd();
242 /// This function needs to be called for each created thread
243 /// to initialize thread specific variables.
246 /// This is automatically called by <see cref="Init"/>.
249 [DllImport("libmySQL",
250 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
251 EntryPoint="my_thread_init", ExactSpelling=true)]
252 public static extern void ThreadInit();