Skip to content
Open
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
4 changes: 0 additions & 4 deletions Examples/PackageTraits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ let package = Package(
The `SQLMiddleware` module has just a single top-level function `middlewareDatabaseType()` that will return either "SQLCipher <version>" or "SQLite3 <version>" depending on whether it was included with the "SQLCipher" trait.

```swift
#if canImport(SQLCipher)
import SQLCipher
#else
import SQLite3
#endif

public func databaseVersion() -> String? {
#if canImport(SQLCipher)
Expand Down
351 changes: 305 additions & 46 deletions Package.swift

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions Package@swift-5.9.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// swift-tools-version:5.9
import PackageDescription

let package = Package(
name: "swift-sqlcipher",
platforms: [
.iOS(.v13),
.macOS(.v10_13),
.watchOS(.v4),
.tvOS(.v12),
.visionOS(.v1)
],
products: [
.library(
name: "SQLiteDB",
targets: ["SQLiteDB"]
),
.library(
name: "SQLCipher",
targets: ["SQLCipher"]
)
],
targets: [
.target(
name: "SQLiteDB",
dependencies: [.target(name: "SQLCipher")],
cSettings: [.define("SQLITE_HAS_CODEC")]
),
.target(
name: "SQLCipher",
sources: ["sqlite", "libtomcrypt"],
publicHeadersPath: "sqlite",
cSettings: [
.headerSearchPath("libtomcrypt/headers"),
.define("SQLITE_DQS", to: "0"),
.define("SQLITE_ENABLE_API_ARMOR"),
.define("SQLITE_ENABLE_COLUMN_METADATA"),
.define("SQLITE_ENABLE_DBSTAT_VTAB"),
.define("SQLITE_ENABLE_FTS3"),
.define("SQLITE_ENABLE_FTS3_PARENTHESIS"),
.define("SQLITE_ENABLE_FTS3_TOKENIZER"),
.define("SQLITE_ENABLE_FTS4"),
.define("SQLITE_ENABLE_FTS5"),
.define("SQLITE_ENABLE_MEMORY_MANAGEMENT"),
.define("SQLITE_ENABLE_PREUPDATE_HOOK"),
.define("SQLITE_ENABLE_RTREE"),
.define("SQLITE_ENABLE_SESSION"),
.define("SQLITE_ENABLE_STMTVTAB"),
.define("SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION"),
.define("SQLITE_ENABLE_UNLOCK_NOTIFY"),
.define("SQLITE_MAX_VARIABLE_NUMBER", to: "250000"),
.define("SQLITE_LIKE_DOESNT_MATCH_BLOBS"),
.define("SQLITE_OMIT_DEPRECATED"),
.define("SQLITE_OMIT_SHARED_CACHE"),
.define("SQLITE_SECURE_DELETE"),
.define("SQLITE_THREADSAFE", to: "2"),
.define("SQLITE_USE_URI"),
.define("SQLITE_ENABLE_SNAPSHOT"),
.define("SQLITE_HAS_CODEC"),
.define("SQLITE_HOMEGROWN_RECURSIVE_MUTEX"), // needed or we see hangs in test cases
.define("SQLITE_TEMP_STORE", to: "2"),
.define("SQLITE_EXTRA_INIT", to: "sqlcipher_extra_init"),
.define("SQLITE_EXTRA_SHUTDOWN", to: "sqlcipher_extra_shutdown"),
.define("HAVE_GETHOSTUUID", to: "0"),
.define("HAVE_STDINT_H"),
.define("SQLCIPHER_CRYPTO_LIBTOMCRYPT"),
.define("SQLCIPHER_CRYPTO_CUSTOM", to: "sqlcipher_ltc_setup"),
],
linkerSettings: [.linkedLibrary("log", .when(platforms: [.android]))]),
.testTarget(
name: "SQLiteDBTests",
dependencies: ["SQLiteDB"],
resources: [.process("Resources")]
)
]
)
4 changes: 2 additions & 2 deletions Sources/SQLCipher/sqlite/sqlite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -6661,7 +6661,7 @@ SQLITE_API int sqlite3_collation_needed16(
);

/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
//#ifdef SQLITE_HAS_CODEC
/*
** Specify the key for an encrypted database. This routine should be
** called right after sqlite3_open().
Expand Down Expand Up @@ -6717,7 +6717,7 @@ SQLITE_API int sqlite3_rekey_v2(
SQLITE_API void sqlite3_activate_see(
const char *zPassPhrase /* Activation phrase */
);
#endif
//#endif
/* END SQLCIPHER */

#ifdef SQLITE_ENABLE_CEROD
Expand Down
8 changes: 0 additions & 8 deletions Sources/SQLiteDB/Core/Backup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@

import Foundation
import Dispatch
#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux) || os(Windows) || os(Android)
import CSQLite
#else
import SQLite3
#endif

/// An object representing database backup.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/SQLiteDB/Core/Blob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// THE SOFTWARE.
//

public struct Blob {
public struct Blob: Sendable {

public let bytes: [UInt8]

Expand Down
8 changes: 0 additions & 8 deletions Sources/SQLiteDB/Core/Connection+Aggregation.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import Foundation
#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux) || os(Windows) || os(Android)
import CSQLite
#else
import SQLite3
#endif

extension Connection {
private typealias Aggregate = @convention(block) (Int, Context, Int32, Argv) -> Void
Expand Down
15 changes: 0 additions & 15 deletions Sources/SQLiteDB/Core/Connection+Attach.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import Foundation
#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux) || os(Windows) || os(Android)
import CSQLite
#else
import SQLite3
#endif

extension Connection {
#if SQLITE_SWIFT_SQLCIPHER
/// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#attach
public func attach(_ location: Location, as schemaName: String, key: String? = nil) throws {
if let key {
Expand All @@ -19,12 +10,6 @@ extension Connection {
try run("ATTACH DATABASE ? AS ?", location.description, schemaName)
}
}
#else
/// See https://www3.sqlite.org/lang_attach.html
public func attach(_ location: Location, as schemaName: String) throws {
try run("ATTACH DATABASE ? AS ?", location.description, schemaName)
}
#endif

/// See https://www3.sqlite.org/lang_detach.html
public func detach(_ schemaName: String) throws {
Expand Down
8 changes: 0 additions & 8 deletions Sources/SQLiteDB/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@

import Foundation
import Dispatch
#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux) || os(Windows) || os(Android)
import CSQLite
#else
import SQLite3
#endif

/// A connection to SQLite.
public final class Connection {
Expand Down
16 changes: 4 additions & 12 deletions Sources/SQLiteDB/Core/Result.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux) || os(Windows) || os(Android)
import CSQLite
#else
import SQLite3
#endif

public enum Result: Error {

Expand All @@ -19,7 +11,7 @@ public enum Result: Error {
/// - code: SQLite [error code](https://sqlite.org/rescode.html#primary_result_code_list)
///
/// - statement: the statement which produced the error
case error(message: String, code: Int32, statement: Statement?)
case error(message: String, code: Int32, statement: String?)

/// Represents a SQLite specific [extended error code] (https://sqlite.org/rescode.html#primary_result_codes_versus_extended_result_codes)
///
Expand All @@ -28,20 +20,20 @@ public enum Result: Error {
/// - extendedCode: SQLite [extended error code](https://sqlite.org/rescode.html#extended_result_code_list)
///
/// - statement: the statement which produced the error
case extendedError(message: String, extendedCode: Int32, statement: Statement?)
case extendedError(message: String, extendedCode: Int32, statement: String?)

init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) {
guard !Result.successCodes.contains(errorCode) else { return nil }

let message = String(cString: sqlite3_errmsg(connection.handle))

guard connection.usesExtendedErrorCodes else {
self = .error(message: message, code: errorCode, statement: statement)
self = .error(message: message, code: errorCode, statement: statement?.description)
return
}

let extendedErrorCode = sqlite3_extended_errcode(connection.handle)
self = .extendedError(message: message, extendedCode: extendedErrorCode, statement: statement)
self = .extendedError(message: message, extendedCode: extendedErrorCode, statement: statement?.description)
}

}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SQLiteDB/Core/SQLiteVersion.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct SQLiteVersion: Comparable, CustomStringConvertible {
public struct SQLiteVersion: Comparable, CustomStringConvertible, Sendable {
public let major: Int
public let minor: Int
public var point: Int = 0
Expand All @@ -17,6 +17,6 @@ public struct SQLiteVersion: Comparable, CustomStringConvertible {
lhs.tuple == rhs.tuple
}

static var zero: SQLiteVersion = .init(major: 0, minor: 0)
static let zero: SQLiteVersion = .init(major: 0, minor: 0)
private var tuple: (Int, Int, Int) { (major, minor, point) }
}
8 changes: 0 additions & 8 deletions Sources/SQLiteDB/Core/Statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@
// THE SOFTWARE.
//

#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux) || os(Windows) || os(Android)
import CSQLite
#else
import SQLite3
#endif

/// A single SQL statement.
public final class Statement {
Expand Down
6 changes: 3 additions & 3 deletions Sources/SQLiteDB/Core/URIQueryParameter.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Foundation

/// See https://www.sqlite.org/uri.html
public enum URIQueryParameter: CustomStringConvertible {
public enum FileMode: String {
public enum URIQueryParameter: CustomStringConvertible, Sendable {
public enum FileMode: String, Sendable {
case readOnly = "ro", readWrite = "rw", readWriteCreate = "rwc", memory
}

public enum CacheMode: String {
public enum CacheMode: String, Sendable {
case shared, `private`
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/SQLiteDB/Core/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
///
/// Do not conform custom types to the Binding protocol. See the `Value`
/// protocol, instead.
public protocol Binding {}
public protocol Binding: Sendable {}

public protocol Number: Binding {}

Expand Down Expand Up @@ -105,7 +105,7 @@ extension Blob: Binding, Value {

extension Bool: Binding, Value {

public static var declaredDatatype = Int64.declaredDatatype
public static let declaredDatatype = Int64.declaredDatatype

public static func fromDatatypeValue(_ datatypeValue: Int64) -> Bool {
datatypeValue != 0
Expand All @@ -119,7 +119,7 @@ extension Bool: Binding, Value {

extension Int: Number, Value {

public static var declaredDatatype = Int64.declaredDatatype
public static let declaredDatatype = Int64.declaredDatatype

public static func fromDatatypeValue(_ datatypeValue: Int64) -> Int {
Int(datatypeValue)
Expand Down
2 changes: 0 additions & 2 deletions Sources/SQLiteDB/Extensions/Cipher.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if SQLITE_SWIFT_SQLCIPHER
import SQLCipher


Expand Down Expand Up @@ -112,4 +111,3 @@ extension Connection {
_ = try scalar("SELECT count(*) FROM sqlite_master;")
}
}
#endif
2 changes: 1 addition & 1 deletion Sources/SQLiteDB/Extensions/FTS4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension VirtualTable {
}

// swiftlint:disable identifier_name
public struct Tokenizer {
public struct Tokenizer: Sendable {

public static let Simple = Tokenizer("simple")
public static let Porter = Tokenizer("porter")
Expand Down
2 changes: 1 addition & 1 deletion Sources/SQLiteDB/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension Date: Value {
/// A global date formatter used to serialize and deserialize `NSDate` objects.
/// If multiple date formats are used in an application’s database(s), use a
/// custom `Value` type per additional format.
public var dateFormatter: DateFormatter = {
public let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand Down
8 changes: 0 additions & 8 deletions Sources/SQLiteDB/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@
// THE SOFTWARE.
//

#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux) || os(Windows) || os(Android)
import CSQLite
#else
import SQLite3
#endif

public typealias Star = (SQLExpression<Binding>?, SQLExpression<Binding>?) -> SQLExpression<Void>

Expand Down
Loading