• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACE_PROCESSOR_UTIL_SQL_ARGUMENT_H_
18 #define SRC_TRACE_PROCESSOR_UTIL_SQL_ARGUMENT_H_
19 
20 #include <optional>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "perfetto/base/logging.h"
26 #include "perfetto/base/status.h"
27 #include "perfetto/ext/base/string_view.h"
28 #include "perfetto/trace_processor/basic_types.h"
29 #include "src/trace_processor/containers/null_term_string_view.h"
30 
31 namespace perfetto::trace_processor::sql_argument {
32 
33 // Possible types which can be specified in SQL.
34 // This differs from SqlValue::Type by allowing specifying richer
35 // types (e.g. kBool, kInt, kUint and kLong all map to
36 // SqlValue::Type::kLong). This allows more accurate type checking
37 // and, when lots of values are stored, reduced memory usage.
38 enum class Type {
39   kBool,
40   kLong,
41   kDouble,
42   kString,
43 
44   // Deprecated types.
45   // TODO(b/380259828): Remove.
46   kInt,
47   kUint,
48   kProto,
49   kFloat,
50   kBytes,
51 };
52 
53 // Represents the definition of an argument from SQL. See
54 // |ParseArgumentDefinitions| for more details.
55 class ArgumentDefinition {
56  public:
ArgumentDefinition(std::string dollar_name,Type type)57   ArgumentDefinition(std::string dollar_name, Type type)
58       : dollar_name_(std::move(dollar_name)), type_(type) {
59     PERFETTO_DCHECK(!dollar_name_.empty() && dollar_name_[0] == '$');
60   }
61 
dollar_name()62   NullTermStringView dollar_name() const {
63     return NullTermStringView(dollar_name_);
64   }
65 
name()66   NullTermStringView name() const {
67     return NullTermStringView(dollar_name_.c_str() + 1,
68                               dollar_name_.size() - 1);
69   }
70 
type()71   Type type() const { return type_; }
72 
73   bool operator==(const ArgumentDefinition& other) const {
74     return dollar_name_ == other.dollar_name_ && type_ == other.type_;
75   }
76 
77  private:
78   std::string dollar_name_;
79   Type type_;
80 };
81 
82 // Returns whether the given |name| is considered valid.
83 //
84 // Names are valid if they only contain alpha-numeric characters or underscores.
85 bool IsValidName(base::StringView name);
86 
87 // Parses a string containing a type from SQL and converts it to a Type enum
88 // value.
89 // Returns std::nullopt if |type| did not correspond to any of the enum values.
90 std::optional<Type> ParseType(base::StringView type);
91 
92 // Converts an argument type to a string for printing (e.g. in error messages
93 // etc).
94 const char* TypeToHumanFriendlyString(sql_argument::Type type);
95 
96 // Converts an argument type to the equivalent SqlValue::Type.
97 SqlValue::Type TypeToSqlValueType(sql_argument::Type type);
98 
99 // Parses a string containing argument definitions from SQL and converts it into
100 // a typed list of ArgumentDefintion structs
101 //
102 // An argument defintion is a variable name followed by a type. Variable names
103 // only contain alpha-numeric characters or underscores. Types must be one of
104 // the types corresponding to the Type enum.
105 //
106 // The expected form of |args| is comma-separated list of argument definitions.
107 // For example: foo BYTES, bar PROTO, baz INT, foobar STRING
108 base::Status ParseArgumentDefinitions(const std::string& args,
109                                       std::vector<ArgumentDefinition>& out);
110 
111 // Serialises the given argument list into a string.
112 std::string SerializeArguments(const std::vector<ArgumentDefinition>& args);
113 
114 }  // namespace perfetto::trace_processor::sql_argument
115 
116 #endif  // SRC_TRACE_PROCESSOR_UTIL_SQL_ARGUMENT_H_
117