diff --git a/include/message_serialization.hpp b/include/message_serialization.hpp index 8d8227bc..168f4117 100644 --- a/include/message_serialization.hpp +++ b/include/message_serialization.hpp @@ -30,6 +30,7 @@ void serialize(DynamicBuffer& buffer, const StmtExecute& value); ReadIterator deserialize(ReadIterator from, ReadIterator last, StmtExecuteResponseHeader& output); std::pair compute_field_type(const BinaryValue&); void serialize(DynamicBuffer& buffer, const StmtFetch& value); +void serialize(DynamicBuffer& buffer, const StmtClose& value); // Text serialization std::ostream& operator<<(std::ostream& os, const Handshake& value); diff --git a/include/messages.hpp b/include/messages.hpp index cd123d32..e9892126 100644 --- a/include/messages.hpp +++ b/include/messages.hpp @@ -253,6 +253,11 @@ struct StmtFetch int4 rows_to_fetch; }; +struct StmtClose +{ + int4 statement_id; +}; + } diff --git a/include/prepared_statement.hpp b/include/prepared_statement.hpp index 202a3ec3..eaa00da1 100644 --- a/include/prepared_statement.hpp +++ b/include/prepared_statement.hpp @@ -78,7 +78,8 @@ public: template BinaryResultset execute_with_cursor(int4 fetch_count, Params&&... params); - // close(Connection) + + void close(); // Destructor should try to auto-close static PreparedStatement prepare(MysqlStream& stream, std::string_view query); diff --git a/main.cpp b/main.cpp index 15830641..b92ee327 100644 --- a/main.cpp +++ b/main.cpp @@ -74,10 +74,11 @@ int main() stream, "SELECT * from users WHERE age < ? and first_name <> ?"); auto res = stmt.execute_with_cursor(2, 200, string_lenenc{"hola"}); print(res); - /*auto make_older = mysql::PreparedStatement::prepare(stream, "UPDATE users SET age = age + 1"); + auto make_older = mysql::PreparedStatement::prepare(stream, "UPDATE users SET age = age + 1"); res = make_older.execute(); print(res); - res = stmt.execute_with_cursor(2, 40, string_lenenc{"hola"}); + make_older.close(); + res = stmt.execute_with_cursor(8, 70, string_lenenc{"hola"}); cout << "\n\n"; - print(res);*/ + print(res); } diff --git a/src/message_serialization.cpp b/src/message_serialization.cpp index e19e203c..6c7bd12c 100644 --- a/src/message_serialization.cpp +++ b/src/message_serialization.cpp @@ -217,6 +217,12 @@ void mysql::serialize(DynamicBuffer& buffer, const StmtFetch& value) serialize(buffer, value.rows_to_fetch); } +void mysql::serialize(DynamicBuffer& buffer, const StmtClose& value) +{ + serialize(buffer, Command::COM_STMT_CLOSE); + serialize(buffer, value.statement_id); +} + // Text serialization std::ostream& mysql::operator<<(std::ostream& os, const Handshake& value) { diff --git a/src/prepared_statement.cpp b/src/prepared_statement.cpp index b7557990..80ebfbe3 100644 --- a/src/prepared_statement.cpp +++ b/src/prepared_statement.cpp @@ -215,7 +215,6 @@ bool mysql::BinaryResultset::retrieve_next() const mysql::OkPacket& mysql::BinaryResultset::ok_packet() const { - // TODO: fetch semantics are not aligned with this assertion assert(state_ == State::exhausted || (state_ == State::data_available && cursor_exists())); return ok_packet_; @@ -234,7 +233,6 @@ mysql::BinaryResultset mysql::PreparedStatement::do_execute( { std::vector read_buffer; - // TODO: other cursor types DynamicBuffer write_buffer; serialize(write_buffer, message); stream_->reset_sequence_number(); @@ -243,4 +241,14 @@ mysql::BinaryResultset mysql::PreparedStatement::do_execute( return mysql::BinaryResultset {*stream_, statement_id_, fetch_count}; } +void mysql::PreparedStatement::close() +{ + assert(statement_id_ != 0); + StmtClose msg { statement_id_ }; + + DynamicBuffer write_buffer; + serialize(write_buffer, msg); + stream_->reset_sequence_number(); + stream_->write(write_buffer.get()); +}