toolkit/components/places/SQLFunctions.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/SQLFunctions.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,323 @@
     1.4 +/* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_places_SQLFunctions_h_
    1.10 +#define mozilla_places_SQLFunctions_h_
    1.11 +
    1.12 +/**
    1.13 + * This file contains functions that Places adds to the database handle that can
    1.14 + * be accessed by SQL queries.
    1.15 + *
    1.16 + * Keep the GUID-related parts of this file in sync with
    1.17 + * toolkit/downloads/SQLFunctions.[h|cpp]!
    1.18 + */
    1.19 +
    1.20 +#include "mozIStorageFunction.h"
    1.21 +#include "mozilla/Attributes.h"
    1.22 +
    1.23 +class mozIStorageConnection;
    1.24 +
    1.25 +namespace mozilla {
    1.26 +namespace places {
    1.27 +
    1.28 +////////////////////////////////////////////////////////////////////////////////
    1.29 +//// AutoComplete Matching Function
    1.30 +
    1.31 +/**
    1.32 + * This function is used to determine if a given set of data should match an
    1.33 + * AutoComplete query.
    1.34 + *
    1.35 + * In SQL, you'd use it in the WHERE clause like so:
    1.36 + * WHERE AUTOCOMPLETE_MATCH(aSearchString, aURL, aTitle, aTags, aVisitCount,
    1.37 + *                          aTyped, aBookmark, aOpenPageCount, aMatchBehavior,
    1.38 + *                          aSearchBehavior)
    1.39 + *
    1.40 + * @param aSearchString
    1.41 + *        The string to compare against.
    1.42 + * @param aURL
    1.43 + *        The URL to test for an AutoComplete match.
    1.44 + * @param aTitle
    1.45 + *        The title to test for an AutoComplete match.
    1.46 + * @param aTags
    1.47 + *        The tags to test for an AutoComplete match.
    1.48 + * @param aVisitCount
    1.49 + *        The number of visits aURL has.
    1.50 + * @param aTyped
    1.51 + *        Indicates if aURL is a typed URL or not.  Treated as a boolean.
    1.52 + * @param aBookmark
    1.53 + *        Indicates if aURL is a bookmark or not.  Treated as a boolean.
    1.54 + * @param aOpenPageCount
    1.55 + *        The number of times aURL has been registered as being open.  (See
    1.56 + *        mozIPlacesAutoComplete::registerOpenPage.)
    1.57 + * @param aMatchBehavior
    1.58 + *        The match behavior to use for this search.
    1.59 + * @param aSearchBehavior
    1.60 + *        A bitfield dictating the search behavior.
    1.61 + */
    1.62 +class MatchAutoCompleteFunction MOZ_FINAL : public mozIStorageFunction
    1.63 +{
    1.64 +public:
    1.65 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.66 +  NS_DECL_MOZISTORAGEFUNCTION
    1.67 +
    1.68 +  /**
    1.69 +   * Registers the function with the specified database connection.
    1.70 +   *
    1.71 +   * @param aDBConn
    1.72 +   *        The database connection to register with.
    1.73 +   */
    1.74 +  static nsresult create(mozIStorageConnection *aDBConn);
    1.75 +
    1.76 +private:
    1.77 +  /**
    1.78 +   * Argument Indexes
    1.79 +   */
    1.80 +  static const uint32_t kArgSearchString = 0;
    1.81 +  static const uint32_t kArgIndexURL = 1;
    1.82 +  static const uint32_t kArgIndexTitle = 2;
    1.83 +  static const uint32_t kArgIndexTags = 3;
    1.84 +  static const uint32_t kArgIndexVisitCount = 4;
    1.85 +  static const uint32_t kArgIndexTyped = 5;
    1.86 +  static const uint32_t kArgIndexBookmark = 6;
    1.87 +  static const uint32_t kArgIndexOpenPageCount = 7;
    1.88 +  static const uint32_t kArgIndexMatchBehavior = 8;
    1.89 +  static const uint32_t kArgIndexSearchBehavior = 9;
    1.90 +  static const uint32_t kArgIndexLength = 10;
    1.91 +
    1.92 +  /**
    1.93 +   * Typedefs
    1.94 +   */
    1.95 +  typedef bool (*searchFunctionPtr)(const nsDependentCSubstring &aToken,
    1.96 +                                    const nsACString &aSourceString);
    1.97 +
    1.98 +  typedef nsACString::const_char_iterator const_char_iterator;
    1.99 +
   1.100 +  /**
   1.101 +   * Obtains the search function to match on.
   1.102 +   *
   1.103 +   * @param aBehavior
   1.104 +   *        The matching behavior to use defined by one of the
   1.105 +   *        mozIPlacesAutoComplete::MATCH_* values.
   1.106 +   * @return a pointer to the function that will perform the proper search.
   1.107 +   */
   1.108 +  static searchFunctionPtr getSearchFunction(int32_t aBehavior);
   1.109 +
   1.110 +  /**
   1.111 +   * Tests if aSourceString starts with aToken.
   1.112 +   *
   1.113 +   * @param aToken
   1.114 +   *        The string to search for.
   1.115 +   * @param aSourceString
   1.116 +   *        The string to search.
   1.117 +   * @return true if found, false otherwise.
   1.118 +   */
   1.119 +  static bool findBeginning(const nsDependentCSubstring &aToken,
   1.120 +                            const nsACString &aSourceString);
   1.121 +
   1.122 +  /**
   1.123 +   * Tests if aSourceString starts with aToken in a case sensitive way.
   1.124 +   *
   1.125 +   * @param aToken
   1.126 +   *        The string to search for.
   1.127 +   * @param aSourceString
   1.128 +   *        The string to search.
   1.129 +   * @return true if found, false otherwise.
   1.130 +   */
   1.131 +  static bool findBeginningCaseSensitive(const nsDependentCSubstring &aToken,
   1.132 +                                         const nsACString &aSourceString);
   1.133 +
   1.134 +  /**
   1.135 +   * Searches aSourceString for aToken anywhere in the string in a case-
   1.136 +   * insensitive way.
   1.137 +   *
   1.138 +   * @param aToken
   1.139 +   *        The string to search for.
   1.140 +   * @param aSourceString
   1.141 +   *        The string to search.
   1.142 +   * @return true if found, false otherwise.
   1.143 +   */
   1.144 +  static bool findAnywhere(const nsDependentCSubstring &aToken,
   1.145 +                           const nsACString &aSourceString);
   1.146 +
   1.147 +  /**
   1.148 +   * Tests if aToken is found on a word boundary in aSourceString.
   1.149 +   *
   1.150 +   * @param aToken
   1.151 +   *        The string to search for.
   1.152 +   * @param aSourceString
   1.153 +   *        The string to search.
   1.154 +   * @return true if found, false otherwise.
   1.155 +   */
   1.156 +  static bool findOnBoundary(const nsDependentCSubstring &aToken,
   1.157 +                             const nsACString &aSourceString);
   1.158 +
   1.159 +
   1.160 +  /**
   1.161 +   * Fixes a URI's spec such that it is ready to be searched.  This includes
   1.162 +   * unescaping escaped characters and removing certain specs that we do not
   1.163 +   * care to search for.
   1.164 +   *
   1.165 +   * @param aURISpec
   1.166 +   *        The spec of the URI to prepare for searching.
   1.167 +   * @param aMatchBehavior
   1.168 +   *        The matching behavior to use defined by one of the
   1.169 +   *        mozIPlacesAutoComplete::MATCH_* values.
   1.170 +   * @param _fixedSpec
   1.171 +   *        An out parameter that is the fixed up string.
   1.172 +   */
   1.173 +  static void fixupURISpec(const nsCString &aURISpec, int32_t aMatchBehavior,
   1.174 +                           nsCString &_fixedSpec);
   1.175 +};
   1.176 +
   1.177 +
   1.178 +
   1.179 +////////////////////////////////////////////////////////////////////////////////
   1.180 +//// Frecency Calculation Function
   1.181 +
   1.182 +/**
   1.183 + * This function is used to calculate frecency for a page.
   1.184 + *
   1.185 + * In SQL, you'd use it in when setting frecency like:
   1.186 + * SET frecency = CALCULATE_FRECENCY(place_id).
   1.187 + * Optional parameters must be passed in if the page is not yet in the database,
   1.188 + * otherwise they will be fetched from it automatically.
   1.189 + *
   1.190 + * @param pageId
   1.191 + *        The id of the page.  Pass -1 if the page is being added right now.
   1.192 + * @param [optional] typed
   1.193 + *        Whether the page has been typed in.  Default is false.
   1.194 + * @param [optional] fullVisitCount
   1.195 + *        Count of all the visits (All types).  Default is 0.
   1.196 + * @param [optional] isBookmarked
   1.197 + *        Whether the page is bookmarked. Default is false.
   1.198 + */
   1.199 +class CalculateFrecencyFunction MOZ_FINAL : public mozIStorageFunction
   1.200 +{
   1.201 +public:
   1.202 +  NS_DECL_THREADSAFE_ISUPPORTS
   1.203 +  NS_DECL_MOZISTORAGEFUNCTION
   1.204 +
   1.205 +  /**
   1.206 +   * Registers the function with the specified database connection.
   1.207 +   *
   1.208 +   * @param aDBConn
   1.209 +   *        The database connection to register with.
   1.210 +   */
   1.211 +  static nsresult create(mozIStorageConnection *aDBConn);
   1.212 +};
   1.213 +
   1.214 +/**
   1.215 + * SQL function to generate a GUID for a place or bookmark item.  This is just
   1.216 + * a wrapper around GenerateGUID in Helpers.h.
   1.217 + *
   1.218 + * @return a guid for the item.
   1.219 + */
   1.220 +class GenerateGUIDFunction MOZ_FINAL : public mozIStorageFunction
   1.221 +{
   1.222 +public:
   1.223 +  NS_DECL_THREADSAFE_ISUPPORTS
   1.224 +  NS_DECL_MOZISTORAGEFUNCTION
   1.225 +
   1.226 +  /**
   1.227 +   * Registers the function with the specified database connection.
   1.228 +   *
   1.229 +   * @param aDBConn
   1.230 +   *        The database connection to register with.
   1.231 +   */
   1.232 +  static nsresult create(mozIStorageConnection *aDBConn);
   1.233 +};
   1.234 +
   1.235 +/**
   1.236 + * SQL function to unreverse the rev_host of a page.
   1.237 + *
   1.238 + * @param rev_host
   1.239 + *        The rev_host value of the page.
   1.240 + *
   1.241 + * @return the unreversed host of the page.
   1.242 + */
   1.243 +class GetUnreversedHostFunction MOZ_FINAL : public mozIStorageFunction
   1.244 +{
   1.245 +public:
   1.246 +  NS_DECL_THREADSAFE_ISUPPORTS
   1.247 +  NS_DECL_MOZISTORAGEFUNCTION
   1.248 +
   1.249 +  /**
   1.250 +   * Registers the function with the specified database connection.
   1.251 +   *
   1.252 +   * @param aDBConn
   1.253 +   *        The database connection to register with.
   1.254 +   */
   1.255 +  static nsresult create(mozIStorageConnection *aDBConn);
   1.256 +};
   1.257 +
   1.258 +
   1.259 +////////////////////////////////////////////////////////////////////////////////
   1.260 +//// Fixup URL Function
   1.261 +
   1.262 +/**
   1.263 + * Make a given URL more suitable for searches, by removing common prefixes
   1.264 + * such as "www."
   1.265 + *
   1.266 + * @param url
   1.267 + *        A URL.
   1.268 + * @return
   1.269 + *        The same URL, with redundant parts removed.
   1.270 + */
   1.271 +class FixupURLFunction MOZ_FINAL : public mozIStorageFunction
   1.272 +{
   1.273 +public:
   1.274 +  NS_DECL_THREADSAFE_ISUPPORTS
   1.275 +  NS_DECL_MOZISTORAGEFUNCTION
   1.276 +
   1.277 +  /**
   1.278 +   * Registers the function with the specified database connection.
   1.279 +   *
   1.280 +   * @param aDBConn
   1.281 +   *        The database connection to register with.
   1.282 +   */
   1.283 +  static nsresult create(mozIStorageConnection *aDBConn);
   1.284 +};
   1.285 +
   1.286 +
   1.287 +////////////////////////////////////////////////////////////////////////////////
   1.288 +//// Frecency Changed Notification Function
   1.289 +
   1.290 +/**
   1.291 + * For a given place, posts a runnable to the main thread that calls
   1.292 + * onFrecencyChanged on nsNavHistory's nsINavHistoryObservers.  The passed-in
   1.293 + * newFrecency value is returned unchanged.
   1.294 + *
   1.295 + * @param newFrecency
   1.296 + *        The place's new frecency.
   1.297 + * @param url
   1.298 + *        The place's URL.
   1.299 + * @param guid
   1.300 + *        The place's GUID.
   1.301 + * @param hidden
   1.302 + *        The place's hidden boolean.
   1.303 + * @param lastVisitDate
   1.304 + *        The place's last visit date.
   1.305 + * @return newFrecency
   1.306 + */
   1.307 +class FrecencyNotificationFunction MOZ_FINAL : public mozIStorageFunction
   1.308 +{
   1.309 +public:
   1.310 +  NS_DECL_THREADSAFE_ISUPPORTS
   1.311 +  NS_DECL_MOZISTORAGEFUNCTION
   1.312 +
   1.313 +  /**
   1.314 +   * Registers the function with the specified database connection.
   1.315 +   *
   1.316 +   * @param aDBConn
   1.317 +   *        The database connection to register with.
   1.318 +   */
   1.319 +  static nsresult create(mozIStorageConnection *aDBConn);
   1.320 +};
   1.321 +
   1.322 +
   1.323 +} // namespace places
   1.324 +} // namespace storage
   1.325 +
   1.326 +#endif // mozilla_places_SQLFunctions_h_

mercurial