• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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>
21 #include <cstdint>
22 
23 namespace perfetto::trace_processor::sqlite::result {
24 
25 // This file contains wraps the sqlite3_result_* functions which tell SQLite
26 // about the result of executing a function or calling a xColumn on a virtual
27 // table.
28 
29 const auto kSqliteStatic = reinterpret_cast<sqlite3_destructor_type>(0);
30 const auto kSqliteTransient = reinterpret_cast<sqlite3_destructor_type>(-1);
31 
Null(sqlite3_context * ctx)32 inline void Null(sqlite3_context* ctx) {
33   sqlite3_result_null(ctx);
34 }
35 
Long(sqlite3_context * ctx,int64_t res)36 inline void Long(sqlite3_context* ctx, int64_t res) {
37   sqlite3_result_int64(ctx, res);
38 }
39 
Double(sqlite3_context * ctx,double res)40 inline void Double(sqlite3_context* ctx, double res) {
41   sqlite3_result_double(ctx, res);
42 }
43 
RawString(sqlite3_context * ctx,const char * str,int size,sqlite3_destructor_type destructor)44 inline void RawString(sqlite3_context* ctx,
45                       const char* str,
46                       int size,
47                       sqlite3_destructor_type destructor) {
48   sqlite3_result_text(ctx, str, size, destructor);
49 }
RawString(sqlite3_context * ctx,const char * str,sqlite3_destructor_type destructor)50 inline void RawString(sqlite3_context* ctx,
51                       const char* str,
52                       sqlite3_destructor_type destructor) {
53   RawString(ctx, str, -1, destructor);
54 }
StaticString(sqlite3_context * ctx,const char * str)55 inline void StaticString(sqlite3_context* ctx, const char* str) {
56   RawString(ctx, str, kSqliteStatic);
57 }
TransientString(sqlite3_context * ctx,const char * str)58 inline void TransientString(sqlite3_context* ctx, const char* str) {
59   RawString(ctx, str, kSqliteTransient);
60 }
61 
RawBytes(sqlite3_context * ctx,const void * bytes,int size,sqlite3_destructor_type destructor)62 inline void RawBytes(sqlite3_context* ctx,
63                      const void* bytes,
64                      int size,
65                      sqlite3_destructor_type destructor) {
66   sqlite3_result_blob(ctx, bytes, size, destructor);
67 }
StaticBytes(sqlite3_context * ctx,const void * bytes,int size)68 inline void StaticBytes(sqlite3_context* ctx, const void* bytes, int size) {
69   RawBytes(ctx, bytes, size, kSqliteStatic);
70 }
TransientBytes(sqlite3_context * ctx,const void * bytes,int size)71 inline void TransientBytes(sqlite3_context* ctx, const void* bytes, int size) {
72   RawBytes(ctx, bytes, size, kSqliteTransient);
73 }
74 
Error(sqlite3_context * ctx,const char * error)75 inline void Error(sqlite3_context* ctx, const char* error) {
76   sqlite3_result_error(ctx, error, -1);
77 }
78 
Value(sqlite3_context * ctx,sqlite3_value * value)79 inline void Value(sqlite3_context* ctx, sqlite3_value* value) {
80   sqlite3_result_value(ctx, value);
81 }
82 
RawPointer(sqlite3_context * ctx,void * ptr,const char * name,sqlite3_destructor_type destructor)83 inline void RawPointer(sqlite3_context* ctx,
84                        void* ptr,
85                        const char* name,
86                        sqlite3_destructor_type destructor) {
87   sqlite3_result_pointer(ctx, ptr, name, destructor);
88 }
StaticPointer(sqlite3_context * ctx,void * ptr,const char * name)89 inline void StaticPointer(sqlite3_context* ctx, void* ptr, const char* name) {
90   RawPointer(ctx, ptr, name, nullptr);
91 }
92 
93 }  // namespace perfetto::trace_processor::sqlite::result
94 
95 #endif  // SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_RESULT_H_
96