Exemplo 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 = OracleUtils.IsUnicode(dbType);
            string result  = this.EscapeQuote(value.ToString(), unicode, true);

            return(result);
        }
Exemplo 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)
        {
            // 默认精度为6
            string format = "yyyy-MM-dd HH:mm:ss.ffffff";

            if (OracleUtils.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("TO_TIMESTAMP_TZ('{0} {1}','yyyy-mm-dd hh24:mi:ss.ff tzh:tzm')", myDateTime, myOffset);

            return(result);
        }
Exemplo 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)
        {
            DateTime date = (DateTime)value;

            // 默认精度6
            string format = "yyyy-MM-dd HH:mm:ss.ffffff";

            if (OracleUtils.IsDate(dbType))
            {
                format = "yyyy-MM-dd";
            }
            else if (OracleUtils.IsDateTime(dbType) || OracleUtils.IsDateTime2(dbType))
            {
                string s = string.Empty;
                format = "yyyy-MM-dd HH:mm:ss.ffffff";
                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);

            result = string.Format("TO_TIMESTAMP({0},'yyyy-mm-dd hh24:mi:ss.ff')", result);
            return(result);
        }
Exemplo 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)
        {
            // https://docs.oracle.com/en/database/oracle/oracle-database/12.2/nlspg/datetime-data-types-and-time-zone-support.html#GUID-FD8C41B7-8CDC-4D02-8E6B-5250416BC17D

            TimeSpan ts = (TimeSpan)value;
            // 默认精度为6
            string format = @"hh\:mm\:ss\.ffffff";

            if (OracleUtils.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 = ts.ToString(format);

            result = string.Format("{0} {1}", ts.Days, result);
            result = this.EscapeQuote(result, false, false);
            result = string.Format("TO_DSINTERVAL({0})", result);
            return(result);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 设置命令参数对象的 DbType属性
 /// </summary>
 /// <param name="parameter">命令参数对象</param>
 /// <param name="dbType">DbType属性</param>
 public static void DbType(this OracleParameter parameter, object dbType)
 {
     if (dbType != null)
     {
         if (dbType is DbType)
         {
             parameter.DbType = (DbType)dbType;
         }
         else if (dbType is OracleDbType)
         {
             parameter.OracleDbType = (OracleDbType)dbType;
         }
         else
         {
             OracleUtils.ThrowException(dbType);
         }
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 是否日期+时间类型
 /// </summary>
 /// <param name="dbType">DbType属性</param>
 public static bool IsDateTime(object dbType)
 {
     if (dbType == null)
     {
         return(false);
     }
     else if (dbType is DbType)
     {
         return(((DbType)dbType) == System.Data.DbType.DateTime);
     }
     else if (dbType is OracleDbType)
     {
         return(((OracleDbType)dbType) == OracleDbType.TimeStamp);
     }
     else
     {
         return(OracleUtils.ThrowException(dbType));
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// 检查是否Unicode数据类型
 /// </summary>
 /// <param name="dbType">DbType属性</param>
 public static bool IsUnicode(object dbType)
 {
     if (dbType == null)
     {
         return(true);
     }
     else if (dbType is DbType)
     {
         return(((DbType)dbType) == System.Data.DbType.String || ((DbType)dbType) == System.Data.DbType.StringFixedLength);
     }
     else if (dbType is OracleDbType)
     {
         return(((OracleDbType)dbType) == OracleDbType.NVarchar2 || ((OracleDbType)dbType) == OracleDbType.NChar || ((OracleDbType)dbType) == OracleDbType.NClob);
     }
     else
     {
         return(OracleUtils.ThrowException(dbType));
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// 访问 EndWidth 方法
        /// </summary>
        /// <param name="m">方法表达式</param>
        protected override Expression VisitEndsWith(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 = OracleUtils.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);
        }
Exemplo n.º 9
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(OracleUtils.IsUnicode(column == null ? null : column.DbType));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 检查字段或属性成员声明的 DbType 是否为 Unicode 数据类型
        /// </summary>
        /// <param name="m">将要检查的字段或属性成员</param>
        /// <returns></returns>
        public static bool IsUnicode(MemberVisitedStack.VisitedMember m)
        {
            ColumnAttribute column = null;

            return(OracleUtils.IsUnicode(m, out column));
        }
Exemplo n.º 11
0
        /// <summary>
        /// 访问 ToString 方法
        /// </summary>
        /// <param name="node">即将访问的表达式</param>
        protected override Expression VisitToStringImpl(Expression node)
        {
            // => a.ID.ToString()
            // 字符串不进行转换
            if (node == null || node.Type == typeof(string))
            {
                return(_visitor.Visit(node));
            }

            ColumnAttribute column    = null;
            bool            isUnicode = OracleUtils.IsUnicode(_visitedMark.Current, out column);
            bool            isBytes   = node.Type == typeof(byte[]);
            bool            isDate    = node.Type == typeof(DateTime) ||
                                        node.Type == typeof(DateTime?) ||
                                        node.Type == typeof(TimeSpan) ||
                                        node.Type == typeof(TimeSpan?) ||
                                        node.Type == typeof(DateTimeOffset) ||
                                        node.Type == typeof(DateTimeOffset?);

            if (!isBytes)
            {
                if (isUnicode)
                {
                    _builder.Append("TO_NCHAR(");
                }
                else
                {
                    _builder.Append("TO_CHAR(");
                }
            }

            // 其它类型转字符串
            if (isDate)
            {
                _visitor.Visit(node);

                string          format = string.Empty;
                ColumnAttribute c      = _visitedMark.Current != null?TypeUtils.GetColumnAttribute(_visitedMark.Current.Member, _visitedMark.Current.ReflectedType) : null;

                if (c != null && OracleUtils.IsDate(c.DbType))
                {
                    format = "yyyy-mm-dd";
                }
                else if (c != null && (OracleUtils.IsDateTime(c.DbType) || OracleUtils.IsDateTime2(c.DbType)))
                {
                    format = "yyyy-mm-dd hh24:mi:ss.ff";
                }
                else if (c != null && OracleUtils.IsDateTimeOffset(c.DbType))
                {
                    format = "yyyy-mm-dd hh24:mi:ss.ff tzh:tzm";
                }

                // 没有显式指定数据类型,则根据表达式的类型来判断
                if (string.IsNullOrEmpty(format))
                {
                    if (node.Type == typeof(DateTime) || node.Type == typeof(DateTime?))
                    {
                        format = "yyyy-mm-dd hh24:mi:ss.ff";
                    }
                    else if (node.Type == typeof(DateTimeOffset) || node.Type == typeof(DateTimeOffset?))
                    {
                        format = "yyyy-mm-dd hh24:mi:ss.ff tzh:tzm";
                    }
                }

                if (!string.IsNullOrEmpty(format))
                {
                    _builder.Append(",'");
                    _builder.Append(format);
                    _builder.Append("'");
                }
            }
            else if (isBytes)
            {
                _builder.Append("RTRIM(DBMS_LOB.SUBSTR(");
                _visitor.Visit(node);
                _builder.Append(')');
            }
            else if (node.Type == typeof(Guid))
            {
                _builder.Append("REGEXP_REPLACE(REGEXP_REPLACE(");
                _visitor.Visit(node);
                _builder.Append(@",'(.{8})(.{4})(.{4})(.{4})(.{12})', '\1-\2-\3-\4-\5'),'(.{2})(.{2})(.{2})(.{2}).(.{2})(.{2}).(.{2})(.{2})(.{18})','\4\3\2\1-\6\5-\8\7\9')");
            }
            else
            {
                _visitor.Visit(node);
            }

            _builder.Append(')');
            return(node);
        }