/src/mozilla-central/toolkit/components/places/SQLFunctions.h
Line | Count | Source (jump to first uncovered line) |
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 | | class MatchAutoCompleteFunction final : public mozIStorageFunction |
60 | | { |
61 | | public: |
62 | | MatchAutoCompleteFunction(); |
63 | | |
64 | | NS_DECL_THREADSAFE_ISUPPORTS |
65 | | NS_DECL_MOZISTORAGEFUNCTION |
66 | | |
67 | | /** |
68 | | * Registers the function with the specified database connection. |
69 | | * |
70 | | * @param aDBConn |
71 | | * The database connection to register with. |
72 | | */ |
73 | | static nsresult create(mozIStorageConnection *aDBConn); |
74 | | |
75 | | private: |
76 | 0 | ~MatchAutoCompleteFunction() {} |
77 | | |
78 | | /** |
79 | | * IntegerVariants for 0 and 1 are frequently used in awesomebar queries, |
80 | | * so we cache them to avoid allocating memory repeatedly. |
81 | | */ |
82 | | nsCOMPtr<nsIVariant> mCachedZero; |
83 | | nsCOMPtr<nsIVariant> mCachedOne; |
84 | | |
85 | | /** |
86 | | * Argument Indexes |
87 | | */ |
88 | | static const uint32_t kArgSearchString = 0; |
89 | | static const uint32_t kArgIndexURL = 1; |
90 | | static const uint32_t kArgIndexTitle = 2; |
91 | | static const uint32_t kArgIndexTags = 3; |
92 | | static const uint32_t kArgIndexVisitCount = 4; |
93 | | static const uint32_t kArgIndexTyped = 5; |
94 | | static const uint32_t kArgIndexBookmark = 6; |
95 | | static const uint32_t kArgIndexOpenPageCount = 7; |
96 | | static const uint32_t kArgIndexMatchBehavior = 8; |
97 | | static const uint32_t kArgIndexSearchBehavior = 9; |
98 | | static const uint32_t kArgIndexLength = 10; |
99 | | |
100 | | /** |
101 | | * Typedefs |
102 | | */ |
103 | | typedef bool (*searchFunctionPtr)(const nsDependentCSubstring &aToken, |
104 | | const nsACString &aSourceString); |
105 | | |
106 | | typedef nsACString::const_char_iterator const_char_iterator; |
107 | | |
108 | | /** |
109 | | * Obtains the search function to match on. |
110 | | * |
111 | | * @param aBehavior |
112 | | * The matching behavior to use defined by one of the |
113 | | * mozIPlacesAutoComplete::MATCH_* values. |
114 | | * @return a pointer to the function that will perform the proper search. |
115 | | */ |
116 | | static searchFunctionPtr getSearchFunction(int32_t aBehavior); |
117 | | |
118 | | /** |
119 | | * Tests if aSourceString starts with aToken. |
120 | | * |
121 | | * @param aToken |
122 | | * The string to search for. |
123 | | * @param aSourceString |
124 | | * The string to search. |
125 | | * @return true if found, false otherwise. |
126 | | */ |
127 | | static bool findBeginning(const nsDependentCSubstring &aToken, |
128 | | const nsACString &aSourceString); |
129 | | |
130 | | /** |
131 | | * Tests if aSourceString starts with aToken in a case sensitive way. |
132 | | * |
133 | | * @param aToken |
134 | | * The string to search for. |
135 | | * @param aSourceString |
136 | | * The string to search. |
137 | | * @return true if found, false otherwise. |
138 | | */ |
139 | | static bool findBeginningCaseSensitive(const nsDependentCSubstring &aToken, |
140 | | const nsACString &aSourceString); |
141 | | |
142 | | /** |
143 | | * Searches aSourceString for aToken anywhere in the string in a case- |
144 | | * insensitive way. |
145 | | * |
146 | | * @param aToken |
147 | | * The string to search for. |
148 | | * @param aSourceString |
149 | | * The string to search. |
150 | | * @return true if found, false otherwise. |
151 | | */ |
152 | | static bool findAnywhere(const nsDependentCSubstring &aToken, |
153 | | const nsACString &aSourceString); |
154 | | |
155 | | /** |
156 | | * Tests if aToken is found on a word boundary in aSourceString. |
157 | | * |
158 | | * @param aToken |
159 | | * The string to search for. |
160 | | * @param aSourceString |
161 | | * The string to search. |
162 | | * @return true if found, false otherwise. |
163 | | */ |
164 | | static bool findOnBoundary(const nsDependentCSubstring &aToken, |
165 | | const nsACString &aSourceString); |
166 | | |
167 | | |
168 | | /** |
169 | | * Fixes a URI's spec such that it is ready to be searched. This includes |
170 | | * unescaping escaped characters and removing certain specs that we do not |
171 | | * care to search for. |
172 | | * |
173 | | * @param aURISpec |
174 | | * The spec of the URI to prepare for searching. |
175 | | * @param aMatchBehavior |
176 | | * The matching behavior to use defined by one of the |
177 | | * mozIPlacesAutoComplete::MATCH_* values. |
178 | | * @param aSpecBuf |
179 | | * A string buffer that the returned slice can point into, if needed. |
180 | | * @return the fixed up string. |
181 | | */ |
182 | | static nsDependentCSubstring fixupURISpec(const nsACString &aURISpec, |
183 | | int32_t aMatchBehavior, |
184 | | nsACString &aSpecBuf); |
185 | | }; |
186 | | |
187 | | |
188 | | //////////////////////////////////////////////////////////////////////////////// |
189 | | //// Frecency Calculation Function |
190 | | |
191 | | /** |
192 | | * This function is used to calculate frecency for a page. |
193 | | * |
194 | | * In SQL, you'd use it in when setting frecency like: |
195 | | * SET frecency = CALCULATE_FRECENCY(place_id). |
196 | | * Optional parameters must be passed in if the page is not yet in the database, |
197 | | * otherwise they will be fetched from it automatically. |
198 | | * |
199 | | * @param {int64_t} pageId |
200 | | * The id of the page. Pass -1 if the page is being added right now. |
201 | | * @param [optional] {int32_t} useRedirectBonus |
202 | | * Whether we should use the lower redirect bonus for the most recent |
203 | | * page visit. If not passed in, it will use a database guess. |
204 | | */ |
205 | | class CalculateFrecencyFunction final : public mozIStorageFunction |
206 | | { |
207 | | public: |
208 | | NS_DECL_THREADSAFE_ISUPPORTS |
209 | | NS_DECL_MOZISTORAGEFUNCTION |
210 | | |
211 | | /** |
212 | | * Registers the function with the specified database connection. |
213 | | * |
214 | | * @param aDBConn |
215 | | * The database connection to register with. |
216 | | */ |
217 | | static nsresult create(mozIStorageConnection *aDBConn); |
218 | | private: |
219 | 0 | ~CalculateFrecencyFunction() {} |
220 | | }; |
221 | | |
222 | | /** |
223 | | * SQL function to generate a GUID for a place or bookmark item. This is just |
224 | | * a wrapper around GenerateGUID in Helpers.h. |
225 | | * |
226 | | * @return a guid for the item. |
227 | | */ |
228 | | class GenerateGUIDFunction final : public mozIStorageFunction |
229 | | { |
230 | | public: |
231 | | NS_DECL_THREADSAFE_ISUPPORTS |
232 | | NS_DECL_MOZISTORAGEFUNCTION |
233 | | |
234 | | /** |
235 | | * Registers the function with the specified database connection. |
236 | | * |
237 | | * @param aDBConn |
238 | | * The database connection to register with. |
239 | | */ |
240 | | static nsresult create(mozIStorageConnection *aDBConn); |
241 | | private: |
242 | 0 | ~GenerateGUIDFunction() {} |
243 | | }; |
244 | | |
245 | | /** |
246 | | * SQL function to check if a GUID is valid. This is just a wrapper around |
247 | | * IsValidGUID in Helpers.h. |
248 | | * |
249 | | * @return true if valid, false otherwise. |
250 | | */ |
251 | | class IsValidGUIDFunction final : public mozIStorageFunction |
252 | | { |
253 | | public: |
254 | | NS_DECL_THREADSAFE_ISUPPORTS |
255 | | NS_DECL_MOZISTORAGEFUNCTION |
256 | | |
257 | | /** |
258 | | * Registers the function with the specified database connection. |
259 | | * |
260 | | * @param aDBConn |
261 | | * The database connection to register with. |
262 | | */ |
263 | | static nsresult create(mozIStorageConnection *aDBConn); |
264 | | private: |
265 | 0 | ~IsValidGUIDFunction() {} |
266 | | }; |
267 | | |
268 | | /** |
269 | | * SQL function to unreverse the rev_host of a page. |
270 | | * |
271 | | * @param rev_host |
272 | | * The rev_host value of the page. |
273 | | * |
274 | | * @return the unreversed host of the page. |
275 | | */ |
276 | | class GetUnreversedHostFunction final : public mozIStorageFunction |
277 | | { |
278 | | public: |
279 | | NS_DECL_THREADSAFE_ISUPPORTS |
280 | | NS_DECL_MOZISTORAGEFUNCTION |
281 | | |
282 | | /** |
283 | | * Registers the function with the specified database connection. |
284 | | * |
285 | | * @param aDBConn |
286 | | * The database connection to register with. |
287 | | */ |
288 | | static nsresult create(mozIStorageConnection *aDBConn); |
289 | | private: |
290 | 0 | ~GetUnreversedHostFunction() {} |
291 | | }; |
292 | | |
293 | | |
294 | | //////////////////////////////////////////////////////////////////////////////// |
295 | | //// Fixup URL Function |
296 | | |
297 | | /** |
298 | | * Make a given URL more suitable for searches, by removing common prefixes |
299 | | * such as "www." |
300 | | * |
301 | | * @param url |
302 | | * A URL. |
303 | | * @return |
304 | | * The same URL, with redundant parts removed. |
305 | | */ |
306 | | class FixupURLFunction final : public mozIStorageFunction |
307 | | { |
308 | | public: |
309 | | NS_DECL_THREADSAFE_ISUPPORTS |
310 | | NS_DECL_MOZISTORAGEFUNCTION |
311 | | |
312 | | /** |
313 | | * Registers the function with the specified database connection. |
314 | | * |
315 | | * @param aDBConn |
316 | | * The database connection to register with. |
317 | | */ |
318 | | static nsresult create(mozIStorageConnection *aDBConn); |
319 | | private: |
320 | 0 | ~FixupURLFunction() {} |
321 | | }; |
322 | | |
323 | | |
324 | | //////////////////////////////////////////////////////////////////////////////// |
325 | | //// Frecency Changed Notification Function |
326 | | |
327 | | /** |
328 | | * For a given place, posts a runnable to the main thread that calls |
329 | | * onFrecencyChanged on nsNavHistory's nsINavHistoryObservers. The passed-in |
330 | | * newFrecency value is returned unchanged. |
331 | | * |
332 | | * @param newFrecency |
333 | | * The place's new frecency. |
334 | | * @param url |
335 | | * The place's URL. |
336 | | * @param guid |
337 | | * The place's GUID. |
338 | | * @param hidden |
339 | | * The place's hidden boolean. |
340 | | * @param lastVisitDate |
341 | | * The place's last visit date. |
342 | | * @return newFrecency |
343 | | */ |
344 | | class FrecencyNotificationFunction final : public mozIStorageFunction |
345 | | { |
346 | | public: |
347 | | NS_DECL_THREADSAFE_ISUPPORTS |
348 | | NS_DECL_MOZISTORAGEFUNCTION |
349 | | |
350 | | /** |
351 | | * Registers the function with the specified database connection. |
352 | | * |
353 | | * @param aDBConn |
354 | | * The database connection to register with. |
355 | | */ |
356 | | static nsresult create(mozIStorageConnection *aDBConn); |
357 | | private: |
358 | 0 | ~FrecencyNotificationFunction() {} |
359 | | }; |
360 | | |
361 | | |
362 | | //////////////////////////////////////////////////////////////////////////////// |
363 | | //// Store Last Inserted Id Function |
364 | | |
365 | | /** |
366 | | * Store the last inserted id for reference purpose. |
367 | | * |
368 | | * @param tableName |
369 | | * The table name. |
370 | | * @param id |
371 | | * The last inserted id. |
372 | | * @return null |
373 | | */ |
374 | | class StoreLastInsertedIdFunction final : public mozIStorageFunction |
375 | | { |
376 | | public: |
377 | | NS_DECL_THREADSAFE_ISUPPORTS |
378 | | NS_DECL_MOZISTORAGEFUNCTION |
379 | | |
380 | | /** |
381 | | * Registers the function with the specified database connection. |
382 | | * |
383 | | * @param aDBConn |
384 | | * The database connection to register with. |
385 | | */ |
386 | | static nsresult create(mozIStorageConnection *aDBConn); |
387 | | private: |
388 | 0 | ~StoreLastInsertedIdFunction() {} |
389 | | }; |
390 | | |
391 | | |
392 | | //////////////////////////////////////////////////////////////////////////////// |
393 | | //// Hash Function |
394 | | |
395 | | /** |
396 | | * Calculates hash for a given string using the mfbt AddToHash function. |
397 | | * |
398 | | * @param string |
399 | | * A string. |
400 | | * @return |
401 | | * The hash for the string. |
402 | | */ |
403 | | class HashFunction final : public mozIStorageFunction |
404 | | { |
405 | | public: |
406 | | NS_DECL_THREADSAFE_ISUPPORTS |
407 | | NS_DECL_MOZISTORAGEFUNCTION |
408 | | |
409 | | /** |
410 | | * Registers the function with the specified database connection. |
411 | | * |
412 | | * @param aDBConn |
413 | | * The database connection to register with. |
414 | | */ |
415 | | static nsresult create(mozIStorageConnection *aDBConn); |
416 | | private: |
417 | 0 | ~HashFunction() {} |
418 | | }; |
419 | | |
420 | | //////////////////////////////////////////////////////////////////////////////// |
421 | | //// Get Query Param Function |
422 | | |
423 | | /** |
424 | | * Extracts and returns the value of a parameter from a query string. |
425 | | * |
426 | | * @param string |
427 | | * A string. |
428 | | * @return |
429 | | * The value of the query parameter as a string, or NULL if not set. |
430 | | */ |
431 | | class GetQueryParamFunction final : public mozIStorageFunction |
432 | | { |
433 | | public: |
434 | | NS_DECL_THREADSAFE_ISUPPORTS |
435 | | NS_DECL_MOZISTORAGEFUNCTION |
436 | | |
437 | | /** |
438 | | * Registers the function with the specified database connection. |
439 | | * |
440 | | * @param aDBConn |
441 | | * The database connection to register with. |
442 | | */ |
443 | | static nsresult create(mozIStorageConnection *aDBConn); |
444 | | private: |
445 | 0 | ~GetQueryParamFunction() {} |
446 | | }; |
447 | | |
448 | | |
449 | | //////////////////////////////////////////////////////////////////////////////// |
450 | | //// Get prefix function |
451 | | |
452 | | /** |
453 | | * Gets the length of the prefix in a URL. "Prefix" is defined to be the |
454 | | * scheme, colon, and, if present, two slashes. |
455 | | * |
456 | | * @param url |
457 | | * A URL, or a string that may be a URL. |
458 | | * @return |
459 | | * If `url` is actually a URL and has a prefix, then this returns the |
460 | | * prefix. Otherwise this returns an empty string. |
461 | | */ |
462 | | class GetPrefixFunction final : public mozIStorageFunction |
463 | | { |
464 | | public: |
465 | | NS_DECL_THREADSAFE_ISUPPORTS |
466 | | NS_DECL_MOZISTORAGEFUNCTION |
467 | | |
468 | | /** |
469 | | * Registers the function with the specified database connection. |
470 | | * |
471 | | * @param aDBConn |
472 | | * The database connection to register with. |
473 | | */ |
474 | | static nsresult create(mozIStorageConnection *aDBConn); |
475 | | private: |
476 | 0 | ~GetPrefixFunction() {} |
477 | | }; |
478 | | |
479 | | |
480 | | //////////////////////////////////////////////////////////////////////////////// |
481 | | //// Get host and port function |
482 | | |
483 | | /** |
484 | | * Gets the host and port substring of a URL. |
485 | | * |
486 | | * @param url |
487 | | * A URL, or a string that may be a URL. |
488 | | * @return |
489 | | * If `url` is actually a URL, or if it's a URL without the prefix, then |
490 | | * this returns the host and port substring of the URL. Otherwise, this |
491 | | * returns `url` itself. |
492 | | */ |
493 | | class GetHostAndPortFunction final : public mozIStorageFunction |
494 | | { |
495 | | public: |
496 | | NS_DECL_THREADSAFE_ISUPPORTS |
497 | | NS_DECL_MOZISTORAGEFUNCTION |
498 | | |
499 | | /** |
500 | | * Registers the function with the specified database connection. |
501 | | * |
502 | | * @param aDBConn |
503 | | * The database connection to register with. |
504 | | */ |
505 | | static nsresult create(mozIStorageConnection *aDBConn); |
506 | | private: |
507 | 0 | ~GetHostAndPortFunction() {} |
508 | | }; |
509 | | |
510 | | |
511 | | //////////////////////////////////////////////////////////////////////////////// |
512 | | //// Strip prefix function |
513 | | |
514 | | /** |
515 | | * Gets the part of a URL after its prefix and userinfo; i.e., the substring |
516 | | * starting at the host. |
517 | | * |
518 | | * @param url |
519 | | * A URL, or a string that may be a URL. |
520 | | * @return |
521 | | * If `url` is actually a URL, or if it's a URL without the prefix, then |
522 | | * this returns the substring starting at the host. Otherwise, this |
523 | | * returns `url` itself. |
524 | | */ |
525 | | class StripPrefixAndUserinfoFunction final : public mozIStorageFunction |
526 | | { |
527 | | public: |
528 | | NS_DECL_THREADSAFE_ISUPPORTS |
529 | | NS_DECL_MOZISTORAGEFUNCTION |
530 | | |
531 | | /** |
532 | | * Registers the function with the specified database connection. |
533 | | * |
534 | | * @param aDBConn |
535 | | * The database connection to register with. |
536 | | */ |
537 | | static nsresult create(mozIStorageConnection *aDBConn); |
538 | | private: |
539 | 0 | ~StripPrefixAndUserinfoFunction() {} |
540 | | }; |
541 | | |
542 | | |
543 | | //////////////////////////////////////////////////////////////////////////////// |
544 | | //// Is frecency decaying function |
545 | | |
546 | | /** |
547 | | * Returns nsNavHistory::IsFrecencyDecaying(). |
548 | | * |
549 | | * @return |
550 | | * True if frecency is currently decaying and false otherwise. |
551 | | */ |
552 | | class IsFrecencyDecayingFunction final : public mozIStorageFunction |
553 | | { |
554 | | public: |
555 | | NS_DECL_THREADSAFE_ISUPPORTS |
556 | | NS_DECL_MOZISTORAGEFUNCTION |
557 | | |
558 | | /** |
559 | | * Registers the function with the specified database connection. |
560 | | * |
561 | | * @param aDBConn |
562 | | * The database connection to register with. |
563 | | */ |
564 | | static nsresult create(mozIStorageConnection *aDBConn); |
565 | | private: |
566 | 0 | ~IsFrecencyDecayingFunction() {} |
567 | | }; |
568 | | |
569 | | |
570 | | //////////////////////////////////////////////////////////////////////////////// |
571 | | //// sqrt function |
572 | | |
573 | | /** |
574 | | * Gets the square root of a given value. |
575 | | */ |
576 | | class SqrtFunction final : public mozIStorageFunction |
577 | | { |
578 | | public: |
579 | | NS_DECL_THREADSAFE_ISUPPORTS |
580 | | NS_DECL_MOZISTORAGEFUNCTION |
581 | | |
582 | | /** |
583 | | * Registers the function with the specified database connection. |
584 | | * |
585 | | * @param aDBConn |
586 | | * The database connection to register with. |
587 | | */ |
588 | | static nsresult create(mozIStorageConnection *aDBConn); |
589 | | private: |
590 | 0 | ~SqrtFunction() {} |
591 | | }; |
592 | | |
593 | | |
594 | | //////////////////////////////////////////////////////////////////////////////// |
595 | | //// Note Sync Change Function |
596 | | |
597 | | /** |
598 | | * Bumps the global Sync change counter. See the comment above |
599 | | * `totalSyncChanges` in `nsINavBookmarksService` for a more detailed |
600 | | * explanation. |
601 | | */ |
602 | | class NoteSyncChangeFunction final : public mozIStorageFunction |
603 | | { |
604 | | public: |
605 | | NS_DECL_THREADSAFE_ISUPPORTS |
606 | | NS_DECL_MOZISTORAGEFUNCTION |
607 | | |
608 | | /** |
609 | | * Registers the function with the specified database connection. |
610 | | * |
611 | | * @param aDBConn |
612 | | * The database connection to register with. |
613 | | */ |
614 | | static nsresult create(mozIStorageConnection *aDBConn); |
615 | | private: |
616 | 0 | ~NoteSyncChangeFunction() {} |
617 | | }; |
618 | | |
619 | | } // namespace places |
620 | | } // namespace mozilla |
621 | | |
622 | | #endif // mozilla_places_SQLFunctions_h_ |