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 #include <optional> 20 21 #include "perfetto/base/status.h" 22 #include "perfetto/trace_processor/basic_types.h" 23 #include "src/trace_processor/containers/null_term_string_view.h" 24 25 namespace perfetto { 26 namespace trace_processor { 27 namespace sql_argument { 28 29 // Possible types which can be specified in SQL. 30 // This differs from SqlValue::Type by allowing specifying richer 31 // types (e.g. kBool, kInt, kUint and kLong all map to 32 // SqlValue::Type::kLong). This allows more accurate type checking 33 // and, when lots of values are stored, reduced memory usage. 34 enum class Type { 35 kBool, 36 kLong, 37 kDouble, 38 kString, 39 40 // Deprecated types. 41 // TODO(b/380259828): Remove. 42 kInt, 43 kUint, 44 kProto, 45 kFloat, 46 kBytes, 47 }; 48 49 // Represents the definition of an argument from SQL. See 50 // |ParseArgumentDefinitions| for more details. 51 class ArgumentDefinition { 52 public: ArgumentDefinition(std::string dollar_name,Type type)53 ArgumentDefinition(std::string dollar_name, Type type) 54 : dollar_name_(std::move(dollar_name)), type_(type) { 55 PERFETTO_DCHECK(!dollar_name_.empty() && dollar_name_[0] == '$'); 56 } 57 dollar_name()58 NullTermStringView dollar_name() const { 59 return NullTermStringView(dollar_name_); 60 } 61 name()62 NullTermStringView name() const { 63 return NullTermStringView(dollar_name_.c_str() + 1, 64 dollar_name_.size() - 1); 65 } 66 type()67 Type type() const { return type_; } 68 69 bool operator==(const ArgumentDefinition& other) const { 70 return dollar_name_ == other.dollar_name_ && type_ == other.type_; 71 } 72 73 private: 74 std::string dollar_name_; 75 Type type_; 76 }; 77 78 // Returns whether the given |name| is considered valid. 79 // 80 // Names are valid if they only contain alpha-numeric characters or underscores. 81 bool IsValidName(base::StringView name); 82 83 // Parses a string containing a type from SQL and converts it to a Type enum 84 // value. 85 // Returns std::nullopt if |type| did not correspond to any of the enum values. 86 std::optional<Type> ParseType(base::StringView type); 87 88 // Converts an argument type to a string for printing (e.g. in error messages 89 // etc). 90 const char* TypeToHumanFriendlyString(sql_argument::Type type); 91 92 // Converts an argument type to the equivalent SqlValue::Type. 93 SqlValue::Type TypeToSqlValueType(sql_argument::Type type); 94 95 // Parses a string containing argument definitions from SQL and converts it into 96 // a typed list of ArgumentDefintion structs 97 // 98 // An argument defintion is a variable name followed by a type. Variable names 99 // only contain alpha-numeric characters or underscores. Types must be one of 100 // the types corresponding to the Type enum. 101 // 102 // The expected form of |args| is comma-separated list of argument definitions. 103 // For example: foo BYTES, bar PROTO, baz INT, foobar STRING 104 base::Status ParseArgumentDefinitions(const std::string& args, 105 std::vector<ArgumentDefinition>& out); 106 107 // Serialises the given argument list into a string. 108 std::string SerializeArguments(const std::vector<ArgumentDefinition>& args); 109 110 } // namespace sql_argument 111 } // namespace trace_processor 112 } // namespace perfetto 113 114 #endif // SRC_TRACE_PROCESSOR_UTIL_SQL_ARGUMENT_H_ 115