2 * Firebird ADO.NET Data provider for .NET and Mono
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
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.
15 * Copyright (c) 2002, 2004 Carlos Guzman Alvarez
16 * All Rights Reserved.
19 using NUnit.Framework;
22 using System.Configuration;
23 using System.Collections;
24 using System.Security.Cryptography;
26 namespace FirebirdSql.Data.Firebird.Tests
32 private FbConnection connection;
33 private FbTransaction transaction;
34 private bool withTransaction;
40 public FbConnection Connection
42 get { return connection; }
45 public FbTransaction Transaction
47 get { return transaction; }
48 set { transaction = value; }
57 this.withTransaction = false;
60 public BaseTest(bool withTransaction)
62 this.withTransaction = withTransaction;
70 public virtual void SetUp()
72 string cs = this.BuildConnectionString();
80 this.connection = new FbConnection(cs);
81 this.connection.Open();
83 if (this.withTransaction)
85 this.transaction = this.connection.BeginTransaction();
90 public virtual void TearDown()
92 if (this.withTransaction)
107 #region Database Creation Methods
109 private static void CreateDatabase(string connectionString)
111 FbConnection.CreateDatabase(
113 Convert.ToInt32(ConfigurationSettings.AppSettings["PageSize"]),
114 Boolean.Parse(ConfigurationSettings.AppSettings["ForcedWrite"]),
118 private static void CreateTables(string connectionString)
120 FbConnection connection = new FbConnection(connectionString);
123 StringBuilder commandText = new StringBuilder();
125 // Table for general purpouse tests
126 commandText.Append("CREATE TABLE TEST (");
127 commandText.Append("INT_FIELD INTEGER DEFAULT 0 NOT NULL PRIMARY KEY,");
128 commandText.Append("CHAR_FIELD CHAR(30),");
129 commandText.Append("VARCHAR_FIELD VARCHAR(100),");
130 commandText.Append("BIGINT_FIELD BIGINT,");
131 commandText.Append("SMALLINT_FIELD SMALLINT,");
132 commandText.Append("DOUBLE_FIELD DOUBLE PRECISION,");
133 commandText.Append("FLOAT_FIELD FLOAT,");
134 commandText.Append("NUMERIC_FIELD NUMERIC(15,2),");
135 commandText.Append("DECIMAL_FIELD DECIMAL(15,2),");
136 commandText.Append("DATE_FIELD DATE,");
137 commandText.Append("TIME_FIELD TIME,");
138 commandText.Append("TIMESTAMP_FIELD TIMESTAMP,");
139 commandText.Append("CLOB_FIELD BLOB SUB_TYPE 1 SEGMENT SIZE 80,");
140 commandText.Append("BLOB_FIELD BLOB SUB_TYPE 0 SEGMENT SIZE 80,");
141 commandText.Append("IARRAY_FIELD INTEGER [0:3],");
142 commandText.Append("SARRAY_FIELD SMALLINT [0:4],");
143 commandText.Append("LARRAY_FIELD BIGINT [0:5],");
144 commandText.Append("FARRAY_FIELD FLOAT [0:3],");
145 commandText.Append("BARRAY_FIELD DOUBLE PRECISION [1:4],");
146 commandText.Append("NARRAY_FIELD NUMERIC(10,6) [1:4],");
147 commandText.Append("DARRAY_FIELD DATE [1:4],");
148 commandText.Append("TARRAY_FIELD TIME [1:4],");
149 commandText.Append("TSARRAY_FIELD TIMESTAMP [1:4],");
150 commandText.Append("CARRAY_FIELD CHAR(21) [1:4],");
151 commandText.Append("VARRAY_FIELD VARCHAR(30) [1:4],");
152 commandText.Append("BIG_ARRAY INTEGER [1:32767],");
153 commandText.Append("EXPR_FIELD COMPUTED BY (smallint_field * 1000),");
154 commandText.Append("CS_FIELD CHAR(1) CHARACTER SET UNICODE_FSS,");
155 commandText.Append("UCCHAR_ARRAY CHAR(10) [1:10] CHARACTER SET UNICODE_FSS);");
157 FbCommand command = new FbCommand(commandText.ToString(), connection);
158 command.ExecuteNonQuery();
164 private static void CreateProcedures(string connectionString)
166 FbConnection connection = new FbConnection(connectionString);
169 StringBuilder commandText = new StringBuilder();
172 commandText = new StringBuilder();
174 commandText.Append("CREATE PROCEDURE SELECT_DATA \r\n");
175 commandText.Append("RETURNS ( \r\n");
176 commandText.Append("INT_FIELD INTEGER, \r\n");
177 commandText.Append("VARCHAR_FIELD VARCHAR(100), \r\n");
178 commandText.Append("DECIMAL_FIELD DECIMAL(15,2)) \r\n");
179 commandText.Append("AS \r\n");
180 commandText.Append("begin \r\n");
181 commandText.Append("FOR SELECT INT_FIELD, VARCHAR_FIELD, DECIMAL_FIELD FROM TEST INTO :INT_FIELD, :VARCHAR_FIELD, :DECIMAL_FIELD \r\n");
182 commandText.Append("DO \r\n");
183 commandText.Append("SUSPEND; \r\n");
184 commandText.Append("end;");
186 FbCommand command = new FbCommand(commandText.ToString(), connection);
187 command.ExecuteNonQuery();
191 commandText = new StringBuilder();
193 commandText.Append("CREATE PROCEDURE GETRECORDCOUNT \r\n");
194 commandText.Append("RETURNS ( \r\n");
195 commandText.Append("RECCOUNT SMALLINT) \r\n");
196 commandText.Append("AS \r\n");
197 commandText.Append("begin \r\n");
198 commandText.Append("for select count(*) from test into :reccount \r\n");
199 commandText.Append("do \r\n");
200 commandText.Append("suspend; \r\n");
201 commandText.Append("end\r\n");
203 command = new FbCommand(commandText.ToString(), connection);
204 command.ExecuteNonQuery();
208 commandText = new StringBuilder();
210 commandText.Append("CREATE PROCEDURE GETVARCHARFIELD (\r\n");
211 commandText.Append("ID INTEGER)\r\n");
212 commandText.Append("RETURNS (\r\n");
213 commandText.Append("VARCHAR_FIELD VARCHAR(100))\r\n");
214 commandText.Append("AS\r\n");
215 commandText.Append("begin\r\n");
216 commandText.Append("for select varchar_field from test where int_field = :id into :varchar_field\r\n");
217 commandText.Append("do\r\n");
218 commandText.Append("suspend;\r\n");
219 commandText.Append("end\r\n");
221 command = new FbCommand(commandText.ToString(), connection);
222 command.ExecuteNonQuery();
226 commandText = new StringBuilder();
228 commandText.Append("CREATE PROCEDURE GETASCIIBLOB (\r\n");
229 commandText.Append("ID INTEGER)\r\n");
230 commandText.Append("RETURNS (\r\n");
231 commandText.Append("ASCII_BLOB BLOB SUB_TYPE 1)\r\n");
232 commandText.Append("AS\r\n");
233 commandText.Append("begin\r\n");
234 commandText.Append("for select clob_field from test where int_field = :id into :ascii_blob\r\n");
235 commandText.Append("do\r\n");
236 commandText.Append("suspend;\r\n");
237 commandText.Append("end\r\n");
239 command = new FbCommand(commandText.ToString(), connection);
240 command.ExecuteNonQuery();
244 commandText = new StringBuilder();
246 commandText.Append("CREATE PROCEDURE DATAREADERTEST\r\n");
247 commandText.Append("RETURNS (\r\n");
248 commandText.Append("content VARCHAR(128))\r\n");
249 commandText.Append("AS\r\n");
250 commandText.Append("begin\r\n");
251 commandText.Append("content = 'test';\r\n");
252 commandText.Append("end\r\n");
254 command = new FbCommand(commandText.ToString(), connection);
255 command.ExecuteNonQuery();
261 private static void CreateTriggers(string connectionString)
263 FbConnection connection = new FbConnection(connectionString);
266 StringBuilder commandText = new StringBuilder();
269 commandText = new StringBuilder();
271 commandText.Append("CREATE TRIGGER new_row FOR test ACTIVE\r\n");
272 commandText.Append("AFTER INSERT POSITION 0\r\n");
273 commandText.Append("AS\r\n");
274 commandText.Append("BEGIN\r\n");
275 commandText.Append("POST_EVENT 'new row';\r\n");
276 commandText.Append("END");
278 FbCommand command = new FbCommand(commandText.ToString(), connection);
279 command.ExecuteNonQuery();
284 commandText = new StringBuilder();
286 commandText.Append("CREATE TRIGGER update_row FOR test ACTIVE\r\n");
287 commandText.Append("AFTER UPDATE POSITION 0\r\n");
288 commandText.Append("AS\r\n");
289 commandText.Append("BEGIN\r\n");
290 commandText.Append("POST_EVENT 'updated row';\r\n");
291 commandText.Append("END");
293 command = new FbCommand(commandText.ToString(), connection);
294 command.ExecuteNonQuery();
300 private static void InsertTestData(string connectionString)
302 FbConnection connection = new FbConnection(connectionString);
305 StringBuilder commandText = new StringBuilder();
307 commandText.Append("insert into test (int_field, char_field, varchar_field, bigint_field, smallint_field, float_field, double_field, numeric_field, date_field, time_field, timestamp_field, clob_field, blob_field)");
308 commandText.Append(" values(@int_field, @char_field, @varchar_field, @bigint_field, @smallint_field, @float_field, @double_field, @numeric_field, @date_field, @time_field, @timestamp_field, @clob_field, @blob_field)");
310 FbTransaction transaction = connection.BeginTransaction();
311 FbCommand command = new FbCommand(commandText.ToString(), connection, transaction);
315 // Add command parameters
316 command.Parameters.Add("@int_field" , FbDbType.Integer);
317 command.Parameters.Add("@char_field" , FbDbType.Char);
318 command.Parameters.Add("@varchar_field" , FbDbType.VarChar);
319 command.Parameters.Add("@bigint_field" , FbDbType.BigInt);
320 command.Parameters.Add("@smallint_field" , FbDbType.SmallInt);
321 command.Parameters.Add("@float_field" , FbDbType.Double);
322 command.Parameters.Add("@double_field" , FbDbType.Double);
323 command.Parameters.Add("@numeric_field" , FbDbType.Numeric);
324 command.Parameters.Add("@date_field" , FbDbType.Date);
325 command.Parameters.Add("@time_Field" , FbDbType.Time);
326 command.Parameters.Add("@timestamp_field" , FbDbType.TimeStamp);
327 command.Parameters.Add("@clob_field" , FbDbType.Text);
328 command.Parameters.Add("@blob_field" , FbDbType.Binary);
332 for (int i = 0; i < 100; i++)
334 command.Parameters["@int_field"].Value = i;
335 command.Parameters["@char_field"].Value = "IRow " + i.ToString();
336 command.Parameters["@varchar_field"].Value = "IRow Number " + i.ToString();
337 command.Parameters["@bigint_field"].Value = i;
338 command.Parameters["@smallint_field"].Value = i;
339 command.Parameters["@float_field"].Value = (float)(i + 10)/5;
340 command.Parameters["@double_field"].Value = Math.Log(i, 10);
341 command.Parameters["@numeric_field"].Value = (decimal)(i + 10)/5;
342 command.Parameters["@date_field"].Value = DateTime.Now;
343 command.Parameters["@time_field"].Value = DateTime.Now;
344 command.Parameters["@timestamp_field"].Value= DateTime.Now;
345 command.Parameters["@clob_field"].Value = "IRow Number " + i.ToString();
346 command.Parameters["@blob_field"].Value = Encoding.Default.GetBytes("IRow Number " + i.ToString());
348 command.ExecuteNonQuery();
351 // Commit transaction
352 transaction.Commit();
356 transaction.Rollback();
368 #region ConnectionString Building methods
370 public string BuildConnectionString()
372 return this.BuildConnectionString(false);
375 public string BuildConnectionString(bool pooling)
377 FbConnectionStringBuilder cs = new FbConnectionStringBuilder();
379 cs.UserID = ConfigurationSettings.AppSettings["User"];
380 cs.Password = ConfigurationSettings.AppSettings["Password"];
381 cs.Database = ConfigurationSettings.AppSettings["Database"];
382 cs.DataSource = ConfigurationSettings.AppSettings["DataSource"];
383 cs.Port = Int32.Parse(ConfigurationSettings.AppSettings["Port"]);
384 cs.Charset = ConfigurationSettings.AppSettings["Charset"];
385 cs.Pooling = pooling;
386 cs.ServerType = Int32.Parse(ConfigurationSettings.AppSettings["ServerType"]);
388 return cs.ToString();
391 public string BuildServicesConnectionString()
393 return this.BuildServicesConnectionString(true);
396 public string BuildServicesConnectionString(bool includeDatabase)
398 FbConnectionStringBuilder cs = new FbConnectionStringBuilder();
400 cs.UserID = ConfigurationSettings.AppSettings["User"];
401 cs.Password = ConfigurationSettings.AppSettings["Password"];
404 cs.Database = ConfigurationSettings.AppSettings["Database"];
406 cs.ServerType = Convert.ToInt32(ConfigurationSettings.AppSettings["ServerType"]);
408 return cs.ToString();
417 RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
419 byte[] buffer = new byte[4];
421 rng.GetBytes(buffer);
423 return BitConverter.ToInt32(buffer, 0);