|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "nsPlacesTables.h" |
|
8 |
|
9 #ifndef __nsPlacesTriggers_h__ |
|
10 #define __nsPlacesTriggers_h__ |
|
11 |
|
12 /** |
|
13 * Exclude these visit types: |
|
14 * 0 - invalid |
|
15 * 4 - EMBED |
|
16 * 7 - DOWNLOAD |
|
17 * 7 - FRAMED_LINK |
|
18 **/ |
|
19 #define EXCLUDED_VISIT_TYPES "0, 4, 7, 8" |
|
20 |
|
21 /** |
|
22 * This triggers update visit_count and last_visit_date based on historyvisits |
|
23 * table changes. |
|
24 */ |
|
25 #define CREATE_HISTORYVISITS_AFTERINSERT_TRIGGER NS_LITERAL_CSTRING( \ |
|
26 "CREATE TEMP TRIGGER moz_historyvisits_afterinsert_v2_trigger " \ |
|
27 "AFTER INSERT ON moz_historyvisits FOR EACH ROW " \ |
|
28 "BEGIN " \ |
|
29 "UPDATE moz_places SET " \ |
|
30 "visit_count = visit_count + (SELECT NEW.visit_type NOT IN (" EXCLUDED_VISIT_TYPES ")), "\ |
|
31 "last_visit_date = MAX(IFNULL(last_visit_date, 0), NEW.visit_date) " \ |
|
32 "WHERE id = NEW.place_id;" \ |
|
33 "END" \ |
|
34 ) |
|
35 |
|
36 #define CREATE_HISTORYVISITS_AFTERDELETE_TRIGGER NS_LITERAL_CSTRING( \ |
|
37 "CREATE TEMP TRIGGER moz_historyvisits_afterdelete_v2_trigger " \ |
|
38 "AFTER DELETE ON moz_historyvisits FOR EACH ROW " \ |
|
39 "BEGIN " \ |
|
40 "UPDATE moz_places SET " \ |
|
41 "visit_count = visit_count - (SELECT OLD.visit_type NOT IN (" EXCLUDED_VISIT_TYPES ")), "\ |
|
42 "last_visit_date = (SELECT visit_date FROM moz_historyvisits " \ |
|
43 "WHERE place_id = OLD.place_id " \ |
|
44 "ORDER BY visit_date DESC LIMIT 1) " \ |
|
45 "WHERE id = OLD.place_id;" \ |
|
46 "END" \ |
|
47 ) |
|
48 |
|
49 /** |
|
50 * A predicate matching pages on rev_host, based on a given host value. |
|
51 * 'host' may be either the moz_hosts.host column or an alias representing an |
|
52 * equivalent value. |
|
53 */ |
|
54 #define HOST_TO_REVHOST_PREDICATE \ |
|
55 "rev_host = get_unreversed_host(host || '.') || '.' " \ |
|
56 "OR rev_host = get_unreversed_host(host || '.') || '.www.'" |
|
57 |
|
58 /** |
|
59 * Select the best prefix for a host, based on existing pages registered for it. |
|
60 * Prefixes have a priority, from the top to the bottom, so that secure pages |
|
61 * have higher priority, and more generically "www." prefixed hosts come before |
|
62 * unprefixed ones. |
|
63 * Given a host, examine associated pages and: |
|
64 * - if all of the typed pages start with https://www. return https://www. |
|
65 * - if all of the typed pages start with https:// return https:// |
|
66 * - if all of the typed pages start with ftp: return ftp:// |
|
67 * - if all of the typed pages start with www. return www. |
|
68 * - otherwise don't use any prefix |
|
69 */ |
|
70 #define HOSTS_PREFIX_PRIORITY_FRAGMENT \ |
|
71 "SELECT CASE " \ |
|
72 "WHEN 1 = ( " \ |
|
73 "SELECT min(substr(url,1,12) = 'https://www.') FROM moz_places h " \ |
|
74 "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
|
75 ") THEN 'https://www.' " \ |
|
76 "WHEN 1 = ( " \ |
|
77 "SELECT min(substr(url,1,8) = 'https://') FROM moz_places h " \ |
|
78 "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
|
79 ") THEN 'https://' " \ |
|
80 "WHEN 1 = ( " \ |
|
81 "SELECT min(substr(url,1,4) = 'ftp:') FROM moz_places h " \ |
|
82 "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
|
83 ") THEN 'ftp://' " \ |
|
84 "WHEN 1 = ( " \ |
|
85 "SELECT min(substr(url,1,11) = 'http://www.') FROM moz_places h " \ |
|
86 "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
|
87 ") THEN 'www.' " \ |
|
88 "END " |
|
89 |
|
90 /** |
|
91 * These triggers update the hostnames table whenever moz_places changes. |
|
92 */ |
|
93 #define CREATE_PLACES_AFTERINSERT_TRIGGER NS_LITERAL_CSTRING( \ |
|
94 "CREATE TEMP TRIGGER moz_places_afterinsert_trigger " \ |
|
95 "AFTER INSERT ON moz_places FOR EACH ROW " \ |
|
96 "WHEN LENGTH(NEW.rev_host) > 1 " \ |
|
97 "BEGIN " \ |
|
98 "INSERT OR REPLACE INTO moz_hosts (id, host, frecency, typed, prefix) " \ |
|
99 "VALUES (" \ |
|
100 "(SELECT id FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), " \ |
|
101 "fixup_url(get_unreversed_host(NEW.rev_host)), " \ |
|
102 "MAX(IFNULL((SELECT frecency FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), -1), NEW.frecency), " \ |
|
103 "MAX(IFNULL((SELECT typed FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), 0), NEW.typed), " \ |
|
104 "(" HOSTS_PREFIX_PRIORITY_FRAGMENT \ |
|
105 "FROM ( " \ |
|
106 "SELECT fixup_url(get_unreversed_host(NEW.rev_host)) AS host " \ |
|
107 ") AS match " \ |
|
108 ") " \ |
|
109 "); " \ |
|
110 "END" \ |
|
111 ) |
|
112 |
|
113 #define CREATE_PLACES_AFTERDELETE_TRIGGER NS_LITERAL_CSTRING( \ |
|
114 "CREATE TEMP TRIGGER moz_places_afterdelete_trigger " \ |
|
115 "AFTER DELETE ON moz_places FOR EACH ROW " \ |
|
116 "BEGIN " \ |
|
117 "DELETE FROM moz_hosts " \ |
|
118 "WHERE host = fixup_url(get_unreversed_host(OLD.rev_host)) " \ |
|
119 "AND NOT EXISTS(" \ |
|
120 "SELECT 1 FROM moz_places " \ |
|
121 "WHERE rev_host = get_unreversed_host(host || '.') || '.' " \ |
|
122 "OR rev_host = get_unreversed_host(host || '.') || '.www.' " \ |
|
123 "); " \ |
|
124 "UPDATE moz_hosts " \ |
|
125 "SET prefix = (" HOSTS_PREFIX_PRIORITY_FRAGMENT ") " \ |
|
126 "WHERE host = fixup_url(get_unreversed_host(OLD.rev_host)); " \ |
|
127 "END" \ |
|
128 ) |
|
129 |
|
130 // For performance reasons the host frecency is updated only when the page |
|
131 // frecency changes by a meaningful percentage. This is because the frecency |
|
132 // decay algorithm requires to update all the frecencies at once, causing a |
|
133 // too high overhead, while leaving the ordering unchanged. |
|
134 #define CREATE_PLACES_AFTERUPDATE_FRECENCY_TRIGGER NS_LITERAL_CSTRING( \ |
|
135 "CREATE TEMP TRIGGER moz_places_afterupdate_frecency_trigger " \ |
|
136 "AFTER UPDATE OF frecency ON moz_places FOR EACH ROW " \ |
|
137 "WHEN NEW.frecency >= 0 " \ |
|
138 "AND ABS(" \ |
|
139 "IFNULL((NEW.frecency - OLD.frecency) / CAST(NEW.frecency AS REAL), " \ |
|
140 "(NEW.frecency - OLD.frecency))" \ |
|
141 ") > .05 " \ |
|
142 "BEGIN " \ |
|
143 "UPDATE moz_hosts " \ |
|
144 "SET frecency = (SELECT MAX(frecency) FROM moz_places " \ |
|
145 "WHERE rev_host = get_unreversed_host(host || '.') || '.' " \ |
|
146 "OR rev_host = get_unreversed_host(host || '.') || '.www.') " \ |
|
147 "WHERE host = fixup_url(get_unreversed_host(NEW.rev_host)); " \ |
|
148 "END" \ |
|
149 ) |
|
150 |
|
151 #define CREATE_PLACES_AFTERUPDATE_TYPED_TRIGGER NS_LITERAL_CSTRING( \ |
|
152 "CREATE TEMP TRIGGER moz_places_afterupdate_typed_trigger " \ |
|
153 "AFTER UPDATE OF typed ON moz_places FOR EACH ROW " \ |
|
154 "WHEN NEW.typed = 1 " \ |
|
155 "BEGIN " \ |
|
156 "UPDATE moz_hosts " \ |
|
157 "SET typed = 1 " \ |
|
158 "WHERE host = fixup_url(get_unreversed_host(NEW.rev_host)); " \ |
|
159 "END" \ |
|
160 ) |
|
161 |
|
162 /** |
|
163 * This trigger removes a row from moz_openpages_temp when open_count reaches 0. |
|
164 * |
|
165 * @note this should be kept up-to-date with the definition in |
|
166 * nsPlacesAutoComplete.js |
|
167 */ |
|
168 #define CREATE_REMOVEOPENPAGE_CLEANUP_TRIGGER NS_LITERAL_CSTRING( \ |
|
169 "CREATE TEMPORARY TRIGGER moz_openpages_temp_afterupdate_trigger " \ |
|
170 "AFTER UPDATE OF open_count ON moz_openpages_temp FOR EACH ROW " \ |
|
171 "WHEN NEW.open_count = 0 " \ |
|
172 "BEGIN " \ |
|
173 "DELETE FROM moz_openpages_temp " \ |
|
174 "WHERE url = NEW.url;" \ |
|
175 "END" \ |
|
176 ) |
|
177 |
|
178 #endif // __nsPlacesTriggers_h__ |