Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed issue with user-defined type conversion operator

  • Loading branch information...
commit 8f22cd8d44bda87c9f204b9ea1db229b8a69a9f8 1 parent b70d49a
Thomas Grassauer brainless83 authored
70 src/ServiceStack.OrmLite.MySql.Tests/Expressions/UnaryExpressionsTest.cs
@@ -178,6 +178,56 @@ public void Can_select_unary_cast_variable_expression()
178 178 CollectionAssert.Contains(actual, expected);
179 179 }
180 180
  181 + [Test]
  182 + public void Can_select_unary_cast_variable_with_explicit_userdefined_type_conversion_expression()
  183 + {
  184 + // ReSharper disable ConvertToConstant.Local
  185 + var intVal = 12;
  186 + // ReSharper restore ConvertToConstant.Local
  187 +
  188 + var expected = new TestType()
  189 + {
  190 + IntColumn = 12,
  191 + BoolColumn = true,
  192 + StringColumn = "test"
  193 + };
  194 +
  195 + EstablishContext(10, expected);
  196 +
  197 + var value = new IntWrapper(intVal);
  198 +
  199 + var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == (int)value);
  200 +
  201 + Assert.IsNotNull(actual);
  202 + Assert.AreEqual(1, actual.Count);
  203 + CollectionAssert.Contains(actual, expected);
  204 + }
  205 +
  206 + [Test]
  207 + public void Can_select_unary_cast_variable_with_implicit_userdefined_type_conversion_expression()
  208 + {
  209 + // ReSharper disable ConvertToConstant.Local
  210 + var intVal = 12;
  211 + // ReSharper restore ConvertToConstant.Local
  212 +
  213 + var expected = new TestType()
  214 + {
  215 + IntColumn = 12,
  216 + BoolColumn = true,
  217 + StringColumn = "test"
  218 + };
  219 +
  220 + EstablishContext(10, expected);
  221 +
  222 + var value = new IntWrapper(intVal);
  223 +
  224 + var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == value);
  225 +
  226 + Assert.IsNotNull(actual);
  227 + Assert.AreEqual(1, actual.Count);
  228 + CollectionAssert.Contains(actual, expected);
  229 + }
  230 +
181 231 #endregion
182 232
183 233 #region method
@@ -222,4 +272,24 @@ public void Can_select_unary_cast_method_expression()
222 272
223 273 #endregion
224 274 }
  275 +
  276 + public struct IntWrapper
  277 + {
  278 + private readonly int _id;
  279 +
  280 + public IntWrapper(int projectId)
  281 + {
  282 + _id = projectId;
  283 + }
  284 +
  285 + public static explicit operator IntWrapper(int value)
  286 + {
  287 + return new IntWrapper(value);
  288 + }
  289 +
  290 + public static implicit operator int(IntWrapper value)
  291 + {
  292 + return value._id;
  293 + }
  294 + }
225 295 }
10 src/ServiceStack.OrmLite/Expressions/SqlExpressionVisitor.cs
@@ -773,9 +773,13 @@ protected virtual object VisitUnary(UnaryExpression u)
773 773 o = o + "=" + GetQuotedTrueValue();
774 774
775 775 return new PartialSqlString("NOT (" + o + ")");
776   - default:
777   - return Visit(u.Operand);
778   - }
  776 + case ExpressionType.Convert:
  777 + if (u.Method != null)
  778 + return Expression.Lambda(u).Compile().DynamicInvoke();
  779 + break;
  780 + }
  781 +
  782 + return Visit(u.Operand);
779 783
780 784 }
781 785
71 tests/ServiceStack.OrmLite.Tests/Expression/UnaryExpressionsTest.cs
@@ -178,6 +178,56 @@ public void Can_select_unary_cast_variable_expression()
178 178 CollectionAssert.Contains(actual, expected);
179 179 }
180 180
  181 + [Test]
  182 + public void Can_select_unary_cast_variable_with_explicit_userdefined_type_conversion_expression()
  183 + {
  184 + // ReSharper disable ConvertToConstant.Local
  185 + var intVal = 12;
  186 + // ReSharper restore ConvertToConstant.Local
  187 +
  188 + var expected = new TestType()
  189 + {
  190 + IntColumn = 12,
  191 + BoolColumn = true,
  192 + StringColumn = "test"
  193 + };
  194 +
  195 + EstablishContext(10, expected);
  196 +
  197 + var value = new IntWrapper(intVal);
  198 +
  199 + var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == (int)value);
  200 +
  201 + Assert.IsNotNull(actual);
  202 + Assert.AreEqual(1, actual.Count);
  203 + CollectionAssert.Contains(actual, expected);
  204 + }
  205 +
  206 + [Test]
  207 + public void Can_select_unary_cast_variable_with_implicit_userdefined_type_conversion_expression()
  208 + {
  209 + // ReSharper disable ConvertToConstant.Local
  210 + var intVal = 12;
  211 + // ReSharper restore ConvertToConstant.Local
  212 +
  213 + var expected = new TestType()
  214 + {
  215 + IntColumn = 12,
  216 + BoolColumn = true,
  217 + StringColumn = "test"
  218 + };
  219 +
  220 + EstablishContext(10, expected);
  221 +
  222 + var value = new IntWrapper(intVal);
  223 +
  224 + var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == value);
  225 +
  226 + Assert.IsNotNull(actual);
  227 + Assert.AreEqual(1, actual.Count);
  228 + CollectionAssert.Contains(actual, expected);
  229 + }
  230 +
181 231 #endregion
182 232
183 233 #region method
@@ -222,4 +272,25 @@ public void Can_select_unary_cast_method_expression()
222 272
223 273 #endregion
224 274 }
  275 +
  276 + public struct IntWrapper
  277 + {
  278 + private readonly int _id;
  279 +
  280 + public IntWrapper(int projectId)
  281 + {
  282 + _id = projectId;
  283 + }
  284 +
  285 + public static explicit operator IntWrapper(int value)
  286 + {
  287 + return new IntWrapper(value);
  288 + }
  289 +
  290 + public static implicit operator int(IntWrapper value)
  291 + {
  292 + return value._id;
  293 + }
  294 + }
  295 +
225 296 }

0 comments on commit 8f22cd8

Please sign in to comment.
Something went wrong with that request. Please try again.