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