예제 #1
0
func Update(executor Executor, val interface{}) *Result {
	pkName := sqlutils.GetPrimaryKeyColumnName(val)
	if pkName == nil {
		panic("primary key column is not defined.")
	}

	sql, values := sqlutils.BuildUpdateClause(val)

	var id = sqlutils.GetPrimaryKeyValue(val)
	values = append(values, id)

	// sql += fmt.Sprintf(" WHERE %s = $%d", *pkName, len(values))
	sql += " WHERE " + *pkName + " = $" + strconv.Itoa(len(values))

	stmt, err := executor.Prepare(sql)
	if err != nil {
		return NewErrorResult(err, sql)
	}

	defer stmt.Close()

	res, err := stmt.Exec(values...)
	if err != nil {
		return NewErrorResult(err, sql)
	}

	result := NewResult(sql)
	result.Result = res
	return result
}
예제 #2
0
// Delete from DB connection object or a transaction object (pointer)
func Delete(executor Executor, val PtrRecord) *Result {
	pkName := sqlutils.GetPrimaryKeyColumnName(val)

	if pkName == nil {
		return NewErrorResult(errors.New("PrimaryKey column is not defined."), "")
	}

	sqlStr := "DELETE FROM " + sqlutils.GetTableName(val) + " WHERE " + *pkName + " = $1"

	var id = sqlutils.GetPrimaryKeyValue(val)
	var err error
	var res sql.Result

	res, err = executor.Exec(sqlStr, id)
	if err != nil {
		return NewErrorResult(err, sqlStr)
	}

	var r = NewResult(sqlStr)
	r.Result = res
	r.Id = id
	return r
}