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
|
/*
* Firebird ADO.NET Data provider for .NET and Mono
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* Copyright (c) 2002, 2004 Carlos Guzman Alvarez
* All Rights Reserved.
*/
using NUnit.Framework;
using System;
using System.Collections;
using System.Data;
using FirebirdSql.Data.Firebird;
namespace FirebirdSql.Data.Firebird.Tests
{
[TestFixture]
public class FbCommandTest : BaseTest
{
public FbCommandTest() : base(false)
{
}
[Test]
public void ExecuteNonQueryTest()
{
Transaction = Connection.BeginTransaction();
FbCommand command = Connection.CreateCommand();
command.Transaction = Transaction;
command.CommandText = "insert into TEST (INT_FIELD) values (?) ";
command.Parameters.Add("@INT_FIELD", 100);
int affectedRows = command.ExecuteNonQuery();
Assert.AreEqual(affectedRows, 1);
Transaction.Rollback();
command.Dispose();
}
[Test]
public void ExecuteReaderTest()
{
FbCommand command = Connection.CreateCommand();
command.CommandText = "select * from TEST";
FbDataReader reader = command.ExecuteReader();
reader.Close();
command.Dispose();
}
[Test]
public void ExecuteMultipleReaderTest()
{
FbCommand command1 = Connection.CreateCommand();
FbCommand command2 = Connection.CreateCommand();
command1.CommandText = "select * from test where int_field = 1";
command2.CommandText = "select * from test where int_field = 2";
FbDataReader r1 = command1.ExecuteReader();
FbDataReader r2 = command2.ExecuteReader();
r2.Close();
try
{
// Try to call ExecuteReader in command1
// it should throw an exception
r2 = command1.ExecuteReader();
throw new InvalidProgramException();
}
catch
{
r1.Close();
}
}
[Test]
public void ExecuteReaderWithBehaviorTest()
{
FbCommand command = new FbCommand("select * from TEST", Connection);
FbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
reader.Close();
command.Dispose();
}
[Test]
public void ExecuteScalarTest()
{
FbCommand command = Connection.CreateCommand();
command.CommandText = "select CHAR_FIELD from TEST where INT_FIELD = ?";
command.Parameters.Add("@INT_FIELD", 2);
string charFieldValue = command.ExecuteScalar().ToString();
Console.WriteLine("Scalar value: {0}", charFieldValue);
command.Dispose();
}
[Test]
public void PrepareTest()
{
// Create a new test table
FbCommand create = new FbCommand("create table PrepareTest(test_field varchar(20));", Connection);
create.ExecuteNonQuery();
create.Dispose();
// Insert data using a prepared statement
FbCommand command = new FbCommand(
"insert into PrepareTest(test_field) values(@test_field);",
Connection);
command.Parameters.Add("@test_field", FbDbType.VarChar).Value = DBNull.Value;
command.Prepare();
for (int i = 0; i < 5; i++)
{
if (i < 1)
{
command.Parameters[0].Value = DBNull.Value;
}
else
{
command.Parameters[0].Value = i.ToString();
}
command.ExecuteNonQuery();
}
command.Dispose();
try
{
// Check that data is correct
FbCommand select = new FbCommand("select * from PrepareTest", Connection);
FbDataReader reader = select.ExecuteReader();
int count = 0;
while (reader.Read())
{
if (count == 0)
{
Assert.AreEqual(DBNull.Value, reader[0], "Invalid value.");
}
else
{
Assert.AreEqual(count, reader.GetInt32(0), "Invalid value.");
}
count++;
}
reader.Close();
select.Dispose();
}
catch (Exception)
{
throw;
}
finally
{
// Drop table
FbCommand drop = new FbCommand("drop table PrepareTest", Connection);
drop.ExecuteNonQuery();
drop.Dispose();
}
}
[Test]
public void NamedParametersTest()
{
FbCommand command = Connection.CreateCommand();
command.CommandText = "select CHAR_FIELD from TEST where INT_FIELD = @int_field or CHAR_FIELD = @char_field";
command.Parameters.Add("@int_field", 2);
command.Parameters.Add("@char_field", "TWO");
FbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
count++;
}
Assert.AreEqual(1, count, "Invalid number of records fetched.");
reader.Close();
command.Dispose();
}
[Test]
public void NamedParametersAndLiterals()
{
string sql = "update test set char_field = 'carlos@firebird.org', bigint_field = @bigint, varchar_field = 'carlos@ado.net' where int_field = @integer";
FbCommand command = new FbCommand(sql, this.Connection);
command.Parameters.Add("@bigint", FbDbType.BigInt).Value = 200;
command.Parameters.Add("@integer", FbDbType.Integer).Value = 1;
int recordsAffected = command.ExecuteNonQuery();
command.Dispose();
Assert.AreEqual(recordsAffected, 1, "Invalid number of records affected.");
}
[Test]
public void NamedParametersReuseTest()
{
string sql = "select * from test where int_field >= @lang and int_field <= @lang";
FbCommand command = new FbCommand(sql, this.Connection);
command.Parameters.Add("@lang", FbDbType.Integer).Value = 10;
FbDataReader reader = command.ExecuteReader();
int count = 0;
int intValue = 0;
while (reader.Read())
{
if (count == 0)
{
intValue = reader.GetInt32(0);
}
count++;
}
Assert.AreEqual(1, count, "Invalid number of records fetched.");
Assert.AreEqual(10, intValue, "Invalid record fetched.");
reader.Close();
command.Dispose();
}
[Test]
public void ExecuteStoredProcTest()
{
FbCommand command = new FbCommand("EXECUTE PROCEDURE GETVARCHARFIELD(?)", Connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ID", FbDbType.VarChar).Direction = ParameterDirection.Input;
command.Parameters.Add("@VARCHAR_FIELD", FbDbType.VarChar).Direction = ParameterDirection.Output;
command.Parameters[0].Value = 1;
// This will fill output parameters values
command.ExecuteNonQuery();
Console.WriteLine("Output Parameters");
Console.WriteLine(command.Parameters[1].Value);
}
[Test]
public void RecordsAffectedTest()
{
FbCommand selectCommand = new FbCommand("SELECT * FROM TEST WHERE INT_FIELD = -1", Connection);
int recordsAffected = selectCommand.ExecuteNonQuery();
Console.WriteLine("\r\nRecords Affected: {0}", recordsAffected);
Assert.IsTrue(recordsAffected == -1);
selectCommand.Dispose();
FbCommand deleteCommand = new FbCommand("DELETE FROM TEST WHERE INT_FIELD = -1", Connection);
recordsAffected = deleteCommand.ExecuteNonQuery();
Console.WriteLine("\r\nRecords Affected: {0}", recordsAffected);
Assert.IsTrue(recordsAffected == 0);
deleteCommand.Dispose();
}
[Test]
public void ExecuteNonQueryWithOutputParameters()
{
FbCommand command = new FbCommand("EXECUTE PROCEDURE GETASCIIBLOB(?)", Connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ID", FbDbType.VarChar).Direction = ParameterDirection.Input;
command.Parameters.Add("@CLOB_FIELD", FbDbType.Text).Direction = ParameterDirection.Output;
command.Parameters[0].Value = 1;
// This will fill output parameters values
command.ExecuteNonQuery();
// Check that the output parameter has a correct value
Assert.AreEqual("IRow Number 1", command.Parameters[1].Value, "Output parameter value is not valid");
// Dispose command - this will do a transaction commit
command.Dispose();
}
[Test]
public void InvalidParameterFormat()
{
string sql = "update test set timestamp_field = @timestamp where int_field = @integer";
FbTransaction transaction = this.Connection.BeginTransaction();
try
{
FbCommand command = new FbCommand(sql, this.Connection, transaction);
command.Parameters.Add("@timestamp", FbDbType.TimeStamp).Value = 1;
command.Parameters.Add("@integer", FbDbType.Integer).Value = 1;
command.ExecuteNonQuery();
command.Dispose();
transaction.Commit();
}
catch
{
transaction.Rollback();
}
}
[Test]
public void UnicodeTest()
{
string createTable = "CREATE TABLE VARCHARTEST (VARCHAR_FIELD VARCHAR(10));";
FbCommand ct = new FbCommand(createTable, this.Connection);
ct.ExecuteNonQuery();
ct.Dispose();
ArrayList l = new ArrayList();
l.Add("INSERT INTO VARCHARTEST (VARCHAR_FIELD) VALUES ('1');");
l.Add("INSERT INTO VARCHARTEST (VARCHAR_FIELD) VALUES ('11');");
l.Add("INSERT INTO VARCHARTEST (VARCHAR_FIELD) VALUES ('111');");
l.Add("INSERT INTO VARCHARTEST (VARCHAR_FIELD) VALUES ('1111');");
foreach (string statement in l)
{
FbCommand insert = new FbCommand(statement, this.Connection);
insert.ExecuteNonQuery();
insert.Dispose();
}
string sql = "select * from varchartest";
FbCommand cmd = new FbCommand(sql, this.Connection);
FbDataReader r = cmd.ExecuteReader();
while (r.Read())
{
Console.WriteLine("{0} :: {1}", r[0], r[0].ToString().Length);
}
r.Close();
}
[Test]
public void SimplifiedChineseTest()
{
string createTable = "CREATE TABLE TABLE1 (FIELD1 varchar(20))";
FbCommand create = new FbCommand(createTable, this.Connection);
create.ExecuteNonQuery();
create.Dispose();
// insert using parametrized SQL
string sql = "INSERT INTO Table1 VALUES (@value)";
FbCommand command = new FbCommand(sql, this.Connection);
command.Parameters.Add("@value", FbDbType.VarChar).Value = "中文";
command.ExecuteNonQuery();
command.Dispose();
sql = "SELECT * FROM TABLE1";
FbCommand select = new FbCommand(sql, this.Connection);
string result = select.ExecuteScalar().ToString();
select.Dispose();
Assert.AreEqual("中文", result, "Incorrect results in parametrized insert");
sql = "DELETE FROM TABLE1";
FbCommand delete = new FbCommand(sql, this.Connection);
delete.ExecuteNonQuery();
delete.Dispose();
// insert using plain SQL
sql = "INSERT INTO Table1 VALUES ('中文')";
FbCommand plainCommand = new FbCommand(sql, this.Connection);
plainCommand.ExecuteNonQuery();
plainCommand.Dispose();
sql = "SELECT * FROM TABLE1";
select = new FbCommand(sql, this.Connection);
result = select.ExecuteScalar().ToString();
select.Dispose();
Assert.AreEqual("中文", result, "Incorrect results in plain insert");
}
[Test]
public void InsertDateTest()
{
string sql = "insert into TEST (int_field, date_field) values (1002, @date)";
FbCommand command = new FbCommand(sql, this.Connection);
command.Parameters.Add("@date", FbDbType.Date).Value = DateTime.Now.ToString();
int ra = command.ExecuteNonQuery();
Assert.AreEqual(ra, 1);
}
[Test]
public void InsertNullTest()
{
string sql = "insert into TEST (int_field) values (@value)";
FbCommand command = new FbCommand(sql, this.Connection);
command.Parameters.Add("@value", FbDbType.Integer).Value = null;
try
{
command.ExecuteNonQuery();
throw new Exception("The command was executed without throw an exception");
}
catch
{
}
}
[Test]
public void ParameterDescribeTest()
{
string sql = "insert into TEST (int_field) values (@value)";
FbCommand command = new FbCommand(sql, this.Connection);
command.Prepare();
command.Parameters.Add("@value", FbDbType.Integer).Value = 100000;
command.ExecuteNonQuery();
command.Dispose();
}
[Test]
public void ReadOnlyTransactionTest()
{
using (IDbCommand command = this.Connection.CreateCommand())
{
using (IDbTransaction transaction = this.Connection.BeginTransaction(FbTransactionOptions.Read))
{
try
{
command.Transaction = transaction;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "CREATE TABLE X_TABLE_1(FIELD VARCHAR(50));";
command.ExecuteNonQuery();
transaction.Commit();
}
catch (FbException)
{
}
}
}
}
}
}
|