SqlQuery customization 1

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

This example demonstrates how to create a custom attribute which allows specifying more than one SQL query for different data providers.

CustomSqlQuery1.cs
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);
        }
    }
}
Person.cs
using System;

using BLToolkit.DataAccess;
using BLToolkit.Mapping;

namespace HowTo.DataAccess
{
    public class Person
    {
        [MapField("PersonID"), PrimaryKey, NonUpdatable]
        public int    ID;

        public string LastName;
        public string FirstName;
        public string MiddleName;
        public Gender Gender;
    }
}
Gender.cs
using System;

using BLToolkit.Mapping;

namespace HowTo.DataAccess
{
    public enum Gender
    {
        [MapValue("F")] Female,
        [MapValue("M")] Male,
        [MapValue("U")] Unknown,
        [MapValue("O")] Other
    }
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add
            key   = "ConnectionString"
            value = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
    </appSettings>
</configuration>
Create.sql script
 
© 2010 www.bltoolkit.net
[email protected]