Skip to content

Commit bb0c40a

Browse files
committed
Update SQLiteAdapter.swift
1 parent 0df644b commit bb0c40a

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

Sources/SQLiteAdapter/SQLiteAdapter.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import Foundation
1111
import SQLite3
12+
#if os(Linux)
13+
import Dispatch
14+
#endif
1215

1316
public enum SQLiteError: Error {
1417
case OpenDB(_ msg: String)
@@ -20,7 +23,7 @@ public enum SQLiteError: Error {
2023
case Other(_ msg: String)
2124
}
2225

23-
// http://www.sqlite.org/datatype3.html
26+
/// http://www.sqlite.org/datatype3.html
2427
public enum SQLType {
2528
case INT // Includes INT, INTEGER, INT2, INT8, BIGINT, MEDIUMINT, SMALLINT, TINYINT
2629
case BOOL // Includes BOOL, BOOLEAN, BIT
@@ -95,7 +98,7 @@ open class SQLite: SQLiteType {
9598
return id
9699
}
97100

98-
/// - Returns: the number of rows changed by the most recently completed INSERT, DELETE or UPDATE statement
101+
/// - Returns: The number of rows changed by the most recently completed `INSERT`, `DELETE` or `UPDATE` statement.
99102
public var changes: Int {
100103
var changes = 0
101104
queue.sync {
@@ -105,7 +108,7 @@ open class SQLite: SQLiteType {
105108
return changes
106109
}
107110

108-
/// - Returns: the number of rows changed by INSERT, DELETE or UPDATE statements since the current DB was opened
111+
/// - Returns: The number of rows changed by `INSERT`, `DELETE` or `UPDATE` statements since the current DB was opened.
109112
public var totalChanges: Int {
110113
var totalChanges = 0
111114
queue.sync {
@@ -321,7 +324,7 @@ open class SQLite: SQLiteType {
321324
log("COMMIT")
322325
}
323326

324-
/// Can be used to insert one or several rows depending on the SQL statement
327+
/// Can be used to insert one or several rows depending on the SQL statement.
325328
/// - Returns: (the number of inserted rows, id for the last inserted row)
326329
@discardableResult
327330
public func insertRow(sql: String, params: [Any]? = nil) throws -> (changes: Int, lastInsertID: Int) {
@@ -337,8 +340,8 @@ open class SQLite: SQLiteType {
337340
return (changes, lastInsertID)
338341
}
339342

340-
/// Can be used to update one or several rows depending on the SQL statement
341-
/// - Returns: the number of updated rows
343+
/// Can be used to update one or several rows depending on the SQL statement.
344+
/// - Returns: The number of updated rows.
342345
@discardableResult
343346
public func updateRow(sql: String, params: [Any]? = nil) throws -> Int {
344347
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("UPDATE ") else {
@@ -353,8 +356,8 @@ open class SQLite: SQLiteType {
353356
return changes
354357
}
355358

356-
/// Can be used to delete one or several rows depending on the SQL statement
357-
/// - Returns: the number of deleted rows
359+
/// Can be used to delete one or several rows depending on the SQL statement.
360+
/// - Returns: The number of deleted rows.
358361
@discardableResult
359362
public func deleteRow(sql: String, params: [Any]? = nil) throws -> Int {
360363
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("DELETE ") else {
@@ -369,7 +372,7 @@ open class SQLite: SQLiteType {
369372
return changes
370373
}
371374

372-
/// - Returns: 1 if the row with the specified id was deleted, otherwise returns 0
375+
/// - Returns: 1 if the row with the specified id was deleted, otherwise returns 0.
373376
@discardableResult
374377
public func deleteByID(in table: SQLTable, id: Int) throws -> Int {
375378
let sql = "DELETE FROM \(table.name) WHERE \(table.primaryKey) = ?;"
@@ -382,7 +385,7 @@ open class SQLite: SQLiteType {
382385
return changes
383386
}
384387

385-
/// - Returns: the number of deleted rows
388+
/// - Returns: The number of deleted rows.
386389
@discardableResult
387390
public func deleteAllRows(in table: SQLTable, vacuum: Bool = true, resetAutoincrement: Bool = true) throws -> Int {
388391
let sql = "DELETE FROM \(table.name);"
@@ -440,8 +443,8 @@ open class SQLite: SQLiteType {
440443
return Int(count)
441444
}
442445

443-
/// Can be used to read one or several rows depending on the SQL statement
444-
/// - Returns: [SQLValues] if one or more rows were selected, otherwise returns nil
446+
/// Can be used to read one or several rows depending on the SQL statement.
447+
/// - Returns: `[SQLValues]` if one or more rows were selected, otherwise returns `nil`.
445448
public func getRow(from table: SQLTable, sql: String, params: [Any]? = nil) throws -> [SQLValues]? {
446449
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("SELECT ") else {
447450
throw SQLiteError.Statement("Invalid SQL statement")
@@ -530,7 +533,7 @@ open class SQLite: SQLiteType {
530533
}
531534
}
532535

533-
/// Checks the structure of the result table and synchronizes it in SQLTableColums
536+
/// Checks the structure of the result table and synchronizes it in `SQLTableColums`.
534537
private func getResultColumns(_ table: SQLTable, sqlStatement: OpaquePointer?) throws -> SQLTableColums {
535538
var columnNamesToReturn: [String] = []
536539
let columnCount = sqlite3_column_count(sqlStatement)
@@ -554,7 +557,7 @@ open class SQLite: SQLiteType {
554557
return resultColumns
555558
}
556559

557-
/// - Returns: [SQLValues] if one or more rows were selected, otherwise returns nil
560+
/// - Returns: `[SQLValues]` if one or more rows were selected, otherwise returns `nil`.
558561
public func getAllRows(from table: SQLTable) throws -> [SQLValues]? {
559562
let sql = "SELECT * FROM \(table.name);"
560563
if let result = try getRow(from: table, sql: sql) {
@@ -564,7 +567,7 @@ open class SQLite: SQLiteType {
564567
return nil
565568
}
566569

567-
/// - Returns: SQLValues if a row was selected, otherwise returns nil
570+
/// - Returns: `SQLValues` if a row was selected, otherwise returns `nil`.
568571
public func getByID(from table: SQLTable, id: Int) throws -> SQLValues? {
569572
let sql = "SELECT * FROM \(table.name) WHERE \(table.primaryKey) = ? LIMIT 1;"
570573
if let result = try getRow(from: table, sql: sql, params: [id]) {
@@ -574,7 +577,7 @@ open class SQLite: SQLiteType {
574577
return nil
575578
}
576579

577-
/// - Returns: SQLValues if a row was selected, otherwise returns nil
580+
/// - Returns: `SQLValues` if a row was selected, otherwise returns `nil`.
578581
public func getFirstRow(from table: SQLTable) throws -> SQLValues? {
579582
let sql = "SELECT * FROM \(table.name) WHERE \(table.primaryKey) = (SELECT MIN(\(table.primaryKey)) FROM \(table.name));"
580583
if let result = try getRow(from: table, sql: sql) {
@@ -584,7 +587,7 @@ open class SQLite: SQLiteType {
584587
return nil
585588
}
586589

587-
/// - Returns: SQLValues if a row was selected, otherwise returns nil
590+
/// - Returns: `SQLValues` if a row was selected, otherwise returns `nil`.
588591
public func getLastRow(from table: SQLTable) throws -> SQLValues? {
589592
let sql = "SELECT * FROM \(table.name) WHERE \(table.primaryKey) = (SELECT MAX(\(table.primaryKey)) FROM \(table.name));"
590593
if let result = try getRow(from: table, sql: sql) {
@@ -594,15 +597,15 @@ open class SQLite: SQLiteType {
594597
return nil
595598
}
596599

597-
/// Repacks the DB to take advantage of deleted data
600+
/// Repacks the DB to take advantage of deleted data.
598601
public func vacuum() throws {
599602
let sql = "VACUUM;"
600603
try operation(sql: sql)
601604
log("VACUUM")
602605
}
603606

604-
/// Any other query except reading
605-
/// - Returns: the number of rows changed
607+
/// Any other query except reading.
608+
/// - Returns: The number of rows changed.
606609
@discardableResult
607610
public func query(sql: String, params: [Any]? = nil) throws -> Int {
608611
let changes = try operation(sql: sql, params: params)

0 commit comments

Comments
 (0)