1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
//
// Mono.Data.MySql.MyConnection.cs
//
// Author:
// Daniel Morgan (danmorg@sc.rr.com)
// Tim Coleman (tim@timcoleman.com)
//
// (C) Daniel Morgan 2002
// Copyright (C) Tim Coleman, 2002
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Runtime.InteropServices;
using System.Text;
namespace Mono.Data.MySql {
public sealed class MySqlConnection : Component, ICloneable, IDbConnection {
#region Fields
private IntPtr mysqlInitStruct = IntPtr.Zero;
private IntPtr mysqlConn = IntPtr.Zero;
private string connectionString = "";
private string mysqlConnectionString = "";
private MySqlTransaction trans = null;
private int connectionTimeout = 15;
// default for 15 seconds
// MySQL connection string parameters
string host = "";
string user = "";
string passwd = "";
string dbname = "";
uint port = MySql.Port;
string socketName = "";
uint flags = 0;
// connection state
private ConnectionState conState = ConnectionState.Closed;
// DataReader state
//private MySqlDataReader rdr = null;
private bool dataReaderOpen = false;
// FIXME: if true, throw an exception if SqlConnection
// is used for anything other than reading
// data using SqlDataReader
private string versionString = "Unknown";
private bool disposed = false;
#endregion // Fields
#region Constructors
// A lot of the defaults were initialized in the Fields
[MonoTODO]
public MySqlConnection () {
}
[MonoTODO]
public MySqlConnection (String connectionString) {
SetConnectionString (connectionString);
}
#endregion // Constructors
#region Destructors
protected override void Dispose(bool disposing) {
if(!this.disposed)
try {
if(disposing) {
// release any managed resources
trans = null;
}
// release any unmanaged resources
mysqlInitStruct = IntPtr.Zero;
IntPtr mysqlConn = IntPtr.Zero;
// close any handles
this.disposed = true;
}
finally {
base.Dispose(disposing);
}
}
// aka Finalize
~MySqlConnection() {
Dispose (false);
}
#endregion // Destructors
#region Public Methods
IDbTransaction IDbConnection.BeginTransaction () {
return BeginTransaction ();
}
public MySqlTransaction BeginTransaction () {
return TransactionBegin (); // call private method
}
IDbTransaction IDbConnection.BeginTransaction (IsolationLevel
il) {
return BeginTransaction (il);
}
public MySqlTransaction BeginTransaction (IsolationLevel il) {
return TransactionBegin (il); // call private method
}
[MonoTODO]
public MySqlTransaction BeginTransaction(string transactionName) {
// FIXME: Can MySQL handle named transactions?
return TransactionBegin (); // call private method
}
[MonoTODO]
public MySqlTransaction BeginTransaction(IsolationLevel iso,
string transactionName) {
// FIXME: Can MySQL handle named transactions?
return TransactionBegin (iso); // call private method
}
public void ChangeDatabase (string databaseName) {
dbname = databaseName;
int sdb = MySql.SelectDb(mysqlInitStruct, dbname);
if (sdb != 0) {
string msg =
"MySql Error: " +
"Can not select the " +
dbname +
" database because: " +
MySql.Error(mysqlInitStruct);
throw new MySqlException (msg);
}
}
object ICloneable.Clone() {
throw new NotImplementedException ();
}
[MonoTODO]
public void Close () {
if(dataReaderOpen == true) {
// TODO: what do I do if
// the user Closes the connection
// without closing the Reader first?
}
CloseDataSource ();
}
IDbCommand IDbConnection.CreateCommand () {
return CreateCommand ();
}
public MySqlCommand CreateCommand () {
MySqlCommand sqlcmd = new MySqlCommand ("", this);
return sqlcmd;
}
[MonoTODO]
public void Open () {
if(dbname.Equals(""))
throw new InvalidOperationException(
"dbname missing");
else if(conState == ConnectionState.Open)
throw new InvalidOperationException(
"ConnnectionState is already Open");
else if(connectionString.Equals(String.Empty))
throw new InvalidOperationException(
"ConnectionString is not set");
// FIXME: check to make sure we have
// everything to connect,
// otherwise, throw an exception
mysqlInitStruct = MySql.Init(IntPtr.Zero);
if (mysqlInitStruct == IntPtr.Zero) {
throw new MySqlException("MySQL Init failed.");
}
// *** this is what it should be ***
//mysqlConn = MySql.Connect(mysqlInitStruct,
// host.Equals("") ? null : host,
// user.Equals("") ? null : user,
// passwd.Equals("") ? null : passwd,
// dbname.Equals("") ? null : dbname,
// port,
// socketName.Equals("") ? null : socketName,
// flags);
//
mysqlConn = MySql.Connect(mysqlInitStruct,
host,
user,
passwd,
dbname,
port,
socketName,
flags);
if (mysqlConn == IntPtr.Zero) {
string msg = "MySQL Connect failed, " +
MySql.Error(mysqlInitStruct);
throw new MySqlException(msg);
}
this.ChangeDatabase (dbname);
// Successfully Connected
SetupConnection();
}
#endregion // Public Methods
#region Protected Methods
#endregion
#region Internal Methods
// Used to prevent MySqlConnection
// from doing anything while
// MySqlDataReader is open.
// Open the Reader. (called from MySqlCommand)
/*
internal void OpenReader(MySqlDataReader reader) {
if(dataReaderOpen == true) {
// TODO: throw exception here?
// because a reader
// is already open
}
else {
rdr = reader;
dataReaderOpen = true;
}
}
*/
// Used to prevent MySqlConnection
// from doing anything while
// MySqlDataReader is open
// Close the Reader (called from MySqlCommand)
// if closeConnection true, Close() the connection
// this is based on CommandBehavior.CloseConnection
internal void CloseReader(bool closeConnection) {
if(closeConnection == true)
CloseDataSource();
else
dataReaderOpen = false;
}
#endregion // Internal Methods
#region Private Methods
private void SetupConnection() {
conState = ConnectionState.Open;
versionString = GetDatabaseServerVersion();
}
private string GetDatabaseServerVersion() {
MySqlCommand cmd = new MySqlCommand("select version()",this);
return (string) cmd.ExecuteScalar();
}
private void CloseDataSource () {
// FIXME: just a quick hack
if(conState == ConnectionState.Open) {
/*
if(trans != null)
if(trans.DoingTransaction == true) {
trans.Rollback();
// trans.Dispose();
trans = null;
}
*/
conState = ConnectionState.Closed;
MySql.Close(mysqlInitStruct);
MySql.ThreadEnd();
mysqlConn = IntPtr.Zero;
}
}
void SetConnectionString (string connectionString) {
this.connectionString = connectionString;
connectionString += ";";
NameValueCollection parameters = new NameValueCollection ();
if (connectionString == String.Empty)
return;
bool inQuote = false;
bool inDQuote = false;
string name = String.Empty;
string value = String.Empty;
StringBuilder sb = new StringBuilder ();
foreach (char c in connectionString) {
switch (c) {
case '\'':
inQuote = !inQuote;
break;
case '"' :
inDQuote = !inDQuote;
break;
case ';' :
if (!inDQuote && !inQuote) {
if (name != String.Empty && name != null) {
value = sb.ToString ();
parameters [name.ToUpper ().Trim ()] = value.Trim ();
}
name = String.Empty;
value = String.Empty;
sb = new StringBuilder ();
}
else
sb.Append (c);
break;
case '=' :
if (!inDQuote && !inQuote) {
name = sb.ToString ();
sb = new StringBuilder ();
}
else
sb.Append (c);
break;
default:
sb.Append (c);
break;
}
}
SetProperties (parameters);
}
private void SetProperties (NameValueCollection parameters) {
StringBuilder connectionStr = new StringBuilder();
string value;
foreach (string name in parameters) {
value = parameters[name];
bool found = true;
switch (name) {
case "PORT" :
port = UInt32.Parse(value);
break;
case "DATA SOURCE" :
case "SERVER" :
case "HOST" :
// set DataSource property
host = value;
break;
case "INITIAL CATALOG" :
case "DATABASE" :
case "DBNAME" :
// set Database property
dbname = value;
break;
case "PASSWORD" :
case "PWD" :
case "PASSWD" :
passwd = value;
break;
case "USER ID" :
case "UID" :
case "USER" :
user = value;
break;
case "SOCKETNAME":
socketName = value;
break;
case "FLAGS" :
// FIXME: how to get these flags and
// and pass to MySQL?
// flags is a bitfield
flags = UInt32.Parse(value);
break;
default:
found = false;
// FIXME: throw exception?
break;
}
if (found == true) {
string valuePair = name + "=" + value;
connectionStr.Append (valuePair + " ");
}
}
this.mysqlConnectionString = connectionStr.ToString ();
}
private MySqlTransaction TransactionBegin () {
// FIXME: need to keep track of
// transaction in-progress
trans = new MySqlTransaction ();
// using internal methods of SqlTransaction
trans.SetConnection (this);
trans.Begin();
return trans;
}
private MySqlTransaction TransactionBegin (IsolationLevel il) {
// FIXME: need to keep track of
// transaction in-progress
trans = new MySqlTransaction ();
// using internal methods of MySqlTransaction
trans.SetConnection (this);
trans.SetIsolationLevel (il);
trans.Begin();
return trans;
}
#endregion
#region Public Properties
[MonoTODO]
public ConnectionState State {
get {
return conState;
}
}
public string ConnectionString {
get {
return connectionString;
}
set {
SetConnectionString (value);
}
}
public int ConnectionTimeout {
get {
return connectionTimeout;
}
}
public string Database {
get {
return dbname;
}
}
public string DataSource {
get {
return host;
}
}
public int PacketSize {
get {
throw new NotImplementedException ();
}
}
public string ServerVersion {
get {
return versionString;
}
}
#endregion // Public Properties
#region Internal Properties
// For Mono.Data.MySql classes
// to get the current transaction
// in progress - if any
internal MySqlTransaction Transaction {
get {
return trans;
}
}
// For Mono.Data.MySql classes
// to get the unmanaged MySql connection
internal IntPtr NativeMySqlConnection {
get {
return mysqlConn;
}
}
// For Mono.Data.MySql classes
// to get the unmanaged MySql connection
internal IntPtr NativeMySqlInitStruct {
get {
return mysqlInitStruct;
}
}
// Used to prevent SqlConnection
// from doing anything while
// SqlDataReader is open
internal bool IsReaderOpen {
get {
return dataReaderOpen;
}
}
#endregion // Internal Properties
#region Events
/*
public event
MyInfoMessageEventHandler InfoMessage;
public event
StateChangeEventHandler StateChange;
*/
#endregion
}
}
|