Skip to content

Commit 5c1bb52

Browse files
committed
Refactor
1 parent c044dfa commit 5c1bb52

7 files changed

Lines changed: 521 additions & 213 deletions

File tree

src/fb-cpp/Attachment.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,21 @@ void Attachment::dropDatabase()
9797
disconnectOrDrop(true);
9898
}
9999

100-
bool Attachment::execute(Transaction& transaction, std::string_view sql)
101-
{
102-
return execute(transaction, sql, StatementOptions{});
103-
}
104-
105100
bool Attachment::execute(Transaction& transaction, std::string_view sql, const StatementOptions& options)
106101
{
107102
Statement statement{*this, transaction, sql, options};
108103
return statement.execute(transaction);
109104
}
110105

111-
RowSet Attachment::queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows)
112-
{
113-
return queryRowSet(transaction, sql, maxRows, StatementOptions{});
114-
}
115-
116106
RowSet Attachment::queryRowSet(
117107
Transaction& transaction, std::string_view sql, unsigned maxRows, const StatementOptions& options)
118108
{
119109
Statement statement{*this, transaction, sql, options};
110+
return queryPreparedRowSet(statement, transaction, maxRows);
111+
}
120112

113+
RowSet Attachment::queryPreparedRowSet(Statement& statement, Transaction& transaction, unsigned maxRows)
114+
{
121115
switch (statement.getType())
122116
{
123117
case StatementType::SELECT:

src/fb-cpp/Attachment.h

Lines changed: 128 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626
#define FBCPP_ATTACHMENT_H
2727

2828
#include "fb-api.h"
29-
#include "SmartPtrs.h"
3029
#include "RowSet.h"
30+
#include "SmartPtrs.h"
31+
#include "StatementOptions.h"
32+
#include <cstddef>
3133
#include <cstdint>
3234
#include <memory>
3335
#include <optional>
3436
#include <string>
3537
#include <string_view>
3638
#include <vector>
37-
#include <cstddef>
3839

3940

4041
///
@@ -43,7 +44,7 @@
4344
namespace fbcpp
4445
{
4546
class Client;
46-
class StatementOptions;
47+
class Statement;
4748
class Transaction;
4849

4950
///
@@ -308,64 +309,101 @@ namespace fbcpp
308309
///
309310
/// Prepares and executes an SQL statement using the supplied transaction.
310311
///
311-
bool execute(Transaction& transaction, std::string_view sql);
312+
bool execute(Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
312313

313314
///
314-
/// Prepares and executes an SQL statement using the supplied transaction and statement options.
315+
/// Prepares, binds parameters, and executes an SQL statement using the supplied transaction.
315316
///
316-
bool execute(Transaction& transaction, std::string_view sql, const StatementOptions& options);
317+
template <typename Params>
318+
bool execute(Transaction& transaction, std::string_view sql, const Params& params);
319+
320+
///
321+
/// Prepares, binds parameters, and executes an SQL statement using the supplied transaction and statement
322+
/// options.
323+
///
324+
template <typename Params>
325+
bool execute(
326+
Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
317327

318328
///
319329
/// Prepares and executes a query using the supplied transaction and returns up to maxRows rows.
320330
///
321-
RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows);
331+
RowSet queryRowSet(
332+
Transaction& transaction, std::string_view sql, unsigned maxRows, const StatementOptions& options = {});
322333

323334
///
324-
/// Prepares and executes a query using the supplied transaction and statement options and returns up to maxRows
335+
/// Prepares, binds parameters, and executes a query using the supplied transaction and returns up to maxRows
325336
/// rows.
326337
///
327-
RowSet queryRowSet(
328-
Transaction& transaction, std::string_view sql, unsigned maxRows, const StatementOptions& options);
338+
template <typename Params>
339+
RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params);
340+
341+
///
342+
/// Prepares, binds parameters, and executes a query using the supplied transaction and statement options and
343+
/// returns up to maxRows rows.
344+
///
345+
template <typename Params>
346+
RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
347+
const StatementOptions& options, const Params& params);
329348

330349
///
331350
/// Prepares and executes a query using the supplied transaction and returns the first column of the first row.
332351
///
333352
template <typename T>
334-
std::optional<T> queryScalar(Transaction& transaction, std::string_view sql);
353+
std::optional<T> queryScalar(
354+
Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
335355

336356
///
337-
/// Prepares and executes a query using the supplied transaction and statement options and returns the first
338-
/// column of the first row.
357+
/// Prepares, binds parameters, and executes a query using the supplied transaction and returns the first column
358+
/// of the first row.
339359
///
340-
template <typename T>
341-
std::optional<T> queryScalar(Transaction& transaction, std::string_view sql, const StatementOptions& options);
360+
template <typename T, typename Params>
361+
std::optional<T> queryScalar(Transaction& transaction, std::string_view sql, const Params& params);
362+
363+
///
364+
/// Prepares, binds parameters, and executes a query using the supplied transaction and statement options and
365+
/// returns the first column of the first row.
366+
///
367+
template <typename T, typename Params>
368+
std::optional<T> queryScalar(
369+
Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
342370

343371
///
344372
/// Prepares and executes a query using the supplied transaction and returns the first row mapped as T.
345373
///
346374
template <typename T>
347-
std::optional<T> queryFirstRowAs(Transaction& transaction, std::string_view sql);
375+
std::optional<T> queryFirstRowAs(
376+
Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
348377

349378
///
350-
/// Prepares and executes a query using the supplied transaction and statement options and returns the first row
379+
/// Prepares, binds parameters, and executes a query using the supplied transaction and returns the first row
351380
/// mapped as T.
352381
///
353-
template <typename T>
382+
template <typename T, typename Params>
383+
std::optional<T> queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params);
384+
385+
///
386+
/// Prepares, binds parameters, and executes a query using the supplied transaction and statement options and
387+
/// returns the first row mapped as T.
388+
///
389+
template <typename T, typename Params>
354390
std::optional<T> queryFirstRowAs(
355-
Transaction& transaction, std::string_view sql, const StatementOptions& options);
391+
Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
356392

357393
private:
358394
void disconnectOrDrop(bool drop);
395+
RowSet queryPreparedRowSet(Statement& statement, Transaction& transaction, unsigned maxRows);
359396

360397
private:
361398
Client* client;
362399
FbRef<fb::IAttachment> handle;
363400
};
364401

365402
template <typename T>
366-
std::optional<T> Attachment::queryScalar(Transaction& transaction, std::string_view sql)
403+
std::optional<T> Attachment::queryScalar(
404+
Transaction& transaction, std::string_view sql, const StatementOptions& options)
367405
{
368-
auto rowSet = queryRowSet(transaction, sql, 1u);
406+
auto rowSet = queryRowSet(transaction, sql, 1u, options);
369407

370408
if (rowSet.getCount() == 0u)
371409
return std::nullopt;
@@ -374,40 +412,98 @@ namespace fbcpp
374412
}
375413

376414
template <typename T>
377-
std::optional<T> Attachment::queryScalar(
415+
std::optional<T> Attachment::queryFirstRowAs(
378416
Transaction& transaction, std::string_view sql, const StatementOptions& options)
379417
{
380418
auto rowSet = queryRowSet(transaction, sql, 1u, options);
381419

382420
if (rowSet.getCount() == 0u)
383421
return std::nullopt;
384422

385-
return rowSet.getRow(0).get<std::optional<T>>(0);
423+
return rowSet.getRow(0).get<T>();
386424
}
425+
} // namespace fbcpp
387426

388-
template <typename T>
389-
std::optional<T> Attachment::queryFirstRowAs(Transaction& transaction, std::string_view sql)
427+
#include "Statement.h"
428+
429+
namespace fbcpp
430+
{
431+
template <typename Params>
432+
bool Attachment::execute(Transaction& transaction, std::string_view sql, const Params& params)
390433
{
391-
auto rowSet = queryRowSet(transaction, sql, 1u);
434+
return execute(transaction, sql, StatementOptions{}, params);
435+
}
436+
437+
template <typename Params>
438+
bool Attachment::execute(
439+
Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
440+
{
441+
Statement statement{*this, transaction, sql, options};
442+
statement.set(params);
443+
return statement.execute(transaction);
444+
}
445+
446+
template <typename Params>
447+
RowSet Attachment::queryRowSet(
448+
Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params)
449+
{
450+
return queryRowSet(transaction, sql, maxRows, StatementOptions{}, params);
451+
}
452+
453+
template <typename Params>
454+
RowSet Attachment::queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
455+
const StatementOptions& options, const Params& params)
456+
{
457+
Statement statement{*this, transaction, sql, options};
458+
statement.set(params);
459+
return queryPreparedRowSet(statement, transaction, maxRows);
460+
}
461+
462+
template <typename T, typename Params>
463+
std::optional<T> Attachment::queryScalar(Transaction& transaction, std::string_view sql, const Params& params)
464+
{
465+
auto rowSet = queryRowSet(transaction, sql, 1u, params);
392466

393467
if (rowSet.getCount() == 0u)
394468
return std::nullopt;
395469

396-
return rowSet.getRow(0).get<T>();
470+
return rowSet.getRow(0).template get<std::optional<T>>(0);
397471
}
398472

399-
template <typename T>
473+
template <typename T, typename Params>
474+
std::optional<T> Attachment::queryScalar(
475+
Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
476+
{
477+
auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
478+
479+
if (rowSet.getCount() == 0u)
480+
return std::nullopt;
481+
482+
return rowSet.getRow(0).template get<std::optional<T>>(0);
483+
}
484+
485+
template <typename T, typename Params>
486+
std::optional<T> Attachment::queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params)
487+
{
488+
auto rowSet = queryRowSet(transaction, sql, 1u, params);
489+
490+
if (rowSet.getCount() == 0u)
491+
return std::nullopt;
492+
493+
return rowSet.getRow(0).template get<T>();
494+
}
495+
496+
template <typename T, typename Params>
400497
std::optional<T> Attachment::queryFirstRowAs(
401-
Transaction& transaction, std::string_view sql, const StatementOptions& options)
498+
Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
402499
{
403-
auto rowSet = queryRowSet(transaction, sql, 1u, options);
500+
auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
404501

405502
if (rowSet.getCount() == 0u)
406503
return std::nullopt;
407504

408-
return rowSet.getRow(0).get<T>();
505+
return rowSet.getRow(0).template get<T>();
409506
}
410507
} // namespace fbcpp
411508

412-
413509
#endif // FBCPP_ATTACHMENT_H

src/fb-cpp/RowSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
#include "RowSet.h"
26+
#include "Attachment.h"
2627
#include "Client.h"
2728
#include "Statement.h"
2829
#include <algorithm>

src/fb-cpp/Statement.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,52 @@ Statement::Statement(
181181
outRow = std::make_unique<Row>(attachment.getClient(), outDescriptors, std::span{outMessage});
182182
}
183183

184+
Statement::Statement(Statement&& o) noexcept
185+
: attachment{o.attachment},
186+
statusWrapper{std::move(o.statusWrapper)},
187+
calendarConverter{std::move(o.calendarConverter)},
188+
numericConverter{std::move(o.numericConverter)},
189+
statementHandle{std::move(o.statementHandle)},
190+
resultSetHandle{std::move(o.resultSetHandle)},
191+
inMetadata{std::move(o.inMetadata)},
192+
inDescriptors{std::move(o.inDescriptors)},
193+
inMessage{std::move(o.inMessage)},
194+
outMetadata{std::move(o.outMetadata)},
195+
outDescriptors{std::move(o.outDescriptors)},
196+
outMessage{std::move(o.outMessage)},
197+
outRow{std::make_unique<Row>(attachment->getClient(), outDescriptors, std::span{outMessage})},
198+
type{o.type},
199+
cursorFlags{o.cursorFlags}
200+
{
201+
o.outRow.reset();
202+
}
203+
204+
Statement& Statement::operator=(Statement&& o) noexcept
205+
{
206+
if (this != &o)
207+
{
208+
attachment = o.attachment;
209+
statusWrapper = std::move(o.statusWrapper);
210+
calendarConverter = std::move(o.calendarConverter);
211+
numericConverter = std::move(o.numericConverter);
212+
statementHandle = std::move(o.statementHandle);
213+
resultSetHandle = std::move(o.resultSetHandle);
214+
inMetadata = std::move(o.inMetadata);
215+
inDescriptors = std::move(o.inDescriptors);
216+
inMessage = std::move(o.inMessage);
217+
outMetadata = std::move(o.outMetadata);
218+
outDescriptors = std::move(o.outDescriptors);
219+
outMessage = std::move(o.outMessage);
220+
outRow = std::make_unique<Row>(attachment->getClient(), outDescriptors, std::span{outMessage});
221+
type = o.type;
222+
cursorFlags = o.cursorFlags;
223+
224+
o.outRow.reset();
225+
}
226+
227+
return *this;
228+
}
229+
184230
void Statement::free()
185231
{
186232
assert(isValid());
@@ -243,6 +289,11 @@ bool Statement::execute(Transaction& transaction)
243289
}
244290
}
245291

292+
Client& Statement::getClient() noexcept
293+
{
294+
return attachment->getClient();
295+
}
296+
246297
bool Statement::fetchNext()
247298
{
248299
assert(isValid());

0 commit comments

Comments
 (0)