|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.data.r2dbc.repository.query; |
| 18 | + |
| 19 | +import io.r2dbc.spi.Row; |
| 20 | +import org.springframework.core.convert.converter.Converter; |
| 21 | +import org.springframework.core.convert.converter.ConverterFactory; |
| 22 | +import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToStringConverter; |
| 23 | +import org.springframework.util.Assert; |
| 24 | +import org.springframework.util.NumberUtils; |
| 25 | + |
| 26 | +import java.net.InetAddress; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Date; |
| 30 | +import java.util.List; |
| 31 | +import java.util.UUID; |
| 32 | + |
| 33 | +/** |
| 34 | + * Base class for row data converter. |
| 35 | + * |
| 36 | + * @author Hebert Coelho |
| 37 | + */ |
| 38 | +abstract class RowDataConverter { |
| 39 | + private RowDataConverter() { |
| 40 | + } |
| 41 | + |
| 42 | + static Collection<Object> getConvertersToRegister() { |
| 43 | + List<Object> converters = new ArrayList<>(); |
| 44 | + |
| 45 | + converters.add(RowToBooleanConverter.INSTANCE); |
| 46 | + converters.add(RowToDateConverter.INSTANCE); |
| 47 | + converters.add(RowToInetAddressConverter.INSTANCE); |
| 48 | + converters.add(RowToNumberConverterFactory.INSTANCE); |
| 49 | + converters.add(RowToStringConverter.INSTANCE); |
| 50 | + converters.add(RowToUuidConverter.INSTANCE); |
| 51 | + |
| 52 | + return converters; |
| 53 | + } |
| 54 | + |
| 55 | + public enum RowToBooleanConverter implements Converter<Row, Boolean> { |
| 56 | + INSTANCE; |
| 57 | + |
| 58 | + @Override |
| 59 | + public Boolean convert(Row row) { |
| 60 | + return (Boolean) row.get(0); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public enum RowToDateConverter implements Converter<Row, Date> { |
| 65 | + INSTANCE; |
| 66 | + |
| 67 | + @Override |
| 68 | + public Date convert(Row row) { |
| 69 | + return (Date) row.get(0); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public enum RowToInetAddressConverter implements Converter<Row, InetAddress> { |
| 74 | + INSTANCE; |
| 75 | + |
| 76 | + @Override |
| 77 | + public InetAddress convert(Row row) { |
| 78 | + return (InetAddress) row.get(0); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public enum RowToNumberConverterFactory implements ConverterFactory<Row, Number> { |
| 83 | + |
| 84 | + INSTANCE; |
| 85 | + |
| 86 | + @Override |
| 87 | + public <T extends Number> Converter<Row, T> getConverter(Class<T> targetType) { |
| 88 | + Assert.notNull(targetType, "Target type must not be null"); |
| 89 | + return new RowToNumber<>(targetType); |
| 90 | + } |
| 91 | + |
| 92 | + private static final class RowToNumber<T extends Number> implements Converter<Row, T> { |
| 93 | + private final Class<T> targetType; |
| 94 | + |
| 95 | + RowToNumber(Class<T> targetType) { |
| 96 | + this.targetType = targetType; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public T convert(Row source) { |
| 101 | + |
| 102 | + Object object = source.get(0); |
| 103 | + |
| 104 | + return (object != null ? NumberUtils.convertNumberToTargetClass((Number) object, this.targetType) : null); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public enum RowToStringConverter implements Converter<Row, String> { |
| 109 | + INSTANCE; |
| 110 | + |
| 111 | + @Override |
| 112 | + public String convert(Row row) { |
| 113 | + return row.get(0).toString(); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public enum RowToUuidConverter implements Converter<Row, UUID> { |
| 119 | + INSTANCE; |
| 120 | + |
| 121 | + @Override |
| 122 | + public UUID convert(Row row) { |
| 123 | + return (UUID) row.get(0); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments