]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 | ||
18 | package org.apache.arrow.adapter.jdbc; | |
19 | ||
20 | import java.sql.ResultSetMetaData; | |
21 | import java.sql.SQLException; | |
22 | import java.sql.Types; | |
23 | ||
24 | import org.apache.arrow.util.Preconditions; | |
25 | ||
26 | /** | |
27 | * This class represents the information about a JDBC ResultSet Field that is | |
28 | * needed to construct an {@link org.apache.arrow.vector.types.pojo.ArrowType}. | |
29 | * Currently, this is: | |
30 | * <ul> | |
31 | * <li>The JDBC {@link java.sql.Types} type.</li> | |
32 | * <li>The field's precision (used for {@link java.sql.Types#DECIMAL} and {@link java.sql.Types#NUMERIC} types)</li> | |
33 | * <li>The field's scale (used for {@link java.sql.Types#DECIMAL} and {@link java.sql.Types#NUMERIC} types)</li> | |
34 | * </ul> | |
35 | */ | |
36 | public class JdbcFieldInfo { | |
37 | private final int jdbcType; | |
38 | private final int precision; | |
39 | private final int scale; | |
40 | ||
41 | /** | |
42 | * Builds a <code>JdbcFieldInfo</code> using only the {@link java.sql.Types} type. Do not use this constructor | |
43 | * if the field type is {@link java.sql.Types#DECIMAL} or {@link java.sql.Types#NUMERIC}; the precision and | |
44 | * scale will be set to <code>0</code>. | |
45 | * | |
46 | * @param jdbcType The {@link java.sql.Types} type. | |
47 | * @throws IllegalArgumentException if jdbcType is {@link java.sql.Types#DECIMAL} or {@link java.sql.Types#NUMERIC}. | |
48 | */ | |
49 | public JdbcFieldInfo(int jdbcType) { | |
50 | Preconditions.checkArgument( | |
51 | (jdbcType != Types.DECIMAL && jdbcType != Types.NUMERIC), | |
52 | "DECIMAL and NUMERIC types require a precision and scale; please use another constructor."); | |
53 | ||
54 | this.jdbcType = jdbcType; | |
55 | this.precision = 0; | |
56 | this.scale = 0; | |
57 | } | |
58 | ||
59 | /** | |
60 | * Builds a <code>JdbcFieldInfo</code> from the {@link java.sql.Types} type, precision, and scale. | |
61 | * Use this constructor for {@link java.sql.Types#DECIMAL} and {@link java.sql.Types#NUMERIC} types. | |
62 | * | |
63 | * @param jdbcType The {@link java.sql.Types} type. | |
64 | * @param precision The field's numeric precision. | |
65 | * @param scale The field's numeric scale. | |
66 | */ | |
67 | public JdbcFieldInfo(int jdbcType, int precision, int scale) { | |
68 | this.jdbcType = jdbcType; | |
69 | this.precision = precision; | |
70 | this.scale = scale; | |
71 | } | |
72 | ||
73 | /** | |
74 | * Builds a <code>JdbcFieldInfo</code> from the corresponding {@link java.sql.ResultSetMetaData} column. | |
75 | * | |
76 | * @param rsmd The {@link java.sql.ResultSetMetaData} to get the field information from. | |
77 | * @param column The column to get the field information for (on a 1-based index). | |
78 | * @throws SQLException If the column information cannot be retrieved. | |
79 | * @throws NullPointerException if <code>rsmd</code> is <code>null</code>. | |
80 | * @throws IllegalArgumentException if <code>column</code> is out of bounds. | |
81 | */ | |
82 | public JdbcFieldInfo(ResultSetMetaData rsmd, int column) throws SQLException { | |
83 | Preconditions.checkNotNull(rsmd, "ResultSetMetaData cannot be null."); | |
84 | Preconditions.checkArgument(column > 0, "ResultSetMetaData columns have indices starting at 1."); | |
85 | Preconditions.checkArgument( | |
86 | column <= rsmd.getColumnCount(), | |
87 | "The index must be within the number of columns (1 to %s, inclusive)", rsmd.getColumnCount()); | |
88 | ||
89 | this.jdbcType = rsmd.getColumnType(column); | |
90 | this.precision = rsmd.getPrecision(column); | |
91 | this.scale = rsmd.getScale(column); | |
92 | } | |
93 | ||
94 | /** | |
95 | * The {@link java.sql.Types} type. | |
96 | */ | |
97 | public int getJdbcType() { | |
98 | return jdbcType; | |
99 | } | |
100 | ||
101 | /** | |
102 | * The numeric precision, for {@link java.sql.Types#NUMERIC} and {@link java.sql.Types#DECIMAL} types. | |
103 | */ | |
104 | public int getPrecision() { | |
105 | return precision; | |
106 | } | |
107 | ||
108 | /** | |
109 | * The numeric scale, for {@link java.sql.Types#NUMERIC} and {@link java.sql.Types#DECIMAL} types. | |
110 | */ | |
111 | public int getScale() { | |
112 | return scale; | |
113 | } | |
114 | } |