private string GetFunctionDefinition(StoreProcedure storeProcedure) { if (storeProcedure == null) { throw new ArgumentNullException("StoreProcedure"); } int iTabCnt = 2; StringBuilder sb = new StringBuilder(); sb.Append(GetFunSummary(storeProcedure)); sb.Append(CodeHelper.AddTab(2) + functionAuthority); switch (storeProcedure.ProcExecuteReturnType) { case StoreProcedure.ExecuteReturnType.NonQuery: sb.Append("int "); break; case StoreProcedure.ExecuteReturnType.DataReader: sb.Append("SqlDataReader "); break; case StoreProcedure.ExecuteReturnType.Scalar: sb.Append("object "); break; case StoreProcedure.ExecuteReturnType.Dataset: sb.Append("DataSet "); break; case StoreProcedure.ExecuteReturnType.DataTable: sb.Append("DataTable "); break; } #region 添加参数 sb.Append(storeProcedure.Name.Replace(" ", "") + "("); foreach (SPField var in storeProcedure.Parameters) { sb.Append(", "); if (var.PARAMETER_MODE.Trim().ToUpper() == "IN") { sb.Append(CodeHelper.ConvertDBTypeToDoNetType(var.DATA_TYPE) + " " + CodeHelper.GetPureParmName(var.PARAMETER_NAME)); } else { sb.Append("ref " + CodeHelper.ConvertDBTypeToDoNetType(var.DATA_TYPE) + " " + CodeHelper.GetPureParmName(var.PARAMETER_NAME)); } } if (storeProcedure.NeedReturnValue) { sb.Append(", ref int RETURN_VALUE"); } switch (this.connectType) { case ConnectType.ConnectionString: sb.Append(", string connectionString"); break; case ConnectType.SqlConnection: sb.Append(", SqlConnection connection"); break; } if (needTransaction && this.connectType == ConnectType.SqlConnection) { sb.Append(", SqlTransaction transaction"); } sb.Replace("(, ", "("); sb.Append(")"); #endregion sb.Append("\r\n" + CodeHelper.AddTab(2) + "{\r\n"); iTabCnt = 3; sb.AppendLine(CodeHelper.AddTab(iTabCnt) + "@@CreateCommand\r\n"); sb.AppendLine(CodeHelper.AddTab(iTabCnt) + "@@CommandExecute"); sb.AppendLine(CodeHelper.AddTab(2) + "}\r\n"); return(sb.ToString()); }
private string GetSetByRefVal(StoreProcedure storeProcedure, int tabCnt) { if (storeProcedure == null) { throw new ArgumentNullException("StoreProcedure"); } int iTabCnt = tabCnt; string strTabs = CodeHelper.AddTab(iTabCnt); StringBuilder sb = new StringBuilder(); foreach (SPField var in storeProcedure.Parameters) { if (var.PARAMETER_MODE.Trim().ToUpper() != "IN") { sb.AppendLine(strTabs + string.Format("{0} = ({1})command.Parameters[\"@{0}\"].Value;", CodeHelper.GetPureParmName(var.PARAMETER_NAME), CodeHelper.ConvertDBTypeToDoNetType(var.DATA_TYPE))); } } if (storeProcedure.NeedReturnValue) { sb.AppendLine(strTabs + "RETURN_VALUE = (int)command.Parameters[\"@RETURN_VALUE\"].Value;"); } return(sb.ToString()); }
/// <summary> /// 生成IDAL接口层代码 /// </summary> /// <param name="l"></param> /// <returns></returns> public static string GetIDALCode(Model.Table table, Model.CodeStyle style) { List <Model.Field> l = table.Fields; bool HasIdentifierRow; Model.Field IdentifierRow = CodeHelper.GetKeyField(table, out HasIdentifierRow); StringBuilder code = new StringBuilder(CommonCode.GetCSharpCopyrightCode()); code.AppendLine("using System;"); code.AppendLine("using System.Data;"); code.AppendLine("using System.Collections;"); code.AppendLine("using System.Collections.Generic;"); code.AppendLine(""); code.AppendLine("namespace " + style.BeforeNamespaceDot + "IDAL" + style.DotAfterNamespace); code.AppendLine("{"); code.AppendLine(" /// <summary>"); code.AppendLine(" /// 接口层 I" + table.Name + ""); code.AppendLine(" /// </summary>"); code.AppendLine(" public interface I" + table.Name + ""); code.AppendLine(" {"); code.AppendLine(" #region 成员方法"); code.AppendLine(" /// <summary>"); code.AppendLine(" /// 增加一条数据"); code.AppendLine(" /// </summary>"); code.AppendLine(" int Add(" + CodeHelper.GetModelClass(table, style) + " model);"); code.AppendLine(""); code.AppendLine(" /// <summary>"); code.AppendLine(" /// 更新一条数据"); code.AppendLine(" /// </summary>"); code.AppendLine(" bool Update(" + CodeHelper.GetModelClass(table, style) + " model);"); code.AppendLine(""); code.AppendLine(" /// <summary>"); code.AppendLine(" /// 删除一条数据"); code.AppendLine(" /// </summary>"); code.AppendLine(" bool Delete(" + CodeUtility.TypeConverter.DataTypeToCSharpTypeString(IdentifierRow.FieldType) + " " + IdentifierRow.FieldName + ");"); code.AppendLine(""); if (IdentifierRow.FieldType == Model.DataType.intType || IdentifierRow.FieldType == Model.DataType.bigintType || IdentifierRow.FieldType == Model.DataType.smallintType || IdentifierRow.FieldType == Model.DataType.tinyintType) { code.AppendLine(" /// <summary>"); code.AppendLine(" /// 得到最大ID"); code.AppendLine(" ///</summary>"); code.AppendLine(" int GetMaxId();"); code.AppendLine(""); } code.AppendLine(" /// <summary>"); code.AppendLine(" /// 是否存在该记录"); code.AppendLine(" /// </summary>"); code.AppendLine(" bool Exists(" + CodeUtility.TypeConverter.DataTypeToCSharpTypeString(IdentifierRow.FieldType) + " " + IdentifierRow.FieldName + ");"); code.AppendLine(""); code.AppendLine(" /// <summary>"); code.AppendLine(" /// 得到一个对象实体"); code.AppendLine(" /// </summary>"); code.AppendLine(" " + CodeHelper.GetModelClass(table, style) + " GetModel(" + CodeUtility.TypeConverter.DataTypeToCSharpTypeString(IdentifierRow.FieldType) + " " + IdentifierRow.FieldName + ");"); code.AppendLine(""); code.AppendLine(" /// <summary>"); code.AppendLine(" /// 获取泛型数据列表"); code.AppendLine(" /// </summary>"); code.AppendLine(" List<" + CodeHelper.GetModelClass(table, style) + "> GetList();"); code.AppendLine(""); code.AppendLine(" /// <summary>"); code.AppendLine(" /// 分页获取泛型数据列表"); code.AppendLine(" /// </summary>"); code.AppendLine(" List<" + CodeHelper.GetModelClass(table, style) + "> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int counts);"); code.AppendLine(" #endregion"); code.AppendLine(" }"); code.AppendLine("}"); return(code.ToString()); }
private string GetFunSummary(StoreProcedure storeProcedure) { if (storeProcedure == null) { throw new ArgumentNullException("StoreProcedure"); } string strTabs = " /// "; StringBuilder sb = new StringBuilder(); sb.AppendLine(strTabs + "<summary>"); sb.AppendLine(strTabs + storeProcedure.Name); sb.AppendLine(strTabs + "</summary>"); foreach (SPField var in storeProcedure.Parameters) { sb.AppendLine(strTabs + string.Format("<param name=\"{0}\">{0}</param>", CodeHelper.GetPureParmName(var.PARAMETER_NAME))); } if (storeProcedure.NeedReturnValue) { sb.AppendLine(strTabs + string.Format("<param name=\"{0}\">{0}</param>", "RETURN_VALUE")); } switch (this.connectType) { case ConnectType.ConnectionString: sb.AppendLine(strTabs + string.Format("<param name=\"{0}\">{0}</param>", "connectionString")); break; case ConnectType.SqlConnection: sb.AppendLine(strTabs + string.Format("<param name=\"{0}\">{0}</param>", "connection")); break; } if (needTransaction && this.connectType == ConnectType.SqlConnection) { sb.AppendLine(strTabs + string.Format("<param name=\"{0}\">{0}</param>", "transaction")); } sb.AppendLine(strTabs + "<returns></returns>"); return(sb.ToString()); }
/// <summary> /// 生成IDAL接口层代码 /// </summary> /// <param name="l"></param> /// <returns></returns> public static string GetIDALCode(Model.Table table, Model.CodeStyle style) { StringBuilder code = new StringBuilder(CommonCode.GetCSharpCopyrightCode()); AppendFormatLine(code, 0, "using System;"); AppendFormatLine(code, 0, "using System.Data;"); AppendFormatLine(code, 0, "using System.Collections;"); AppendFormatLine(code, 0, "using System.Collections.Generic;"); code.AppendLine(); AppendFormatLine(code, 0, "namespace {0}", style.IDALNameSpace); AppendFormatLine(code, 0, "{"); AppendFormatLine(code, 1, "/// <summary>"); AppendFormatLine(code, 1, "/// 接口层 I{0}", table.Name); AppendFormatLine(code, 1, "/// </summary>"); AppendFormatLine(code, 1, "public interface I{0}", table.Name); AppendFormatLine(code, 1, "{"); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 增加一条数据"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "int Add({0}.{1} model);", style.ModelNameSpace, table.Name); code.AppendLine(); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 更新一条数据"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "int Update({0}.{1} model);", style.ModelNameSpace, table.Name); code.AppendLine(); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 删除一条数据"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "int Delete({0});", CodeHelper.GetArgumentsOfFunction(table)); code.AppendLine(); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 是否存在该记录"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "bool Exists({0});", CodeHelper.GetArgumentsOfFunction(table)); code.AppendLine(); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 得到一个对象实体"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "{0}.{1} GetModel({2});", style.ModelNameSpace, table.Name, GetArgumentsOfFunction(table)); code.AppendLine(); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 获取泛型数据列表"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "List<{0}.{1}> GetList();", style.ModelNameSpace, table.Name); code.AppendLine(); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 取得数据行数"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "int GetCount(string condition);"); code.AppendLine(); AppendFormatLine(code, 2, "/// <summary>"); AppendFormatLine(code, 2, "/// 分页获取泛型数据列表"); AppendFormatLine(code, 2, "/// </summary>"); AppendFormatLine(code, 2, "List<{0}.{1}> GetPageList(int pageSize, int pageIndex, string fldSort, bool fldDir, string condition);", style.ModelNameSpace, table.Name); AppendFormatLine(code, 1, "}"); AppendFormatLine(code, 0, "}"); return(code.ToString()); }