Exemple #1
0
// Create is for creating new record. Mutates passed ID
func (d *Driver) Create(tx interface{}, tablename string, fields []field.Field, ID *field.Field) error {
	d.maxID++
	ID.Value = d.maxID
	changeID(fields, *ID)
	d.insertTo(tablename, fields)
	return nil
}
Exemple #2
0
// Create is for creating new record. Mutates passed ID
func (d *Driver) Create(tx interface{}, tablename string, fields []field.Field, ID *field.Field) error {
	if tx != nil {
		return tx.(*Driver).Create(nil, tablename, fields, ID)
	}

	d.maxID++
	ID.Value = d.maxID
	changeID(fields, *ID)
	d.insertTo(tablename, fields)
	d.createdIDs[d.maxID] = struct{}{}
	return nil
}
Exemple #3
0
// Create is for creating new record and updating its ID
func (d *Driver) Create(tx interface{}, tablename string, fields []field.Field, ID *field.Field) error {
	names := fieldNamesWithoutID(fields, *ID)
	values := fieldValuesWithoutID(fields, *ID)

	query := "INSERT INTO %s (%s) VALUES (%s) RETURNING %s"
	query = fmt.Sprintf(query, tablename, namesRepr(names), valuesRepr(values, 0), ID.DriverName)

	idValue := reflect.New(ID.Ty)
	if err := d.queryRow(tx, query, values...).Scan(idValue.Interface()); err != nil {
		return fmt.Errorf("Unable to insert into %s - %s", tablename, err)
	}

	ID.Value = idValue.Elem().Interface()
	return nil
}
Exemple #4
0
func populateFieldValue(record interface{}, f *field.Field) error {
	v := reflect.ValueOf(record)
	for valueHasElem(v) {
		v = v.Elem()
	}

	ty := v.Type()

	if _, ok := ty.FieldByName(f.Name); !ok {
		return fmt.Errorf("Field %s not found on record %+v", f.Name, record)
	}

	f.Value = v.FieldByName(f.Name).Interface()
	return nil
}