1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/password_manager/core/browser/statistics_table.h"
7 #include "sql/connection.h"
8 #include "sql/statement.h"
10 namespace password_manager
{
13 // Convenience enum for interacting with SQL queries that use all the columns.
14 enum LoginTableColumns
{
15 COLUMN_ORIGIN_DOMAIN
= 0,
23 StatisticsTable::StatisticsTable() : db_(nullptr) {
26 StatisticsTable::~StatisticsTable() {
29 bool StatisticsTable::Init(sql::Connection
* db
) {
31 if (!db_
->DoesTableExist("stats")) {
33 "CREATE TABLE stats ("
34 "origin_domain VARCHAR NOT NULL PRIMARY KEY, "
35 "nopes_count INTEGER, "
36 "dismissal_count INTEGER, "
37 "start_date INTEGER NOT NULL)";
38 if (!db_
->Execute(query
))
44 bool StatisticsTable::AddRow(const InteractionsStats
& stats
) {
45 sql::Statement
s(db_
->GetCachedStatement(
47 "INSERT OR REPLACE INTO stats "
48 "(origin_domain, nopes_count, dismissal_count, start_date) "
49 "VALUES (?, ?, ?, ?)"));
50 s
.BindString(COLUMN_ORIGIN_DOMAIN
, stats
.origin_domain
.spec());
51 s
.BindInt(COLUMN_NOPES
, stats
.nopes_count
);
52 s
.BindInt(COLUMN_DISMISSALS
, stats
.dismissal_count
);
53 s
.BindInt64(COLUMN_DATE
, stats
.start_date
.ToInternalValue());
57 bool StatisticsTable::RemoveRow(const GURL
& domain
) {
58 sql::Statement
s(db_
->GetCachedStatement(SQL_FROM_HERE
,
59 "DELETE FROM stats WHERE "
60 "origin_domain = ? "));
61 s
.BindString(0, domain
.spec());
65 scoped_ptr
<InteractionsStats
> StatisticsTable::GetRow(const GURL
& domain
) {
67 "SELECT origin_domain, nopes_count, "
68 "dismissal_count, start_date FROM stats WHERE origin_domain == ?";
69 sql::Statement
s(db_
->GetCachedStatement(SQL_FROM_HERE
, query
));
70 s
.BindString(0, domain
.spec());
72 scoped_ptr
<InteractionsStats
> stats(new InteractionsStats
);
73 stats
->origin_domain
= GURL(s
.ColumnString(COLUMN_ORIGIN_DOMAIN
));
74 stats
->nopes_count
= s
.ColumnInt(COLUMN_NOPES
);
75 stats
->dismissal_count
= s
.ColumnInt(COLUMN_DISMISSALS
);
77 base::Time::FromInternalValue(s
.ColumnInt64(COLUMN_DATE
));
80 return scoped_ptr
<InteractionsStats
>();
83 } // namespace password_manager