xref: /aosp_15_r20/external/perfetto/src/trace_processor/sqlite/bindings/sqlite_result.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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_SQLITE_BINDINGS_SQLITE_RESULT_H_
18 #define SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_RESULT_H_
19 
20 #include <sqlite3.h>  // IWYU pragma: export
21 #include <cstdint>
22 #include <memory>
23 
24 namespace perfetto::trace_processor::sqlite::result {
25 
26 // This file contains wraps the sqlite3_result_* functions which tell SQLite
27 // about the result of executing a function or calling a xColumn on a virtual
28 // table.
29 
30 const auto kSqliteStatic = reinterpret_cast<sqlite3_destructor_type>(0);
31 const auto kSqliteTransient = reinterpret_cast<sqlite3_destructor_type>(-1);
32 
Null(sqlite3_context * ctx)33 inline void Null(sqlite3_context* ctx) {
34   sqlite3_result_null(ctx);
35 }
36 
Long(sqlite3_context * ctx,int64_t res)37 inline void Long(sqlite3_context* ctx, int64_t res) {
38   sqlite3_result_int64(ctx, res);
39 }
40 
Double(sqlite3_context * ctx,double res)41 inline void Double(sqlite3_context* ctx, double res) {
42   sqlite3_result_double(ctx, res);
43 }
44 
RawString(sqlite3_context * ctx,const char * str,int size,sqlite3_destructor_type destructor)45 inline void RawString(sqlite3_context* ctx,
46                       const char* str,
47                       int size,
48                       sqlite3_destructor_type destructor) {
49   sqlite3_result_text(ctx, str, size, destructor);
50 }
RawString(sqlite3_context * ctx,const char * str,sqlite3_destructor_type destructor)51 inline void RawString(sqlite3_context* ctx,
52                       const char* str,
53                       sqlite3_destructor_type destructor) {
54   RawString(ctx, str, -1, destructor);
55 }
StaticString(sqlite3_context * ctx,const char * str)56 inline void StaticString(sqlite3_context* ctx, const char* str) {
57   RawString(ctx, str, kSqliteStatic);
58 }
TransientString(sqlite3_context * ctx,const char * str)59 inline void TransientString(sqlite3_context* ctx, const char* str) {
60   RawString(ctx, str, kSqliteTransient);
61 }
62 
RawBytes(sqlite3_context * ctx,const void * bytes,int size,sqlite3_destructor_type destructor)63 inline void RawBytes(sqlite3_context* ctx,
64                      const void* bytes,
65                      int size,
66                      sqlite3_destructor_type destructor) {
67   sqlite3_result_blob(ctx, bytes, size, destructor);
68 }
StaticBytes(sqlite3_context * ctx,const void * bytes,int size)69 inline void StaticBytes(sqlite3_context* ctx, const void* bytes, int size) {
70   RawBytes(ctx, bytes, size, kSqliteStatic);
71 }
TransientBytes(sqlite3_context * ctx,const void * bytes,int size)72 inline void TransientBytes(sqlite3_context* ctx, const void* bytes, int size) {
73   RawBytes(ctx, bytes, size, kSqliteTransient);
74 }
75 
Error(sqlite3_context * ctx,const char * error)76 inline void Error(sqlite3_context* ctx, const char* error) {
77   sqlite3_result_error(ctx, error, -1);
78 }
79 
Value(sqlite3_context * ctx,sqlite3_value * value)80 inline void Value(sqlite3_context* ctx, sqlite3_value* value) {
81   sqlite3_result_value(ctx, value);
82 }
83 
RawPointer(sqlite3_context * ctx,void * ptr,const char * name,sqlite3_destructor_type destructor)84 inline void RawPointer(sqlite3_context* ctx,
85                        void* ptr,
86                        const char* name,
87                        sqlite3_destructor_type destructor) {
88   sqlite3_result_pointer(ctx, ptr, name, destructor);
89 }
StaticPointer(sqlite3_context * ctx,void * ptr,const char * name)90 inline void StaticPointer(sqlite3_context* ctx, void* ptr, const char* name) {
91   RawPointer(ctx, ptr, name, nullptr);
92 }
93 template <typename T>
UniquePointer(sqlite3_context * ctx,std::unique_ptr<T> ptr,const char * name)94 inline void UniquePointer(sqlite3_context* ctx,
95                           std::unique_ptr<T> ptr,
96                           const char* name) {
97   sqlite::result::RawPointer(ctx, ptr.release(), name, [](void* ptr) {
98     std::unique_ptr<T>(static_cast<T*>(ptr));
99   });
100 }
101 
102 }  // namespace perfetto::trace_processor::sqlite::result
103 
104 #endif  // SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_RESULT_H_
105