using System;
using System.Collections.Generic;
using NUnit.Framework;
using BLToolkit.Data;
using BLToolkit.DataAccess;
namespace HowTo.DataAccess
{
[TestFixture]
public class CustomSqlQuery1
{
public class TestQueryAttribute : SqlQueryAttribute
{
public TestQueryAttribute()
{
IsDynamic = true;
}
public string OracleText { get; set; }
public string FbText { get; set; }
public string SQLiteText { get; set; }
public override string GetSqlText(DataAccessor accessor, DbManager dbManager)
{
switch (dbManager.DataProvider.Name)
{
case "Sql" :
case "Access": return SqlText;
case "Oracle": return OracleText ?? SqlText;
case "Fdp" : return FbText ?? SqlText;
case "SQLite": return SQLiteText ?? SqlText;
}
throw new ApplicationException(string.Format("Unknown data provider '{0}'", dbManager.DataProvider.Name));
}
}
public abstract class PersonAccessor : DataAccessor
{
[TestQuery(
SqlText = "SELECT * FROM Person WHERE LastName = @lastName",
OracleText = "SELECT * FROM Person WHERE LastName = :lastName")]
public abstract List<Person> SelectByLastName(string lastName);
[TestQuery(
SqlText = "SELECT * FROM Person WHERE {0} = @value",
OracleText = "SELECT * FROM Person WHERE {0} = :value")]
public abstract List<Person> SelectBy([Format] string fieldName, string value);
[TestQuery(
SqlText = "SELECT TOP {0} * FROM Person WHERE LastName = @lastName",
OracleText = "SELECT * FROM Person WHERE LastName = :lastName AND rownum <= {0}",
FbText = "SELECT FIRST {0} * FROM Person WHERE LastName = @lastName",
SQLiteText = "SELECT * FROM Person WHERE LastName = @lastName LIMIT {0}")]
public abstract List<Person> SelectByLastName(string lastName, [Format(0)] int top);
[TestQuery(
SqlText = "SELECT @id as PersonID",
OracleText = "SELECT :id PersonID FROM Dual",
FbText = "SELECT CAST(@id AS INTEGER) PersonID FROM Dual")]
public abstract List<Person> SelectID(int @id);
}
[Test]
public void Test1()
{
PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>();
List<Person> list = da.SelectByLastName("Testerson");
Assert.AreNotEqual(0, list.Count);
}
[Test]
public void Test2()
{
PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>();
List<Person> list = da.SelectBy("FirstName", "John");
Assert.AreNotEqual(0, list.Count);
}
[Test]
public void Test3()
{
PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>();
List<Person> list = da.SelectByLastName("Testerson", 1);
Assert.AreNotEqual(0, list.Count);
}
[Test]
public void Test4()
{
PersonAccessor da = DataAccessor.CreateInstance<PersonAccessor>();
List<Person> list = da.SelectID(42);
Assert.AreEqual(42, list[0].ID);
}
}
} |