1 /*
2  * Copyright (c) 1996, 2005, 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  *  execeeded <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      * <p>
53      *
54      * @param index The index of the parameter or column value
55      * @param parameter true if a parameter value was truncated
56      * @param read true if a read was truncated
57      * @param dataSize the original size of the data
58      * @param transferSize the size after truncation
59      */
DataTruncation(int index, boolean parameter, boolean read, int dataSize, int transferSize)60     public DataTruncation(int index, boolean parameter,
61                           boolean read, int dataSize,
62                           int transferSize) {
63         super("Data truncation", read == true?"01004":"22001");
64         this.index = index;
65         this.parameter = parameter;
66         this.read = read;
67         this.dataSize = dataSize;
68         this.transferSize = transferSize;
69 
70     }
71 
72     /**
73      * Creates a <code>DataTruncation</code> object
74      * with the SQLState initialized
75      * to 01004 when <code>read</code> is set to <code>true</code> and 22001
76      * when <code>read</code> is set to <code>false</code>,
77      * the reason set to "Data truncation", the
78      * vendor code set to 0, and
79      * the other fields set to the given values.
80      * <p>
81      *
82      * @param index The index of the parameter or column value
83      * @param parameter true if a parameter value was truncated
84      * @param read true if a read was truncated
85      * @param dataSize the original size of the data
86      * @param transferSize the size after truncation
87      * @param cause the underlying reason for this <code>DataTruncation</code>
88      * (which is saved for later retrieval by the <code>getCause()</code> method);
89      * may be null indicating the cause is non-existent or unknown.
90      *
91      * @since 1.6
92      */
DataTruncation(int index, boolean parameter, boolean read, int dataSize, int transferSize, Throwable cause)93     public DataTruncation(int index, boolean parameter,
94                           boolean read, int dataSize,
95                           int transferSize, Throwable cause) {
96         super("Data truncation", read == true?"01004":"22001",cause);
97         this.index = index;
98         this.parameter = parameter;
99         this.read = read;
100         this.dataSize = dataSize;
101         this.transferSize = transferSize;
102     }
103 
104     /**
105      * Retrieves the index of the column or parameter that was truncated.
106      *
107      * <P>This may be -1 if the column or parameter index is unknown, in
108      * which case the <code>parameter</code> and <code>read</code> fields should be ignored.
109      *
110      * @return the index of the truncated paramter or column value
111      */
getIndex()112     public int getIndex() {
113         return index;
114     }
115 
116     /**
117      * Indicates whether the value truncated was a parameter value or
118          * a column value.
119      *
120      * @return <code>true</code> if the value truncated was a parameter;
121          *         <code>false</code> if it was a column value
122      */
getParameter()123     public boolean getParameter() {
124         return parameter;
125     }
126 
127     /**
128      * Indicates whether or not the value was truncated on a read.
129      *
130      * @return <code>true</code> if the value was truncated when read from
131          *         the database; <code>false</code> if the data was truncated on a write
132      */
getRead()133     public boolean getRead() {
134         return read;
135     }
136 
137     /**
138      * Gets the number of bytes of data that should have been transferred.
139      * This number may be approximate if data conversions were being
140      * performed.  The value may be <code>-1</code> if the size is unknown.
141      *
142      * @return the number of bytes of data that should have been transferred
143      */
getDataSize()144     public int getDataSize() {
145         return dataSize;
146     }
147 
148     /**
149      * Gets the number of bytes of data actually transferred.
150      * The value may be <code>-1</code> if the size is unknown.
151      *
152      * @return the number of bytes of data actually transferred
153      */
getTransferSize()154     public int getTransferSize() {
155         return transferSize;
156     }
157 
158         /**
159         * @serial
160         */
161     private int index;
162 
163         /**
164         * @serial
165         */
166     private boolean parameter;
167 
168         /**
169         * @serial
170         */
171     private boolean read;
172 
173         /**
174         * @serial
175         */
176     private int dataSize;
177 
178         /**
179         * @serial
180         */
181     private int transferSize;
182 
183     /**
184      * @serial
185      */
186     private static final long serialVersionUID = 6464298989504059473L;
187 
188 }
189