File: OdbcConnection.cs

package info (click to toggle)
virtuoso-opensource 7.2.12%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 299,308 kB
  • sloc: ansic: 655,054; sql: 508,209; xml: 269,573; java: 84,064; javascript: 79,847; cpp: 37,662; sh: 32,429; cs: 25,702; php: 12,690; yacc: 11,666; lex: 7,933; makefile: 7,308; jsp: 4,523; awk: 1,719; perl: 1,013; ruby: 1,003; python: 326
file content (866 lines) | stat: -rw-r--r-- 21,519 bytes parent folder | download
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//  
//  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
//  project.
//  
//  Copyright (C) 1998-2024 OpenLink Software
//  
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License as published by the
//  Free Software Foundation; only version 2 of the License, dated June 1991.
//  
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License along
//  with this program; if not, write to the Free Software Foundation, Inc.,
//  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//  
//  
//
// $Id$
//

using System;
using System.Collections;
using System.Configuration;
using System.Diagnostics;
using System.EnterpriseServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

#if ODBC_CLIENT
namespace OpenLink.Data.VirtuosoOdbcClient
#elif CLIENT
namespace OpenLink.Data.VirtuosoClient
#else
namespace OpenLink.Data.Virtuoso
#endif
{
	internal sealed class OdbcConnection : InnerConnectionBase, ICreateErrors, IDisposable
	{
		private static string driver = "OpenLink Virtuoso Driver";

		/// <summary>
		/// ODBC environment handle.
		/// </summary>
		private static IntPtr henv;

		/// <summary>
		/// connection count.
		/// </summary>
		private static int connections;

		/// <summary>
		/// ODBC connection handle.
		/// </summary>
		internal IntPtr hdbc;

		//
		// List of statements
		//
#if !WIN32_FINALIZERS

		private class Item
		{
			internal Item (IntPtr hstmt, VirtuosoCommand command)
			{
				this.hstmt = hstmt;
				this.weakCommand = command == null ? null : new WeakReference (command);
			}

			internal IntPtr hstmt;
			internal WeakReference weakCommand;
		};

		private ArrayList list;

#else

		// struct Head
		// {
		//	int spinlock;
		//	int refcount;
		//	int eltcount;
		//	IntPtr* elts;
		// };
		private const int INITIAL_ELT_COUNT = 16;
		private readonly static int ELT_SIZE = IntPtr.Size;
		private IntPtr head;
		private ArrayList commands;

#endif

		static OdbcConnection ()
		{
#if MONO || !ADONET2
			string d = System.Configuration.ConfigurationSettings.AppSettings["driver"];
#else
			string d = System.Configuration.ConfigurationManager.AppSettings["driver"];
#endif
			if (d != null && d != "")
				driver = d;

			henv = IntPtr.Zero;
			connections = 0;
		}

		internal OdbcConnection ()
			: this (0)
		{
		}

		internal OdbcConnection (int eltcount)
		{
#if !WIN32_FINALIZERS
			if (eltcount != 0)
				list = new ArrayList (eltcount);
			else
				list = new ArrayList ();
#else
			if (eltcount != 0)
				commands = new ArrayList (eltcount);
			else
				commands = new ArrayList ();

			int size = 12 + IntPtr.Size;
			head = Marshal.AllocCoTaskMem (size);
			Marshal.WriteInt32 (head, 0, 0);
			Marshal.WriteInt32 (head, 4, 0);
			Marshal.WriteInt32 (head, 8, 0);
			Marshal.WriteIntPtr (head, 12, IntPtr.Zero);
			if (eltcount > 0)
			{
				IntPtr elts = Marshal.AllocCoTaskMem (eltcount * ELT_SIZE);
				for (int i = 0; i < eltcount; i++)
					Marshal.WriteIntPtr (elts, i * ELT_SIZE, IntPtr.Zero);
				Marshal.WriteInt32 (head, 8, eltcount);
				Marshal.WriteIntPtr (head, 12, elts);
			}
			Reference ();
#endif
		}

		public VirtuosoErrorCollection CreateErrors ()
		{
			if (hdbc == IntPtr.Zero)
				return OdbcErrors.CreateErrors (CLI.HandleType.SQL_HANDLE_ENV, henv);
			else
				return OdbcErrors.CreateErrors (CLI.HandleType.SQL_HANDLE_DBC, hdbc);
		}

#if ADONET2
// jch todo
        public override string ServerVersion
        {
            get
            {
                string version = "";
                return version;
            }
        }

        public override string ServerName
        {
            get
            {
                string name = "";
                return name;
            }
        }

        public override CLI.IdentCase IdentCase
        {
            get
            {
                int caseMode = 0;
                if (caseMode == 2)
                  return CLI.IdentCase.SQL_IC_MIXED;
                else if (caseMode == 1)
                  return CLI.IdentCase.SQL_IC_UPPER;
                else
                  return CLI.IdentCase.SQL_IC_SENSITIVE;
            }
        }

        public override CLI.IdentCase QuotedIdentCase
        {
            get
            {
                return CLI.IdentCase.SQL_IC_SENSITIVE;
            }
        }

        public override string UserName
        {
            get
            {
                string username = "";
                return username;
            }
        }

#endif

		public override bool IsValid ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "OdbcConnection.IsValid ()");

			if (hdbc == IntPtr.Zero)
				return false;

			int value = 0, length;
			CLI.ReturnCode rc;
			unsafe
			{
				IntPtr valuePtr = new IntPtr (&value);
				rc = (CLI.ReturnCode) CLI.SQLGetConnectAttr (hdbc,
					(int) CLI.ConnectionAttribute.SQL_ATTR_CONNECTION_DEAD,
					valuePtr, (int) CLI.LengthCode.SQL_IS_UINTEGER, out length);
			}
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				return false;

			return (value == 0);
		}

		public override void Open (ConnectionOptions options)
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "OdbcConnection.Open ()");

			OnConnect ();
			try
			{
				Connect (options);
			}
			catch (Exception)
			{
				OnDisconnect ();
				throw;
			}
		}

		public override void Close ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "OdbcConnection.Close ()");

			//statementList.CloseAll ();
			CloseAll ();
			Disconnect ();
			OnDisconnect ();
		}

		public override void Pool ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "OdbcConnection.Pool ()");

			//statementList.CloseAll ();
			CloseAll ();
		}

		public override IInnerCommand CreateInnerCommand (VirtuosoCommand outerCommand)
		{
			IntPtr hstmt;

			CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLAllocHandle (
				(short) CLI.HandleType.SQL_HANDLE_STMT,
				hdbc,
				out hstmt);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				HandleConnectionErrors (rc);

			try
			{
				//statementList.Add (hstmt, outerCommand);
				Add (hstmt, outerCommand);
			}
			catch (Exception)
			{
				CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_STMT, hstmt);
				throw;
			}

			GC.KeepAlive (this);
			return new OdbcCommand (hstmt, outerCommand);
		}

		public void DeleteInnerCommand (OdbcCommand odbcCommand, VirtuosoCommand outerCommand)
		{
			//statementList.Remove (odbcCommand.hstmt, outerCommand);
			Remove (odbcCommand.hstmt, outerCommand);
		}

		public override void BeginTransaction (CLI.IsolationLevel level)
		{
			CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLSetConnectAttr (
				hdbc,
				(int) CLI.ConnectionAttribute.SQL_ATTR_AUTOCOMMIT,
				(IntPtr) (int) CLI.AutoCommit.SQL_AUTOCOMMIT_OFF,
				(int) CLI.LengthCode.SQL_IS_UINTEGER);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				HandleConnectionErrors (rc);
			rc = (CLI.ReturnCode) CLI.SQLSetConnectAttr (
				hdbc,
				(int) CLI.ConnectionAttribute.SQL_ATTR_TXN_ISOLATION,
				(IntPtr) (int) level,
				(int) CLI.LengthCode.SQL_IS_UINTEGER);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
			{
				if (rc == CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
					Diagnostics.HandleWarnings (this, OuterConnection);
				else
				{
					try
					{
						Diagnostics.HandleErrors (rc, CLI.HandleType.SQL_HANDLE_DBC, hdbc);
					}
					finally
					{
						CLI.SQLSetConnectAttr (
							hdbc,
							(int) CLI.ConnectionAttribute.SQL_ATTR_AUTOCOMMIT,
							(IntPtr) (int) CLI.AutoCommit.SQL_AUTOCOMMIT_ON,
							(int) CLI.LengthCode.SQL_IS_UINTEGER);
					}
				}
			}

			GC.KeepAlive (this);
		}

		public override void EndTransaction (bool commit)
		{
			CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLEndTran (
				(short) CLI.HandleType.SQL_HANDLE_DBC, hdbc,
				(short) (commit ? CLI.CompletionType.SQL_COMMIT : CLI.CompletionType.SQL_ROLLBACK));
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				HandleConnectionErrors (rc);

			rc = (CLI.ReturnCode) CLI.SQLSetConnectAttr (
				hdbc,
				(int) CLI.ConnectionAttribute.SQL_ATTR_AUTOCOMMIT,
				(IntPtr) (int) CLI.AutoCommit.SQL_AUTOCOMMIT_ON,
				(int) CLI.LengthCode.SQL_IS_UINTEGER);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				HandleConnectionErrors (rc);

			GC.KeepAlive (this);
		}

		public override void Enlist (object distributedTransaction)
		{
			ITransaction transaction = (ITransaction) distributedTransaction;

			IntPtr pIUnknown = IntPtr.Zero;
			if (transaction != null)
				pIUnknown = Marshal.GetIUnknownForObject (transaction);

			CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLSetConnectAttr (
				hdbc,
				(int) CLI.ConnectionAttribute.SQL_ATTR_ENLIST_IN_DTC,
				pIUnknown,
				(int) CLI.LengthCode.SQL_IS_POINTER);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				HandleConnectionErrors (rc);

			GC.KeepAlive (this);
		}

		public override string GetCurrentCatalog ()
		{
			MemoryHandle buffer = new MemoryHandle ((CLI.SQL_MAX_COLUMN_NAME_LEN + 1) * Platform.WideCharSize);
			int length;
			CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLGetConnectAttr (
				hdbc,
				(int) CLI.ConnectionAttribute.SQL_ATTR_CURRENT_CATALOG,
				buffer.Handle,
				buffer.Length,
				out length);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				HandleConnectionErrors (rc);

			GC.KeepAlive (this);
			return Platform.WideCharsToString (buffer.Handle, length);
		}

		public override void SetCurrentCatalog (string name)
		{
			IntPtr buffer = IntPtr.Zero;
			try
			{
				int length = (name.Length + 1) * Platform.WideCharSize;
				buffer = Platform.MarshalAlloc (length);
				Platform.StringToWideChars (name, buffer, length);
				CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLSetConnectAttr (
					hdbc,
					(int) CLI.ConnectionAttribute.SQL_ATTR_CURRENT_CATALOG,
					buffer,
					(int) CLI.LengthCode.SQL_NTS);
				if (rc != CLI.ReturnCode.SQL_SUCCESS)
					HandleConnectionErrors (rc);
			}
			finally
			{
				if (buffer != IntPtr.Zero)
					Platform.MarshalFree (buffer);
			}

			GC.KeepAlive (this);
		}

		internal void HandleConnectionErrors (CLI.ReturnCode rc)
		{
			Diagnostics.HandleResult (rc, this, OuterConnection);
		}

		private string GetOdbcString (ConnectionOptions options)
		{
			StringBuilder sb = new StringBuilder ("DRIVER={" + driver + "};");
			if (options.DataSource!= null)
				sb.AppendFormat ("HOST={0};", options.DataSource);
			if (options.UserId != null)
				sb.AppendFormat ("UID={0};", options.UserId);
			if (options.Password != null)
				sb.AppendFormat ("PWD={0};", options.Password);
			if (options.Database != null)
				sb.AppendFormat ("DATABASE={0};", options.Database);
			if (options.Charset != null)
				sb.AppendFormat ("CHARSET={0};", options.Charset);
			if (options.Encrypt != null)
				sb.AppendFormat ("ENCRYPT={0};", options.Encrypt);
			return sb.ToString ();
		}

		private void Connect (ConnectionOptions options)
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "OdbcConnection.Connect ()");

			string connectionString = GetOdbcString (options);

			CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLAllocHandle (
				(short) CLI.HandleType.SQL_HANDLE_DBC,
				henv,
				out hdbc);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				Diagnostics.HandleResult (rc, CLI.HandleType.SQL_HANDLE_ENV, henv, OuterConnection);

#if false
			IntPtr hwnd = Platform.GetDesktopWindow ();
#else
			IntPtr hwnd = Platform.GetForegroundWindow ();
#endif

#if WIDE_CHAR_CONNECT && !WIN32_ONLY
			MemoryHandle inBuffer = null;
#endif
			MemoryHandle outBuffer = null;
			try
			{
				short length;
				int bufferSize = (CLI.SQL_MAX_CONNECTION_STRING_LEN + 1) * Platform.WideCharSize;
				outBuffer = new MemoryHandle (bufferSize);

#if WIDE_CHAR_CONNECT && !WIN32_ONLY
				inBuffer = new MemoryHandle (bufferSize);
				Platform.StringToWideChars (connectionString, inBuffer, bufferSize);
				rc = (CLI.ReturnCode) CLI.SQLDriverConnect (
					hdbc, hwnd,
					inBuffer, (short) CLI.LengthCode.SQL_NTS,
					outBuffer.Handle, (short) outBuffer.Length, out length,
					(short) CLI.DriverCompletion.SQL_DRIVER_COMPLETE);
#else
				rc = (CLI.ReturnCode) CLI.SQLDriverConnect (
					hdbc, hwnd,
					connectionString, (short) CLI.LengthCode.SQL_NTS,
					outBuffer.Handle, (short) outBuffer.Length, out length,
					(short) CLI.DriverCompletion.SQL_DRIVER_COMPLETE);
#endif
				if (rc != CLI.ReturnCode.SQL_SUCCESS)
				{
					if (rc == CLI.ReturnCode.SQL_NO_DATA)
						throw new InvalidOperationException ("The connection was canceled by the user.");
					HandleConnectionErrors (rc);
				}

#if WIDE_CHAR_CONNECT
				connectionString = Platform.WideCharsToString (outBuffer.Handle, length);
#else
				connectionString = Marshal.PtrToStringAnsi (outBuffer.Handle, length);
#endif	
			}
			catch (Exception)
			{
				CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_DBC, hdbc);
				hdbc = IntPtr.Zero;
				throw;
			}
			finally
			{
#if WIDE_CHAR_CONNECT && !WIN32_ONLY
				if (inBuffer != null)
					inBuffer.Dispose ();
#endif
				if (outBuffer != null)
					outBuffer.Dispose ();
			}

			try
			{
				rc = (CLI.ReturnCode) CLI.SQLSetConnectAttr (
					hdbc,
					(int) CLI.ConnectionAttribute.SQL_ATTR_CONNECTION_TIMEOUT,
					(IntPtr) options.ConnectionTimeout,
					(int) CLI.LengthCode.SQL_IS_INTEGER);
				if (rc != CLI.ReturnCode.SQL_SUCCESS)
					HandleConnectionErrors (rc);
			}
			catch (Exception)
			{
				Disconnect ();
				throw;
			}

			GC.KeepAlive (this);
		}

		private void Disconnect ()
		{
			CLI.SQLDisconnect (hdbc);
			CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_DBC, hdbc);
			hdbc = IntPtr.Zero;

			GC.KeepAlive (this);
		}

		private static void OnConnect ()
		{
			if (1 == Interlocked.Increment (ref connections))
			{
				try
				{
					InitHEnv ();
				} 
				catch (Exception) 
				{
					Interlocked.Decrement (ref connections);
					throw;
				}
			}
		}

		private static void OnDisconnect ()
		{
			if (0 == Interlocked.Decrement (ref connections))
			{
				FreeHEnv ();
			}
		}

		private static void InitHEnv ()
		{
			CLI.ReturnCode rc;

#if USE_DRIVER_MANAGER
			rc = (CLI.ReturnCode) CLI.SQLSetEnvAttr (
				IntPtr.Zero,
				(int) CLI.EnvironmentAttribute.SQL_ATTR_CONNECTION_POOLING,
				(IntPtr) (int) CLI.ConnectionPooling.SQL_CP_OFF,
				(int) CLI.LengthCode.SQL_IS_INTEGER);
			if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
				Diagnostics.HandleErrors (rc, CLI.HandleType.SQL_HANDLE_ENV, IntPtr.Zero);
#endif

			rc = (CLI.ReturnCode) CLI.SQLAllocHandle (
				(short) CLI.HandleType.SQL_HANDLE_ENV,
				CLI.SQL_NULL_HANDLE,
				out henv);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
				Diagnostics.HandleErrors (rc, CLI.HandleType.SQL_HANDLE_ENV, IntPtr.Zero);

			rc = (CLI.ReturnCode) CLI.SQLSetEnvAttr (
				henv,
				(int) CLI.EnvironmentAttribute.SQL_ATTR_ODBC_VERSION,
				(IntPtr) (int) CLI.OdbcVersion.SQL_OV_ODBC3,
				(int) CLI.LengthCode.SQL_IS_INTEGER);
			if (rc != CLI.ReturnCode.SQL_SUCCESS)
			{
				try
				{
					Diagnostics.HandleErrors (rc, CLI.HandleType.SQL_HANDLE_ENV, henv);
				}
				finally
				{
					FreeHEnv ();
				}
			}
		}

		private static void FreeHEnv ()
		{
			CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_ENV, henv);
			henv = IntPtr.Zero;
		}

		protected override void Dispose (bool disposing)
		{
		  Debug.WriteLineIf (CLI.FnTrace.Enabled, "OdbcConnection.Dispose ()");
			try
			{
			if (disposing)
			{
				Close ();
			}
			else if (hdbc != IntPtr.Zero)
			{
				EndTransaction (false);
				//statementList.FinalizeAll ();
				FinalizeAll ();
				Disconnect ();
				OnDisconnect ();
			}

#if WIN3#if WIN32_FINALIZERS
			//statementList.Unreference ();
			Unreference ();
#endif
		}
			catch (Exception e)
			{
				// Dispose method should never throw an exception
				Debug.WriteLineIf(CLI.FnTrace.Enabled,
					"OdbcConnection.Dispose caught exception: " + e.Message);
			}
		}

		internal void Add (IntPtr hstmt, VirtuosoCommand command)
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "StatementList.Add (" + hstmt + ")");
#if !WIN32_FINALIZERS
			lock (list)
			{
				list.Add (new Item (hstmt, command));
				CloseFinalizedCommands ();
			}
#else
			if (command != null)
			{
				lock (commands)
				{
					commands.Add (new WeakReference (command));
				}
			}

			Lock ();
			int eltcount = Marshal.ReadInt32 (head, 8);
			IntPtr elts = Marshal.ReadIntPtr (head, 12);
			for (int i = 0; i < eltcount; i++)
			{
				IntPtr h = Marshal.ReadIntPtr (elts, i * ELT_SIZE);
				if (h == IntPtr.Zero)
				{
					Marshal.WriteIntPtr (elts, i * ELT_SIZE, hstmt);
					Unlock ();
					return;
				}
			}
			int neweltcount = eltcount == 0 ? INITIAL_ELT_COUNT : eltcount * 2;
			IntPtr newelts = Marshal.ReAllocCoTaskMem (elts, neweltcount * ELT_SIZE);
			for (int i = eltcount + 1; i < neweltcount; i++)
				Marshal.WriteIntPtr (newelts, i * ELT_SIZE, IntPtr.Zero);
			Marshal.WriteIntPtr (newelts, eltcount * ELT_SIZE, hstmt);
			Marshal.WriteInt32 (head, 8, neweltcount);
			Marshal.WriteIntPtr (head, 12, newelts);
			Unlock ();
#endif
		}

		internal bool Remove (IntPtr hstmt, VirtuosoCommand command)
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "StatementList.Remove (" + hstmt + ")");
#if !WIN32_FINALIZERS
			lock (list)
			{
				for (int i = 0; i < list.Count; i++)
				{
					Item item = (Item) list[i];
					if (item.hstmt == hstmt)
					{
						list.RemoveAt (i);
						CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_STMT, hstmt);
					}
				}
				CloseFinalizedCommands ();
			}
#else
			if (commands != null)
			{
				lock (commands)
				{
					int i = 0;
					while (i < commands.Count)
					{
						WeakReference weak = (WeakReference) commands[i];
						VirtuosoCommand target = (VirtuosoCommand) weak.Target;
						if (target == null || target == command)
							commands.RemoveAt (i);
						else
							i++;
					}
				}
			}

			Lock ();
			int eltcount = Marshal.ReadInt32 (head, 8);
			IntPtr elts = Marshal.ReadIntPtr (head, 12);
			for (int i = 0; i < eltcount; i++)
			{
				IntPtr h = Marshal.ReadIntPtr (elts, i * ELT_SIZE);
				if (h == hstmt)
				{
					Marshal.WriteIntPtr (elts, i * ELT_SIZE, IntPtr.Zero);
					Unlock ();
					CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_STMT, hstmt);
					return true;
				}
			}
			Unlock ();
#endif
			return false;
		}

		internal void CloseAll ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "StatementList.CloseAll ()");
#if !WIN32_FINALIZERS
			lock (list)
			{
				for (int i = 0; i < list.Count; i++)
				{
					Item item = (Item) list[i];
					VirtuosoCommand command = item.weakCommand == null ? null : (VirtuosoCommand) item.weakCommand.Target;
					if (command != null)
					{
						command.OnConnectionClose ();
						item.weakCommand = null;
					}
					else
					{
						CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_STMT, item.hstmt);
					}
				}
				list.Clear ();
			}
#else
			if (commands != null)
			{
				lock (commands)
				{
					for (int i = 0; i < commands.Count; i++)
					{
						WeakReference weak = (WeakReference) commands[i];
						VirtuosoCommand target = (VirtuosoCommand) weak.Target;
						if (target != null)
						{
							target.OnConnectionClose ();
							weak.Target = null;
						}
					}
				}
				commands.Clear ();
			}

			FinalizeAll ();
#endif
		}

		internal void FinalizeAll ()
		{
#if !WIN32_FINALIZERS
			// no-op
#else
			Lock ();
			int eltcount = Marshal.ReadInt32 (head, 8);
			IntPtr elts = Marshal.ReadIntPtr (head, 12);
			for (int i = 0; i < eltcount; i++)
			{
				IntPtr hstmt = Marshal.ReadIntPtr (elts, i * ELT_SIZE);
				if (hstmt != IntPtr.Zero)
				{
					Marshal.WriteIntPtr (elts, i * ELT_SIZE, IntPtr.Zero);
					CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_STMT, hstmt);
				}
			}
			Unlock ();
			//Unreference (list);
#endif
		}

#if !WIN32_FINALIZERS

		private void CloseFinalizedCommands ()
		{
			for (int i = 0; i < list.Count; i++)
			{
				Item item = (Item) list[i];
				if (item.weakCommand != null && item.weakCommand.Target == null)
				{
					IntPtr hstmt = item.hstmt;
					list.RemoveAt (i);
					CLI.SQLFreeHandle ((short) CLI.HandleType.SQL_HANDLE_STMT, hstmt);
				}
			}
		}

#else

		internal int Reference ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "StatementList.Reference ()");
			return Platform.InterlockedIncrement (GetAddress (4));
		}

		internal int Unreference ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "StatementList.Unreference ()");
			if (head != IntPtr.Zero)
			{
				int refcount = Platform.InterlockedDecrement (GetAddress (4));
				if (refcount == 0)
					Release ();
				return refcount;
			}
			return 0;
		}

		private void Lock ()
		{
			while (0 != Platform.InterlockedExchange (head, 1))
				Thread.Sleep (0);
		}

		private void Unlock ()
		{
			Platform.InterlockedExchange (head, 0);
		}

		private void Release ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "StatementList.Release ()");
			IntPtr elts = Marshal.ReadIntPtr (head, 12);
			if (elts != IntPtr.Zero)
				Marshal.FreeCoTaskMem (elts);
			Marshal.FreeCoTaskMem (head);
			head = IntPtr.Zero;
		}

		private IntPtr GetAddress (int offset)
		{
			return System.IntPtr.Size == 8
				? (IntPtr) ((System.Int64) head + offset)
				: (IntPtr) ((System.Int32) head + offset);
		}
#endif
	}
}