|
| 1 | +/* |
| 2 | + * Copyright 2017-2020 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 | + * https://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 io.r2dbc.postgresql.codec; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.r2dbc.postgresql.api.PostgresqlResult; |
| 21 | +import io.r2dbc.postgresql.api.RefCursor; |
| 22 | +import io.r2dbc.postgresql.client.Parameter; |
| 23 | +import io.r2dbc.postgresql.message.Format; |
| 24 | +import io.r2dbc.postgresql.type.PostgresqlObjectId; |
| 25 | +import io.r2dbc.postgresql.util.Assert; |
| 26 | +import io.r2dbc.postgresql.util.ByteBufUtils; |
| 27 | +import reactor.core.publisher.Mono; |
| 28 | +import reactor.util.annotation.Nullable; |
| 29 | + |
| 30 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.REF_CURSOR; |
| 31 | + |
| 32 | +final class RefCursorCodec extends AbstractCodec<RefCursor> { |
| 33 | + |
| 34 | + static final RefCursorCodec INSTANCE = new RefCursorCodec(); |
| 35 | + |
| 36 | + RefCursorCodec() { |
| 37 | + super(RefCursor.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Parameter encodeNull() { |
| 42 | + throw new UnsupportedOperationException("RefCursor cannot be encoded"); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + boolean doCanDecode(PostgresqlObjectId type, Format format) { |
| 47 | + Assert.requireNonNull(format, "format must not be null"); |
| 48 | + Assert.requireNonNull(type, "type must not be null"); |
| 49 | + |
| 50 | + return REF_CURSOR == type; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + RefCursor doDecode(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Format format, @Nullable Class<? extends RefCursor> type) { |
| 55 | + Assert.requireNonNull(buffer, "byteBuf must not be null"); |
| 56 | + |
| 57 | + return new SimpleRefCursor(ByteBufUtils.decode(buffer)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + Parameter doEncode(RefCursor value) { |
| 62 | + throw new UnsupportedOperationException("RefCursor cannot be encoded"); |
| 63 | + } |
| 64 | + |
| 65 | + static class SimpleRefCursor implements RefCursor { |
| 66 | + |
| 67 | + private final String portal; |
| 68 | + |
| 69 | + SimpleRefCursor(String portal) { |
| 70 | + this.portal = portal; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getCursorName() { |
| 75 | + return this.portal; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Mono<PostgresqlResult> fetch() { |
| 80 | + throw new UnsupportedOperationException("Stateless RefCursor does not support fetch()"); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Mono<Void> close() { |
| 85 | + throw new UnsupportedOperationException("Stateless RefCursor does not support close()"); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String toString() { |
| 90 | + return "SimpleRefCursor{" + |
| 91 | + "portal='" + this.portal + '\'' + |
| 92 | + '}'; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments