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

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

		if nil != err {
			return err
		}

		manager.Into(column)
	}

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

		if nil != err {
			return err
		}

		manager.Returning(column)
	}

	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
	}

	rows, err := stmt.Query()

	if nil != err {
		return err
	}

	err = CARTOGRAPHER.Sync(rows, model.definition)

	if nil != err {
		return err
	}

	model.isNew = false
	model.values, err = CARTOGRAPHER.FieldValueMapFor(model.definition)

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

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

	// The following receiver methods should exist.
	_ = mgr.Insert(1)
	_ = mgr.Into(1)
	_ = mgr.Returning(1)
	_ = mgr.Engine(1)
	_, _ = mgr.ToSql()
}