• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 24959179949-2999

26 Apr 2026 02:37PM UTC coverage: 69.227% (+0.09%) from 69.141%
24959179949-2999

push

github

tstack
[tests] fix paths

53969 of 77959 relevant lines covered (69.23%)

568944.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/sql_util.hh
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
25
 * ON 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 <array>
36
#include <map>
37
#include <set>
38
#include <string>
39
#include <unordered_map>
40
#include <vector>
41

42
#include <sqlite3.h>
43
#include <sys/time.h>
44
#include <time.h>
45

46
#include "base/attr_line.hh"
47
#include "base/auto_mem.hh"
48
#include "base/intern_string.hh"
49
#include "base/time_util.hh"
50

51
extern const std::array<const char*, 145> sqlite_keywords;
52
extern const char* sql_function_names[];
53
extern const std::unordered_map<unsigned char, const char*>
54
    sql_constraint_names;
55
extern const char* const LNAV_ATTACH_DB;
56

57
inline const char*
58
sql_constraint_op_name(unsigned char op)
291✔
59
{
60
    const auto iter = sql_constraint_names.find(op);
291✔
61
    if (iter == sql_constraint_names.end()) {
291✔
62
        return "??";
×
63
    }
64

65
    return iter->second;
291✔
66
}
67

68
using sqlite_exec_callback = int (*)(void*, int, char**, char**);
69
using db_table_list_t = std::vector<std::string>;
70
using db_table_map_t = std::map<std::string, db_table_list_t>;
71

72
struct sqlite_metadata_callbacks {
73
    sqlite_exec_callback smc_collation_list;
74
    sqlite_exec_callback smc_database_list;
75
    sqlite_exec_callback smc_table_list;
76
    sqlite_exec_callback smc_table_info;
77
    sqlite_exec_callback smc_foreign_key_list;
78
    void* smc_userdata{nullptr};
79
    std::string smc_table_name;
80
    db_table_map_t smc_db_list{};
81
};
82

83
int walk_sqlite_metadata(sqlite3* db, struct sqlite_metadata_callbacks& smc);
84

85
void dump_sqlite_schema(sqlite3* db, std::string& schema_out);
86

87
void attach_sqlite_db(sqlite3* db, const std::string& filename);
88

89
inline ssize_t
90
sql_strftime(char* buffer,
2,507✔
91
             size_t buffer_size,
92
             std::chrono::microseconds micros,
93
             char sep = ' ')
94
{
95
    return lnav::strftime_rfc3339(buffer, buffer_size, micros, sep);
2,507✔
96
}
97

98
inline ssize_t
99
sql_strftime(char* buffer,
2,443✔
100
             size_t buffer_size,
101
             const timeval& tv,
102
             char sep = ' ')
103
{
104
    return sql_strftime(buffer, buffer_size, to_us(tv), sep);
2,443✔
105
}
106

107
void sql_install_logger();
108

109
bool sql_ident_needs_quote(const char* ident);
110

111
auto_mem<char, sqlite3_free> sql_quote_ident(const char* ident);
112

113
std::string sql_safe_ident(const string_fragment& ident);
114

115
std::string sql_quote_text(const std::string& str);
116

117
int guess_type_from_pcre(const std::string& pattern, std::string& collator);
118

119
const char* sqlite3_type_to_string(int type);
120

121
attr_line_t sqlite3_errmsg_to_attr_line(sqlite3* db);
122

123
attr_line_t annotate_sql_with_error(sqlite3* db,
124
                                    const char* sql,
125
                                    const char* tail);
126

127
int sqlite_authorizer(void* pUserData,
128
                      int action_code,
129
                      const char* detail1,
130
                      const char* detail2,
131
                      const char* detail3,
132
                      const char* detail4);
133

134
// RAII helper that, while in scope, collects the names of every table
135
// accessed by subsequent sqlite3_prepare_v2 calls on this thread into
136
// the supplied set.  Used by execute_sql to tell whether the user's
137
// query reads from log-backed vtables.  Nested guards restore the
138
// previous capture target on destruction.
139
class sql_table_capture_guard {
140
public:
141
    explicit sql_table_capture_guard(std::set<std::string>& into);
142
    ~sql_table_capture_guard();
143

144
    sql_table_capture_guard(const sql_table_capture_guard&) = delete;
145
    sql_table_capture_guard& operator=(const sql_table_capture_guard&)
146
        = delete;
147

148
private:
149
    std::set<std::string>* stcg_prev;
150
};
151

152
namespace lnav::sql {
153

154
auto_mem<char, sqlite3_free> mprintf(const char* fmt, ...);
155

156
}  // namespace lnav::sql
157

158
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc