Expose raw SQLite connection and statement handles

This commit is contained in:
Gwendal Roué committed 2015-10-02 09:00:32 +02:00
1 parent e5d6dcea4f
commit ea2c862b22
5 files changed
+67 -11

No files matched your search

+8
View File
@@ -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
+14 -6
View File
@@ -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
+4 -4
View File
@@ -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()
+41
View File
@@ -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
=================
-1
View File
@@ -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/)