public static function updateBinding(
        $db,
        $table,
        $id_field,
        $id,
        $value_field,
        $values,
        $bound_table,
        $bound_field
    ) {
        $id_field = new SwatDBField($id_field, 'integer');
        $value_field = new SwatDBField($value_field, 'integer');
        $bound_field = new SwatDBField($bound_field, 'integer');

        $values = self::initArray($values);

        $delete_sql = 'delete from %s where %s = %s';

        $delete_sql = sprintf(
            $delete_sql,
            $table,
            $id_field->name,
            $db->quote($id, $id_field->type)
        );

        if (count($values)) {
            foreach ($values as &$value) {
                $value = $db->quote($value, $value_field->type);
            }

            $value_list = implode(',', $values);

            $insert_sql =
                'insert into %s (%s, %s) select %s, %s from %s ' .
                'where %s not in (select %s from %s where %s = %s) and %s in (%s)';

            $insert_sql = sprintf(
                $insert_sql,
                $table,
                $id_field->name,
                $value_field->name,
                $db->quote($id, $id_field->type),
                $bound_field->name,
                $bound_table,
                $bound_field->name,
                $value_field->name,
                $table,
                $id_field->name,
                $db->quote($id, $id_field->type),
                $bound_field->name,
                $value_list
            );

            $delete_sql .= sprintf(
                ' and %s not in (%s)',
                $value_field->name,
                $value_list
            );
        }

        $transaction = new SwatDBTransaction($db);
        try {
            if (count($values)) {
                self::exec($db, $insert_sql);
            }

            self::exec($db, $delete_sql);
        } catch (Exception $e) {
            $transaction->rollback();
            throw $e;
        }
        $transaction->commit();
    }