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

tstack / lnav / 17589970077-2502

09 Sep 2025 05:00PM UTC coverage: 65.196% (-5.0%) from 70.225%
17589970077-2502

push

github

tstack
[format] add fields for source file/line

Knowing the source file/line context in a log
message can help find log messages when using
log2src.

56 of 70 new or added lines in 2 files covered. (80.0%)

13954 existing lines in 210 files now uncovered.

45516 of 69814 relevant lines covered (65.2%)

404154.37 hits per line

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

77.22
/src/sqlitepp.client.hh
1
/**
2
 * Copyright (c) 2022, 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

30
#ifndef lnav_sqlitepp_client_hh
31
#define lnav_sqlitepp_client_hh
32

33
#include <sqlite3.h>
34

35
#include "base/auto_mem.hh"
36
#include "base/intern_string.hh"
37
#include "base/lnav_log.hh"
38
#include "base/short_alloc.h"
39
#include "sql_util.hh"
40
#include "vtab_module.hh"
41

42
inline int
43
bind_to_sqlite(sqlite3_stmt* stmt, int index, const struct timeval& tv)
143✔
44
{
45
    char timestamp[64];
46

47
    sql_strftime(timestamp, sizeof(timestamp), tv, 'T');
143✔
48

49
    return sqlite3_bind_text(stmt, index, timestamp, -1, SQLITE_TRANSIENT);
286✔
50
}
51

52
inline int
UNCOV
53
bind_to_sqlite(sqlite3_stmt* stmt,
×
54
               int index,
55
               const std::chrono::system_clock::time_point& tp)
56
{
UNCOV
57
    auto epoch_ts = tp.time_since_epoch().count();
×
58

UNCOV
59
    return sqlite3_bind_int64(stmt, index, epoch_ts);
×
60
}
61

62
inline int
63
bind_to_sqlite(sqlite3_stmt* stmt, int index, const char* str)
64
{
65
    return sqlite3_bind_text(stmt, index, str, -1, SQLITE_TRANSIENT);
66
}
67

68
inline int
69
bind_to_sqlite(sqlite3_stmt* stmt, int index, intern_string_t ist)
99✔
70
{
71
    return sqlite3_bind_text(
99✔
72
        stmt, index, ist.get(), ist.size(), SQLITE_TRANSIENT);
198✔
73
}
74

75
inline int
UNCOV
76
bind_to_sqlite(sqlite3_stmt* stmt, int index, string_fragment sf)
×
77
{
UNCOV
78
    return sqlite3_bind_text(
×
UNCOV
79
        stmt, index, sf.data(), sf.length(), SQLITE_TRANSIENT);
×
80
}
81

82
inline int
83
bind_to_sqlite(sqlite3_stmt* stmt, int index, const std::string& str)
1,168✔
84
{
85
    return sqlite3_bind_text(
1,168✔
86
        stmt, index, str.c_str(), str.size(), SQLITE_TRANSIENT);
2,336✔
87
}
88

89
inline int
90
bind_to_sqlite(sqlite3_stmt* stmt, int index, int64_t i)
105✔
91
{
92
    return sqlite3_bind_int64(stmt, index, i);
105✔
93
}
94

95
template<typename... Args, std::size_t... Idx>
96
int
97
bind_values_helper(sqlite3_stmt* stmt,
1,190✔
98
                   std::index_sequence<Idx...> idxs,
99
                   Args... args)
100
{
101
    int rcs[] = {bind_to_sqlite(stmt, Idx + 1, args)...};
1,190✔
102

103
    for (size_t lpc = 0; lpc < idxs.size(); lpc++) {
2,705✔
104
        if (rcs[lpc] != SQLITE_OK) {
1,515✔
UNCOV
105
            log_error("Failed to bind column %d in statement: %s -- %s",
×
106
                      lpc,
107
                      sqlite3_sql(stmt),
108
                      sqlite3_errstr(rcs[lpc]));
UNCOV
109
            return rcs[lpc];
×
110
        }
111
    }
112

113
    return SQLITE_OK;
1,190✔
114
}
115

116
template<typename... Args>
117
int
118
bind_values(sqlite3_stmt* stmt, Args... args)
1,190✔
119
{
120
    return bind_values_helper(
2,358✔
121
        stmt, std::make_index_sequence<sizeof...(Args)>(), args...);
2,358✔
122
}
123

124
struct prepared_stmt {
125
    explicit prepared_stmt(auto_mem<sqlite3_stmt> stmt)
10,824✔
126
        : ps_stmt(std::move(stmt))
10,824✔
127
    {
128
    }
10,824✔
129

130
    Result<void, std::string> execute()
10,095✔
131
    {
132
        auto rc = sqlite3_reset(this->ps_stmt.in());
10,095✔
133
        if (rc != SQLITE_OK) {
10,095✔
UNCOV
134
            return Err(std::string(
×
135
                sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in()))));
×
136
        }
137

138
        rc = sqlite3_step(this->ps_stmt.in());
10,095✔
139
        if (rc == SQLITE_OK || rc == SQLITE_DONE) {
10,095✔
140
            return Ok();
10,095✔
141
        }
142

143
        auto msg = std::string(
UNCOV
144
            sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in())));
×
145
        return Err(msg);
×
146
    }
147

148
    struct end_of_rows {};
149
    struct fetch_error {
150
        std::string fe_msg;
151
    };
152

UNCOV
153
    void reset() { sqlite3_reset(this->ps_stmt.in()); }
×
154

155
    template<typename T>
156
    using fetch_result = mapbox::util::variant<T, end_of_rows, fetch_error>;
157

158
    template<typename T>
159
    fetch_result<T> fetch_row()
9,740✔
160
    {
161
        auto rc = sqlite3_step(this->ps_stmt.in());
9,740✔
162
        if (rc == SQLITE_OK || rc == SQLITE_DONE) {
9,740✔
163
            return end_of_rows{};
729✔
164
        }
165

166
        if (rc == SQLITE_ROW) {
9,011✔
167
            const auto argc = sqlite3_column_count(this->ps_stmt.in());
9,011✔
168
            stack_vector<sqlite3_value*>::allocator_type::arena_type arena;
9,011✔
169
            stack_vector<sqlite3_value*> argv(arena);
9,011✔
170
            argv.resize(argc);
9,011✔
171

172
            for (int lpc = 0; lpc < argc; lpc++) {
18,059✔
173
                argv[lpc] = sqlite3_column_value(this->ps_stmt.in(), lpc);
9,048✔
174
            }
175

176
            return from_sqlite<T>()(argc, argv.data(), 0);
9,011✔
177
        }
9,011✔
178

179
        return fetch_error{
180
            sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in())),
181
        };
182
    }
183

184
    template<typename T, typename F>
185
    Result<void, fetch_error> for_each_row(F func)
708✔
186
    {
187
        std::optional<fetch_error> err;
708✔
188
        auto done = false;
708✔
189

190
        while (!done) {
10,427✔
191
            done = this->template fetch_row<T>().match(
19,438✔
192
                func,
193
                [](end_of_rows) { return true; },
708✔
194
                [&err](const fetch_error& fe) {
9,719✔
UNCOV
195
                    err = fe;
×
196
                    return true;
×
197
                });
198
        }
199

200
        if (err) {
708✔
201
            return Err(err.value());
×
202
        }
203

204
        return Ok();
708✔
205
    }
708✔
206

207
    auto_mem<sqlite3_stmt> ps_stmt;
208
};
209

210
template<typename... Args>
211
static Result<prepared_stmt, std::string>
212
prepare_stmt(sqlite3* db, const char* sql, Args... args)
10,825✔
213
{
214
    auto_mem<sqlite3_stmt> retval(sqlite3_finalize);
10,825✔
215

216
    if (sqlite3_prepare_v2(db, sql, -1, retval.out(), nullptr) != SQLITE_OK) {
10,825✔
217
        return Err(
218
            fmt::format(FMT_STRING("unable to prepare SQL statement: {}"),
2✔
219
                        sqlite3_errmsg(db)));
2✔
220
    }
221

222
    if (sizeof...(args) > 0) {
223
        if (bind_values(retval.in(), args...) != SQLITE_OK) {
1,091✔
224
            return Err(
UNCOV
225
                fmt::format(FMT_STRING("unable to prepare SQL statement: {}"),
×
UNCOV
226
                            sqlite3_errmsg(db)));
×
227
        }
228
    }
229

230
    return Ok(prepared_stmt{
21,648✔
231
        std::move(retval),
10,824✔
232
    });
21,648✔
233
}
10,825✔
234

235
#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