1 /* 2 * Copyright (C) 2020 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 INCLUDE_PERFETTO_TRACE_PROCESSOR_ITERATOR_H_ 18 #define INCLUDE_PERFETTO_TRACE_PROCESSOR_ITERATOR_H_ 19 20 #include <stdint.h> 21 22 #include <memory> 23 24 #include "perfetto/base/export.h" 25 #include "perfetto/trace_processor/basic_types.h" 26 #include "perfetto/trace_processor/status.h" 27 28 namespace perfetto { 29 namespace trace_processor { 30 31 class IteratorImpl; 32 33 // Iterator returning SQL rows satisfied by a query. 34 // 35 // Example usage: 36 // auto sql = "select name, ifnull(cat, "[NULL]") from slice"; 37 // for (auto it = tp.ExecuteQuery(sql); it.Next();) 38 // for (uint32_t i = 0; i < it.ColumnCount(); ++i) { 39 // printf("%s ", it.Get(i).AsString()); 40 // } 41 // printf("\n"); 42 // } 43 class PERFETTO_EXPORT Iterator { 44 public: 45 explicit Iterator(std::unique_ptr<IteratorImpl>); 46 ~Iterator(); 47 48 Iterator(Iterator&) noexcept = delete; 49 Iterator& operator=(Iterator&) = delete; 50 51 Iterator(Iterator&&) noexcept; 52 Iterator& operator=(Iterator&&) noexcept; 53 54 // Forwards the iterator to the next result row and returns a boolean of 55 // whether there is a next row. If this method returns false, 56 // |Status()| should be called to check if there was an error. If 57 // there was no error, this means the EOF was reached. 58 bool Next(); 59 60 // Returns the value associated with the column |col|. Any call to 61 // |Get()| must be preceded by a call to |Next()| returning 62 // true. |col| must be less than the number returned by |ColumnCount()|. 63 SqlValue Get(uint32_t col); 64 65 // Returns the name of the column at index |col|. Can be called even before 66 // calling |Next()|. 67 std::string GetColumnName(uint32_t col); 68 69 // Returns the number of columns in this iterator's query. Can be called 70 // even before calling |Next()|. 71 uint32_t ColumnCount(); 72 73 // Returns the number of statements in the provided SQL (including the final 74 // statement which is iterated using this iterator). Comments and empty 75 // statements are *not* counted i.e. 76 // "SELECT 1; /* comment */; select 2; -- comment" 77 // returns 2 not 4. 78 uint32_t StatementCount(); 79 80 // Returns the number of statements which produced output rows in the provided 81 // SQL (including, potentially, the final statement which is iterated using 82 // this iterator). 83 // This value is guaranteed to be <= |StatementCount()|. 84 uint32_t StatementWithOutputCount(); 85 86 // Returns the status of the iterator. 87 util::Status Status(); 88 89 private: 90 friend class QueryResultSerializer; 91 92 // This is to allow QueryResultSerializer, which is very perf sensitive, to 93 // access direct the impl_ and avoid one extra function call for each cell. 94 template <typename T = IteratorImpl> take_impl()95 std::unique_ptr<T> take_impl() { 96 return std::move(iterator_); 97 } 98 99 // A PIMPL pattern is used to avoid leaking the dependencies on sqlite3.h and 100 // other internal classes. 101 std::unique_ptr<IteratorImpl> iterator_; 102 }; 103 104 } // namespace trace_processor 105 } // namespace perfetto 106 107 #endif // INCLUDE_PERFETTO_TRACE_PROCESSOR_ITERATOR_H_ 108