summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/activity_actions.cc
blob: e443e2f9688d801667c4150c7374ae76d3840990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>
#include "base/logging.h"
#include "base/stringprintf.h"
#include "chrome/browser/extensions/api_actions.h"

namespace extensions {

const char* Action::kTableBasicFields =
    "extension_id LONGVARCHAR NOT NULL, "
    "time INTEGER NOT NULL";

Action::Action(const std::string& extension_id,
               const base::Time& time)
    : extension_id_(extension_id),
      time_(time) {}

// static
bool Action::InitializeTableInternal(sql::Connection* db,
                                     const char* table_name,
                                     const char* content_fields[],
                                     const int num_content_fields) {
  if (!db->DoesTableExist(table_name)) {
    std::string table_creator = base::StringPrintf(
        "CREATE TABLE %s (%s", table_name, kTableBasicFields);
    for (int i = 0; i < num_content_fields; i++) {
      table_creator += base::StringPrintf(", %s LONGVARCHAR",
                                          content_fields[i]);
    }
    table_creator += ")";
    if (!db->Execute(table_creator.c_str()))
      return false;
  } else {
    // In case we ever want to add new fields, this initializes them to be
    // empty strings.
    for (int i = 0; i < num_content_fields; i++) {
      if (!db->DoesColumnExist(table_name, content_fields[i])) {
        std::string table_updater = base::StringPrintf(
            "ALTER TABLE %s ADD COLUMN %s LONGVARCHAR; ",
             table_name,
             content_fields[i]);
        if (!db->Execute(table_updater.c_str()))
          return false;
      }
    }
  }
  return true;
}

}  // namespace extensions