9#ifndef HEADER_SQLITEPP_VTAB_IFACE_INCLUDED
10#define HEADER_SQLITEPP_VTAB_IFACE_INCLUDED
12#include "database_iface.hpp"
13#include "exception_iface.hpp"
14#include "value_iface.hpp"
15#include "context_iface.hpp"
16#include "memory_iface.hpp"
49 template<
class T =
void>
53 "template argument must be void or a pointer to a trivially destructible type");
66 {
return { this->
c_ptr()->aConstraint, size_t(this->
c_ptr()->nConstraint)}; };
69 {
return { this->
c_ptr()->aOrderBy, size_t(this->
c_ptr()->nOrderBy)}; };
71 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 10, 0)
78 {
return uint64_t(this->
c_ptr()->colUsed); }
87 const char *
collation(
int constraint_idx)
const noexcept
98 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 38, 0)
105 bool is_in(
int constraint_idx)
const noexcept
125 {
return { this->
c_ptr()->aConstraintUsage, size_t(this->
c_ptr()->nConstraint)}; }
132 {
return { this->
c_ptr()->aConstraintUsage, size_t(this->
c_ptr()->nConstraint)}; }
136 {
return this->
c_ptr()->idxNum; }
139 { this->
c_ptr()->idxNum = val; }
148 {
return (T)this->
c_ptr()->idxStr; }
165 this->
c_ptr()->idxStr = (
char *)data;
166 this->
c_ptr()->needToFreeIdxStr = allocated;
202 {
return this->
c_ptr()->orderByConsumed != 0; }
205 { this->
c_ptr()->orderByConsumed = val; }
209 {
return this->
c_ptr()->estimatedCost; }
212 { this->
c_ptr()->estimatedCost = val; }
214 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 8, 2)
221 {
return int64_t(this->
c_ptr()->estimatedRows); }
231 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 9, 0)
238 {
return this->
c_ptr()->idxFlags; }
245 { this->
c_ptr()->idxFlags = val; }
276 template<
class Derived>
312 cursor(cursor &) =
delete;
313 cursor & operator=(cursor &) =
delete;
317 {
return const_cast<cursor *
>(
this); }
343 template<
class D=Derived>
346 [[maybe_unused]]
typename D::index_data_type idx_data,
347 [[maybe_unused]]
int argc,
348 [[maybe_unused]]
value ** argv)
375 template<
class D=Derived>
378 [[maybe_unused]]
int argc,
379 [[maybe_unused]]
value ** argv)
457 {
return static_cast<Derived *
>(this->pVtab); }
499 template<
class D=Derived>
505 typename D::constructor_data_type data,
506 void(*destructor)(
typename D::constructor_data_type)
noexcept =
nullptr)
509 db.
create_module(name, vtab::get_module(), data, destructor);
528 template<
class D=Derived>
537 db.
create_module(name, vtab::get_module(), data.release(), [](
typename D::constructor_data_type ptr)
noexcept {
566 template<
class D=Derived>
567 bool best_index(index_info<typename D::index_data_type> & info)
const
570 info.set_estimated_cost(0);
587 template<
class D=Derived>
588 std::unique_ptr<typename D::cursor> open()
591 return std::unique_ptr<typename D::cursor>(
592 new typename D::cursor(
static_cast<D *
>(
this))
603 static sqlite3_module * get_module();
620 void set_error_message(
error & err)
const
622 auto me =
const_cast<vtab *
>(
this);
626 me->zErrMsg = (
char *)message.
release();
629 void set_error_message(exception & ex)
const
630 { set_error_message(ex.error()); }
632 void set_error_message(std::exception & ex)
const
634 auto me =
const_cast<vtab *
>(
this);
638 me->zErrMsg =
nullptr;
640 auto message = ex.
what();
641 const auto len =
strlen(message) + 1;
642 if (
char *
const ret = (
char *)sqlite_allocate_nothrow(len))
644 memcpy(ret, message, len);
649 static constexpr void check_requirements();
651 #define SQLITEPP_DECLARE_IMPL(xname, name) \
652 static std::remove_pointer_t<decltype(sqlite3_module::xname)> name##_impl
653 #define SQLITEPP_DECLARE_CONDITIONAL_IMPL(xname, name) \
654 SQLITEPP_DECLARE_IMPL(xname, name); \
655 static constexpr decltype(sqlite3_module::xname) get_##name##_impl()
657 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xCreate, create);
658 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xConnect, connect);
659 SQLITEPP_DECLARE_IMPL(xBestIndex, best_index);
660 SQLITEPP_DECLARE_IMPL(xDisconnect, disconnect);
661 SQLITEPP_DECLARE_IMPL(xDestroy, destroy);
662 SQLITEPP_DECLARE_IMPL(xOpen, open);
663 SQLITEPP_DECLARE_IMPL(xClose, close);
664 SQLITEPP_DECLARE_IMPL(xEof, eof);
665 SQLITEPP_DECLARE_IMPL(xFilter, filter);
666 SQLITEPP_DECLARE_IMPL(xNext, next);
667 SQLITEPP_DECLARE_IMPL(xColumn, column);
668 SQLITEPP_DECLARE_IMPL(xRowid, rowid);
669 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xUpdate, update);
670 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xFindFunction, find_function);
671 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xBegin, begin);
672 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xSync, sync);
673 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xCommit, commit);
674 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xRollback, rollback);
675 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xRename, rename);
676 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xSavepoint, savepoint);
677 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xRelease, release);
678 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xRollbackTo, rollback_to);
680 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 26, 0)
682 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xShadowName, shadow_name);
686 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 44, 0)
688 SQLITEPP_DECLARE_CONDITIONAL_IMPL(xIntegrity, integrity);
692 #undef SQLITEPP_DECLARE_IMPL
693 #undef SQLITEPP_DECLARE_CONDITIONAL_IMPL
SQL Function Context Object.
Definition context_iface.hpp:38
void result(std::nullptr_t) noexcept
Return NULL from the implemented SQL function.
Definition context_iface.hpp:108
Database Connection.
Definition database_iface.hpp:109
void create_module(const string_param &name, const sqlite3_module *mod)
Register a virtual table implementation.
Definition database_iface.hpp:962
Carries information about SQLite error.
Definition exception_iface.hpp:39
std::unique_ptr< const char, free_message > message_ptr
An owning pointer to SQLite error message.
Definition exception_iface.hpp:59
message_ptr extract_message() &noexcept
Move the message out of this object.
Definition exception_iface.hpp:150
Exception used to report any SQLite errors.
Definition exception_iface.hpp:180
sqlite3_index_info * c_ptr() const noexcept
Definition handle.hpp:45
Virtual Table Indexing Information.
Definition vtab_iface.hpp:51
void set_index_data(std::unique_ptr< X > data) noexcept
Set the index data.
Definition vtab_iface.hpp:197
int64_t estimated_rows() const noexcept
Returns estimated number of rows returned.
Definition vtab_iface.hpp:220
sqlite3_index_info::sqlite3_index_constraint constraint
Alias for unwieldy C struct name.
Definition vtab_iface.hpp:56
void set_estimated_cost(double val) noexcept
Sets estimated cost of using this index.
Definition vtab_iface.hpp:211
T index_data() const noexcept
Returns data associated with the index.
Definition vtab_iface.hpp:147
bool order_by_consumed() const noexcept
Returns whether the cursor output is already ordered.
Definition vtab_iface.hpp:201
int distinct() const noexcept
Determine if the query is DISTINCT.
Definition vtab_iface.hpp:95
void set_order_by_consumed(bool val) noexcept
Sets whether the cursor output is already ordered.
Definition vtab_iface.hpp:204
const char * collation(int constraint_idx) const noexcept
Determine the collation for a constraint.
Definition vtab_iface.hpp:87
void set_index_number(int val) noexcept
Sets number used to identify the index.
Definition vtab_iface.hpp:138
span< const orderby > orderbys() const noexcept
Returns the table of ORDER BY clause constraints.
Definition vtab_iface.hpp:68
span< const constraint > constraints() const noexcept
Returns the table of WHERE clause constraints.
Definition vtab_iface.hpp:65
span< const constraint_usage > constraints_usage() const noexcept
Returns the desired usage of the constraints.
Definition vtab_iface.hpp:124
sqlite3_index_info::sqlite3_index_orderby orderby
Alias for unwieldy C struct name.
Definition vtab_iface.hpp:60
int index_flags() const noexcept
Returns mask of SQLITE_INDEX_SCAN_ flags.
Definition vtab_iface.hpp:237
uint64_t columns_used() const noexcept
Returns mask of columns used by statement.
Definition vtab_iface.hpp:77
void handle_in(int constraint_idx, bool handle) const noexcept
Set all-at-once processing of an IN operator.
Definition vtab_iface.hpp:113
void set_index_data(X *data, bool allocated=false) noexcept
Set the index data.
Definition vtab_iface.hpp:163
void set_estimated_rows(int64_t val) noexcept
Sets estimated number of rows returned.
Definition vtab_iface.hpp:226
sqlite3_index_info::sqlite3_index_constraint_usage constraint_usage
Alias for unwieldy C struct name.
Definition vtab_iface.hpp:58
double estimated_cost() const noexcept
Returns estimated cost of using this index.
Definition vtab_iface.hpp:208
span< constraint_usage > constraints_usage() noexcept
Returns the desired usage of the constraints.
Definition vtab_iface.hpp:131
bool is_in(int constraint_idx) const noexcept
Determine if a constraint is an IN that can be processed all at once.
Definition vtab_iface.hpp:105
void set_index_data(std::unique_ptr< X, sqlite_deleter< X > > data) noexcept
Set the index data.
Definition vtab_iface.hpp:180
void set_index_flags(int val) noexcept
Sets mask of SQLITE_INDEX_SCAN_ flags.
Definition vtab_iface.hpp:244
int index_number() const noexcept
Returns number used to identify the index.
Definition vtab_iface.hpp:135
Memory deleter that uses sqlite3_free.
Definition memory_iface.hpp:32
Dynamically Typed Value Object.
Definition value_iface.hpp:36
Base class for cursors.
Definition vtab_iface.hpp:309
void next()
Advances the cursor.
Definition vtab_iface.hpp:408
Derived * owner() const noexcept
Returns the owning vtab - derived class.
Definition vtab_iface.hpp:456
sqlite3_vtab_cursor * c_ptr() const noexcept
Access the underlying sqlite3_vtab_cursor struct.
Definition vtab_iface.hpp:316
cursor(Derived *owner)
Constructs an instance with a given owner.
Definition vtab_iface.hpp:443
sqlite_int64 rowid() const
Retrieves the rowid of the row cursor is currently pointing at.
Definition vtab_iface.hpp:437
bool eof() const noexcept
Whether the cursor reached the end.
Definition vtab_iface.hpp:394
void filter(int idx, int argc, value **argv)
Begins a search of a virtual table.
Definition vtab_iface.hpp:377
void filter(int idx, typename D::index_data_type idx_data, int argc, value **argv)
Begins a search of a virtual table.
Definition vtab_iface.hpp:345
void column(context &ctxt, int idx) const
Retrieves the value of the virtual table column in a row cursor is currently pointing at.
Definition vtab_iface.hpp:424
Base class for virtual table object implementations.
Definition vtab_iface.hpp:278
void index_data_type
Type of data stored in index_info and passed between best_index and filter.
Definition vtab_iface.hpp:299
SQLITEPP_ENABLE_IFP(static,(std::is_pointer_v< typename D::constructor_data_type >), void) create_module(database &db
Register a virtual table implementation with a database connection.
vtab(const vtab &)=delete
You cannot copy (or move) this class.
vtab()
This class is default constructible only by derived classes.
Definition vtab_iface.hpp:607
~vtab()
This class is destructible only by derived classes.
Definition vtab_iface.hpp:615
static void create_module(database &db, const string_param &name)
Register a virtual table implementation with a database connection.
Definition vtab_iface.hpp:477
void constructor_data_type
Type of data passed via create_module to the constructor(s).
Definition vtab_iface.hpp:288
vtab & operator=(const vtab &)=delete
You cannot assign this class.
Marker type that tells the constructor of Derived to be used to connect to an existing table.
Definition vtab_iface.hpp:464
Marker type that marks the constructor of Derived to be used to create a new table.
Definition vtab_iface.hpp:461
std::span< T > span
Alias or reimplementation of std::span.
Definition span.hpp:36
basic_string_param< char > string_param
Convenience typedef.
Definition string_param.hpp:58
T is_trivially_destructible_v
ThinSQLite++ namespace.
Definition backup_iface.hpp:17