⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 joinpath.cs

📁 客户关系管理系统ASP.NET+VB.NET编程完整程序!
💻 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 JoinPath.
	/// </summary>
	public class JoinPathGenerator
	{
		private TemplateParser tp = new TemplateParser();

		public JoinPathGenerator(){}

		public string Generate(Database db)
		{
			return tp.RestoreAfterFormatting( RenderPage(db) );
		}

		private string RenderPage(Database db)
		{
			string start = tp.LoadTemplate(@"Biz\JoinPath\class");
			
			return string.Format(start, Shared.NameSpace, GetStaticProperties(db), GetTableClasses(db), GetColumnClasses(db));
		}

		private string GetStaticProperties(Database db)
		{
			string properties = tp.LoadTemplate(@"Biz\JoinPath\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\JoinPath\table");
			StringBuilder sb = new StringBuilder();

			foreach ( Table t in db.Tables )
			{
				if ( t.GenerateCodeFor.ToLower() == "true" )
					sb.AppendFormat(table,t.AccessorName, GetRelations(t));
			}
			return sb.ToString();
		}

		private string GetRelations(Table t)
		{
			string relation = tp.LoadTemplate(@"Biz\JoinPath\relation");
			StringBuilder sb = new StringBuilder();

			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);
					sb.AppendFormat(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);
					sb.AppendFormat(relation,r.ParentTable.Name, pkey, r.ParentTable.AccessorName, r.ForeignKeyColumnName, r.ParentAccessorName, r.ChildTable.Name, "true", ormAlias);
				}
			}
			return sb.ToString();
		}

		private string GetColumnClasses(Database db)
		{
			string tableColumns = tp.LoadTemplate(@"Biz\JoinPath\tableColumns");
			StringBuilder sb = new StringBuilder();

			foreach ( Table t in db.Tables )
			{
				if ( t.GenerateCodeFor.ToLower() == "true" )
					sb.AppendFormat(tableColumns,t.AccessorName, GetColumns(t));
			}
			return sb.ToString();
		}

		private string GetColumns(Table t)
		{
			string column = tp.LoadTemplate(@"Biz\JoinPath\column");
			StringBuilder sb = new StringBuilder();

			foreach ( Column c in t.Columns )
			{
				sb.AppendFormat(column, c.Table.Name, c.Name, c.AccessorName );
			}
			return sb.ToString();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -