|
26 | 26 | #define FBCPP_ATTACHMENT_H |
27 | 27 |
|
28 | 28 | #include "fb-api.h" |
| 29 | +#include "RowSet.h" |
29 | 30 | #include "SmartPtrs.h" |
| 31 | +#include "StatementOptions.h" |
| 32 | +#include <cstddef> |
30 | 33 | #include <cstdint> |
31 | 34 | #include <memory> |
32 | 35 | #include <optional> |
33 | 36 | #include <string> |
| 37 | +#include <string_view> |
34 | 38 | #include <vector> |
35 | | -#include <cstddef> |
36 | 39 |
|
37 | 40 |
|
38 | 41 | /// |
|
41 | 44 | namespace fbcpp |
42 | 45 | { |
43 | 46 | class Client; |
| 47 | + class Statement; |
| 48 | + class Transaction; |
44 | 49 |
|
45 | 50 | /// |
46 | 51 | /// Represents options used when creating an Attachment object. |
@@ -301,14 +306,204 @@ namespace fbcpp |
301 | 306 | /// |
302 | 307 | void dropDatabase(); |
303 | 308 |
|
| 309 | + /// |
| 310 | + /// Prepares and executes an SQL statement using the supplied transaction. |
| 311 | + /// |
| 312 | + bool execute(Transaction& transaction, std::string_view sql, const StatementOptions& options = {}); |
| 313 | + |
| 314 | + /// |
| 315 | + /// Prepares, binds parameters, and executes an SQL statement using the supplied transaction. |
| 316 | + /// |
| 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); |
| 327 | + |
| 328 | + /// |
| 329 | + /// Prepares and executes a query using the supplied transaction and returns up to maxRows rows. |
| 330 | + /// |
| 331 | + RowSet queryRowSet( |
| 332 | + Transaction& transaction, std::string_view sql, unsigned maxRows, const StatementOptions& options = {}); |
| 333 | + |
| 334 | + /// |
| 335 | + /// Prepares, binds parameters, and executes a query using the supplied transaction and returns up to maxRows |
| 336 | + /// rows. |
| 337 | + /// |
| 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); |
| 348 | + |
| 349 | + /// |
| 350 | + /// Prepares and executes a query using the supplied transaction and returns the first column of the first row. |
| 351 | + /// |
| 352 | + template <typename T> |
| 353 | + std::optional<T> queryScalar( |
| 354 | + Transaction& transaction, std::string_view sql, const StatementOptions& options = {}); |
| 355 | + |
| 356 | + /// |
| 357 | + /// Prepares, binds parameters, and executes a query using the supplied transaction and returns the first column |
| 358 | + /// of the first row. |
| 359 | + /// |
| 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); |
| 370 | + |
| 371 | + /// |
| 372 | + /// Prepares and executes a query using the supplied transaction and returns the first row mapped as T. |
| 373 | + /// |
| 374 | + template <typename T> |
| 375 | + std::optional<T> queryFirstRowAs( |
| 376 | + Transaction& transaction, std::string_view sql, const StatementOptions& options = {}); |
| 377 | + |
| 378 | + /// |
| 379 | + /// Prepares, binds parameters, and executes a query using the supplied transaction and returns the first row |
| 380 | + /// mapped as T. |
| 381 | + /// |
| 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> |
| 390 | + std::optional<T> queryFirstRowAs( |
| 391 | + Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params); |
| 392 | + |
304 | 393 | private: |
305 | 394 | void disconnectOrDrop(bool drop); |
| 395 | + RowSet queryPreparedRowSet(Statement& statement, Transaction& transaction, unsigned maxRows); |
306 | 396 |
|
307 | 397 | private: |
308 | 398 | Client* client; |
309 | 399 | FbRef<fb::IAttachment> handle; |
310 | 400 | }; |
| 401 | + |
| 402 | + template <typename T> |
| 403 | + std::optional<T> Attachment::queryScalar( |
| 404 | + Transaction& transaction, std::string_view sql, const StatementOptions& options) |
| 405 | + { |
| 406 | + auto rowSet = queryRowSet(transaction, sql, 1u, options); |
| 407 | + |
| 408 | + if (rowSet.getCount() == 0u) |
| 409 | + return std::nullopt; |
| 410 | + |
| 411 | + return rowSet.getRow(0).get<std::optional<T>>(0); |
| 412 | + } |
| 413 | + |
| 414 | + template <typename T> |
| 415 | + std::optional<T> Attachment::queryFirstRowAs( |
| 416 | + Transaction& transaction, std::string_view sql, const StatementOptions& options) |
| 417 | + { |
| 418 | + auto rowSet = queryRowSet(transaction, sql, 1u, options); |
| 419 | + |
| 420 | + if (rowSet.getCount() == 0u) |
| 421 | + return std::nullopt; |
| 422 | + |
| 423 | + return rowSet.getRow(0).get<T>(); |
| 424 | + } |
311 | 425 | } // namespace fbcpp |
312 | 426 |
|
| 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) |
| 433 | + { |
| 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); |
| 466 | + |
| 467 | + if (rowSet.getCount() == 0u) |
| 468 | + return std::nullopt; |
| 469 | + |
| 470 | + return rowSet.getRow(0).template get<std::optional<T>>(0); |
| 471 | + } |
| 472 | + |
| 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> |
| 497 | + std::optional<T> Attachment::queryFirstRowAs( |
| 498 | + Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params) |
| 499 | + { |
| 500 | + auto rowSet = queryRowSet(transaction, sql, 1u, options, params); |
| 501 | + |
| 502 | + if (rowSet.getCount() == 0u) |
| 503 | + return std::nullopt; |
| 504 | + |
| 505 | + return rowSet.getRow(0).template get<T>(); |
| 506 | + } |
| 507 | +} // namespace fbcpp |
313 | 508 |
|
314 | 509 | #endif // FBCPP_ATTACHMENT_H |
0 commit comments