Ejemplo n.º 1
0
        /// <summary>
        /// 生成更新语句
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual string BuildUpdateSQL(Model.Model model)
        {
            List <string> fileds    = new List <string>();
            List <string> wheres    = new List <string>();
            var           propertys = model.GetType().GetProperties();
            var           primarys  = model.getPrimaryKeys();
            var           identity  = model.getIdentify();

            foreach (var p in propertys)
            {
                var notmaps = p.GetCustomAttributes(typeof(Model.NotMapingAttribute), false);
                if (notmaps != null && notmaps.Length > 0)
                {
                    continue;
                }

                if (primarys.Any(m => m.Name == p.Name))
                {
                    wheres.Add(string.Format("[{0}]=@{0}", p.Name));
                }
                if (!Magic.Mvc.Equals.IsNull(identity) && identity.Name == p.Name)
                {
                    continue;
                }

                fileds.Add(string.Format("[{0}]=@{0}", p.Name));
            }

            return(string.Format(" UPDATE [{0}] SET {1} WHERE {2}", model.GetType().Name, string.Join(",", fileds.ToArray()), string.Join(" and ", wheres.ToArray())));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成插入语句
        /// </summary>
        /// <param name="model">模型对象</param>
        /// <returns></returns>
        public virtual string BuildInsertSQL(Model.Model model)
        {
            List <string> fileds    = new List <string>();
            List <string> values    = new List <string>();
            var           propertys = model.GetType().GetProperties();
            var           identity  = model.getIdentify();

            foreach (var p in propertys)
            {
                if (!Magic.Mvc.Equals.IsNull(identity) && identity.Name == p.Name)
                {
                    continue;
                }

                var notmaps = p.GetCustomAttributes(typeof(Model.NotMapingAttribute), false);
                if (notmaps != null && notmaps.Length > 0)
                {
                    continue;
                }

                fileds.Add(string.Format("[{0}]", p.Name));
                values.Add(string.Format("@{0}", p.Name));
            }

            return(string.Format(" INSERT INTO [{0}]({1}) VALUES({2}) ", model.GetType().Name, string.Join(",", fileds.ToArray()), string.Join(",", values.ToArray())));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取sql参数
        /// </summary>
        /// <returns></returns>
        public virtual IDataParameter[] GetParameters(Model.Model model)
        {
            var propertys = model.GetType().GetProperties();

            List <IDataParameter> listSQLParamter = new List <IDataParameter>();

            foreach (var p in propertys)
            {
                listSQLParamter.Add(new SqlParameter()
                {
                    ParameterName = p.Name, Value = model.Property(p.Name)
                });
            }
            return(listSQLParamter.ToArray());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 生成删除语句
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual string BuildDeleteSQL(Model.Model model)
        {
            List <string> wheres   = new List <string>();
            var           primarys = model.getPrimaryKeys();

            foreach (var p in primarys)
            {
                var notmaps = p.GetCustomAttributes(typeof(Model.NotMapingAttribute), false);
                if (notmaps != null && notmaps.Length > 0)
                {
                    continue;
                }

                wheres.Add(string.Format("[{0}]=@{0}", p.Name));
            }

            return(string.Format(" DELETE FROM [{0}]  WHERE {1}", model.GetType().Name, string.Join(" and ", wheres.ToArray())));
        }