Contents |
---|
1.表的列 |
2.視圖的列 |
3.命令參數(shù) |
在前面例子 CodeSmith 使用教程(3): 自動生成 Yii Framework ActiveRecord 我們使用了SchemaExplorer 來獲取數(shù)據(jù)的 MetaData(數(shù)據(jù)庫 Schema 定義)來自動生成 Yii Framework 的數(shù)據(jù)庫表對應(yīng)的 ActiveRecord 定義,本篇較詳細(xì)的介紹一下的 SchemaExplorer 的用法,下一篇通過實例除了自動生成自動生成 Yii Framework 的數(shù)據(jù)庫表對應(yīng)的 ActiveRecord 定義外,還自動生成關(guān)聯(lián) ActiveRecord 的關(guān)系定義,也就是根據(jù)數(shù)據(jù)庫表之間的關(guān)系(一對多,一對一,多對多)為ActiveRecord 定義 relations.
CodeSmith 的 SchemaExplorer 定義在 Assembly SchemaExplorer.dll 中,其命名空間為SchemaExplorer ,因此如果需要使用 CodeSmith 的 SchemaExplorer 功能的話,需要添加對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 庫的引用,并定義了一個屬性 SourceDatabase,其類型為SchemaExplorer.DatabaseSchema ,在運行這個模板前,必須設(shè)置 SourceDatabase 的值:
SourceDatabase 屬性后顯示一個“…”的按鈕,表示使用一個附加的專用的編輯器來定義這個屬性,點擊這個按鈕將啟動數(shù)據(jù)庫選擇對話框:
使用這個對象框可以選擇已通過 Schema Explorer 定義過的數(shù)據(jù)庫或者添加新的數(shù)據(jù)庫,通過單擊“…”來添加新的數(shù)據(jù)庫定義:
如果添加一個新的數(shù)據(jù)源,SchemaExplorer 打開了 數(shù)據(jù)源對話庫 ,選擇合適的數(shù)據(jù)源類型:
CodeSmith 缺省支持的數(shù)據(jù)源類型有很多,包括了常用的 ADO, DB2,MySQL,Oracle,PostgreSQL, SQL Server,Sqlite 等,也可以自定義新的數(shù)據(jù)源類型。
本例我們選用 SQL Server 類型 ,并使用 Chinook 示例數(shù)據(jù)庫:
選擇數(shù)據(jù)庫 Chinook,顯示結(jié)果:
Tables in database "Chinook": Album Artist Customer Employee Genre Invoice InvoiceLine MediaType Playlist PlaylistTrack Track SchemaExplorer 對應(yīng)數(shù)據(jù)庫的 MetaData(表定義,列定義,主鍵,外鍵定義等)定義如下的對象模型,可以在代碼模板中使用:
上圖表示 SchemaExplorer 定義了多種對象集合類型,對象類型,比如 DatabaseSchema 定義了Commands 屬性, 其類型為 CommandSchemaCollection,這個集合的每項類型為 CommandSchema ,對應(yīng)到數(shù)據(jù)庫定義中的一個命令。 通過這個屬性可以獲取 Command 的定義等信息。
使用 SchemaExplorer 除了可以使用 SchemaExplorer.DatabaseSchema 類型來定義屬性,還可以通過下面四種類型
分別對應(yīng)到表類型,視圖類型,命令類型,列類型,比如使用
<%@ Property Name="SourceColumns" Type="SchemaExplorer.ColumnSchemaCollection"
Category="Database" Description="Select a set of columns." %>
選擇一個表的多個列(ColumnSchemaCollection)
對應(yīng)這些集合類型(比如 TableSchemaCollection,ColumnSchemaCollection)缺省的排序是由數(shù)據(jù)庫決定的,因此可能不是排好序的,如果需要排序的話,可以通過 Sort 方法來實現(xiàn),比如:
TableSchemaCollection tables = new TableSchemaCollection(SourceDatabase.Tables);
tables.Sort(new PropertyComparer("Name"));
SQL Server 數(shù)據(jù)庫可以對表或列定義一些附加的屬性(Extended Property)SchemaExplorer 也提供了方法可以來訪問/添加 這些 Extended Property。 比如 SQL Server 定義一個擴展屬性表示某個列是否為 Identity 列,這可以通過下面代碼來獲?。?/p>
Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {
if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) == true) {
Response.Write(cs.Name);
}
} %>
更好的方法是使用SchemaExplorer.ExtendedPropertyNames類和ExtendedProperty定義的擴展方法。
例如:
Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {
if(cs.ExtendedProperties.GetByKey<bool>(SchemaExplorer.ExtendedPropertyNames.IsIdentity) == true) {
Response.Write(cs.Name);
}
} %>
CodeSmith 缺省支持的擴展屬性如下:
表的列
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 |
下一篇通過 Table 的 Key(外鍵和主鍵)為 Yii Framework 表的 ActiveRecord 添加 Relations
更多建議: