Helper.cs 模板生成帮助类

  1using System;
  2using System.Data;
  3using System.ComponentModel;
  4using CodeSmith.Engine;
  5using SchemaExplorer;
  6using Microsoft.CSharp;
  7
  8public class Helper : CodeTemplate
  9{
 10    public string GetCSharpVariableType(ColumnSchema column)
 11    {
 12        if (column.Name.EndsWith("TypeCode")) return column.Name;
 13
 14        switch (column.DataType)
 15        {
 16            case DbType.AnsiString: return "string";
 17            case DbType.AnsiStringFixedLength: return "string";
 18            case DbType.Binary: return "byte[]";
 19            case DbType.Boolean: return "bool";
 20            case DbType.Byte: return "byte";
 21            case DbType.Currency: return "decimal";
 22            case DbType.Date: return "DateTime";
 23            case DbType.DateTime: return "DateTime";
 24            case DbType.Decimal: return "decimal";
 25            case DbType.Double: return "double";
 26            case DbType.Guid: return "Guid";
 27            case DbType.Int16: return "short";
 28            case DbType.Int32: return "int";
 29            case DbType.Int64: return "long";
 30            case DbType.Object: return "object";
 31            case DbType.SByte: return "sbyte";
 32            case DbType.Single: return "float";
 33            case DbType.String: return "string";
 34            case DbType.StringFixedLength: return "string";
 35            case DbType.Time: return "TimeSpan";
 36            case DbType.UInt16: return "ushort";
 37            case DbType.UInt32: return "uint";
 38            case DbType.UInt64: return "ulong";
 39            case DbType.VarNumeric: return "decimal";
 40            default:
 41                {
 42                    return "__UNKNOWN__" + column.NativeType;
 43                }

 44        }

 45    }

 46
 47    public string GetSqlDbType(ColumnSchema column)
 48    {
 49        switch (column.NativeType)
 50        {
 51            case "bigint"return "BigInt";
 52            case "binary"return "Binary";
 53            case "bit"return "Bit";
 54            case "char"return "Char";
 55            case "datetime"return "DateTime";
 56            case "decimal"return "Decimal";
 57            case "float"return "Float";
 58            case "image"return "Image";
 59            case "int"return "Int";
 60            case "money"return "Money";
 61            case "nchar"return "NChar";
 62            case "ntext"return "NText";
 63            case "numeric"return "Decimal";
 64            case "nvarchar"return "NVarChar";
 65            case "real"return "Real";
 66            case "smalldatetime"return "SmallDateTime";
 67            case "smallint"return "SmallInt";
 68            case "smallmoney"return "SmallMoney";
 69            case "sql_variant"return "Variant";
 70            case "sysname"return "NChar";
 71            case "text"return "Text";
 72            case "timestamp"return "Timestamp";
 73            case "tinyint"return "TinyInt";
 74            case "uniqueidentifier"return "UniqueIdentifier";
 75            case "varbinary"return "VarBinary";
 76            case "varchar"return "VarChar";
 77            defaultreturn "__UNKNOWN__" + column.NativeType;
 78        }

 79    }

 80    
 81    public int GetParamSize(ParameterSchema param)
 82    {
 83        switch (param.NativeType)
 84        {
 85            case "bigint"return 8;
 86            case "binary"return 8;
 87            case "bit"return 1;
 88            case "char"return 1;
 89            case "datetime"return 8;
 90            case "decimal"return 4;
 91            case "float"return 8;
 92            case "image"return 8;
 93            case "int"return 4;
 94            case "money"return 8;
 95            case "nchar"return param.Size;
 96            case "ntext"return 16;
 97            case "numeric"return 8;
 98            case "nvarchar"return param.Size;
 99            case "real"return 8;
100            case "smalldatetime"return 4;
101            case "smallint"return 2;
102            case "smallmoney"return 2;
103            case "sql_variant"return param.Size;
104            case "sysname"return param.Size;
105            case "text"return param.Size;
106            case "timestamp"return 8;
107            case "tinyint"return 1;
108            case "uniqueidentifier"return 16;
109            case "varbinary"return 16;
110            case "varchar"return param.Size;
111            default:
112            {
113                return -1;
114            }

115        }

116    }

117    
118    public int GetParamSize(ColumnSchema column)
119    {
120        switch (column.NativeType)
121        {
122            case "bigint"return 8;
123            case "binary"return 8;
124            case "bit"return 1;
125            case "char"return 1;
126            case "datetime"return 8;
127            case "decimal"return 4;
128            case "float"return 8;
129            case "image"return 8;
130            case "int"return 4;
131            case "money"return 8;
132            case "nchar"return column.Size;
133            case "ntext"return 16;
134            case "numeric"return 8;
135            case "nvarchar"return column.Size;
136            case "real"return 8;
137            case "smalldatetime"return 4;
138            case "smallint"return 2;
139            case "smallmoney"return 2;
140            case "sql_variant"return column.Size;
141            case "sysname"return column.Size;
142            case "text"return column.Size;
143            case "timestamp"return 8;
144            case "tinyint"return 1;
145            case "uniqueidentifier"return 16;
146            case "varbinary"return 16;
147            case "varchar"return column.Size;
148            default:
149            {
150                return -1;
151            }

152        }

153    }

154    
155    public string GetTypeAndSize(SchemaExplorer.ColumnSchema column)
156    {
157        string ret = String.Empty;
158        ret += column.NativeType;
159        if (column.NativeType == "varbinary" ||
160            column.NativeType == "nvarchar" ||
161            column.NativeType == "binary" ||
162            column.NativeType == "char" || 
163            column.NativeType == "nchar")
164            ret +=  GetSize(column.Size);
165        return ret;
166    }

167    
168    public string GetSize(int size)
169    {
170        switch (size)
171        {
172            case 0:
173                return "";
174            case 2147483647:
175                return "";
176            case 1073741823:
177                return "";
178            case -1:
179                return "(MAX)";
180            default:
181                return "(" + size + ")";
182        }

183    }

184    
185    public string GetComma(ColumnSchema column,ColumnSchemaCollection columns)
186    {
187        if (column.Name != columns[columns.Count-1].Name)
188        {
189            return ",";
190        }

191        else
192        {
193            return "";
194        }

195    }

196    public string GetAnd(ColumnSchema column,ColumnSchemaCollection columns)
197    {
198        if (column.Name != columns[columns.Count-1].Name)
199        {
200            return "And";
201        }

202        else
203        {
204            return "";
205        }

206    }

207
208    public string GetPrimaryKeyType(TableSchema table)
209    {
210        if (table.PrimaryKey != null)
211        {
212            if (table.PrimaryKey.MemberColumns.Count == 1)
213            {
214                return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
215            }

216            else
217            {
218                throw new ApplicationException("This template will not work on primary keys with more than one member column.");
219            }

220        }

221        else
222        {
223            throw new ApplicationException("This template will only work on tables with a primary key.");
224        }

225    }

226
227    public string GetClassName(TableSchema table)
228    {
229        string className = table.Name;
230        if (className.IndexOf("_"> 0)
231        {
232            className = table.Name.Substring(table.Name.IndexOf("_"+ 1);
233        }

234        if (className.EndsWith("s"))
235        {
236            
237            if(className.EndsWith("ies"))
238            {
239                className = className.Replace("ies","y");
240                return className.Substring(0,1).ToUpper()+className.Substring(1);
241            }

242            else
243            {
244                className= className.Substring(0, className.Length - 1);
245                return className.Substring(0,1).ToUpper()+className.Substring(1);
246            }

247            
248        }

249        else
250        {
251            return className.Substring(0,1).ToUpper()+className.Substring(1);
252        }

253    }

254    
255    public string GetparameteryName(TableSchema table)
256    {
257        return table.Name.Substring(0,1).ToLower()+table.Name.Substring(1);
258    }

259
260    public string GetFiledName(ColumnSchema column)
261    {
262        return column.Name.Substring(01).ToLower() + column.Name.Substring(1);
263    }

264
265    public string GetPropertyName(ColumnSchema column)
266    {
267        return column.Name.Substring(01).ToUpper() + column.Name.Substring(1);
268    }

269    
270    public string GetTableName(TableSchema Table)
271    {
272        return Table.Name.ToString();    
273    }

274    
275    public string GetMorePrimaryOutput(TableSchema Table,ColumnSchema column)
276    {
277        if(Table.PrimaryKey.MemberColumns.Count >1)
278        {
279            return "";
280        }

281        else
282        {
283            if(column.IsPrimaryKeyMember)
284            {
285                return "Output";    
286            }

287            else
288            {
289                return "";
290            }

291        }

292    }

293    
294    public string GetInsertParam(TableSchema Table,ColumnSchema column)
295    {
296        if(Table.PrimaryKey.MemberColumns.Count >1)
297        {
298            return "["+column.Name+"]";    
299        }

300        else
301        {
302            if(column.IsPrimaryKeyMember)
303            {
304                return "";
305            }

306            else
307            {
308                return "["+column.Name+"]";    
309            }

310        }

311    }

312    
313    public string GetInsertInOrOutParam(TableSchema Table,ColumnSchema column)
314    {
315        if(Table.PrimaryKey.MemberColumns.Count >1)
316        {
317            return GetMorePrimaryAt(Table,column)+column.Name;    
318        }

319        else
320        {
321            if(column.IsPrimaryKeyMember)
322            {
323                return "";
324            }

325            else
326            {
327                return GetMorePrimaryAt(Table,column)+column.Name;    
328            }

329        }

330    }

331    
332    public string GetMorePrimaryAt(TableSchema Table,ColumnSchema column)
333    {
334        if(Table.PrimaryKey.MemberColumns.Count >1)
335        {
336            return "@";    
337        }

338        else
339        {
340            if(column.IsPrimaryKeyMember)
341            {
342                return "";
343            }

344            else
345            {
346                return "@";    
347            }

348        }
    
349    }

350    
351    public string GetMorePrimaryComma(TableSchema Table,ColumnSchema column,ColumnSchemaCollection columns)
352    {
353        if(Table.PrimaryKey.MemberColumns.Count >1)
354        {
355            return GetComma(column,columns);    
356        }

357        else
358        {
359            if(column.IsPrimaryKeyMember)
360            {
361                return "";
362            }

363            else
364            {
365                return GetComma(column,columns);    
366            }

367        }

368    }

369    
370    public bool IsMorePrimary(TableSchema Table)
371    {
372        if(Table.PrimaryKey.MemberColumns.Count >1)
373        {
374            return true;    
375        }

376        else
377        {
378            return false;
379        }

380    }

381    
382    public string GetMorePrimaryEqual(TableSchema Table,ColumnSchema column)
383    {
384        if(Table.PrimaryKey.MemberColumns.Count >1)
385        {
386            return "=";    
387        }

388        else
389        {
390            if(column.IsPrimaryKeyMember)
391            {
392                return "";
393            }

394            else
395            {
396                return "=";    
397            }

398        }
        
399    }

400}

401
402
posted @ 2007-07-05 19:54  吴碧宇  阅读(401)  评论(1编辑  收藏  举报