~ubuntu-branches/ubuntu/saucy/f-spot/saucy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * FSpot.Query.LogicalTerm
 *
 * Author(s):
 *	Stephane Delcroix  <stephane@delcroix.org>
 *
 * This is free software. See COPYING for details.
 */

using System;
using System.Collections.Generic;
using Hyena;

namespace FSpot.Query
{
	public abstract class LogicalTerm : IQueryCondition
	{
		public abstract string SqlClause ();
	}

	public class TagTerm : LogicalTerm, IDisposable
	{
		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)
		{
			if (tagids.Length == 0)
				return null;
			if (tagids.Length == 1)
				return String.Format (" (photos.id IN (SELECT photo_id FROM photo_tags WHERE tag_id = {0})) ", tagids[0]);
			else
				return String.Format (" (photos.id IN (SELECT photo_id FROM photo_tags WHERE tag_id IN ({0}))) ", String.Join (", ", tagids));
		}

		public void Dispose ()
		{
			if (tag != null)
				tag.Dispose ();
			System.GC.SuppressFinalize (this);
		}

		~TagTerm ()
		{
			Log.DebugFormat ("Finalizer called on {0}. Should be Disposed", GetType ());
			if (tag != null)
				tag.Dispose ();
		}
	}

	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 static string SqlClause (string op, string[] items)
		{
			if (items.Length == 1)
				return items [0];
			else
				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 ());
		}
	}
}