Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ATTACH/DETACH (#30) #1142

Merged
merged 9 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
0.13.3 (25-01-2022), [diff][diff-0.13.3]
0.14.0 (tbd), [diff][diff-0.14.0]
========================================

* Support `ATTACH`/`DETACH` ([#30][], [#1142][])
* Support `WITH` clause ([#1139][])
* Add `Value` conformance for `NSURL` ([#1110][], [#1141][])
* Add decoding for `UUID` ([#1137][])
* Fix `insertMany([Encodable])` ([#1130][], [#1138][])
* Fix incorrect spelling of `remove_diacritics` ([#1128][])
* Fix project build order ([#1131][])
* Performance improvements ([#1109][], [#1115][], [#1132][])

0.13.3 (27-03-2022), [diff][diff-0.13.3]
========================================

* UUID Fix ([#1112][])
Expand Down Expand Up @@ -110,7 +122,9 @@
[diff-0.13.1]: https://github.com/stephencelis/SQLite.swift/compare/0.13.0...0.13.1
[diff-0.13.2]: https://github.com/stephencelis/SQLite.swift/compare/0.13.1...0.13.2
[diff-0.13.3]: https://github.com/stephencelis/SQLite.swift/compare/0.13.2...0.13.3
[diff-0.14.0]: https://github.com/stephencelis/SQLite.swift/compare/0.13.3...0.14.0

[#30]: https://github.com/stephencelis/SQLite.swift/issues/30
[#142]: https://github.com/stephencelis/SQLite.swift/issues/142
[#315]: https://github.com/stephencelis/SQLite.swift/issues/315
[#426]: https://github.com/stephencelis/SQLite.swift/pull/426
Expand Down Expand Up @@ -150,6 +164,18 @@
[#1095]: https://github.com/stephencelis/SQLite.swift/pull/1095
[#1100]: https://github.com/stephencelis/SQLite.swift/pull/1100
[#1105]: https://github.com/stephencelis/SQLite.swift/pull/1105
[#1109]: https://github.com/stephencelis/SQLite.swift/issues/1109
[#1110]: https://github.com/stephencelis/SQLite.swift/pull/1110
[#1112]: https://github.com/stephencelis/SQLite.swift/pull/1112
[#1115]: https://github.com/stephencelis/SQLite.swift/pull/1115
[#1119]: https://github.com/stephencelis/SQLite.swift/pull/1119
[#1121]: https://github.com/stephencelis/SQLite.swift/pull/1121
[#1128]: https://github.com/stephencelis/SQLite.swift/issues/1128
[#1130]: https://github.com/stephencelis/SQLite.swift/issues/1130
[#1131]: https://github.com/stephencelis/SQLite.swift/pull/1131
[#1132]: https://github.com/stephencelis/SQLite.swift/pull/1132
[#1137]: https://github.com/stephencelis/SQLite.swift/pull/1137
[#1138]: https://github.com/stephencelis/SQLite.swift/pull/1138
[#1139]: https://github.com/stephencelis/SQLite.swift/pull/1139
[#1141]: https://github.com/stephencelis/SQLite.swift/pull/1141
[#1142]: https://github.com/stephencelis/SQLite.swift/pull/1142
39 changes: 36 additions & 3 deletions Documentation/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Read-Write Databases](#read-write-databases)
- [Read-Only Databases](#read-only-databases)
- [In-Memory Databases](#in-memory-databases)
- [URI parameters](#uri-parameters)
- [Thread-Safety](#thread-safety)
- [Building Type-Safe SQL](#building-type-safe-sql)
- [Expressions](#expressions)
Expand Down Expand Up @@ -61,9 +62,9 @@
- [Custom Collations](#custom-collations)
- [Full-text Search](#full-text-search)
- [Executing Arbitrary SQL](#executing-arbitrary-sql)
- [Attaching and detaching databases](#attaching-and-detaching-databases)
- [Logging](#logging)


[↩]: #sqliteswift-documentation


Expand Down Expand Up @@ -334,6 +335,16 @@ let db = try Connection(.temporary)
In-memory databases are automatically deleted when the database connection is
closed.

#### URI parameters

We can pass `.uri` to the `Connection` initializer to control more aspects of
the database connection with the help of `URIQueryParameter`s:

```swift
let db = try Connection(.uri("file.sqlite", parameters: [.cache(.private), .noLock(true)]))
```

See [Uniform Resource Identifiers](https://www.sqlite.org/uri.html#recognized_query_parameters) for more details.

#### Thread-Safety

Expand Down Expand Up @@ -372,6 +383,9 @@ to their [SQLite counterparts](https://www.sqlite.org/datatype3.html).
| `String` | `TEXT` |
| `nil` | `NULL` |
| `SQLite.Blob`† | `BLOB` |
| `URL` | `TEXT` |
| `UUID` | `TEXT` |
| `Date` | `TEXT` |

> *While `Int64` is the basic, raw type (to preserve 64-bit integers on
> 32-bit platforms), `Int` and `Bool` work transparently.
Expand Down Expand Up @@ -1089,8 +1103,8 @@ users.limit(5, offset: 5)

#### Recursive and Hierarchical Queries

We can perform a recursive or hierarchical query using a [query's](#queries) `with`
function.
We can perform a recursive or hierarchical query using a [query's](#queries)
[`WITH`](https://sqlite.org/lang_with.html) function.

```swift
// Get the management chain for the manager with id == 8
Expand Down Expand Up @@ -2070,6 +2084,25 @@ let backup = try db.backup(usingConnection: target)
try backup.step()
```

## Attaching and detaching databases

We can [ATTACH](https://www3.sqlite.org/lang_attach.html) and [DETACH](https://www3.sqlite.org/lang_detach.html)
databases to an existing connection:

```swift
let db = try Connection("db.sqlite")

try db.attach(.uri("external.sqlite", parameters: [.mode(.readOnly)]), as: "external")
// ATTACH DATABASE 'file:external.sqlite?mode=ro' AS 'external'

let table = Table("table", database: "external")
let count = try db.scalar(table.count)
// SELECT count(*) FROM 'external.table'

try db.detach("external")
// DETACH DATABASE 'external'
```

## Logging

We can log SQL using the database’s `trace` function.
Expand Down
2 changes: 0 additions & 2 deletions Documentation/Planning.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ be referred to when it comes time to add the corresponding feature._

### Features

* encapsulate ATTACH DATABASE / DETACH DATABASE as methods, per
[#30](https://github.com/stephencelis/SQLite.swift/issues/30)
* provide separate threads for update vs read, so updates don't block reads,
per [#236](https://github.com/stephencelis/SQLite.swift/issues/236)
* expose triggers, per
Expand Down
18 changes: 11 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ let package = Package(
"Info.plist"
],
resources: [
.copy("fixtures/encrypted-3.x.sqlite"),
.copy("fixtures/encrypted-4.x.sqlite")
.copy("Resources")
]
)
]
Expand All @@ -55,10 +54,15 @@ package.targets = [
dependencies: [.product(name: "CSQLite", package: "CSQLite")],
exclude: ["Extensions/FTS4.swift", "Extensions/FTS5.swift"]
),
.testTarget(name: "SQLiteTests", dependencies: ["SQLite"], path: "Tests/SQLiteTests", exclude: [
"FTSIntegrationTests.swift",
"FTS4Tests.swift",
"FTS5Tests.swift"
])
.testTarget(
name: "SQLiteTests",
dependencies: ["SQLite"],
path: "Tests/SQLiteTests", exclude: [
"FTSIntegrationTests.swift",
"FTS4Tests.swift",
"FTS5Tests.swift"
],
resources: [ .copy("Resources") ]
)
]
#endif
6 changes: 3 additions & 3 deletions SQLite.swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Pod::Spec.new do |s|
ss.library = 'sqlite3'

ss.test_spec 'tests' do |test_spec|
test_spec.resources = 'Tests/SQLiteTests/fixtures/*'
test_spec.resources = 'Tests/SQLiteTests/Resources/*'
test_spec.source_files = 'Tests/SQLiteTests/*.swift'
test_spec.ios.deployment_target = ios_deployment_target
test_spec.tvos.deployment_target = tvos_deployment_target
Expand All @@ -55,7 +55,7 @@ Pod::Spec.new do |s|
ss.dependency 'sqlite3'

ss.test_spec 'tests' do |test_spec|
test_spec.resources = 'Tests/SQLiteTests/fixtures/*'
test_spec.resources = 'Tests/SQLiteTests/Resources/*'
test_spec.source_files = 'Tests/SQLiteTests/*.swift'
test_spec.ios.deployment_target = ios_deployment_target
test_spec.tvos.deployment_target = tvos_deployment_target
Expand All @@ -73,7 +73,7 @@ Pod::Spec.new do |s|
ss.dependency 'SQLCipher', '>= 4.0.0'

ss.test_spec 'tests' do |test_spec|
test_spec.resources = 'Tests/SQLiteTests/fixtures/*'
test_spec.resources = 'Tests/SQLiteTests/Resources/*'
test_spec.source_files = 'Tests/SQLiteTests/*.swift'
test_spec.ios.deployment_target = ios_deployment_target
test_spec.tvos.deployment_target = tvos_deployment_target
Expand Down
Loading