Refactor entire codebase and rewrite documentation

This commit is contained in:
2025-10-10 18:06:34 +03:00
parent b4e9755c15
commit 8e471f2b9f
74 changed files with 3405 additions and 4149 deletions

View File

@@ -4,29 +4,14 @@ import DataLiteC
extension Function {
/// A collection representing the arguments passed to an SQLite function.
///
/// This structure provides a collection interface to access the arguments passed to an SQLite
/// function. Each argument is represented by an instance of `SQLiteValue`, which can hold
/// various types of SQLite values such as integers, floats, text, blobs, or nulls.
///
/// - Important: This collection does not perform bounds checking when accessing arguments via
/// subscripts. It is the responsibility of the caller to ensure that the provided index is within the bounds
/// of the argument list.
///
/// - Important: The indices of this collection start from 0 and go up to, but not including, the
/// count of arguments.
public struct Arguments: Collection {
/// Alias for the type representing an element in `Arguments`, which is a `SQLiteValue`.
public typealias Element = SQLiteRawValue
/// Alias for the index type used in `Arguments`.
public typealias Index = Int
/// The `Arguments` structure provides a type-safe interface for accessing the arguments
/// received by a user-defined SQLite function. Each element of the collection is represented
/// by a ``SQLiteValue`` instance that can store integers, floating-point numbers, text, blobs,
/// or nulls.
public struct Arguments: ArgumentsProtocol {
// MARK: - Properties
/// The number of arguments passed to the SQLite function.
private let argc: Int32
/// A pointer to an array of `OpaquePointer?` representing SQLite values.
private let argv: UnsafeMutablePointer<OpaquePointer?>?
/// The number of arguments passed to the SQLite function.
@@ -34,28 +19,23 @@ extension Function {
Int(argc)
}
/// A Boolean value indicating whether there are no arguments passed to the SQLite function.
/// A Boolean value indicating whether there are no arguments.
public var isEmpty: Bool {
count == 0
}
/// The starting index of the arguments passed to the SQLite function.
/// The index of the first argument.
public var startIndex: Index {
0
}
/// The ending index of the arguments passed to the SQLite function.
/// The index immediately after the last valid argument.
public var endIndex: Index {
count
}
// MARK: - Inits
/// Initializes the argument list with the provided count and pointer to SQLite values.
///
/// - Parameters:
/// - argc: The number of arguments.
/// - argv: A pointer to an array of `OpaquePointer?` representing SQLite values.
init(argc: Int32, argv: UnsafeMutablePointer<OpaquePointer?>?) {
self.argc = argc
self.argv = argv
@@ -63,18 +43,17 @@ extension Function {
// MARK: - Subscripts
/// Accesses the SQLite value at the specified index.
/// Returns the SQLite value at the specified index.
///
/// - Parameter index: The index of the SQLite value to access.
/// Retrieves the raw value from the SQLite function arguments and returns it as an
/// instance of ``SQLiteValue``.
///
/// - Parameter index: The index of the argument to retrieve.
/// - Returns: The SQLite value at the specified index.
///
/// This subscript allows accessing the SQLite value at a specific index within the argument list.
/// If the index is out of bounds, a fatal error is triggered.
///
/// - Complexity: O(1)
public subscript(index: Index) -> Element {
guard count > index else {
fatalError("\(index) out of bounds")
guard index < count else {
fatalError("Index \(index) out of bounds")
}
let arg = argv.unsafelyUnwrapped[index]
switch sqlite3_value_type(arg) {
@@ -86,31 +65,12 @@ extension Function {
}
}
/// Accesses the SQLite value at the specified index and converts it to a type conforming to
/// `SQLiteConvertible`.
///
/// - Parameter index: The index of the SQLite value to access.
/// - Returns: The SQLite value at the specified index, converted to the specified type,
/// or `nil` if conversion fails.
///
/// This subscript allows accessing the SQLite value at a specific index within the argument
/// list and converting it to a type conforming to `SQLiteConvertible`.
///
/// - Complexity: O(1)
public subscript<T: SQLiteRawRepresentable>(index: Index) -> T? {
T(self[index])
}
// MARK: - Methods
/// Returns the index after the specified index.
///
/// - Parameter i: The index.
/// - Returns: The index immediately after the specified index.
///
/// This method is used to advance to the next index in the argument list when iterating over
/// its elements.
/// Returns the index that follows the specified index.
///
/// - Parameter i: The current index.
/// - Returns: The index immediately after the specified one.
/// - Complexity: O(1)
public func index(after i: Index) -> Index {
i + 1
@@ -120,39 +80,10 @@ extension Function {
// MARK: - Functions
/// Retrieves the textual data from an SQLite value.
///
/// - Parameter value: An opaque pointer to an SQLite value.
/// - Returns: A `String` representing the text value extracted from the SQLite value.
///
/// This function retrieves the textual data from an SQLite value and converts it into a Swift `String`.
///
/// - Note: The returned string may contain UTF-8 encoded text.
/// - Note: Ensure the provided `OpaquePointer` is valid and points to a valid SQLite value.
/// Passing a null pointer will result in undefined behavior.
///
/// - Important: This function does not perform error checking for null pointers or invalid SQLite values.
/// It is the responsibility of the caller to ensure the validity of the provided pointer.
///
/// - SeeAlso: [SQLite Documentation](https://www.sqlite.org/index.html)
private func sqlite3_value_text(_ value: OpaquePointer!) -> String {
String(cString: DataLiteC.sqlite3_value_text(value))
}
/// Retrieves binary data from an SQLite value.
///
/// - Parameter value: An opaque pointer to an SQLite value.
/// - Returns: A `Data` object representing the binary data extracted from the SQLite value.
///
/// This function retrieves binary data from an SQLite value and converts it into a Swift `Data` object.
///
/// - Note: Ensure the provided `OpaquePointer` is valid and points to a valid SQLite value.
/// Passing a null pointer will result in undefined behavior.
///
/// - Important: This function does not perform error checking for null pointers or invalid SQLite values.
/// It is the responsibility of the caller to ensure the validity of the provided pointer.
///
/// - SeeAlso: [SQLite Documentation](https://www.sqlite.org/index.html)
private func sqlite3_value_blob(_ value: OpaquePointer!) -> Data {
Data(
bytes: sqlite3_value_blob(value),