1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package java.sql;
19
20import java.io.Serializable;
21
22/**
23 * An exception which is thrown when a JDBC driver unexpectedly truncates a data
24 * value either when reading (resulting in warning), or when writing data
25 * (resulting in an error). The {@code SQLState} error code for truncated data
26 * is {@code 01004}.
27 */
28public class DataTruncation extends SQLWarning implements Serializable {
29
30    private static final long serialVersionUID = 6464298989504059473L;
31
32    private int index = 0;
33
34    private boolean parameter = false;
35
36    private boolean read = false;
37
38    private int dataSize = 0;
39
40    private int transferSize = 0;
41
42    private static final String THE_REASON = "Data truncation";
43
44    private static final String THE_SQLSTATE_READ = "01004";
45
46    private static final String THE_SQLSTATE_WRITE = "22001";
47
48    private static final int THE_ERROR_CODE = 0;
49
50    /**
51     * Creates the {@code DataTruncation} object. The reason is set to {@code
52     * "Data truncation"}, the error code is set to the {@code
53     * SQLException} default value, and the other fields are set to the values
54     * supplied as arguments.
55     *
56     * @param index
57     *            the Index value of the column value or parameter that was
58     *            truncated.
59     * @param parameter
60     *            {@code true} if it was a parameter value that was truncated,
61     *            {@code false} otherwise.
62     * @param read
63     *            {@code true} if the truncation occurred on a read operation,
64     *            {@code false} otherwise.
65     * @param dataSize
66     *            the original size of the truncated data.
67     * @param transferSize
68     *            the size of the data after truncation.
69     */
70    public DataTruncation(int index, boolean parameter, boolean read,
71            int dataSize, int transferSize) {
72        super(THE_REASON, THE_SQLSTATE_READ, THE_ERROR_CODE);
73        this.index = index;
74        this.parameter = parameter;
75        this.read = read;
76        this.dataSize = dataSize;
77        this.transferSize = transferSize;
78    }
79
80    /**
81     * Creates a DataTruncation. The Reason is set to "Data truncation", the
82     * error code is set to the SQLException default value and other fields are
83     * set to the values supplied on this method.
84     *
85     * @param index
86     *            the Index value of the column value or parameter that was
87     *            truncated
88     * @param parameter
89     *            true if it was a Parameter value that was truncated, false
90     *            otherwise
91     * @param read
92     *            true if the truncation occurred on a read operation, false
93     *            otherwise
94     * @param dataSize
95     *            the original size of the truncated data
96     * @param transferSize
97     *            the size of the data after truncation
98     * @param cause
99     *            the root reason for this DataTruncation
100     *
101     * @since 1.6
102     */
103    public DataTruncation(int index, boolean parameter, boolean read,
104            int dataSize, int transferSize, Throwable cause) {
105        super(THE_REASON, read ? THE_SQLSTATE_READ : THE_SQLSTATE_WRITE,
106                THE_ERROR_CODE, cause);
107        this.index = index;
108        this.parameter = parameter;
109        this.read = read;
110        this.dataSize = dataSize;
111        this.transferSize = transferSize;
112    }
113
114    /**
115     * Gets the number of bytes of data that should have been read/written.
116     *
117     * @return the number of bytes that should have been read or written. The
118     *         value is set to {@code -1} if the size is unknown.
119     */
120    public int getDataSize() {
121        return dataSize;
122    }
123
124    /**
125     * Gets the index of the column or of the parameter that was truncated.
126     *
127     * @return the index number of the column or of the parameter.
128     */
129    public int getIndex() {
130        return index;
131    }
132
133    /**
134     * Gets whether the value truncated was a parameter value or a column value.
135     *
136     * @return {@code true} if the value truncated was a parameter value,
137     *         {@code false} if it was a column value.
138     */
139    public boolean getParameter() {
140        return parameter;
141    }
142
143    /**
144     * Gets whether the value was truncated on a read operation or a write
145     * operation
146     *
147     * @return {@code true} if the value was truncated on a read operation,
148     *         {@code false} otherwise.
149     */
150    public boolean getRead() {
151        return read;
152    }
153
154    /**
155     * Gets the number of bytes of data that was actually read or written.
156     *
157     * @return the number of bytes actually read/written. The value may be set
158     *         to {@code -1} if the size is unknown.
159     */
160    public int getTransferSize() {
161        return transferSize;
162    }
163}
164