Line data Source code
1 : /* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef mozilla_places_SQLFunctions_h_
7 : #define mozilla_places_SQLFunctions_h_
8 :
9 : /**
10 : * This file contains functions that Places adds to the database handle that can
11 : * be accessed by SQL queries.
12 : *
13 : * Keep the GUID-related parts of this file in sync with
14 : * toolkit/downloads/SQLFunctions.[h|cpp]!
15 : */
16 :
17 : #include "mozIStorageFunction.h"
18 : #include "mozilla/Attributes.h"
19 :
20 : class mozIStorageConnection;
21 :
22 : namespace mozilla {
23 : namespace places {
24 :
25 : ////////////////////////////////////////////////////////////////////////////////
26 : //// AutoComplete Matching Function
27 :
28 : /**
29 : * This function is used to determine if a given set of data should match an
30 : * AutoComplete query.
31 : *
32 : * In SQL, you'd use it in the WHERE clause like so:
33 : * WHERE AUTOCOMPLETE_MATCH(aSearchString, aURL, aTitle, aTags, aVisitCount,
34 : * aTyped, aBookmark, aOpenPageCount, aMatchBehavior,
35 : * aSearchBehavior)
36 : *
37 : * @param aSearchString
38 : * The string to compare against.
39 : * @param aURL
40 : * The URL to test for an AutoComplete match.
41 : * @param aTitle
42 : * The title to test for an AutoComplete match.
43 : * @param aTags
44 : * The tags to test for an AutoComplete match.
45 : * @param aVisitCount
46 : * The number of visits aURL has.
47 : * @param aTyped
48 : * Indicates if aURL is a typed URL or not. Treated as a boolean.
49 : * @param aBookmark
50 : * Indicates if aURL is a bookmark or not. Treated as a boolean.
51 : * @param aOpenPageCount
52 : * The number of times aURL has been registered as being open. (See
53 : * mozIPlacesAutoComplete::registerOpenPage.)
54 : * @param aMatchBehavior
55 : * The match behavior to use for this search.
56 : * @param aSearchBehavior
57 : * A bitfield dictating the search behavior.
58 : */
59 1 : class MatchAutoCompleteFunction final : public mozIStorageFunction
60 : {
61 : public:
62 : NS_DECL_THREADSAFE_ISUPPORTS
63 : NS_DECL_MOZISTORAGEFUNCTION
64 :
65 : /**
66 : * Registers the function with the specified database connection.
67 : *
68 : * @param aDBConn
69 : * The database connection to register with.
70 : */
71 : static nsresult create(mozIStorageConnection *aDBConn);
72 :
73 : private:
74 0 : ~MatchAutoCompleteFunction() {}
75 :
76 : /**
77 : * Argument Indexes
78 : */
79 : static const uint32_t kArgSearchString = 0;
80 : static const uint32_t kArgIndexURL = 1;
81 : static const uint32_t kArgIndexTitle = 2;
82 : static const uint32_t kArgIndexTags = 3;
83 : static const uint32_t kArgIndexVisitCount = 4;
84 : static const uint32_t kArgIndexTyped = 5;
85 : static const uint32_t kArgIndexBookmark = 6;
86 : static const uint32_t kArgIndexOpenPageCount = 7;
87 : static const uint32_t kArgIndexMatchBehavior = 8;
88 : static const uint32_t kArgIndexSearchBehavior = 9;
89 : static const uint32_t kArgIndexLength = 10;
90 :
91 : /**
92 : * Typedefs
93 : */
94 : typedef bool (*searchFunctionPtr)(const nsDependentCSubstring &aToken,
95 : const nsACString &aSourceString);
96 :
97 : typedef nsACString::const_char_iterator const_char_iterator;
98 :
99 : /**
100 : * Obtains the search function to match on.
101 : *
102 : * @param aBehavior
103 : * The matching behavior to use defined by one of the
104 : * mozIPlacesAutoComplete::MATCH_* values.
105 : * @return a pointer to the function that will perform the proper search.
106 : */
107 : static searchFunctionPtr getSearchFunction(int32_t aBehavior);
108 :
109 : /**
110 : * Tests if aSourceString starts with aToken.
111 : *
112 : * @param aToken
113 : * The string to search for.
114 : * @param aSourceString
115 : * The string to search.
116 : * @return true if found, false otherwise.
117 : */
118 : static bool findBeginning(const nsDependentCSubstring &aToken,
119 : const nsACString &aSourceString);
120 :
121 : /**
122 : * Tests if aSourceString starts with aToken in a case sensitive way.
123 : *
124 : * @param aToken
125 : * The string to search for.
126 : * @param aSourceString
127 : * The string to search.
128 : * @return true if found, false otherwise.
129 : */
130 : static bool findBeginningCaseSensitive(const nsDependentCSubstring &aToken,
131 : const nsACString &aSourceString);
132 :
133 : /**
134 : * Searches aSourceString for aToken anywhere in the string in a case-
135 : * insensitive way.
136 : *
137 : * @param aToken
138 : * The string to search for.
139 : * @param aSourceString
140 : * The string to search.
141 : * @return true if found, false otherwise.
142 : */
143 : static bool findAnywhere(const nsDependentCSubstring &aToken,
144 : const nsACString &aSourceString);
145 :
146 : /**
147 : * Tests if aToken is found on a word boundary in aSourceString.
148 : *
149 : * @param aToken
150 : * The string to search for.
151 : * @param aSourceString
152 : * The string to search.
153 : * @return true if found, false otherwise.
154 : */
155 : static bool findOnBoundary(const nsDependentCSubstring &aToken,
156 : const nsACString &aSourceString);
157 :
158 :
159 : /**
160 : * Fixes a URI's spec such that it is ready to be searched. This includes
161 : * unescaping escaped characters and removing certain specs that we do not
162 : * care to search for.
163 : *
164 : * @param aURISpec
165 : * The spec of the URI to prepare for searching.
166 : * @param aMatchBehavior
167 : * The matching behavior to use defined by one of the
168 : * mozIPlacesAutoComplete::MATCH_* values.
169 : * @param aSpecBuf
170 : * A string buffer that the returned slice can point into, if needed.
171 : * @return the fixed up string.
172 : */
173 : static nsDependentCSubstring fixupURISpec(const nsACString &aURISpec,
174 : int32_t aMatchBehavior,
175 : nsACString &aSpecBuf);
176 : };
177 :
178 :
179 : ////////////////////////////////////////////////////////////////////////////////
180 : //// Frecency Calculation Function
181 :
182 : /**
183 : * This function is used to calculate frecency for a page.
184 : *
185 : * In SQL, you'd use it in when setting frecency like:
186 : * SET frecency = CALCULATE_FRECENCY(place_id).
187 : * Optional parameters must be passed in if the page is not yet in the database,
188 : * otherwise they will be fetched from it automatically.
189 : *
190 : * @param {int64_t} pageId
191 : * The id of the page. Pass -1 if the page is being added right now.
192 : * @param [optional] {int32_t} redirect
193 : * Whether the page visit is a redirect. Default is false.
194 : */
195 1 : class CalculateFrecencyFunction final : public mozIStorageFunction
196 : {
197 : public:
198 : NS_DECL_THREADSAFE_ISUPPORTS
199 : NS_DECL_MOZISTORAGEFUNCTION
200 :
201 : /**
202 : * Registers the function with the specified database connection.
203 : *
204 : * @param aDBConn
205 : * The database connection to register with.
206 : */
207 : static nsresult create(mozIStorageConnection *aDBConn);
208 : private:
209 0 : ~CalculateFrecencyFunction() {}
210 : };
211 :
212 : /**
213 : * SQL function to generate a GUID for a place or bookmark item. This is just
214 : * a wrapper around GenerateGUID in Helpers.h.
215 : *
216 : * @return a guid for the item.
217 : */
218 1 : class GenerateGUIDFunction final : public mozIStorageFunction
219 : {
220 : public:
221 : NS_DECL_THREADSAFE_ISUPPORTS
222 : NS_DECL_MOZISTORAGEFUNCTION
223 :
224 : /**
225 : * Registers the function with the specified database connection.
226 : *
227 : * @param aDBConn
228 : * The database connection to register with.
229 : */
230 : static nsresult create(mozIStorageConnection *aDBConn);
231 : private:
232 0 : ~GenerateGUIDFunction() {}
233 : };
234 :
235 : /**
236 : * SQL function to unreverse the rev_host of a page.
237 : *
238 : * @param rev_host
239 : * The rev_host value of the page.
240 : *
241 : * @return the unreversed host of the page.
242 : */
243 1 : class GetUnreversedHostFunction final : public mozIStorageFunction
244 : {
245 : public:
246 : NS_DECL_THREADSAFE_ISUPPORTS
247 : NS_DECL_MOZISTORAGEFUNCTION
248 :
249 : /**
250 : * Registers the function with the specified database connection.
251 : *
252 : * @param aDBConn
253 : * The database connection to register with.
254 : */
255 : static nsresult create(mozIStorageConnection *aDBConn);
256 : private:
257 0 : ~GetUnreversedHostFunction() {}
258 : };
259 :
260 :
261 : ////////////////////////////////////////////////////////////////////////////////
262 : //// Fixup URL Function
263 :
264 : /**
265 : * Make a given URL more suitable for searches, by removing common prefixes
266 : * such as "www."
267 : *
268 : * @param url
269 : * A URL.
270 : * @return
271 : * The same URL, with redundant parts removed.
272 : */
273 1 : class FixupURLFunction final : public mozIStorageFunction
274 : {
275 : public:
276 : NS_DECL_THREADSAFE_ISUPPORTS
277 : NS_DECL_MOZISTORAGEFUNCTION
278 :
279 : /**
280 : * Registers the function with the specified database connection.
281 : *
282 : * @param aDBConn
283 : * The database connection to register with.
284 : */
285 : static nsresult create(mozIStorageConnection *aDBConn);
286 : private:
287 0 : ~FixupURLFunction() {}
288 : };
289 :
290 :
291 : ////////////////////////////////////////////////////////////////////////////////
292 : //// Frecency Changed Notification Function
293 :
294 : /**
295 : * For a given place, posts a runnable to the main thread that calls
296 : * onFrecencyChanged on nsNavHistory's nsINavHistoryObservers. The passed-in
297 : * newFrecency value is returned unchanged.
298 : *
299 : * @param newFrecency
300 : * The place's new frecency.
301 : * @param url
302 : * The place's URL.
303 : * @param guid
304 : * The place's GUID.
305 : * @param hidden
306 : * The place's hidden boolean.
307 : * @param lastVisitDate
308 : * The place's last visit date.
309 : * @return newFrecency
310 : */
311 1 : class FrecencyNotificationFunction final : public mozIStorageFunction
312 : {
313 : public:
314 : NS_DECL_THREADSAFE_ISUPPORTS
315 : NS_DECL_MOZISTORAGEFUNCTION
316 :
317 : /**
318 : * Registers the function with the specified database connection.
319 : *
320 : * @param aDBConn
321 : * The database connection to register with.
322 : */
323 : static nsresult create(mozIStorageConnection *aDBConn);
324 : private:
325 0 : ~FrecencyNotificationFunction() {}
326 : };
327 :
328 :
329 : ////////////////////////////////////////////////////////////////////////////////
330 : //// Store Last Inserted Id Function
331 :
332 : /**
333 : * Store the last inserted id for reference purpose.
334 : *
335 : * @param tableName
336 : * The table name.
337 : * @param id
338 : * The last inserted id.
339 : * @return null
340 : */
341 1 : class StoreLastInsertedIdFunction final : public mozIStorageFunction
342 : {
343 : public:
344 : NS_DECL_THREADSAFE_ISUPPORTS
345 : NS_DECL_MOZISTORAGEFUNCTION
346 :
347 : /**
348 : * Registers the function with the specified database connection.
349 : *
350 : * @param aDBConn
351 : * The database connection to register with.
352 : */
353 : static nsresult create(mozIStorageConnection *aDBConn);
354 : private:
355 0 : ~StoreLastInsertedIdFunction() {}
356 : };
357 :
358 :
359 : ////////////////////////////////////////////////////////////////////////////////
360 : //// Hash Function
361 :
362 : /**
363 : * Calculates hash for a given string using the mfbt AddToHash function.
364 : *
365 : * @param string
366 : * A string.
367 : * @return
368 : * The hash for the string.
369 : */
370 1 : class HashFunction final : public mozIStorageFunction
371 : {
372 : public:
373 : NS_DECL_THREADSAFE_ISUPPORTS
374 : NS_DECL_MOZISTORAGEFUNCTION
375 :
376 : /**
377 : * Registers the function with the specified database connection.
378 : *
379 : * @param aDBConn
380 : * The database connection to register with.
381 : */
382 : static nsresult create(mozIStorageConnection *aDBConn);
383 : private:
384 0 : ~HashFunction() {}
385 : };
386 :
387 : } // namespace places
388 : } // namespace mozilla
389 :
390 : #endif // mozilla_places_SQLFunctions_h_
|