|
1 | 1 | package plugin
|
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gammazero/deque" |
| 5 | + "github.com/mattn/go-sqlite3" |
| 6 | +) |
| 7 | + |
| 8 | +// This file links the plugin to the SQLite Virtual Table interface |
| 9 | + |
| 10 | +// SQLiteModule is a struct that holds the information about the SQLite module |
| 11 | +// |
| 12 | +// For each table that the plugin provides and for each profile, a new SQLiteModule |
| 13 | +// should be created and registered in the main program |
| 14 | +type SQLiteModule struct { |
| 15 | + PluginPath string |
| 16 | + PluginManifest PluginManifest |
| 17 | + TableIndex int |
| 18 | + client *InternalClient |
| 19 | +} |
| 20 | + |
| 21 | +// SQLiteTable that holds the information needed for the BestIndex and Open methods |
| 22 | +type SQLiteTable struct { |
| 23 | + nextCursor int |
| 24 | + tableIndex int |
| 25 | + schema DatabaseSchema |
| 26 | + client *InternalClient |
| 27 | +} |
| 28 | + |
| 29 | +// SQLiteCursor holds the information needed for the Column, Filter, EOF and Next methods |
| 30 | +type SQLiteCursor struct { |
| 31 | + tableIndex int |
| 32 | + cursorIndex int |
| 33 | + schema DatabaseSchema |
| 34 | + client *InternalClient |
| 35 | + noMoreRows bool |
| 36 | + rows *deque.Deque[[]interface{}] // A ring buffer to store the rows before sending them to SQLite |
| 37 | + nextCursor *int |
| 38 | +} |
| 39 | + |
| 40 | +// EponymousOnlyModule is a method that is used to mark the table as eponymous-only |
| 41 | +// |
| 42 | +// See https://www.sqlite.org/vtab.html#eponymous_virtual_tables for more information |
| 43 | +func (m *SQLiteModule) EponymousOnlyModule() {} |
| 44 | + |
| 45 | +// Create is called when the virtual table is created e.g. CREATE VIRTUAL TABLE or SELECT...FROM(epo_table) |
| 46 | +// |
| 47 | +// Its main job is to create a new RPC client and return the needed information |
| 48 | +// for the SQLite virtual table methods |
| 49 | +func (m *SQLiteModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { |
| 50 | + return nil, nil |
| 51 | +} |
| 52 | + |
| 53 | +// Connect is called when the virtual table is connected |
| 54 | +// |
| 55 | +// Because it's an eponymous-only module, the method must be identical to Create |
| 56 | +func (m *SQLiteModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { |
| 57 | + return m.Create(c, args) |
| 58 | +} |
| 59 | + |
| 60 | +// BestIndex is called when the virtual table is queried |
| 61 | +// to figure out the best way to query the table |
| 62 | +// |
| 63 | +// However, we don't use it that way but only to serialize the constraints |
| 64 | +// for the Filter method |
| 65 | +func (t *SQLiteTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) { |
| 66 | + return nil, nil |
| 67 | +} |
| 68 | + |
| 69 | +// Open is called when a new cursor is opened |
| 70 | +// |
| 71 | +// It should return a new cursor |
| 72 | +func (t *SQLiteTable) Open() (sqlite3.VTabCursor, error) { |
| 73 | + return nil, nil |
| 74 | +} |
| 75 | + |
| 76 | +// Close is called when the cursor is no longer needed |
| 77 | +func (c *SQLiteCursor) Close() error { return nil } |
| 78 | + |
| 79 | +// These methods are not used in this plugin |
| 80 | +func (v *SQLiteTable) Disconnect() error { return nil } |
| 81 | +func (v *SQLiteTable) Destroy() error { return nil } |
| 82 | +func (v *SQLiteModule) DestroyModule() {} |
| 83 | + |
| 84 | +// Column is called when a column is queried |
| 85 | +// |
| 86 | +// It should return the value of the column |
| 87 | +func (c *SQLiteCursor) Column(cst int) (interface{}, error) { return nil, nil } |
| 88 | + |
| 89 | +// EOF is called after each row is queried to check if there are more rows |
| 90 | +func (c *SQLiteCursor) EOF() bool { return false } |
| 91 | + |
| 92 | +// Next is called to move the cursor to the next row |
| 93 | +// |
| 94 | +// If noMoreRows is set to false, and the cursor is at the end of the rows, |
| 95 | +// Next will ask the plugin for more rows |
| 96 | +// |
| 97 | +// If noMoreRows is set to true, Next will set EOF to true |
| 98 | +func (c *SQLiteCursor) Next() error { return nil } |
| 99 | + |
| 100 | +// RowID is called to get the row ID of the current row |
| 101 | +func (c *SQLiteCursor) RowID() (int64, error) { return 0, nil } |
| 102 | + |
| 103 | +func (c *SQLiteCursor) Filter(idxNum int, idxStr string, vals []interface{}) error { |
| 104 | + // Filter can be called several times with the same cursor |
| 105 | + // Each time, it is supposed to reset the cursor to the beginning |
| 106 | + // Therefore, it should wipe out all the cursor fields |
| 107 | + // |
| 108 | + // Moreover, for the sake of simplicity, we will create a new cursor on the plugin side, |
| 109 | + // which means the cursorIndex must be incremented while not yelding any conflict |
| 110 | + // How to fix this? We must have access to the parent struct (SQLiteTable). |
| 111 | + return nil |
| 112 | +} |
0 commit comments