Ejemplo n.º 1
0
        /// <summary>
        /// 获取 String 类型的 SQL 片断
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="dbType">数据类型</param>
        /// <param name="size">长度</param>
        /// <returns></returns>
        protected override string GetSqlValueOfString(object value, object dbType, int?size = null)
        {
            bool   unicode = SQLiteUtils.IsUnicode(dbType);
            string result  = this.EscapeQuote(value.ToString(), unicode, true);

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取 DateTimeOffset 类型的 SQL 片断
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="dbType">数据类型</param>
        /// <param name="scale">小数位</param>
        /// <returns></returns>
        protected override string GetSqlValueOfDateTimeOffset(object value, object dbType, int?scale)
        {
            // 默认精度为7
            string format = "yyyy-MM-dd HH:mm:ss.fffffff";

            if (SQLiteUtils.IsDateTimeOffset(dbType))
            {
                string s = string.Empty;
                if (scale != null && scale.Value > 0)
                {
                    s = string.Empty.PadLeft(scale.Value > 7 ? 7 : scale.Value, 'f');
                }
                if (!string.IsNullOrEmpty(s))
                {
                    format = string.Format("yyyy-MM-dd HH:mm:ss.{0}", s);
                }
            }

            string myDateTime = ((DateTimeOffset)value).DateTime.ToString(format);
            string myOffset   = ((DateTimeOffset)value).Offset.ToString(@"hh\:mm");

            myOffset = string.Format("{0}{1}", ((DateTimeOffset)value).Offset < TimeSpan.Zero ? '-' : '+', myOffset);

            string result = string.Format("'{0} {1}'", myDateTime, myOffset);

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取 DatetTime 类型的 SQL 片断
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="dbType">数据类型</param>
        /// <param name="scale">小数位</param>
        /// <returns></returns>
        protected override string GetSqlValueOfDateTime(object value, object dbType, int?scale)
        {
            // 默认精度为3
            string format = "yyyy-MM-dd HH:mm:ss.fff";

            if (SQLiteUtils.IsDate(dbType))
            {
                format = "yyyy-MM-dd";
            }
            else if (SQLiteUtils.IsDateTime(dbType))
            {
                format = "yyyy-MM-dd HH:mm:ss.fff";
            }
            else if (SQLiteUtils.IsDateTime2(dbType))
            {
                string s = string.Empty;
                format = "yyyy-MM-dd HH:mm:ss.fffffff";
                if (scale != null && scale.Value > 0)
                {
                    s = string.Empty.PadLeft(scale.Value > 7 ? 7 : scale.Value, 'f');
                }
                if (!string.IsNullOrEmpty(s))
                {
                    format = string.Format("yyyy-MM-dd HH:mm:ss.{0}", s);
                }
            }

            string result = this.EscapeQuote(((DateTime)value).ToString(format), false, false);

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取 Time 类型的 SQL 片断
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="dbType">数据类型</param>
        /// <param name="scale">小数位</param>
        /// <returns></returns>
        protected override string GetSqlValueOfTime(object value, object dbType, int?scale)
        {
            // SQLSERVER 的Time类型范围:00:00:00.0000000 到 23:59:59.9999999
            // https://docs.microsoft.com/zh-cn/sql/t-sql/data-types/time-transact-sql?view=sql-server-2017

            // 默认精度为7
            string format = @"hh\:mm\:ss\.fffffff";

            if (SQLiteUtils.IsTime(dbType))
            {
                string s = string.Empty;
                if (scale != null && scale.Value > 0)
                {
                    s = string.Empty.PadLeft(scale.Value > 7 ? 7 : scale.Value, 'f');
                }
                if (!string.IsNullOrEmpty(s))
                {
                    format = string.Format(@"hh\:mm\:ss\.{0}", s);
                }
            }

            string result = this.EscapeQuote(((TimeSpan)value).ToString(format), false, false);

            return(result);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 设置命令参数对象的 DbType属性
 /// </summary>
 /// <param name="parameter">命令参数对象</param>
 /// <param name="dbType">DbType属性</param>
 public static void DbType(this SQLiteParameter parameter, object dbType)
 {
     if (dbType != null)
     {
         if (dbType is DbType)
         {
             parameter.DbType = (DbType)dbType;
         }
         else
         {
             SQLiteUtils.ThrowException(dbType);
         }
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 是否日期+时间+精度类型
 /// </summary>
 /// <param name="dbType">DbType属性</param>
 public static bool IsDateTime2(object dbType)
 {
     if (dbType == null)
     {
         return(false);
     }
     else if (dbType is DbType)
     {
         return(((DbType)dbType) == System.Data.DbType.DateTime2);
     }
     else
     {
         return(SQLiteUtils.ThrowException(dbType));
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 访问 Contains 方法
        /// </summary>
        /// <param name="m">方法表达式</param>
        protected override Expression VisitStringContains(MethodCallExpression m)
        {
            _visitor.Visit(m.Object);
            if (this.NotOperands != null && this.NotOperands.Contains(m))
            {
                _builder.Append(" NOT");
            }
            _builder.Append(" LIKE ");
            if (m.Arguments[0].CanEvaluate())
            {
                ColumnAttribute column    = null;
                bool            isUnicode = SQLiteUtils.IsUnicode(_visitedMark.Current, out column);
                string          value     = _constor.GetSqlValue(m.Arguments[0].Evaluate().Value, _builder.TranslateContext, column);
                if (!_builder.Parameterized && value != null)
                {
                    value = value.TrimStart('N').Trim('\'');
                }

                if (_builder.Parameterized)
                {
                    _builder.Append("'%' || ");
                    _builder.Append(value);
                    _builder.Append(" || '%'");
                }
                else
                {
                    if (isUnicode)
                    {
                        _builder.Append('N');
                    }
                    _builder.Append("'%");
                    _builder.Append(value);
                    _builder.Append("%'");
                }
            }
            else
            {
                _builder.Append("('%' || ");
                _visitor.Visit(m.Arguments[0]);
                _builder.Append(" || '%')");
            }
            return(m);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 检查字段或属性成员声明的 DbType 是否为 Unicode 数据类型
        /// </summary>
        /// <param name="m">将要检查的字段或属性成员</param>
        /// <param name="column">字段或属性成员显示声明的列特性</param>
        /// <returns></returns>
        public static bool IsUnicode(MemberVisitedStack.VisitedMember m, out ColumnAttribute column)
        {
            column = m != null?TypeUtils.GetColumnAttribute(m.Member, m.ReflectedType) : null;

            return(SQLiteUtils.IsUnicode(column == null ? null : column.DbType));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 检查字段或属性成员声明的 DbType 是否为 Unicode 数据类型
        /// </summary>
        /// <param name="m">将要检查的字段或属性成员</param>
        /// <returns></returns>
        public static bool IsUnicode(MemberVisitedStack.VisitedMember m)
        {
            ColumnAttribute column = null;

            return(SQLiteUtils.IsUnicode(m, out column));
        }