CodeSmith模板(ToolsCodeTemplate)

using System;
using SchemaExplorer;
using System.Data;
using CodeSmith.Engine;
using System.Text.RegularExpressions;

public class ToolsCodeTemplate:CodeTemplate
{
 public string GetModelClassName(TableSchema table)
 {
  string result;
  if ( table.ExtendedProperties.Contains("ModelName") )
  {
   result = (string)table.ExtendedProperties["ModelName"].Value; 
   return MakePascal(result);
  }
 
  if (table.Name.EndsWith("s"))
  {
   result = table.Name.Substring(0,table.Name.Length-1);
  }
  else
  {
   result = table.Name;
  }
 
  return MakePascal(result);
 }
 public string GetPropertyType(ColumnSchema column)
 {
  return GetCSharpTypeFromDBFieldType(column);
 }
 public string GetCSharpTypeFromDBFieldType(ColumnSchema column)
 {
  if (column.Name.EndsWith("TypeCode")) return column.Name;
  string type;
  switch (column.DataType)
  {
   case DbType.AnsiString: type= "string";break;
   case DbType.AnsiStringFixedLength: type= "string";break;
   case DbType.Binary: type= "byte[]";break;
   case DbType.Boolean: type= "bool";break;
   case DbType.Byte: type= "byte";break;
   case DbType.Currency: type= "decimal";break;
   case DbType.Date: type= "DateTime";break;
   case DbType.DateTime: type= "DateTime";break;
   case DbType.Decimal: type= "decimal";break;
   case DbType.Double: type= "double";break;
   case DbType.Guid: type= "Guid";break;
   case DbType.Int16: type= "short";break;
   case DbType.Int32: type= "int";break;
   case DbType.Int64: type= "long";break;
   case DbType.Object: type= "object";break;
   case DbType.SByte: type= "sbyte";break;
   case DbType.Single: type= "float";break;
   case DbType.String: type= "string";break;
   case DbType.StringFixedLength: type= "string";break;
   case DbType.Time: type= "TimeSpan";break;
   case DbType.UInt16: type= "ushort";break;
   case DbType.UInt32: type= "uint";break;
   case DbType.UInt64: type= "ulong";break;
   case DbType.VarNumeric: type= "decimal";break;
   default:
   {
    type= "__UNKNOWN__" + column.NativeType;
    break;
   }
  }
  if(column.AllowDBNull&&
   column.SystemType.IsValueType)
  {
   type=type+"?";
  }
  return type;
 }
 
 public string GetPropertyName(ColumnSchema column)
 {
  return MakePascal(GetNameFromDBFieldName(column));
 }
 public string GetNameFromDBFieldName(ColumnSchema column)
 {
  return column.Name;
 }
 
 //I will be dirty too! -- coded by shenbo
 public string MakePascal(string value)
 {
  return value.Substring(0, 1).ToUpper() + value.Substring(1);
 }
 
 public string MakeSingle(string name)
 {
  Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
  Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
  Regex plural3 = new Regex("(?<keep>[sxzh])es$");
  Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");
 
  if(plural1.IsMatch(name))
   return plural1.Replace(name, "${keep}y");
  else if(plural2.IsMatch(name))
   return plural2.Replace(name, "${keep}");
  else if(plural3.IsMatch(name))
   return plural3.Replace(name, "${keep}");
  else if(plural4.IsMatch(name))
   return plural4.Replace(name, "${keep}");
 
  return name;
 }
 public void PrintHeader()
 {
  Response.WriteLine("//************************************************************");
  Response.WriteLine("//******************************作者:金彪*********************");
  Response.WriteLine("//******************************时间:"+DateTime.Now.ToString()+"*******");
  Response.WriteLine();
 }
 
 public string GetPKName(TableSchema TargetTable)
 {
  if (TargetTable.PrimaryKey != null)
  {
   if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
   {
    return TargetTable.PrimaryKey.MemberColumns[0].Name;
   }
   else
   {
    throw new Exception("This template will not work on primary keys with more than one member column.");
   }
  }
  else
  {
   throw new Exception("This template will only work on tables with a primary key.");
  }
 }
 
 public string GetPKType(TableSchema TargetTable)
 {
  if (TargetTable.PrimaryKey != null)
  {
   if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
   {
    return GetCSharpTypeFromDBFieldType(TargetTable.PrimaryKey.MemberColumns[0]);
   }
   else
   {
    throw new ApplicationException("This template will not work on primary keys with more than one member column.");
   }
  }
  else
  {
   throw new ApplicationException("This template will only work on MyTables with a primary key.");
  }
 }
 
 public string MakeCamel(string value)
 {
  return value.Substring(0, 1).ToLower() + value.Substring(1);
 }
 
 public string MakePlural(string name)
 {
  Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
  Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
  Regex plural3 = new Regex("(?<keep>[sxzh])$");
  Regex plural4 = new Regex("(?<keep>[^sxzhy])$");
 
  if(plural1.IsMatch(name))
   return plural1.Replace(name, "${keep}ies");
  else if(plural2.IsMatch(name))
   return plural2.Replace(name, "${keep}s");
  else if(plural3.IsMatch(name))
   return plural3.Replace(name, "${keep}es");
  else if(plural4.IsMatch(name))
   return plural4.Replace(name, "${keep}s");
 
  return name;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值