|
Lightweight 0.20251202.0
|
Represents a connection to a SQL database. More...
#include <SqlConnection.hpp>
Public Member Functions | |
| LIGHTWEIGHT_API | SqlConnection () |
| Constructs a new SQL connection to the default connection. | |
| LIGHTWEIGHT_API | SqlConnection (std::optional< SqlConnectionString > connectInfo) |
| Constructs a new SQL connection to the given connect informaton. | |
| LIGHTWEIGHT_API | SqlConnection (SqlConnection &&) noexcept |
| LIGHTWEIGHT_API SqlConnection & | operator= (SqlConnection &&) noexcept |
| SqlConnection (SqlConnection const &)=delete | |
| SqlConnection & | operator= (SqlConnection const &)=delete |
| LIGHTWEIGHT_API | ~SqlConnection () noexcept |
| Destructs this SQL connection object,. | |
| uint64_t | ConnectionId () const noexcept |
| Retrieves the connection ID. | |
| LIGHTWEIGHT_API void | Close () noexcept |
| Closes the connection (attempting to put it back into the connect[[ion pool). | |
| LIGHTWEIGHT_API bool | Connect (SqlConnectionDataSource const &info) noexcept |
| LIGHTWEIGHT_API bool | Connect (SqlConnectionString sqlConnectionString) noexcept |
| LIGHTWEIGHT_API SqlErrorInfo | LastError () const |
| Retrieves the last error information with respect to this SQL connection handle. | |
| LIGHTWEIGHT_API std::string | DatabaseName () const |
| Retrieves the name of the database in use. | |
| LIGHTWEIGHT_API std::string | UserName () const |
| Retrieves the name of the user. | |
| LIGHTWEIGHT_API std::string | ServerName () const |
| Retrieves the name of the server. | |
| LIGHTWEIGHT_API std::string | ServerVersion () const |
| Retrieves the reported server version. | |
| SqlServerType | ServerType () const noexcept |
| Retrieves the type of the server. | |
| std::string const & | DriverName () const noexcept |
| Retrieves the name of the driver used for this connection. | |
| SqlQueryFormatter const & | QueryFormatter () const noexcept |
| Retrieves a query formatter suitable for the SQL server being connected. | |
| LIGHTWEIGHT_API SqlQueryBuilder | Query (std::string_view const &table={}) const |
| LIGHTWEIGHT_API SqlQueryBuilder | QueryAs (std::string_view const &table, std::string_view const &tableAlias) const |
| LIGHTWEIGHT_API SqlMigrationQueryBuilder | Migration () const |
| Creates a new migration query builder, compatible the current connection. | |
| LIGHTWEIGHT_API bool | TransactionActive () const noexcept |
| Tests if a transaction is active. | |
| LIGHTWEIGHT_API bool | TransactionsAllowed () const noexcept |
| Tests if transactions are allowed. | |
| LIGHTWEIGHT_API bool | IsAlive () const noexcept |
| Tests if the connection is still active. | |
| LIGHTWEIGHT_API SqlConnectionString const & | ConnectionString () const noexcept |
| Retrieves the connection information. | |
| SQLHDBC | NativeHandle () const noexcept |
| Retrieves the native handle. | |
| LIGHTWEIGHT_API std::chrono::steady_clock::time_point | LastUsed () const noexcept |
| Retrieves the last time the connection was used. | |
| LIGHTWEIGHT_API void | SetLastUsed (std::chrono::steady_clock::time_point lastUsed) noexcept |
| Sets the last time the connection was used. | |
| LIGHTWEIGHT_API void | RequireSuccess (SQLRETURN sqlResult, std::source_location sourceLocation=std::source_location::current()) const |
| Checks the result of an SQL operation, and throws an exception if it is not successful. | |
Static Public Member Functions | |
| static LIGHTWEIGHT_API SqlConnectionString const & | DefaultConnectionString () noexcept |
| Retrieves the default connection information. | |
| static LIGHTWEIGHT_API void | SetDefaultConnectionString (SqlConnectionString const &connectionString) noexcept |
| static LIGHTWEIGHT_API void | SetDefaultDataSource (SqlConnectionDataSource const &dataSource) noexcept |
| Sets the default connection information as SqlConnectionDataSource. | |
| static LIGHTWEIGHT_API void | SetPostConnectedHook (std::function< void(SqlConnection &)> hook) |
| Sets a callback to be called after each connection being established. | |
| static LIGHTWEIGHT_API void | ResetPostConnectedHook () |
| Resets the post connected hook. | |
Represents a connection to a SQL database.
Definition at line 39 of file SqlConnection.hpp.
| LIGHTWEIGHT_API Lightweight::SqlConnection::SqlConnection | ( | ) |
Constructs a new SQL connection to the default connection.
The default connection is set via SetDefaultConnectInfo. In case the default connection is not set, the connection will fail. And in case the connection fails, the last error will be set.
|
explicit |
Constructs a new SQL connection to the given connect informaton.
| connectInfo | The connection information to use. If not provided, no connection will be established. |
|
staticnoexcept |
Sets the default connection information.
| connectionString | The connection information to use. |
|
inlinenoexcept |
Retrieves the connection ID.
This is a unique identifier for the connection, which is useful for debugging purposes. Note, this ID will not change if the connection is moved nor when it is reused via the connection pool.
Definition at line 84 of file SqlConnection.hpp.
|
noexcept |
Connects to the given database with the given username and password.
This method can be called on a connection that has been closed via Close(). If the ODBC handles have been freed, they will be automatically reallocated.
| true | if the connection was successful. |
| false | if the connection failed. Use LastError() to retrieve the error information. |
|
noexcept |
Connects to the given database with the given connection string.
This method can be called on a connection that has been closed via Close(). If the ODBC handles have been freed, they will be automatically reallocated.
| true | if the connection was successful. |
| false | if the connection failed. Use LastError() to retrieve the error information. |
|
inlinenoexcept |
Retrieves the type of the server.
Definition at line 197 of file SqlConnection.hpp.
Referenced by Lightweight::DataMapper::CreateTable(), Lightweight::DataMapper::Query(), Lightweight::DataMapper::Query(), and Lightweight::DataMapper::QuerySingleWithoutRelationAutoLoading().
|
inlinenoexcept |
Retrieves the name of the driver used for this connection.
Definition at line 202 of file SqlConnection.hpp.
|
inlinenoexcept |
Retrieves a query formatter suitable for the SQL server being connected.
Definition at line 207 of file SqlConnection.hpp.
Referenced by Lightweight::SqlStatement::MigrateDirect(), and Lightweight::DataMapper::Query().
| LIGHTWEIGHT_API SqlQueryBuilder Lightweight::SqlConnection::Query | ( | std::string_view const & | table = {} | ) | const |
Creates a new query builder for the given table, compatible with the current connection.
| table | The table to query. If not provided, the query will be a generic query builder. |
Referenced by Lightweight::DataMapper::Delete(), Lightweight::DataMapper::FromTable(), Lightweight::DataMapper::QuerySingleWithoutRelationAutoLoading(), and Lightweight::DataMapper::Update().
| LIGHTWEIGHT_API SqlQueryBuilder Lightweight::SqlConnection::QueryAs | ( | std::string_view const & | table, |
| std::string_view const & | tableAlias | ||
| ) | const |
Creates a new query builder for the given table with an alias, compatible with the current connection.
| table | The table to query. |
| tableAlias | The alias to use for the table. |
|
inlinenoexcept |
Retrieves the native handle.
Definition at line 163 of file SqlConnection.hpp.
Referenced by Lightweight::SqlTransaction::TryCommit().