[f-spot] Cleanup PhotoQuery and PhotoStore



commit 22d1c9871a3757f27b842790518e93053a9c7d9e
Author: Mike Gemünde <mike gemuende de>
Date:   Wed Jun 24 22:35:58 2009 +0200

    Cleanup PhotoQuery and PhotoStore
    
    * PhotoQuery.cs: use all the IQueryconditions.
    * PhotoStore.cs: don't duplicate code

 src/PhotoQuery.cs |   38 +++++++++++++++++++++++++++++++++++-
 src/PhotoStore.cs |   55 ++++++++++++++++++++++++----------------------------
 2 files changed, 62 insertions(+), 31 deletions(-)
---
diff --git a/src/PhotoQuery.cs b/src/PhotoQuery.cs
index 1e45763..d5d84cd 100644
--- a/src/PhotoQuery.cs
+++ b/src/PhotoQuery.cs
@@ -251,10 +251,31 @@ namespace FSpot {
 		public void RequestReload ()
 		{
 			uint timer = Log.DebugTimerStart ();
-			if (untagged)
+		/*	if (untagged)
 				store.QueryToTemp (temp_table, new UntaggedCondition (), Range, RollSet, RatingRange, OrderByTime, OrderByUri);
 			else
 				store.QueryToTemp (temp_table, terms, extra_condition, Range, RollSet, RatingRange, OrderByTime, OrderByUri);
+*/
+			IQueryCondition[] condition_array;
+			
+			int i = 0;
+			if (untagged) {
+				condition_array = new IQueryCondition[conditions.Count + 1];
+				condition_array[0] = new UntaggedCondition ();
+				i = 1;
+			} else {
+				condition_array = new IQueryCondition[conditions.Count + 2];
+				condition_array[0] = new TagConditionWrapper (extra_condition);
+				condition_array[1] = new TagConditionWrapper (terms != null ? terms.SqlCondition () : null);
+				i = 2;
+			}
+			
+			foreach (IQueryCondition condition in Conditions.Values) {
+				condition_array[i] = condition;
+				i++;
+			}
+	
+			store.QueryToTemp (temp_table, condition_array);
 
 			count = -1;
 			cache = new PhotoCache (store, temp_table);
@@ -358,5 +379,20 @@ namespace FSpot {
 		{
 			ItemsChanged (this, new BrowsableEventArgs (indexes, changes));
 		}
+		
+		private class TagConditionWrapper : IQueryCondition
+		{
+			string condition;
+			
+			public TagConditionWrapper (string condition)
+			{
+				this.condition = condition;
+			}
+			
+			public string SqlClause ()
+			{
+				return condition;
+			}
+		}
 	}
 }
diff --git a/src/PhotoStore.cs b/src/PhotoStore.cs
index c584c50..dd0ce15 100644
--- a/src/PhotoStore.cs
+++ b/src/PhotoStore.cs
@@ -779,19 +779,25 @@ public class PhotoStore : DbStore<Photo> {
 	public Photo [] Query (Tag [] tags) {
 		return Query (tags, null, null, null, null);
 	}
-
-	public Photo [] Query (params IQueryCondition [] conditions)
+	
+	private string BuildQuery (params IQueryCondition [] conditions)
 	{
 		StringBuilder query_builder = new StringBuilder ("SELECT * FROM photos ");
-		
+
 		bool where_added = false;
 		foreach (IQueryCondition condition in conditions) {
 			if (condition == null)
 				continue;
 			if (condition is IOrderCondition)
 				continue;
+			
+			string sql_clause = condition.SqlClause ();
+			
+			if (sql_clause == null || sql_clause.Trim () == String.Empty)
+				continue;
+			
 			query_builder.Append (where_added ? " AND " : " WHERE ");
-			query_builder.Append (condition.SqlClause ());
+			query_builder.Append (sql_clause);
 			where_added = true;
 		}
 
@@ -801,39 +807,28 @@ public class PhotoStore : DbStore<Photo> {
 				continue;
 			if (!(condition is IOrderCondition))
 				continue;
+			
+			string sql_clause = condition.SqlClause ();
+			
+			if (sql_clause == null || sql_clause.Trim () == String.Empty)
+				continue;
+			
 			query_builder.Append (order_added ? " , " : "ORDER BY ");
-			query_builder.Append (condition.SqlClause ());
+			query_builder.Append (sql_clause);
 			order_added = true;
 		}
-		return Query (query_builder.ToString ());
+		
+		return query_builder.ToString ();
 	}
 
-	public void QueryToTemp (string temp_table, params IQueryCondition [] conditions)
+	public Photo [] Query (params IQueryCondition [] conditions)
 	{
-		StringBuilder query_builder = new StringBuilder ("SELECT * FROM photos ");
-
-		bool where_added = false;
-		foreach (IQueryCondition condition in conditions) {
-			if (condition == null)
-				continue;
-			if (condition is IOrderCondition)
-				continue;
-			query_builder.Append (where_added ? " AND " : " WHERE ");
-			query_builder.Append (condition.SqlClause ());
-			where_added = true;
-		}
+		return Query (BuildQuery (conditions));
+	}
 
-		bool order_added = false;
-		foreach (IQueryCondition condition in conditions) {
-			if (condition == null)
-				continue;
-			if (!(condition is IOrderCondition))
-				continue;
-			query_builder.Append (order_added ? " , " : "ORDER BY ");
-			query_builder.Append (condition.SqlClause ());
-			order_added = true;
-		}
-		QueryToTemp (temp_table, query_builder.ToString ());
+	public void QueryToTemp (string temp_table, params IQueryCondition [] conditions)
+	{
+		QueryToTemp (temp_table, BuildQuery (conditions));
 	}
 
 	public void QueryToTemp(string temp_table, string query)



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