DT 的数据字典
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

259 lines
7.2 KiB

<%@ CodeTemplate Language="C#" ClassName="Go_ModelJson" TargetLanguage="C#" Debug="False" Description="" encoding="GB2312" %>
<%@ Property Name="databaseProjectName" Type="System.String" Default="SpSampleDb" Optional="True" Category="Context" Description="" %>
<%@ Property Name="nameSpace" Type="System.String" Default="Model" Optional="True" Category="Context" Description="" %>
<%
DbEntity db = Template.GetDataBase(databaseProjectName);
foreach(Table tb in db.Tables)
{
OpenResponse(databaseProjectName+"\\GoModelJson\\"+tb.ModuleName+"\\"+tb.PropertyName.ToLower()+".go");
string pkParmList = "";
foreach(Field fd in tb.Fields)
{
if(fd.IsPK == true)
pkParmList += fd.DataType.DotNetType+" "+fd.MemberName+",";
}
pkParmList = pkParmList.Trim(',');
string noDefaultFields = "";
string noDefaultFieldFormats = "";
string noDefaultFieldValues = "";
int iField = 0;
foreach(Field fd in tb.Fields)
{
if( (fd.DefaultValue != "" || fd.DefaultValue != null) && fd.Identity.Enable == false)
{
noDefaultFields += ""+fd.PropertyName.ToLower()+",";
if( fd.DataType.Convert == "ToString")
noDefaultFieldValues += "'\"+formatcom.String(m."+fd.PropertyName+")+\"',";
else if( fd.DataType.Convert == "ToDateTime")
noDefaultFieldValues += "'\"+formatcom.ToString(m."+fd.PropertyName+")+\"',";
else if( fd.DataType.Convert == "ToDecimal")
noDefaultFieldValues += "'\"+formatcom.FloatToStr(m."+fd.PropertyName+")+\"',";
else if( fd.DataType.Convert == "ToInt32")
noDefaultFieldValues += "'\"+formatcom.IntToStr(m."+fd.PropertyName+")+\"',";
else
noDefaultFieldValues += "\"+formatcom.String(m."+fd.PropertyName+")+\",";
if( fd.DataType.GroupName.ToUpper() == "NUMBER")
noDefaultFieldFormats += "{"+iField.ToString()+"},";
//else if( fd.DataType.GroupName.ToUpper() == "DATETIME")
// noDefaultFieldFormats += "{"+iField.ToString()+"},";
else
noDefaultFieldFormats += "'{"+iField.ToString()+"}',";
iField++;
}
}
if( noDefaultFields.EndsWith(","))
noDefaultFields = noDefaultFields.Substring(0,noDefaultFields.Length-1);
if( noDefaultFieldValues.EndsWith(","))
noDefaultFieldValues = noDefaultFieldValues.Substring(0,noDefaultFieldValues.Length-1);
noDefaultFieldFormats = noDefaultFieldFormats.Trim(',');
noDefaultFieldFormats = noDefaultFieldFormats.Trim('+');
%>
/*******************************************************/
/*Project:
Module :
Description :
Date : <%=DateTime.Now.ToString()%>
Create : Lxc
Update :
TODO : */
/*******************************************************/
package <%=tb.ModuleName.ToLower()%>
import (
"hm/eclibs/formatcom"
"hm/eclibs/logger"
"hm/eclibs/orm"
)
type <%=tb.PropertyName%> struct {
<%
foreach(Field fd in tb.Fields)
{
if( fd.DataType.Convert == "ToDateTime")
{
Response.Write("\t"+fd.PropertyName +" string ");
}
else if( fd.DataType.Convert == "ToInt32")
{
Response.Write("\t"+fd.PropertyName +" int64 ");
}
else if( fd.DataType.Convert == "ToString")
{
Response.Write("\t"+fd.PropertyName +" string");
}
else if( fd.DataType.Convert == "ToDecimal")
{
Response.Write("\t"+fd.PropertyName +" float64");
}
else
Response.Write("\t"+fd.PropertyName +" string");
Response.WriteLine("\t`json:\""+fd.PropertyName.ToLower()+"\"`");
}
%>
}
func (m *<%=tb.PropertyName%>)Add() bool {
sql := "insert into " + orm.TableName("<%=tb.PropertyName%>") + " (<%=noDefaultFields%>) values(<%=noDefaultFieldValues%>) "
ok, err := orm.Insert(sql)
if err != nil {
logger.Error(err.Error())
}
return ok
}
func (m *<%=tb.PropertyName%>) Update( ) bool {
sql := "Update " + orm.TableName("<%=tb.PropertyName%>") + " set <%
iField = 0;
string updstr = "";
string upvalue = "";
iField = 0;
bool bHasPrevPk = false;
foreach(Field fd in tb.Fields)
{
if( fd.Identity.Enable == false && (fd.IsPK == false || bHasPrevPk== true))
{
updstr += ""+fd.PropertyName+"= ";
if( fd.DataType.Convert == "ToInt32")
{
updstr += "\"+formatcom.IntToStr(m."+fd.PropertyName +")+\",";
}
else if( fd.DataType.Convert == "ToDecimal")
{
updstr += "\"+formatcom.FloatToStr(m."+fd.PropertyName +")+\",";
}
else
updstr += "'\""+"+formatcom.ToString(m."+fd.PropertyName+")+\"',";
//updstr += ",";
iField++;
}
if( fd.IsPK == true)
bHasPrevPk = true;
}
if( updstr.EndsWith(",\r\n"))
updstr = updstr.Substring(0,updstr.Length-3);
if( updstr.EndsWith(","))
updstr = updstr.Substring(0,updstr.Length-1);
%><%=updstr%>where id="+formatcom.IntToStr(m.Id)
ok, err := orm.Update(sql)
if err != nil {
logger.Error(err.Error())
}
return ok
}
func (m *<%=tb.PropertyName%>) Delete( ) bool {
id:=m.Id
sql:="delete from " + orm.TableName("<%=tb.PropertyName%>") + " where id="+ formatcom.IntToStr(id)
ok, err := orm.Delete(sql)
if err != nil {
logger.Error(err.Error())
}
return ok
}
func (m *<%=tb.PropertyName%>) GetModel(uid int64) *<%=tb.PropertyName%> {
where := "id=" + formatcom.IntToStr(uid)
return m.GetModelByWhere(where )
}
func (m *<%=tb.PropertyName%>) GetModelByWhere(where string) *<%=tb.PropertyName%> {
<%=tb.PropertyName.ToLower()%>Arr, err := m.<%=tb.PropertyName%>Arr(where)
if err != nil {
logger.Error("GetModel where=", where)
return nil
}
for _, value := range <%=tb.PropertyName.ToLower()%>Arr {
model := m.MapToClass(value)
return model
}
return nil
}
func (m *<%=tb.PropertyName%>)<%=tb.PropertyName%>Arr(where string) ([]orm.Params, error) {
var sqlstr = "select a.* from " + orm.TableName("<%=tb.PropertyName%>") + " a "
if where != "" {
sqlstr += " where " + where
}
<%=tb.PropertyName.ToLower()%>list, err := orm.Query(sqlstr)
return <%=tb.PropertyName.ToLower()%>list, err
}
func (m *<%=tb.PropertyName%>)<%=tb.PropertyName%>List(where string) ([]*<%=tb.PropertyName%>, error) {
<%=tb.PropertyName.ToLower()%>List := make([]*<%=tb.PropertyName%>, 0, 1)
<%=tb.PropertyName.ToLower()%>Arr, err := m.<%=tb.PropertyName%>Arr(where)
if err != nil {
return <%=tb.PropertyName.ToLower()%>List, err
}
if <%=tb.PropertyName.ToLower()%>Arr == nil {
return <%=tb.PropertyName.ToLower()%>List, nil
}
for _, value := range <%=tb.PropertyName.ToLower()%>Arr {
<%=tb.PropertyName.ToLower()%> := m.MapToClass(value)
<%=tb.PropertyName.ToLower()%>List = append(<%=tb.PropertyName.ToLower()%>List, <%=tb.PropertyName.ToLower()%>)
}
return <%=tb.PropertyName.ToLower()%>List, nil
}
func (m *<%=tb.PropertyName%>)MapToClass(value orm.Params) *<%=tb.PropertyName%> {
<%=tb.PropertyName.ToLower()%> := new(<%=tb.PropertyName%> )
<%
foreach(Field fd in tb.Fields)
{
if( fd.DataType.Convert == "ToInt32")
{
Response.WriteLine("\t"+tb.PropertyName.ToLower()+"."+fd.PropertyName+" = formatcom.StrToInt(value[\""+fd.PropertyName.ToLower()+"\"])");
}
else if( fd.DataType.Convert == "ToDecimal")
{
Response.WriteLine("\t"+tb.PropertyName.ToLower()+"."+fd.PropertyName+" = formatcom.StrToFloat(value[\""+fd.PropertyName.ToLower()+"\"])");
}
else
Response.WriteLine("\t"+tb.PropertyName.ToLower()+"."+fd.PropertyName+" = formatcom.ToString(value[\""+fd.PropertyName.ToLower()+"\"])");
}
%>
return <%=tb.PropertyName.ToLower()%>
}
<%
CloseResponse();
}
%>