f-spot r3567 - in trunk: . Tests/src Tests/src/Query src src/Query



Author: sdelcroix
Date: Fri Jan 18 10:00:07 2008
New Revision: 3567
URL: http://svn.gnome.org/viewvc/f-spot?rev=3567&view=rev

Log:
2008-01-18  Stephane Delcroix  <sdelcroix novell com>

	* src/Query/LogicalTerm.cs: not stubbed anymore.

	* src/PhotoStore.cs: ns changes.


Added:
   trunk/Tests/src/Query/
   trunk/Tests/src/Query/LogicalTerm.cs
Modified:
   trunk/ChangeLog
   trunk/Tests/src/ChangeLog
   trunk/Tests/src/Makefile
   trunk/src/PhotoStore.cs
   trunk/src/Query/LogicalTerm.cs

Modified: trunk/Tests/src/Makefile
==============================================================================
--- trunk/Tests/src/Makefile	(original)
+++ trunk/Tests/src/Makefile	Fri Jan 18 10:00:07 2008
@@ -5,7 +5,8 @@
 SOURCES = 				\
 	IBrowsableItem.cs		\
 	PhotoStore.cs			\
-	TagStore.cs
+	TagStore.cs			\
+	Query/LogicalTerm.cs		
 
 PKGS = 					\
 	-pkg:mono-nunit			\

Added: trunk/Tests/src/Query/LogicalTerm.cs
==============================================================================
--- (empty file)
+++ trunk/Tests/src/Query/LogicalTerm.cs	Fri Jan 18 10:00:07 2008
@@ -0,0 +1,37 @@
+#if ENABLE_NUNIT
+using NUnit.Framework;
+
+namespace FSpot.Query.Tests
+{
+	[TestFixture]
+	public class LogicalTermTests
+	{
+		[Test]
+		public void SomeTests ()
+		{
+			Tag t1 = new Tag (null, 1, "tag1");
+			Tag t2 = new Tag (null, 2, "tag2");
+			Tag t3 = new Tag (null, 3, "tag3");
+			Tag t4 = new Tag (null, 4, "tag4");
+			Tag t5 = new Tag (null, 5, "tag5");
+
+			TagTerm tt1 = new TagTerm (t1);
+			TagTerm tt2 = new TagTerm (t2);
+			TagTerm tt3 = new TagTerm (t3);
+			TagTerm tt4 = new TagTerm (t4);
+			TagTerm tt5 = new TagTerm (t5);
+
+			object [] tests = {
+				" (photos.id IN (SELECT photo_id FROM photo_tags WHERE tag_id = 1)) ", tt1,
+				" (photos.id IN (SELECT photo_id FROM photo_tags WHERE tag_id IN (2, 3))) ", new OrTerm (tt2, tt3),
+				" photos.tag_id IN (3, 4, 5) ", new OrTerm (tt3, tt4, tt5),
+				
+			};
+	
+			for (int i=0; i < tests.Length; i+=2) {
+				Assert.AreEqual (tests[i] as string, (tests[i+1] as LogicalTerm).SqlClause ());
+			}
+		}
+	}
+}
+#endif

Modified: trunk/src/PhotoStore.cs
==============================================================================
--- trunk/src/PhotoStore.cs	(original)
+++ trunk/src/PhotoStore.cs	Fri Jan 18 10:00:07 2008
@@ -1260,13 +1260,13 @@
 	[Obsolete ("drop this, use IQueryCondition correctly instead")]
 	public Photo [] Query (Tag [] tags, string extra_condition, DateRange range, RollSet importidrange)
 	{
-		return Query (OrTerm.FromTags(tags), extra_condition, range, importidrange, null);
+		return Query (FSpot.OrTerm.FromTags(tags), extra_condition, range, importidrange, null);
 	}
 
 	[Obsolete ("drop this, use IQueryCondition correctly instead")]
 	public Photo [] Query (Tag [] tags, string extra_condition, DateRange range, RollSet importidrange, RatingRange ratingrange)
 	{
-		return Query (OrTerm.FromTags(tags), extra_condition, range, importidrange, ratingrange);
+		return Query (FSpot.OrTerm.FromTags(tags), extra_condition, range, importidrange, ratingrange);
 	}
 
 	[Obsolete ("drop this, use IQueryCondition correctly instead")]

Modified: trunk/src/Query/LogicalTerm.cs
==============================================================================
--- trunk/src/Query/LogicalTerm.cs	(original)
+++ trunk/src/Query/LogicalTerm.cs	Fri Jan 18 10:00:07 2008
@@ -7,10 +7,172 @@
  * This is free software. See COPYING for details.
  */
 
+using System;
+using System.Collections.Generic;
+
 namespace FSpot.Query
 {
-	public class LogicalTerm
+	public abstract class LogicalTerm : IQueryCondition
+	{
+		public abstract string SqlClause ();
+	}
+
+	public class TagTerm : LogicalTerm
+	{
+		Tag tag;
+		public Tag Tag {
+			get { return tag; }
+		}
+
+		public TagTerm (Tag tag)
+		{
+			this.tag = tag;
+		}
+
+		public override string SqlClause ()
+		{
+			return SqlClause (this);
+		}
+
+		internal static string SqlClause (params TagTerm [] tags)
+		{
+			List<string> list = new List<string> (tags.Length);
+			foreach (TagTerm tag in tags)
+				list.Add (tag.Tag.Id.ToString ());
+			return SqlClause (list.ToArray ());
+		}
+
+		private static string SqlClause (string [] tagids)
+		{
+			return String.Format (" (photos.id IN (SELECT photo_id FROM photo_tags WHERE tag_id = {0})) ", String.Join (", ", tagids));
+		}
+	}
+
+	public class TextTerm : LogicalTerm
+	{
+		string text;
+		public string Text {
+			get { return text; }
+		}
+
+		string field;
+		public string Field {
+			get { return field;  }
+		}
+
+		public TextTerm (string text, string field)
+		{
+			this.text = text;
+			this.field = field;
+		}
+
+		public static OrTerm SearchMultiple (string text, params string[] fields)
+		{
+			List<TextTerm> terms = new List<TextTerm> (fields.Length);
+			foreach (string field in fields)
+				terms.Add (new TextTerm (text, field));
+			return new OrTerm (terms.ToArray ());
+		}
+
+		public override string SqlClause ()
+		{
+			return String.Format (" {0} LIKE %{1}% ", field, text);
+		}
+	}
+
+	public class NotTerm : LogicalTerm
 	{
+		LogicalTerm term;
+		public LogicalTerm Term {
+			get { return term; }
+		}
+
+		public NotTerm (LogicalTerm term)
+		{
+			this.term = term;
+		}
+
+		public override string SqlClause ()
+		{
+			return String.Format (" NOT ({0}) ", term.SqlClause ());
+		}
+	}
+
+	public abstract class NAryOperator : LogicalTerm
+	{
+		protected List<LogicalTerm> terms;
+		public LogicalTerm[] Terms {
+			get { return terms.ToArray (); }
+		}
+
+		protected string [] ToStringArray ()
+		{
+			List<string> ls = new List<string> (terms.Count);
+			foreach (LogicalTerm term in terms)
+				ls.Add (term.SqlClause ());
+			return ls.ToArray ();
+		}
+
+		public string SqlClause (string op, string[] items)
+		{
+			return " (" + String.Join (String.Format (" {0} ", op), items) + ") ";
+		}
 		
 	}
+
+	public class OrTerm : NAryOperator
+	{
+		public OrTerm (params LogicalTerm[] terms)
+		{
+			this.terms = new List<LogicalTerm> (terms.Length);
+			foreach (LogicalTerm term in terms)
+				Add (term);
+		}
+
+		private void Add (LogicalTerm term)
+		{
+			if (term is OrTerm)
+				foreach (LogicalTerm t in (term as OrTerm).terms)
+					Add (t);
+			else
+				terms.Add (term);
+		}
+
+		public override string SqlClause ()
+		{
+			List<TagTerm> tagterms = new List<TagTerm> ();
+			List<string> otherterms = new List<string> ();
+			foreach (LogicalTerm term in terms)
+				if (term is TagTerm)
+					tagterms.Add (term as TagTerm);
+				else
+					otherterms.Add (term.SqlClause ());
+			otherterms.Insert (0, TagTerm.SqlClause (tagterms.ToArray ()));
+			return SqlClause ("OR", otherterms.ToArray ());
+		}
+	}
+
+	public class AndTerm : NAryOperator
+	{
+		public AndTerm (params LogicalTerm[] terms)
+		{
+			this.terms = new List<LogicalTerm> (terms.Length);
+			foreach (LogicalTerm term in terms)
+				Add (term);
+		}
+
+		private void Add (LogicalTerm term)
+		{
+			if (term is AndTerm)
+				foreach (LogicalTerm t in (term as AndTerm).terms)
+					Add (t);
+			else
+				terms.Add (term);			
+		}
+
+		public override string SqlClause ()
+		{
+			return SqlClause ("AND", ToStringArray ());
+		}
+	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]