1 /*
2  * Copyright (C) 2018 Julien Viet
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 module hunt.database.driver.postgresql.impl.codec.QueryCommandBaseCodec;
18 
19 import hunt.database.driver.postgresql.impl.codec.ErrorResponse;
20 import hunt.database.driver.postgresql.impl.codec.RowResultDecoder;
21 import hunt.database.driver.postgresql.impl.codec.CommandCodec;
22 
23 import hunt.database.base.Row;
24 import hunt.database.base.impl.RowDecoder;
25 import hunt.database.base.impl.RowDesc;
26 import hunt.database.base.impl.RowSetImpl;
27 import hunt.database.base.impl.command.QueryCommandBase;
28 
29 import hunt.Exceptions;
30 import hunt.net.buffer.ByteBuf;
31 
32 // import java.util.stream.Collector;
33 
34 abstract class QueryCommandBaseCodec(T, C) : CommandCodec!(bool, C) 
35     if(is(C : QueryCommandBase!(T))) { 
36 
37     RowResultDecoder!T decoder;
38 
39     this(C cmd) {
40         super(cmd);
41     }
42 
43     override
44     void handleCommandComplete(int updated) {
45         this.result = false;
46         T r; // RowSet
47         int size;
48         RowDesc desc;
49         if (decoder !is null) {
50             r = decoder.complete();
51             desc = decoder.desc;
52             size = decoder.size();
53             decoder.reset();
54         } else {
55             r = new RowSetImpl(); 
56             size = 0;
57             desc = null;
58         }
59         cmd.resultHandler().handleResult(updated, size, desc, r);
60     }
61 
62     override
63     void handleErrorResponse(ErrorResponse errorResponse) {
64         _failure = errorResponse.toException();
65     }
66 
67     override void decodeRow(int len, ByteBuf inBuffer) {
68         decoder.decodeRow(len, inBuffer);
69         // if(decoder is null) {
70         //     import hunt.logging;
71         //     warningf("The decoder is null, len: %d, buffer: %s", len, inBuffer.toString());
72         // } else {
73         //     decoder.decodeRow(len, inBuffer);
74         // }
75     }
76 
77     // private static <A, T> T emptyResult(Collector!(Row, A, T) collector) {
78     //     return collector.finisher().apply(collector.supplier().get());
79     // }
80 }