how can I recover from truncate write?
how can I recover from truncate write?
От:
Steve Pothier <pothiers@nemesis.tucson.saic.com>
Дата:
If I attempt to write a field to the DB that is too long, I get an SQLException (not SQLWarning, not DataTrunctation). The INSERT apparently fails. I'ld like to issue a warning and automatically truncate to fit. Given that I only get a general SQLException, PROGRAMATICALLY its difficult to know that I got a data truncation (as opposed to other exception) and if truncation, its difficult to know how long the field is allowed to be (or even which field failed). SQLException.getMessage() yields: "value too long for type character varying(20)" so I COULD parse that message but that seems horrid. Any better ideas? The doc http://lab.applinet.nl/postgresql-jdbc/ discusses two conflicting readings on what should happen with regard to data truncation. I just tried some tests and neither approaches work (ie. the truncation is not reported by Statement.getWarnings(), and is not thrown as an SQLWarning or DataTruncation exceptions). I think this means the driver is guaranteed to be wrong (and hard to use in the face of truncations!) The doc already warns that DataTruncation is never thrown. It does not point out that Statement.getWarnings also does not work for data truncation errors. -Steve-
Re: how can I recover from truncate write?
От:
Barry Lind <blind@xythos.com>
Дата:
Steve, There really is no good way to do this. The database is generating the error. And actually the SQL standard says this is supposed to be an error condition. So I am not sure anything should be changed in either the driver or the database. You really need to test the data before sending it to the driver. thanks, --Barry Steve Pothier wrote: > If I attempt to write a field to the DB that is too long, I get an > SQLException (not SQLWarning, not DataTrunctation). The INSERT > apparently fails. I'ld like to issue a warning and automatically > truncate to fit. Given that I only get a general SQLException, > PROGRAMATICALLY its difficult to know that I got a data truncation (as > opposed to other exception) and if truncation, its difficult to know > how long the field is allowed to be (or even which field failed). > > SQLException.getMessage() yields: "value too long for type character > varying(20)" so I COULD parse that message but that seems horrid. > > Any better ideas? > > > > The doc http://lab.applinet.nl/postgresql-jdbc/ discusses two > conflicting readings on what should happen with regard to data > truncation. I just tried some tests and neither approaches work > (ie. the truncation is not reported by Statement.getWarnings(), and > is not thrown as an SQLWarning or DataTruncation exceptions). I think > this means the driver is guaranteed to be wrong (and hard to use in > the face of truncations!) > > The doc already warns that DataTruncation is never thrown. It does not > point out that Statement.getWarnings also does not work for data > truncation errors. > > > -Steve- > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster >