summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Welin <roger.welin@icloud.com>2022-08-10 23:56:44 +0200
committerGitHub <noreply@github.com>2022-08-10 15:56:44 -0600
commit9ca891fb279c3909a1fbc9875016f0ed433b4a24 (patch)
treeb5ba1c373385436effb2305049acd4e2afd37865
parentb6373fb244bf25c71ccbf42008da68fa16a98606 (diff)
add pg_range support (#15)
* add pg_range support * fix correct table name
-rw-r--r--pg_range.go110
-rw-r--r--server.go6
2 files changed, 116 insertions, 0 deletions
diff --git a/pg_range.go b/pg_range.go
new file mode 100644
index 0000000..bfb71fe
--- /dev/null
+++ b/pg_range.go
@@ -0,0 +1,110 @@
+package postlite
+
+import (
+ "fmt"
+
+ "github.com/mattn/go-sqlite3"
+)
+
+type pgRangeModule struct{}
+
+func (m *pgRangeModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
+ err := c.DeclareVTab(fmt.Sprintf(`
+ CREATE TABLE %s (
+ rngtypid INTEGER,
+ rngsubtype INTEGER,
+ rngmultitypid INTEGER,
+ rngcollation INTEGER,
+ rngsubopc INTEGER,
+ rngcanonical TEXT,
+ rngsubdiff TEXT
+ )`, args[0]))
+ if err != nil {
+ return nil, err
+ }
+ return &pgNamespaceTable{}, nil
+}
+
+func (m *pgRangeModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
+ return m.Create(c, args)
+}
+
+func (m *pgRangeModule) DestroyModule() {}
+
+type pgRangeTable struct{}
+
+func (t *pgRangeTable) Open() (sqlite3.VTabCursor, error) {
+ return &pgTypeCursor{}, nil
+}
+
+func (t *pgRangeTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
+ return &sqlite3.IndexResult{Used: make([]bool, len(cst))}, nil
+}
+
+func (t *pgRangeTable) Disconnect() error { return nil }
+func (t *pgRangeTable) Destroy() error { return nil }
+
+type pgRangeCursor struct {
+ index int
+}
+
+func (c *pgRangeCursor) Column(sctx *sqlite3.SQLiteContext, col int) error {
+ switch col {
+ case 0:
+ sctx.ResultInt(pgRanges[c.index].rngtypid)
+ case 1:
+ sctx.ResultInt(pgRanges[c.index].rngsubtype)
+ case 2:
+ sctx.ResultInt(pgRanges[c.index].rngmultitypid)
+ case 3:
+ sctx.ResultInt(pgRanges[c.index].rngcollation)
+ case 4:
+ sctx.ResultInt(pgRanges[c.index].rngsubopc)
+ case 5:
+ sctx.ResultText(pgRanges[c.index].rngcanonical)
+ case 6:
+ sctx.ResultText(pgRanges[c.index].rngsubdiff)
+ }
+ return nil
+}
+
+func (c *pgRangeCursor) Filter(idxNum int, idxStr string, vals []interface{}) error {
+ c.index = 0
+ return nil
+}
+
+func (c *pgRangeCursor) Next() error {
+ c.index++
+ return nil
+}
+
+func (c *pgRangeCursor) EOF() bool {
+ return c.index >= len(pgTypes)
+}
+
+func (c *pgRangeCursor) Rowid() (int64, error) {
+ return int64(c.index), nil
+}
+
+func (c *pgRangeCursor) Close() error {
+ return nil
+}
+
+type pgRange struct {
+ rngtypid int
+ rngsubtype int
+ rngmultitypid int
+ rngcollation int
+ rngsubopc int
+ rngcanonical string
+ rngsubdiff string
+}
+
+var pgRanges = []pgRange{
+ {3904, 23, 4451, 0, 1978, "", ""},
+ {3906, 1700, 4532, 0, 3125, "", ""},
+ {3908, 1114, 4533, 0, 3128, "", ""},
+ {3910, 1184, 4534, 0, 3127, "", ""},
+ {3912, 1082, 4535, 0, 3122, "", ""},
+ {3926, 20, 4536, 0, 3124, "", ""},
+}
diff --git a/server.go b/server.go
index c49a722..190e0a8 100644
--- a/server.go
+++ b/server.go
@@ -70,6 +70,9 @@ func init() {
if err := conn.CreateModule("pg_class_module", &pgClassModule{}); err != nil {
return fmt.Errorf("cannot register pg_class module")
}
+ if err := conn.CreateModule("pg_range_module", &pgRangeModule{}); err != nil {
+ return fmt.Errorf("cannot register pg_range module")
+ }
return nil
},
})
@@ -303,6 +306,9 @@ func (s *Server) handleStartupMessage(ctx context.Context, c *Conn, msg *pgproto
if _, err := c.db.Exec("CREATE VIRTUAL TABLE IF NOT EXISTS pg_catalog.pg_class USING pg_class_module (oid, relname, relnamespace, reltype, reloftype, relowner, relam, relfilenode, reltablespace, relpages, reltuples, relallvisible, reltoastrelid, relhasindex, relisshared, relpersistence, relkind, relnatts, relchecks, relhasrules, relhastriggers, relhassubclass, relrowsecurity, relforcerowsecurity, relispopulated, relreplident, relispartition, relrewrite, relfrozenxid, relminmxid, relacl, reloptions, relpartbound)"); err != nil {
return fmt.Errorf("create pg_class: %w", err)
}
+ if _, err := c.db.Exec("CREATE VIRTUAL TABLE IF NOT EXISTS pg_catalog.pg_range USING pg_range_module (rngtypid, rngsubtype, rngmultitypid, rngcollation, rngsubopc, rngcanonical, rngsubdiff)"); err != nil {
+ return fmt.Errorf("create pg_range: %w", err)
+ }
return writeMessages(c,
&pgproto3.AuthenticationOk{},