Fixing HQL encoding

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2013-06-07 18:23:23 -07:00
parent c2ef1f3baf
commit d8c720c2d2
2 changed files with 21 additions and 10 deletions

View File

@@ -150,6 +150,9 @@ namespace Orchard.Tests.ContentManagement {
result = queryWhere(x => x.Like("StringStuff", "bc", HqlMatchMode.Anywhere)); result = queryWhere(x => x.Like("StringStuff", "bc", HqlMatchMode.Anywhere));
Assert.That(result.Count(), Is.EqualTo(1)); Assert.That(result.Count(), Is.EqualTo(1));
result = queryWhere(x => x.Like("StringStuff", "bc'", HqlMatchMode.Anywhere));
Assert.That(result.Count(), Is.EqualTo(0));
result = queryWhere(x => x.Like("StringStuff", "ab", HqlMatchMode.Anywhere)); result = queryWhere(x => x.Like("StringStuff", "ab", HqlMatchMode.Anywhere));
Assert.That(result.Count(), Is.EqualTo(1)); Assert.That(result.Count(), Is.EqualTo(1));

View File

@@ -119,22 +119,30 @@ namespace Orchard.ContentManagement {
return from object value in values select FormatValue(value, quoteStrings); return from object value in values select FormatValue(value, quoteStrings);
} }
public static string FormatValue(string value, bool quoteStrings = true) {
return FormatValue((object)value, quoteStrings);
}
public static string FormatValue(object value, bool quoteStrings = true) { public static string FormatValue(object value, bool quoteStrings = true) {
var typeCode = Type.GetTypeCode(value.GetType()); var typeCode = Type.GetTypeCode(value.GetType());
switch (typeCode) { switch (typeCode) {
case TypeCode.String: case TypeCode.String:
if (quoteStrings) { if (quoteStrings) {
return String.Concat("'", Convert.ToString(value, CultureInfo.InvariantCulture), "'"); return String.Concat("'", EncodeQuotes(Convert.ToString(value, CultureInfo.InvariantCulture)), "'");
} }
return Convert.ToString(value, CultureInfo.InvariantCulture); return EncodeQuotes(Convert.ToString(value, CultureInfo.InvariantCulture));
case TypeCode.DateTime: case TypeCode.DateTime:
// convert the date time to a valid string representation for Hql // convert the date time to a valid string representation for Hql
var sortableDateTime = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); var sortableDateTime = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
return quoteStrings ? String.Concat("'", sortableDateTime, "'") : sortableDateTime; return quoteStrings ? String.Concat("'", EncodeQuotes(sortableDateTime), "'") : sortableDateTime;
} }
return Convert.ToString(value, CultureInfo.InvariantCulture); return EncodeQuotes(Convert.ToString(value, CultureInfo.InvariantCulture));
}
private static string EncodeQuotes(string value) {
return value.Replace("'", "''");
} }
public static IHqlCriterion AllEq(IDictionary propertyNameValues) { public static IHqlCriterion AllEq(IDictionary propertyNameValues) {
@@ -248,20 +256,20 @@ namespace Orchard.ContentManagement {
public static BinaryExpression Like(string propertyName, string value, HqlMatchMode matchMode) { public static BinaryExpression Like(string propertyName, string value, HqlMatchMode matchMode) {
switch (matchMode) { switch (matchMode) {
case HqlMatchMode.Start: case HqlMatchMode.Start:
value = "'" + value + "%'"; value = "'" + FormatValue(value, false) + "%'";
break; break;
case HqlMatchMode.Exact: case HqlMatchMode.Exact:
value = "'" + value + "'"; value = "'" + FormatValue(value, false) + "'";
break; break;
case HqlMatchMode.Anywhere: case HqlMatchMode.Anywhere:
value = "'%" + value + "%'"; value = "'%" + FormatValue(value, false) + "%'";
break; break;
case HqlMatchMode.End: case HqlMatchMode.End:
value = "'%" + value + "'"; value = "'%" + FormatValue(value, false) + "'";
break; break;
} }
return new BinaryExpression("like", propertyName, FormatValue((object)value, false)); return new BinaryExpression("like", propertyName, value);
} }
public static IHqlCriterion Lt(string propertyName, object value) { public static IHqlCriterion Lt(string propertyName, object value) {