allStatements() can build statements without any arguments

This commit is contained in:
Gwendal Roué committed 2021-07-30 14:24:08 +02:00
1 parent be0a2c01e1
commit dc3fac95db
2 files changed
+130 -14

No files matched your search

+51 -13
View File
@@ -35,7 +35,7 @@ extension Database {
/// - returns: A Statement.
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
/// - precondition: No argument must be set, or all arguments must be set.
/// A fatal error is raised otherwise.
/// An error is raised otherwise.
///
/// // OK
/// try makeStatement(literal: """
@@ -54,8 +54,8 @@ extension Database {
let (sql, arguments) = try sqlLiteral.build(self)
let statement = try makeStatement(sql: sql)
if arguments.isEmpty == false {
// Crash if arguments do not match
statement.arguments = arguments
// Throws if arguments do not match
try statement.setArguments(arguments)
}
return statement
}
@@ -80,7 +80,7 @@ extension Database {
/// - returns: A Statement.
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
/// - precondition: No argument must be set, or all arguments must be set.
/// A fatal error is raised otherwise.
/// An error is raised otherwise.
///
/// // OK
/// try makeSelectStatement(literal: """
@@ -177,7 +177,7 @@ extension Database {
/// - returns: A Statement.
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
/// - precondition: No argument must be set, or all arguments must be set.
/// A fatal error is raised otherwise.
/// An error is raised otherwise.
///
/// // OK
/// try cachedStatement(literal: """
@@ -196,8 +196,8 @@ extension Database {
let (sql, arguments) = try sqlLiteral.build(self)
let statement = try cachedStatement(sql: sql)
if arguments.isEmpty == false {
// Crash if arguments do not match
statement.arguments = arguments
// Throws if arguments do not match
try statement.setArguments(arguments)
}
return statement
}
@@ -228,7 +228,7 @@ extension Database {
/// - returns: A Statement.
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
/// - precondition: No argument must be set, or all arguments must be set.
/// A fatal error is raised otherwise.
/// An error is raised otherwise.
///
/// // OK
/// try cachedSelectStatement(literal: """
@@ -273,7 +273,7 @@ extension Database {
/// - returns: A Statement.
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
/// - precondition: No argument must be set, or all arguments must be set.
/// A fatal error is raised otherwise.
/// An error is raised otherwise.
///
/// // OK
/// try makeUpdateStatement(literal: """
@@ -315,7 +315,7 @@ extension Database {
/// - returns: A Statement.
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
/// - precondition: No argument must be set, or all arguments must be set.
/// A fatal error is raised otherwise.
/// An error is raised otherwise.
///
/// // OK
/// try cachedUpdateStatement(literal: """
@@ -350,10 +350,28 @@ extension Database {
/// - arguments: Statement arguments.
/// - returns: A cursor of `Statement`
/// - throws: A DatabaseError whenever an SQLite error occurs.
public func allStatements(sql: String, arguments: StatementArguments = StatementArguments())
/// - precondition: Arguments must be nil, or all arguments must be set.
/// The returned cursor will throw an error otherwise.
///
/// // OK
/// try allStatements(sql: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """)
/// try allStatements(sql: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """, arguments: [1000, 1000])
///
/// // NOT OK
/// try allStatements(sql: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """, arguments: [1000])
public func allStatements(sql: String, arguments: StatementArguments? = nil)
throws -> SQLStatementCursor
{
try allStatements(literal: SQL(sql: sql, arguments: arguments))
SQLStatementCursor(database: self, sql: sql, arguments: arguments)
}
/// Returns a cursor of all SQL statements separated by semi-colons.
@@ -373,10 +391,30 @@ extension Database {
/// - parameter sqlLiteral: An `SQL` literal.
/// - returns: A cursor of `Statement`
/// - throws: A DatabaseError whenever an SQLite error occurs.
/// - precondition: No argument must be set, or all arguments must be set.
/// The returned cursor will throw an error otherwise.
///
/// // OK
/// try allStatements(literal: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """)
/// try allStatements(literal: """
/// SELECT COUNT(*) FROM player WHERE score < \(1000);
/// SELECT COUNT(*) FROM player WHERE score > \(1000);
/// """)
///
/// // NOT OK
/// try allStatements(literal: """
/// SELECT COUNT(*) FROM player WHERE score < \(1000);
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """)
public func allStatements(literal sqlLiteral: SQL) throws -> SQLStatementCursor {
let context = SQLGenerationContext(self)
let sql = try sqlLiteral.sql(context)
let arguments = context.arguments
let arguments = context.arguments.isEmpty
? nil // builds statements without arguments
: context.arguments // force arguments to match
return SQLStatementCursor(database: self, sql: sql, arguments: arguments)
}
+79 -1
View File
@@ -176,7 +176,49 @@ class SelectStatementTests : GRDBTestCase {
XCTAssertEqual(ages, [13, 26, 41, 41, 26, 13])
}
do {
// Literal, arguments
// Literal, no argument
let statements = try db.allStatements(literal: """
SELECT age FROM persons ORDER BY age;
SELECT age FROM persons ORDER BY age DESC;
""")
let ages = try Array(statements.flatMap { try Int.fetchCursor($0) })
XCTAssertEqual(ages, [13, 26, 41, 41, 26, 13])
}
do {
// SQL, missing arguments
let statements = try db.allStatements(sql: """
SELECT count(*) FROM persons WHERE age > ?;
SELECT count(*) FROM persons WHERE age < ?;
""")
let counts = try Array(statements.map { try
Int.fetchOne($0, arguments: [30])!
})
XCTAssertEqual(counts, [1, 2])
}
do {
// Literal, missing arguments
let statements = try db.allStatements(literal: """
SELECT count(*) FROM persons WHERE age > ?;
SELECT count(*) FROM persons WHERE age < ?;
""")
let counts = try Array(statements.map { try
Int.fetchOne($0, arguments: [30])!
})
XCTAssertEqual(counts, [1, 2])
}
do {
// SQL, matching arguments
let statements = try db.allStatements(sql: """
SELECT name FROM persons WHERE name = ?;
SELECT name FROM persons WHERE age > ? ORDER BY name;
""", arguments: ["Arthur", 20])
let names = try Array(statements.map { try String.fetchAll($0) })
XCTAssertEqual(names, [["Arthur"], ["Arthur", "Barbara"]])
}
do {
// Literal, matching arguments
let statements = try db.allStatements(literal: """
SELECT name FROM persons WHERE name = \("Arthur");
SELECT name FROM persons WHERE age > \(20) ORDER BY name;
@@ -184,6 +226,42 @@ class SelectStatementTests : GRDBTestCase {
let names = try Array(statements.map { try String.fetchAll($0) })
XCTAssertEqual(names, [["Arthur"], ["Arthur", "Barbara"]])
}
do {
// SQL, too few arguments
let statements = try db.allStatements(sql: """
SELECT name FROM persons WHERE name = ?;
SELECT name FROM persons WHERE age > ? ORDER BY name;
""", arguments: ["Arthur"])
_ = try Array(statements.map { try String.fetchAll($0) })
XCTFail("Expected Error")
} catch DatabaseError.SQLITE_MISUSE {
// OK
}
do {
// Literal, too few arguments
let statements = try db.allStatements(literal: """
SELECT name FROM persons WHERE name = \("Arthur");
SELECT name FROM persons WHERE age > ? ORDER BY name;
""")
_ = try Array(statements.map { try String.fetchAll($0) })
XCTFail("Expected Error")
} catch DatabaseError.SQLITE_MISUSE {
// OK
}
do {
// SQL, too many arguments
let statements = try db.allStatements(sql: """
SELECT name FROM persons WHERE name = ?;
SELECT name FROM persons WHERE age > ? ORDER BY name;
""", arguments: ["Arthur", 20, 55])
_ = try Array(statements.map { try String.fetchAll($0) })
XCTFail("Expected Error")
} catch DatabaseError.SQLITE_MISUSE {
// OK
}
do {
// Mix statement kinds
let statements = try db.allStatements(literal: """