]>
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.consumer; | |
19 | ||
20 | import java.nio.charset.StandardCharsets; | |
21 | import java.sql.ResultSet; | |
22 | import java.sql.SQLException; | |
23 | ||
24 | import org.apache.arrow.vector.VarCharVector; | |
25 | ||
26 | /** | |
27 | * Consumer which consume varchar type values from {@link ResultSet}. | |
28 | * Write the data to {@link org.apache.arrow.vector.VarCharVector}. | |
29 | */ | |
30 | public abstract class VarCharConsumer { | |
31 | ||
32 | /** | |
33 | * Creates a consumer for {@link VarCharVector}. | |
34 | */ | |
35 | public static JdbcConsumer<VarCharVector> createConsumer(VarCharVector vector, int index, boolean nullable) { | |
36 | if (nullable) { | |
37 | return new NullableVarCharConsumer(vector, index); | |
38 | } else { | |
39 | return new NonNullableVarCharConsumer(vector, index); | |
40 | } | |
41 | } | |
42 | ||
43 | /** | |
44 | * Nullable consumer for var char. | |
45 | */ | |
46 | static class NullableVarCharConsumer extends BaseConsumer<VarCharVector> { | |
47 | ||
48 | /** | |
49 | * Instantiate a VarCharConsumer. | |
50 | */ | |
51 | public NullableVarCharConsumer(VarCharVector vector, int index) { | |
52 | super(vector, index); | |
53 | } | |
54 | ||
55 | @Override | |
56 | public void consume(ResultSet resultSet) throws SQLException { | |
57 | String value = resultSet.getString(columnIndexInResultSet); | |
58 | if (!resultSet.wasNull()) { | |
59 | byte[] bytes = value.getBytes(StandardCharsets.UTF_8); | |
60 | vector.setSafe(currentIndex, bytes); | |
61 | } | |
62 | currentIndex++; | |
63 | } | |
64 | } | |
65 | ||
66 | /** | |
67 | * Non-nullable consumer for var char. | |
68 | */ | |
69 | static class NonNullableVarCharConsumer extends BaseConsumer<VarCharVector> { | |
70 | ||
71 | /** | |
72 | * Instantiate a VarCharConsumer. | |
73 | */ | |
74 | public NonNullableVarCharConsumer(VarCharVector vector, int index) { | |
75 | super(vector, index); | |
76 | } | |
77 | ||
78 | @Override | |
79 | public void consume(ResultSet resultSet) throws SQLException { | |
80 | String value = resultSet.getString(columnIndexInResultSet); | |
81 | byte[] bytes = value.getBytes(StandardCharsets.UTF_8); | |
82 | vector.setSafe(currentIndex, bytes); | |
83 | currentIndex++; | |
84 | } | |
85 | } | |
86 | } |