1 // DataReaderTest.cs - NUnit Test Cases for testing the
2 // DataReader family of classes
5 // Sureshkumar T (tsureshkumar@novell.com)
6 // Gert Driesen (drieseng@users.sourceforge.net)
8 // Copyright (c) 2004 Novell Inc., and the individuals listed on the
11 // Permission is hereby granted, free of charge, to any person
12 // obtaining a copy of this software and associated documentation
13 // files (the "Software"), to deal in the Software without
14 // restriction, including without limitation the rights to use, copy,
15 // modify, merge, publish, distribute, sublicense, and/or sell copies
16 // of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 using System.Data.Common;
34 using System.Data.Odbc;
35 using System.Data.SqlClient;
36 using System.Data.SqlTypes;
37 using System.Globalization;
41 using NUnit.Framework;
43 namespace MonoTests.System.Data
47 [Category ("sqlserver")]
48 public class DataReaderTest
50 static byte [] long_bytes = new byte [] {
51 0x00, 0x66, 0x06, 0x66, 0x97, 0x00, 0x66, 0x06, 0x66,
52 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06,
53 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66,
54 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06,
55 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97,
56 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66,
57 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06,
58 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66,
59 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06,
60 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97,
61 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66,
62 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06,
63 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66,
64 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06,
65 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97,
66 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66,
67 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06,
68 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66,
69 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06,
70 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97,
71 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66,
72 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06,
73 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66,
74 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06,
75 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97,
76 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66,
77 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06,
78 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06, 0x66,
79 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97, 0x06,
80 0x66, 0x06, 0x66, 0x97, 0x06, 0x66, 0x06, 0x66, 0x97,
81 0x06, 0x66, 0x06, 0x66, 0x98};
89 conn = ConnectionManager.Singleton.Connection;
90 ConnectionManager.Singleton.OpenConnection ();
91 cmd = conn.CreateCommand ();
95 public void TearDown ()
99 ConnectionManager.Singleton.CloseConnection ();
103 public void FieldCount ()
105 IDataReader reader = null;
108 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
109 reader = cmd.ExecuteReader ();
110 Assert.AreEqual (6, reader.FieldCount);
118 public void FieldCount_Command_Disposed ()
120 IDataReader reader = null;
123 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
124 reader = cmd.ExecuteReader ();
126 Assert.AreEqual (6, reader.FieldCount);
134 public void FieldCount_Reader_Closed ()
136 IDataReader reader = null;
139 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
140 reader = cmd.ExecuteReader ();
143 int fieldcount = reader.FieldCount;
144 Assert.Fail ("#1:" + fieldcount);
145 } catch (InvalidOperationException ex) {
146 // Invalid attempt to FieldCount when
148 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
149 Assert.IsNull (ex.InnerException, "#3");
150 Assert.IsNotNull (ex.Message, "#4");
159 public void GetChars_Index_Invalid ()
161 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
163 using (IDataReader rdr = cmd.ExecuteReader ()) {
164 Assert.IsTrue (rdr.Read ());
167 rdr.GetChars (-1, 0, (char []) null, 0, 0);
169 } catch (IndexOutOfRangeException ex) {
170 // Index was outside the bounds of the array
171 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#A2");
172 Assert.IsNull (ex.InnerException, "#A3");
173 Assert.IsNotNull (ex.Message, "#A4");
177 rdr.GetChars (1, 0, (char []) null, 0, 0);
179 } catch (IndexOutOfRangeException ex) {
180 // Index was outside the bounds of the array
181 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#B2");
182 Assert.IsNull (ex.InnerException, "#B3");
183 Assert.IsNotNull (ex.Message, "#B4");
187 using (IDataReader rdr = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
188 Assert.IsTrue (rdr.Read ());
191 rdr.GetChars (-1, 0, (char []) null, 0, 0);
193 } catch (IndexOutOfRangeException ex) {
194 // Index was outside the bounds of the array
195 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#C2");
196 Assert.IsNull (ex.InnerException, "#C3");
197 Assert.IsNotNull (ex.Message, "#C4");
201 rdr.GetChars (1, 0, (char []) null, 0, 0);
203 } catch (IndexOutOfRangeException ex) {
204 // Index was outside the bounds of the array
205 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#D2");
206 Assert.IsNull (ex.InnerException, "#D3");
207 Assert.IsNotNull (ex.Message, "#D4");
213 public void GetChars_Reader_Closed ()
215 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
217 using (IDataReader rdr = cmd.ExecuteReader ()) {
218 Assert.IsTrue (rdr.Read ());
222 rdr.GetChars (-1, 0, (char []) null, 0, 0);
224 } catch (InvalidOperationException ex) {
225 // No data exists for the row/column
226 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
227 Assert.IsNull (ex.InnerException, "#A3");
228 Assert.IsNotNull (ex.Message, "#A4");
232 using (IDataReader rdr = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
233 Assert.IsTrue (rdr.Read ());
237 rdr.GetChars (-1, 0, (char []) null, 0, 0);
239 } catch (InvalidOperationException ex) {
240 // No data exists for the row/column
241 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
242 Assert.IsNull (ex.InnerException, "#B3");
243 Assert.IsNotNull (ex.Message, "#B4");
249 public void GetChars_Reader_NoData ()
251 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 666";
253 using (IDataReader rdr = cmd.ExecuteReader ()) {
255 rdr.GetChars (-1, 0, (char []) null, 0, 0);
257 } catch (InvalidOperationException ex) {
258 // No data exists for the row/column
259 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
260 Assert.IsNull (ex.InnerException, "#A3");
261 Assert.IsNotNull (ex.Message, "#A4");
264 Assert.IsFalse (rdr.Read (), "#B");
267 rdr.GetChars (-1, 0, (char []) null, 0, 0);
269 } catch (InvalidOperationException ex) {
270 // No data exists for the row/column
271 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
272 Assert.IsNull (ex.InnerException, "#C3");
273 Assert.IsNotNull (ex.Message, "#C4");
279 public void GetDataTypeName ()
281 IDataReader reader = null;
284 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
285 reader = cmd.ExecuteReader ();
287 switch (ConnectionManager.Singleton.Engine.Type) {
288 case EngineType.SQLServer:
289 Assert.AreEqual ("int", reader.GetDataTypeName (0), "#1");
291 case EngineType.MySQL:
292 Assert.AreEqual ("integer", reader.GetDataTypeName (0), "#1");
295 Assert.Fail ("Engine type not supported.");
298 Assert.AreEqual ("varchar", reader.GetDataTypeName (1), "#2");
299 Assert.AreEqual ("varchar", reader.GetDataTypeName (2), "#3");
300 Assert.AreEqual ("datetime", reader.GetDataTypeName (3), "#4");
301 Assert.AreEqual ("datetime", reader.GetDataTypeName (4), "#5");
302 Assert.AreEqual ("varchar", reader.GetDataTypeName (5), "#6");
310 public void GetDataTypeName_Index_Invalid ()
312 IDataReader reader = null;
315 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
316 reader = cmd.ExecuteReader ();
319 reader.GetDataTypeName (-1);
321 } catch (IndexOutOfRangeException ex) {
322 // Index was outside the bounds of the array
323 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#A2");
324 Assert.IsNull (ex.InnerException, "#A3");
325 Assert.IsNotNull (ex.Message, "#A4");
329 reader.GetDataTypeName (6);
331 } catch (IndexOutOfRangeException ex) {
332 // Index was outside the bounds of the array
333 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#B2");
334 Assert.IsNull (ex.InnerException, "#B3");
335 Assert.IsNotNull (ex.Message, "#B4");
344 public void GetDataTypeName_Reader_Closed ()
346 IDataReader reader = null;
349 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
350 reader = cmd.ExecuteReader ();
354 reader.GetDataTypeName (0);
356 } catch (InvalidOperationException ex) {
357 // No data exists for the row/column
358 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
359 Assert.IsNull (ex.InnerException, "#3");
360 Assert.IsNotNull (ex.Message, "#4");
369 public void GetFieldType ()
371 IDataReader reader = null;
374 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
375 reader = cmd.ExecuteReader ();
376 Assert.AreEqual (typeof (int), reader.GetFieldType (0), "#1");
377 Assert.AreEqual (typeof (string), reader.GetFieldType (2), "#2");
378 Assert.AreEqual (typeof (DateTime), reader.GetFieldType (4), "#3");
386 public void GetFieldType_Index_Invalid ()
388 IDataReader reader = null;
391 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
392 reader = cmd.ExecuteReader ();
394 reader.GetFieldType (-1);
396 } catch (IndexOutOfRangeException ex) {
397 // Index was outside the bounds of the array
398 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#A2");
399 Assert.IsNull (ex.InnerException, "#A3");
400 Assert.IsNotNull (ex.Message, "#A4");
404 reader.GetFieldType (6);
406 } catch (IndexOutOfRangeException ex) {
407 // Index was outside the bounds of the array
408 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#B2");
409 Assert.IsNull (ex.InnerException, "#B3");
410 Assert.IsNotNull (ex.Message, "#B4");
419 public void GetFieldType_Reader_Closed ()
421 IDataReader reader = null;
424 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
425 reader = cmd.ExecuteReader ();
428 reader.GetFieldType (0);
430 } catch (InvalidOperationException ex) {
431 // No data exists for the row/column
432 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
433 Assert.IsNull (ex.InnerException, "#3");
434 Assert.IsNotNull (ex.Message, "#4");
443 public void GetName ()
445 IDataReader reader = null;
448 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
449 reader = cmd.ExecuteReader ();
450 Assert.AreEqual ("id", reader.GetName (0), "#1");
451 Assert.AreEqual ("fname", reader.GetName (1), "#2");
452 Assert.AreEqual ("lname", reader.GetName (2), "#3");
453 Assert.AreEqual ("dob", reader.GetName (3), "#4");
454 Assert.AreEqual ("doj", reader.GetName (4), "#5");
455 Assert.AreEqual ("email", reader.GetName (5), "#6");
463 public void GetName_Index_Invalid ()
465 IDataReader reader = null;
468 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
469 reader = cmd.ExecuteReader ();
473 } catch (IndexOutOfRangeException ex) {
474 // Index was outside the bounds of the array
475 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#A2");
476 Assert.IsNull (ex.InnerException, "#A3");
477 Assert.IsNotNull (ex.Message, "#A4");
483 } catch (IndexOutOfRangeException ex) {
484 // Index was outside the bounds of the array
485 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#B2");
486 Assert.IsNull (ex.InnerException, "#B3");
487 Assert.IsNotNull (ex.Message, "#B4");
496 public void GetName_Reader_Closed ()
498 IDataReader reader = null;
501 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
502 reader = cmd.ExecuteReader ();
507 } catch (InvalidOperationException ex) {
508 // No data exists for the row/column
509 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
510 Assert.IsNull (ex.InnerException, "#3");
511 Assert.IsNotNull (ex.Message, "#4");
520 public void GetOrdinal ()
522 IDataReader reader = null;
525 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
526 reader = cmd.ExecuteReader ();
527 Assert.AreEqual (0, reader.GetOrdinal ("id"), "#1");
528 Assert.AreEqual (1, reader.GetOrdinal ("fname"), "#2");
529 Assert.AreEqual (2, reader.GetOrdinal ("lname"), "#3");
530 Assert.AreEqual (3, reader.GetOrdinal ("doB"), "#4");
531 Assert.AreEqual (4, reader.GetOrdinal ("doj"), "#5");
532 Assert.AreEqual (5, reader.GetOrdinal ("EmaiL"), "#6");
533 Assert.AreEqual (0, reader.GetOrdinal ("iD"), "#7");
534 Assert.AreEqual (5, reader.GetOrdinal ("eMail"), "#8");
542 public void GetOrdinal_Name_DoesNotExist ()
544 IDataReader reader = null;
547 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
548 reader = cmd.ExecuteReader ();
550 reader.GetOrdinal ("non_existing_column");
552 } catch (IndexOutOfRangeException ex) {
553 // Index was outside the bounds of the array
554 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#A2");
555 Assert.IsNull (ex.InnerException, "#A3");
556 Assert.IsNotNull (ex.Message, "#A4");
560 reader.GetOrdinal (string.Empty);
562 } catch (IndexOutOfRangeException ex) {
563 // Index was outside the bounds of the array
564 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#B2");
565 Assert.IsNull (ex.InnerException, "#B3");
566 Assert.IsNotNull (ex.Message, "#B4");
575 public void GetOrdinal_Name_Null ()
577 IDataReader reader = null;
580 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
581 reader = cmd.ExecuteReader ();
583 reader.GetOrdinal (null);
585 } catch (ArgumentNullException ex) {
586 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
587 Assert.IsNull (ex.InnerException, "#3");
588 Assert.IsNotNull (ex.Message, "#4");
589 Assert.AreEqual ("fieldName", ex.ParamName, "#5");
598 public void GetOrdinal_Reader_Closed ()
600 cmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
602 using (IDataReader rdr = cmd.ExecuteReader ()) {
606 rdr.GetOrdinal (null);
608 } catch (InvalidOperationException ex) {
609 // No data exists for the row/column
610 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
611 Assert.IsNull (ex.InnerException, "#A3");
612 Assert.IsNotNull (ex.Message, "#A4");
618 public void GetOrdinal_Reader_NoData ()
620 cmd.CommandText = "SELECT * FROM employee WHERE id = 666";
622 using (IDataReader rdr = cmd.ExecuteReader ()) {
623 Assert.AreEqual (0, rdr.GetOrdinal ("id"), "#A1");
624 Assert.AreEqual (5, rdr.GetOrdinal ("eMail"), "#A2");
626 Assert.IsFalse (rdr.Read (), "#B");
628 Assert.AreEqual (2, rdr.GetOrdinal ("lname"), "#C1");
629 Assert.AreEqual (3, rdr.GetOrdinal ("dob"), "#C2");
634 public void GetSchemaTable_Command_Disposed ()
636 if (RunningOnMono && (conn is OdbcConnection))
637 Assert.Ignore ("Our statement handle is closed when we dispose the (Odbc)Command");
639 IDataReader reader = null;
642 cmd.CommandText = "select id, fname, id + 20 as plustwenty from employee";
643 reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly);
645 DataTable schema = reader.GetSchemaTable ();
646 Assert.AreEqual (3, schema.Rows.Count, "RowCount");
654 public void GetSchemaTable_Reader_Closed ()
656 IDataReader reader = null;
659 cmd.CommandText = "select id, fname, id + 20 as plustwenty from employee";
660 reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
664 reader.GetSchemaTable ();
666 } catch (InvalidOperationException ex) {
667 // Invalid attempt to call GetSchemaTable
668 // when reader is closed
669 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
670 Assert.IsNull (ex.InnerException, "#3");
671 Assert.IsNotNull (ex.Message, "#4");
680 public void GetNameTest ()
682 cmd.CommandText = "SELECT type_tinyint from numeric_family"; ;
683 using (IDataReader reader = cmd.ExecuteReader ()) {
684 Assert.AreEqual ("type_tinyint", reader.GetName (0), "#1");
689 public void IsClosed_Command_Disposed ()
691 IDataReader reader = null;
694 cmd.CommandText = "select id, fname, id + 20 as plustwenty from employee";
695 reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
696 Assert.IsFalse (reader.IsClosed, "#1");
698 Assert.IsFalse (reader.IsClosed, "#2");
706 public void IsClosed_Connection_Closed ()
709 Assert.Ignore ("We do not mark the corresponding Reader closed when we close a Connection.");
711 IDataReader reader = null;
714 cmd.CommandText = "select id, fname, id + 20 as plustwenty from employee";
715 reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
716 Assert.IsFalse (reader.IsClosed, "#1");
717 ConnectionManager.Singleton.CloseConnection ();
718 Assert.IsTrue (reader.IsClosed, "#2");
726 public void IsClosed_Reader_Closed ()
728 IDataReader reader = null;
731 cmd.CommandText = "select id, fname, id + 20 as plustwenty from employee";
732 reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
733 Assert.IsFalse (reader.IsClosed, "#1");
735 Assert.IsTrue (reader.IsClosed, "#2");
743 public void NumericTest()
745 cmd.CommandText = "select type_numeric1 from numeric_family where id = 1;";
747 using (IDataReader reader = cmd.ExecuteReader ()) {
748 Assert.IsTrue (reader.Read(), "#1");
749 object value = reader.GetValue (0);
750 Assert.AreEqual (typeof (decimal), value.GetType (), "#2");
751 Assert.AreEqual (1000m, value, "#3");
756 public void TinyIntTest ()
758 cmd.CommandText = "select type_tinyint from numeric_family where id = 1;";
759 using (IDataReader reader = cmd.ExecuteReader ()) {
760 Assert.IsTrue (reader.Read (), "#1");
761 object value = reader.GetValue (0);
762 Assert.AreEqual (typeof (byte), value.GetType (), "#2");
763 Assert.AreEqual (255, value, "#3");
768 public void GetByteTest ()
770 cmd.CommandText = "select type_tinyint from numeric_family where id = 1";
771 using (IDataReader reader = cmd.ExecuteReader ()) {
772 Assert.IsTrue (reader.Read (), "#1");
773 byte b = reader.GetByte (0);
774 Assert.AreEqual (255, b, "#2");
779 public void GetValue_Reader_Closed ()
781 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
783 using (IDataReader reader = cmd.ExecuteReader ()) {
784 Assert.IsTrue (reader.Read ());
788 reader.GetValue (-1);
790 } catch (InvalidOperationException ex) {
791 // No data exists for the row/column
792 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
793 Assert.IsNull (ex.InnerException, "#3");
794 Assert.IsNotNull (ex.Message, "#4");
800 public void GetValue_Reader_NoData ()
802 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 666";
804 using (IDataReader rdr = cmd.ExecuteReader ()) {
808 } catch (InvalidOperationException ex) {
809 // No data exists for the row/column
810 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
811 Assert.IsNull (ex.InnerException, "#A3");
812 Assert.IsNotNull (ex.Message, "#A4");
815 Assert.IsFalse (rdr.Read (), "#B");
820 } catch (InvalidOperationException ex) {
821 // No data exists for the row/column
822 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
823 Assert.IsNull (ex.InnerException, "#C3");
824 Assert.IsNotNull (ex.Message, "#C4");
830 public void GetValueBinaryTest ()
832 cmd.CommandText = "select type_binary from binary_family where id = 1";
833 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
834 Assert.IsTrue (reader.Read (), "#1");
835 object ob = reader.GetValue (0);
836 Assert.IsNotNull (ob, "#2");
837 Assert.AreEqual (typeof (byte []), ob.GetType (), "#3");
842 public void GetBytes ()
844 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
846 CommandBehavior behavior;
848 behavior = CommandBehavior.SingleResult | CommandBehavior.SequentialAccess;
849 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
850 Assert.IsTrue (reader.Read (), "#A1");
852 // Get By Parts for the column blob
853 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
858 byte [] val = new byte [totalsize];
860 ret = reader.GetBytes (0, offset, val, offset,
861 (int) Math.Min (buffsize, totalsize - count));
864 } while (count < totalsize);
866 Assert.AreEqual (5, count, "#A2");
867 Assert.AreEqual (new byte [] { 0x32, 0x56, 0x00, 0x44, 0x22 }, val, "#A3");
870 behavior = CommandBehavior.SingleResult;
871 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
872 Assert.IsTrue (reader.Read (), "#B1");
874 // Get By Parts for the column blob
875 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
880 byte [] val = new byte [totalsize];
882 ret = reader.GetBytes (0, offset, val, offset,
883 (int) Math.Min (buffsize, totalsize - count));
886 } while (count < totalsize);
888 Assert.AreEqual (5, count, "#B2");
889 Assert.AreEqual (new byte [] { 0x32, 0x56, 0x00, 0x44, 0x22 }, val, "#B3");
892 behavior = CommandBehavior.SingleResult;
893 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
894 Assert.IsTrue (reader.Read (), "#D1");
896 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
898 byte [] val = new byte [totalsize];
899 long ret = reader.GetBytes (0, 0L, val, 0, (int) (totalsize * 2));
900 Assert.AreEqual (totalsize, ret, "#D2");
901 Assert.AreEqual (new byte [] { 0x32, 0x56, 0x00, 0x44, 0x22 }, val, "#D3");
904 /* FIXME: dataIndex is currently ignored */
906 behavior = CommandBehavior.SingleResult | CommandBehavior.SequentialAccess;
907 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
908 Assert.IsTrue (reader.Read (), "#E1");
910 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
914 byte [] val = new byte [totalsize + bufferIndex];
915 for (int i = 0; i < val.Length; i++)
917 ret = reader.GetBytes (0, 1L, val, bufferIndex, (int) (totalsize - 2));
918 Assert.AreEqual (3, ret, "#E2");
919 Assert.AreEqual (new byte [] { 0x0a, 0x0a, 0x0a, 0x56, 0x00, 0x44, 0x0a, 0x0a }, val, "#E3");
921 reader.GetBytes (0, 3L, val, 1, 2);
923 } catch (InvalidOperationException ex) {
924 // Invalid GetBytes attempt at dataIndex '3'.
925 // With CommandBehavior.SequentialAccess, you
926 // may only read from dataIndex '4' or greater
927 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E5");
928 Assert.IsNull (ex.InnerException, "#E6");
929 Assert.IsNotNull (ex.Message, "#E7");
930 Assert.IsTrue (ex.Message.IndexOf ("CommandBehavior.SequentialAccess") != -1, "#E8:" + ex.Message);
931 Assert.IsTrue (ex.Message.IndexOf ("'" + 3L.ToString (CultureInfo.InvariantCulture) + "'") != -1, "#E9:" + ex.Message);
932 Assert.IsTrue (ex.Message.IndexOf ("'" + 4L.ToString (CultureInfo.InvariantCulture) + "'") != -1, "#E10:" + ex.Message);
935 ret = reader.GetBytes (0, 4L, val, bufferIndex + 4, 2);
936 Assert.AreEqual (1, ret, "#E11");
937 Assert.AreEqual (new byte [] { 0x0a, 0x0a, 0x0a, 0x56, 0x00, 0x44, 0x0a, 0x22 }, val, "#E12");
940 behavior = CommandBehavior.SingleResult;
941 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
942 Assert.IsTrue (reader.Read (), "#F1");
944 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
948 byte [] val = new byte [totalsize + bufferIndex];
949 for (int i = 0; i < val.Length; i++)
951 ret = reader.GetBytes (0, 1L, val, bufferIndex, (int) (totalsize - 2));
952 Assert.AreEqual (3, ret, "#F2");
953 Assert.AreEqual (new byte [] { 0x0a, 0x0a, 0x0a, 0x56, 0x00, 0x44, 0x0a, 0x0a }, val, "#F3");
954 ret = reader.GetBytes (0, 3L, val, 1, 2);
955 Assert.AreEqual (2, ret, "#F4");
956 Assert.AreEqual (new byte [] { 0x0a, 0x44, 0x22, 0x56, 0x00, 0x44, 0x0a, 0x0a }, val, "#F5");
962 public void GetBytes_Buffer_Null ()
964 cmd.CommandText = "SELECT type_blob FROM binary_family where id in (1,2,3,4) order by id";
967 conn = ConnectionManager.Singleton.Connection;
968 ConnectionManager.Singleton.OpenConnection ();
970 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
971 Console.WriteLine ("A");
972 Assert.IsTrue (reader.Read (), "#A1");
973 Console.WriteLine ("B");
974 Assert.AreEqual (5, reader.GetBytes (0, 0, null, 0, 0), "#A2");
975 Console.WriteLine ("C");
976 Assert.IsTrue (reader.Read (), "#B1");
977 Assert.AreEqual (275, reader.GetBytes (0, 0, null, 0, 0), "#B2");
979 Assert.IsTrue (reader.Read (), "#C1");
980 Assert.AreEqual (0, reader.GetBytes (0, 0, null, 0, 0), "#C2");
982 Assert.IsTrue (reader.Read (), "#D1");
983 if (conn is SqlConnection) {
986 reader.GetBytes (0, 0, null, 0, 0);
988 } catch (SqlNullValueException ex) {
989 // Data is Null. This method or
990 // property cannot be called on
992 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "#D3");
993 Assert.IsNull (ex.InnerException, "#D4");
994 Assert.IsNotNull (ex.Message, "#D5");
997 Assert.AreEqual (0, reader.GetBytes (0, 0, null, 0, 0), "#D2");
1000 Assert.AreEqual (-1, reader.GetBytes (0, 0, null, 0, 0), "#D2");
1006 public void GetBytes_DataIndex_OffSet ()
1008 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 2";
1010 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SingleResult | CommandBehavior.SequentialAccess)) {
1011 Assert.IsTrue (reader.Read ());
1013 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
1014 byte [] val = new byte [totalsize];
1017 // start reading at index 0
1018 ret = reader.GetBytes (0, 0, val, 0, 5);
1019 Assert.AreEqual (5, ret, "#A1");
1020 for (int i = 0; i < ret; i++)
1021 Assert.AreEqual (long_bytes [i], val [i], "#A2:" + i);
1022 Assert.AreEqual (0x00, val [5], "#A3");
1024 // attempt to read data prior to current pointer
1026 reader.GetBytes (0, 4, val, 0, 5);
1027 Assert.Fail ("#B1");
1028 } catch (InvalidOperationException ex) {
1029 // Invalid GetBytes attempt at dataIndex '4'
1030 // With CommandBehavior.SequentialAccess,
1031 // you may only read from dataIndex '5'
1033 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1034 Assert.IsNull (ex.InnerException, "#B3");
1035 Assert.IsNotNull (ex.Message, "#B4");
1036 Assert.IsTrue (ex.Message.IndexOf ("'4'") != -1, "#B5:" + ex.Message);
1037 Assert.IsTrue (ex.Message.IndexOf ("'5'") != -1, "#B6:" + ex.Message);
1040 // continue reading at current pointer
1041 ret = reader.GetBytes (0, 5, val, 5, 6);
1042 Assert.AreEqual (6, ret, "#C1");
1043 for (int i = 0; i < 11; i++)
1044 Assert.AreEqual (long_bytes [i], val [i], "#C2:" + i);
1045 Assert.AreEqual (0x00, val [11], "#C3");
1047 // skip 4 characters
1048 ret = reader.GetBytes (0, 15, val, 13, (val.Length - 13));
1049 Assert.AreEqual (260, ret, "#D1");
1050 for (int i = 0; i < 11; i++)
1051 Assert.AreEqual (long_bytes [i], val [i], "#D2:" + i);
1052 for (int i = 11; i < 13; i++)
1053 Assert.AreEqual (0x00, val [i], "#D3:" + i);
1054 for (int i = 13; i < (totalsize - 4); i++)
1055 Assert.AreEqual (long_bytes [i + 2], val [i], "#D4:" + i);
1058 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SingleResult | CommandBehavior.SequentialAccess)) {
1059 Assert.IsTrue (reader.Read ());
1061 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
1062 byte [] val = new byte [totalsize];
1065 int offset = (int) totalsize - 5;
1066 int buffer_offset = 7;
1068 // start reading at a specific position
1069 ret = reader.GetBytes (0, offset, val, buffer_offset,
1070 val.Length - buffer_offset);
1071 Assert.AreEqual (5, ret, "#E1");
1072 for (int i = 0; i < buffer_offset; i++)
1073 Assert.AreEqual (0x00, val [i], "#E2:" + i);
1074 for (int i = 0; i < ret; i++)
1075 Assert.AreEqual (long_bytes [offset + i], val [buffer_offset + i], "#E3:" + i);
1076 for (int i = (buffer_offset + (int) ret); i < val.Length; i++)
1077 Assert.AreEqual (0x00, val [i], "#E4:" + i);
1082 public void GetBytes_Reader_Closed ()
1084 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
1086 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
1087 Assert.IsTrue (reader.Read (), "#1");
1091 reader.GetBytes (0, 0, null, -1, 0);
1093 } catch (InvalidOperationException ex) {
1094 // No data exists for the row/column
1095 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
1096 Assert.IsNull (ex.InnerException, "#4");
1097 Assert.IsNotNull (ex.Message, "#5");
1103 public void GetBytes_Reader_NoData ()
1105 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
1107 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
1109 reader.GetBytes (0, 0, null, -1, 0);
1111 } catch (InvalidOperationException ex) {
1112 // No data exists for the row/column
1113 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
1114 Assert.IsNull (ex.InnerException, "#4");
1115 Assert.IsNotNull (ex.Message, "#5");
1121 public void GetSchemaTableTest_AutoIncrement ()
1123 cmd.CommandText = "select type_autoincrement from numeric_family";
1124 cmd.ExecuteNonQuery ();
1125 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly)) {
1126 DataTable schemaTable = reader.GetSchemaTable ();
1127 Assert.IsTrue ((bool) schemaTable.Rows [0]["IsAutoIncrement"], "#1");
1129 if (!RunningOnMono) {
1130 /* FIXME: we always set it to false */
1131 if (schemaTable.Columns.Contains ("IsIdentity"))
1132 Assert.IsTrue ((bool) schemaTable.Rows [0] ["IsIdentity"], "#2");
1138 public void GetValues_Reader_Closed ()
1140 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
1142 using (IDataReader rdr = cmd.ExecuteReader ()) {
1143 Assert.IsTrue (rdr.Read ());
1147 rdr.GetValues ((object []) null);
1149 } catch (InvalidOperationException ex) {
1150 // No data exists for the row/column
1151 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1152 Assert.IsNull (ex.InnerException, "#3");
1153 Assert.IsNotNull (ex.Message, "#4");
1159 public void GetValues_Reader_NoData ()
1161 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 666";
1163 using (IDataReader rdr = cmd.ExecuteReader ()) {
1165 rdr.GetValues ((object []) null);
1166 Assert.Fail ("#A1");
1167 } catch (InvalidOperationException ex) {
1168 // No data exists for the row/column
1169 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1170 Assert.IsNull (ex.InnerException, "#A3");
1171 Assert.IsNotNull (ex.Message, "#A4");
1174 Assert.IsFalse (rdr.Read (), "#B");
1177 rdr.GetValues ((object []) null);
1178 Assert.Fail ("#C1");
1179 } catch (InvalidOperationException ex) {
1180 // No data exists for the row/column
1181 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
1182 Assert.IsNull (ex.InnerException, "#C3");
1183 Assert.IsNotNull (ex.Message, "#C4");
1188 static bool RunningOnMono {
1190 return (Type.GetType ("System.MonoType", false) != null);