Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cpp/src/arrow/flight/sql/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ FlightSqlClient::FlightSqlClient(std::shared_ptr<FlightClient> client)

PreparedStatement::PreparedStatement(FlightSqlClient* client, std::string handle,
std::shared_ptr<Schema> dataset_schema,
std::shared_ptr<Schema> parameter_schema)
std::shared_ptr<Schema> parameter_schema,
bool is_update)
: client_(client),
handle_(std::move(handle)),
dataset_schema_(std::move(dataset_schema)),
parameter_schema_(std::move(parameter_schema)),
is_closed_(false) {}
is_closed_(false),
is_update_(is_update) {}

PreparedStatement::~PreparedStatement() {
if (IsClosed()) return;
Expand Down Expand Up @@ -632,7 +634,8 @@ arrow::Result<std::shared_ptr<PreparedStatement>> PreparedStatement::ParseRespon
auto handle = prepared_statement_result.prepared_statement_handle();

return std::make_shared<PreparedStatement>(client, handle, dataset_schema,
parameter_schema);
parameter_schema,
prepared_statement_result.is_update());
}

arrow::Result<std::unique_ptr<FlightInfo>> PreparedStatement::Execute(
Expand Down Expand Up @@ -700,6 +703,8 @@ Status PreparedStatement::SetParameters(

bool PreparedStatement::IsClosed() const { return is_closed_; }

bool PreparedStatement::is_update() const { return is_update_; }

const std::shared_ptr<Schema>& PreparedStatement::dataset_schema() const {
return dataset_schema_;
}
Expand Down
8 changes: 7 additions & 1 deletion cpp/src/arrow/flight/sql/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,11 @@ class ARROW_FLIGHT_SQL_EXPORT PreparedStatement {
/// \param[in] handle Handle for this prepared statement.
/// \param[in] dataset_schema Schema of the resulting dataset.
/// \param[in] parameter_schema Schema of the parameters (if any).
/// \param[in] is_update Whether this is an update query.
PreparedStatement(FlightSqlClient* client, std::string handle,
std::shared_ptr<Schema> dataset_schema,
std::shared_ptr<Schema> parameter_schema);
std::shared_ptr<Schema> parameter_schema,
bool is_update = false);

/// \brief Default destructor for the PreparedStatement class.
/// The destructor will call the Close method from the class in order,
Expand Down Expand Up @@ -472,6 +474,9 @@ class ARROW_FLIGHT_SQL_EXPORT PreparedStatement {
/// \return The ResultSet schema from the query.
const std::shared_ptr<Schema>& dataset_schema() const;

/// \brief Check if the prepared statement represents an update query.
bool is_update() const;

/// \brief Set a RecordBatch that contains the parameters that will be bound.
Status SetParameters(std::shared_ptr<RecordBatch> parameter_binding);

Expand Down Expand Up @@ -499,6 +504,7 @@ class ARROW_FLIGHT_SQL_EXPORT PreparedStatement {
std::shared_ptr<Schema> parameter_schema_;
std::shared_ptr<RecordBatchReader> parameter_binding_;
bool is_closed_;
bool is_update_;
};

/// \brief A handle for a server-side savepoint.
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/flight/sql/example/sqlite_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,9 @@ class SQLiteFlightSqlServer::Impl {
}

std::shared_ptr<Schema> parameter_schema = arrow::schema(parameter_fields);
const bool is_update = sqlite3_stmt_readonly(stmt) == 0;
return ActionCreatePreparedStatementResult{
std::move(dataset_schema), std::move(parameter_schema), std::move(handle)};
std::move(dataset_schema), std::move(parameter_schema), std::move(handle), is_update};
}

Status ClosePreparedStatement(const ServerCallContext& context,
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/flight/sql/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ ARROW_UNSUPPRESS_DEPRECATION_WARNING
arrow::Result<Result> PackActionResult(ActionCreatePreparedStatementResult result) {
pb::sql::ActionCreatePreparedStatementResult pb_result;
pb_result.set_prepared_statement_handle(std::move(result.prepared_statement_handle));
pb_result.set_is_update(result.is_update);
if (result.dataset_schema != nullptr) {
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Buffer> serialized,
ipc::SerializeSchema(*result.dataset_schema));
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/arrow/flight/sql/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ struct ARROW_FLIGHT_SQL_EXPORT ActionCreatePreparedStatementResult {
std::shared_ptr<Schema> parameter_schema;
/// \brief The server-generated opaque identifier for the statement.
std::string prepared_statement_handle;
/// \brief Whether this prepared statement represents an update query.
bool is_update = false;
};

/// @}
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/arrow/flight/sql/server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ TEST_F(TestFlightSqlServer, TestCommandStatementUpdate) {
TEST_F(TestFlightSqlServer, TestCommandPreparedStatementQuery) {
ASSERT_OK_AND_ASSIGN(auto prepared_statement,
sql_client->Prepare({}, "SELECT * FROM intTable"));
ASSERT_FALSE(prepared_statement->is_update());

ASSERT_OK_AND_ASSIGN(auto flight_info, prepared_statement->Execute());

Expand Down Expand Up @@ -587,6 +588,7 @@ TEST_F(TestFlightSqlServer, TestCommandPreparedStatementUpdate) {
auto prepared_statement,
sql_client->Prepare(
{}, "INSERT INTO INTTABLE (keyName, value) VALUES ('new_value', 999)"));
ASSERT_TRUE(prepared_statement->is_update());

ASSERT_OK_AND_EQ(5, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
ASSERT_OK_AND_EQ(1, prepared_statement->ExecuteUpdate());
Expand Down
Loading