2009-05-11 13:05:56 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-08-04 20:59:56 +04:00
|
|
|
|
2009-05-11 13:05:56 +04:00
|
|
|
#include "nsPlacesTables.h"
|
|
|
|
|
2008-08-04 20:59:56 +04:00
|
|
|
#ifndef __nsPlacesTriggers_h__
|
|
|
|
#define __nsPlacesTriggers_h__
|
|
|
|
|
2009-05-11 13:05:56 +04:00
|
|
|
/**
|
|
|
|
* Exclude these visit types:
|
|
|
|
* 0 - invalid
|
|
|
|
* 4 - EMBED
|
|
|
|
* 7 - DOWNLOAD
|
2010-03-10 15:40:34 +03:00
|
|
|
* 7 - FRAMED_LINK
|
2009-05-11 13:05:56 +04:00
|
|
|
**/
|
2010-03-10 15:40:34 +03:00
|
|
|
#define EXCLUDED_VISIT_TYPES "0, 4, 7, 8"
|
2008-08-04 20:59:56 +04:00
|
|
|
|
2008-10-28 01:52:19 +03:00
|
|
|
/**
|
2010-08-09 19:59:59 +04:00
|
|
|
* This triggers update visit_count and last_visit_date based on historyvisits
|
|
|
|
* table changes.
|
2008-10-28 01:52:19 +03:00
|
|
|
*/
|
2010-08-09 19:59:59 +04:00
|
|
|
#define CREATE_HISTORYVISITS_AFTERINSERT_TRIGGER NS_LITERAL_CSTRING( \
|
2010-11-24 18:36:05 +03:00
|
|
|
"CREATE TEMP TRIGGER moz_historyvisits_afterinsert_v2_trigger " \
|
2010-08-09 19:59:59 +04:00
|
|
|
"AFTER INSERT ON moz_historyvisits FOR EACH ROW " \
|
2008-10-28 01:52:19 +03:00
|
|
|
"BEGIN " \
|
2010-08-09 19:59:59 +04:00
|
|
|
"UPDATE moz_places SET " \
|
|
|
|
"visit_count = visit_count + (SELECT NEW.visit_type NOT IN (" EXCLUDED_VISIT_TYPES ")), "\
|
|
|
|
"last_visit_date = MAX(IFNULL(last_visit_date, 0), NEW.visit_date) " \
|
2009-05-15 03:28:03 +04:00
|
|
|
"WHERE id = NEW.place_id;" \
|
2008-10-28 01:52:19 +03:00
|
|
|
"END" \
|
|
|
|
)
|
|
|
|
|
2010-08-09 19:59:59 +04:00
|
|
|
#define CREATE_HISTORYVISITS_AFTERDELETE_TRIGGER NS_LITERAL_CSTRING( \
|
2010-11-24 18:36:05 +03:00
|
|
|
"CREATE TEMP TRIGGER moz_historyvisits_afterdelete_v2_trigger " \
|
2010-08-09 19:59:59 +04:00
|
|
|
"AFTER DELETE ON moz_historyvisits FOR EACH ROW " \
|
2008-10-28 01:52:19 +03:00
|
|
|
"BEGIN " \
|
2010-08-09 19:59:59 +04:00
|
|
|
"UPDATE moz_places SET " \
|
|
|
|
"visit_count = visit_count - (SELECT OLD.visit_type NOT IN (" EXCLUDED_VISIT_TYPES ")), "\
|
|
|
|
"last_visit_date = (SELECT visit_date FROM moz_historyvisits " \
|
|
|
|
"WHERE place_id = OLD.place_id " \
|
|
|
|
"ORDER BY visit_date DESC LIMIT 1) " \
|
|
|
|
"WHERE id = OLD.place_id;" \
|
2008-10-28 01:52:19 +03:00
|
|
|
"END" \
|
|
|
|
)
|
|
|
|
|
2013-06-11 01:59:15 +04:00
|
|
|
/**
|
|
|
|
* A predicate matching pages on rev_host, based on a given host value.
|
|
|
|
* 'host' may be either the moz_hosts.host column or an alias representing an
|
|
|
|
* equivalent value.
|
|
|
|
*/
|
|
|
|
#define HOST_TO_REVHOST_PREDICATE \
|
|
|
|
"rev_host = get_unreversed_host(host || '.') || '.' " \
|
|
|
|
"OR rev_host = get_unreversed_host(host || '.') || '.www.'"
|
|
|
|
|
2012-06-01 16:38:39 +04:00
|
|
|
/**
|
|
|
|
* Select the best prefix for a host, based on existing pages registered for it.
|
|
|
|
* Prefixes have a priority, from the top to the bottom, so that secure pages
|
|
|
|
* have higher priority, and more generically "www." prefixed hosts come before
|
|
|
|
* unprefixed ones.
|
2013-06-11 01:59:15 +04:00
|
|
|
* Given a host, examine associated pages and:
|
|
|
|
* - if all of the typed pages start with https://www. return https://www.
|
|
|
|
* - if all of the typed pages start with https:// return https://
|
|
|
|
* - if all of the typed pages start with ftp: return ftp://
|
|
|
|
* - if all of the typed pages start with www. return www.
|
|
|
|
* - otherwise don't use any prefix
|
2012-06-01 16:38:39 +04:00
|
|
|
*/
|
|
|
|
#define HOSTS_PREFIX_PRIORITY_FRAGMENT \
|
|
|
|
"SELECT CASE " \
|
2013-06-11 01:59:15 +04:00
|
|
|
"WHEN 1 = ( " \
|
|
|
|
"SELECT min(substr(url,1,12) = 'https://www.') FROM moz_places h " \
|
|
|
|
"WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \
|
2012-06-01 16:38:39 +04:00
|
|
|
") THEN 'https://www.' " \
|
2013-06-11 01:59:15 +04:00
|
|
|
"WHEN 1 = ( " \
|
|
|
|
"SELECT min(substr(url,1,8) = 'https://') FROM moz_places h " \
|
|
|
|
"WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \
|
2012-06-01 16:38:39 +04:00
|
|
|
") THEN 'https://' " \
|
2013-06-11 01:59:15 +04:00
|
|
|
"WHEN 1 = ( " \
|
|
|
|
"SELECT min(substr(url,1,4) = 'ftp:') FROM moz_places h " \
|
|
|
|
"WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \
|
2012-06-01 16:38:39 +04:00
|
|
|
") THEN 'ftp://' " \
|
2013-06-11 01:59:15 +04:00
|
|
|
"WHEN 1 = ( " \
|
|
|
|
"SELECT min(substr(url,1,11) = 'http://www.') FROM moz_places h " \
|
|
|
|
"WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \
|
2012-06-01 16:38:39 +04:00
|
|
|
") THEN 'www.' " \
|
|
|
|
"END "
|
|
|
|
|
2012-01-19 15:31:23 +04:00
|
|
|
/**
|
|
|
|
* These triggers update the hostnames table whenever moz_places changes.
|
|
|
|
*/
|
|
|
|
#define CREATE_PLACES_AFTERINSERT_TRIGGER NS_LITERAL_CSTRING( \
|
|
|
|
"CREATE TEMP TRIGGER moz_places_afterinsert_trigger " \
|
|
|
|
"AFTER INSERT ON moz_places FOR EACH ROW " \
|
|
|
|
"WHEN LENGTH(NEW.rev_host) > 1 " \
|
|
|
|
"BEGIN " \
|
2012-06-01 16:38:39 +04:00
|
|
|
"INSERT OR REPLACE INTO moz_hosts (id, host, frecency, typed, prefix) " \
|
2012-01-19 15:31:23 +04:00
|
|
|
"VALUES (" \
|
|
|
|
"(SELECT id FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), " \
|
|
|
|
"fixup_url(get_unreversed_host(NEW.rev_host)), " \
|
2012-04-07 03:17:49 +04:00
|
|
|
"MAX(IFNULL((SELECT frecency FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), -1), NEW.frecency), " \
|
2012-06-01 16:38:39 +04:00
|
|
|
"MAX(IFNULL((SELECT typed FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), 0), NEW.typed), " \
|
|
|
|
"(" HOSTS_PREFIX_PRIORITY_FRAGMENT \
|
|
|
|
"FROM ( " \
|
|
|
|
"SELECT fixup_url(get_unreversed_host(NEW.rev_host)) AS host " \
|
|
|
|
") AS match " \
|
|
|
|
") " \
|
2012-01-19 15:31:23 +04:00
|
|
|
"); " \
|
|
|
|
"END" \
|
|
|
|
)
|
|
|
|
|
|
|
|
#define CREATE_PLACES_AFTERDELETE_TRIGGER NS_LITERAL_CSTRING( \
|
|
|
|
"CREATE TEMP TRIGGER moz_places_afterdelete_trigger " \
|
|
|
|
"AFTER DELETE ON moz_places FOR EACH ROW " \
|
|
|
|
"BEGIN " \
|
|
|
|
"DELETE FROM moz_hosts " \
|
|
|
|
"WHERE host = fixup_url(get_unreversed_host(OLD.rev_host)) " \
|
2012-02-22 15:23:14 +04:00
|
|
|
"AND NOT EXISTS(" \
|
|
|
|
"SELECT 1 FROM moz_places " \
|
|
|
|
"WHERE rev_host = get_unreversed_host(host || '.') || '.' " \
|
|
|
|
"OR rev_host = get_unreversed_host(host || '.') || '.www.' " \
|
|
|
|
"); " \
|
2012-06-01 16:38:39 +04:00
|
|
|
"UPDATE moz_hosts " \
|
2013-06-11 01:59:15 +04:00
|
|
|
"SET prefix = (" HOSTS_PREFIX_PRIORITY_FRAGMENT ") " \
|
2012-06-01 16:38:39 +04:00
|
|
|
"WHERE host = fixup_url(get_unreversed_host(OLD.rev_host)); " \
|
2012-01-19 15:31:23 +04:00
|
|
|
"END" \
|
|
|
|
)
|
|
|
|
|
2012-02-07 18:59:09 +04:00
|
|
|
// For performance reasons the host frecency is updated only when the page
|
|
|
|
// frecency changes by a meaningful percentage. This is because the frecency
|
|
|
|
// decay algorithm requires to update all the frecencies at once, causing a
|
|
|
|
// too high overhead, while leaving the ordering unchanged.
|
2012-02-17 17:24:58 +04:00
|
|
|
#define CREATE_PLACES_AFTERUPDATE_FRECENCY_TRIGGER NS_LITERAL_CSTRING( \
|
2012-01-19 15:31:23 +04:00
|
|
|
"CREATE TEMP TRIGGER moz_places_afterupdate_frecency_trigger " \
|
|
|
|
"AFTER UPDATE OF frecency ON moz_places FOR EACH ROW " \
|
|
|
|
"WHEN NEW.frecency >= 0 " \
|
2012-02-07 18:59:09 +04:00
|
|
|
"AND ABS(" \
|
|
|
|
"IFNULL((NEW.frecency - OLD.frecency) / CAST(NEW.frecency AS REAL), " \
|
|
|
|
"(NEW.frecency - OLD.frecency))" \
|
|
|
|
") > .05 " \
|
2012-01-19 15:31:23 +04:00
|
|
|
"BEGIN " \
|
|
|
|
"UPDATE moz_hosts " \
|
2012-02-07 18:59:09 +04:00
|
|
|
"SET frecency = (SELECT MAX(frecency) FROM moz_places " \
|
2012-02-22 15:23:14 +04:00
|
|
|
"WHERE rev_host = get_unreversed_host(host || '.') || '.' " \
|
|
|
|
"OR rev_host = get_unreversed_host(host || '.') || '.www.') " \
|
2012-01-19 15:31:23 +04:00
|
|
|
"WHERE host = fixup_url(get_unreversed_host(NEW.rev_host)); " \
|
|
|
|
"END" \
|
|
|
|
)
|
|
|
|
|
2012-02-17 17:24:58 +04:00
|
|
|
#define CREATE_PLACES_AFTERUPDATE_TYPED_TRIGGER NS_LITERAL_CSTRING( \
|
|
|
|
"CREATE TEMP TRIGGER moz_places_afterupdate_typed_trigger " \
|
|
|
|
"AFTER UPDATE OF typed ON moz_places FOR EACH ROW " \
|
|
|
|
"WHEN NEW.typed = 1 " \
|
|
|
|
"BEGIN " \
|
|
|
|
"UPDATE moz_hosts " \
|
|
|
|
"SET typed = 1 " \
|
|
|
|
"WHERE host = fixup_url(get_unreversed_host(NEW.rev_host)); " \
|
|
|
|
"END" \
|
|
|
|
)
|
|
|
|
|
2010-03-27 00:59:02 +03:00
|
|
|
/**
|
|
|
|
* This trigger removes a row from moz_openpages_temp when open_count reaches 0.
|
2010-11-20 03:39:55 +03:00
|
|
|
*
|
|
|
|
* @note this should be kept up-to-date with the definition in
|
|
|
|
* nsPlacesAutoComplete.js
|
2010-03-27 00:59:02 +03:00
|
|
|
*/
|
|
|
|
#define CREATE_REMOVEOPENPAGE_CLEANUP_TRIGGER NS_LITERAL_CSTRING( \
|
|
|
|
"CREATE TEMPORARY TRIGGER moz_openpages_temp_afterupdate_trigger " \
|
|
|
|
"AFTER UPDATE OF open_count ON moz_openpages_temp FOR EACH ROW " \
|
|
|
|
"WHEN NEW.open_count = 0 " \
|
|
|
|
"BEGIN " \
|
|
|
|
"DELETE FROM moz_openpages_temp " \
|
2010-08-14 05:53:05 +04:00
|
|
|
"WHERE url = NEW.url;" \
|
2010-03-27 00:59:02 +03:00
|
|
|
"END" \
|
|
|
|
)
|
|
|
|
|
2008-08-04 20:59:56 +04:00
|
|
|
#endif // __nsPlacesTriggers_h__
|