github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/pubsub/sql/schema_adapter.go (about) 1 package sql 2 3 import ( 4 "database/sql" 5 6 "github.com/pkg/errors" 7 8 "github.com/wfusion/gofusion/common/infra/watermill/message" 9 "github.com/wfusion/gofusion/common/utils/serialize/json" 10 ) 11 12 // SchemaAdapter produces the SQL queries and arguments appropriately for a specific schema and dialect 13 // It also transforms sql.Rows into Watermill messages. 14 type SchemaAdapter interface { 15 // InsertQuery returns the SQL query and arguments that will insert the Watermill message into the SQL storage. 16 InsertQuery(topic string, msgs message.Messages) (string, []any, error) 17 18 // SelectQuery returns the SQL query and arguments 19 // that returns the next unread message for a given consumer group. 20 SelectQuery(topic string, consumerGroup string, offsetsAdapter OffsetsAdapter) (string, []any) 21 22 // DeleteQuery returns the SQL query and arguments 23 DeleteQuery(topic string, offset int64) (string, []any) 24 25 // UnmarshalMessage transforms the Row obtained SelectQuery a Watermill message. 26 // It also returns the offset of the last read message, for the purpose of acking. 27 UnmarshalMessage(row Scanner) (Row, error) 28 29 // SchemaInitializingQueries returns SQL queries which will make sure (CREATE IF NOT EXISTS) 30 // that the appropriate tables exist to write messages to the given topic. 31 SchemaInitializingQueries(topic string) []string 32 33 // SubscribeIsolationLevel returns the isolation level that will be used when subscribing. 34 SubscribeIsolationLevel() sql.IsolationLevel 35 } 36 37 // Deprecated: Use DefaultMySQLSchema instead. 38 type DefaultSchema = DefaultMySQLSchema 39 40 type Row struct { 41 Offset int64 42 UUID []byte 43 Payload []byte 44 Metadata []byte 45 46 Msg *message.Message 47 48 ExtraData map[string]any 49 } 50 51 func defaultInsertArgs(msgs message.Messages) ([]any, error) { 52 var args []any 53 for _, msg := range msgs { 54 metadata, err := json.Marshal(msg.Metadata) 55 if err != nil { 56 return nil, errors.Wrapf(err, "could not marshal metadata into JSON for message %s", msg.UUID) 57 } 58 59 args = append(args, msg.UUID, []byte(msg.Payload), metadata) 60 } 61 62 return args, nil 63 }