📄 datamanager.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 System.Collections;
using OrmLib;
using ORMBiz;
namespace ORM.NET
{
/// <summary>
/// Summary description for DataManagerGenerator.
/// </summary>
public class DataManagerGenerator
{
private TemplateParser tp = new TemplateParser();
private StringBuilder sb;
public DataManagerGenerator(){}
public string Generate(Database db)
{
// this is calculated from multiple databases:
// max was 5000 per table, but almost all were 3900-3990 per table
sb = new StringBuilder(db.Tables.Count * 4096);
string output = RenderPage(db);
return tp.RestoreAfterFormatting( output );
}
private string RenderPage(Database db)
{
string classString = tp.LoadTemplate(@"Biz\DataManager\class");
return string.Format( classString,Shared.NameSpace,GetMethods(db) );
}
private string GetMethods(Database db)
{
foreach ( Table t in db.Tables )
{
if ( t.GenerateCodeFor.ToLower() == "true" && t.IsLookupTable.ToLower() == "false" && t.HasPrimaryKey )
{
sb.Append(GetNewObjectMethod(t));
sb.Append(GetGetObjectMethod(t));
sb.Append(GetGetObjectCollectionMethod(t));
}
}
return sb.ToString();
}
private string GetNewObjectMethod(Table t)
{
ArrayList Params = new ArrayList();
ArrayList Statements = new ArrayList();
ArrayList ParentStatements = new ArrayList();
foreach ( Column c in t.Columns )
{
if ( c.RequiredForInstantiation.ToLower() == "true" )
{
string formatter = Shared.GenerateVbCode ? "{1} As {0}" : "{0} {1}";
if ( c.PrimaryKey.ToLower() == "true" )
Params.Add(string.Format(formatter, c.DataType.NetType, c.AccessorName ));
else
Params.Add(string.Format(formatter, c.DataType.NetSqlType, c.AccessorName ));
Statements.Add( "\t\t\t_" + t.AccessorName + "." + c.AccessorName + " = " + c.AccessorName + (Shared.GenerateVbCode ? "" : ";") );
}
}
// name the lookups different to the parent tables.
foreach ( Relation r in t.ParentRelations )
{
if ( r.ParentTable.GenerateCodeFor.ToLower() == "true" )
{
if ( r.IsMandatory.ToLower() == "true" )
{
string formatter = Shared.GenerateVbCode ? "ByRef {1} As {0}" : "{0} {1}";
Params.Add(string.Format(formatter,
Shared.NameSpace +
"." +
r.ParentTable.AccessorName,
((r.ParentTable.IsLookupTable.ToLower() == "true") ? "lookup" : "parent") +
r.ParentAccessorName
)
);
Statements.Add( "\t\t\t_" +
t.AccessorName +
"." + r.ParentAccessorName +
((r.ParentTable.IsLookupTable.ToLower() == "true") ? " = lookup" : " = parent") +
r.ParentAccessorName +
(Shared.GenerateVbCode ? "" : ";") );
}
}
}
string lineDelimeter = Shared.GenerateVbCode ? ", _\n\t\t\t" : ",\n\t\t\t";
string parameters = string.Join(lineDelimeter, (string[])Params.ToArray(typeof(string)));
string statements = string.Join("\n", (string[])Statements.ToArray(typeof(string)));
string newObject = tp.LoadTemplate(@"Biz\DataManager\new");
return string.Format(newObject, t.AccessorName,t.Name, Shared.NameSpace + "." + t.AccessorName, parameters, statements);
}
private string GetGetObjectMethod(Table t)
{
string pkeyName = "";
string pkeyDataType = "";
string pkeyAccessorName = "";
foreach(Column c in t.Columns)
{
if (c.PrimaryKey.ToLower() == "true")
{
pkeyDataType = c.DataType.NetType;
pkeyName = c.Name;
pkeyAccessorName = c.AccessorName;
}
}
string getObject = tp.LoadTemplate(@"Biz\DataManager\get");
return string.Format(getObject, t.AccessorName,t.Name, Shared.NameSpace + "." + t.AccessorName, pkeyDataType, pkeyName, pkeyAccessorName);
}
private string GetGetObjectCollectionMethod(Table t)
{
string getObjectCollection = tp.LoadTemplate(@"Biz\DataManager\getCollection");
return string.Format(getObjectCollection, t.AccessorName,t.Name, Shared.NameSpace + "." + t.AccessorName);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -