[sql] fix joins on some vtables

This commit is contained in:
Timothy Stack 2019-02-18 14:44:59 -08:00
parent 43243c7ba3
commit 4614cbcb75
5 changed files with 37 additions and 7 deletions

View File

@ -104,11 +104,10 @@ CREATE TABLE fstat (
sqlite3_vtab_cursor base;
string c_pattern;
static_root_mem<glob_t, globfree> c_glob;
size_t c_path_index;
size_t c_path_index{0};
struct stat c_stat;
cursor(sqlite3_vtab *vt)
: base({vt}), c_path_index(0) {
cursor(sqlite3_vtab *vt) : base({vt}) {
memset(&this->c_stat, 0, sizeof(this->c_stat));
};
@ -129,6 +128,10 @@ CREATE TABLE fstat (
return SQLITE_OK;
};
int reset() {
return SQLITE_OK;
}
int eof() {
return this->c_path_index >= this->c_glob->gl_pathc;
};

View File

@ -103,6 +103,10 @@ CREATE TABLE regexp_capture (
this->c_context.set_count(0);
};
int reset() {
return SQLITE_OK;
};
int next() {
if (this->c_index >= (this->c_context.get_count() - 1)) {
this->c_input->pi_offset = this->c_input->pi_next_offset;

View File

@ -542,8 +542,8 @@ CREATE TABLE lnav_view_filters (
struct lnav_view_filter_stats : public tvt_iterator_cursor<lnav_view_filter_stats>,
public lnav_view_filter_base {
static constexpr const char *CREATE_STMT = R"(
-- Access lnav's filters through this table.
CREATE TABLE lnav_view_filters (
-- Access statistics for filters through this table.
CREATE TABLE lnav_view_filter_stats (
view_name TEXT, -- The name of the view.
filter_id INTEGER, -- The filter identifier.
hits INTEGER -- The number of lines that matched this filter.
@ -574,6 +574,11 @@ CREATE TABLE lnav_view_filters (
}
};
static const char *CREATE_FILTER_VIEW = R"(
CREATE VIEW lnav_view_filters_and_stats AS
SELECT * FROM lnav_view_filters LEFT NATURAL JOIN lnav_view_filter_stats
)";
int register_views_vtab(sqlite3 *db)
{
static vtab_module<lnav_views> LNAV_VIEWS_MODULE;
@ -595,5 +600,11 @@ int register_views_vtab(sqlite3 *db)
rc = LNAV_VIEW_FILTER_STATS_MODULE.create(db, "lnav_view_filter_stats");
assert(rc == SQLITE_OK);
char *errmsg;
if (sqlite3_exec(db, CREATE_FILTER_VIEW, nullptr, nullptr, &errmsg) != SQLITE_OK) {
log_error("Unable to create filter view: %s", errmsg);
}
return rc;
}

View File

@ -610,7 +610,9 @@ struct vtab_module {
static int vt_filter(sqlite3_vtab_cursor *p_vtc,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv) {
return SQLITE_OK;
auto *p_cur = (typename T::cursor *) p_vtc;
return p_cur->reset();
}
static int tvt_update(sqlite3_vtab *tab,
@ -698,6 +700,14 @@ struct tvt_iterator_cursor {
this->iter = handler.begin();
};
int reset() {
T handler;
this->iter = handler.begin();
return SQLITE_OK;
};
int next()
{
T handler;

View File

@ -666,7 +666,7 @@ EOF
schema_dump() {
${lnav_test} -n -c ';.schema' ${test_dir}/logfile_access_log.0 | head -n15
${lnav_test} -n -c ';.schema' ${test_dir}/logfile_access_log.0 | head -n17
}
run_test schema_dump
@ -678,6 +678,8 @@ CREATE VIRTUAL TABLE lnav_views USING lnav_views_impl();
CREATE VIRTUAL TABLE lnav_view_stack USING lnav_view_stack_impl();
CREATE VIRTUAL TABLE lnav_view_filters USING lnav_view_filters_impl();
CREATE VIRTUAL TABLE lnav_view_filter_stats USING lnav_view_filter_stats_impl();
CREATE VIEW lnav_view_filters_and_stats AS
SELECT * FROM lnav_view_filters LEFT NATURAL JOIN lnav_view_filter_stats;
CREATE VIRTUAL TABLE lnav_file USING lnav_file_impl();
CREATE VIRTUAL TABLE regexp_capture USING regexp_capture_impl();
CREATE VIRTUAL TABLE fstat USING fstat_impl();