|
wxSQLite3
5.0.1
|
wxSQLite3 provides a number of convenience features that make working with SQLite result sets and prepared statements more natural in modern C++ code. These features complement the traditional cursor-based interface and the existing type-specific methods.
In addition to the existing type-specific methods for retrieving values from a result set, wxSQLite3 provides a template-based Get<T>() method. The method returns a std::optional<T>, making the distinction between an SQL NULL value and a value of the requested C++ type explicit.
For example:
If the corresponding SQLite value is NULL, the returned std::optional is empty. Otherwise, it contains the value converted to the requested C++ type.
The template-based interface is implemented in terms of the existing type-specific result-set methods. It therefore provides a convenient and type-oriented interface without introducing a separate value-conversion mechanism.
Prepared-statement parameters can likewise be bound using the template-based Bind<T>() interface. The method accepts a std::optional<T>, allowing an SQL NULL value to be represented naturally by an empty std::optional.
For example:
An engaged std::optional binds its contained value, while an empty std::optional binds an SQL NULL.
As with Get<T>(), the template-based binding methods use the existing type-specific binding methods internally.
This makes std::optional a convenient way to handle nullable database values without having to treat SQL NULL as a special case in application code.
For situations where several values need to be bound to a prepared statement or retrieved from a result set, wxSQLite3 also provides BindTuple() and GetTuple().
The first variant uses consecutive parameter or column indices. The values are therefore associated with indices starting at the specified first index.
For example:
retrieves three values from consecutive columns.
Similarly, values can be bound to consecutive parameters:
The tuple-based methods use the same type-aware conversion and std::optional handling as the individual Get<T>() and Bind() methods. Consequently, nullable database values can be represented directly in the resulting or supplied tuple.
A second variant of GetTuple() and BindTuple() accepts an array of indices. This allows the values to be associated with arbitrary, non-consecutive columns or parameters.
For example:
This is useful when the values of interest are distributed across a result set and their positions do not form a consecutive sequence.
The corresponding binding operation can use an index array in the same way:
The tuple-based interface is particularly useful when several database values correspond directly to members or values of a C++ data structure and should be handled as a group.
Traditionally, a wxSQLite3ResultSet is processed using its cursor-oriented interface:
For applications using modern C++ features, a result set can also be processed with a range-based for loop:
This provides a concise and familiar way to iterate over all rows of a result set.
The iterator interface can also be used directly when more control over the iteration is required:
The iterator and range-based interfaces are alternatives to the traditional NextRow() loop. They do not change the underlying SQLite result-set processing model; they provide an additional interface that integrates with the standard C++ iteration mechanisms.
The individual template methods, tuple operations, and range-based result-set processing can be combined to write concise and type-oriented database code.
For example:
The tuple returned by GetTuple() contains std::optional values, so SQL NULL values remain distinguishable from actual C++ values.
These additions are intended to complement rather than replace the established wxSQLite3 API. Existing applications can continue to use the traditional cursor-based and type-specific methods without modification.