private string CreateNRCode(DBTableAttribute tableAttribute, Type p)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// " + this.Description);
            sb.AppendLine("        /// </summary>");
            var param   = this.GetSqlParams(this.WhereSql);
            var tempPar = p.GetProperties().Select(a => a.Name).ToList();

            foreach (var item in param)
            {
                var result = tempPar.FindAll(a => a.ToLower() == item.ToLower()).FirstOrDefault();
                sb.AppendLine(string.Format("        /// <param name=\"where{0}\">查询条件参数{0}</param>", result));
            }
            sb.AppendLine("        /// <param name=\"pageSize\">每页大小</param>");
            sb.AppendLine("        /// <param name=\"pageIndex\">页索引,从零开始计数</param>");
            sb.AppendLine("        /// <param name=\"cdb\">传入参数</param>");
            sb.AppendLine("        /// <param name=\"isDesc\">是否正排,默认true最大的排在前面</param>");
            sb.AppendLine("        /// <returns></returns>");
            var temp = this.GetMethodParam(param, p, "where");

            temp = string.IsNullOrWhiteSpace(temp) ? string.Empty : temp + ",";
            sb.AppendLine(string.Format("        internal IList<{0}> {1}({2} int pageSize, int pageIndex,CDataBase cdb,bool isDesc=true)", p.FullName, this.MethodName, temp));
            sb.AppendLine("        {");
            sb.AppendLine("            try");
            sb.AppendLine("            {");
            //sb.AppendLine("                using (CDataBase cdb = new CDataBase(_dbUser))");
            //sb.AppendLine("                {");
            //sb.AppendLine("                    cdb.Parameters.Clear();");
            sb.AppendLine(this.GetParamStr("                cdb.Parameters.Add(\":{0}\", where{1});", param, p));
            sb.AppendLine(string.Format("                var result = cdb.GetDataItems<{0}>(string.Format(", p.FullName));
            sb.AppendLine("                \"select {1} from {0} where rowid in (select rid from (select rownum rn,rid from(select rowid rid,{2} from {0} {6} order by {2} {3}) where rownum<{4}) where rn>{5}) order by {2} {3}\",");
            sb.AppendLine(string.Format("                \"{0}\",", tableAttribute.TableName));
            sb.AppendLine(string.Format("                \"{0}\",", string.Join(",", this.SelectFields)));
            sb.AppendLine(string.Format("                \"{0}\",", string.Join(",", OrderFields)));
            sb.AppendLine("                isDesc ? \"desc\" : \"asc\",");
            sb.AppendLine("                ((pageIndex + 1) * pageSize + 1),");
            sb.AppendLine("                pageSize * pageIndex,");
            var whereSqlStr = string.IsNullOrWhiteSpace(this.WhereSql) ? "string.Empty" : string.Format("\"where {0}\"", this.WhereSql);

            sb.AppendLine(string.Format("                {0}));", whereSqlStr));
            sb.AppendLine("                return result;");
            sb.AppendLine("            }");
            //sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)");
            //sb.AppendLine("            {");
            //sb.AppendLine("                if (oracleException.Number == 3113)");
            //var tempR = this.GetMethodParamFields(param, p, "where");
            //tempR = string.IsNullOrWhiteSpace(tempR) ? string.Empty : tempR + ",";
            //sb.AppendLine(string.Format("                    return {0}({1} pageSize, pageIndex,cdb, isDesc);", this.MethodName, tempR));
            //sb.AppendLine("                else");
            //sb.AppendLine("                    throw oracleException;");
            //sb.AppendLine("            }");
            sb.AppendLine("            catch (Exception ex)");
            sb.AppendLine("            {");
            sb.AppendLine("                throw ex;");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            return(sb.ToString());
        }
Beispiel #2
0
 public string CreateCode(DBTableAttribute tableAttribute, Type p)
 {
     try
     {
         return(string.Format("{0}{1}", CreateDLCode(tableAttribute, p), CreateNRCode(tableAttribute, p)));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
        /// <summary>
        /// 自助释放CDataBase
        /// </summary>
        /// <param name="tableAttribute"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        private string CreateDLCode(DBTableAttribute tableAttribute, Type p)
        {
            if (string.IsNullOrWhiteSpace(this.WhereSql))
            {
                throw new Exception("where 不能为空!");
            }
            this.VerifyWhereSql(this.WhereSql, p.FullName);
            StringBuilder sb    = new StringBuilder();
            var           param = this.GetSqlParams(this.WhereSql);

            sb.AppendLine("        /// <summary>").
            AppendLine("        /// " + this.Description).
            AppendLine("        /// </summary>");
            foreach (var par in param)
            {
                sb.AppendLine(string.Format("        /// <param name=\"{0}\">{0}</param>", par));
            }
            sb.AppendLine("        /// <returns></returns>").
            AppendLine(string.Format("        public int {0}({1})", this.MethodName, this.GetMethodParam(param, p))).
            AppendLine("        {").
            AppendLine("            try").
            AppendLine("            {").
            AppendLine("                using (CDataBase cdb = new CDataBase(_dbUser))").
            AppendLine("                {").
            Append(this.GetParamStr("                    cdb.Parameters.Add(\":{0}\",{1});", param, p)).
            AppendLine(string.Format("                    cdb.ExeSQL(\"delete from {0} where {1}\");", tableAttribute.TableName, this.WhereSql));
            sb.AppendLine("                    var result=cdb.SqlNRows;");
            sb.AppendLine("                    if (result > 0){").
            AppendLine("                         cdb.Commit();").
            AppendLine("                    }").
            AppendLine("                    else").
            AppendLine("                    {").
            AppendLine("                         cdb.Rollback();").
            AppendLine("                    }");
            sb.AppendLine("                    return result;");
            sb.AppendLine("                }").
            AppendLine("            }");

            sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)").
            AppendLine("            {").
            AppendLine("                if (oracleException.Number == 3113)").
            AppendLine(string.Format("                    return {0}({1});", this.MethodName, GetMethodParamFields(param, p))).
            AppendLine("                else").
            AppendLine("                    throw oracleException;").
            AppendLine("            }");

            sb.AppendLine("            catch (Exception ex)").
            AppendLine("            {").
            AppendLine("                throw ex;").
            AppendLine("            }").
            AppendLine("        }");
            return(sb.ToString());
        }
Beispiel #4
0
        public string CreateDLCode(DBTableAttribute tableAttribute, Type p)
        {
            this.VerifySelectSql(this.Sql, p.FullName);
            StringBuilder sb    = new StringBuilder();
            var           param = this.GetSqlParams(this.Sql);

            sb.AppendLine("        /// <summary>").
            AppendLine("        /// " + this.Description).
            AppendLine("        /// </summary>");
            foreach (var par in param)
            {
                sb.AppendLine(string.Format("        /// <param name=\"{0}\">{0}</param>", par));
            }
            string tempReturn = this.IsMult ? string.Format("IList<{0}>", p.FullName) : p.FullName;

            sb.AppendLine("        /// <returns></returns>").
            AppendLine(string.Format("        public {0} {1}({2})", tempReturn, this.MethodName, this.GetMethodParam(param, p))).
            AppendLine("        {").
            AppendLine("            try").
            AppendLine("            {").
            AppendLine("                using (CDataBase cdb = new CDataBase(_dbUser))").
            AppendLine("                {").
            Append(this.GetParamStr("                    cdb.Parameters.Add(\":{0}\",{1});", param, p)).
            AppendLine(string.Format("                    var result = cdb.GetDataItems<{0}>(\"{1}\");", p.FullName, this.Sql));
            if (IsMult)
            {
                sb.AppendLine("                    return result;");
            }
            else
            {
                sb.AppendLine("                    return result.FirstOrDefault();");
            }
            sb.AppendLine("                }");
            sb.AppendLine("            }");


            sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)").
            AppendLine("            {").
            AppendLine("                if (oracleException.Number == 3113)").
            AppendLine(string.Format("                    return {0}({1});", this.MethodName, this.GetMethodParamFields(param, p))).
            AppendLine("                else").
            AppendLine("                    throw oracleException;").
            AppendLine("            }");

            sb.AppendLine("            catch (Exception ex)").
            AppendLine("            {").
            AppendLine("                throw ex;").
            AppendLine("            }").
            AppendLine("        }");
            return(sb.ToString());
        }
        public string CreateCode(DBTableAttribute tableAttribute, Type p)
        {
            this.VerifyWhereSql(this.WhereSql, p.FullName);
            if (string.IsNullOrWhiteSpace(this.WhereSql))
                throw new Exception("where 不能为空!");
            StringBuilder sb = new StringBuilder();
            var param = this.GetSqlParams(this.WhereSql);
            sb.AppendLine("        /// <summary>").
               AppendLine("        /// " + this.Description).
               AppendLine("        /// </summary>");
            foreach (var par in param)
            {
                sb.AppendLine(string.Format("        /// <param name=\"{0}\">{0}</param>", par));
            }
            sb.AppendLine("        /// <returns></returns>").
               AppendLine(string.Format("        public int {0}({1})",this.MethodName, this.GetMethodParam(param, p))).
               AppendLine("        {").
               AppendLine("            try").
               AppendLine("            {").
               Append(this.GetParamStr("                cdb.Parameters.Add(\":{0}\",{1});", param, p)).
               AppendLine(string.Format("                cdb.ExeSQL(\"delete from {0} where {1}\");", tableAttribute.TableName, this.WhereSql));
            sb.AppendLine("                var result=cdb.SqlNRows;");
            sb.AppendLine("                if (result > 0){").
               AppendLine("                     cdb.Commit();").
               AppendLine("                }").
               AppendLine("                else").
               AppendLine("                {").
               AppendLine("                     cdb.Rollback();").
               AppendLine("                }");
            sb.AppendLine("                return result;");
            sb.AppendLine("            }");

            sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)").
              AppendLine("            {").
              AppendLine("                if (oracleException.Number == 3113)").
              AppendLine(string.Format("                    return {0}({1});", this.MethodName, GetMethodParamFields(param, p))).
              AppendLine("                else").
              AppendLine("                    throw oracleException;").
              AppendLine("            }");
            sb.AppendLine("            catch (Exception ex)").
               AppendLine("            {").
               AppendLine("                throw ex;").
               AppendLine("            }").
               AppendLine("        }");
            return sb.ToString();
        }
 public string CreateCode(DBTableAttribute tableAttribute, Type p)
 {
     try
     {
         var pros = p.GetProperties().Select(a => a.Name.ToLower()).ToList();
         foreach (var item in this.SelectFields)//验证字段是否包含在实体对象中
         {
             if (pros.FindAll(a => a == item.ToLower()).Count <= 0)
             {
                 throw new Exception(string.Format("{0}对象中的{1}方法中查询结果有不包含{2}字段问题", p.FullName, this.MethodName, item));
                 break;
             }
         }
         return(string.Format("{0}{1}{2}{3}", CreateDLCode(tableAttribute, p), CreateNRCode(tableAttribute, p), CreateDLCountCode(tableAttribute, p), CreateNRCountCode(tableAttribute, p)));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
Beispiel #7
0
        public string CreateCode(DBTableAttribute tableAttribute, Type p)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("        /// <summary>").
            AppendLine("        /// " + this.Description).
            AppendLine("        /// </summary>").
            AppendLine("        /// <param name=\"model\">新增对象</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine(string.Format("        public int {0}({1} model)", this.MethodName, p.FullName));
            sb.AppendLine("        {");
            sb.AppendLine("            try");
            sb.AppendLine("            {");
            sb.Append(this.GetParamStrAdd("                cdb.Parameters.Add(\":{0}\",model.{1});", p));
            var fields = p.GetProperties().Select(a => a.Name).ToList();

            sb.AppendLine(string.Format("                cdb.ExeSQL(\"insert into {0}({1}) values(:{2})\");", tableAttribute.TableName, string.Join(",", fields), string.Join(",:", fields)));
            sb.AppendLine("                var result=cdb.SqlNRows;");
            sb.AppendLine("                if (result > 0){").
            AppendLine("                     cdb.Commit();").
            AppendLine("                }").
            AppendLine("                else").
            AppendLine("                {").
            AppendLine("                     cdb.Rollback();").
            AppendLine("                }");
            sb.AppendLine("                return result;");
            sb.AppendLine("            }");
            sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)").
            AppendLine("            {").
            AppendLine("                if (oracleException.Number == 3113)").
            AppendLine(string.Format("                    return {0}(model);", this.MethodName)).
            AppendLine("                else").
            AppendLine("                    throw oracleException;").
            AppendLine("            }");
            sb.AppendLine("            catch (Exception ex)").
            AppendLine("            {").
            AppendLine("                throw ex;").
            AppendLine("            }").
            AppendLine("        }");
            return(sb.ToString());
        }
Beispiel #8
0
        /// <summary>
        /// 生成类,并且判断是那种方式的生成
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private static string GetClassCode(Type p)
        {
            MemberInfo    classInfo = p;
            StringBuilder sb        = new StringBuilder();
            var           attrs     = System.Attribute.GetCustomAttributes(classInfo, typeof(System.Attribute)).ToList();

            if (attrs == null || attrs.Count <= 0)
            {
                return(string.Empty);
            }
            Attribute.DBTableAttribute table = null;
            bool isPartial = false;//是否存在扩展类

            //bool isNR = false;//是否存在非扩展类
            attrs.ForEach(a =>
            {
                if (table == null && a.GetType() == typeof(Attribute.DBTableAttribute))
                {
                    table = a as Attribute.DBTableAttribute;
                }
                else if (!isPartial && a is Attribute.IReleaseMaker)
                {
                    isPartial = true;
                }
                //else if (!isNR && a is Attribute.INoReleaseMaker)
                //{
                //    isNR = true;
                //}
            });
            if (table == null)
            {
                return(string.Empty);
            }
            if (isPartial)
            {
                sb.AppendLine(DoPartialCode(table, attrs, p));
            }
            //if (isNR)
            //    sb.AppendLine(DoNRCode(table, attrs,p));
            return(sb.ToString());
        }
 public string CreateCode(DBTableAttribute tableAttribute, Type p)
 {
     StringBuilder sb = new StringBuilder();
     
     sb.AppendLine("        /// <summary>").
        AppendLine("        /// " + this.Description).
        AppendLine("        /// </summary>").
        AppendLine("        /// <param name=\"model\">新增对象</param>");
     sb.AppendLine("        /// <returns></returns>");
     sb.AppendLine(string.Format("        public int {0}({1} model)", this.MethodName, p.FullName));
     sb.AppendLine("        {");
     sb.AppendLine("            try");
     sb.AppendLine("            {");
     sb.Append(this.GetParamStrAdd("                cdb.Parameters.Add(\":{0}\",model.{1});", p));
     var fields = p.GetProperties().Select(a => a.Name).ToList();
     sb.AppendLine(string.Format("                cdb.ExeSQL(\"insert into {0}({1}) values(:{2})\");", tableAttribute.TableName, string.Join(",", fields), string.Join(",:", fields)));
     sb.AppendLine("                var result=cdb.SqlNRows;");
     sb.AppendLine("                if (result > 0){").
        AppendLine("                     cdb.Commit();").
        AppendLine("                }").
        AppendLine("                else").
        AppendLine("                {").
        AppendLine("                     cdb.Rollback();").
        AppendLine("                }");
     sb.AppendLine("                return result;");
     sb.AppendLine("            }");
     sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)").
        AppendLine("            {").
        AppendLine("                if (oracleException.Number == 3113)").
        AppendLine(string.Format("                    return {0}(model);", this.MethodName)).
        AppendLine("                else").
        AppendLine("                    throw oracleException;").
        AppendLine("            }");
     sb.AppendLine("            catch (Exception ex)").
        AppendLine("            {").
        AppendLine("                throw ex;").
        AppendLine("            }").
        AppendLine("        }");
     return sb.ToString();
 }
        public string CreateCode(DBTableAttribute tableAttribute, Type p)
        {
            
            if (string.IsNullOrWhiteSpace(this.WhereSql))
                throw new Exception("where 不能为空!");
            if (this.UpdateFields == null || this.UpdateFields.Count <= 0)
                throw new Exception("update 不能为空");
            this.VerifyWhereSql(this.WhereSql, p.FullName);
            var tempPro=p.GetProperties().Select(a=>a.Name.ToUpper()).ToList();
            foreach (var field in this.UpdateFields)
            {
                if (!tempPro.Contains(field.ToUpper())) {
                    throw new Exception(string.Format("{0}对象中不存在updateFields中的{1}字段,请核对!",p.FullName,field));
                }
            }
            StringBuilder sb = new StringBuilder();
            var param = this.GetSqlParams(this.WhereSql);
            sb.AppendLine("        /// <summary>").
               AppendLine("        /// " + this.Description).
               AppendLine("        /// </summary>");
            foreach (var pro in this.UpdateFields)
            {
                sb.AppendLine(string.Format("        /// <param name=\"update{0}\">update{0}</param>", pro));
            }
            foreach (var par in param)
            {
                sb.AppendLine(string.Format("        /// <param name=\"where{0}\">where{0}</param>", par));
            }
            sb.AppendLine("        /// <returns></returns>");
            string whereParams=this.GetMethodParam(param, p,"where");
            string updateParams=this.GetMethodParam(this.UpdateFields, p,"update");
            var tempParam=string.Join(",",new []{updateParams,whereParams});
            sb.AppendLine(string.Format("        public int {0}({1})", this.MethodName,tempParam ));
            sb.AppendLine("        {").
               AppendLine("            try").
               AppendLine("            {").
               Append(this.GetParamStr("                cdb.Parameters.Add(\":update{0}\",update{1});", this.UpdateFields, p)).
               Append(this.GetParamStr("                cdb.Parameters.Add(\":{0}\",where{1});", param, p).Replace("cdb.Parameters.Clear();",string.Empty)).
               AppendLine(string.Format("                cdb.ExeSQL(\"update {0} set {1} where {2}\");",tableAttribute.TableName,this.GetUpdateSql(p), this.WhereSql));
            sb.AppendLine("                var result=cdb.SqlNRows;");
            sb.AppendLine("                if (result > 0){").
               AppendLine("                     cdb.Commit();").
               AppendLine("                }").
               AppendLine("                else").
               AppendLine("                {").
               AppendLine("                     cdb.Rollback();").
               AppendLine("                }");
            sb.AppendLine("                return result;");
            sb.AppendLine("            }");

            string fwhereParams = this.GetMethodParamFields(param, p, "where");
            string fupdateParams = this.GetMethodParamFields(this.UpdateFields, p, "update");
            var ftempParam = string.Join(",", new[] { fupdateParams, fwhereParams });

            sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)").
              AppendLine("            {").
              AppendLine("                if (oracleException.Number == 3113)").
              AppendLine(string.Format("                    return {0}({1});", this.MethodName, ftempParam)).
              AppendLine("                else").
              AppendLine("                    throw oracleException;").
              AppendLine("            }");

            sb.AppendLine("            catch (Exception ex)").
               AppendLine("            {").
               AppendLine("                throw ex;").
               AppendLine("            }").
               AppendLine("        }");
            return sb.ToString();
        }
Beispiel #11
0
        public string CreateNRCode(DBTableAttribute tableAttribute, Type p)
        {
            if (string.IsNullOrWhiteSpace(this.WhereSql))
            {
                throw new Exception("where 不能为空!");
            }
            if (this.UpdateFields == null || this.UpdateFields.Count <= 0)
            {
                throw new Exception("update 不能为空");
            }
            this.VerifyWhereSql(this.WhereSql, p.FullName);
            var tempPro = p.GetProperties().Select(a => a.Name.ToUpper()).ToList();

            foreach (var field in this.UpdateFields)
            {
                if (!tempPro.Contains(field.ToUpper()))
                {
                    throw new Exception(string.Format("{0}对象中不存在updateFields中的{1}字段,请核对!", p.FullName, field));
                }
            }
            StringBuilder sb    = new StringBuilder();
            var           param = this.GetSqlParams(this.WhereSql);

            sb.AppendLine("        /// <summary>").
            AppendLine("        /// " + this.Description).
            AppendLine("        /// </summary>");
            foreach (var pro in this.UpdateFields)
            {
                sb.AppendLine(string.Format("        /// <param name=\"update{0}\">update{0}</param>", pro));
            }
            foreach (var par in param)
            {
                sb.AppendLine(string.Format("        /// <param name=\"where{0}\">where{0}</param>", par));
            }
            sb.AppendLine("        /// <returns></returns>");
            string whereParams  = this.GetMethodParam(param, p, "where");
            string updateParams = this.GetMethodParam(this.UpdateFields, p, "update");
            var    tempParam    = string.Join(",", new[] { updateParams, whereParams });

            sb.AppendLine(string.Format("        internal int {0}({1},CDataBase cdb)", this.MethodName, tempParam));
            sb.AppendLine("        {").
            AppendLine("            try").
            AppendLine("            {").
            Append(this.GetParamStr("                cdb.Parameters.Add(\":update{0}\",update{1});", this.UpdateFields, p)).
            Append(this.GetParamStr("                cdb.Parameters.Add(\":{0}\",where{1});", param, p).Replace("cdb.Parameters.Clear();", string.Empty)).
            AppendLine(string.Format("                cdb.ExeSQL(\"update {0} set {1} where {2}\");", tableAttribute.TableName, this.GetUpdateSql(p), this.WhereSql));
            sb.AppendLine("                var result=cdb.SqlNRows;");
            sb.AppendLine("                if (result > 0){").
            AppendLine("                     cdb.Commit();").
            AppendLine("                }").
            AppendLine("                else").
            AppendLine("                {").
            AppendLine("                     cdb.Rollback();").
            AppendLine("                }");
            sb.AppendLine("                return result;");
            sb.AppendLine("            }");

            //string fwhereParams = this.GetMethodParamFields(param, p, "where");
            //string fupdateParams = this.GetMethodParamFields(this.UpdateFields, p, "update");
            //var ftempParam = string.Join(",", new[] { fupdateParams, fwhereParams });

            //sb.AppendLine("            catch (Oracle.DataAccess.Client.OracleException oracleException)").
            //  AppendLine("            {").
            //  AppendLine("                if (oracleException.Number == 3113)").
            //  AppendLine(string.Format("                    return {0}({1},cdb);", this.MethodName, ftempParam)).
            //  AppendLine("                else").
            //  AppendLine("                    throw oracleException;").
            //  AppendLine("            }");

            sb.AppendLine("            catch (Exception ex)").
            AppendLine("            {").
            AppendLine("                throw ex;").
            AppendLine("            }").
            AppendLine("        }");
            return(sb.ToString());
        }