New test.
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / Test / FbDataReaderTest.cs
1 /*
2  *  Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *     The contents of this file are subject to the Initial 
5  *     Developer's Public License Version 1.0 (the "License"); 
6  *     you may not use this file except in compliance with the 
7  *     License. You may obtain a copy of the License at 
8  *     http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *     Software distributed under the License is distributed on 
11  *     an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *     express or implied.  See the License for the specific 
13  *     language governing rights and limitations under the License.
14  * 
15  *  Copyright (c) 2002, 2004 Carlos Guzman Alvarez
16  *  All Rights Reserved.
17  */
18
19 using NUnit.Framework;
20
21 using System;
22 using System.Data;
23 using FirebirdSql.Data.Firebird;
24
25 namespace FirebirdSql.Data.Firebird.Tests
26 {
27         [TestFixture]
28         public class FbDataReaderTest : BaseTest 
29         {       
30                 public FbDataReaderTest() : base(false)
31                 {               
32                 }
33
34                 [Test]
35                 public void ReadTest()
36                 {
37                         FbTransaction transaction = Connection.BeginTransaction();
38                                                 
39                         FbCommand command = new FbCommand("select * from TEST", Connection, transaction);
40                         
41                         Console.WriteLine();
42                         Console.WriteLine("DataReader - Read Method - Test");
43                         
44                         IDataReader reader = command.ExecuteReader();
45                         while (reader.Read())
46                         {
47                                 for(int i = 0; i < reader.FieldCount; i++)
48                                 {
49                                         Console.Write(reader.GetValue(i) + "\t");
50                                 }
51                         
52                                 Console.WriteLine();
53                         }
54
55                         reader.Close();
56                         command.Dispose();
57                         transaction.Rollback();
58                 }
59
60                 [Test]
61                 public void BigIntGetStringTest()
62                 {
63                         FbTransaction transaction = Connection.BeginTransaction();
64
65                         FbCommand command = new FbCommand("select * from TEST", Connection, transaction);
66
67                         Console.WriteLine();
68                         Console.WriteLine("DataReader - Read Method - Test");
69
70                         IDataReader reader = command.ExecuteReader();
71                         while (reader.Read())
72                         {
73                                 Console.Write(reader.GetString(reader.GetOrdinal("bigint_field")) + "\t");
74
75                                 Console.WriteLine();
76                         }
77
78                         reader.Close();
79                         command.Dispose();
80                         transaction.Rollback();
81                 }
82
83                 [Test]
84                 public void GetValuesTest()
85                 {
86                         FbTransaction transaction = Connection.BeginTransaction();
87                                                 
88                         FbCommand command = new FbCommand("select * from TEST", Connection, transaction);
89                         
90                         Console.WriteLine();
91                         Console.WriteLine("DataReader - Read Method - Test");
92                         
93                         IDataReader reader = command.ExecuteReader();
94                         while (reader.Read())
95                         {
96                                 object[] values = new object[reader.FieldCount];
97                                 reader.GetValues(values);
98
99                                 for (int i = 0; i < values.Length; i++)
100                                 {
101                                         Console.Write(values[i] + "\t");                                        
102                                 }
103                         
104                                 Console.WriteLine();
105                         }
106
107                         reader.Close();
108                         transaction.Rollback(); 
109                         command.Dispose();
110                 }
111
112                 [Test]
113                 public void IndexerByIndexTest()
114                 {
115                         FbTransaction transaction = Connection.BeginTransaction();
116                                                 
117                         FbCommand command = new FbCommand("select * from TEST", Connection, transaction);
118                         
119                         Console.WriteLine();
120                         Console.WriteLine("DataReader - Read Method - Test");
121                         
122                         IDataReader reader = command.ExecuteReader();
123                         while (reader.Read())
124                         {
125                                 for(int i = 0; i < reader.FieldCount; i++)
126                                 {
127                                         Console.Write(reader[i] + "\t");                                        
128                                 }
129                         
130                                 Console.WriteLine();
131                         }
132
133                         reader.Close();
134                         transaction.Rollback();                         
135                         command.Dispose();
136                 }
137
138                 [Test]
139                 public void IndexerByNameTest()
140                 {
141                         FbTransaction transaction = Connection.BeginTransaction();
142                                                 
143                         FbCommand command = new FbCommand("select * from TEST", Connection, transaction);
144                         
145                         Console.WriteLine();
146                         Console.WriteLine("DataReader - Read Method - Test");
147                         
148                         IDataReader reader = command.ExecuteReader();
149                         while (reader.Read())
150                         {
151                                 for(int i = 0; i < reader.FieldCount; i++)
152                                 {
153                                         Console.Write(reader[reader.GetName(i)] + "\t");                                        
154                                 }
155                         
156                                 Console.WriteLine();
157                         }
158
159                         reader.Close();
160                         transaction.Rollback();                         
161                         command.Dispose();
162                 }
163
164                 [Test]
165                 public void GetSchemaTableTest()
166                 {
167                         FbTransaction transaction       = Connection.BeginTransaction();
168                         FbCommand         command               = new FbCommand("select * from TEST", Connection, transaction);
169         
170                         FbDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly);                
171                 
172                         DataTable schema = reader.GetSchemaTable();
173                         
174                         Console.WriteLine();
175                         Console.WriteLine("DataReader - GetSchemaTable Method- Test");
176
177                         DataRow[] currRows = schema.Select(null, null, DataViewRowState.CurrentRows);
178
179                         foreach (DataColumn myCol in schema.Columns)
180                         {
181                                 Console.Write("{0}\t\t", myCol.ColumnName);
182                         }
183
184                         Console.WriteLine();
185                         
186                         foreach (DataRow myRow in currRows)
187                         {
188                                 foreach (DataColumn myCol in schema.Columns)
189                                 {
190                                         Console.Write("{0}\t\t", myRow[myCol]);
191                                 }
192                                 
193                                 Console.WriteLine();
194                         }
195                         
196                         reader.Close();
197                         transaction.Rollback();
198                         command.Dispose();
199                 }
200                 
201                 [Test]
202                 public void GetSchemaTableWithExpressionFieldTest()
203                 {
204                         FbTransaction transaction       = Connection.BeginTransaction();
205                         FbCommand         command               = new FbCommand("select TEST.*, 0 AS VALOR from TEST", Connection, transaction);
206         
207                         FbDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly);                
208                 
209                         DataTable schema = reader.GetSchemaTable();
210                         
211                         Console.WriteLine();
212                         Console.WriteLine("DataReader - GetSchemaTable Method- Test");
213
214                         DataRow[] currRows = schema.Select(null, null, DataViewRowState.CurrentRows);
215
216                         foreach (DataColumn myCol in schema.Columns)
217                         {
218                                 Console.Write("{0}\t\t", myCol.ColumnName);
219                         }
220
221                         Console.WriteLine();
222                         
223                         foreach (DataRow myRow in currRows)
224                         {
225                                 foreach (DataColumn myCol in schema.Columns)
226                                 {
227                                         Console.Write("{0}\t\t", myRow[myCol]);
228                                 }
229                                 
230                                 Console.WriteLine();
231                         }
232                         
233                         reader.Close();
234                         transaction.Rollback();
235                         command.Dispose();
236                 }
237
238                 [Test]
239         [Ignore("Not supported")]
240         public void NextResultTest()
241                 {
242                         string querys = "select * from TEST order by INT_FIELD asc;" +
243                                                         "select * from TEST order by INT_FIELD desc;";
244
245                         FbTransaction   transaction = Connection.BeginTransaction();
246                         FbCommand               command         = new FbCommand(querys, Connection, transaction);
247         
248                         FbDataReader reader = command.ExecuteReader();          
249
250                         Console.WriteLine();
251                         Console.WriteLine("DataReader - NextResult Method - Test ( First Result )");
252
253                         while (reader.Read())
254                         {
255                                 for(int i = 0; i < reader.FieldCount; i++)
256                                 {
257                                         Console.Write(reader.GetValue(i) + "\t");                                       
258                                 }
259                         
260                                 Console.WriteLine();
261                         }
262
263                         if(reader.NextResult())
264                         {
265                                 Console.WriteLine("DataReader - NextResult Method - Test ( Second Result )");
266                 
267                                 while (reader.Read())
268                                 {
269                                         for(int i = 0; i < reader.FieldCount; i++)
270                                         {
271                                                 Console.Write(reader.GetValue(i) + "\t");                                       
272                                         }
273                                 
274                                         Console.WriteLine();
275                                 }
276                         }
277
278                         reader.Close();
279                         transaction.Rollback();
280                         command.Dispose();
281                 }
282
283                 [Test]
284                 public void RecordAffectedTest()
285                 {
286                         string sql = "insert into test (int_field) values (100000)";
287
288                         FbCommand command = new FbCommand(sql, this.Connection);
289
290                         FbDataReader reader = command.ExecuteReader();
291
292                         bool nextResult = true;
293
294                         while (nextResult)
295                         {
296                                 while (reader.Read())
297                                 {
298                                 }
299
300                                 nextResult = reader.NextResult();
301                         }
302
303                         reader.Close();
304
305                         Assert.AreEqual(1, reader.RecordsAffected, "RecordsAffected value is incorrect");
306                 }
307
308                 [Test]
309                 public void GetBytesLengthTest()
310                 {
311                         string sql = "select blob_field from TEST where int_field = @int_field";
312
313                         FbCommand command = new FbCommand(sql, this.Connection);
314                         command.Parameters.Add("@int_field", FbDbType.Integer).Value = 2;
315
316                         FbDataReader reader = command.ExecuteReader();
317
318                         reader.Read();
319
320                         long length = reader.GetBytes(0, 0, null, 0, 0);
321
322                         reader.Close();
323
324                         Assert.AreEqual(13, length, "Incorrect blob length");
325                 }
326
327                 [Test]
328                 public void GetCharsLengthTest()
329                 {
330                         string sql = "select clob_field from TEST where int_field = @int_field";
331
332                         FbCommand command = new FbCommand(sql, this.Connection);
333                         command.Parameters.Add("@int_field", FbDbType.Integer).Value = 50;
334
335                         FbDataReader reader = command.ExecuteReader();
336
337                         reader.Read();
338
339                         long length = reader.GetChars(0, 0, null, 0, 0);
340
341                         reader.Close();
342
343                         Assert.AreEqual(14, length, "Incorrect clob length");
344                 }
345
346         [Test]
347         public void ValidateDecimalSchema()
348         {
349             string sql = "select decimal_field from test";
350
351             FbCommand test = new FbCommand(sql, this.Connection);
352             FbDataReader r = test.ExecuteReader(CommandBehavior.SchemaOnly);
353
354             DataTable schema = r.GetSchemaTable();
355
356             r.Close();
357
358             // Check schema values
359             Assert.AreEqual(schema.Rows[0]["ColumnSize"], 8, "Invalid length");
360             Assert.AreEqual(schema.Rows[0]["NumericPrecision"], 15, "Invalid precision");
361             Assert.AreEqual(schema.Rows[0]["NumericScale"], 2, "Invalid scale");
362         }
363
364                 [Test]
365                 public void DisposeTest()
366                 {
367                         using (FbCommand command = new FbCommand("DATAREADERTEST", Connection))
368                         {
369                                 command.CommandType = CommandType.StoredProcedure;
370
371                                 FbCommandBuilder.DeriveParameters(command);
372
373                                 using (IDataReader reader = command.ExecuteReader())
374                                 {
375                                         while (reader.Read())
376                                         {
377                                         }
378                                 }
379                         }
380                 }
381         }
382 }