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

tstack / lnav / 23819016647-2908

31 Mar 2026 08:53PM UTC coverage: 69.077% (-0.008%) from 69.085%
23819016647-2908

push

github

tstack
[all_opids] add the name of the opid description to the table

53 of 93 new or added lines in 9 files covered. (56.99%)

5 existing lines in 4 files now uncovered.

53260 of 77102 relevant lines covered (69.08%)

535699.83 hits per line

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

81.61
/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)
187✔
44
{
45
    char timestamp[64];
46

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

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

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

59
    return sqlite3_bind_int64(stmt, index, epoch_ts);
1✔
60
}
61

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

68
inline int
69
bind_to_sqlite(sqlite3_stmt* stmt, int index, intern_string_t ist)
129✔
70
{
71
    if (ist.empty()) {
129✔
NEW
72
        return sqlite3_bind_null(stmt, index);
×
73
    }
74
    return sqlite3_bind_text(
129✔
75
        stmt, index, ist.get(), ist.size(), SQLITE_STATIC);
258✔
76
}
77

78
inline int
79
bind_to_sqlite(sqlite3_stmt* stmt, int index, string_fragment sf)
5✔
80
{
81
    return sqlite3_bind_text(
5✔
82
        stmt, index, sf.data(), sf.length(), SQLITE_TRANSIENT);
5✔
83
}
84

85
inline int
86
bind_to_sqlite(sqlite3_stmt* stmt, int index, const std::string& str)
1,408✔
87
{
88
    return sqlite3_bind_text(
1,408✔
89
        stmt, index, str.c_str(), str.size(), SQLITE_TRANSIENT);
2,816✔
90
}
91

92
inline int
93
bind_to_sqlite(sqlite3_stmt* stmt, int index, int64_t i)
135✔
94
{
95
    return sqlite3_bind_int64(stmt, index, i);
135✔
96
}
97

98
template<typename T>
99
int
NEW
100
bind_to_sqlite(sqlite3_stmt* stmt, int index, const std::optional<T>& v)
×
101
{
NEW
102
    if (v.has_value()) {
×
NEW
103
        return bind_to_sqlite(stmt, index, v.value());
×
104
    }
NEW
105
    return sqlite3_bind_null(stmt, index);
×
106
}
107

108
template<typename... Args, std::size_t... Idx>
109
int
110
bind_values_helper(sqlite3_stmt* stmt,
1,432✔
111
                   std::index_sequence<Idx...> idxs,
112
                   Args... args)
113
{
114
    int rcs[] = {bind_to_sqlite(stmt, Idx + 1, args)...};
1,432✔
115

116
    for (size_t lpc = 0; lpc < idxs.size(); lpc++) {
3,289✔
117
        if (rcs[lpc] != SQLITE_OK) {
1,857✔
118
            log_error("Failed to bind column %zu in statement: %s -- %s",
×
119
                      lpc,
120
                      sqlite3_sql(stmt),
121
                      sqlite3_errstr(rcs[lpc]));
122
            return rcs[lpc];
×
123
        }
124
    }
125

126
    return SQLITE_OK;
1,432✔
127
}
128

129
template<typename... Args>
130
int
131
bind_values(sqlite3_stmt* stmt, Args... args)
1,432✔
132
{
133
    return bind_values_helper(
2,835✔
134
        stmt, std::make_index_sequence<sizeof...(Args)>(), args...);
2,835✔
135
}
136

137
struct prepared_stmt {
138
    explicit prepared_stmt(auto_mem<sqlite3_stmt> stmt)
2,996✔
139
        : ps_stmt(std::move(stmt))
2,996✔
140
    {
141
    }
2,996✔
142

143
    Result<void, std::string> execute()
2,132✔
144
    {
145
        auto rc = sqlite3_reset(this->ps_stmt.in());
2,132✔
146
        if (rc != SQLITE_OK) {
2,132✔
147
            return Err(std::string(
×
148
                sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in()))));
×
149
        }
150

151
        rc = sqlite3_step(this->ps_stmt.in());
2,132✔
152
        if (rc == SQLITE_OK || rc == SQLITE_DONE) {
2,132✔
153
            return Ok();
2,132✔
154
        }
155

156
        auto msg = std::string(
157
            sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in())));
×
158
        return Err(msg);
×
159
    }
160

161
    struct end_of_rows {};
162
    struct fetch_error {
163
        std::string fe_msg;
164
    };
165

166
    void reset() { sqlite3_reset(this->ps_stmt.in()); }
16✔
167

168
    template<typename T>
169
    using fetch_result = mapbox::util::variant<T, end_of_rows, fetch_error>;
170

171
    template<typename T>
172
    fetch_result<T> fetch_row()
885✔
173
    {
174
        auto rc = sqlite3_step(this->ps_stmt.in());
885✔
175
        if (rc == SQLITE_OK || rc == SQLITE_DONE) {
885✔
176
            return end_of_rows{};
868✔
177
        }
178

179
        if (rc == SQLITE_ROW) {
17✔
180
            const auto argc = sqlite3_column_count(this->ps_stmt.in());
17✔
181
            stack_vector<sqlite3_value*>::allocator_type::arena_type arena;
17✔
182
            stack_vector<sqlite3_value*> argv(arena);
17✔
183
            argv.resize(argc);
17✔
184

185
            for (int lpc = 0; lpc < argc; lpc++) {
71✔
186
                argv[lpc] = sqlite3_column_value(this->ps_stmt.in(), lpc);
54✔
187
            }
188

189
            return from_sqlite<T>()(argc, argv.data(), 0);
17✔
190
        }
17✔
191

192
        return fetch_error{
193
            sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in())),
194
        };
195
    }
196

197
    template<typename T, typename F>
198
    Result<void, fetch_error> for_each_row(F func)
839✔
199
    {
200
        std::optional<fetch_error> err;
839✔
201
        auto done = false;
839✔
202

203
        while (!done) {
1,687✔
204
            done = this->template fetch_row<T>().match(
1,696✔
205
                func,
206
                [](end_of_rows) { return true; },
839✔
207
                [&err](const fetch_error& fe) {
848✔
208
                    err = fe;
×
209
                    return true;
×
210
                });
211
        }
212

213
        if (err) {
839✔
214
            return Err(err.value());
×
215
        }
216

217
        return Ok();
839✔
218
    }
839✔
219

220
    auto_mem<sqlite3_stmt> ps_stmt;
221
};
222

223
template<typename... Args>
224
static Result<prepared_stmt, std::string>
225
prepare_stmt(sqlite3* db, const char* sql, Args... args)
2,998✔
226
{
227
    auto_mem<sqlite3_stmt> retval(sqlite3_finalize);
2,998✔
228

229
    if (sqlite3_prepare_v2(db, sql, -1, retval.out(), nullptr) != SQLITE_OK) {
2,998✔
230
        return Err(
231
            fmt::format(FMT_STRING("unable to prepare SQL statement: {}"),
4✔
232
                        sqlite3_errmsg(db)));
4✔
233
    }
234

235
    if (sizeof...(args) > 0) {
236
        if (bind_values(retval.in(), args...) != SQLITE_OK) {
1,303✔
237
            return Err(
238
                fmt::format(FMT_STRING("unable to prepare SQL statement: {}"),
×
239
                            sqlite3_errmsg(db)));
×
240
        }
241
    }
242

243
    return Ok(prepared_stmt{
5,992✔
244
        std::move(retval),
2,996✔
245
    });
5,992✔
246
}
2,998✔
247

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