Helper.cs 模板生成帮助类

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

 44ExpandedSubBlockEnd.gif        }

 45ExpandedSubBlockEnd.gif    }

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

 79ExpandedSubBlockEnd.gif    }

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

115ExpandedSubBlockEnd.gif        }

116ExpandedSubBlockEnd.gif    }

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

152ExpandedSubBlockEnd.gif        }

153ExpandedSubBlockEnd.gif    }

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

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

183ExpandedSubBlockEnd.gif    }

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

191InBlock.gif        else
192ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
193InBlock.gif            return "";
194ExpandedSubBlockEnd.gif        }

195ExpandedSubBlockEnd.gif    }

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

202InBlock.gif        else
203ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
204InBlock.gif            return "";
205ExpandedSubBlockEnd.gif        }

206ExpandedSubBlockEnd.gif    }

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

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

220ExpandedSubBlockEnd.gif        }

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

225ExpandedSubBlockEnd.gif    }

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

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

242InBlock.gif            else
243ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
244InBlock.gif                className= className.Substring(0, className.Length - 1);
245InBlock.gif                return className.Substring(0,1).ToUpper()+className.Substring(1);
246ExpandedSubBlockEnd.gif            }

247InBlock.gif            
248ExpandedSubBlockEnd.gif        }

249InBlock.gif        else
250ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
251InBlock.gif            return className.Substring(0,1).ToUpper()+className.Substring(1);
252ExpandedSubBlockEnd.gif        }

253ExpandedSubBlockEnd.gif    }

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

259InBlock.gif
260InBlock.gif    public string GetFiledName(ColumnSchema column)
261ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
262InBlock.gif        return column.Name.Substring(01).ToLower() + column.Name.Substring(1);
263ExpandedSubBlockEnd.gif    }

264InBlock.gif
265InBlock.gif    public string GetPropertyName(ColumnSchema column)
266ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
267InBlock.gif        return column.Name.Substring(01).ToUpper() + column.Name.Substring(1);
268ExpandedSubBlockEnd.gif    }

269InBlock.gif    
270InBlock.gif    public string GetTableName(TableSchema Table)
271ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
272InBlock.gif        return Table.Name.ToString();    
273ExpandedSubBlockEnd.gif    }

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

281InBlock.gif        else
282ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
283InBlock.gif            if(column.IsPrimaryKeyMember)
284ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
285InBlock.gif                return "Output";    
286ExpandedSubBlockEnd.gif            }

287InBlock.gif            else
288ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
289InBlock.gif                return "";
290ExpandedSubBlockEnd.gif            }

291ExpandedSubBlockEnd.gif        }

292ExpandedSubBlockEnd.gif    }

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

300InBlock.gif        else
301ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
302InBlock.gif            if(column.IsPrimaryKeyMember)
303ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
304InBlock.gif                return "";
305ExpandedSubBlockEnd.gif            }

306InBlock.gif            else
307ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
308InBlock.gif                return "["+column.Name+"]";    
309ExpandedSubBlockEnd.gif            }

310ExpandedSubBlockEnd.gif        }

311ExpandedSubBlockEnd.gif    }

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

319InBlock.gif        else
320ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
321InBlock.gif            if(column.IsPrimaryKeyMember)
322ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
323InBlock.gif                return "";
324ExpandedSubBlockEnd.gif            }

325InBlock.gif            else
326ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
327InBlock.gif                return GetMorePrimaryAt(Table,column)+column.Name;    
328ExpandedSubBlockEnd.gif            }

329ExpandedSubBlockEnd.gif        }

330ExpandedSubBlockEnd.gif    }

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

338InBlock.gif        else
339ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
340InBlock.gif            if(column.IsPrimaryKeyMember)
341ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
342InBlock.gif                return "";
343ExpandedSubBlockEnd.gif            }

344InBlock.gif            else
345ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
346InBlock.gif                return "@";    
347ExpandedSubBlockEnd.gif            }

348ExpandedSubBlockEnd.gif        }
    
349ExpandedSubBlockEnd.gif    }

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

357InBlock.gif        else
358ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
359InBlock.gif            if(column.IsPrimaryKeyMember)
360ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
361InBlock.gif                return "";
362ExpandedSubBlockEnd.gif            }

363InBlock.gif            else
364ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
365InBlock.gif                return GetComma(column,columns);    
366ExpandedSubBlockEnd.gif            }

367ExpandedSubBlockEnd.gif        }

368ExpandedSubBlockEnd.gif    }

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

376InBlock.gif        else
377ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
378InBlock.gif            return false;
379ExpandedSubBlockEnd.gif        }

380ExpandedSubBlockEnd.gif    }

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

388InBlock.gif        else
389ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
390InBlock.gif            if(column.IsPrimaryKeyMember)
391ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
392InBlock.gif                return "";
393ExpandedSubBlockEnd.gif            }

394InBlock.gif            else
395ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
396InBlock.gif                return "=";    
397ExpandedSubBlockEnd.gif            }

398ExpandedSubBlockEnd.gif        }
        
399ExpandedSubBlockEnd.gif    }

400ExpandedBlockEnd.gif}

401 None.gif
402 None.gif

转载于:https://www.cnblogs.com/wubiyu/articles/807569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值