1 /**
2  * Copyright (c) 2013, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * @file sqlite-extension-func.h
30  */
31 
32 #ifndef lnav_sqlite_extension_func_h
33 #define lnav_sqlite_extension_func_h
34 
35 #include <stdint.h>
36 #include <sqlite3.h>
37 
38 #include <string>
39 #include <map>
40 
41 #include "help_text_formatter.hh"
42 
43 struct FuncDef {
44     const char *zName{nullptr};
45     signed char nArg{0};
46     int eTextRep{0};          /* 1: UTF-16.  0: UTF-8 */
47     uint8_t needCollSeq{0};
48     void (*xFunc)(sqlite3_context*,int,sqlite3_value **){nullptr};
49     help_text fd_help{};
50 
with_flagsFuncDef51     FuncDef &with_flags(int flags) {
52         this->eTextRep = flags;
53         return *this;
54     };
55 };
56 
57 struct FuncDefAgg {
58     const char *zName{nullptr};
59     signed char nArg{0};
60     uint8_t needCollSeq{0};
61     void (*xStep)(sqlite3_context*,int,sqlite3_value**){nullptr};
62     void (*xFinalize)(sqlite3_context*){nullptr};
63     help_text fda_help{};
64 };
65 
66 typedef int (*sqlite_registration_func_t)(struct FuncDef **basic_funcs,
67                                           struct FuncDefAgg **agg_funcs);
68 
69 int common_extension_functions(struct FuncDef **basic_funcs,
70                                struct FuncDefAgg **agg_funcs);
71 
72 int state_extension_functions(struct FuncDef **basic_funcs,
73                               struct FuncDefAgg **agg_funcs);
74 
75 int string_extension_functions(struct FuncDef **basic_funcs,
76                                struct FuncDefAgg **agg_funcs);
77 
78 int network_extension_functions(struct FuncDef **basic_funcs,
79                                 struct FuncDefAgg **agg_funcs);
80 
81 int fs_extension_functions(struct FuncDef **basic_funcs,
82                            struct FuncDefAgg **agg_funcs);
83 
84 int json_extension_functions(struct FuncDef **basic_funcs,
85                              struct FuncDefAgg **agg_funcs);
86 
87 int time_extension_functions(struct FuncDef **basic_funcs,
88                              struct FuncDefAgg **agg_funcs);
89 
90 extern sqlite_registration_func_t sqlite_registration_funcs[];
91 
92 int register_sqlite_funcs(sqlite3 *db, sqlite_registration_func_t *reg_funcs);
93 
94 extern "C" {
95 int sqlite3_db_dump(
96     sqlite3 *db,               /* The database connection */
97     const char *zSchema,       /* Which schema to dump.  Usually "main". */
98     const char *zTable,        /* Which table to dump.  NULL means everything. */
99     int (*xCallback)(const char*,void*),   /* Output sent to this callback */
100     void *pArg                             /* Second argument of the callback */
101 );
102 }
103 
104 #endif
105