📄 storedprocedures.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.Collections;
using System.Text;
using System.Text.RegularExpressions;
using ORMBiz;
namespace ORM.NET
{
/// <summary>
/// Summary description for StoredProcedures.
/// </summary>
public class StoredProceduresGenerator
{
private TemplateParser parser = new TemplateParser();
public StoredProceduresGenerator()
{
}
private Hashtable UniqueTableNames = new Hashtable();
private Regex TableNameRegex = new Regex("[a-z0-9_]+", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled );
private string GetValidName(string tableName)
{
if ( UniqueTableNames.ContainsKey(tableName) ) return (string)UniqueTableNames[tableName];
string output = "";
int incriment = 1;
foreach ( Match match in TableNameRegex.Matches(tableName) )
{
output += match.Value;
}
// see if the variable is a keyword
ArrayList a = new ArrayList(1);
a.Add(new SchemaValidation.Piece("test",output,null,null,null));
a = SchemaValidation.ValidateValidObjectDeclaration(a);
if ( a != null && a.Count > 0 )
output = output + "Param";
if ( output.Length == 0 )
output = "param";
if ( UniqueTableNames.ContainsValue(output) )
{
while ( UniqueTableNames.ContainsValue(output + incriment.ToString()) )
{
incriment++;
}
UniqueTableNames.Add(tableName,output + incriment.ToString());
return output + incriment.ToString();
}
UniqueTableNames.Add(tableName,output);
return output;
}
public string Generate(Database db)
{
// temporarily disable proc generation until we handle reserved word and invalid character checks on procs
string classString = parser.LoadTemplate(@"Biz\StoredProcedures\class");
StringBuilder sb = new StringBuilder();
//string code = "";
if ( db.Procedures.Count > 0 )
{
foreach(Procedure p in db.Procedures)
{
sb.Append( GetFormattedProcedure(p) );
//code += GetFormattedProcedure(p);
}
return parser.RestoreAfterFormatting( string.Format(classString, Shared.NameSpace, sb.ToString() ) );
}
return "";
}
public string GetFormattedProcedure(Procedure p)
{
string procedureString = parser.LoadTemplate(@"Biz\StoredProcedures\procedure");
return string.Format( procedureString, p.Name, GetFormattedParamList(p), GetFormattedParam(p), GetValidName(p.Name), GetFormattedParamOutput(p));
}
public string GetFormattedParamList(Procedure p)
{
string paramListTemplate = parser.LoadTemplate(@"Biz\StoredProcedures\paramlist");
string paramList = "";
foreach(Param param in p.Params)
{
string refstr = "";
if (param.IsOutputParam.ToLower() == "1")
{
if ( Shared.GenerateVbCode )
refstr = "ByRef ";
else
refstr = "ref ";
}
paramList += string.Format(paramListTemplate, refstr, param.DataType.NetType, GetValidName(param.Name.Substring(1)));
}
return paramList.TrimEnd(',','\n','\r',' ', '_');
}
public string GetFormattedParamOutput(Procedure p)
{
string paramOutputTemplate = parser.LoadTemplate(@"Biz\StoredProcedures\paramOutput");
string paramOutputString = "";
foreach(Param param in p.Params)
{
if (param.IsOutputParam == "1")
{
paramOutputString += string.Format(paramOutputTemplate, GetValidName(param.Name.Substring(1)), param.DataType.NetType);
}
}
return paramOutputString;
}
public string GetFormattedParam(Procedure p)
{
string paramTemplate = parser.LoadTemplate(@"Biz\StoredProcedures\param");
string paramString = "";
foreach(Param param in p.Params)
{
string direction = "Input";
if (param.IsOutputParam.ToLower() == "1") direction = "Output";
paramString += string.Format( paramTemplate, param.Name.Substring(1), param.DataType.SqlType, GetValidName(param.Name.Substring(1)), direction, param.Length);
}
return paramString;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -