1 : /* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Places code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : *
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_places_SQLFunctions_h_
41 : #define mozilla_places_SQLFunctions_h_
42 :
43 : /**
44 : * This file contains functions that Places adds to the database handle that can
45 : * be accessed by SQL queries.
46 : */
47 :
48 : #include "mozIStorageFunction.h"
49 :
50 : class mozIStorageConnection;
51 :
52 : namespace mozilla {
53 : namespace places {
54 :
55 : ////////////////////////////////////////////////////////////////////////////////
56 : //// AutoComplete Matching Function
57 :
58 : /**
59 : * This function is used to determine if a given set of data should match an
60 : * AutoComplete query.
61 : *
62 : * In SQL, you'd use it in the WHERE clause like so:
63 : * WHERE AUTOCOMPLETE_MATCH(aSearchString, aURL, aTitle, aTags, aVisitCount,
64 : * aTyped, aBookmark, aOpenPageCount, aMatchBehavior,
65 : * aSearchBehavior)
66 : *
67 : * @param aSearchString
68 : * The string to compare against.
69 : * @param aURL
70 : * The URL to test for an AutoComplete match.
71 : * @param aTitle
72 : * The title to test for an AutoComplete match.
73 : * @param aTags
74 : * The tags to test for an AutoComplete match.
75 : * @param aVisitCount
76 : * The number of visits aURL has.
77 : * @param aTyped
78 : * Indicates if aURL is a typed URL or not. Treated as a boolean.
79 : * @param aBookmark
80 : * Indicates if aURL is a bookmark or not. Treated as a boolean.
81 : * @param aOpenPageCount
82 : * The number of times aURL has been registered as being open. (See
83 : * mozIPlacesAutoComplete::registerOpenPage.)
84 : * @param aMatchBehavior
85 : * The match behavior to use for this search.
86 : * @param aSearchBehavior
87 : * A bitfield dictating the search behavior.
88 : */
89 : class MatchAutoCompleteFunction : public mozIStorageFunction
90 267 : {
91 : public:
92 : NS_DECL_ISUPPORTS
93 : NS_DECL_MOZISTORAGEFUNCTION
94 :
95 : /**
96 : * Registers the function with the specified database connection.
97 : *
98 : * @param aDBConn
99 : * The database connection to register with.
100 : */
101 : static nsresult create(mozIStorageConnection *aDBConn);
102 :
103 : private:
104 : /**
105 : * Argument Indexes
106 : */
107 : static const PRUint32 kArgSearchString = 0;
108 : static const PRUint32 kArgIndexURL = 1;
109 : static const PRUint32 kArgIndexTitle = 2;
110 : static const PRUint32 kArgIndexTags = 3;
111 : static const PRUint32 kArgIndexVisitCount = 4;
112 : static const PRUint32 kArgIndexTyped = 5;
113 : static const PRUint32 kArgIndexBookmark = 6;
114 : static const PRUint32 kArgIndexOpenPageCount = 7;
115 : static const PRUint32 kArgIndexMatchBehavior = 8;
116 : static const PRUint32 kArgIndexSearchBehavior = 9;
117 : static const PRUint32 kArgIndexLength = 10;
118 :
119 : /**
120 : * Typedefs
121 : */
122 : typedef bool (*searchFunctionPtr)(const nsDependentCSubstring &aToken,
123 : const nsACString &aSourceString);
124 :
125 : typedef nsACString::const_char_iterator const_char_iterator;
126 :
127 : /**
128 : * Obtains the search function to match on.
129 : *
130 : * @param aBehavior
131 : * The matching behavior to use defined by one of the
132 : * mozIPlacesAutoComplete::MATCH_* values.
133 : * @return a pointer to the function that will perform the proper search.
134 : */
135 : static searchFunctionPtr getSearchFunction(PRInt32 aBehavior);
136 :
137 : /**
138 : * Tests if aSourceString starts with aToken.
139 : *
140 : * @param aToken
141 : * The string to search for.
142 : * @param aSourceString
143 : * The string to search.
144 : * @return true if found, false otherwise.
145 : */
146 : static bool findBeginning(const nsDependentCSubstring &aToken,
147 : const nsACString &aSourceString);
148 :
149 : /**
150 : * Searches aSourceString for aToken anywhere in the string in a case-
151 : * insensitive way.
152 : *
153 : * @param aToken
154 : * The string to search for.
155 : * @param aSourceString
156 : * The string to search.
157 : * @return true if found, false otherwise.
158 : */
159 : static bool findAnywhere(const nsDependentCSubstring &aToken,
160 : const nsACString &aSourceString);
161 :
162 : /**
163 : * Tests if aToken is found on a word boundary in aSourceString.
164 : *
165 : * @param aToken
166 : * The string to search for.
167 : * @param aSourceString
168 : * The string to search.
169 : * @return true if found, false otherwise.
170 : */
171 : static bool findOnBoundary(const nsDependentCSubstring &aToken,
172 : const nsACString &aSourceString);
173 :
174 :
175 : /**
176 : * Fixes a URI's spec such that it is ready to be searched. This includes
177 : * unescaping escaped characters and removing certain specs that we do not
178 : * care to search for.
179 : *
180 : * @param aURISpec
181 : * The spec of the URI to prepare for searching.
182 : * @param aMatchBehavior
183 : * The matching behavior to use defined by one of the
184 : * mozIPlacesAutoComplete::MATCH_* values.
185 : * @param _fixedSpec
186 : * An out parameter that is the fixed up string.
187 : */
188 : static void fixupURISpec(const nsCString &aURISpec, PRInt32 aMatchBehavior,
189 : nsCString &_fixedSpec);
190 : };
191 :
192 :
193 :
194 : ////////////////////////////////////////////////////////////////////////////////
195 : //// Frecency Calculation Function
196 :
197 : /**
198 : * This function is used to calculate frecency for a page.
199 : *
200 : * In SQL, you'd use it in when setting frecency like:
201 : * SET frecency = CALCULATE_FRECENCY(place_id).
202 : * Optional parameters must be passed in if the page is not yet in the database,
203 : * otherwise they will be fetched from it automatically.
204 : *
205 : * @param pageId
206 : * The id of the page. Pass -1 if the page is being added right now.
207 : * @param [optional] typed
208 : * Whether the page has been typed in. Default is false.
209 : * @param [optional] fullVisitCount
210 : * Count of all the visits (All types). Default is 0.
211 : * @param [optional] isBookmarked
212 : * Whether the page is bookmarked. Default is false.
213 : */
214 : class CalculateFrecencyFunction : public mozIStorageFunction
215 267 : {
216 : public:
217 : NS_DECL_ISUPPORTS
218 : NS_DECL_MOZISTORAGEFUNCTION
219 :
220 : /**
221 : * Registers the function with the specified database connection.
222 : *
223 : * @param aDBConn
224 : * The database connection to register with.
225 : */
226 : static nsresult create(mozIStorageConnection *aDBConn);
227 : };
228 :
229 : /**
230 : * SQL function to generate a GUID for a place or bookmark item. This is just
231 : * a wrapper around GenerateGUID in Helpers.h.
232 : *
233 : * @return a guid for the item.
234 : */
235 : class GenerateGUIDFunction : public mozIStorageFunction
236 267 : {
237 : public:
238 : NS_DECL_ISUPPORTS
239 : NS_DECL_MOZISTORAGEFUNCTION
240 :
241 : /**
242 : * Registers the function with the specified database connection.
243 : *
244 : * @param aDBConn
245 : * The database connection to register with.
246 : */
247 : static nsresult create(mozIStorageConnection *aDBConn);
248 : };
249 :
250 : /**
251 : * SQL function to unreverse the rev_host of a page.
252 : *
253 : * @param rev_host
254 : * The rev_host value of the page.
255 : *
256 : * @return the unreversed host of the page.
257 : */
258 : class GetUnreversedHostFunction : public mozIStorageFunction
259 267 : {
260 : public:
261 : NS_DECL_ISUPPORTS
262 : NS_DECL_MOZISTORAGEFUNCTION
263 :
264 : /**
265 : * Registers the function with the specified database connection.
266 : *
267 : * @param aDBConn
268 : * The database connection to register with.
269 : */
270 : static nsresult create(mozIStorageConnection *aDBConn);
271 : };
272 :
273 :
274 : ////////////////////////////////////////////////////////////////////////////////
275 : //// Fixup URL Function
276 :
277 : /**
278 : * Make a given URL more suitable for searches, by removing common prefixes
279 : * such as "www."
280 : *
281 : * @param url
282 : * A URL.
283 : * @return
284 : * The same URL, with redundant parts removed.
285 : */
286 : class FixupURLFunction : public mozIStorageFunction
287 267 : {
288 : public:
289 : NS_DECL_ISUPPORTS
290 : NS_DECL_MOZISTORAGEFUNCTION
291 :
292 : /**
293 : * Registers the function with the specified database connection.
294 : *
295 : * @param aDBConn
296 : * The database connection to register with.
297 : */
298 : static nsresult create(mozIStorageConnection *aDBConn);
299 : };
300 :
301 : } // namespace places
302 : } // namespace storage
303 :
304 : #endif // mozilla_places_SQLFunctions_h_
|