2007-05-10 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / Test / FbImplicitTransactionTest.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 System;
20 using System.Data;
21 using System.Text;
22
23 using FirebirdSql.Data.Firebird;
24 using NUnit.Framework;
25
26 namespace FirebirdSql.Data.Firebird.Tests
27 {
28         [TestFixture]
29         public class FbImplicitTransactionTest : BaseTest
30         {
31                 public FbImplicitTransactionTest() : base(false)
32                 {               
33                 }
34                 
35                 [Test]
36                 public void DataAdapterFillTest()
37                 {
38                         FbCommand               command = new FbCommand("select * from TEST where DATE_FIELD = ?", Connection);
39                         FbDataAdapter   adapter = new FbDataAdapter(command);
40
41                         adapter.SelectCommand.Parameters.Add("@DATE_FIELD", FbDbType.Date, 4, "DATE_FIELD").Value = new DateTime(2003, 1, 5);
42                         
43                         FbCommandBuilder builder = new FbCommandBuilder(adapter);
44
45                         DataSet ds = new DataSet();
46                         adapter.Fill(ds, "TEST");
47                         
48                         Console.WriteLine();
49                         Console.WriteLine("Implicit transactions - DataAdapter Fill Method - Test");
50
51                         foreach (DataTable table in ds.Tables)
52                         {
53                                 foreach (DataColumn col in table.Columns)
54                                 {
55                                         Console.Write(col.ColumnName + "\t\t");
56                                 }
57                                 
58                                 Console.WriteLine();
59                                 
60                                 foreach (DataRow row in table.Rows)
61                                 {
62                                         for (int i = 0; i < table.Columns.Count; i++)
63                                         {
64                                                 Console.Write(row[i] + "\t\t");
65                                         }
66
67                                         Console.WriteLine("");
68                                 }
69                         }
70
71                         adapter.Dispose();
72                         builder.Dispose();
73                         command.Dispose();
74                 }
75         
76                 [Test]
77                 public void MultipleDataAdapterFillTest()
78                 {
79                         FbCommand               command = new FbCommand("select * from TEST where DATE_FIELD = ?", Connection);
80                         FbDataAdapter   adapter = new FbDataAdapter(command);
81
82                         adapter.SelectCommand.Parameters.Add("@DATE_FIELD", FbDbType.Date, 4, "DATE_FIELD").Value = new DateTime(2003, 1, 5);
83                         
84                         FbCommandBuilder builder = new FbCommandBuilder(adapter);
85
86                         DataSet ds = new DataSet();
87                         adapter.Fill(ds, "TEST");
88                         
89                         Console.WriteLine();
90                         Console.WriteLine("Implicit transactions - DataAdapter Fill Method - Test");
91
92                         foreach (DataTable table in ds.Tables)
93                         {
94                                 foreach (DataColumn col in table.Columns)
95                                 {
96                                         Console.Write(col.ColumnName + "\t\t");
97                                 }
98                                 
99                                 Console.WriteLine();
100                                 
101                                 foreach (DataRow row in table.Rows)
102                                 {
103                                         for (int i = 0; i < table.Columns.Count; i++)
104                                         {
105                                                 Console.Write(row[i] + "\t\t");
106                                         }
107
108                                         Console.WriteLine("");
109                                 }
110                         }
111
112                         adapter.SelectCommand.Parameters[0].Value = new DateTime(2003, 1, 6);
113
114                         ds = new DataSet();
115                         adapter.Fill(ds, "TEST");
116                         
117                         Console.WriteLine();
118                         Console.WriteLine("Implicit transactions - DataAdapter Fill Method - Test");
119
120                         foreach (DataTable table in ds.Tables)
121                         {
122                                 foreach (DataColumn col in table.Columns)
123                                 {
124                                         Console.Write(col.ColumnName + "\t\t");
125                                 }
126                                 
127                                 Console.WriteLine();
128                                 
129                                 foreach (DataRow row in table.Rows)
130                                 {
131                                         for (int i = 0; i < table.Columns.Count; i++)
132                                         {
133                                                 Console.Write(row[i] + "\t\t");
134                                         }
135
136                                         Console.WriteLine("");
137                                 }
138                         }
139
140
141                         adapter.Dispose();
142                         builder.Dispose();
143                         command.Dispose();
144                 }
145
146                 [Test]
147                 public void ExecuteScalarTest()
148                 {
149                         FbCommand command = new FbCommand("select sum(int_field) from TEST", Connection);
150
151                         Console.WriteLine("\r\nExecuteScalar with implicit transaction: {0}", command.ExecuteScalar());
152
153                         command.Dispose();
154                 }
155
156                 [Test]
157                 public void UpdatedClobFieldTest()
158                 {
159                         Console.WriteLine("\r\nUpdate CLOB field with implicit transaction.");
160
161                         FbCommand command = new FbCommand("update TEST set clob_field = @clob_field where int_field = @int_field", Connection);
162                         command.Parameters.Add("@int_field", FbDbType.Integer).Value = 1;
163                         command.Parameters.Add("@clob_field", FbDbType.Text).Value = "Clob field update with implicit transaction";
164
165                         int i = command.ExecuteNonQuery();
166
167                         Assert.AreEqual(i, 1, "Clob field update with implicit transaction failed");
168
169                         // Force the implicit transaction to be committed
170                         command.Dispose();
171                 }
172
173                 [Test]
174                 public void UpdatedBlobFieldTest()
175                 {
176                         Console.WriteLine("\r\nUpdate BLOB field with implicit transaction.");
177
178                         FbCommand command = new FbCommand("update TEST set blob_field = @blob_field where int_field = @int_field", Connection);
179                         command.Parameters.Add("@int_field", FbDbType.Integer).Value = 1;
180                         command.Parameters.Add("@blob_field", FbDbType.Binary).Value = 
181                                 Encoding.Default.GetBytes("Blob field update with implicit transaction");
182
183                         int i = command.ExecuteNonQuery();
184
185                         Assert.AreEqual(i, 1, "Blob field update with implicit transaction failed");
186
187                         // Force the implicit transaction to be committed
188                         command.Dispose();
189                 }
190
191                 [Test]
192                 public void UpdatedArrayFieldTest()
193                 {
194                         Console.WriteLine("\r\nUpdate CLOB field with implicit transaction.");
195
196                         int[] values = new int[4];
197
198                         values[0] = 10;
199                         values[1] = 20;
200                         values[2] = 30;
201                         values[3] = 40;
202                         
203                         FbCommand command = new FbCommand("update TEST set iarray_field = @iarray_field where int_field = @int_field", Connection);
204                         command.Parameters.Add("@int_field", FbDbType.Integer).Value = 1;
205                         command.Parameters.Add("@iarray_field", FbDbType.Array).Value = values;
206
207                         int i = command.ExecuteNonQuery();
208
209                         Assert.AreEqual(i, 1, "Array field update with implicit transaction failed");
210
211                         // Force the implicit transaction to be committed
212                         command.Dispose();
213                 }
214         }
215 }