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
26 // Permission is hereby granted, free of charge, to any person obtaining
27 // a copy of this software and associated documentation files (the
28 // "Software"), to deal in the Software without restriction, including
29 // without limitation the rights to use, copy, modify, merge, publish,
30 // distribute, sublicense, and/or sell copies of the Software, and to
31 // permit persons to whom the Software is furnished to do so, subject to
32 // the following conditions:
34 // The above copyright notice and this permission notice shall be
35 // included in all copies or substantial portions of the Software.
37 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
40 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
41 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
42 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
43 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 namespace Mono.Data.MySql {
48 using System.Security;
49 using System.Runtime.InteropServices;
51 internal sealed class MySql {
52 ///<value>protocol version</value>
53 public static readonly uint ProtocolVersion = 10;
54 ///<value>server version</value>
55 public static readonly string ServerVersion = "3.23.49";
56 ///<value>server suffix</value>
57 public static readonly string ServerSuffix = "";
58 ///<value>server version as int</value>
59 public static readonly uint VersionId = 32349;
60 ///<value>server port number</value>
61 public static readonly uint Port = 3306;
62 ///<value>unix named socket</value>
63 public static readonly string UnixAddr = "/tmp/mysql.sock";
64 ///<value>character set</value>
65 public static readonly string CharSet = "latin1";
67 ///<summary>Gets or initializes a `MySql' structure</summary>
68 ///<returns>An initialized `MYSQL*' handle. IntPtr.Zero if there was insufficient memory to allocate a new object.</returns>
69 [SuppressUnmanagedCodeSecurity]
70 [DllImport("libmySQL", EntryPoint="mysql_init", ExactSpelling=true)]
71 public static extern IntPtr Init(IntPtr db);
73 [SuppressUnmanagedCodeSecurity]
74 [DllImport("libmySQL", EntryPoint="mysql_affected_rows", ExactSpelling=true)]
75 public static extern ulong AffectedRows(IntPtr db);
76 //my_ulonglong mysql_affected_rows(MYSQL *mysql);
\r
78 ///<summary>Connects to a MySql server</summary>
79 ///<returns><paramref name="db"/> value on success, else returns IntPtr.Zero</returns>
80 [SuppressUnmanagedCodeSecurity]
81 [DllImport("libmySQL",
82 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
83 EntryPoint="mysql_connect", ExactSpelling=true)]
84 public static extern IntPtr Connect(IntPtr db,
85 [In] string host, [In] string user, [In] string passwd,
87 uint port, [In] string socketName, uint flags
90 ///<summary>Selects a database</summary>
91 ///<returns>Zero for success. Non-zero if an error occurred.</returns>
92 [SuppressUnmanagedCodeSecurity]
93 [DllImport("libmySQL",
94 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
95 EntryPoint="mysql_select_db", ExactSpelling=true)]
96 public static extern int SelectDb(IntPtr conn, [In] string db);
98 ///<summary>Closes a server connection</summary>
99 //[SuppressUnmanagedCodeSecurity]
100 [DllImport("libmySQL", EntryPoint="mysql_close", ExactSpelling=true)]
101 public static extern void Close(IntPtr db);
103 ///<summary>Executes a SQL query specified as a string</summary>
104 ///<returns>number of rows changed, -1 if zero</returns>
105 [SuppressUnmanagedCodeSecurity]
106 [DllImport("libmySQL",
107 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
108 EntryPoint="mysql_query", ExactSpelling=true)]
109 public static extern int Query(IntPtr conn, [In] string query);
111 ///<summary>Retrieves a complete result set to the client</summary>
112 ///<returns>An IntPtr result structure with the results. IntPtr.Zero if an error occurred.</returns>
113 [SuppressUnmanagedCodeSecurity]
114 [DllImport("libmySQL",
115 EntryPoint="mysql_store_result", ExactSpelling=true)]
116 public static extern IntPtr StoreResult(IntPtr conn);
118 ///<returns>Returns the number of rows in a result set</returns>
119 [SuppressUnmanagedCodeSecurity]
120 [DllImport("libmySQL",
121 EntryPoint="mysql_num_rows", ExactSpelling=true)]
122 public static extern int NumRows(IntPtr r);
124 ///<returns>Returns the number of columns in a result set</returns>
125 [SuppressUnmanagedCodeSecurity]
126 [DllImport("libmySQL",
127 EntryPoint="mysql_num_fields", ExactSpelling=true)]
128 public static extern int NumFields(IntPtr r);
130 ///<returns>Returns an IntPtr to all field structures</returns>
131 [SuppressUnmanagedCodeSecurity]
132 [DllImport("libmySQL",
133 EntryPoint="mysql_fetch_field", ExactSpelling=true)]
134 public static extern IntPtr FetchField(IntPtr r);
136 ///<summary>Retrieves the next row of a result set</summary>
137 ///<returns>An IntPtr structure for the next row. IntPtr.Zero if there are no more rows to retrieve or if an error occurred.</returns>
138 [SuppressUnmanagedCodeSecurity]
139 [DllImport("libmySQL",
140 EntryPoint="mysql_fetch_row", ExactSpelling=true)]
141 public static extern IntPtr FetchRow(IntPtr r);
143 ///<summary>Frees the memory allocated for a result set by <see cref="StoreResult"/></summary>
144 [SuppressUnmanagedCodeSecurity]
145 [DllImport("libmySQL",
146 EntryPoint="mysql_free_result", ExactSpelling=true)]
147 public static extern void FreeResult(IntPtr r);
149 ///<returns>Returns a string that represents the client library version</returns>
150 [SuppressUnmanagedCodeSecurity]
151 [DllImport("libmySQL",
152 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
153 EntryPoint="mysql_get_client_info", ExactSpelling=true)]
154 public static extern string GetClientInfo();
156 ///<returns></returns>
157 [SuppressUnmanagedCodeSecurity]
158 [DllImport("libmySQL",
159 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
160 EntryPoint="mysql_get_host_info", ExactSpelling=true)]
161 public static extern string GetHostInfo(IntPtr db);
163 ///<returns>A string describing the type of connection in use, including the server host name.</returns>
164 [SuppressUnmanagedCodeSecurity]
165 [DllImport("libmySQL",
166 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
167 EntryPoint="mysql_get_server_info", ExactSpelling=true)]
168 public static extern string GetServerInfo(IntPtr db);
170 ///<returns>A string describing the server status. null if an error occurred.</returns>
171 [SuppressUnmanagedCodeSecurity]
172 [DllImport("libmySQL",
173 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
174 EntryPoint="mysql_stat", ExactSpelling=true)]
175 public static extern string Stat(IntPtr db);
178 /// Returns a result set describing the current server
179 /// threads. This is the same kind of information as that
180 /// reported by `mysqladmin processlist' or a `SHOW PROCESSLIST'
181 /// query. You must free the result set with
182 /// <see cref="FreeResult"/>.
185 /// A IntPtr result set for success. IntPtr.Zero if an error
188 [SuppressUnmanagedCodeSecurity]
189 [DllImport("libmySQL",
190 EntryPoint="mysql_list_processes", ExactSpelling=true)]
191 public static extern IntPtr ListProcesses(IntPtr db);
195 /// Returns a result set consisting of table names in
196 /// the current database that match the simple regular expression
197 /// specified by the <paramref name="wild"/> parameter.
198 /// <paramref name="wild"/>may contain the wild-card characters
199 /// "%" or "_", or may be a null pointer to match all tables.
202 /// Calling <see cref="ListTables"/> is similar to executing
203 /// the query "SHOW tables [LIKE wild]".
206 /// You must free the result set with <see cref="FreeResult"/>.
209 [SuppressUnmanagedCodeSecurity]
210 [DllImport("libmySQL",
211 EntryPoint="mysql_list_tables", ExactSpelling=true)]
212 public static extern IntPtr ListTables(IntPtr db, [In] string wild);
215 /// For the connection specified by <paramref name="db"/>,
216 /// <see cref="Error"/> returns the
217 /// error message for the most recently invoked API function
218 /// that can succeed or fail. An empty string ("") is
219 /// returned if no error occurred.
222 /// A string that describes the error. An empty string if no error occurred.
224 [SuppressUnmanagedCodeSecurity]
225 [DllImport("libmySQL",
226 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
227 EntryPoint="mysql_error", ExactSpelling=true)]
228 public static extern string Error(IntPtr db);
232 /// This function needs to be called before exiting
233 /// to free memory allocated explicitly by
234 /// <see cref="ThreadInit"/> or implicitly by
235 /// <see cref="Init"/>.
238 /// Note that this function is NOT invoked automatically by the client
242 [SuppressUnmanagedCodeSecurity]
243 [DllImport("libmySQL",
244 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
245 EntryPoint="my_thread_end", ExactSpelling=true)]
246 public static extern void ThreadEnd();
250 /// This function needs to be called for each created thread
251 /// to initialize thread specific variables.
254 /// This is automatically called by <see cref="Init"/>.
257 [SuppressUnmanagedCodeSecurity]
258 [DllImport("libmySQL",
259 CharSet=System.Runtime.InteropServices.CharSet.Ansi,
260 EntryPoint="my_thread_init", ExactSpelling=true)]
261 public static extern void ThreadInit();