?? testcases.cs
字號:
?using System;
using System.Data.Common;
using System.Data;
using System.Data.SQLite;
using System.Transactions;
namespace test
{
/// <summary>
/// Scalar user-defined function. In this example, the same class is declared twice with
/// different function names to demonstrate how to use alias names for user-defined functions.
/// </summary>
[SQLiteFunction(Name = "Foo", Arguments = 2, FuncType = FunctionType.Scalar)]
[SQLiteFunction(Name = "TestFunc", Arguments = 2, FuncType = FunctionType.Scalar)]
class TestFunc : SQLiteFunction
{
public override object Invoke(object[] args)
{
if (args[0].GetType() != typeof(int)) return args[0];
int Param1 = Convert.ToInt32(args[0]); // First parameter
int Param2 = Convert.ToInt32(args[1]); // Second parameter
return Param1 + Param2;
}
}
/// <summary>
/// Aggregate user-defined function. Arguments = -1 means any number of arguments is acceptable
/// </summary>
[SQLiteFunction(Name = "MyCount", Arguments = -1, FuncType = FunctionType.Aggregate)]
class MyCount : SQLiteFunction
{
public override void Step(object[] args, int nStep, ref object contextData)
{
if (contextData == null)
{
contextData = 1;
}
else
contextData = (int)contextData + 1;
}
public override object Final(object contextData)
{
return contextData;
}
}
/// <summary>
/// Sample regular expression function. Example Usage:
/// SELECT * FROM foo WHERE name REGEXP '$bar'
/// SELECT * FROM foo WHERE REGEXP('$bar', name)
///
/// </summary>
[SQLiteFunction(Name = "REGEXP", Arguments = 2, FuncType = FunctionType.Scalar)]
class MyRegEx : SQLiteFunction
{
public override object Invoke(object[] args)
{
return System.Text.RegularExpressions.Regex.IsMatch(Convert.ToString(args[1]), Convert.ToString(args[0]));
}
}
/// <summary>
/// User-defined collating sequence.
/// </summary>
[SQLiteFunction(Name = "MYSEQUENCE", FuncType = FunctionType.Collation)]
class MySequence : SQLiteFunction
{
public override int Compare(string param1, string param2)
{
// Make sure the string "Field3" is sorted out of order
if (param1 == "Field3") return 1;
if (param2 == "Field3") return -1;
return String.Compare(param1, param2, true);
}
}
internal class TestCases
{
internal static void Run(DbProviderFactory fact, DbConnection cnn)
{
Console.WriteLine("\r\nBeginning Test on " + cnn.GetType().ToString());
try { CreateTable(cnn); Console.WriteLine("SUCCESS - CreateTable"); }
catch (Exception) { Console.WriteLine("FAIL - CreateTable"); }
try { FullTextTest(cnn); Console.WriteLine("SUCCESS - Full Text Search"); }
catch (Exception) { Console.WriteLine("FAIL - Full Text Search"); }
try { TransactionTest(cnn); Console.WriteLine("SUCCESS - Transaction Enlistment"); }
catch (Exception) { Console.WriteLine("FAIL - Transaction Enlistment"); }
try { GuidTest(cnn); Console.WriteLine("SUCCESS - Guid Test"); }
catch (Exception) { Console.WriteLine("FAIL - Guid Test"); }
try { InsertTable(cnn); Console.WriteLine("SUCCESS - InsertTable"); }
catch (Exception) { Console.WriteLine("FAIL - InsertTable"); }
try { VerifyInsert(cnn); Console.WriteLine("SUCCESS - VerifyInsert"); }
catch (Exception) { Console.WriteLine("FAIL - VerifyInsert"); }
try { CoersionTest(cnn); Console.WriteLine("FAIL - CoersionTest"); }
catch (Exception) { Console.WriteLine("SUCCESS - CoersionTest"); }
try { ParameterizedInsert(cnn); Console.WriteLine("SUCCESS - ParameterizedInsert"); }
catch (Exception) { Console.WriteLine("FAIL - ParameterizedInsert"); }
try { BinaryInsert(cnn); Console.WriteLine("SUCCESS - BinaryInsert (using named parameter)"); }
catch (Exception) { Console.WriteLine("FAIL - BinaryInsert"); }
try { VerifyBinaryData(cnn); Console.WriteLine("SUCCESS - VerifyBinaryData"); }
catch (Exception) { Console.WriteLine("FAIL - VerifyBinaryData"); }
try { LockTest(cnn); Console.WriteLine("SUCCESS - LockTest"); }
catch (Exception) { Console.WriteLine("FAIL - LockTest"); }
try { ParameterizedInsertMissingParams(cnn); Console.WriteLine("FAIL - ParameterizedInsertMissingParams\r\n"); }
catch (Exception) { Console.WriteLine("SUCCESS - ParameterizedInsertMissingParams\r\n"); }
// try { TimeoutTest(cnn); Console.WriteLine("SUCCESS - TimeoutTest"); }
// catch (Exception) { Console.WriteLine("FAIL - TimeoutTest"); }
try { DataAdapter(fact, cnn, false); Console.WriteLine(""); }
catch (Exception) { Console.WriteLine("FAIL - DataAdapter"); }
try { DataAdapter(fact, cnn, true); Console.WriteLine(""); }
catch (Exception) { Console.WriteLine("FAIL - DataAdapterWithIdentityFetch"); }
try { FastInsertMany(cnn); Console.WriteLine(""); }
catch (Exception) { Console.WriteLine("FAIL - FastInsertMany"); }
try { IterationTest(cnn); Console.WriteLine(""); }
catch (Exception) { Console.WriteLine("FAIL - Iteration Test"); }
try { UserFunction(cnn); Console.WriteLine(""); }
catch (Exception) { Console.WriteLine("FAIL - UserFunction"); }
try { UserAggregate(cnn); Console.WriteLine(""); }
catch (Exception) { Console.WriteLine("FAIL - UserAggregate"); }
try { UserCollation(cnn); Console.WriteLine("SUCCESS - UserCollation"); }
catch (Exception) { Console.WriteLine("FAIL - UserCollation"); }
try { DropTable(cnn); Console.WriteLine("SUCCESS - DropTable"); }
catch (Exception) { Console.WriteLine("FAIL - DropTable"); }
Console.WriteLine("\r\nTests Finished.");
}
internal static void FullTextTest(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "CREATE VIRTUAL TABLE FullText USING FTS1(name, ingredients);";
cmd.ExecuteNonQuery();
string[] names = { "broccoli stew", "pumpkin stew", "broccoli pie", "pumpkin pie" };
string[] ingredients = { "broccoli peppers cheese tomatoes", "pumpkin onions garlic celery", "broccoli cheese onions flour", "pumpkin sugar flour butter" };
int n;
cmd.CommandText = "insert into FullText (name, ingredients) values (@name, @ingredient);";
DbParameter name = cmd.CreateParameter();
DbParameter ingredient = cmd.CreateParameter();
name.ParameterName = "@name";
ingredient.ParameterName = "@ingredient";
cmd.Parameters.Add(name);
cmd.Parameters.Add(ingredient);
for (n = 0; n < names.Length; n++)
{
name.Value = names[n];
ingredient.Value = ingredients[n];
cmd.ExecuteNonQuery();
}
cmd.CommandText = "select rowid, name, ingredients from FullText where name match 'pie';";
int[] rowids = { 3, 4 };
n = 0;
using (DbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
if (reader.GetInt64(0) != rowids[n++])
throw new ArgumentException("Unexpected rowid returned");
if (n > rowids.Length) throw new ArgumentException("Too many rows returned");
}
}
}
}
internal static void TransactionTest(DbConnection cnn)
{
using (TransactionScope scope = new TransactionScope())
{
using (DbConnection cnn2 = ((ICloneable)cnn).Clone() as DbConnection)
{
using (DbCommand cmd = cnn2.CreateCommand())
{
// Created a table inside the transaction scope
cmd.CommandText = "CREATE TABLE VolatileTable (ID INTEGER PRIMARY KEY, MyValue VARCHAR(50))";
cmd.ExecuteNonQuery();
using (DbCommand cmd2 = cnn2.CreateCommand())
{
using (cmd2.Transaction = cnn2.BeginTransaction())
{
// Inserting a value inside the table, inside a transaction which is inside the transaction scope
cmd2.CommandText = "INSERT INTO VolatileTable (ID, MyValue) VALUES(1, 'Hello')";
cmd2.ExecuteNonQuery();
cmd2.Transaction.Commit();
}
}
}
// Connection is disposed before the transactionscope leaves, thereby forcing the connection to stay open
}
// Exit the transactionscope without committing it, causing a rollback of both the create table and the insert
}
// Verify that the table does not exist
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "SELECT COUNT(*) FROM VolatileTable";
try
{
object o = cmd.ExecuteScalar();
throw new InvalidOperationException("Transaction failed! The table exists!");
}
catch(SQLiteException)
{
return; // Succeeded, the table should not have existed
}
}
}
internal static void CreateTable(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "CREATE TABLE TestCase (ID integer primary key autoincrement, Field1 Integer, Field2 Float, Field3 VARCHAR(50), Field4 CHAR(10), Field5 DateTime, Field6 Image)";
//cmd.CommandText = "CREATE TABLE TestCase (ID bigint primary key identity, Field1 Integer, Field2 Float, Field3 VARCHAR(50), Field4 CHAR(10), Field5 DateTime, Field6 Image)";
cmd.ExecuteNonQuery();
}
}
internal static void DropTable(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "DROP TABLE TestCase";
cmd.ExecuteNonQuery();
}
}
internal static void InsertTable(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "INSERT INTO TestCase(Field1, Field2, Field3, Field4, Field5) VALUES(1, 3.14159, 'Field3', 'Field4', '2005-01-01 13:49:00')";
cmd.ExecuteNonQuery();
}
}
internal static void GuidTest(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
Guid guid = Guid.NewGuid();
cmd.CommandText = "CREATE TABLE GuidTest(MyGuid GUID)";
cmd.ExecuteNonQuery();
// Insert a guid as a default binary representation
cmd.CommandText = "INSERT INTO GuidTest(MyGuid) VALUES(@b)";
((SQLiteParameterCollection)cmd.Parameters).AddWithValue("@b", guid);
// Insert a guid as text
cmd.ExecuteNonQuery();
cmd.Parameters[0].Value = guid.ToString();
cmd.Parameters[0].DbType = DbType.String;
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT MyGuid FROM GuidTest";
using (DbDataReader reader = cmd.ExecuteReader())
{
reader.Read();
if (reader.GetFieldType(0) != typeof(Guid)) throw new ArgumentException("Column is not a Guid");
if (reader.GetGuid(0) != guid) throw new ArgumentException("Guids don't match!");
reader.Read();
if (reader.GetFieldType(0) != typeof(Guid)) throw new ArgumentException("Column is not a Guid");
if (reader.GetGuid(0) != guid) throw new ArgumentException("Guids don't match!");
}
}
}
internal static void VerifyInsert(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "SELECT Field1, Field2, Field3, Field4, Field5 FROM TestCase";
cmd.Prepare();
using (DbDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
long Field1 = rd.GetInt64(0);
double Field2 = rd.GetDouble(1);
string Field3 = rd.GetString(2);
string Field4 = rd.GetString(3).TrimEnd();
DateTime Field5 = rd.GetDateTime(4);
if (Field1 != 1) throw new ArgumentOutOfRangeException("Non-Match on Field1");
if (Field2 != 3.14159) throw new ArgumentOutOfRangeException("Non-Match on Field2");
if (Field3 != "Field3") throw new ArgumentOutOfRangeException("Non-Match on Field3");
if (Field4 != "Field4") throw new ArgumentOutOfRangeException("Non-Match on Field4");
if (Field5.CompareTo(DateTime.Parse("2005-01-01 13:49:00")) != 0) throw new ArgumentOutOfRangeException("Non-Match on Field5");
}
else throw new ArgumentOutOfRangeException("No data in table");
}
}
}
internal static void CoersionTest(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "SELECT Field1, Field2, Field3, Field4, Field5, 'A', 1, 1 + 1, 3.14159 FROM TestCase";
using (DbDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
object Field1 = rd.GetInt32(0);
object Field2 = rd.GetDouble(1);
object Field3 = rd.GetString(2);
object Field4 = rd.GetString(3).TrimEnd();
object Field5 = rd.GetDateTime(4);
// The next statement should cause an exception
Field1 = rd.GetString(0);
Field2 = rd.GetString(1);
Field3 = rd.GetString(2);
Field4 = rd.GetString(3);
Field5 = rd.GetString(4);
Field1 = rd.GetInt32(0);
Field2 = rd.GetInt32(1);
Field3 = rd.GetInt32(2);
Field4 = rd.GetInt32(3);
Field5 = rd.GetInt32(4);
Field1 = rd.GetDecimal(0);
Field2 = rd.GetDecimal(1);
Field3 = rd.GetDecimal(2);
Field4 = rd.GetDecimal(3);
Field5 = rd.GetDecimal(4);
}
else throw new ArgumentOutOfRangeException("No data in table");
}
}
}
internal static void ParameterizedInsert(DbConnection cnn)
{
using (DbCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = "INSERT INTO TestCase(Field1, Field2, Field3, Field4, Field5) VALUES(?,?,?,?,?)";
DbParameter Field1 = cmd.CreateParameter();
DbParameter Field2 = cmd.CreateParameter();
DbParameter Field3 = cmd.CreateParameter();
DbParameter Field4 = cmd.CreateParameter();
DbParameter Field5 = cmd.CreateParameter();
Field1.Value = 2;
Field2.Value = 3.14159;
Field3.Value = "Param Field3";
Field4.Value = "Field4 Par";
Field5.Value = DateTime.Now;
cmd.Parameters.Add(Field1);
cmd.Parameters.Add(Field2);
cmd.Parameters.Add(Field3);
cmd.Parameters.Add(Field4);
cmd.Parameters.Add(Field5);
cmd.ExecuteNonQuery();
}
}
// Inserts binary data into the database using a named parameter
internal static void BinaryInsert(DbConnection cnn)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -