Exemplo n.º 1
1
func (w *DatabaseWorld) SaveThing(thing *Thing) (ok bool) {
	tabletext, err := json.Marshal(thing.Table)
	if err != nil {
		log.Println("Error serializing table data for thing", thing.Id, ":", err.Error())
		return false
	}

	var parent sql.NullInt64
	if thing.Parent != 0 {
		parent.Int64 = int64(thing.Parent)
		parent.Valid = true
	}
	var owner sql.NullInt64
	if thing.Owner != 0 && thing.Type.HasOwner() {
		owner.Int64 = int64(thing.Owner)
		owner.Valid = true
	}
	var program sql.NullString
	if thing.Program != nil {
		program.String = thing.Program.Text
		program.Valid = true
	}

	// TODO: save the allow list
	_, err = w.db.Exec("UPDATE thing SET name = $1, parent = $2, owner = $3, adminlist = $4, denylist = $5, tabledata = $6, program = $7 WHERE id = $8",
		thing.Name, parent, owner, thing.AdminList, thing.DenyList,
		types.JsonText(tabletext), program, thing.Id)
	if err != nil {
		log.Println("Error saving a thing", thing.Id, ":", err.Error())
		return false
	}
	return true
}
Exemplo n.º 2
0
func (esr *EventStreamRepository) toPgEvent(event cqrs.VersionedEvent) (*PgVersionedEvent, error) {
	b, err := json.Marshal(event.Event)
	if err != nil {
		return nil, pgProviderError{event.Event, ErrPgMarshalingEvent, err}
	}

	return &PgVersionedEvent{
		ID:            event.ID,
		CorrelationID: event.CorrelationID,
		SourceID:      event.SourceID,
		Version:       event.Version,
		EventType:     event.EventType,
		Created:       event.Created,
		Event:         types.JsonText(b),
	}, nil
}
Exemplo n.º 3
0
// saveKV saves a kv pair within the provided transaction
func (db *Driver) saveKV(tx *sqlx.Tx, key string, in interface{}) error {
	vj, err := json.Marshal(in)
	if err != nil {
		return err
	}

	exists, err := db.hasKV(key)
	if err != nil {
		return err
	}

	sql := Queries.KV.Insert
	if exists {
		sql = Queries.KV.Update
	}
	_, err = tx.Exec(sql, key, types.JsonText(vj))

	return err
}