github.com/mantzas/incata@v0.3.0/writer/sql_writer.go (about)

     1  package writer
     2  
     3  import (
     4  	"github.com/mantzas/incata/marshal"
     5  	"github.com/mantzas/incata/model"
     6  	"github.com/mantzas/incata/storage"
     7  )
     8  
     9  // SQLWriter writer for writing events
    10  type SQLWriter struct {
    11  	Storage    *storage.Storage
    12  	Serializer marshal.Serializer
    13  }
    14  
    15  // NewSQLWriter creates a new sql writer
    16  func NewSQLWriter(storage *storage.Storage, ser marshal.Serializer) *SQLWriter {
    17  
    18  	return &SQLWriter{Storage: storage, Serializer: ser}
    19  }
    20  
    21  // Write writes a value to db table
    22  func (w *SQLWriter) Write(event model.Event) error {
    23  
    24  	payloadText, err := w.Serializer.Serialize(event.Payload)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	_, err = w.Storage.Exec(w.Storage.AppendStatement, event.SourceID.String(), event.Created, event.EventType, event.Version, payloadText)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	return nil
    35  }