📄 dataset.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 DataSet.
/// </summary>
public class DataSetGenerator
{
private TemplateParser tp = new TemplateParser();
public DataSetGenerator(){}
public string Generate(Database db)
{
return tp.RestoreAfterFormatting( RenderPage(db) );
}
private string RenderPage(Database db)
{
string classString = tp.LoadTemplate(@"Biz\DataSet\class");
return string.Format(classString,Shared.NameSpace, GetInitDbMethod(db));
}
private string GetInitDbMethod(Database db)
{
string InitDbMethod = tp.LoadTemplate(@"Biz\DataSet\InitDbMethod");
return string.Format(InitDbMethod, GetTableStatements(db));
}
private string GetTableStatements(Database db)
{
string table = tp.LoadTemplate(@"Biz\DataSet\table");
StringBuilder sb = new StringBuilder();
foreach ( Table t in db.Tables )
{
if ( t.GenerateCodeFor.ToLower() == "true" )
{
sb.AppendFormat(table,t.AccessorName, t.Name);
sb.Append( GetColumnStatements(t) );
}
}
foreach ( Table t in db.Tables )
{
if ( t.GenerateCodeFor.ToLower() == "true" )
sb.Append( GetRelationStatements(t) );
}
return sb.ToString();
}
private string GetColumnStatements(Table t)
{
string column = tp.LoadTemplate(@"Biz\DataSet\column");
string columnProperties = tp.LoadTemplate(@"Biz\DataSet\columnProperties");
string stringProperty = tp.LoadTemplate(@"Biz\DataSet\stringProperty");
string pkey = tp.LoadTemplate(@"Biz\DataSet\pkey");
string pkeyIdentity = tp.LoadTemplate(@"Biz\DataSet\pkeyidentity");
string pkeyColumn = tp.LoadTemplate(@"Biz\DataSet\pkeyColumn");
StringBuilder sb = new StringBuilder();
foreach ( Column c in t.Columns )
{
sb.AppendFormat(column,c.Table.AccessorName,c.Name,c.DataType.NetType);
sb.AppendFormat(columnProperties, c.Table.AccessorName, c.Name, c.Nullable, c.IsReadOnly);
if ( c.DataType.NetType == "System.String" && c.Length.Value > 0 )
sb.AppendFormat(stringProperty, c.Table.AccessorName, c.Name, c.Length);
}
foreach( Relation r in t.ParentRelations)
{
if ( r.ParentTable.GenerateCodeFor.ToLower() == "true" && t.Columns.FindByName(r.ForeignKeyColumnName) == null )
{
foreach(RelationColumn rc in r.RelationColumns)
{
string dataType = "";
dataType = rc.PrimaryColumn.DataType.NetType;
sb.AppendFormat(column, t.AccessorName, rc.ChildColumnName, dataType);
}
}
}
string primaryKeys = "";
foreach(Column c in t.Columns.FilterByPrimaryKey("true"))
{
primaryKeys += string.Format(pkeyColumn,c.Table.AccessorName,c.Name);
primaryKeys += ", ";
}
primaryKeys = primaryKeys.TrimEnd(',', ' ');
sb.AppendFormat(pkey, t.AccessorName, primaryKeys);
foreach(Column c in t.Columns)
{
if (c.IsIdentity.ToLower() == "true" && c.PrimaryKey.ToLower() == "true")
{
sb.AppendFormat(pkeyIdentity, c.Table.AccessorName);
}
}
return sb.ToString();
}
private string GetRelationStatements(Table t)
{
string relation = tp.LoadTemplate(@"Biz\DataSet\relation");
string relationColumn = tp.LoadTemplate(@"Biz\DataSet\relationColumn");
string rule = tp.LoadTemplate(@"Biz\DataSet\rule");
StringBuilder sb = new StringBuilder();
string pkey = "";
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;
}
// remove this later
if ( r.IsMandatory == null ) r.IsMandatory = "true";
if (r.ChildTable.IsLookupTable.ToLower() == r.ParentTable.IsLookupTable.ToLower() ||
r.ChildTable.IsLookupTable.ToLower() == "true")
{
string parentColumns = "";
string childColumns = "";
foreach( RelationColumn rc in r.RelationColumns)
{
parentColumns += string.Format( relationColumn, r.ParentTable.Name, rc.ParentColumnName) + ", ";
childColumns += string.Format( relationColumn, r.ChildTable.Name, rc.ChildColumnName) + ", " ;
}
string relationName = r.RelationColumns[0].ChildColumnName + r.ChildTable.Name;
parentColumns = parentColumns.TrimEnd(',', ' ');
childColumns = childColumns.TrimEnd(',', ' ');
sb.AppendFormat(relation, relationName, parentColumns, childColumns, "true");
string deleteRule = "None";
string updateRule = "None";
if ( r.CascadeDelete.ToLower() == "true" )
deleteRule = "Cascade";
if ( r.CascadeUpdate.ToLower() == "true" || r.RelationColumns[0].PrimaryColumn.IsIdentity.ToLower() == "true" )
updateRule = "Cascade";
sb.AppendFormat( rule, r.ChildTable.Name, r.ForeignKeyColumnName, deleteRule, updateRule );
}
}
}
return sb.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -