summaryrefslogtreecommitdiff
path: root/chromium/sql/recover_module/parsing.cc
blob: 383ebe275b28c799ae03784bb135fae1511025aa (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2019 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 "sql/recover_module/parsing.h"

#include <cstddef>
#include <tuple>
#include <utility>

#include "base/check.h"
#include "base/notreached.h"
#include "base/optional.h"
#include "base/strings/strcat.h"
#include "base/strings/string_piece.h"
#include "sql/recover_module/record.h"

namespace sql {
namespace recover {

namespace {

// This module defines whitespace as space (0x20).
constexpr bool IsWhiteSpace(char character) {
  return (character == ' ');
}

// Splits a token out of a SQL string representing a column type.
//
// Tokens are separated by space (0x20) characters.
//
// The SQL must not start with a space character.
//
// Returns the token and the rest of the SQL string. Consumes the space after
// the returned token -- the rest of the SQL string will not start with space.
std::pair<base::StringPiece, base::StringPiece> SplitToken(
    base::StringPiece sql) {
  DCHECK(sql.empty() || !IsWhiteSpace(sql[0]));

  size_t token_end = 0;
  while (token_end < sql.length() && !IsWhiteSpace(sql[token_end]))
    ++token_end;

  size_t split = token_end;
  while (split < sql.length() && IsWhiteSpace(sql[split]))
    ++split;

  return {sql.substr(0, token_end), sql.substr(split)};
}

// Column types.
constexpr base::StringPiece kIntegerSql("INTEGER");
constexpr base::StringPiece kFloatSql("FLOAT");
constexpr base::StringPiece kTextSql("TEXT");
constexpr base::StringPiece kBlobSql("BLOB");
constexpr base::StringPiece kNumericSql("NUMERIC");
constexpr base::StringPiece kRowidSql("ROWID");
constexpr base::StringPiece kAnySql("ANY");

// SQL keywords recognized by the recovery module.
constexpr base::StringPiece kStrictSql("STRICT");
constexpr base::StringPiece kNonNullSql1("NOT");
constexpr base::StringPiece kNonNullSql2("NULL");

base::Optional<ModuleColumnType> ParseColumnType(
    base::StringPiece column_type_sql) {
  if (column_type_sql == kIntegerSql)
    return ModuleColumnType::kInteger;
  if (column_type_sql == kFloatSql)
    return ModuleColumnType::kFloat;
  if (column_type_sql == kTextSql)
    return ModuleColumnType::kText;
  if (column_type_sql == kBlobSql)
    return ModuleColumnType::kBlob;
  if (column_type_sql == kNumericSql)
    return ModuleColumnType::kNumeric;
  if (column_type_sql == kRowidSql)
    return ModuleColumnType::kRowId;
  if (column_type_sql == kAnySql)
    return ModuleColumnType::kAny;

  return base::nullopt;
}

// Returns a view into a SQL string representing the column type.
//
// The backing string is guaranteed to live for as long as the process runs.
base::StringPiece ColumnTypeToSql(ModuleColumnType column_type) {
  switch (column_type) {
    case ModuleColumnType::kInteger:
      return kIntegerSql;
    case ModuleColumnType::kFloat:
      return kFloatSql;
    case ModuleColumnType::kText:
      return kTextSql;
    case ModuleColumnType::kBlob:
      return kBlobSql;
    case ModuleColumnType::kNumeric:
      return kNumericSql;
    case ModuleColumnType::kRowId:
      return kIntegerSql;  // rowids are ints.
    case ModuleColumnType::kAny:
      return base::StringPiece();
  }
  NOTREACHED();
}

}  // namespace

std::string RecoveredColumnSpec::ToCreateTableSql() const {
  base::StringPiece not_null_sql = (is_non_null) ? " NOT NULL" : "";
  return base::StrCat(
      {this->name, " ", ColumnTypeToSql(this->type), not_null_sql});
}

bool RecoveredColumnSpec::IsAcceptableValue(ValueType value_type) const {
  if (value_type == ValueType::kNull)
    return !is_non_null || type == ModuleColumnType::kRowId;
  if (type == ModuleColumnType::kAny)
    return true;

  if (value_type == ValueType::kInteger) {
    return type == ModuleColumnType::kInteger ||
           (!is_strict && type == ModuleColumnType::kFloat);
  }
  if (value_type == ValueType::kFloat) {
    return type == ModuleColumnType::kFloat ||
           (!is_strict && type == ModuleColumnType::kFloat);
  }
  if (value_type == ValueType::kText)
    return type == ModuleColumnType::kText;
  if (value_type == ValueType::kBlob) {
    return type == ModuleColumnType::kBlob ||
           (!is_strict && type == ModuleColumnType::kText);
  }
  NOTREACHED() << "Unimplemented value type";
  return false;
}

RecoveredColumnSpec ParseColumnSpec(const char* sqlite_arg) {
  // The result is invalid until its |name| member is set.
  RecoveredColumnSpec result;
  base::StringPiece sql(sqlite_arg);

  base::StringPiece column_name;
  std::tie(column_name, sql) = SplitToken(sql);
  if (column_name.empty()) {
    // Empty column names are invalid.
    DCHECK(!result.IsValid());
    return result;
  }

  base::StringPiece column_type_sql;
  std::tie(column_type_sql, sql) = SplitToken(sql);
  base::Optional<ModuleColumnType> column_type =
      ParseColumnType(column_type_sql);
  if (!column_type.has_value()) {
    // Invalid column type.
    DCHECK(!result.IsValid());
    return result;
  }
  result.type = column_type.value();

  // Consume keywords.
  result.is_non_null = result.type == ModuleColumnType::kRowId;
  while (!sql.empty()) {
    base::StringPiece token;
    std::tie(token, sql) = SplitToken(sql);

    if (token == kStrictSql) {
      if (result.type == ModuleColumnType::kAny) {
        // STRICT is incompatible with ANY.
        DCHECK(!result.IsValid());
        return result;
      }

      result.is_strict = true;
      continue;
    }

    if (token != kNonNullSql1) {
      // Invalid SQL keyword.
      DCHECK(!result.IsValid());
      return result;
    }
    std::tie(token, sql) = SplitToken(sql);
    if (token != kNonNullSql2) {
      // Invalid SQL keyword.
      DCHECK(!result.IsValid());
      return result;
    }
    result.is_non_null = true;
  }

  result.name = column_name.as_string();
  return result;
}

TargetTableSpec ParseTableSpec(const char* sqlite_arg) {
  base::StringPiece sql(sqlite_arg);

  size_t separator_pos = sql.find('.');
  if (separator_pos == base::StringPiece::npos) {
    // The default attachment point name is "main".
    return TargetTableSpec{"main", sqlite_arg};
  }

  if (separator_pos == 0) {
    // Empty attachment point names are invalid.
    return TargetTableSpec();
  }

  base::StringPiece db_name = sql.substr(0, separator_pos);
  base::StringPiece table_name = sql.substr(separator_pos + 1);
  return TargetTableSpec{db_name.as_string(), table_name.as_string()};
}

}  // namespace recover
}  // namespace sql