Source code

Revision control

Other Tools

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