-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
134 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Foundation | ||
import SQLite3 | ||
|
||
public final class DiskSqlite: Sqlite { | ||
|
||
private let path: String | ||
|
||
private var db: OpaquePointer? | ||
|
||
public init(path: String) { | ||
self.path = path | ||
} | ||
|
||
public func openDatabase() throws { | ||
guard sqlite3_open_v2(path, &db, SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK else { | ||
throw SQLiteError.openDatabase(path: path) | ||
} | ||
var error: UnsafeMutablePointer<CChar>? | ||
guard sqlite3_exec(db, "PRAGMA journal_mode=WAL;", nil, nil, &error) == SQLITE_OK else { | ||
let message = error.map { String(cString: $0) } | ||
throw SQLiteError.exec(error: message) | ||
} | ||
} | ||
|
||
public func query<Row: SqliteRow>(sql: String) throws -> [Row] { | ||
var queryStatement: OpaquePointer? | ||
guard sqlite3_prepare_v2(db, sql, -1, &queryStatement, nil) == SQLITE_OK else { | ||
throw SQLiteError.queryPrepare(statement: sql) | ||
} | ||
var rows: [Row] = [] | ||
while sqlite3_step(queryStatement) == SQLITE_ROW { | ||
let decoder = SqliteRowDecoder(statement: queryStatement) | ||
guard let row = try? Row(decoder: decoder) else { continue } | ||
rows.append(row) | ||
} | ||
sqlite3_finalize(queryStatement) | ||
return rows | ||
} | ||
|
||
public func execute(sql: String) throws { | ||
var error: UnsafeMutablePointer<CChar>? | ||
guard sqlite3_exec(db, sql, nil, nil, &error) == SQLITE_OK else { | ||
let message = error.map { String(cString: $0) } | ||
throw SQLiteError.exec(error: message) | ||
} | ||
} | ||
|
||
public func closeConnection() { | ||
sqlite3_close(db) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Foundation | ||
import SQLite3 | ||
|
||
public final class MemorySqlite: Sqlite { | ||
|
||
private var db: OpaquePointer? | ||
|
||
public init() throws { | ||
guard sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK else { | ||
throw SQLiteError.openDatabaseMemory | ||
} | ||
var error: UnsafeMutablePointer<CChar>? | ||
guard sqlite3_exec(db, "PRAGMA journal_mode=WAL;", nil, nil, &error) == SQLITE_OK else { | ||
let message = error.map { String(cString: $0) } | ||
throw SQLiteError.exec(error: message) | ||
} | ||
} | ||
|
||
public func openDatabase() throws { | ||
// No op | ||
} | ||
|
||
public func query<Row: SqliteRow>(sql: String) throws -> [Row] { | ||
var queryStatement: OpaquePointer? | ||
guard sqlite3_prepare_v2(db, sql, -1, &queryStatement, nil) == SQLITE_OK else { | ||
throw SQLiteError.queryPrepare(statement: sql) | ||
} | ||
var rows: [Row] = [] | ||
while sqlite3_step(queryStatement) == SQLITE_ROW { | ||
let decoder = SqliteRowDecoder(statement: queryStatement) | ||
guard let row = try? Row(decoder: decoder) else { continue } | ||
rows.append(row) | ||
} | ||
sqlite3_finalize(queryStatement) | ||
return rows | ||
} | ||
|
||
public func execute(sql: String) throws { | ||
var error: UnsafeMutablePointer<CChar>? | ||
guard sqlite3_exec(db, sql, nil, nil, &error) == SQLITE_OK else { | ||
let message = error.map { String(cString: $0) } | ||
throw SQLiteError.exec(error: message) | ||
} | ||
} | ||
|
||
public func closeConnection() { | ||
// No op | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,20 @@ | ||
import Foundation | ||
import SQLite3 | ||
|
||
public final class Sqlite { | ||
|
||
private var db: OpaquePointer? | ||
|
||
public init() { } | ||
public protocol Sqlite { | ||
|
||
/// Opening A New Database Connection | ||
/// - Parameter path: Path to database | ||
public func openDatabase(path: String) throws { | ||
guard sqlite3_open_v2(path, &db, SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK else { | ||
throw SQLiteError.openDatabase(path: path) | ||
} | ||
var error: UnsafeMutablePointer<CChar>? | ||
guard sqlite3_exec(db, "PRAGMA journal_mode=WAL;", nil, nil, &error) == SQLITE_OK else { | ||
let message = error.map { String(cString: $0) } | ||
throw SQLiteError.exec(error: message) | ||
} | ||
} | ||
func openDatabase() throws | ||
|
||
/// Evaluate an SQL Statement | ||
/// - Parameter sql: SQL query | ||
/// - Returns: Table rows array | ||
public func query<Row: SqliteRow>(sql: String) throws -> [Row] { | ||
var queryStatement: OpaquePointer? | ||
guard sqlite3_prepare_v2(db, sql, -1, &queryStatement, nil) == SQLITE_OK else { | ||
throw SQLiteError.queryPrepare(statement: sql) | ||
} | ||
var rows: [Row] = [] | ||
while sqlite3_step(queryStatement) == SQLITE_ROW { | ||
let decoder = SqliteRowDecoder(statement: queryStatement) | ||
guard let row = try? Row(decoder: decoder) else { continue } | ||
rows.append(row) | ||
} | ||
sqlite3_finalize(queryStatement) | ||
return rows | ||
} | ||
func query<Row: SqliteRow>(sql: String) throws -> [Row] | ||
|
||
/// One-Step query execution | ||
/// - Parameter sql: SQL query | ||
public func execute(sql: String) throws { | ||
var error: UnsafeMutablePointer<CChar>? | ||
guard sqlite3_exec(db, sql, nil, nil, &error) == SQLITE_OK else { | ||
let message = error.map { String(cString: $0) } | ||
throw SQLiteError.exec(error: message) | ||
} | ||
} | ||
func execute(sql: String) throws | ||
|
||
/// Closing A Database Connection | ||
public func closeConnection() { | ||
sqlite3_close(db) | ||
} | ||
func closeConnection() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters