]>
git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.adapter
.jdbc
.consumer
;
21 import java
.sql
.ResultSet
;
22 import java
.sql
.SQLException
;
23 import java
.util
.Calendar
;
24 import java
.util
.concurrent
.TimeUnit
;
26 import org
.apache
.arrow
.vector
.DateDayVector
;
27 import org
.apache
.arrow
.vector
.DateMilliVector
;
30 * Consumer which consume date type values from {@link ResultSet}.
31 * Write the data to {@link org.apache.arrow.vector.DateDayVector}.
33 public class DateConsumer
{
36 * Creates a consumer for {@link DateMilliVector}.
38 public static JdbcConsumer
<DateDayVector
> createConsumer(
39 DateDayVector vector
, int index
, boolean nullable
, Calendar calendar
) {
41 return new NullableDateConsumer(vector
, index
, calendar
);
43 return new NonNullableDateConsumer(vector
, index
, calendar
);
48 * Nullable consumer for date.
50 static class NullableDateConsumer
extends BaseConsumer
<DateDayVector
> {
52 protected final Calendar calendar
;
55 * Instantiate a DateConsumer.
57 public NullableDateConsumer(DateDayVector vector
, int index
) {
58 this(vector
, index
, /* calendar */null);
62 * Instantiate a DateConsumer.
64 public NullableDateConsumer(DateDayVector vector
, int index
, Calendar calendar
) {
66 this.calendar
= calendar
;
70 public void consume(ResultSet resultSet
) throws SQLException
{
71 Date date
= calendar
== null ? resultSet
.getDate(columnIndexInResultSet
) :
72 resultSet
.getDate(columnIndexInResultSet
, calendar
);
73 if (!resultSet
.wasNull()) {
74 // for fixed width vectors, we have allocated enough memory proactively,
75 // so there is no need to call the setSafe method here.
76 vector
.set(currentIndex
, Math
.toIntExact(TimeUnit
.MILLISECONDS
.toDays(date
.getTime())));
83 * Non-nullable consumer for date.
85 static class NonNullableDateConsumer
extends BaseConsumer
<DateDayVector
> {
87 protected final Calendar calendar
;
90 * Instantiate a DateConsumer.
92 public NonNullableDateConsumer(DateDayVector vector
, int index
) {
93 this(vector
, index
, /* calendar */null);
97 * Instantiate a DateConsumer.
99 public NonNullableDateConsumer(DateDayVector vector
, int index
, Calendar calendar
) {
100 super(vector
, index
);
101 this.calendar
= calendar
;
105 public void consume(ResultSet resultSet
) throws SQLException
{
106 Date date
= calendar
== null ? resultSet
.getDate(columnIndexInResultSet
) :
107 resultSet
.getDate(columnIndexInResultSet
, calendar
);
108 // for fixed width vectors, we have allocated enough memory proactively,
109 // so there is no need to call the setSafe method here.
110 vector
.set(currentIndex
, Math
.toIntExact(TimeUnit
.MILLISECONDS
.toDays(date
.getTime())));