CodeSmith OracleModel写法

<%--
Name:OracleModel
Author: 小谈
Description: 根据指定的数据库生成业务逻辑类模板
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="根据指定的数据库表生成访问层类模板" ResponseEncoding="Unicode" %>
<%@ Property Name=" NameSpace" Type="System.String" Default="ty.Model"  Category="Context" Description="名称空间" %>
<%@ Property Name="DevelopersName" Type="String" Default="小谈" Category="Context"  Description="开发人员姓名" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="数据库表名" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
///////////////////////////////////////////////////////////////////////////////////////
// File: <%= GetDecimal(SourceTable) %>.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright <%= DevelopersName%> <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
//    <%= DateTime.Now.ToShortDateString() %>    <%= DevelopersName%>    Original Version
///////////////////////////////////////////////////////////////////////////////////////
using System;

namespace <% =NameSpace %>
{
 /// <summary>
 /// 实体类<% =GetDecimal(SourceTable) %>(属性说明自动提取数据库字段的描述信息)
 /// </summary>
 [Serializable]
 public class <% =GetDecimal(SourceTable) %>
 {
  public <% =GetDecimal(SourceTable) %>()
  {}
  #region Model
  <% =GetVariables() %>
  
  <% =GetMethod() %>
  #endregion
  
  
 }
}

<script runat="template">
// My methods here.
//截取字符串前面的小数点只显示后面的字符串
public string GetDecimal(TableSchema table)
{
    return table.Name.Substring(table.Name.LastIndexOf(".")+1).ToString();
}


//判断数据类型是否为空
public string IsNull(ColumnSchema column)
{
 if(column.AllowDBNull)
 {
  return "?";
 }
 return "";
}

//把字符串转换成小写
public string IsReplace(string str)
{
 return str.ToLower();
}

#region  拼装字符串

#region 拼装初始值
public string GetVariables()
{
 string str=string.Empty;
 
 for(int i=0;i<this.SourceTable.Columns.Count;i++)
 {
  if(IsNull(this.SourceTable.Columns[i])=="")
  {//数据类不型为空,数据类型不要加?
   str+=string.Format("private {0} _{1};",
   GetOracleDbType(this.SourceTable.Columns[i]),
   IsReplace(this.SourceTable.Columns[i].Name));
  }
  else
  {//为空时加问号
   if(GetOracleDbType(this.SourceTable.Columns[i])=="string")
   {//特殊情况数据类型是string类型不管为空,还是不能为空不能加?
    str+=string.Format("private {0} _{1};",
    GetOracleDbType(this.SourceTable.Columns[i]),
    IsReplace(this.SourceTable.Columns[i].Name));
   }
   else
   {//否则数据类型是非空类型后面带问号
    str+=string.Format("private {0}? _{1};",
    GetOracleDbType(this.SourceTable.Columns[i]),
    IsReplace(this.SourceTable.Columns[i].Name));
   }
  }
  
  if(i!=(this.SourceTable.Columns.Count-1))
  {
   str+="/r/n  ";
  }
 
 }
  
 return str;
}
#endregion

#region 拼装方法
public string GetMethod()
{
 string str=string.Empty;
 string a="{";
 string b="}";
 
 for(int i=0;i<this.SourceTable.Columns.Count;i++)
 {
  if(IsNull(this.SourceTable.Columns[i])=="")
  {//数据类不型为空,数据类型不要加?
   //拼装格式
   str+="/// <summary>/r/n  ";
   str+="/// /r/n  ";
   str+="/// <summary>/r/n  ";
   
   str+=string.Format("public {0} {1}",
   GetOracleDbType(this.SourceTable.Columns[i]),
   this.SourceTable.Columns[i].Name);
   str+="/r/n  {/r/n   ";
   str+=string.Format("set{1} _{0} = value;{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
   str+="/r/n   ";
   str+=string.Format("get{1}return _{0};{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
   str+="/r/n  }";
  }
  else
  {//为空时加问号
   if(GetOracleDbType(this.SourceTable.Columns[i])=="string")
   {//特殊情况数据类型是string类型不管为空,还是不能为空不能加?
    //拼装格式
    str+="/// <summary>/r/n  ";
    str+="/// /r/n  ";
    str+="/// <summary>/r/n  ";
    
    str+=string.Format("public {0} {1}",
    GetOracleDbType(this.SourceTable.Columns[i]),
    this.SourceTable.Columns[i].Name);
    str+="/r/n  {/r/n   ";
    str+=string.Format("set{1} _{0} = value;{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="/r/n   ";
    str+=string.Format("get{1}return _{0};{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="/r/n  }";
   }
   else
   {//否则数据类型是非空类型后面带问号
    //拼装格式
    str+="/// <summary>/r/n  ";
    str+="/// /r/n  ";
    str+="/// <summary>/r/n  ";
    
    str+=string.Format("public {0}? {1}",
    GetOracleDbType(this.SourceTable.Columns[i]),
    this.SourceTable.Columns[i].Name);
    str+="/r/n  {/r/n   ";
    str+=string.Format("set{1} _{0} = value;{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="/r/n   ";
    str+=string.Format("get{1}return _{0};{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="/r/n  }";
   }
  }
  
  if(i!=(this.SourceTable.Columns.Count-1))
  {
   str+="/r/n  ";
  }
 
 }
 return str;
}
#endregion

#endregion

#region 判断数据类型


#region 根据列获取数据库的类型
///<summary>
///根据列获取数据库的类型
///<summary>
public string GetOracleDbType(ColumnSchema column)
{
 switch (column.NativeType)
 {
  case "Char": case "char": case "CHAR":
  case "varchar2": case "VarChar2": case "Varchar2": case "VARCHAR2":
  case "nchar": case "Nchar": case "NCHAR":
  case "nvarchar2": case "NVarChar2": case "NVARCHAR2":
  case "Long": case "LONG": case "long":
  case "Raw": case "RAW": case "raw":
  case "Long raw": case "LONG RAW": case "long raw":
  case "Rowid": case "rowid": case "ROWID":
  case "Blob": case "blob": case "BLOB":
  case "Clob": case "clob": case "CLOB":
  case "nclob": case "NCLOB": case "Nclob":
  case "Bfile": case "bfile": case "BFILE":
  case "Urowid": case "urowid": case "UROWID":
  return "string";
   
  case "number": case "Number": case "NUMBER":
  return "int";
  
  case "double": case "Double": case "DOUBLE":
  case "float": case "Float": case "FLOAT":
  return "decimal";
  
  case "DateTime": case "datetime": case "DATETIME":
  case "Date": case "date": case "DATE":
  return "DateTime";
  
  default: return "__UNKNOWN__" + column.NativeType; 
 }
}
#endregion

#endregion
</script>

你可能感兴趣的:(Date,数据库,String,assembly,import,float)