File: url_table.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (6)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/file_manager/indexing/url_table.h"

namespace ash::file_manager {

namespace {

#define URL_TABLE "url_table"
#define URL_ID "url_id"
#define URL_SPEC "url_spec"
#define URL_INDEX "url_index"

// The statement used to create the URL table.
static constexpr char kCreateUrlTableQuery[] =
    // clang-format off
    "CREATE TABLE IF NOT EXISTS " URL_TABLE "("
        URL_ID " INTEGER PRIMARY KEY AUTOINCREMENT,"
        URL_SPEC " TEXT NOT NULL)";
// clang-format on

// The statement used to delete a URL from the database by URL ID.
static constexpr char kDeleteUrlQuery[] =
    // clang-format off
    "DELETE FROM " URL_TABLE " WHERE " URL_ID "=?";
// clang-format on

// The statement used fetch the ID of the URL.
static constexpr char kGetUrlIdQuery[] =
    // clang-format off
     "SELECT " URL_ID " FROM " URL_TABLE " WHERE " URL_SPEC "=?";
// clang-format on

// The statement used fetch the ID of the URL.
static constexpr char kGetUrlValueQuery[] =
    // clang-format off
     "SELECT " URL_SPEC " FROM " URL_TABLE " WHERE " URL_ID "=?";
// clang-format on

// The statement used to insert a new URL into the table.
static constexpr char kInsertUrlQuery[] =
    // clang-format off
     "INSERT INTO " URL_TABLE "(" URL_SPEC ") VALUES (?) RETURNING " URL_ID;
// clang-format on

// The statement that creates an index on url_spec column.
static constexpr char kCreateUrlIndexQuery[] =
    // clang-format off
    "CREATE UNIQUE INDEX IF NOT EXISTS " URL_INDEX " ON " URL_TABLE
    "(" URL_SPEC ")";
// clang-format on

// The statement that changes URL from one string to another
static constexpr char kChangeUrlSpecQuery[] =
    // clang-format off
   "UPDATE OR ROLLBACK " URL_TABLE " SET " URL_SPEC "=? WHERE "
   URL_SPEC "=? RETURNING " URL_ID;
// clang-format on

}  // namespace

UrlTable::UrlTable(sql::Database* db) : TextTable(db, "" URL_TABLE "") {}
UrlTable::~UrlTable() = default;

int64_t UrlTable::DeleteUrl(const GURL& url) {
  DCHECK(url.is_valid());
  return DeleteValue(url.spec());
}

int64_t UrlTable::GetUrlId(const GURL& url) const {
  DCHECK(url.is_valid());
  return GetValueId(url.spec());
}

std::optional<std::string> UrlTable::GetUrlSpec(int64_t url_id) const {
  return GetValue(url_id);
}

int64_t UrlTable::GetOrCreateUrlId(const GURL& url) {
  DCHECK(url.is_valid());
  return GetOrCreateValueId(url.spec());
}

int64_t UrlTable::ChangeUrl(const GURL& from, const GURL& to) {
  DCHECK(from.is_valid());
  DCHECK(to.is_valid());
  return ChangeValue(from.spec(), to.spec());
}

std::unique_ptr<sql::Statement> UrlTable::MakeGetValueIdStatement() const {
  return std::make_unique<sql::Statement>(
      db_->GetCachedStatement(SQL_FROM_HERE, kGetUrlIdQuery));
}

std::unique_ptr<sql::Statement> UrlTable::MakeGetValueStatement() const {
  return std::make_unique<sql::Statement>(
      db_->GetCachedStatement(SQL_FROM_HERE, kGetUrlValueQuery));
}

std::unique_ptr<sql::Statement> UrlTable::MakeInsertStatement() const {
  return std::make_unique<sql::Statement>(
      db_->GetCachedStatement(SQL_FROM_HERE, kInsertUrlQuery));
}

std::unique_ptr<sql::Statement> UrlTable::MakeDeleteStatement() const {
  return std::make_unique<sql::Statement>(
      db_->GetCachedStatement(SQL_FROM_HERE, kDeleteUrlQuery));
}

std::unique_ptr<sql::Statement> UrlTable::MakeCreateTableStatement() const {
  return std::make_unique<sql::Statement>(
      db_->GetCachedStatement(SQL_FROM_HERE, kCreateUrlTableQuery));
}

std::unique_ptr<sql::Statement> UrlTable::MakeCreateIndexStatement() const {
  return std::make_unique<sql::Statement>(
      db_->GetCachedStatement(SQL_FROM_HERE, kCreateUrlIndexQuery));
}

std::unique_ptr<sql::Statement> UrlTable::MakeChangeValueStatement() const {
  return std::make_unique<sql::Statement>(
      db_->GetCachedStatement(SQL_FROM_HERE, kChangeUrlSpecQuery));
}

}  // namespace ash::file_manager