6 SQL集成(SQL Integration)
.NET 語言級集成查詢可以用來直接查詢關系型的數據存儲(relational data stores),而不用離開本地編程語言(local programming language)的語法或編譯時環境(the syntax or compile-time environment)。這個技巧(This facility),代碼名叫 DLinq(code-named DLinq),把 SQL schema 信息的集成(the integration of SQL schema information)的優勢帶入了(takes advantage of) CLR 元數據(metadata)。這個集成(integration)把 SQL 表(table)和視圖(view)的定義編譯進 CLR 類型,這樣能夠從任何語言上訪問。
DLinq 定義了兩個核心的屬性(two core attributes),[Table] 和 [Column],它們指出(indicate)了哪個 CLR 類型和屬性(which CLR types and properties)是符合(correspond to)外部的 SQL 數據(external SQL data)。[Table] 屬性能被應用于(applied to)一個類并聯合(associates) CLR 類型和一個指定的(named) SQL 表或視圖。這兩個屬性都是參數化的(parameterized),以允許 SQL 型的元數據(SQL-specific metadata)得以保留(retained)。舉例來說,考察下面這段 SQL schema 的定義:
create table People (
Name nvarchar(32) primary key not null,
Age int not null,
CanCode bit not null
)

create table Orders (
OrderID nvarchar(32) primary key not null,
Customer nvarchar(32) not null,
Amount int
)

與之對等(equivalent)的 CLR 定義看起來如下:
[Table(Name="People")]

public class Person
{
[Column(DbType="nvarchar(32) not null", Id=true)]
public string Name;

[Column]
public int Age;

[Column]
public bool CanCode;
}

[Table(Name="Orders")]

public class Order
{
[Column(DbType="nvarchar(32) not null", Id=true)]
public string OrderID;

[Column(DbType="nvarchar(32) not null")]
public string Customer;

[Column]
public int? Amount;
}

通過這個例子注意到,允許為 null (nullable)的 column 映射到(map to)CLR 里的允許為 null 的類型(允許為 null 的類型首次出現在 .NET Framework version 2),對 SQL 類型來說沒有一個與之一一對應的(a 1:1 correspondence with)CLR 類型(比如nvarchar, char, text),原始的 SQL 類型在 CLR metadata 里被保留。
為發行一個查詢而不是一個關系型存儲(issue a query against a relational store),LINQ 模式的 Dlinq 實現把查詢從它的表達樹(expression tree)轉換并編成(translates …form into)一個 SQL 表達試和適合遠程賦值(suitable for remote evaluation)的 ADO.NET DbCommand 對象。例于考察如下簡單的查詢:
// establish a query context over ADO.NET sql connection
DataContext context = new DataContext(
"Initial Catalog=petdb;Integrated Security=sspi");

// grab variables that represent the remote tables that
// correspond to the Person and Order CLR types
Table<Person> custs = context.GetTable<Person>();
Table<Order> orders = context.GetTable<Order>();

// build the query
var query = from c in custs, o in orders
where o.Customer == c.Name

select new
{
c.Name,
o.OrderID,
o.Amount,
c.Age
};

// execute the query
foreach (var item in query)
Console.WriteLine("{0} {1} {2} {3}",
item.Name, item.OrderID,
item.Amount, item.Age);

DataContext 類型提供了一個輕量級的轉換器(lightweight translator),它的工作是把標準查詢操作符(standard query operators)轉換成 SQL。DataContext 使用現有的 ADO.NET IdbConnection 來訪問存儲(accessing the store),能夠使用一個確定(established)的ADO.NET 連接對象或者一個可以用來創建一個連接的連接字符串(a connection string)的任一個來初始化(initialized)。
GetTable 方法提供 IEnumerable 兼容的變量(IEnumerable-compatible variables),能夠被用在查詢表達式(query expressions)里來代表(represent)遠程的表或視圖(the remote table or view)。調用 GetTable 不會導致任何與數據庫的交互(interaction),更準確的說(rather)它們扮演(represent)the potential 通過時用查詢表達式來與遠程的表或視圖相配合(interact with)。在我們上面的例子中,查詢不會對存儲(store)發送傳輸的影響(get transmitted to),直到程序迭代出(iterates over)查詢表達式為止,在 C# 中使用 foreach 語句也是如此(in this case)。當程序開始迭代完成(iterates over)查詢時,DataContext 機構(machinery)把查詢表達式樹(expression tree)轉換成要發送給存儲(sent to the store)的如下所示的 SQL 語句:
SELECT [t0].[Age], [t1].[Amount],
[t0].[Name], [t1].[OrderID]
FROM [Customers] AS [t0], [Orders] AS [t1]
WHERE [t1].[Customer] = [t0].[Name]

需要重點注意的是通過在本地編程語言(local programming language)中直接(directly)內建查詢能力(building query capability),開發人員可以獲得關系模型(the relational model)的完全能力(full power),而不用不得不靜態地(statically)將關聯烘烤進(bake the relationships into)CLR 類型中。成熟的 O/R 映射(Full-blown object/relational mapping)技術還能夠為那些希望它泛函性(functionality)的用戶利用(take advantage of)這個核心查詢能力(core query capability)。
待續, 錯誤難免,請批評指正,譯者Naven 2005-10-24