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 sql_util.hh
30  */
31 
32 #ifndef lnav_sql_util_hh
33 #define lnav_sql_util_hh
34 
35 #include <time.h>
36 #include <sys/time.h>
37 
38 #include <sqlite3.h>
39 
40 #include <map>
41 #include <string>
42 #include <vector>
43 
44 #include "base/time_util.hh"
45 #include "attr_line.hh"
46 #include "help_text.hh"
47 
48 extern const char *sql_keywords[145];
49 extern const char *sql_function_names[];
50 
51 typedef int (*sqlite_exec_callback)(void *, int, char **, char **);
52 typedef std::vector<std::string>               db_table_list_t;
53 typedef std::map<std::string, db_table_list_t> db_table_map_t;
54 
55 struct sqlite_metadata_callbacks {
56     sqlite_exec_callback smc_collation_list;
57     sqlite_exec_callback smc_database_list;
58     sqlite_exec_callback smc_table_list;
59     sqlite_exec_callback smc_table_info;
60     sqlite_exec_callback smc_foreign_key_list;
61     void *smc_userdata{nullptr};
62     db_table_map_t smc_db_list{};
63 };
64 
65 int walk_sqlite_metadata(sqlite3 *db, struct sqlite_metadata_callbacks &smc);
66 
67 void dump_sqlite_schema(sqlite3 *db, std::string &schema_out);
68 
69 void attach_sqlite_db(sqlite3 *db, const std::string &filename);
70 
71 ssize_t sql_strftime(char *buffer, size_t buffer_size, lnav::time64_t tim, int millis,
72     char sep = ' ');
73 
sql_strftime(char * buffer,size_t buffer_size,const struct timeval & tv,char sep=' ')74 inline ssize_t sql_strftime(char *buffer, size_t buffer_size,
75                             const struct timeval &tv, char sep = ' ')
76 {
77     return sql_strftime(buffer, buffer_size, tv.tv_sec, tv.tv_usec / 1000, sep);
78 }
79 
80 void sql_install_logger();
81 
82 bool sql_ident_needs_quote(const char *ident);
83 
84 char *sql_quote_ident(const char *ident);
85 
86 std::string sql_safe_ident(const string_fragment &ident);
87 
88 void sql_compile_script(sqlite3 *db,
89                         const char *src_name,
90                         const char *script,
91                         std::vector<sqlite3_stmt *> &stmts,
92                         std::vector<std::string> &errors);
93 
94 void sql_execute_script(sqlite3 *db,
95                         const std::vector<sqlite3_stmt *> &stmts,
96                         std::vector<std::string> &errors);
97 
98 void sql_execute_script(sqlite3 *db,
99                         const char *src_name,
100                         const char *script,
101                         std::vector<std::string> &errors);
102 
103 int guess_type_from_pcre(const std::string &pattern, std::string &collator);
104 
105 /* XXX figure out how to do this with the template */
106 void sqlite_close_wrapper(void *mem);
107 
108 int sqlite_authorizer(void* pUserData, int action_code, const char *detail1,
109                       const char *detail2, const char *detail3,
110                       const char *detail4);
111 
112 extern string_attr_type SQL_COMMAND_ATTR;
113 extern string_attr_type SQL_KEYWORD_ATTR;
114 extern string_attr_type SQL_IDENTIFIER_ATTR;
115 extern string_attr_type SQL_FUNCTION_ATTR;
116 extern string_attr_type SQL_STRING_ATTR;
117 extern string_attr_type SQL_OPERATOR_ATTR;
118 extern string_attr_type SQL_PAREN_ATTR;
119 extern string_attr_type SQL_GARBAGE_ATTR;
120 
121 void annotate_sql_statement(attr_line_t &al_inout);
122 
123 extern std::multimap<std::string, help_text *> sqlite_function_help;
124 
125 std::string sql_keyword_re();
126 std::vector<const help_text *> find_sql_help_for_line(const attr_line_t &al, size_t x);
127 
128 #endif
129