Example #1
0
        public int Delete(Type type, IDictionary <string, object> param)
        {
            bool   hasId      = false;
            string primaryKey = GetPrimaryKey(type);
            string sql        = MappingInfo.GetSqlStatementByType(type, "Delete");

            sql = sql.Substring(0, sql.IndexOf(" where ", StringComparison.CurrentCultureIgnoreCase) + 7);

            string where = "";

            foreach (string key in param.Keys)
            {
                if ((String.Compare(key, primaryKey, true) == 0))
                {
                    hasId = true;
                }

                where += string.Format(" and {0}={1}{0}", key, StatementParser.PREFIX);
            }
            sql = sql + where.Substring(5);
            return(Dao.Execute(
                       sql,
                       param
                       ));
        }
Example #2
0
        public bool Update(Object obj, string extTableName = null)
        {
            if (obj == null)
            {
                return(false);
            }

            string sql = MappingInfo.GetSqlStatementByType(obj.GetType(), "Update", extTableName);

            return(Dao.Execute(
                       sql,
                       obj
                       ) == 1);
        }
Example #3
0
        public bool Delete(Object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            string sql = MappingInfo.GetSqlStatementByType(obj.GetType(), "Delete");

            return(Dao.Execute(
                       sql,
                       obj
                       ) == 1);
        }
Example #4
0
        public int Update <T>(IList <T> li, string extTableName = null)
        {
            if (li == null || li.Count == 0)
            {
                return(-1);
            }

            string sql = MappingInfo.GetSqlStatementByType(li[0].GetType(), "Update", extTableName);

            List <string> paramNames = new List <string>();

            foreach (Match m in REGEX_GET_PARAM.Matches(sql))
            {
                paramNames.Add(m.Value.Substring(1));
            }

            sql = REGEX_GET_PARAM.Replace(sql, "$1#");//为所有参数加入#号
            StringBuilder sb = new StringBuilder();

            IDictionary <string, Object> param = new Dictionary <string, Object>(li.Count * paramNames.Count);

            for (int i = 0; i < li.Count; i++)
            {
                sb.Append(sql).AppendLine(";");
                foreach (string propname in paramNames)
                {
                    param.Add(propname + i,
                              Dappers.Context.Reflection.GetData(li[i], propname));
                }
            }

            return(Dao.Execute(
                       sb.ToString(),
                       param
                       ));
        }
Example #5
0
        public bool Insert(Object obj, out Int32 lastInsertId, string extTableName = null)
        {
            lastInsertId = -1;
            if (obj == null)
            {
                return(false);
            }

            string sql = MappingInfo.GetSqlStatementByType(obj.GetType(), "Insert", extTableName);

            if (Dao.ConnectionManager.ConnectionTypeName.StartsWith("MySql."))
            {
                sql += ";SELECT LAST_INSERT_ID();";
            }

            lastInsertId = Convert.ToInt32(
                Dao.Query <long>(
                    sql,
                    obj
                    ).First()
                );

            return(lastInsertId > 0);
        }
Example #6
0
        public int Insert <T>(IList <T> li, string extTableName = null)
        {
            int affectedRows = 0;

            if (li == null || li.Count == 0)
            {
                return(-1);
            }
            string sql = MappingInfo.GetSqlStatementByType(li[0].GetType(), "Insert", extTableName);

            List <string> paramNames = new List <string>();

            foreach (Match m in REGEX_GET_PARAM.Matches(sql))
            {
                paramNames.Add(m.Value.Substring(1));
            }

            sql = REGEX_GET_PARAM.Replace(sql, "$1#");//为所有参数加入#号
            StringBuilder sb = new StringBuilder(sql);

            string values = sql.Substring(sql.IndexOf(" values", StringComparison.CurrentCultureIgnoreCase) + 7);

            IDictionary <string, Object> param = new Dictionary <string, Object>();

            for (int i = 0; i < li.Count; i++)
            {
                if (sb.ToString().Length > 100000) //限制sql字符数
                {
                    affectedRows += Dao.Execute(sb.ToString(), param);

                    sb    = new StringBuilder(sql);
                    param = new Dictionary <string, Object>();

                    sb.Replace("#", i.ToString());
                }
                else if (i == 0)
                {
                    sb.Replace("#", i.ToString());
                }
                else
                {
                    sb.AppendLine(",")
                    .Append(values.Replace("#", i.ToString()));
                }

                foreach (string propname in paramNames)
                {
                    param.Add(propname + i,
                              Dappers.Context.Reflection.GetData(li[i], propname));
                }
            }

            if (sb.ToString().Length > 0)
            {
                affectedRows += Dao.Execute(sb.ToString(), param);

                sb    = new StringBuilder(sql);
                param = new Dictionary <string, Object>();
            }

            return(affectedRows);
        }