1 module hunt.database.driver.mysql.impl.codec.ExtendedBatchQueryCommandCodec;
2 
3 import hunt.database.driver.mysql.impl.codec.ExtendedQueryCommandBaseCodec;
4 import hunt.database.driver.mysql.impl.codec.MySQLEncoder;
5 import hunt.database.driver.mysql.impl.codec.Packets;
6 
7 import hunt.database.driver.mysql.MySQLException;
8 
9 import hunt.database.base.Exceptions;
10 import hunt.database.base.impl.command.CommandResponse;
11 import hunt.database.base.impl.command.ExtendedBatchQueryCommand;
12 import hunt.database.base.Tuple;
13 
14 import hunt.collection.List;
15 import hunt.net.buffer.ByteBuf;
16 
17 
18 
19 class ExtendedBatchQueryCommandCodec(R) : ExtendedQueryCommandBaseCodec!(R, ExtendedBatchQueryCommand!(R)) {
20 
21     private List!(Tuple) params;
22     private int batchIdx = 0;
23 
24     this(ExtendedBatchQueryCommand!(R) cmd) {
25         super(cmd);
26         params = cmd.params();
27     }
28 
29     override
30     void encode(MySQLEncoder encoder) {
31         super.encode(encoder);
32         if (params.isEmpty() && statement.paramDesc.paramDefinitions().length > 0) {
33             completionHandler(failedResponse!(ICommandResponse)(
34                     new DatabaseException("Statement parameter is not set because of the empty batch param list")));
35             return;
36         }
37         doExecuteBatch();
38     }
39 
40     override
41     protected void handleSingleResultsetDecodingCompleted(int serverStatusFlags, int affectedRows, int lastInsertId) {
42         super.handleSingleResultsetDecodingCompleted(serverStatusFlags, affectedRows, lastInsertId);
43         doExecuteBatch();
44     }
45 
46     override
47     protected bool isDecodingCompleted(int serverStatusFlags) {
48         return super.isDecodingCompleted(serverStatusFlags) && batchIdx == params.size();
49     }
50 
51     private void doExecuteBatch() {
52         if (batchIdx < params.size()) {
53             this.sequenceId = 0;
54             Tuple param = params.get(batchIdx);
55             sendStatementExecuteCommand(statement.statementId, 
56                 statement.paramDesc.paramDefinitions(), sendType, param, cast(byte) 0x00);
57             batchIdx++;
58         }
59     }
60 }