Skip to content

Commit 3a3282f

Browse files
committed
Added new case Statement to SQLiteError, updated several methods
1 parent ec408d5 commit 3a3282f

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Sources/SQLiteAdapter/SQLiteAdapter.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum SQLiteError: Error {
1616
case Step(_ msg: String)
1717
case Bind(_ msg: String)
1818
case Column(_ msg: String)
19+
case Statement(_ msg: String)
1920
case Other(_ msg: String)
2021
}
2122

@@ -220,6 +221,9 @@ open class SQLite: SQLiteType {
220221
}
221222

222223
public func createTable(sql: String) throws {
224+
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("CREATE ") else {
225+
throw SQLiteError.Statement("Invalid SQL statement")
226+
}
223227
try operation(sql: sql)
224228
log("successfully created table, sql: \(sql)")
225229
}
@@ -297,19 +301,28 @@ open class SQLite: SQLiteType {
297301
/// Can be used to insert one or several rows depending on the SQL statement
298302
/// - Returns: The id for the last inserted row
299303
public func insertRow(sql: String, valuesToBind: SQLValues? = nil) throws -> Int {
304+
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("INSERT ") else {
305+
throw SQLiteError.Statement("Invalid SQL statement")
306+
}
300307
try operation(sql: sql, valuesToBind: valuesToBind)
301308
log("successfully inserted row(s), sql: \(sql)")
302309
return getLastInsertID()
303310
}
304311

305312
/// Can be used to update one or several rows depending on the SQL statement
306313
public func updateRow(sql: String, valuesToBind: SQLValues? = nil) throws {
314+
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("UPDATE ") else {
315+
throw SQLiteError.Statement("Invalid SQL statement")
316+
}
307317
try operation(sql: sql, valuesToBind: valuesToBind)
308318
log("successfully updated row(s), sql: \(sql)")
309319
}
310320

311321
/// Can be used to delete one or several rows depending on the SQL statement
312322
public func deleteRow(sql: String, valuesToBind: SQLValues? = nil) throws {
323+
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("DELETE ") else {
324+
throw SQLiteError.Statement("Invalid SQL statement")
325+
}
313326
try operation(sql: sql, valuesToBind: valuesToBind)
314327
log("successfully deleted row(s), sql: \(sql)")
315328
}
@@ -348,6 +361,10 @@ open class SQLite: SQLiteType {
348361
}
349362

350363
public func getRowCountWithCondition(sql: String, valuesToBind: SQLValues? = nil) throws -> Int {
364+
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("SELECT ") else {
365+
throw SQLiteError.Statement("Invalid SQL statement")
366+
}
367+
351368
let sqlStatement = try prepareStatement(sql: sql)
352369
defer {
353370
sqlite3_finalize(sqlStatement)
@@ -365,6 +382,10 @@ open class SQLite: SQLiteType {
365382

366383
/// Can be used to read one or several rows depending on the SQL statement
367384
public func getRow(sql: String, valuesToBind: SQLValues? = nil, valuesToGet: SQLValues) throws -> [SQLValues] {
385+
guard sql.uppercased().trimmingCharacters(in: .whitespaces).hasPrefix("SELECT ") else {
386+
throw SQLiteError.Statement("Invalid SQL statement")
387+
}
388+
368389
let sqlStatement = try prepareStatement(sql: sql)
369390
defer {
370391
sqlite3_finalize(sqlStatement)

0 commit comments

Comments
 (0)