aboutsummaryrefslogtreecommitdiff
path: root/src/trace_processor/sqlite/query_constraints.h
blob: 2c47562cba492280e618d8ce82ab275926d5ed92 (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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_TRACE_PROCESSOR_SQLITE_QUERY_CONSTRAINTS_H_
#define SRC_TRACE_PROCESSOR_SQLITE_QUERY_CONSTRAINTS_H_

#include <sqlite3.h>

#include <vector>

#include "perfetto/ext/base/scoped_file.h"

namespace perfetto {
namespace trace_processor {

// This class stores the constraints (including the order-by information) for
// a query on a sqlite3 virtual table and handles their de/serialization into
// strings.
// This is because the constraint columns and the order-by clauses are passed
// to the xBestIndex method but the constraint values are available only in the
// xFilter method. Unfortunately sqlite vtable API don't give any hint about
// the validity of the constraints (i.e. constraints passed to xBestIndex can
// be used by future xFilter calls in the far future). The only mechanism
// offered by sqlite is the idxStr string which is returned by the vtable
// in the xBestIndex call and passed to each corresponding xFilter call.
class QueryConstraints {
 public:
  using Constraint = sqlite3_index_info::sqlite3_index_constraint;
  using OrderBy = sqlite3_index_info::sqlite3_index_orderby;

  static int FreeSqliteString(char* resource);

  using SqliteString = base::ScopedResource<char*, FreeSqliteString, nullptr>;

  QueryConstraints();
  ~QueryConstraints();
  QueryConstraints(QueryConstraints&&) noexcept;
  QueryConstraints& operator=(QueryConstraints&&);

  // Two QueryConstraints with the same constraint and orderby vectors
  // are equal.
  bool operator==(const QueryConstraints& other) const;

  void AddConstraint(int column, unsigned char op);

  void AddOrderBy(int column, unsigned char desc);

  void ClearOrderBy() { order_by_.clear(); }

  // Converts the constraints and order by information to a string for
  // use by sqlite.
  SqliteString ToNewSqlite3String() const;

  // Deserializes the string into QueryConstraints. String given is in the form
  // C{# of constraints},col1,op1,col2,op2...,O{# of order by},col1,desc1...
  // For example C1,0,3,O2,1,0,4,1
  static QueryConstraints FromString(const char* idxStr);

  const std::vector<OrderBy>& order_by() const { return order_by_; }

  const std::vector<Constraint>& constraints() const { return constraints_; }

 private:
  QueryConstraints(const QueryConstraints&) = delete;
  QueryConstraints& operator=(const QueryConstraints&) = delete;

  std::vector<OrderBy> order_by_;
  std::vector<Constraint> constraints_;
};

}  // namespace trace_processor
}  // namespace perfetto

#endif  // SRC_TRACE_PROCESSOR_SQLITE_QUERY_CONSTRAINTS_H_