banshee r5033 - in trunk/banshee: . src/Libraries/Hyena/Hyena.Query
- From: gburt svn gnome org
- To: svn-commits-list gnome org
- Subject: banshee r5033 - in trunk/banshee: . src/Libraries/Hyena/Hyena.Query
- Date: Wed, 11 Feb 2009 05:30:06 +0000 (UTC)
Author: gburt
Date: Wed Feb 11 05:30:06 2009
New Revision: 5033
URL: http://svn.gnome.org/viewvc/banshee?rev=5033&view=rev
Log:
2009-02-10 Gabriel Burt <gabriel burt gmail com>
Patch from AndrÃs G. Aragoneses fixing bug with string matching functions
that use the SQL 'like' command not escaping % and _ properly.
* DateQueryValue.cs:
* ExactStringQueryValue.cs:
* IntegerQueryValue.cs:
* NullQueryValue.cs:
* RelativeTimeSpanQueryValue.cs:
* TimeSpanQueryValue.cs: Change all to override the ToSql (op) instead of
ToSql () method.
* QueryField.cs: Call ToSql w/ the op we already have at hand.
* QueryValue.cs: Add new abstract ToSql (op) method and change the ToSql
method to call it with a null op.
* StringQueryValue.cs: Fix bug with not escaping % and _ characters which
are treated specially by SQLite in LIKE expressions (BGO #570312)
Modified:
trunk/banshee/ChangeLog
trunk/banshee/src/Libraries/Hyena/Hyena.Query/DateQueryValue.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/ExactStringQueryValue.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/IntegerQueryValue.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/NullQueryValue.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryField.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryValue.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/RelativeTimeSpanQueryValue.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/StringQueryValue.cs
trunk/banshee/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/DateQueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/DateQueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/DateQueryValue.cs Wed Feb 11 05:30:06 2009
@@ -106,7 +106,7 @@
}
}
- public override string ToSql ()
+ public override string ToSql (Operator op)
{
return DateTimeUtil.FromDateTime (value).ToString (System.Globalization.CultureInfo.InvariantCulture);
}
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/ExactStringQueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/ExactStringQueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/ExactStringQueryValue.cs Wed Feb 11 05:30:06 2009
@@ -31,7 +31,7 @@
// A query value that requires the string match exactly
public class ExactStringQueryValue : StringQueryValue
{
- public override string ToSql ()
+ public override string ToSql (Operator op)
{
return String.IsNullOrEmpty (value) ? null : value.Replace ("'", "''").ToLower ();
}
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/IntegerQueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/IntegerQueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/IntegerQueryValue.cs Wed Feb 11 05:30:06 2009
@@ -102,7 +102,7 @@
get { return Int64.MaxValue; }
}
- public override string ToSql ()
+ public override string ToSql (Operator op)
{
return Convert.ToString (value, System.Globalization.CultureInfo.InvariantCulture);
}
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/NullQueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/NullQueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/NullQueryValue.cs Wed Feb 11 05:30:06 2009
@@ -81,7 +81,7 @@
{
}
- public override string ToSql ()
+ public override string ToSql (Operator op)
{
return null;
}
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryField.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryField.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryField.cs Wed Feb 11 05:30:06 2009
@@ -143,7 +143,7 @@
public string ToSql (Operator op, QueryValue qv)
{
- string value = qv.ToSql () ?? String.Empty;
+ string value = qv.ToSql (op) ?? String.Empty;
if (op == null) op = qv.OperatorSet.First;
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/QueryValue.cs Wed Feb 11 05:30:06 2009
@@ -141,6 +141,11 @@
return Value.ToString ();
}
- public abstract string ToSql ();
+ public string ToSql ()
+ {
+ return ToSql (null);
+ }
+
+ public abstract string ToSql (Operator op);
}
}
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/RelativeTimeSpanQueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/RelativeTimeSpanQueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/RelativeTimeSpanQueryValue.cs Wed Feb 11 05:30:06 2009
@@ -78,7 +78,7 @@
node.SetAttribute ("type", "rel");
}
- public override string ToSql ()
+ public override string ToSql (Operator op)
{
return DateTimeUtil.FromDateTime (DateTime.Now + TimeSpan.FromSeconds ((double) offset)).ToString (System.Globalization.CultureInfo.InvariantCulture);
}
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/StringQueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/StringQueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/StringQueryValue.cs Wed Feb 11 05:30:06 2009
@@ -38,12 +38,14 @@
{
public class StringQueryValue : QueryValue
{
- public static readonly Operator Contains = new Operator ("contains", Catalog.GetString ("contains"), "LIKE '%{0}%'", ":");
- public static readonly Operator DoesNotContain = new Operator ("doesNotContain", Catalog.GetString ("doesn't contain"), "NOT LIKE '%{0}%'", true, "!:");
+ private const string ESCAPE_CLAUSE = " ESCAPE '\\'";
+
+ public static readonly Operator Contains = new Operator ("contains", Catalog.GetString ("contains"), "LIKE '%{0}%'" + ESCAPE_CLAUSE, ":");
+ public static readonly Operator DoesNotContain = new Operator ("doesNotContain", Catalog.GetString ("doesn't contain"), "NOT LIKE '%{0}%'" + ESCAPE_CLAUSE, true, "!:");
public static readonly Operator Equal = new Operator ("equals", Catalog.GetString ("is"), "= '{0}'", "==");
public static readonly Operator NotEqual = new Operator ("notEqual", Catalog.GetString ("is not"), "!= '{0}'", true, "!=");
- public static readonly Operator StartsWith = new Operator ("startsWith", Catalog.GetString ("starts with"), "LIKE '{0}%'", "=");
- public static readonly Operator EndsWith = new Operator ("endsWith", Catalog.GetString ("ends with"), "LIKE '%{0}'", ":=");
+ public static readonly Operator StartsWith = new Operator ("startsWith", Catalog.GetString ("starts with"), "LIKE '{0}%'" + ESCAPE_CLAUSE, "=");
+ public static readonly Operator EndsWith = new Operator ("endsWith", Catalog.GetString ("ends with"), "LIKE '%{0}'" + ESCAPE_CLAUSE, ":=");
protected string value;
@@ -77,11 +79,30 @@
ParseUserQuery (str);
}
- public override string ToSql ()
+ public override string ToSql (Operator op)
{
+ if (String.IsNullOrEmpty (value))
+ return null;
+
+ string ret = null;
+
// SearchKey() removes ' anyway, but it's escaped again so proper
// SQL behavior isn't dependent on search behavior.
- return Hyena.StringUtil.SearchKey (value).Replace ("'", "''");
+ ret = Hyena.StringUtil.SearchKey (value).Replace ("'", "''");
+
+ if (op == Contains || op == DoesNotContain ||
+ op == StartsWith || op == EndsWith) {
+ ret = EscapeLike (ret);
+ }
+
+ return ret;
+ }
+
+ private static string EscapeLike (string orig)
+ {
+ return orig.Replace ("\\", "\\\\")
+ .Replace ("%", "\\%")
+ .Replace ("_", "\\_");
}
}
}
Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs (original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs Wed Feb 11 05:30:06 2009
@@ -158,7 +158,7 @@
node.SetAttribute ("factor", factor.ToString ());
}
- public override string ToSql ()
+ public override string ToSql (Operator op)
{
return Convert.ToString (offset * 1000, System.Globalization.CultureInfo.InvariantCulture);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]