Skip to content

Commit bc1b3f0

Browse files
authored
Merge pull request stephencelis#553 from stephencelis/sqlcipher
Re-add support for SQLcipher via CocoaPods
2 parents e2633a4 + f8bdf2a commit bc1b3f0

14 files changed

+234
-60
lines changed

‎.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ matrix:
1111
- env: VALIDATOR_SUBSPEC="none"
1212
- env: VALIDATOR_SUBSPEC="standard"
1313
- env: VALIDATOR_SUBSPEC="standalone"
14+
- env: VALIDATOR_SUBSPEC="SQLCipher"
1415
before_install:
1516
- gem update bundler
1617
- gem install xcpretty --no-document

‎CHANGELOG.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
0.11.1 (, [diff][diff-0.11.1])
1+
2+
0.11.1 (06-12-2016), [diff][diff-0.11.1]
23
========================================
34

4-
* Made lastInsertRowid consistent with other SQLite wrappers (#532)
5+
* Integrate SQLCipher via CocoaPods ([#546][], [#553][])
6+
* Made lastInsertRowid consistent with other SQLite wrappers ([#532][])
57
* Fix for ~= operator used with Double ranges
68
* Various documentation updates
79

8-
0.11.0 (19-10-2016)
10+
0.11.0 (10-19-2016)
911
===================
1012

1113
* Swift3 migration ([diff][diff-0.11.0])
1214

1315

14-
[diff-0.11.1]: https://.com/stephencelis/SQLite.swift/compare/0.11.1...0.11.0
16+
[diff-0.11.1]: https://.com/stephencelis/SQLite.swift/compare/0.11.0...0.11.1
1517
[diff-0.11.0]: https://.com/stephencelis/SQLite.swift/compare/0.10.1...0.11.0
18+
19+
[#532]: https://.com/stephencelis/SQLite.swift/issues/532
20+
[#546]: https://.com/stephencelis/SQLite.swift/issues/546
21+
[#553]: https://.com/stephencelis/SQLite.swift/pull/553

‎Documentation/Index.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ install SQLite.swift with Carthage:
114114
4. Run `pod install --repo-update`.
115115
116116
117-
#### Requiring a specific version of SQLite
117+
#### Requiring a specific version of SQLite
118118
119119
If you want to use a more recent version of SQLite than what is provided with the OS you can require the `standalone` subspec:
120120
@@ -131,10 +131,30 @@ By default this will use the most recent version of SQLite without any extras. I
131131

132132
See the [sqlite3 podspec][sqlite3pod] for more details.
133133

134+
#### Using SQLite.swift with SQLCipher
135+
136+
If you want to use [SQLCipher][] with SQLite.swift you can require the `SQLCipher`
137+
subspec in your Podfile:
138+
139+
``` ruby
140+
pod 'SQLite.swift/SQLCipher', '~> 0.11.1'
141+
```
142+
143+
This will automatically add a dependency to the SQLCipher pod as well as extend
144+
`Connection` with methods to change the database key:
145+
146+
``` swift
147+
import SQLite
148+
149+
let db = try Connection("path/to/db.sqlite3")
150+
try db.key("secret")
151+
try db.rekey("another secret")
152+
```
153+
134154
[CocoaPods]: https://cocoapods.org
135155
[CocoaPods Installation]: https://guides.cocoapods.org/using/getting-started.html#getting-started
136156
[sqlite3pod]: https://.com/clemensg/sqlite3pod
137-
157+
[SQLCipher]: https://www.zetetic.net/sqlcipher/
138158

139159
### Manual
140160

‎Documentation/Planning.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
# SQLite.swift Planning
22

3-
This document captures both near term steps (aka Roadmap) and feature requests.
3+
This document captures both near term steps (aka Roadmap) and feature requests.
44
The goal is to add some visibility and guidance for future additions and Pull Requests, as well as to keep the Issues list clear of enhancement requests so that bugs are more visible.
55

66
## Roadmap
77

88
_Lists agreed upon next steps in approximate priority order._
99

10-
* ~~publish to the CocoaPods directory at https://cocoapods.org/, per [#257](https://.com/stephencelis/SQLite.swift/issues/257)~~ _jan 7, 2016_)
11-
* add SQLCipher back into the product as a separate repo, per [#311](https://.com/stephencelis/SQLite.swift/issues/311), _in progress jan 6, 2016_
12-
13-
1410
## Feature Requests
1511

1612
_A gathering point for ideas for new features. In general, the corresponding issue will be closed once it is added here, with the assumption that it will be referred to when it comes time to add the corresponding feature._
1713

1814
### Packaging
1915

20-
* add TV OS support, per [#272](https://.com/stephencelis/SQLite.swift/issues/272) - currently pending SQLiteCipher merge and/or adding ability for user to pick their preferred [SQLCipher](https://.com/sqlcipher/sqlcipher) branch
21-
* linux support via Swift Package Manager, per [#315](https://.com/stephencelis/SQLite.swift/issues/315)
16+
* linux support via Swift Package Manager, per [#315](https://.com/stephencelis/SQLite.swift/issues/315), _in progress_: [#548](https://.com/stephencelis/SQLite.swift/pull/548)
2217

2318
### Features
2419

‎README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ syntax _and_ intent.
2525
- [Full-text search][] support
2626
- [Well-documented][See Documentation]
2727
- Extensively tested
28-
- Companion project has [SQLCipher support](https://.com/stephencelis/SQLiteCipher.swift)
28+
- SQLCipher support via CocoaPods
2929
- Active support at [StackOverflow](http://stackoverflow.com/questions/tagged/sqlite.swift), and [Gitter Chat Room](https://gitter.im/stephencelis/SQLite.swift) (_experimental_)
3030

3131
[Full-text search]: Documentation/Index.md#full-text-search
@@ -224,7 +224,6 @@ file](./LICENSE.txt) for more information.
224224
225225
These projects enhance or use SQLite.swift:
226226
227-
- [SQLiteCipher.swift](https://.com/stephencelis/SQLiteCipher.swift)
228227
- [SQLiteMigrationManager.swift](https://.com/garriguv/SQLiteMigrationManager.swift) (inspired by [FMDBMigrationManager](https://.com/layerhq/FMDBMigrationManager))
229228
230229

‎SQLite.swift.podspec

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Pod::Spec.new do |s|
2626

2727
s.subspec 'standard' do |ss|
2828
ss.source_files = 'SQLite/**/*.{c,h,m,swift}'
29+
ss.exclude_files = 'SQLite/Extensions/Cipher.swift'
2930
ss.private_header_files = 'SQLite/Core/fts3_tokenizer.h'
3031

3132
ss.library = 'sqlite3'
@@ -47,9 +48,23 @@ Pod::Spec.new do |s|
4748

4849
s.subspec 'standalone' do |ss|
4950
ss.source_files = 'SQLite/**/*.{c,h,m,swift}'
51+
ss.exclude_files = 'SQLite/Extensions/Cipher.swift'
5052
ss.private_header_files = 'SQLite/Core/fts3_tokenizer.h'
51-
ss.xcconfig = { 'OTHER_SWIFT_FLAGS' => '$(inherited) -DSQLITE_SWIFT_STANDALONE' }
53+
ss.xcconfig = {
54+
'OTHER_SWIFT_FLAGS' => '$(inherited) -DSQLITE_SWIFT_STANDALONE'
55+
}
56+
57+
ss.dependency 'sqlite3', '>= 3.14.0'
58+
end
59+
60+
s.subspec 'SQLCipher' do |ss|
61+
ss.source_files = 'SQLite/**/*.{c,h,m,swift}'
62+
ss.private_header_files = 'SQLite/Core/fts3_tokenizer.h'
63+
ss.xcconfig = {
64+
'OTHER_SWIFT_FLAGS' => '$(inherited) -DSQLITE_SWIFT_SQLCIPHER',
65+
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SQLITE_HAS_CODEC=1'
66+
}
5267

53-
ss.dependency 'sqlite3'
68+
ss.dependency 'SQLCipher', '>= 3.4.0'
5469
end
5570
end

‎SQLite.xcodeproj/project.pbxproj

+18
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@
4848
03A65E971C6BB3210062603F /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A65E961C6BB3210062603F /* libsqlite3.tbd */; };
4949
19A1717B10CC941ACB5533D6 /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
5050
19A171E6FA242F72A308C594 /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
51+
19A171F12AB8B07F2FD7201A /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A178A39ACA9667A62663CC /* Cipher.swift */; };
5152
19A17254FBA7894891F7297B /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
5253
19A174D78559CD30679BCCCB /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
5354
19A1750CEE9B05267995CF3D /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
55+
19A177CC33F2E6A24AF90B02 /* CipherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17399EA9E61235D5D77BF /* CipherTests.swift */; };
5456
19A178072B371489E6A1E839 /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
57+
19A17835FD5886FDC5A3228F /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A178A39ACA9667A62663CC /* Cipher.swift */; };
58+
19A179A0C45377CB09BB358C /* CipherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17399EA9E61235D5D77BF /* CipherTests.swift */; };
59+
19A179CCF9671E345E5A9811 /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A178A39ACA9667A62663CC /* Cipher.swift */; };
60+
19A179E76EA6207669B60C1B /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A178A39ACA9667A62663CC /* Cipher.swift */; };
61+
19A17C4B951CB054EE48AB1C /* CipherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A17399EA9E61235D5D77BF /* CipherTests.swift */; };
5562
19A17E04C4C0956715C5676A /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
5663
19A17EC0D68BA8C03288ADF7 /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
5764
19A17FB80B94E882050AA908 /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
@@ -189,6 +196,8 @@
189196
03A65E961C6BB3210062603F /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/lib/libsqlite3.tbd; sourceTree = DEVELOPER_DIR; };
190197
19A1721B8984686B9963B45D /* FTS5Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5Tests.swift; sourceTree = "<group>"; };
191198
19A1730E4390C775C25677D1 /* FTS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5.swift; sourceTree = "<group>"; };
199+
19A17399EA9E61235D5D77BF /* CipherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CipherTests.swift; sourceTree = "<group>"; };
200+
19A178A39ACA9667A62663CC /* Cipher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cipher.swift; sourceTree = "<group>"; };
192201
19A1794CC4D7827E997E32A7 /* FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationTests.swift; sourceTree = "<group>"; };
193202
39548A631CA63C740003E3B5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
194203
39548A651CA63C740003E3B5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
@@ -455,6 +464,7 @@
455464
EE247AE41C3F04ED00AE3E12 /* Info.plist */,
456465
19A1721B8984686B9963B45D /* FTS5Tests.swift */,
457466
19A1794CC4D7827E997E32A7 /* FoundationTests.swift */,
467+
19A17399EA9E61235D5D77BF /* CipherTests.swift */,
458468
);
459469
path = SQLiteTests;
460470
sourceTree = "<group>";
@@ -479,6 +489,7 @@
479489
EE247AF51C3F06E900AE3E12 /* FTS4.swift */,
480490
EE247AF61C3F06E900AE3E12 /* RTree.swift */,
481491
19A1730E4390C775C25677D1 /* FTS5.swift */,
492+
19A178A39ACA9667A62663CC /* Cipher.swift */,
482493
);
483494
path = Extensions;
484495
sourceTree = "<group>";
@@ -845,6 +856,7 @@
845856
03A65E771C6BB2E60062603F /* Connection.swift in Sources */,
846857
03A65E7E1C6BB2FB0062603F /* AggregateFunctions.swift in Sources */,
847858
19A17EC0D68BA8C03288ADF7 /* FTS5.swift in Sources */,
859+
19A179E76EA6207669B60C1B /* Cipher.swift in Sources */,
848860
);
849861
runOnlyForDeploymentPostprocessing = 0;
850862
};
@@ -869,6 +881,7 @@
869881
03A65E951C6BB3030062603F /* TestHelpers.swift in Sources */,
870882
19A17254FBA7894891F7297B /* FTS5Tests.swift in Sources */,
871883
19A17E04C4C0956715C5676A /* FoundationTests.swift in Sources */,
884+
19A179A0C45377CB09BB358C /* CipherTests.swift in Sources */,
872885
);
873886
runOnlyForDeploymentPostprocessing = 0;
874887
};
@@ -895,6 +908,7 @@
895908
3D67B3F61DB246D100A4F4C6 /* Setter.swift in Sources */,
896909
3D67B3E71DB246BA00A4F4C6 /* Blob.swift in Sources */,
897910
3D67B3E81DB246BA00A4F4C6 /* Connection.swift in Sources */,
911+
19A179CCF9671E345E5A9811 /* Cipher.swift in Sources */,
898912
);
899913
runOnlyForDeploymentPostprocessing = 0;
900914
};
@@ -921,6 +935,7 @@
921935
EE247B071C3F06E900AE3E12 /* Statement.swift in Sources */,
922936
EE247B0D1C3F06E900AE3E12 /* AggregateFunctions.swift in Sources */,
923937
19A1717B10CC941ACB5533D6 /* FTS5.swift in Sources */,
938+
19A171F12AB8B07F2FD7201A /* Cipher.swift in Sources */,
924939
);
925940
runOnlyForDeploymentPostprocessing = 0;
926941
};
@@ -945,6 +960,7 @@
945960
EE247B251C3F137700AE3E12 /* ConnectionTests.swift in Sources */,
946961
19A171E6FA242F72A308C594 /* FTS5Tests.swift in Sources */,
947962
19A17FB80B94E882050AA908 /* FoundationTests.swift in Sources */,
963+
19A177CC33F2E6A24AF90B02 /* CipherTests.swift in Sources */,
948964
);
949965
runOnlyForDeploymentPostprocessing = 0;
950966
};
@@ -971,6 +987,7 @@
971987
EE247B661C3F3FEC00AE3E12 /* Connection.swift in Sources */,
972988
EE247B6D1C3F3FEC00AE3E12 /* AggregateFunctions.swift in Sources */,
973989
19A1750CEE9B05267995CF3D /* FTS5.swift in Sources */,
990+
19A17835FD5886FDC5A3228F /* Cipher.swift in Sources */,
974991
);
975992
runOnlyForDeploymentPostprocessing = 0;
976993
};
@@ -995,6 +1012,7 @@
9951012
EE247B5B1C3F3FC700AE3E12 /* QueryTests.swift in Sources */,
9961013
19A174D78559CD30679BCCCB /* FTS5Tests.swift in Sources */,
9971014
19A178072B371489E6A1E839 /* FoundationTests.swift in Sources */,
1015+
19A17C4B951CB054EE48AB1C /* CipherTests.swift in Sources */,
9981016
);
9991017
runOnlyForDeploymentPostprocessing = 0;
10001018
};

‎SQLite/Core/Connection.swift

+48-35
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import Foundation.NSUUID
2626
import Dis
2727
#if SQLITE_SWIFT_STANDALONE
2828
import sqlite3
29+
#elseif SQLITE_SWIFT_SQLCIPHER
30+
import SQLCipher
2931
#elseif COCOAPODS
3032
import CSQLite
3133
#endif
@@ -411,11 +413,15 @@ public final class Connection {
411413
///
412414
/// db.trace { SQL in print(SQL) }
413415
public func trace(_ callback: ((String) -> Void)?) {
414-
if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
415-
trace_v2(callback)
416-
} else {
416+
#if SQLITE_SWIFT_SQLCIPHER
417417
trace_v1(callback)
418-
}
418+
#else
419+
if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
420+
trace_v2(callback)
421+
} else {
422+
trace_v1(callback)
423+
}
424+
#endif
419425
}
420426

421427
fileprivate func trace_v1(_ callback: ((String) -> Void)?) {
@@ -439,37 +445,8 @@ public final class Connection {
439445
trace = box
440446
}
441447

442-
@available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *)
443-
fileprivate func trace_v2(_ callback: ((String) -> Void)?) {
444-
guard let callback = callback else {
445-
// If the X callback is NULL or if the M mask is zero, then tracing is disabled.
446-
sqlite3_trace_v2(handle, 0 /* mask */, nil /* xCallback */, nil /* pCtx */)
447-
trace = nil
448-
return
449-
}
450448

451-
let box: Trace = { (pointer: UnsafeRawPointer) in
452-
callback(String(cString: pointer.assumingMemoryBound(to: UInt8.self)))
453-
}
454-
sqlite3_trace_v2(handle,
455-
UInt32(SQLITE_TRACE_STMT) /* mask */,
456-
{
457-
// A trace callback is invoked with four arguments: callback(T,C,P,X).
458-
// The T argument is one of the SQLITE_TRACE constants to indicate why the
459-
// callback was invoked. The C argument is a copy of the context pointer.
460-
// The P and X arguments are pointers whose meanings depend on T.
461-
(T: UInt32, C: UnsafeMutableRawPointer?, P: UnsafeMutableRawPointer?, X: UnsafeMutableRawPointer?) in
462-
if let P = P,
463-
let expandedSQL = sqlite3_expanded_sql(OpaquePointer(P)) {
464-
unsafeBitCast(C, to: Trace.self)(expandedSQL)
465-
sqlite3_free(expandedSQL)
466-
}
467-
return Int32(0) // currently ignored
468-
},
469-
unsafeBitCast(box, to: UnsafeMutableRawPointer.self) /* pCtx */
470-
)
471-
trace = box
472-
}
449+
473450

474451
fileprivate typealias Trace = @convention(block) (UnsafeRawPointer) -> Void
475452
fileprivate var trace: Trace?
@@ -674,7 +651,7 @@ public final class Connection {
674651
return success!
675652
}
676653

677-
@discardableResult public func check(_ resultCode: Int32, statement: Statement? = nil) throws -> Int32 {
654+
@discardableResult func check(_ resultCode: Int32, statement: Statement? = nil) throws -> Int32 {
678655
guard let error = Result(errorCode: resultCode, connection: self, statement: statement) else {
679656
return resultCode
680657
}
@@ -740,3 +717,39 @@ extension Result : CustomStringConvertible {
740717
}
741718

742719
}
720+
721+
#if !SQLITE_SWIFT_SQLCIPHER
722+
@available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *)
723+
extension Connection {
724+
fileprivate func trace_v2(_ callback: ((String) -> Void)?) {
725+
guard let callback = callback else {
726+
// If the X callback is NULL or if the M mask is zero, then tracing is disabled.
727+
sqlite3_trace_v2(handle, 0 /* mask */, nil /* xCallback */, nil /* pCtx */)
728+
trace = nil
729+
return
730+
}
731+
732+
let box: Trace = { (pointer: UnsafeRawPointer) in
733+
callback(String(cString: pointer.assumingMemoryBound(to: UInt8.self)))
734+
}
735+
sqlite3_trace_v2(handle,
736+
UInt32(SQLITE_TRACE_STMT) /* mask */,
737+
{
738+
// A trace callback is invoked with four arguments: callback(T,C,P,X).
739+
// The T argument is one of the SQLITE_TRACE constants to indicate why the
740+
// callback was invoked. The C argument is a copy of the context pointer.
741+
// The P and X arguments are pointers whose meanings depend on T.
742+
(T: UInt32, C: UnsafeMutableRawPointer?, P: UnsafeMutableRawPointer?, X: UnsafeMutableRawPointer?) in
743+
if let P = P,
744+
let expandedSQL = sqlite3_expanded_sql(OpaquePointer(P)) {
745+
unsafeBitCast(C, to: Trace.self)(expandedSQL)
746+
sqlite3_free(expandedSQL)
747+
}
748+
return Int32(0) // currently ignored
749+
},
750+
unsafeBitCast(box, to: UnsafeMutableRawPointer.self) /* pCtx */
751+
)
752+
trace = box
753+
}
754+
}
755+
#endif

‎SQLite/Core/Statement.swift

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#if SQLITE_SWIFT_STANDALONE
2626
import sqlite3
27+
#elseif SQLITE_SWIFT_SQLCIPHER
28+
import SQLCipher
2729
#elseif COCOAPODS
2830
import CSQLite
2931
#endif

0 commit comments

Comments
 (0)