From ea2c862b22f14007ea0e34bfa867bb0df638f813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 2 Oct 2015 09:00:32 +0200 Subject: [PATCH] Expose raw SQLite connection and statement handles --- CHANGELOG.md | 8 ++++++++ GRDB/Core/Database.swift | 20 +++++++++++++------ GRDB/Core/Statement.swift | 8 ++++---- README.md | 41 +++++++++++++++++++++++++++++++++++++++ TODO.md | 1 - 5 files changed, 67 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5f6fe5f4..2523442cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ Release Notes ============= +## Next Release + +**New** + +- `Database.sqliteConnection`: the raw SQLite connection, suitable for SQLite C API. +- `Statement.sqliteStatement`: the raw SQLite statement, suitable for SQLite C API. + + ## 0.21.0 Released october 1, 2015 diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index e82e91fa1..6affeb509 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -1,5 +1,5 @@ -/// A nicer name than COpaquePointer for SQLite connection handle -typealias SQLiteConnection = COpaquePointer +/// A raw SQLite connection, suitable for the SQLite C API. +public typealias SQLiteConnection = COpaquePointer /** A Database connection. @@ -491,13 +491,21 @@ public final class Database { // ========================================================================= - // MARK: - Initialization + // MARK: - Raw SQLite connetion + + /// The raw SQLite connection, suitable for the SQLite C API. + public let sqliteConnection: SQLiteConnection + + + // ========================================================================= + // MARK: - Configuration /// The database configuration - let configuration: Configuration + public let configuration: Configuration - /// The SQLite connection handle - let sqliteConnection: SQLiteConnection + + // ========================================================================= + // MARK: - Initialization /// The queue from which the database can be used. See assertValidQueue(). /// Design note: this is not very clean. A delegation pattern may be a diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 2bd050200..8794cc5e7 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -1,4 +1,4 @@ -/// The pointer to a raw SQLite statement. +/// A raw SQLite statement, suitable for the SQLite C API. public typealias SQLiteStatement = COpaquePointer private let SQLITE_TRANSIENT = unsafeBitCast(COpaquePointer(bitPattern: -1), sqlite3_destructor_type.self) @@ -11,6 +11,9 @@ SelectStatement that fetches rows. */ public class Statement { + /// The raw SQLite statement, suitable for the SQLite C API. + public let sqliteStatement: SQLiteStatement + /// The SQL query public var sql: String @@ -30,9 +33,6 @@ public class Statement { /// The database let database: Database - /// The SQLite statement handle - let sqliteStatement: SQLiteStatement - init(database: Database, sql: String) throws { database.assertValidQueue() diff --git a/README.md b/README.md index 4e326b1d5..adf125543 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ To fiddle with the library, open the `GRDB.xcworkspace` workspace: it contains a - [Error Handling](#error-handling) - [Transactions](#transactions) - [Concurrency](#concurrency) +- [Raw SQLite Pointers](#raw-sqlite-pointers) **[Application Tools](#application-tools)** @@ -891,6 +892,46 @@ By default, GRDB opens database in the **default journal mode**, uses **IMMEDIAT See [Configuration](GRDB/Core/Configuration.swift) type and [DatabaseQueue.inTransaction()](GRDB/Core/DatabaseQueue.swift) method for more precise handling of transactions and eventual SQLITE_BUSY errors. +## Raw SQLite Pointers + +Not all SQLite APIs are exposed in GRDB. + +The `Database.sqliteConnection` and `Statement.sqliteStatement` properties provide the raw pointers that are suitable for [SQLite C API](https://www.sqlite.org/c3ref/funclist.html): + +```swift +dbQueue.inDatabase { db in + let sqliteConnection = db.sqliteConnection + sqlite3_db_config(sqliteConnection, ...) + + let statement = db.selectStatement("SELECT ...") + let sqliteStatement = statement.sqliteStatement + sqlite3_step(sqliteStatement) +} +``` + +> :point_up: **Notes** +> +> - Those pointers are owned by GRDB: don't close connections or finalize statements created by GRDB. +> - SQLite connections are opened in the [Multi-thread mode](https://www.sqlite.org/threadsafe.html) mode, which (oddly) means that **they are not thread-safe**. Make sure you touch raw databases and statements inside the database queues. + +Before jumping in the low-level wagon, here is a reminder of SQLite APIs supported by GRDB: + +- Connections & statements, obviously. +- Errors (pervasive) + - [sqlite3_errmsg](https://www.sqlite.org/c3ref/errcode.html) +- Inserted RowIDs (as the result of Database.execute()). + - [sqlite3_last_insert_rowid](https://www.sqlite.org/c3ref/last_insert_rowid.html) +- Changes count (as the result of Database.execute()). + - [sqlite3_changes](https://www.sqlite.org/c3ref/changes.html) +- Busy mode (see [Concurrency](#concurrency)). + - [sqlite3_busy_handler](https://www.sqlite.org/c3ref/busy_handler.html) + - [sqlite3_busy_timeout](https://www.sqlite.org/c3ref/busy_timeout.html) +- Update, commit and rollback hooks (see [Database Changes Observation](#database-changes-observation)): + - [sqlite3_update_hook](https://www.sqlite.org/c3ref/update_hook.html) + - [sqlite3_commit_hook](https://www.sqlite.org/c3ref/commit_hook.html) + - [sqlite3_rollback_hook](https://www.sqlite.org/c3ref/commit_hook.html) + + Application Tools ================= diff --git a/TODO.md b/TODO.md index d8d402285..49aac746d 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,6 @@ - [ ] #2: this commit may be how stephencelis fixed it: https://github.com/stephencelis/SQLite.swift/commit/8f64e357c3a6668c5f011c91ba33be3e8d4b88d0 - [ ] Use @warn_unused_result where applicable - [ ] Turn DatabaseTableMapping into a private protocol -- [ ] Expose raw SQLite connection and statement handles. Without them the user won't be able to perform operations that don't have a matching GRDB API. - [ ] Ship TableChangeObserver with the library. - [ ] Study VACUUM (https://blogs.gnome.org/jnelson/) - [ ] Study full text search (related: https://blogs.gnome.org/jnelson/)