Path: blob/master/src/java.sql/share/classes/java/sql/DataTruncation.java
41153 views
/*1* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.sql;2627/**28* An exception thrown as a {@code DataTruncation} exception29* (on writes) or reported as a30* {@code DataTruncation} warning (on reads)31* when a data values is unexpectedly truncated for reasons other than its having32* exceeded {@code MaxFieldSize}.33*34* <P>The SQLstate for a {@code DataTruncation} during read is {@code 01004}.35* <P>The SQLstate for a {@code DataTruncation} during write is {@code 22001}.36*37* @since 1.138*/3940public class DataTruncation extends SQLWarning {4142/**43* Creates a {@code DataTruncation} object44* with the SQLState initialized45* to 01004 when {@code read} is set to {@code true} and 2200146* when {@code read} is set to {@code false},47* the reason set to "Data truncation", the48* vendor code set to 0, and49* the other fields set to the given values.50* The {@code cause} is not initialized, and may subsequently be51* initialized by a call to the52* {@link Throwable#initCause(java.lang.Throwable)} method.53*54* @param index The index of the parameter or column value55* @param parameter true if a parameter value was truncated56* @param read true if a read was truncated57* @param dataSize the original size of the data58* @param transferSize the size after truncation59*/60public DataTruncation(int index, boolean parameter,61boolean read, int dataSize,62int transferSize) {63super("Data truncation", read == true?"01004":"22001");64this.index = index;65this.parameter = parameter;66this.read = read;67this.dataSize = dataSize;68this.transferSize = transferSize;6970}7172/**73* Creates a {@code DataTruncation} object74* with the SQLState initialized75* to 01004 when {@code read} is set to {@code true} and 2200176* when {@code read} is set to {@code false},77* the reason set to "Data truncation", the78* vendor code set to 0, and79* the other fields set to the given values.80*81* @param index The index of the parameter or column value82* @param parameter true if a parameter value was truncated83* @param read true if a read was truncated84* @param dataSize the original size of the data85* @param transferSize the size after truncation86* @param cause the underlying reason for this {@code DataTruncation}87* (which is saved for later retrieval by the {@code getCause()} method);88* may be null indicating the cause is non-existent or unknown.89*90* @since 1.691*/92public DataTruncation(int index, boolean parameter,93boolean read, int dataSize,94int transferSize, Throwable cause) {95super("Data truncation", read == true?"01004":"22001",cause);96this.index = index;97this.parameter = parameter;98this.read = read;99this.dataSize = dataSize;100this.transferSize = transferSize;101}102103/**104* Retrieves the index of the column or parameter that was truncated.105*106* <P>This may be -1 if the column or parameter index is unknown, in107* which case the {@code parameter} and {@code read} fields should be ignored.108*109* @return the index of the truncated parameter or column value110*/111public int getIndex() {112return index;113}114115/**116* Indicates whether the value truncated was a parameter value or117* a column value.118*119* @return {@code true} if the value truncated was a parameter;120* {@code false} if it was a column value121*/122public boolean getParameter() {123return parameter;124}125126/**127* Indicates whether or not the value was truncated on a read.128*129* @return {@code true} if the value was truncated when read from130* the database; {@code false} if the data was truncated on a write131*/132public boolean getRead() {133return read;134}135136/**137* Gets the number of bytes of data that should have been transferred.138* This number may be approximate if data conversions were being139* performed. The value may be {@code -1} if the size is unknown.140*141* @return the number of bytes of data that should have been transferred142*/143public int getDataSize() {144return dataSize;145}146147/**148* Gets the number of bytes of data actually transferred.149* The value may be {@code -1} if the size is unknown.150*151* @return the number of bytes of data actually transferred152*/153public int getTransferSize() {154return transferSize;155}156157/**158* @serial159*/160private int index;161162/**163* @serial164*/165private boolean parameter;166167/**168* @serial169*/170private boolean read;171172/**173* @serial174*/175private int dataSize;176177/**178* @serial179*/180private int transferSize;181182/**183* @serial184*/185private static final long serialVersionUID = 6464298989504059473L;186187}188189190