SQLiteConnection.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../../Core/Object.h"
  24. #include "../../Database/DbResult.h"
  25. #include <SQLite/sqlite3.h>
  26. namespace Urho3D
  27. {
  28. /// %Database connection.
  29. class URHO3D_API DbConnection : public Object
  30. {
  31. URHO3D_OBJECT(DbConnection, Object);
  32. public:
  33. /// Construct.
  34. DbConnection(Context* context, const String& connectionString);
  35. /// Destruct.
  36. ~DbConnection() override;
  37. /// Finalize all prepared statements, close all BLOB handles, and finish all sqlite3_backup objects.
  38. void Finalize();
  39. /// Execute an SQL statements immediately. Send E_DBCURSOR event for each row in the resultset when useCursorEvent parameter is set to true.
  40. DbResult Execute(const String& sql, bool useCursorEvent = false);
  41. /// 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.
  42. const String& GetConnectionString() const { return connectionString_; }
  43. /// Return the underlying implementation connection object pointer. It is sqlite* when using SQLite3 or nanodbc::connection* when using ODBC.
  44. const sqlite3* GetConnectionImpl() const { return connectionImpl_; }
  45. /// Return true when the connection object is connected to the associated database.
  46. bool IsConnected() const { return connectionImpl_ != nullptr; }
  47. private:
  48. /// 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.
  49. String connectionString_;
  50. /// The underlying implementation connection object.
  51. sqlite3* connectionImpl_;
  52. };
  53. }