> this.  Most people who would need such a thing are probably using models
> with validates_unique, and probably don't have a problem with raising a
> regular exception if the race is lost.  That being said, it seems like a
> useful thing to me, so I'll look into implementing it.
>
Or...they roll their own...I usually try to report on exactly what goes wrong...

class DataWouldBeTruncated < DatabaseError
end

class InvalidDateError < DatabaseError
end

class Patient

  def raise_whats_too_long
    DB.schema(:patient).each do |col|
      if col[1][:db_type] == 'varchar' and @values[col[0]].size >
col[1][:max_chars]
        raise DataWouldBeTruncated, "#{col[0]} is too long with
[#{@values[col[0]]}]"
      end
    end
    raise DataWouldBeTruncated, "A field has an data that is too long
to save for record #{id}"
  end

  def parseble_date(value)
    begin
      !!Date.parse(value)
    rescue
      nil
    end
  end

  def raise_what_date_has_error
    DB.schema(:patient).each do |col|
      if col[1][:db_type] =~ /date/ and !parseable_date(@values[col[0]])
        raise InvalidDateError, "#{col[0]} has invalid date value:
[#{@values[col[0]]}]"
      end
    end
    raise InvalidDateError, "A date field has an invalid value for record #{id}"
  end

  def insert
    begin
      DB[:patient].insert(@values)
    rescue Exception => e
      if e.message =~ /data would be truncated/
        raise_whats_too_long
      elsif e.message =~ /conversion of a char data type to a datetime/
        raise_what_date_has_error
      else
        raise
      end
    end
  end
end

Probably much harder to generalize such an approach as above, but I
would definitely like to see more specific errors emitting from the
Sequel library.

+1

Michael
-- 
http://codeconnoisseur.org

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to