Skip to content

Commit dd8d8dc

Browse files
committed
Add Attachment::execute methods
1 parent 928d3b2 commit dd8d8dc

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/fb-cpp/Attachment.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "Attachment.h"
2626
#include "Client.h"
2727
#include "Exception.h"
28+
#include "Statement.h"
29+
#include "Transaction.h"
2830

2931
using namespace fbcpp;
3032
using namespace fbcpp::impl;
@@ -93,3 +95,14 @@ void Attachment::dropDatabase()
9395
{
9496
disconnectOrDrop(true);
9597
}
98+
99+
bool Attachment::execute(Transaction& transaction, std::string_view sql)
100+
{
101+
return execute(transaction, sql, StatementOptions{});
102+
}
103+
104+
bool Attachment::execute(Transaction& transaction, std::string_view sql, const StatementOptions& options)
105+
{
106+
Statement statement{*this, transaction, sql, options};
107+
return statement.execute(transaction);
108+
}

src/fb-cpp/Attachment.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <memory>
3232
#include <optional>
3333
#include <string>
34+
#include <string_view>
3435
#include <vector>
3536
#include <cstddef>
3637

@@ -41,6 +42,8 @@
4142
namespace fbcpp
4243
{
4344
class Client;
45+
class StatementOptions;
46+
class Transaction;
4447

4548
///
4649
/// Represents options used when creating an Attachment object.
@@ -301,6 +304,16 @@ namespace fbcpp
301304
///
302305
void dropDatabase();
303306

307+
///
308+
/// Prepares and executes an SQL statement using the supplied transaction.
309+
///
310+
bool execute(Transaction& transaction, std::string_view sql);
311+
312+
///
313+
/// Prepares and executes an SQL statement using the supplied transaction and statement options.
314+
///
315+
bool execute(Transaction& transaction, std::string_view sql, const StatementOptions& options);
316+
304317
private:
305318
void disconnectOrDrop(bool drop);
306319

src/test/Attachment.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ BOOST_AUTO_TEST_CASE(createDatabaseWithForcedWritesOff)
9292
transaction.commit();
9393
}
9494

95+
BOOST_AUTO_TEST_CASE(executePreparesAndExecutesStatement)
96+
{
97+
const auto database = getTempFile("Attachment-executePreparesAndExecutesStatement.fdb");
98+
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true).setForcedWrites(false)};
99+
FbDropDatabase attachmentDrop{attachment};
100+
101+
Transaction transaction{attachment};
102+
BOOST_CHECK(attachment.execute(transaction, "create table t (id integer not null primary key)"));
103+
transaction.commitRetaining();
104+
105+
BOOST_CHECK(attachment.execute(transaction, "insert into t (id) values (1)"));
106+
BOOST_CHECK(attachment.execute(transaction, "select id from t"));
107+
BOOST_CHECK(!attachment.execute(transaction, "select id from t where id = 2"));
108+
109+
transaction.commit();
110+
}
111+
95112
BOOST_AUTO_TEST_CASE(isNotValidAfterMove)
96113
{
97114
const auto database = getTempFile("Attachment-isNotValidAfterMove.fdb");

0 commit comments

Comments
 (0)