Exemple #1
0
// FIXME: Lots of duplicated code between insert and update, no bueno.
func Update(values, columns []interface{}, model *Model) error {
	accessor := accessorFor(model.table)
	manager := managers.Modification(accessor.Relation())

	for _, column := range columns {
		column, err := CARTOGRAPHER.ColumnForField(model.definition, column.(string))

		if nil != err {
			return err
		}

		manager.Set(column)
	}

	manager.To(values...)

	if 0 < len(model.table.PrimaryKey) {
		column, err := CARTOGRAPHER.ColumnForField(model.definition, model.table.PrimaryKey)

		if nil != err {
			return err
		}

		field, err := CARTOGRAPHER.FieldForColumn(model.definition, model.table.PrimaryKey)

		if nil != err {
			return err
		}

		manager.Where(accessor(column).Eq(model.values[field]))

	} else {
		return errors.New("Unable to update record missing value for primary key field.")
	}

	sql, err := manager.ToSql()

	if nil != err {
		return err
	}

	// FIXME: This should be a transaction.
	stmt, err := connection.session.Prepare(sql)

	if nil != err {
		return err
	}

	_, err = stmt.Exec()

	return err
}
func TestUpdateManager(t *testing.T) {
	relation := nodes.Relation("table")
	mgr := managers.Modification(relation)

	// The following struct members should exist.
	_ = mgr.Tree
	_ = mgr.Engine

	// The following receiver methods should exist.
	_ = mgr.Set(1)
	_ = mgr.To(1)
	_ = mgr.Where(1)
	_ = mgr.Limit(1)
	_ = mgr.Engine(1)
	_, _ = mgr.ToSql()
}