99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

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

2018-08-12 21:16 更新

使用 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,其類(lèi)型為SchemaExplorer.DatabaseSchema ,在運(yùn)行這個(gè)模板前,必須設(shè)置 SourceDatabase 的值:

第19張

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

第20張

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

第21張

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

第22張

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

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

第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ì)象模型,可以在代碼模板中使用:

第24張

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

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

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

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

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

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

第25張

對(duì)應(yīng)這些集合類(lèi)型(比如 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類(lèi)和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

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)