ldj/codeSmith/bakcup/TableImplements.cst

133 lines
4.4 KiB
Plaintext

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="This template demonstrates using properties defined in external assemblies." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="The table to use for this sample." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<script runat="template">
public string getTableName()
{
return this.SourceTable.Name ;
}
public string getObjClassName()
{
return this.SourceTable.Name.Substring(2);
}
public string getColums4Insert(string prefix)
{
String colums ="";
foreach (ColumnSchema column in this.SourceTable.Columns) {
colums+= prefix + column.Name + ",";
}
return colums.Substring(0,colums.Length -1) ;
}
public string getColums4update()
{
String colums ="";
foreach (ColumnSchema column in this.SourceTable.Columns) {
colums+= column.Name + " = @" +column.Name +", " ;
}
return colums.Substring(0,colums.Length -1) ;
}
</script>
/// <summary>
///Data Implemention Object
///BASIC CRUD CLASS FOR TABLE <%=this.SourceTable.Name%>
///By wm with codesmith.
///on <%= DateTime.Now.ToString("MM/dd/yyyy")%>
/// </summary>
using System;
using Tracen.Data.BaseObject;
using Tracen.Utils;
using System.Data.SqlClient;
namespace Deiniu.Wms.Data.Model
{
#region <%= getObjClassName() %>_Imp
[Serializable] class <%= getObjClassName() %>_Imp : BaseModel_Imp{
protected override void Assem_Model(SqlDataReader reader, BaseModel obj) {
if (reader != null && !reader.IsClosed) {
<%= getObjClassName() %> tmpObj = (<%= getObjClassName() %>)obj;
if (reader.Read()) {
<%
int i = 0;
foreach (ColumnSchema column in this.SourceTable.Columns) { %>
if (!reader.IsDBNull(<%=i%>)) tmpObj._<%=column.Name %> = reader.Get<%= column.DataType %>(<%=i%>)<% if(column.DataType.ToString().Equals("DateTime")) {%>.ToString() <% } %>;
<% i++;
}
%>
reader.Close();
}
}
}
protected override void CmdPrepare(SqlCommand oraCmd) {
<%= getObjClassName() %> tmpObj = (<%= getObjClassName() %>)ModelObj;
switch (this._op_flag {
case (int)op_flag.add:
_strSql = "INSERT INTO dbo. <%= getTableName() %>(<%=getColums4Insert("")%>) VALUES(<%=getColums4Insert("@")%>)";
break;
case (int)op_flag.update:
_strSql = "UPDATE dbo.<%= getTableName() %> SET <%= getColums4update()%> WHERE ID = @ID";
break;
case (int)op_flag.delete:
_strSql = "UPDATE dbo.<%= getTableName() %> SET DR =0 WHERE ID = @ID";
break;
case (int)op_flag.getObj:
_strSql = "SELECT * FROM dbo.<%= getTableName() %> WHERE ID = @ID";
break;
case (int)op_flag.queryAll:
_strSql = "SELECT * FROM dbo.<%= getTableName() %> ";
break;
case (int)op_flag.queryActived:
_strSql = "SELECT * FROM dbo.<%= getTableName() %> WHERE DR =1";
break;
case (int)op_flag.getPk:
_strSql = "SELECT MAX(ID) FROM dbo.<%= getTableName() %> WHERE DR =1";
break;
}
oraCmd.CommandText = _strSql;
fillParameters(oraCmd,tmpObj);
}
private void fillParameters(SqlCommand oraCmd, Department tmpObj) {
switch (this._op_flag) {
case (int)op_flag.getObj:
case (int)op_flag.delete:
case (int)op_flag.getPk:
oraCmd.Parameters.AddWithValue("@ID", tmpObj.ID);
break;
case (int)op_flag.queryAll:
case 0:
case (int)op_flag.queryActived:
return;
case (int)op_flag.update:
oraCmd.Parameters.AddWithValue("@ID", tmpObj.ID);
break;
}
if ((_op_flag !=(int)op_flag.update) &&( _op_flag !=(int)op_flag.add)) return;
<%
foreach (ColumnSchema column in this.SourceTable.Columns) { %>
oraCmd.Parameters.AddWithValue("@<%=column.Name %>", VerifyDbnull(tmpObj._<%=column.Name %>)) ;
<%
}
%>
}
}
#endregion
}