1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.sql;
  27 
  28 /**
  29  * An exception  thrown as a <code>DataTruncation</code> exception
  30  * (on writes) or reported as a
  31  * <code>DataTruncation</code> warning (on reads)
  32  *  when a data values is unexpectedly truncated for reasons other than its having
  33  *  exceeded <code>MaxFieldSize</code>.
  34  *
  35  * <P>The SQLstate for a <code>DataTruncation</code> during read is <code>01004</code>.
  36  * <P>The SQLstate for a <code>DataTruncation</code> during write is <code>22001</code>.
  37  */
  38 
  39 public class DataTruncation extends SQLWarning {
  40 
  41     /**
  42      * Creates a <code>DataTruncation</code> object
  43      * with the SQLState initialized
  44      * to 01004 when <code>read</code> is set to <code>true</code> and 22001
  45      * when <code>read</code> is set to <code>false</code>,
  46      * the reason set to "Data truncation", the
  47      * vendor code set to 0, and
  48      * the other fields set to the given values.
  49      * The <code>cause</code> is not initialized, and may subsequently be
  50      * initialized by a call to the
  51      * {@link Throwable#initCause(java.lang.Throwable)} method.
  52      *
  53      * @param index The index of the parameter or column value
  54      * @param parameter true if a parameter value was truncated
  55      * @param read true if a read was truncated
  56      * @param dataSize the original size of the data
  57      * @param transferSize the size after truncation
  58      */
  59     public DataTruncation(int index, boolean parameter,
  60                           boolean read, int dataSize,
  61                           int transferSize) {
  62         super("Data truncation", read == true?"01004":"22001");
  63         this.index = index;
  64         this.parameter = parameter;
  65         this.read = read;
  66         this.dataSize = dataSize;
  67         this.transferSize = transferSize;
  68 
  69     }
  70 
  71     /**
  72      * Creates a <code>DataTruncation</code> object
  73      * with the SQLState initialized
  74      * to 01004 when <code>read</code> is set to <code>true</code> and 22001
  75      * when <code>read</code> is set to <code>false</code>,
  76      * the reason set to "Data truncation", the
  77      * vendor code set to 0, and
  78      * the other fields set to the given values.
  79      *
  80      * @param index The index of the parameter or column value
  81      * @param parameter true if a parameter value was truncated
  82      * @param read true if a read was truncated
  83      * @param dataSize the original size of the data
  84      * @param transferSize the size after truncation
  85      * @param cause the underlying reason for this <code>DataTruncation</code>
  86      * (which is saved for later retrieval by the <code>getCause()</code> method);
  87      * may be null indicating the cause is non-existent or unknown.
  88      *
  89      * @since 1.6
  90      */
  91     public DataTruncation(int index, boolean parameter,
  92                           boolean read, int dataSize,
  93                           int transferSize, Throwable cause) {
  94         super("Data truncation", read == true?"01004":"22001",cause);
  95         this.index = index;
  96         this.parameter = parameter;
  97         this.read = read;
  98         this.dataSize = dataSize;
  99         this.transferSize = transferSize;
 100     }
 101 
 102     /**
 103      * Retrieves the index of the column or parameter that was truncated.
 104      *
 105      * <P>This may be -1 if the column or parameter index is unknown, in
 106      * which case the <code>parameter</code> and <code>read</code> fields should be ignored.
 107      *
 108      * @return the index of the truncated parameter or column value
 109      */
 110     public int getIndex() {
 111         return index;
 112     }
 113 
 114     /**
 115      * Indicates whether the value truncated was a parameter value or
 116          * a column value.
 117      *
 118      * @return <code>true</code> if the value truncated was a parameter;
 119          *         <code>false</code> if it was a column value
 120      */
 121     public boolean getParameter() {
 122         return parameter;
 123     }
 124 
 125     /**
 126      * Indicates whether or not the value was truncated on a read.
 127      *
 128      * @return <code>true</code> if the value was truncated when read from
 129          *         the database; <code>false</code> if the data was truncated on a write
 130      */
 131     public boolean getRead() {
 132         return read;
 133     }
 134 
 135     /**
 136      * Gets the number of bytes of data that should have been transferred.
 137      * This number may be approximate if data conversions were being
 138      * performed.  The value may be <code>-1</code> if the size is unknown.
 139      *
 140      * @return the number of bytes of data that should have been transferred
 141      */
 142     public int getDataSize() {
 143         return dataSize;
 144     }
 145 
 146     /**
 147      * Gets the number of bytes of data actually transferred.
 148      * The value may be <code>-1</code> if the size is unknown.
 149      *
 150      * @return the number of bytes of data actually transferred
 151      */
 152     public int getTransferSize() {
 153         return transferSize;
 154     }
 155 
 156         /**
 157         * @serial
 158         */
 159     private int index;
 160 
 161         /**
 162         * @serial
 163         */
 164     private boolean parameter;
 165 
 166         /**
 167         * @serial
 168         */
 169     private boolean read;
 170 
 171         /**
 172         * @serial
 173         */
 174     private int dataSize;
 175 
 176         /**
 177         * @serial
 178         */
 179     private int transferSize;
 180 
 181     /**
 182      * @serial
 183      */
 184     private static final long serialVersionUID = 6464298989504059473L;
 185 
 186 }