OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h" | 5 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 void AutocompleteActionPredictorTable::GetAllRows(Rows* row_buffer) { | 93 void AutocompleteActionPredictorTable::GetAllRows(Rows* row_buffer) { |
94 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 94 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
95 if (CantAccessDatabase()) | 95 if (CantAccessDatabase()) |
96 return; | 96 return; |
97 | 97 |
98 row_buffer->clear(); | 98 row_buffer->clear(); |
99 | 99 |
100 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | 100 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
101 base::StringPrintf( | 101 base::StringPrintf( |
102 "SELECT * FROM %s", kAutocompletePredictorTableName).c_str())); | 102 "SELECT * FROM %s", kAutocompletePredictorTableName).c_str())); |
103 if (!statement.is_valid()) | |
dominich
2012/06/12 19:11:06
have you seen this happen?
Shishir
2012/06/12 19:35:22
Not here, but the API says that you should check v
dominich
2012/06/12 19:44:43
Done.
| |
104 return; | |
103 | 105 |
104 Row row; | 106 Row row; |
105 while (StepAndInitializeRow(&statement, &row)) | 107 while (StepAndInitializeRow(&statement, &row)) |
106 row_buffer->push_back(row); | 108 row_buffer->push_back(row); |
107 } | 109 } |
108 | 110 |
109 void AutocompleteActionPredictorTable::AddRow( | 111 void AutocompleteActionPredictorTable::AddRow( |
110 const AutocompleteActionPredictorTable::Row& row) { | 112 const AutocompleteActionPredictorTable::Row& row) { |
111 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 113 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
112 if (CantAccessDatabase()) | 114 if (CantAccessDatabase()) |
(...skipping 20 matching lines...) Expand all Loading... | |
133 | 135 |
134 if (!DB()->BeginTransaction()) | 136 if (!DB()->BeginTransaction()) |
135 return; | 137 return; |
136 for (Rows::const_iterator it = rows_to_add.begin(); | 138 for (Rows::const_iterator it = rows_to_add.begin(); |
137 it != rows_to_add.end(); ++it) { | 139 it != rows_to_add.end(); ++it) { |
138 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | 140 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
139 base::StringPrintf( | 141 base::StringPrintf( |
140 "INSERT INTO %s " | 142 "INSERT INTO %s " |
141 "(id, user_text, url, number_of_hits, number_of_misses) " | 143 "(id, user_text, url, number_of_hits, number_of_misses) " |
142 "VALUES (?,?,?,?,?)", kAutocompletePredictorTableName).c_str())); | 144 "VALUES (?,?,?,?,?)", kAutocompletePredictorTableName).c_str())); |
145 if (!statement.is_valid()) { | |
146 DB()->RollbackTransaction(); | |
147 return; | |
148 } | |
149 | |
143 BindRowToStatement(*it, &statement); | 150 BindRowToStatement(*it, &statement); |
144 if (!statement.Run()) { | 151 if (!statement.Run()) { |
145 DB()->RollbackTransaction(); | 152 DB()->RollbackTransaction(); |
146 return; | 153 return; |
147 } | 154 } |
148 } | 155 } |
149 for (Rows::const_iterator it = rows_to_update.begin(); | 156 for (Rows::const_iterator it = rows_to_update.begin(); |
150 it != rows_to_update.end(); ++it) { | 157 it != rows_to_update.end(); ++it) { |
151 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | 158 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
152 base::StringPrintf( | 159 base::StringPrintf( |
153 "UPDATE %s " | 160 "UPDATE %s " |
154 "SET id=?, user_text=?, url=?, number_of_hits=?, number_of_misses=?" | 161 "SET id=?, user_text=?, url=?, number_of_hits=?, number_of_misses=?" |
155 " WHERE id=?1", kAutocompletePredictorTableName).c_str())); | 162 " WHERE id=?1", kAutocompletePredictorTableName).c_str())); |
163 if (!statement.is_valid()) { | |
164 DB()->RollbackTransaction(); | |
165 return; | |
166 } | |
167 | |
156 BindRowToStatement(*it, &statement); | 168 BindRowToStatement(*it, &statement); |
157 if (!statement.Run()) { | 169 if (!statement.Run()) { |
158 DB()->RollbackTransaction(); | 170 DB()->RollbackTransaction(); |
159 return; | 171 return; |
160 } | 172 } |
161 DCHECK_GT(DB()->GetLastChangeCount(), 0); | 173 DCHECK_GT(DB()->GetLastChangeCount(), 0); |
162 } | 174 } |
163 DB()->CommitTransaction(); | 175 DB()->CommitTransaction(); |
164 } | 176 } |
165 | 177 |
166 void AutocompleteActionPredictorTable::DeleteRows( | 178 void AutocompleteActionPredictorTable::DeleteRows( |
167 const std::vector<Row::Id>& id_list) { | 179 const std::vector<Row::Id>& id_list) { |
168 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 180 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
169 if (CantAccessDatabase()) | 181 if (CantAccessDatabase()) |
170 return; | 182 return; |
171 | 183 |
172 sql::Statement statement(DB()->GetUniqueStatement(base::StringPrintf( | |
173 "DELETE FROM %s WHERE id=?", | |
174 kAutocompletePredictorTableName).c_str())); | |
175 | |
176 if (!DB()->BeginTransaction()) | 184 if (!DB()->BeginTransaction()) |
177 return; | 185 return; |
178 for (std::vector<Row::Id>::const_iterator it = id_list.begin(); | 186 for (std::vector<Row::Id>::const_iterator it = id_list.begin(); |
179 it != id_list.end(); ++it) { | 187 it != id_list.end(); ++it) { |
188 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
dominich
2012/06/12 19:11:06
this doesn't change through the loop - which is wh
Shishir
2012/06/12 19:35:22
To be consistent with the rest of the code. We do
dominich
2012/06/12 19:44:43
Done.
| |
189 base::StringPrintf( | |
190 "DELETE FROM %s WHERE id=?", | |
191 kAutocompletePredictorTableName).c_str())); | |
192 if (!statement.is_valid()) { | |
dominich
2012/06/12 19:11:06
how can the statement be valid before it's bound?
Shishir
2012/06/12 19:35:22
This function does not check SQL validity (IsSQLVa
dominich
2012/06/12 19:44:43
Done.
| |
193 DB()->RollbackTransaction(); | |
194 return; | |
195 } | |
196 | |
180 statement.BindString(0, *it); | 197 statement.BindString(0, *it); |
181 if (!statement.Run()) { | 198 if (!statement.Run()) { |
182 DB()->RollbackTransaction(); | 199 DB()->RollbackTransaction(); |
183 return; | 200 return; |
184 } | 201 } |
185 statement.Reset(true); | |
186 } | 202 } |
187 DB()->CommitTransaction(); | 203 DB()->CommitTransaction(); |
188 } | 204 } |
189 | 205 |
190 void AutocompleteActionPredictorTable::DeleteAllRows() { | 206 void AutocompleteActionPredictorTable::DeleteAllRows() { |
191 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 207 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
192 if (CantAccessDatabase()) | 208 if (CantAccessDatabase()) |
193 return; | 209 return; |
194 | 210 |
195 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | 211 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
196 base::StringPrintf("DELETE FROM %s", | 212 base::StringPrintf("DELETE FROM %s", |
197 kAutocompletePredictorTableName).c_str())); | 213 kAutocompletePredictorTableName).c_str())); |
214 if (!statement.is_valid()) | |
dominich
2012/06/12 19:11:06
have you seen this happen?
Shishir
2012/06/12 19:35:22
Nope, but again the API suggests doing it
From th
dominich
2012/06/12 19:44:43
Done.
| |
215 return; | |
198 | 216 |
199 statement.Run(); | 217 statement.Run(); |
200 } | 218 } |
201 | 219 |
202 AutocompleteActionPredictorTable::AutocompleteActionPredictorTable() | 220 AutocompleteActionPredictorTable::AutocompleteActionPredictorTable() |
203 : PredictorTableBase() { | 221 : PredictorTableBase() { |
204 } | 222 } |
205 | 223 |
206 AutocompleteActionPredictorTable::~AutocompleteActionPredictorTable() { | 224 AutocompleteActionPredictorTable::~AutocompleteActionPredictorTable() { |
207 } | 225 } |
(...skipping 18 matching lines...) Expand all Loading... | |
226 } | 244 } |
227 | 245 |
228 void AutocompleteActionPredictorTable::LogDatabaseStats() { | 246 void AutocompleteActionPredictorTable::LogDatabaseStats() { |
229 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 247 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
230 if (CantAccessDatabase()) | 248 if (CantAccessDatabase()) |
231 return; | 249 return; |
232 | 250 |
233 sql::Statement count_statement(DB()->GetUniqueStatement( | 251 sql::Statement count_statement(DB()->GetUniqueStatement( |
234 base::StringPrintf("SELECT count(id) FROM %s", | 252 base::StringPrintf("SELECT count(id) FROM %s", |
235 kAutocompletePredictorTableName).c_str())); | 253 kAutocompletePredictorTableName).c_str())); |
236 if (!count_statement.Step()) | 254 if (!count_statement.is_valid() || !count_statement.Step()) |
237 return; | 255 return; |
238 UMA_HISTOGRAM_COUNTS("AutocompleteActionPredictor.DatabaseRowCount", | 256 UMA_HISTOGRAM_COUNTS("AutocompleteActionPredictor.DatabaseRowCount", |
239 count_statement.ColumnInt(0)); | 257 count_statement.ColumnInt(0)); |
240 } | 258 } |
241 | 259 |
242 } // namespace predictors | 260 } // namespace predictors |
OLD | NEW |