From e8a6696bcbea8ccfafabb48a8929dd724b5467a9 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 21 Aug 2023 13:57:47 +0200 Subject: [PATCH] work in progress: msgstore: store message --- storage/msgstore/msgstore.go | 19 +++++++++++++++ storage/msgstore/sql/tables_pg.sql | 17 +++++++++++++ storage/msgstore/sql/tables_sqlite.sql | 17 +++++++++++++ tests/etc/etc_sql.go | 2 +- tests/storage_test.go | 33 ++++++++++++++++---------- 5 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 storage/msgstore/msgstore.go create mode 100644 storage/msgstore/sql/tables_pg.sql create mode 100644 storage/msgstore/sql/tables_sqlite.sql diff --git a/storage/msgstore/msgstore.go b/storage/msgstore/msgstore.go new file mode 100644 index 0000000..9e5649d --- /dev/null +++ b/storage/msgstore/msgstore.go @@ -0,0 +1,19 @@ +package msgstore + +import ( + "fmt" + + lib "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/storage/sql" +) + +func Store(db *sql.Storage, msg lib.Message) { + q := fmt.Sprintf(sql_insert_msg, "messages") + fmt.Println(q) + db.Exec(q, msg.Domain(), msg.Action(), msg.Class(), msg.Item(), msg.Payload()) +} + +const ( + sql_insert_msg = `insert into %s (domain, action, class, item, payload) + values ($1, $2, $3, $4, $5)` +) diff --git a/storage/msgstore/sql/tables_pg.sql b/storage/msgstore/sql/tables_pg.sql new file mode 100644 index 0000000..6fe7d5d --- /dev/null +++ b/storage/msgstore/sql/tables_pg.sql @@ -0,0 +1,17 @@ +create table messages ( + id bigserial not null primary key, + domain text, + action text, + class text, + item text, + sender text, + payload jsonb, + tstamp timestamptz default current_timestamp +); + +-- indexes + +create index idx_msg on messages using btree (domain, action, class, item); +create index idx_msg_item on messages using btree (domain, class, item); +--create index idx_msg_action on messages using btree (action); -- obsolete + diff --git a/storage/msgstore/sql/tables_sqlite.sql b/storage/msgstore/sql/tables_sqlite.sql new file mode 100644 index 0000000..5e6dbfb --- /dev/null +++ b/storage/msgstore/sql/tables_sqlite.sql @@ -0,0 +1,17 @@ +create table messages ( + id integer not null primary key, + domain text, + action text, + class text, + item text, + sender text, + payload jsonb, + tstamp timestamptz default current_timestamp +); + +-- indexes + +create index idx_msg on messages (domain, action, class, item); +create index idx_msg_item on messages (domain, class, item); +--create index idx_msg_action on messages using btree (action); -- obsolete + diff --git a/tests/etc/etc_sql.go b/tests/etc/etc_sql.go index 8f1fb53..4a25158 100644 --- a/tests/etc/etc_sql.go +++ b/tests/etc/etc_sql.go @@ -34,6 +34,6 @@ func OverridesSql() config.Settings { return config.Settings{ //SQLITE_CONNSTR: ":memory", SQLITE_CONNSTR: "data/scopes.sqlite", - PGSQL_CONNSTR: "user=ccotest password=cco dbname=ccotest", + PGSQL_CONNSTR: "user=ccotest password=cco dbname=scpstest", } } diff --git a/tests/storage_test.go b/tests/storage_test.go index eec5dea..99fd6cf 100644 --- a/tests/storage_test.go +++ b/tests/storage_test.go @@ -4,6 +4,8 @@ import ( tbase "testing" "git.sr.ht/~cco/go-scopes/common/testing" + "git.sr.ht/~cco/go-scopes/core/message" + "git.sr.ht/~cco/go-scopes/storage/msgstore" "git.sr.ht/~cco/go-scopes/storage/sql" _ "git.sr.ht/~cco/go-scopes/storage/sql/pgsql" _ "git.sr.ht/~cco/go-scopes/storage/sql/sqlite" @@ -28,6 +30,7 @@ func TestPgsql(tb *tbase.T) { func DoTests(t *testing.T, cfg *sql.Cfg, db *sql.Storage) { t.Run("base", func(t *testing.T) { BaseTest(t, cfg, db) }) + t.Run("msgstore", func(t *testing.T) { MsgstoreTest(t, cfg, db) }) } type greet struct { @@ -45,18 +48,23 @@ func (g *greet) ScanP(rows *sql.Rows) error { } func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) { - db.Exec(insert) + db.Exec(sql_insert) //data := sql.QueryCol[string](db, query, 2) - data := sql.QueryData[greet](db, query) + data := sql.QueryData[greet](db, sql_query) t.AssertEqual(len(data), 2) t.AssertEqual(data[0].label, "Hello World") t.AssertEqual(data[1].label, "Good Afternoon") - data = sql.QueryDataP[*greet](db, query) + data = sql.QueryDataP[*greet](db, sql_query) t.AssertEqual(len(data), 2) t.AssertEqual(data[0].label, "Hello World") t.AssertEqual(data[1].label, "Good Afternoon") } +func MsgstoreTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) { + msg := message.SimpleMessage("data") + msgstore.Store(db, msg) +} + func resetSqlite(db *sql.Storage) { db.Exec(drop_table) db.Exec(sqlite_create_table) @@ -69,23 +77,24 @@ func resetPgsql(db *sql.Storage) { // SQL statements -var drop_table = `drop table test` +const ( + drop_table = `drop table test` -var insert = `insert into test (label) values + sql_insert = `insert into test (label) values ('Hello World'), ('Good Afternoon')` -var query = `select id, label from test ` + sql_query = `select id, label from test ` -//where id = $1` + //where id = $1` + // database-specific SQL statements -// database-specific SQL statements - -// note: ... integer ... primary key ... automatically fills row sequentially -var sqlite_create_table = `create table test ( + // note: ... integer ... primary key ... automatically fills row sequentially + sqlite_create_table = `create table test ( id integer not null primary key, label text)` -var pgsql_create_table = `create table test ( + pgsql_create_table = `create table test ( id serial not null primary key, label text)` +)