CodeSmith基礎(二

本文將介紹CodeSmith與數據庫進行交互生成相應的存儲過程,本例使用的數據庫爲SQL Server 2000。
        在與數據庫進行交互時,我們使用到了一個CodeSmith自帶的組件
SchemaExplorer,利用這個組件我們可以訪問數據庫的數據表、存儲過程、視圖等,並可以得到相應的數據類型、標識列、列的(字段)名稱等信息。
        下面這個例子是教我們如何生成一個存儲過程,雖然網上有很多一樣的例子,但是我是從CodeSmith中的英文幫助中自己翻譯出來的:)
        使用的是SQL Server 2000自帶的Northwind數據庫,生成一個關於Orders訂單表的更新存儲過程。

        第一步還是指明模板使用的語言和生成的目標語言。
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Generates a update stored procedure." %>

        第二步就是我們要加載使用訪問數據庫的組件SchemaExplorer,並聲明其使用的命名空間。
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

        因爲是針對表去生成存儲過程,則首先要定義一個存儲表名稱使用的變量,然後指明這個變量類型爲數據庫中的表,這樣我們可以通過這個數據表類型的變量得到相應的表的信息。
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context"  Description="Table that the stored procedures should be based on." %>

        如果想訪問視圖的話,則將變量類型Type中的SchemaExplorer.TableSchema修改爲SchemaExplorer.ViewSchema即可。
        得到表名的方法
CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>

        下面利用循環語句遍歷表的各個列,拼出存儲過程需要傳遞的參數。
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
<% } %>

        調用的GetSqlParameterStatement方法是用來生成參數的字符串,例如生成“@CustomerID nchar(5)”,後邊緊跟的if判斷是用來生成參數之間相隔使用的逗號的。
        生成參數字符串的方法,參數爲SchemaExplorer.ColumnSchema列類型
 1 <script runat="template">
 2 public string GetSqlParameterStatement(ColumnSchema column)
 3 {
 4       string param = "@" + column.Name + " " + column.NativeType;
 5  
 6       switch (column.DataType)
 7       {
 8             case DbType.Decimal:
 9             {
10                   param += "(" + column.Precision + "" + column.Scale + ")";
11                   break;
12             }
13             default:
14             {
15                   if (column.Size > 0)
16                   {
17                         param += "(" + column.Size + ")";
18                   }
19                   break;
20             }
21       }
22  
23       return param;
24 }
25 </script>

        下面來生成需要更新的字段,更新時僅能更新非主鍵字段的值,在SchemaExplorer中支持這種區別,使用SourceTable.NonPrimaryKeyColumns即可得到非主鍵字段的集合。
1UPDATE [<%= SourceTable.Name %>] SET
2      <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
3      [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>= @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
4      <% } %>

    然後再使用SourceTable.PrimaryKey.MemberColumns得到數據表中的主鍵集合,生成更新條件

1 WHERE
2       <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
3       <% if (i > 0) { %>AND <% } %>
4       [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>= @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
5       <% } %>

    以下爲整體的代碼結構
 1 <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
 2       Description="Generates a update stored procedure." %>
 3  
 4 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
 5       Category="Context"
 6       Description="Table that the stored procedures should be based on." %>
 7  
 8 <%@ Assembly Name="SchemaExplorer" %>
 9  
10 <%@ Import Namespace="SchemaExplorer" %>
11       
12 <script runat="template">
13 public string GetSqlParameterStatement(ColumnSchema column)
14 {
15       string param = "@" + column.Name + " " + column.NativeType;
16  
17       switch (column.DataType)
18       {
19             case DbType.Decimal:
20             {
21                   param += "(" + column.Precision + ", " + column.Scale + ")";
22                   break;
23             }
24             default:
25             {
26                   if (column.Size > 0)
27                   {
28                         param += "(" + column.Size + ")";
29                   }
30                   break;
31             }
32       }
33  
34       return param;
35 }
36 </script>
37  
38 -----------------------------------------------------------------
39 -- Date Created: <%= DateTime.Now.ToLongDateString() %>
40 -- Created By:   Generated by CodeSmith
41 -----------------------------------------------------------------
42  
43 CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>
44       <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
45       <%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
46       <% } %>
47 AS
48  
49 UPDATE [<%= SourceTable.Name %>] SET
50       <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
51       [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
52       <% } %>
53 WHERE
54       <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
55       <% if (i > 0) { %>AND <% } %>
56       [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
57       <% } %>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章