public override string ToQuotedString(Type fieldType, object value)
        {
            var isEnumAsInt = fieldType.HasAttribute <EnumAsIntAttribute>();

            if (isEnumAsInt)
            {
                return(this.ConvertNumber(Enum.GetUnderlyingType(fieldType), value).ToString());
            }

            var isEnumFlags = fieldType.IsEnumFlags() ||
                              (!fieldType.IsEnum && fieldType.IsNumericType()); //i.e. is real int && not Enum

            if (!isEnumFlags && long.TryParse(value.ToString(), out var enumValue))
            {
                value = Enum.ToObject(fieldType, enumValue);
            }

            var enumString = DialectProvider.StringSerializer.SerializeToString(value);

            if (enumString == null || enumString == "null")
            {
                enumString = value.ToString();
            }

            return(!isEnumFlags
                ? DialectProvider.GetQuotedValue(enumString.Trim('"'))
                : enumString);
        }
        public override string ToQuotedString(Type fieldType, object value)
        {
            var dateTime = (DateTime)value;

            if (DateStyle == DateTimeKind.Unspecified)
            {
                dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Local);
            }
            else if (DateStyle == DateTimeKind.Local && dateTime.Kind == DateTimeKind.Unspecified)
            {
                dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc).ToLocalTime();
            }
            else if (DateStyle == DateTimeKind.Utc)
            {
                dateTime = dateTime.Kind == DateTimeKind.Local
                    ? DateTime.SpecifyKind(dateTime, DateTimeKind.Local).ToUniversalTime()
                    : DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);

                return(DialectProvider.GetQuotedValue(dateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), typeof(string)));
            }

            var dateStr = DateTimeSerializer.ToLocalXsdDateTimeString(dateTime);

            dateStr = dateStr.Replace("T", " ");
            const int tzPos       = 6; //"-00:00".Length;
            var       timeZoneMod = dateStr.Substring(dateStr.Length - tzPos, 1);

            if (timeZoneMod == "+" || timeZoneMod == "-")
            {
                dateStr = dateStr.Substring(0, dateStr.Length - tzPos);
            }

            return(DialectProvider.GetQuotedValue(dateStr, typeof(string)));
        }
        public override string ToQuotedString(Type fieldType, object value)
        {
            if (fieldType.HasAttribute <EnumAsIntAttribute>())
            {
                return(this.ConvertNumber(fieldType.GetEnumUnderlyingType(), value).ToString());
            }

            if (value is int && !fieldType.IsEnumFlags())
            {
                value = fieldType.GetEnumName(value);
            }

            if (fieldType.IsEnum)
            {
                var enumValue = DialectProvider.StringSerializer.SerializeToString(value);
                // Oracle stores empty strings in varchar columns as null so match that behavior here
                if (enumValue == null)
                {
                    return(null);
                }
                enumValue = DialectProvider.GetQuotedValue(enumValue.Trim('"'));
                return(enumValue == "''"
                    ? "null"
                    : enumValue);
            }
            return(base.ToQuotedString(fieldType, value));
        }
Пример #4
0
        /// <summary>
        /// Quoted Value in SQL Statement
        /// </summary>
        /// <param name="fieldType">Type of the field.</param>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public override string ToQuotedString(Type fieldType, object value)
        {
            var enumKind = GetEnumKind(fieldType);

            if (enumKind == EnumKind.Int)
            {
                return(this.ConvertNumber(Enum.GetUnderlyingType(fieldType), value).ToString());
            }

            if (enumKind == EnumKind.Char)
            {
                return(DialectProvider.GetQuotedValue(ToCharValue(value).ToString()));
            }

            var isEnumFlags = fieldType.IsEnumFlags() ||
                              !fieldType.IsEnum && fieldType.IsNumericType(); //i.e. is real int && not Enum

            if (!isEnumFlags && long.TryParse(value.ToString(), out var enumValue))
            {
                value = Enum.ToObject(fieldType, enumValue);
            }

            var enumString = enumKind == EnumKind.EnumMember
                ? value.ToString()
                : DialectProvider.StringSerializer.SerializeToString(value);

            if (enumString == null || enumString == "null")
            {
                enumString = value.ToString();
            }

            return(!isEnumFlags
                ? DialectProvider.GetQuotedValue(enumString.Trim('"'))
                : enumString);
        }
        public virtual string DateTimeFmt(DateTime dateTime, string dateTimeFormat)
        {
            if (DateStyle == DateTimeKind.Utc && dateTime.Kind == DateTimeKind.Local)
                dateTime = dateTime.ToUniversalTime();

            if (DateStyle == DateTimeKind.Local && dateTime.Kind != DateTimeKind.Local)
                dateTime = dateTime.ToLocalTime();

            return DialectProvider.GetQuotedValue(dateTime.ToString(dateTimeFormat, CultureInfo.InvariantCulture), typeof(string));
        }
Пример #6
0
        public override object GetValue(object value, Type type)
        {
            if ((!OrmLiteConfig.UseParameterizeSqlExpressions) || SkipParameterizationForThisExpression)
            {
                return(DialectProvider.GetQuotedValue(value, type));
            }

            var paramValue = DialectProvider.GetParamValue(value, type);

            return(paramValue ?? "null");
        }
Пример #7
0
        public override string ToUpdateStatement(T item, bool excludeDefaults = false)
        {
            var setFields = new StringBuilder();

            foreach (var fieldDef in ModelDef.FieldDefinitions)
            {
                if (fieldDef.ShouldSkipUpdate())
                {
                    continue;
                }
                if (fieldDef.IsRowVersion)
                {
                    continue;
                }
                if (UpdateFields.Count > 0 &&
                    !UpdateFields.Contains(fieldDef.Name) ||
                    fieldDef.AutoIncrement)
                {
                    continue;                            // added
                }
                var value = fieldDef.GetValue(item);
                if (excludeDefaults &&
                    (value == null || (!fieldDef.IsNullable && value.Equals(value.GetType().GetDefaultValue()))))
                {
                    continue;
                }

                fieldDef.GetQuotedValue(item, DialectProvider);

                if (setFields.Length > 0)
                {
                    setFields.Append(", ");
                }

                setFields
                .Append(DialectProvider.GetQuotedColumnName(fieldDef.FieldName))
                .Append("=")
                .Append(DialectProvider.GetQuotedValue(value, fieldDef.FieldType));
            }

            if (setFields.Length == 0)
            {
                throw new ArgumentException("No non-null or non-default values were provided for type: " + typeof(T).Name);
            }

            return(string.Format("UPDATE {0} SET {1} {2}",
                                 base.DialectProvider.GetQuotedTableName(ModelDef), setFields, WhereExpression));
        }
Пример #8
0
        public override string ToQuotedString(Type fieldType, object value)
        {
            if (value is int && !fieldType.IsEnumFlags())
            {
                value = fieldType.GetEnumName(value);
            }

            var enumValue = DialectProvider.StringSerializer.SerializeToString(value);

            // Oracle stores empty strings in varchar columns as null so match that behavior here
            if (enumValue == null)
            {
                return(null);
            }
            enumValue = DialectProvider.GetQuotedValue(enumValue.Trim('"'));
            return(enumValue == "''"
                ? "null"
                : enumValue);
        }
Пример #9
0
        public override string ToQuotedString(Type fieldType, object value)
        {
            var isEnumFlags = fieldType.IsEnumFlags() ||
                              (!fieldType.IsEnum && fieldType.IsNumericType()); //i.e. is real int && not Enum

            long enumValue;

            if (!isEnumFlags && long.TryParse(value.ToString(), out enumValue))
            {
                value = Enum.ToObject(fieldType, enumValue);
            }

            var enumString = DialectProvider.StringSerializer.SerializeToString(value);

            if (!isEnumFlags)
            {
                return(DialectProvider.GetQuotedValue(enumString.Trim('"')));
            }

            return(enumString);
        }
Пример #10
0
        public override string ToQuotedString(Type fieldType, object value)
        {
            var guid = (Guid)value;

            return(DialectProvider.GetQuotedValue(guid.ToString("d"), typeof(string)));
        }
 public override string ToQuotedString(Type fieldType, object value)
 {
     return(DialectProvider.GetQuotedValue(DialectProvider.StringSerializer.SerializeToString(value)));
 }
 /// <summary>
 /// Quoted Value in SQL Statement
 /// </summary>
 public virtual string ToQuotedString(Type fieldType, object value)
 {
     return(DialectProvider.GetQuotedValue(value.ToString()));
 }
Пример #13
0
 public override object GetValue(object value, Type type)
 {
     return(SkipParameterizationForThisExpression
         ? DialectProvider.GetQuotedValue(value, type)
         : DialectProvider.GetParamValue(value, type));
 }
Пример #14
0
        protected override object VisitBinary(BinaryExpression b)
        {
            object left, right;
            var    operand = BindOperant(b.NodeType); //sep= " " ??

            if (operand == "AND" || operand == "OR")
            {
                var m = b.Left as MemberExpression;
                if (m != null && m.Expression != null &&
                    m.Expression.NodeType == ExpressionType.Parameter)
                {
                    left = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue()));
                }
                else
                {
                    left = Visit(b.Left);
                }

                m = b.Right as MemberExpression;
                if (m != null && m.Expression != null &&
                    m.Expression.NodeType == ExpressionType.Parameter)
                {
                    right = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue()));
                }
                else
                {
                    right = Visit(b.Right);
                }

                if (left as PartialSqlString == null && right as PartialSqlString == null)
                {
                    var result = Expression.Lambda(b).Compile().DynamicInvoke();
                    return(new PartialSqlString(base.DialectProvider.GetQuotedValue(result, result.GetType())));
                }

                if (left as PartialSqlString == null)
                {
                    left = ((bool)left) ? GetTrueExpression() : GetFalseExpression();
                }
                if (right as PartialSqlString == null)
                {
                    right = ((bool)right) ? GetTrueExpression() : GetFalseExpression();
                }
            }
            else
            {
                left  = Visit(b.Left);
                right = Visit(b.Right);

                var leftEnum  = left as EnumMemberAccess;
                var rightEnum = right as EnumMemberAccess;

                var rightNeedsCoercing = leftEnum != null && rightEnum == null;
                var leftNeedsCoercing  = rightEnum != null && leftEnum == null;

                if (rightNeedsCoercing)
                {
                    var rightPartialSql = right as PartialSqlString;
                    if (rightPartialSql == null)
                    {
                        right = GetValue(right, leftEnum.EnumType);
                    }
                }
                else if (leftNeedsCoercing)
                {
                    var leftPartialSql = left as PartialSqlString;
                    if (leftPartialSql == null)
                    {
                        left = DialectProvider.GetQuotedValue(left, rightEnum.EnumType);
                    }
                }
                else if (left as PartialSqlString == null && right as PartialSqlString == null)
                {
                    var result = Expression.Lambda(b).Compile().DynamicInvoke();
                    return(result);
                }
                else if (left as PartialSqlString == null)
                {
                    left = DialectProvider.GetQuotedValue(left, left != null ? left.GetType() : null);
                }
                else if (right as PartialSqlString == null)
                {
                    right = GetValue(right, right != null ? right.GetType() : null);
                }
            }

            if (operand == "=" && right.ToString().EqualsIgnoreCase("null"))
            {
                operand = "is";
            }
            else if (operand == "<>" && right.ToString().EqualsIgnoreCase("null"))
            {
                operand = "is not";
            }
            else if (operand == "=" || operand == "<>")
            {
                if (IsTrueExpression(right))
                {
                    right = GetQuotedTrueValue();
                }
                else if (IsFalseExpression(right))
                {
                    right = GetQuotedFalseValue();
                }

                if (IsTrueExpression(left))
                {
                    left = GetQuotedTrueValue();
                }
                else if (IsFalseExpression(left))
                {
                    left = GetQuotedFalseValue();
                }
            }

            switch (operand)
            {
            case "MOD":
            case "COALESCE":
                return(new PartialSqlString(string.Format("{0}({1},{2})", operand, left, right)));

            default:
                return(new PartialSqlString("(" + left + Sep + operand + Sep + right + ")"));
            }
        }
Пример #15
0
 public override object GetValue(object value, Type type)
 {
     return(((!OrmLiteConfig.UseParameterizeSqlExpressions) || SkipParameterizationForThisExpression)
         ? DialectProvider.GetQuotedValue(value, type)
         : DialectProvider.GetParamValue(value, type));
 }