鍍金池/ 教程/ C++/ 使用 SchemaExplorer 來(lái)獲取數(shù)據(jù)庫(kù)定義
使用主從代碼模板
CodeTemplateInfo 對(duì)象
引用其它文件或 .Net 類庫(kù)
編寫(xiě)第一個(gè)代碼模板
Progress 對(duì)象
基本語(yǔ)法-使用注釋
基本語(yǔ)法-轉(zhuǎn)義Asp.Net標(biāo)記
調(diào)試
為 Yii Framework 創(chuàng)建生成 ActiveRecord 的代碼模板
基本語(yǔ)法-聲明和使用屬性
自動(dòng)生成Yii Framework ActiveRecord類簡(jiǎn)單模板
Merge 策略
使用 XMLProperty
CodeTemplate 對(duì)象
基本語(yǔ)法-CodeTemplate 指令
概述
使用 SchemaExplorer 來(lái)獲取數(shù)據(jù)庫(kù)定義

使用 SchemaExplorer 來(lái)獲取數(shù)據(jù)庫(kù)定義

Contents
1.表的列
2.視圖的列
3.命令參數(shù)

在前面例子 CodeSmith 使用教程(3): 自動(dòng)生成 Yii Framework ActiveRecord 我們使用了SchemaExplorer 來(lái)獲取數(shù)據(jù)的 MetaData(數(shù)據(jù)庫(kù) Schema 定義)來(lái)自動(dòng)生成 Yii Framework 的數(shù)據(jù)庫(kù)表對(duì)應(yīng)的 ActiveRecord 定義,本篇較詳細(xì)的介紹一下的 SchemaExplorer 的用法,下一篇通過(guò)實(shí)例除了自動(dòng)生成自動(dòng)生成 Yii Framework 的數(shù)據(jù)庫(kù)表對(duì)應(yīng)的 ActiveRecord 定義外,還自動(dòng)生成關(guān)聯(lián) ActiveRecord 的關(guān)系定義,也就是根據(jù)數(shù)據(jù)庫(kù)表之間的關(guān)系(一對(duì)多,一對(duì)一,多對(duì)多)為ActiveRecord 定義 relations.

CodeSmith 的 SchemaExplorer 定義在 Assembly SchemaExplorer.dll 中,其命名空間為SchemaExplorer ,因此如果需要使用 CodeSmith 的 SchemaExplorer 功能的話,需要添加對(duì)SchemaExplorer.dll 的引用,如下:

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="List all database tables" %>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
 Category="Context" Description="Database containing the tables." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
Tables in database "<%= SourceDatabase %>":
<% for (int i = 0; i < SourceDatabase.Tables.Count; i++) { %>
        <%= SourceDatabase.Tables[i].Name %>
<% } %>

以上代碼添加了 SchemaExplorer 庫(kù)的引用,并定義了一個(gè)屬性 SourceDatabase,其類型為SchemaExplorer.DatabaseSchema ,在運(yùn)行這個(gè)模板前,必須設(shè)置 SourceDatabase 的值:

http://wiki.jikexueyuan.com/project/codesmith/images/19.png" alt="第19張" />

SourceDatabase 屬性后顯示一個(gè)“…”的按鈕,表示使用一個(gè)附加的專用的編輯器來(lái)定義這個(gè)屬性,點(diǎn)擊這個(gè)按鈕將啟動(dòng)數(shù)據(jù)庫(kù)選擇對(duì)話框:

http://wiki.jikexueyuan.com/project/codesmith/images/20.png" alt="第20張" />

使用這個(gè)對(duì)象框可以選擇已通過(guò) Schema Explorer 定義過(guò)的數(shù)據(jù)庫(kù)或者添加新的數(shù)據(jù)庫(kù),通過(guò)單擊“…”來(lái)添加新的數(shù)據(jù)庫(kù)定義:

http://wiki.jikexueyuan.com/project/codesmith/images/21.png" alt="第21張" />

如果添加一個(gè)新的數(shù)據(jù)源,SchemaExplorer 打開(kāi)了 數(shù)據(jù)源對(duì)話庫(kù) ,選擇合適的數(shù)據(jù)源類型:

http://wiki.jikexueyuan.com/project/codesmith/images/22.png" alt="第22張" />

CodeSmith 缺省支持的數(shù)據(jù)源類型有很多,包括了常用的 ADO, DB2,MySQL,Oracle,PostgreSQL, SQL Server,Sqlite 等,也可以自定義新的數(shù)據(jù)源類型。

本例我們選用 SQL Server 類型 ,并使用 Chinook 示例數(shù)據(jù)庫(kù):

http://wiki.jikexueyuan.com/project/codesmith/images/23.png" alt="第23張" />

選擇數(shù)據(jù)庫(kù) Chinook,顯示結(jié)果:

Tables in database "Chinook": Album Artist Customer Employee Genre Invoice InvoiceLine MediaType Playlist PlaylistTrack Track SchemaExplorer 對(duì)應(yīng)數(shù)據(jù)庫(kù)的 MetaData(表定義,列定義,主鍵,外鍵定義等)定義如下的對(duì)象模型,可以在代碼模板中使用:

http://wiki.jikexueyuan.com/project/codesmith/images/24.png" alt="第24張" />

上圖表示 SchemaExplorer 定義了多種對(duì)象集合類型,對(duì)象類型,比如 DatabaseSchema 定義了Commands 屬性, 其類型為 CommandSchemaCollection,這個(gè)集合的每項(xiàng)類型為 CommandSchema ,對(duì)應(yīng)到數(shù)據(jù)庫(kù)定義中的一個(gè)命令。 通過(guò)這個(gè)屬性可以獲取 Command 的定義等信息。

使用 SchemaExplorer 除了可以使用 SchemaExplorer.DatabaseSchema 類型來(lái)定義屬性,還可以通過(guò)下面四種類型

  • TableSchema和 TableSchemaCollection
  • ViewSchema 和 ViewSchemaCollection
  • CommandSchema 和 CommandSchemaCollection
  • ColumnSchema 和 ColumnSchemaCollection

分別對(duì)應(yīng)到表類型,視圖類型,命令類型,列類型,比如使用

<%@ Property Name="SourceColumns"  Type="SchemaExplorer.ColumnSchemaCollection"
Category="Database"  Description="Select a set of columns." %>

選擇一個(gè)表的多個(gè)列(ColumnSchemaCollection)

http://wiki.jikexueyuan.com/project/codesmith/images/25.png" alt="第25張" />

對(duì)應(yīng)這些集合類型(比如 TableSchemaCollection,ColumnSchemaCollection)缺省的排序是由數(shù)據(jù)庫(kù)決定的,因此可能不是排好序的,如果需要排序的話,可以通過(guò) Sort 方法來(lái)實(shí)現(xiàn),比如:

TableSchemaCollection tables = new TableSchemaCollection(SourceDatabase.Tables);
tables.Sort(new PropertyComparer("Name"));

SQL Server 數(shù)據(jù)庫(kù)可以對(duì)表或列定義一些附加的屬性(Extended Property)SchemaExplorer 也提供了方法可以來(lái)訪問(wèn)/添加 這些 Extended Property。 比如 SQL Server 定義一個(gè)擴(kuò)展屬性表示某個(gè)列是否為 Identity 列,這可以通過(guò)下面代碼來(lái)獲?。?/p>

Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {
    if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) == true) {
        Response.Write(cs.Name);
    }
} %>

更好的方法是使用SchemaExplorer.ExtendedPropertyNames類和ExtendedProperty定義的擴(kuò)展方法。

例如:

Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {
    if(cs.ExtendedProperties.GetByKey<bool>(SchemaExplorer.ExtendedPropertyNames.IsIdentity) == true) {
        Response.Write(cs.Name);
    }
} %>

CodeSmith 缺省支持的擴(kuò)展屬性如下:

表的列

Extended Property Key SchemaExplorer.ExtendedPropertyName Property Name 描述
CS_Description Description The Description
CS_IsRowGuidCol IsRowGuidColumn The Column is a Row Guid
CS_IsIdentity IsIdentity Identity Column
CS_IsComputed IsComputed Computed Column or Index
CS_IsDeterministic IsDeterministic Column is Deterministic
CS_IdentitySeed IdentitySeed Identity Seed
CS_IdentityIncrement IdentityIncrement Identity Increment
CS_SystemType SystemType The System Type (E.G., System.String)
CS_Default DefaultValue The default value

視圖的列

Extended Property Key SchemaExplorer.ExtendedPropertyName Property Name 描述
CS_Description Description The Description
CS_IsComputed IsComputed Computed Column or Index
CS_IsDeterministic IsDeterministic Column is Deterministic

命令參數(shù)

Extended Property Key SchemaExplorer.ExtendedPropertyName Property Name 描述
CS_Description Description The Description
CS_Default DefaultValue The default value

下一篇通過(guò) Table 的 Key(外鍵和主鍵)為 Yii Framework 表的 ActiveRecord 添加 Relations