1 /*
2  * Copyright (C) 2019, HuntLabs
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.PrepareStatementCommandCodec;
18 
19 import hunt.database.driver.postgresql.impl.codec.Describe;
20 import hunt.database.driver.postgresql.impl.codec.ErrorResponse;
21 import hunt.database.driver.postgresql.impl.codec.Parse;
22 import hunt.database.driver.postgresql.impl.codec.CommandCodec;
23 import hunt.database.driver.postgresql.impl.codec.PgEncoder;
24 import hunt.database.driver.postgresql.impl.codec.PgParamDesc;
25 import hunt.database.driver.postgresql.impl.codec.PgPreparedStatement;
26 import hunt.database.driver.postgresql.impl.codec.PgRowDesc;
27 
28 
29 import hunt.database.base.impl.TxStatus;
30 import hunt.database.base.impl.command.PrepareStatementCommand;
31 import hunt.database.base.impl.PreparedStatement;
32 
33 class PrepareStatementCommandCodec : CommandCodec!(PreparedStatement, PrepareStatementCommand) {
34 
35     private PgParamDesc parameterDesc;
36     private PgRowDesc rowDesc;
37 
38     this(PrepareStatementCommand cmd) {
39         super(cmd);
40     }
41 
42     override
43     void encode(PgEncoder encoder) {
44         encoder.writeParse(new Parse(cmd.sql(), cmd.statement()));
45         encoder.writeDescribe(new Describe(cmd.statement(), null));
46         encoder.writeSync();
47     }
48 
49     override
50     void handleParseComplete() {
51         // Response to parse
52     }
53 
54     override
55     void handleParameterDescription(PgParamDesc paramDesc) {
56         // Response to Describe
57         this.parameterDesc = paramDesc;
58     }
59 
60     override
61     void handleRowDescription(PgRowDesc rowDesc) {
62         // Response to Describe
63         this.rowDesc = rowDesc;
64     }
65 
66     override
67     void handleNoData() {
68         // Response to Describe
69     }
70 
71     override
72     void handleErrorResponse(ErrorResponse errorResponse) {
73         _failure = errorResponse.toException();
74     }
75 
76     override
77     void handleReadyForQuery(TxStatus txStatus) {
78         result = new PgPreparedStatement(cmd.sql(), cmd.statement(), this.parameterDesc, this.rowDesc);
79         super.handleReadyForQuery(txStatus);
80     }
81 }