Beispiel #1
0
        /// <summary>
        /// 得到sqltag
        /// </summary>
        /// <param name="sqlId"></param>
        /// <param name="parameter"></param>
        /// <param name="formatText"></param>
        /// <returns></returns>
        public SqlTagFormat GetSqlTagFormat <T>(string sqlId, EasySqlParameter <T> parameter, bool formatText = false)
        {
            var tag = this.GetSqlTag(sqlId);

            if (tag == null)
            {
                throw new KeyNotExistedException(sqlId, "the sql tag '{0}' not found in the sql files", sqlId);
            }

            return(formatText ? tag.FormatForText(parameter) : tag.Format(parameter));
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="updateId"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public int Update <T>(string updateId, EasySqlParameter <T> parameter)
        {
            var tag = this.GetSqlTag(updateId);

            if (tag == null)
            {
                throw new KeyNotExistedException(updateId, "the sql tag '{0}' not found in the sql files", updateId);
            }

            return(this.Update <T>(tag, parameter));
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="Result"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="selectId"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public IEnumerable <Result> QueryForEnumerable <Result, T>(string selectId, EasySqlParameter <T> parameter)
        {
            var tag = this.GetSqlTag(selectId);

            if (tag == null)
            {
                throw new KeyNotExistedException(selectId, "the sql tag '{0}' not found in the sql files", selectId);
            }

            return(this.QueryForEnumerable <Result, T>(tag, parameter));
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="insertId"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public object Insert <T>(string insertId, EasySqlParameter <T> parameter)
        {
            var tag = this.GetSqlTag(insertId);

            if (tag == null)
            {
                throw new KeyNotExistedException(insertId, "the sql tag '{0}' not found in the sql files", insertId);
            }

            return(this.Insert <T>(tag, parameter));
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="callId"></param>
        /// <param name="parameter"></param>
        /// <param name="callmode"></param>
        /// <returns></returns>
        public object Call <T>(string callId, EasySqlParameter <T> parameter, CallMode callmode)
        {
            var tag = this.GetSqlTag(callId);

            if (tag == null)
            {
                throw new KeyNotExistedException(callId, "the sql tag '{0}' not found in the sql files", callId);
            }

            return(this.Call <T>(tag, parameter, callmode));
        }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tag"></param>
        /// <param name="parameter"></param>
        /// <param name="callmode"></param>
        /// <returns></returns>
        public virtual object Call <T>(SqlTag tag, EasySqlParameter <T> parameter, CallMode callmode)
        {
            var format = tag.Format(parameter);

            if ((callmode & CallMode.ExecuteScalar) == CallMode.ExecuteScalar)
            {
                if ((callmode & CallMode.CommandText) == CallMode.CommandText)
                {
                    return(this.SqlExecuter.ExecuteScalar(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
                }

                if ((callmode & CallMode.CommandStoredProcedure) == CallMode.CommandStoredProcedure)
                {
                    return(this.SqlExecuter.ExecuteScalar(format.ToString(), CommandType.StoredProcedure, format.Parameters.ToArray()));
                }

                if ((callmode & CallMode.CommandTableDirect) == CallMode.CommandTableDirect)
                {
                    return(this.SqlExecuter.ExecuteScalar(format.ToString(), CommandType.TableDirect, format.Parameters.ToArray()));
                }

                return(this.SqlExecuter.ExecuteScalar(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
            }

            if ((callmode & CallMode.ExecuteNonQuery) == CallMode.ExecuteNonQuery)
            {
                if ((callmode & CallMode.CommandText) == CallMode.CommandText)
                {
                    return(this.SqlExecuter.ExecuteNonQuery(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
                }

                if ((callmode & CallMode.CommandStoredProcedure) == CallMode.CommandStoredProcedure)
                {
                    return(this.SqlExecuter.ExecuteNonQuery(format.ToString(), CommandType.StoredProcedure, format.Parameters.ToArray()));
                }

                if ((callmode & CallMode.CommandTableDirect) == CallMode.CommandTableDirect)
                {
                    return(this.SqlExecuter.ExecuteNonQuery(format.ToString(), CommandType.TableDirect, format.Parameters.ToArray()));
                }

                return(this.SqlExecuter.ExecuteNonQuery(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
            }

            return(this.SqlExecuter.ExecuteScalar(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
        }
Beispiel #7
0
 object IDao.Insert <T>(string insertId, EasySqlParameter <T> parameter)
 {
     return(this.dao.Insert <T>(insertId, parameter));
 }
Beispiel #8
0
 int IDao.Delete <T>(string deleteId, EasySqlParameter <T> parameter)
 {
     return(this.dao.Delete <T>(deleteId, parameter));
 }
Beispiel #9
0
 object IDao.Call <T>(string deleteId, EasySqlParameter <T> parameter, CallMode callmode)
 {
     return(this.dao.Call <T>(deleteId, parameter, callmode));
 }
Beispiel #10
0
 SqlTagFormat IDao.GetSqlTagFormat <T>(string sqlId, EasySqlParameter <T> parameter, bool formatText)
 {
     return(this.dao.GetSqlTagFormat <T>(sqlId, parameter, formatText));
 }
Beispiel #11
0
 int IDao.Update <T>(string updateId, EasySqlParameter <T> parameter)
 {
     return(this.dao.Update <T>(updateId, parameter));
 }
Beispiel #12
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="Result"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="tag"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public virtual IEnumerable <Result> QueryForEnumerable <Result, T>(SqlTag tag, EasySqlParameter <T> parameter)
        {
            var format = tag.Format(parameter);
            var temp   = this.SqlExecuter.QueryForEnumerable <Result>(format.ToString(), CommandType.Text, format.Parameters.ToArray());

            return(temp == null ? new Result[0] : temp);
        }
Beispiel #13
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="Result"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="tag"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public virtual Result QueryForObject <Result, T>(SqlTag tag, EasySqlParameter <T> parameter)
        {
            var format = tag.Format(parameter);

            return(this.SqlExecuter.QueryForObject <Result>(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
        }
Beispiel #14
0
        /// <summary>
        /// 插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tag"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public virtual object Insert <T>(SqlTag tag, EasySqlParameter <T> parameter)
        {
            var format = tag.Format(parameter);

            if (format.ReturnType.IsNullOrEmpty())
            {
                return(this.SqlExecuter.Insert(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
            }

            var @object = this.SqlExecuter.ExecuteScalar(format.ToString(), CommandType.Text, format.Parameters.ToArray());

            switch (format.ReturnType)
            {
            case "byte":
                return(Convert.ChangeType(@object, TypeCode.Byte));

            case "sbyte":
                return(Convert.ChangeType(@object, TypeCode.SByte));

            case "bool":
                return(Convert.ChangeType(@object, TypeCode.Boolean));

            case "char":
                return(Convert.ChangeType(@object, TypeCode.Char));

            case "datetime":
                return(Convert.ChangeType(@object, TypeCode.DateTime));

            case "decimal":
                return(Convert.ChangeType(@object, TypeCode.Decimal));

            case "double":
                return(Convert.ChangeType(@object, TypeCode.Double));

            case "float":
                return(Convert.ChangeType(@object, TypeCode.Single));

            case "int":
                return(Convert.ChangeType(@object, TypeCode.Int32));

            case "uint":
                return(Convert.ChangeType(@object, TypeCode.UInt32));

            case "short":
                return(Convert.ChangeType(@object, TypeCode.Int16));

            case "ushort":
                return(Convert.ChangeType(@object, TypeCode.UInt16));

            case "long":
                return(Convert.ChangeType(@object, TypeCode.Int64));

            case "ulong":
                return(Convert.ChangeType(@object, TypeCode.UInt64));

            case "guid":
                return(@object == null ? Guid.Empty : Guid.Parse(@object.ToString()));

            case "string":
                return(Convert.ChangeType(@object, TypeCode.String));
            }

            return(@object);
        }
Beispiel #15
0
 Result IDao.QueryForObject <Result, T>(string selectId, EasySqlParameter <T> parameter)
 {
     return(this.dao.QueryForObject <Result, T>(selectId, parameter));
 }
Beispiel #16
0
 IEnumerable <Result> IDao.QueryForEnumerable <Result, T>(string selectId, EasySqlParameter <T> parameter)
 {
     return(this.dao.QueryForEnumerable <Result, T>(selectId, parameter));
 }
Beispiel #17
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tag"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public virtual int Update <T>(SqlTag tag, EasySqlParameter <T> parameter)
        {
            var format = tag.Format(parameter);

            return(this.SqlExecuter.Update(format.ToString(), CommandType.Text, format.Parameters.ToArray()));
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="dao"></param>
 /// <param name="parameter"></param>
 public EasyDecoratedXmlDao(IDao dao, EasySqlParameter <Parameter> parameter) : base(dao)
 {
     this.dao       = dao;
     this.parameter = parameter;
 }
        public override void Format <T>(SqlTagFormat format, EasySqlParameter <T> parameter, IReadOnlyList <KeyValueTuple <string, object> > convert)
        {
            if (this.lable.SqlText.IsNullOrEmpty())
            {
                return;
            }

            if (this.parameterPositions == null || this.parameterPositionCount == 0)
            {
                format.Write(this.lable.SqlText);
                return;
            }

            for (var i = 0; i < this.lable.SqlText.Length; i++)
            {
                var para = this.MathPosition(this.parameterPositions, i);
                if (para == null)
                {
                    format.Write(this.lable.SqlText[i]);
                    continue;
                }

                var firstConvert = convert.FirstOrDefault(o => o.Key.IsEquals(para.Name));
                if (firstConvert == null)
                {
                    throw new Exception(string.Format("当前在sql语句中参数为{0}的值在所提供的参数列表中找不到", para.Name));
                }

                var value = firstConvert.Value;
                if (value is INullableParameter)
                {
                    value = ((IReferceNullableParameter)firstConvert.Value).Value;
                }

                var isArray = value is System.Collections.IEnumerable;
                if (!isArray || value is string)
                {
                    if (format.IfTextParameter(para))
                    {
                        if (value == null)
                        {
                            //format.WriteOnTextMode("\'null\'");
                            //format.WriteOnTextMode("null");
                        }
                        if (value == DBNull.Value)
                        {
                            //format.WriteOnTextMode("\'null\'");
                            format.WriteOnTextMode("null");
                        }
                        else
                        {
                            //format.WriteOnTextMode('\'');
                            format.WriteOnTextMode(value.ToString());
                            //format.WriteOnTextMode('\'');
                        }

                        i += para.PositionLength + 1;
                        format.WriteOnTextMode(this.lable.SqlText[i]);
                    }
                    else
                    {
                        var item = convert.FirstOrDefault(o => o.Key.Equals(para.Name));
                        if (item == null)
                        {
                            throw new Exception(string.Format("当前在sql语句中参数为{0}的值在所提供的参数列表中找不到", para.Name));
                        }

                        format.Write(this.lable.SqlText, para.PrefixStart, para.PositionLength + 1);
                        format.AddParameter(item);
                        i += para.PositionLength + 1;
                        format.Write(this.lable.SqlText[i]);
                    }
                }
                else
                {
                    var item       = value as System.Collections.IEnumerable;
                    var ator       = item.GetEnumerator();
                    var hadA       = false;
                    var arrayLevel = 0;
                    if (format.IfTextParameter(para))
                    {
                        while (ator.MoveNext())
                        {
                            if (ator.Current == null || ator.Current == DBNull.Value)
                            {
                                continue;
                            }

                            if (hadA)
                            {
                                format.WriteOnTextMode(",");
                            }

                            //format.WriteOnTextMode('\'');
                            format.WriteOnTextMode(ator.Current.ToString());
                            //format.WriteOnTextMode('\'');
                            hadA = true;
                        }

                        i += para.PositionLength + 1;
                        format.WriteOnTextMode(this.lable.SqlText[i]);
                    }
                    else
                    {
                        format.Write(this.lable.SqlText[i]);
                        while (ator.MoveNext())
                        {
                            if (ator.Current == null || ator.Current == DBNull.Value)
                            {
                                continue;
                            }

                            var newvalue = (ator.Current == null || ator.Current == DBNull.Value) ? DBNull.Value : ator.Current;
                            var newkey   = string.Format("{0}x{1}z", para.Name, arrayLevel);

                            if (hadA)
                            {
                                format.Write(",");
                                format.Write(para.ActualPrefix);
                            }

                            format.Write(newkey);
                            arrayLevel++;

                            format.AddParameter(newkey, newvalue);
                            hadA = true;
                        }

                        i += para.PositionLength + 1;
                        format.Write(this.lable.SqlText[i]);
                    }
                }
            }
        }