Example #1
0
        public override string MergeStr <T>(T model, bool IsPosibleNullValue)
        {
            StringBuilder str       = new StringBuilder("");
            string        tableName = helper.GetTableName(model.GetType());
            var           props     = model.GetType().GetProperties();

            long count    = 0;
            long pk_Count = 0;

            str.AppendFormat("INSERT INTO {0} ( ", tableName);

            foreach (var p in props)
            {
                if (!helper.CheckIgnore(p))
                {
                    var columnName = helper.ColumnName(p);

                    if (p.GetValue(model) != null || helper.CheckCreatedDate(p))
                    {
                        if (helper.CheckKey(p))
                        {
                            pk_Count++;
                        }
                        if (count >= 1)
                        {
                            str.Append(", ");
                        }
                        str.Append(columnName);
                        count++;
                    }
                }
            }

            if (pk_Count < 1)
            {
                throw new PkNotFoundException("Insert를 할때에는 Primary Key가 한개 이상 존재해야 합니다.");
            }

            str.Append(" ) VALUES ( ");

            count = 0;
            foreach (var p in props)
            {
                if (!helper.CheckIgnore(p))
                {
                    var columnName = helper.ColumnName(p);

                    if (p.GetValue(model) != null || helper.CheckCreatedDate(p))
                    {
                        if (count >= 1)
                        {
                            str.Append(", ");
                        }

                        if (helper.CheckCreatedDate(p))
                        {
                            str.Append("NOW()");
                        }
                        else
                        {
                            str.Append("@" + p.Name);
                        }
                        count++;
                    }
                }
            }
            str.Append(" ) ON DUPLICATE KEY UPDATE ");

            count = 0;
            foreach (var p in props)
            {
                var columnName = helper.ColumnName(p);


                if (IsPosibleNullValue || p.GetValue(model) != null || helper.CheckLastModifiedDate(p))
                {
                    if (helper.CheckCreatedDate(p) || helper.CheckKey(p))
                    {
                        continue;
                    }

                    if (!helper.CheckIgnore(p))
                    {
                        if (count >= 1)
                        {
                            str.Append(", ");
                        }

                        if (helper.CheckLastModifiedDate(p))
                        {
                            str.AppendFormat("{0} = {1}", columnName, "NOW()");
                        }
                        else if (p.GetValue(model) == null)
                        {
                            str.AppendFormat("{0} = NULL", columnName);
                        }
                        else if (helper.CheckCreatedDate(p) == false)
                        {
                            str.AppendFormat("{0} = {1}{2}", columnName, "@", p.Name);
                        }
                        count++;
                    }
                }
            }

            return(str.ToString().TrimEnd());
        }
Example #2
0
        public string UpdateStr <T>(T model, bool IsPosibleNullValue)
        {
            StringBuilder str       = new StringBuilder("");
            string        tableName = helper.GetTableName(model.GetType());
            var           props     = model.GetType().GetProperties();

            long count = 0;

            str.AppendFormat("UPDATE {0} SET ", tableName);
            foreach (var p in props)
            {
                var columnName = helper.ColumnName(p);

                // 필수 입력인데 값이 안들어가 있는 경우 RequiredValueNotFoundException
                if (p.GetValue(model) == null && helper.CheckRequiredColumn(p))
                {
                    throw new RequiredValueNotFoundException(p.GetValue(model) + "에 반드시 값이 들어가야 할 컬럼입니다.");
                }


                if (IsPosibleNullValue || p.GetValue(model) != null || helper.CheckLastModifiedDate(p))
                {
                    if (helper.CheckCreatedDate(p) || helper.CheckKey(p))
                    {
                        continue;
                    }

                    if (!helper.CheckIgnore(p))
                    {
                        if (count >= 1)
                        {
                            str.Append(", ");
                        }

                        if (helper.CheckLastModifiedDate(p))
                        {
                            str.AppendFormat("{0} = {1}", columnName, DBNowDatefunction);
                        }
                        else if (p.GetValue(model) == null)
                        {
                            str.AppendFormat("{0} = NULL", columnName);
                        }
                        else if (helper.CheckCreatedDate(p) == false)
                        {
                            str.AppendFormat("{0} = {1}{2}", columnName, ParamMark, p.Name);
                        }
                        count++;
                    }
                }
            }
            str.Append(" WHERE ");

            count = 0;
            foreach (var p in props)
            {
                var columnName = helper.ColumnName(p);

                if (helper.CheckKey(p))
                {
                    if (p.GetValue(model) == null)
                    {
                        throw new PkNotFoundException("Update를 할때에는 Primary Key에 반드시 값이 존재해야 합니다.");
                    }

                    if (count >= 1)
                    {
                        str.Append(" AND ");
                    }
                    str.AppendFormat("{0} = {1}{2}", columnName, ParamMark, p.Name);
                    count++;
                }
            }


            return(str.ToString().TrimEnd());
        }