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// /
4344namespace 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
0 commit comments