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.ExtendedBatchQueryCommandCodec; 18 19 import hunt.database.driver.postgresql.impl.codec.ExtendedQueryCommandBaseCodec; 20 import hunt.database.driver.postgresql.impl.codec.Parse; 21 import hunt.database.driver.postgresql.impl.codec.PgEncoder; 22 import hunt.database.driver.postgresql.impl.codec.PgPreparedStatement; 23 24 import hunt.database.base.Tuple; 25 import hunt.database.base.impl.command.ExtendedBatchQueryCommand; 26 27 import hunt.collection.List; 28 import hunt.logging; 29 30 import std.variant; 31 32 /** 33 */ 34 class ExtendedBatchQueryCommandCodec(R) : ExtendedQueryCommandBaseCodec!(R, 35 ExtendedBatchQueryCommand!(R)) { 36 37 this(ExtendedBatchQueryCommand!(R) cmd) { 38 super(cmd); 39 } 40 41 override void encode(PgEncoder encoder) { 42 if (cmd.isSuspended()) { 43 encoder.writeExecute(cmd.cursorId(), cmd.fetch()); 44 encoder.writeSync(); 45 } else { 46 PgPreparedStatement ps = cast(PgPreparedStatement) cmd.preparedStatement(); 47 version(HUNT_DB_DEBUG) tracef("batch sql: %s", ps.sql()); 48 if (ps.bind.statement == 0) { 49 encoder.writeParse(new Parse(ps.sql())); 50 } 51 if (cmd.params().isEmpty()) { 52 // We set suspended to false as we won't get a command complete command back from Postgres 53 this.result = false; 54 } else { 55 foreach (Tuple param; cmd.params()) { 56 encoder.writeBind(ps.bind, cmd.cursorId(), cast(List!(Variant)) param); 57 encoder.writeExecute(cmd.cursorId(), cmd.fetch()); 58 } 59 } 60 encoder.writeSync(); 61 } 62 } 63 }