Exemplo n.º 1
0
        public static string DaoDelete(CodeMakerGeneratCodeOut inModel)
        {
            var className = inModel.CodeMakerGeneratCodeIn.ClassName;
            var tableName = inModel.CodeMakerGeneratCodeIn.Table;
            var codeStr   = new StringBuilder();

            inModel.FieldDescriptions.ForEach(f =>
            {
                var field          = new StringBuilder();
                var fieldParameter = new StringBuilder();

                fieldParameter.AppendLine();

                codeStr.AppendLine(string.Format(@"

        /// <summary>
        /// 通过{3}删除实体
        /// </summary>
        /// <param name=""connectionString"">连接字符串</param>
        /// <param name=""wherePart"">删除属性</param>
        /// <returns>删除成功与否</returns>
        public static bool DeleteBy{4}(string connectionString, {0} {1}) {{
            var sql = ""DELETE FROM [{2}] WHERE [{3}] = @{3}"";
		
            var parameters = new List<SqlParameter>();

    {5}		
            var i = JlDatabase.ExecuteNonQuery(connectionString, sql, parameters.ToArray());
		
            return i > 0;
        }}", JlDbTypeMap.Map4CSharp(f.DbType),
                                                 JlString.ToLowerFirst(f.Name),
                                                 tableName,
                                                 f.Name,
                                                 JlString.ToUpperFirst(f.Name),
                                                 string.Format("        parameters.Add(new SqlParameter() {{ ParameterName = \"{0}\", Value = {1} }});", f.Name, JlString.ToLowerFirst(f.Name))));
            });
            return(@"

        #region 删除

" + codeStr.ToString() + @"


        #endregion");
        }
Exemplo n.º 2
0
        public string RefEntity(CodeMakerGeneratCodeOut inModel)
        {
            var result = new StringBuilder();

            result.AppendLine(string.Format(@"using System;

namespace {0}
{{
    public class {1} {{", string.Format(inModel.CodeMakerGeneratCodeIn.Package, "Entity"), inModel.CodeMakerGeneratCodeIn.ClassName));

            inModel.FieldDescriptions.ForEach(f =>
            {
                result.AppendLine(@"
        /// <summary>
        /// " + (string.IsNullOrWhiteSpace(f.Description) ? f.Name : f.Description.Replace("\n", "  ")) + @"
        /// </summary>
        public " + JlDbTypeMap.Map4CSharp(f.DbType, f.IsNullable) + " " + f.Name + " { get; set; }");
            });
            result.AppendLine(@"
    }
}");

            return(result.ToString());
        }
Exemplo n.º 3
0
        public static string DaoGetPageList(CodeMakerGeneratCodeOut inModel)
        {
            var className = inModel.CodeMakerGeneratCodeIn.ClassName;
            var tableName = inModel.CodeMakerGeneratCodeIn.Table;
            var codeStr   = new StringBuilder();

            var field         = new StringBuilder();
            var fieldSetModel = new StringBuilder();

            inModel.FieldDescriptions.ForEach(f =>
            {
                field.AppendLine("                [" + f.Name + "],");
                if (JlDbTypeMap.Map4CSharp(f.DbType) == "string")
                {
                    fieldSetModel.AppendLine(string.Format("                    {0} = row[\"{0}\"].ToString(),", f.Name, JlString.ToLowerFirst(className)));
                }
                else
                {
                    fieldSetModel.AppendLine(string.Format("                    {0} = JlConvert.TryTo{1}(row[\"{0}\"]),", f.Name, JlString.ToUpperFirst(JlDbTypeMap.Map4CSharp(f.DbType))));
                }
            });

            Func <string, string, string> getSelectSql = (data, where) =>
            {
                return(string.Format(@"
            @""WITH Virtual_T AS(
                SELECT
{0}                
                ROW_NUMBER() OVER ({{0}}) AS [RowNumber]
                FROM [{1}] WITH (NOLOCK){{1}})
            SELECT * FROM Virtual_T
            WHERE @PageSize * (@CurrentPage - 1) < RowNumber AND RowNumber <= @PageSize * @CurrentPage""",
                                     field.ToString().Substring(0, field.Length - 1),
                                     tableName));
            };

            var fieldDataAccess = string.Format(@"
            var list = new List<En.{0}>();

            var dataTable = new DataTable();
            JlDatabase.Fill(connectionString, sql, dataTable, parameters.ToArray());

            if (dataTable.Rows.Count > 0)
            {{
                list = dataTable.AsEnumerable().Select(row => new En.{0}()
                {{
{1}
                }}).ToList();
            }}
            return list;", className, fieldSetModel.ToString().Substring(0, fieldSetModel.Length - 3));

            var fieldDataAccess_Count = string.Format(@"
            var list = new List<En.{0}>();

            var dataTable = new DataTable();
            JlDatabase.Fill(connectionString, sql, dataTable, parameters.ToArray());

            //记录总数计算
            var sqlCount = string.Format(""SELECT COUNT(*) CNT FROM [{2}] {{0}} "", string.IsNullOrEmpty(sqlWhere) ? """"
                : "" WHERE("" + sqlWhere.Remove(sqlWhere.Length - 4) + "")"");
            totalCount = SlConvert.TryToInt32(JlDatabase.ExecuteScalar(connectionString, sqlCount));

            if (dataTable.Rows.Count > 0)
            {{
                list = dataTable.AsEnumerable().Select(row => new En.{0}()
                {{
{1}
                }}).ToList();
            }}
            return list;", className, fieldSetModel.ToString().Substring(0, fieldSetModel.Length - 3), tableName);

            codeStr.AppendLine(string.Format(@"

        #region 查询结果集


        /// <summary>
        /// 查询所有实体列表(不存在时,返回null)
        /// </summary>
        /// <param name=""connectionString"">连接字符串</param>
        /// <param name=""pageSize"">每页行数</param>
        /// <param name=""currentPage"">当前页码</param>
        /// <param name=""sqlWhere"">判断条件语句</param>
        /// <param name=""sqlOrder"">排序语句</param>
        /// <returns>查询结果集</returns>
        public static List<En.{1}> GetPageListByConditionAndOrder(string connectionString, int pageSize, int currentPage, 
            string sqlWhere, string sqlOrder) {{
            string sql = {0};

            // 条件查询部分
            sql = string.Format(sql, sqlOrder, string.IsNullOrEmpty(sqlWhere) ? """" : "" WHERE ("" + sqlWhere.Substring(0, sqlWhere.Length - 4) + "")"");
            var parameters = new List<SqlParameter>();

            parameters.Add(new SqlParameter() {{ ParameterName = ""PageSize"", Value = pageSize }});
            parameters.Add(new SqlParameter() {{ ParameterName = ""CurrentPage"", Value = currentPage }});
{2}
        }}", getSelectSql(field.ToString(), null), className, fieldDataAccess));

            codeStr.AppendLine(string.Format(@"

        /// <summary>
        /// 查询所有实体列表(不存在时,返回null)
        /// </summary>
        /// <param name=""connectionString"">连接字符串</param>
        /// <param name=""pageSize"">每页行数</param>
        /// <param name=""currentPage"">当前页码</param>
        /// <param name=""sqlWhere"">判断条件语句</param>
        /// <param name=""sqlOrder"">排序语句</param>
        /// <returns>查询结果集</returns>
        public static List<En.{1}> GetPageListByConditionAndOrder(string connectionString, int pageSize, int currentPage, 
            string sqlWhere, string sqlOrder, out int totalCount) {{
            string sql = {0};

            // 条件查询部分
            sql = string.Format(sql, sqlOrder, string.IsNullOrEmpty(sqlWhere) ? """" : "" WHERE ("" + sqlWhere.Substring(0, sqlWhere.Length - 4) + "")"");
            var parameters = new List<SqlParameter>();

            parameters.Add(new SqlParameter() {{ ParameterName = ""PageSize"", Value = pageSize }});
            parameters.Add(new SqlParameter() {{ ParameterName = ""CurrentPage"", Value = currentPage }});
{2}
        }}

        #endregion
", getSelectSql(field.ToString(), null), className, fieldDataAccess_Count));

            return(codeStr.ToString());
        }
Exemplo n.º 4
0
        public static string DaoGetList(CodeMakerGeneratCodeOut inModel)
        {
            var className = inModel.CodeMakerGeneratCodeIn.ClassName;
            var tableName = inModel.CodeMakerGeneratCodeIn.Table;
            var codeStr   = new StringBuilder();

            var field         = new StringBuilder();
            var fieldSetModel = new StringBuilder();

            inModel.FieldDescriptions.ForEach(f =>
            {
                field.AppendLine("            [" + f.Name + "],");
                if (JlDbTypeMap.Map4CSharp(f.DbType) == "string")
                {
                    fieldSetModel.AppendLine(string.Format("                    {0} = row[\"{0}\"].ToString(),", f.Name, JlString.ToLowerFirst(className)));
                }
                else
                {
                    fieldSetModel.AppendLine(string.Format("                    {0} = JlConvert.TryTo{1}(row[\"{0}\"]),", f.Name, JlString.ToUpperFirst(JlDbTypeMap.Map4CSharp(f.DbType))));
                }
            });

            Func <string, string, string> getSelectSql = (data, where) =>
            {
                return(string.Format(@"@""SELECT
{0}
            FROM [{1}] WITH (NOLOCK){2}""",
                                     field.ToString().Substring(0, field.Length - 3),
                                     tableName,
                                     string.IsNullOrEmpty(where) ? "" : " " + where));
            };

            var fieldDataAccess = string.Format(@"
            var list = new List<En.{0}>();

            var dataTable = new DataTable();
            JlDatabase.Fill(connectionString, sql, dataTable);

            if (dataTable.Rows.Count > 0)
            {{
                list = dataTable.AsEnumerable().Select(row => new En.{0}()
                {{
{1}
                }}).ToList();
            }}
            return list;", className, fieldSetModel.ToString().Substring(0, fieldSetModel.Length - 2));

            codeStr.AppendLine(string.Format(@"
        /// <summary>
        /// 查询所有实体列表(不存在时,返回null)
        /// </summary>
        /// <param name=""connectionString"">连接字符串</param>
        /// <returns>查询结果集</returns>
        public static List<En.{1}> GetAll(string connectionString) {{
            string sql = {0};
{2}
        }}", getSelectSql(field.ToString(), null), className, fieldDataAccess));

            inModel.FieldDescriptions.ForEach(f =>
            {
                var fieldParameter = string.Format("            parameters.Add(new SqlParameter() {{ ParameterName = \"{0}\", Value = wherePart.{0} }});", f.Name, JlString.ToLowerFirst(className));

                var sqlWhere = string.Format("WHERE [{0}] = @{0}", f.Name);
                codeStr.AppendLine(string.Format(@"

        /// <summary>
        /// 查询实体列表(不存在时,返回null)
        /// </summary>
        /// <param name=""connectionString"">连接字符串</param>
        /// <param name=""wherePart"">查询实体</param>
        /// <returns>查询结果集</returns>
        public static List<En.{1}> GetListBy{5}(string connectionString, dynamic wherePart) {{
            string sql = {0};

            var parameters = new List<SqlParameter>();
{2}
{4}
        }}", getSelectSql(field.ToString(), sqlWhere), className, fieldParameter, f.Name, fieldDataAccess, JlString.ToUpperFirst(f.Name)));
            });


            return(@"

        #region 查询

" + codeStr.ToString() + @"


        #endregion");
        }
Exemplo n.º 5
0
        public static string DaoGet(CodeMakerGeneratCodeOut inModel)
        {
            var className     = inModel.CodeMakerGeneratCodeIn.ClassName;
            var tableName     = inModel.CodeMakerGeneratCodeIn.Table;
            var codeStr       = new StringBuilder();
            var field         = new StringBuilder();
            var fieldSetModel = new StringBuilder();

            inModel.FieldDescriptions.ForEach(ff =>
            {
                field.AppendLine("                [" + ff.Name + "],");
                if (JlDbTypeMap.Map4CSharp(ff.DbType) == "string")
                {
                    fieldSetModel.AppendLine(string.Format("                    {0} = row[\"{0}\"].ToString(),", ff.Name, JlString.ToLowerFirst(className)));
                }
                else
                {
                    fieldSetModel.AppendLine(string.Format("                    {0} = JlConvert.TryTo{1}(row[\"{0}\"]),", ff.Name, JlString.ToUpperFirst(JlDbTypeMap.Map4CSharp(ff.DbType))));
                }
            });
            inModel.FieldDescriptions.ForEach(f =>
            {
                var fieldParameter = new StringBuilder();
                fieldParameter.AppendLine(string.Format("            parameters.Add(new SqlParameter() {{ ParameterName = \"{0}\", Value = wherePart.{0} }});", f.Name, JlString.ToLowerFirst(className)));

                codeStr.AppendLine(string.Format(@"

        /// <summary>
        /// 通过{1}查询实体
        /// </summary>
        /// <param name=""connectionString"">连接字符串</param>
        /// <param name=""wherePart"">删除属性</param>
        /// <returns>查询结果</returns>
        public static En.{4} GetBy{6}(string connectionString, dynamic wherePart) {{
            var sql = @""SELECT
{2}
                FROM [{0}] WITH (NOLOCK)
                WHERE [{1}] = @{1}"";
		
            var parameters = new List<SqlParameter>();

{3}		
            var dataTable = new DataTable();
            SlDatabase.Fill(connectionString, sql, dataTable, parameters.ToArray());

            if (dataTable.Rows.Count > 0)
            {{
                var row = dataTable.Rows[0];
                return new En.{4}()
                {{
{5}
                }};
            }}
            else
            {{
                return null;
            }}
        }}
", tableName, f.Name,
                                                 field.ToString().Substring(0, field.Length - 3),
                                                 fieldParameter, className, fieldSetModel, JlString.ToUpperFirst(f.Name)));
            });
            return(@"

        #region 查询

" + codeStr.ToString() + @"


        #endregion");
        }