Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ReaderCache.cs « System.Data.ProviderBase.jvm « System.Data « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 57923ff39e450b0b9ff229102c730f9ae635787d (plain)
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
//
// System.Data.ProviderBase.ReaderCache.cs
//
// Authors:
//	Konstantin Triger <kostat@mainsoft.com>
//	Boris Kirzner <borisk@mainsoft.com>
//	
// (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
//

//
// 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 java.sql;

namespace System.Data.ProviderBase
{
	public interface IReaderCacheContainer
	{
		void Fetch(ResultSet rs, int columnIndex, bool isSequential);
		bool IsNull();
		bool IsNumeric();
		object GetValue();
	}

	internal abstract class ReaderCacheContainerBase : IReaderCacheContainer
	{
		#region Fields

		bool _isNull;

		#endregion // Fields

		#region Methods

		protected abstract void FetchInternal(ResultSet rs, int columnIndex);
		protected virtual void FetchInternal(ResultSet rs, int columnIndex, bool isSequential) {
			FetchInternal(rs, columnIndex);
		}

		public virtual bool IsNumeric() {
			return false;
		}

		public abstract object GetValue();		

		public void Fetch(ResultSet rs, int columnIndex, bool isSequential)
		{
			FetchInternal(rs, columnIndex + 1, isSequential);
			_isNull = rs.wasNull();
		}

		public bool IsNull()
		{
			return _isNull;
		}

		#endregion // Methods
	}


	internal sealed class ArrayReaderCacheContainer : ReaderCacheContainerBase // Types.ARRAY
	{
		#region Fields

		object _a;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			_a = rs.getArray(columnIndex).getArray();
		}

		public override object GetValue()
		{
			return _a;
		}

		#endregion // Methods
	}


	internal sealed class Int64ReaderCacheContainer : ReaderCacheContainerBase // Types.BIGINT
	{
		#region Fields
		
		long _l;

		#endregion // Fields

		#region Methods

		protected override  void FetchInternal(ResultSet rs, int columnIndex)
		{
			_l = rs.getLong(columnIndex);
		}

		public override bool IsNumeric() {
			return true;
		}


		public override object GetValue()
		{
			return _l;
		}

		internal long GetInt64()
		{
			return _l;
		}

		#endregion // Methods
	}


	internal class BytesReaderCacheContainer : ReaderCacheContainerBase // Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY
	{
		#region Fields

		protected byte[] _b;
		
		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			sbyte[] sbyteArray = rs.getBytes(columnIndex);
			if (sbyteArray != null) {
				_b = (byte[])vmw.common.TypeUtils.ToByteArray(sbyteArray);
			}
		}

		public override object GetValue()
		{
			return _b;
		}

		internal byte[] GetBytes()
		{
			return (byte[])GetValue();
		}

		internal virtual long GetBytes(
			long dataIndex,
			byte[] buffer,
			int bufferIndex,
			int length) {
			if (_b == null)
				return 0;
			if (buffer == null)
				return _b.LongLength;
			long actualLength = ((dataIndex + length) >= _b.LongLength) ? (_b.LongLength - dataIndex) : length;
			Array.Copy(_b,dataIndex,buffer,bufferIndex,actualLength);
			return actualLength;
		}

		#endregion // Methods
	}


	internal sealed class BooleanReaderCacheContainer : ReaderCacheContainerBase // Types.BIT
	{
		#region Fields
		
		bool _b;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			_b = rs.getBoolean(columnIndex);
		}

		public override bool IsNumeric() {
			return true;
		}

		public override object GetValue()
		{
			return _b;
		}

		internal bool GetBoolean()
		{
			return _b;
		}

		#endregion // Methods
	}


	internal sealed class BlobReaderCacheContainer : BytesReaderCacheContainer // Types.BLOB
	{
		#region Fields

		static readonly byte[] _emptyByteArr = new byte[0];
		java.sql.Blob _blob;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex) {
			throw new NotImplementedException("Should not be called");
		}


		protected override void FetchInternal(ResultSet rs, int columnIndex, bool isSequential)
		{
			_blob = rs.getBlob(columnIndex);
			if (!isSequential)
				ReadAll();
			
		}

		void ReadAll() {
			if (_blob != null) {
				long length = _blob.length();								
				if (length == 0) {
					_b = _emptyByteArr;
				}
				else {	
					java.io.InputStream input = _blob.getBinaryStream();	
					byte[] byteValue = new byte[length];
					sbyte[] sbyteValue = vmw.common.TypeUtils.ToSByteArray(byteValue);
					input.read(sbyteValue);
					_b = byteValue;
				}
			}
		}

		public override object GetValue()
		{
			if (_b == null)
				ReadAll();
			return base.GetValue();
		}

		internal override long GetBytes(long dataIndex, byte[] buffer, int bufferIndex, int length) {
			if (_b != null)
				return base.GetBytes (dataIndex, buffer, bufferIndex, length);

			if (_blob == null)
				return 0;

			if (buffer == null)
				return _blob.length();

			java.io.InputStream input = _blob.getBinaryStream();
			input.skip(dataIndex);
			return input.read(vmw.common.TypeUtils.ToSByteArray(buffer), bufferIndex, length);
		}


		#endregion // Methods
	}
	

	internal abstract class CharsReaderCacheContainer : ReaderCacheContainerBase // 
	{
		#region Fields
		
		#endregion // Fields

		#region Methods

		internal abstract long GetChars(
			long dataIndex,
			char[] buffer,
			int bufferIndex,
			int length);

		#endregion // Methods
	}


	internal sealed class GuidReaderCacheContainer : ReaderCacheContainerBase // Types.CHAR
	{
		#region Fields
		
		Guid _g;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			string s = rs.getString(columnIndex);
			if (s != null)
				_g = new Guid(s);
		}

		public override object GetValue()
		{
			return _g;
		}

		internal Guid GetGuid()
		{
			return _g;
		}

		#endregion // Methods
	}


	internal sealed class ClobReaderCacheContainer : StringReaderCacheContainer // Types.CLOB
	{
		#region Fields
		
		java.sql.Clob _clob;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex, bool isSequential)
		{
			_clob = rs.getClob(columnIndex);
			if (!isSequential)
				ReadAll();
			
		}

		void ReadAll() {
			if (_clob != null) {
				long length = _clob.length();								
				if (length == 0) {
					_s = String.Empty;
				}
				else {	
					java.io.Reader reader = _clob.getCharacterStream();	
					char[] charValue = new char[length];
					reader.read(charValue);
					if (charValue != null)
						_s = new String(charValue);
				}
			}
		}

		public override object GetValue()
		{
			if (_s == null)
				ReadAll();
			return base.GetValue();
		}

		internal override long GetChars(long dataIndex, char[] buffer, int bufferIndex, int length) {
			if (_s != null)
				return base.GetChars (dataIndex, buffer, bufferIndex, length);

			if (_clob == null)
				return 0;

			if (buffer == null)
				return _clob.length();

			java.io.Reader reader = _clob.getCharacterStream();
			reader.skip(dataIndex);
			return reader.read(buffer, bufferIndex, length);
		}


		#endregion // Methods
	}
	

	internal sealed class TimeSpanReaderCacheContainer : ReaderCacheContainerBase // Types.TIME
	{
		#region Fields
		
		TimeSpan _t;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			Time t = rs.getTime(columnIndex);
			if (t != null) {				
				_t = new TimeSpan(DbConvert.JavaTimeToClrTicks(t));
			}
		}

		public override object GetValue()
		{
			return _t;
		}

		internal TimeSpan GetTimeSpan()
		{
			return _t;
		}

		#endregion // Methods
	}


	internal class DateTimeReaderCacheContainer : ReaderCacheContainerBase // Types.TIMESTAMP
	{
		#region Fields
		
		protected DateTime _d;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			Date d = rs.getDate(columnIndex);
			if (d != null) {
				_d = new DateTime(DbConvert.JavaDateToClrTicks(d));
			}
		}

		public override object GetValue()
		{
			return _d;
		}

		internal DateTime GetDateTime()
		{
			return _d;
		}

		#endregion // Methods
	}

	internal sealed class TimestampReaderCacheContainer : DateTimeReaderCacheContainer // Types.DATE
	{
		protected override void FetchInternal(ResultSet rs, int columnIndex) {
			Timestamp ts = rs.getTimestamp(columnIndex);
			if (ts != null) {
				_d = new DateTime(DbConvert.JavaTimestampToClrTicks(ts));
			}
		}
	}


	internal sealed class DecimalReaderCacheContainer : ReaderCacheContainerBase // Types.DECIMAL, Types.NUMERIC
	{
		#region Fields
		
		decimal _d;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			java.math.BigDecimal bigDecimal = rs.getBigDecimal(columnIndex);
			if (bigDecimal != null) {
				_d = (decimal)vmw.common.PrimitiveTypeUtils.BigDecimalToDecimal(bigDecimal);
			}
		}

		public override bool IsNumeric() {
			return true;
		}

		public override object GetValue()
		{
			return _d;
		}

		internal decimal GetDecimal()
		{
			return _d;
		}

		#endregion // Methods
	}


	internal sealed class DoubleReaderCacheContainer : ReaderCacheContainerBase // Types.DOUBLE, Types.Float, Types.NUMERIC for Oracle with scale = -127
	{
		#region Fields
		
		double _d;

		#endregion // Fields

		#region Methods

		protected override  void FetchInternal(ResultSet rs, int columnIndex)
		{
			_d = rs.getDouble(columnIndex);
		}

		public override bool IsNumeric() {
			return true;
		}

		public override object GetValue()
		{
			return _d;
		}

		internal double GetDouble()
		{
			return _d;
		}

		#endregion // Methods
	}


	internal sealed class Int32ReaderCacheContainer : ReaderCacheContainerBase // Types.INTEGER
	{
		#region Fields
		
		int _i;

		#endregion // Fields

		#region Methods

		protected override  void FetchInternal(ResultSet rs, int columnIndex)
		{
			_i = rs.getInt(columnIndex);
		}

		public override bool IsNumeric() {
			return true;
		}

		public override object GetValue()
		{
			return _i;
		}

		internal int GetInt32()
		{
			return _i;
		}

		#endregion // Methods
	}


	internal class StringReaderCacheContainer : CharsReaderCacheContainer // Types.LONGVARCHAR, Types.VARCHAR, Types.CHAR
	{
		#region Fields
		
		protected string _s;

		#endregion // Fields

		#region Methods

		protected override  void FetchInternal(ResultSet rs, int columnIndex)
		{
			_s = rs.getString(columnIndex);
			// Oracle Jdbc driver returns extra trailing 0 chars for NCHAR columns
//			if ((_s != null) && (_jdbcType == 1)) {	
//				Console.WriteLine(_jdbcType);
//				int zeroIndex = ((string)_s).IndexOf((char)0);
//				if (zeroIndex > 0) {
//					Console.WriteLine("zero-padded");
//					_s = ((string)_s).Substring(0,zeroIndex);
//				}
//				else {
//					// Oracle sometimes pads with blanks (32)
//					int blankIndex = ((string)_s).IndexOf((char)32);
//					if (blankIndex > 0) {
//						Console.WriteLine("blank-padded");
//						_s = ((string)_s).Substring(0,blankIndex);
//					}
//				}
//			}
		}

		public override object GetValue()
		{
			return _s;
		}

		internal string GetString()
		{
			return _s;
		}
		
		internal override long GetChars(long dataIndex, char[] buffer, int bufferIndex, int length) {
			if (_s == null)
				return 0;
			if (buffer == null)
				return _s.Length;
			int actualLength = ((dataIndex + length) >= _s.Length) ? (_s.Length - (int)dataIndex) : length;
			for (int i = 0, stringIndex = (int)dataIndex; i < actualLength; i++)
				buffer[bufferIndex++] = _s[stringIndex++];
			return actualLength;
		}


		#endregion // Methods
	}


	internal sealed class NullReaderCacheContainer : ReaderCacheContainerBase // Types.NULL
	{
		#region Fields

		#endregion // Fields

		#region Methods

		protected override  void FetchInternal(ResultSet rs, int columnIndex)
		{
		}

		public override object GetValue()
		{
			return DBNull.Value;
		}

		#endregion // Methods
	}


	internal sealed class FloatReaderCacheContainer : ReaderCacheContainerBase // Types.REAL
	{
		#region Fields
		
		float _f;

		#endregion // Fields

		#region Methods

		protected override  void FetchInternal(ResultSet rs, int columnIndex)
		{
			_f = rs.getFloat(columnIndex);
		}

		public override bool IsNumeric() {
			return true;
		}

		public override object GetValue()
		{
			return _f;
		}

		internal float GetFloat()
		{
			return _f;
		}

		#endregion // Methods
	}


	internal sealed class RefReaderCacheContainer : ReaderCacheContainerBase // Types.REF
	{
		#region Fields
		
		java.sql.Ref _r;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			_r = rs.getRef(columnIndex);
		}

		public override object GetValue()
		{
			return _r;
		}

		#endregion // Methods
	}


	internal sealed class Int16ReaderCacheContainer : ReaderCacheContainerBase // Types.SMALLINT
	{
		#region Fields
		
		short _s;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			_s = rs.getShort(columnIndex);
		}

		public override bool IsNumeric() {
			return true;
		}

		public override object GetValue()
		{
			return _s;
		}

		internal short GetInt16()
		{
			return _s;
		}

		#endregion // Methods
	}


	internal sealed class ByteReaderCacheContainer : ReaderCacheContainerBase // Types.TINYINT
	{
		#region Fields
		
		byte _b;

		#endregion // Fields

		#region Methods

		protected override void FetchInternal(ResultSet rs, int columnIndex)
		{
			_b = (byte)rs.getByte(columnIndex);
		}

		public override bool IsNumeric() {
			return true;
		}

		public override object GetValue()
		{
			return _b;
		}

		internal byte GetByte()
		{
			return _b;
		}

		#endregion // Methods
	}


	internal sealed class ObjectReaderCacheContainer : ReaderCacheContainerBase // Types.Distinct, Types.JAVA_OBJECT, Types.OTHER, Types.STRUCT
	{
		#region Fields
		
		object o;

		#endregion // Fields

		#region Methods

		protected override  void FetchInternal(ResultSet rs, int columnIndex)
		{
			o = rs.getObject(columnIndex);
		}

		public override object GetValue()
		{
			return o;
		}

		#endregion // Methods
	}

}