ODBCConnection.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../../Core/Object.h"
  5. #include "../../Database/DbResult.h"
  6. #include <nanodbc/nanodbc.h>
  7. namespace Urho3D
  8. {
  9. /// %Database connection.
  10. class URHO3D_API DbConnection : public Object
  11. {
  12. URHO3D_OBJECT(DbConnection, Object);
  13. public:
  14. /// Construct.
  15. DbConnection(Context* context, const String& connectionString);
  16. /// Destruct.
  17. virtual ~DbConnection() override;
  18. /// Finalize all prepared statements, close all BLOB handles, and finish all sqlite3_backup objects.
  19. void Finalize();
  20. /// Execute an SQL statements immediately. Send E_DBCURSOR event for each row in the resultset when useCursorEvent parameter is set to true.
  21. DbResult Execute(const String& sql, bool useCursorEvent = false);
  22. /// Return database connection string. The connection string for SQLite3 is using the URI format described in https://www.sqlite.org/uri.html, while the connection string for ODBC is using DSN format as per ODBC standard.
  23. const String& GetConnectionString() const { return connectionString_; }
  24. /// Return the underlying implementation connection object pointer. It is sqlite* when using SQLite3 or nanodbc::connection* when using ODBC.
  25. const nanodbc::connection* GetConnectionImpl() const { return &connectionImpl_; }
  26. /// Return true when the connection object is connected to the associated database.
  27. bool IsConnected() const { return connectionImpl_.connected(); }
  28. private:
  29. /// Internal helper method to handle runtime exception by logging it to stderr stream.
  30. void HandleRuntimeError(const char* message, const char* cause);
  31. /// The connection string for SQLite3 is using the URI format described in https://www.sqlite.org/uri.html, while the connection string for ODBC is using DSN format as per ODBC standard.
  32. String connectionString_;
  33. /// The underlying implementation connection object.
  34. nanodbc::connection connectionImpl_;
  35. };
  36. }