github.com/mantzas/incata@v0.3.0/reader/sql_reader.go (about)

     1  package reader
     2  
     3  import (
     4  	"github.com/mantzas/incata/marshal"
     5  	"github.com/mantzas/incata/model"
     6  	"github.com/mantzas/incata/storage"
     7  	"github.com/satori/go.uuid"
     8  )
     9  
    10  // SQLReader for reading events
    11  type SQLReader struct {
    12  	Storage    *storage.Storage
    13  	Serializer marshal.Serializer
    14  }
    15  
    16  // NewSQLReader creates a new sql reader
    17  func NewSQLReader(storage *storage.Storage, ser marshal.Serializer) *SQLReader {
    18  
    19  	return &SQLReader{Storage: storage, Serializer: ser}
    20  }
    21  
    22  // Read from db with source id
    23  func (r *SQLReader) Read(sourceID uuid.UUID) ([]model.Event, error) {
    24  
    25  	rows, err := r.Storage.Query(r.Storage.SelectBySourceIDStatement, sourceID.String())
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	defer rows.Close()
    30  
    31  	var events = make([]model.Event, 0)
    32  
    33  	for rows.Next() {
    34  		var event = new(model.Event)
    35  
    36  		if err := rows.Scan(&event.ID, &event.SourceID, &event.Created, &event.EventType, &event.Version, &event.Payload); err != nil {
    37  			return nil, err
    38  		}
    39  
    40  		events = append(events, *event)
    41  	}
    42  
    43  	if err := rows.Err(); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return events, nil
    48  }