📄 fetchpath.cs
字号:
//This file is part of ORM.NET.
//
//ORM.NET is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//ORM.NET is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with ORM.NET; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Text;
using OrmLib;
using ORMBiz;
namespace ORM.NET
{
/// <summary>
/// Summary description for FetchPath.
/// </summary>
public class FetchPathGenerator
{
private TemplateParser tp = new TemplateParser();
public FetchPathGenerator(){}
public string Generate(Database db)
{
return tp.RestoreAfterFormatting( RenderPage(db) );
}
private string RenderPage(Database db)
{
string start = tp.LoadTemplate(@"Biz\FetchPath\class");
return string.Format(start, Shared.NameSpace, GetStaticProperties(db), GetTableClasses(db));
}
private string GetStaticProperties(Database db)
{
string properties = tp.LoadTemplate(@"Biz\FetchPath\properties");
StringBuilder sb = new StringBuilder();
foreach ( Table t in db.Tables )
{
if ( t.GenerateCodeFor.ToLower() == "true" )
sb.AppendFormat(properties,t.AccessorName);
}
return sb.ToString();
}
private string GetTableClasses(Database db)
{
string table = tp.LoadTemplate(@"Biz\FetchPath\table");
StringBuilder sb = new StringBuilder();
foreach ( Table t in db.Tables )
{
if ( t.GenerateCodeFor.ToLower() == "true" )
sb.AppendFormat(table,t.AccessorName, GetRelations(t), GetRelationListForChildren(t), GetRelationListForParents(t));
}
return sb.ToString();
}
private string GetRelationListForParents(Table t)
{
string table = tp.LoadTemplate(@"Biz\FetchPath\relationList");
StringBuilder sb = new StringBuilder();
foreach ( Relation r in t.ParentRelations )
{
if ( r.ParentTable.GenerateCodeFor.ToLower() == "true" )
{
string pkey = "";
foreach ( Column c in r.ParentTable.Columns )
{
if ( c.PrimaryKey.ToLower() == "true" )
{
pkey = c.Name;
break;
}
}
//string ormAlias = string.Format("p{0}c{1}k{2}",r.ParentTable.ID, r.ChildTable.ID, r.ChildTable.Columns.FindByName(r.ForeignKeyColumnName).ID);
string ormAlias = string.Format("{0}{1}",r.ParentAccessorName.Length > 4 ? r.ParentAccessorName.Substring(0,4) : r.ParentAccessorName, r.ID);
sb.AppendFormat(table,r.ParentTable.AccessorName, pkey, t.Name, r.ForeignKeyColumnName, "true", ormAlias);
}
}
return sb.ToString();
}
private string GetRelationListForChildren(Table t)
{
string table = tp.LoadTemplate(@"Biz\FetchPath\relationList");
string code = "";
string pkey = "";
foreach ( Column c in t.Columns )
{
if ( c.PrimaryKey.ToLower() == "true" )
{
pkey = c.Name;
break;
}
}
foreach ( Relation r in t.ChildRelations )
{
if ( r.ChildTable.GenerateCodeFor.ToLower() == "true" )
{
//string ormAlias = string.Format("p{0}c{1}k{2}",r.ParentTable.ID,r.ChildTable.ID, r.ChildTable.Columns.FindByName(r.ForeignKeyColumnName).ID);
string ormAlias = string.Format("{0}{1}",r.ChildAccessorName.Length > 4 ? r.ChildAccessorName.Substring(0,4) : r.ChildAccessorName, r.ID);
code += string.Format(table,t.Name, pkey, r.ChildTable.Name, r.ForeignKeyColumnName, "false", ormAlias);
}
}
return code;
}
private string GetRelations(Table t)
{
string relation = tp.LoadTemplate(@"Biz\FetchPath\relation");
string code = "";
string pkey = "";
foreach ( Column c in t.Columns )
{
if ( c.PrimaryKey.ToLower() == "true" )
{
pkey = c.Name;
break;
}
}
foreach ( Relation r in t.ChildRelations )
{
if ( r.ChildTable.GenerateCodeFor.ToLower() == "true" )
{
//string ormAlias = string.Format("p{0}c{1}k{2}",r.ParentTable.ID,r.ChildTable.ID, r.ChildTable.Columns.FindByName(r.ForeignKeyColumnName).ID);
string ormAlias = string.Format("{0}{1}",r.ChildAccessorName.Length > 4 ? r.ChildAccessorName.Substring(0,4) : r.ChildAccessorName, r.ID);
code += string.Format(relation,t.Name, pkey, r.ChildTable.AccessorName, r.ForeignKeyColumnName, r.ChildAccessorName, r.ChildTable.Name, "false", ormAlias);
}
}
foreach ( Relation r in t.ParentRelations )
{
if ( r.ParentTable.GenerateCodeFor.ToLower() == "true" )
{
foreach ( Column c in r.ParentTable.Columns )
{
if ( c.PrimaryKey.ToLower() == "true" )
pkey = c.Name;
}
//string ormAlias = string.Format("p{0}c{1}k{2}",r.ParentTable.ID,r.ChildTable.ID, r.ChildTable.Columns.FindByName(r.ForeignKeyColumnName).ID);
string ormAlias = string.Format("{0}{1}",r.ParentAccessorName.Length > 4 ? r.ParentAccessorName.Substring(0,4) : r.ParentAccessorName, r.ID);
code += string.Format(relation,r.ParentTable.Name, pkey, r.ParentTable.AccessorName, r.ForeignKeyColumnName, r.ParentAccessorName, r.ChildTable.Name, "true", ormAlias);
}
}
return code;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -