Skip to content

Commit 348b30c

Browse files
kilnermdjones6
authored andcommitted
fix: Make Model.executeQuery class funcs public (#87)
1 parent a4a2112 commit 348b30c

2 files changed

Lines changed: 82 additions & 29 deletions

File tree

README.md

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -222,35 +222,70 @@ Grade.deleteAll { error in
222222
}
223223
```
224224

225-
### Customization
225+
### Customizing your Model
226226

227-
The ORM uses [Swift-Kuery](https://github.com/IBM-Swift/Swift-Kuery) which allows you to customize and execute your own queries without breaking any existing ORM functionality. You'll want to have access to the table for your object, which you can get with the `getTable()` function:
227+
The ORM defines an extension to `Model` which provides a number of `public static executeQuery(…)` functions. These can be used to create custom functions within your model that perform more complex database operations. The example below defines a Person model and with a custom function that will retrieve all records which have age > 20:
228228

229229
```swift
230-
do {
231-
let table = Grade.getTable()
232-
} catch {
233-
// Error
230+
// define the Person struct
231+
struct Person: Codable {
232+
var firstname: String
233+
var surname: String
234+
var age: Int
234235
}
235-
```
236236

237-
After you retrieve your table, you can create a `Query` object to specify what you want to execute on your database, and perform it like so:
238-
239-
```swift
240-
executeQuery(query: Query) { (grade: Grade?, error: RequestError?) in
241-
...
237+
// extend Person to conform to model and add overTwenties function
238+
extension Person: Model {
239+
240+
// Define a synchronous function to retrieve all records of Person with age > 20
241+
public static func getOverTwenties() -> [Person]? {
242+
let wait = DispatchSemaphore(value: 0)
243+
// First get the table
244+
var table: Table
245+
do {
246+
table = try Person.getTable()
247+
} catch {
248+
// Handle error
249+
}
250+
// Define result, query and execute
251+
var overTwenties: [Person]? = nil
252+
let query = Select(from: table).where("age > 20")
253+
254+
Person.executeQuery(query: query, parameters: nil) { results, error in
255+
guard let results = results else {
256+
// Handle error
257+
}
258+
overTwenties = results
259+
wait.signal()
260+
return
261+
}
262+
wait.wait()
263+
return overTwenties
264+
}
242265
}
243266
```
244267

245-
You can customize the parameters passed into your closure after you execute a `Query` like so:
246-
268+
Alternatively you can define and asynchronous getOverTwenties function:
247269
```swift
248-
executeQuery(query: Query) { grade, error in
249-
...
270+
public static func getOverTwenties(oncompletion: @escaping ([Person]?, RequestError?)-> Void) {
271+
var table: Table
272+
do {
273+
table = try Person.getTable()
274+
} catch {
275+
// Handle error
276+
}
277+
let query = Select(from: table).where("age > 20")
278+
Person.executeQuery(query: query, parameters: nil, oncompletion)
250279
}
280+
```
251281

252-
executeQuery(query: Query) { error in
253-
...
282+
which can be called in a fashion similar to the following:
283+
```swift
284+
Person.getOverTwenties() { result, error in
285+
guard let result = result else {
286+
// Handle error
287+
}
288+
// Use result
254289
}
255290
```
256291

Sources/SwiftKueryORM/Model.swift

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public extension Model {
349349
Self.executeQuery(query: query, parameters: parameters, using: db, onCompletion)
350350
}
351351

352-
internal func executeQuery(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (Self?, RequestError?) -> Void ) {
352+
private func executeQuery(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (Self?, RequestError?) -> Void ) {
353353
Self.executeTask() { connection, error in
354354
guard let connection = connection else {
355355
guard let error = error else {
@@ -372,7 +372,7 @@ public extension Model {
372372
}
373373
}
374374

375-
internal func executeQuery<I: Identifier>(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (I?, Self?, RequestError?) -> Void ) {
375+
private func executeQuery<I: Identifier>(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (I?, Self?, RequestError?) -> Void ) {
376376
Self.executeTask() { connection, error in
377377
guard let connection = connection else {
378378
guard let error = error else {
@@ -422,7 +422,12 @@ public extension Model {
422422
}
423423
}
424424

425-
internal static func executeQuery(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (Self?, RequestError?) -> Void ) {
425+
/// Allows custom functions on a model to query the database directly.
426+
/// - Parameter query: The `Query` to execute
427+
/// - Parameter parameters: An optional array of parameters to pass to the query
428+
/// - Parameter using: Optional Database to use
429+
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of (Self?, RequestError?), of which one will be nil, depending on whether the query was successful.
430+
public static func executeQuery(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (Self?, RequestError?) -> Void ) {
426431
Self.executeTask() { connection, error in
427432
guard let connection = connection else {
428433
guard let error = error else {
@@ -462,7 +467,12 @@ public extension Model {
462467
}
463468
}
464469

465-
internal static func executeQuery<I: Identifier>(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (I?, Self?, RequestError?) -> Void ) {
470+
/// Allows custom functions on a model to query the database directly.
471+
/// - Parameter query: The `Query` to execute
472+
/// - Parameter parameters: An optional array of parameters to pass to the query
473+
/// - Parameter using: Optional Database to use
474+
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of (Identifier?, Self?, RequestError?), of which some will be nil, depending on whether the query was successful.
475+
public static func executeQuery<I: Identifier>(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (I?, Self?, RequestError?) -> Void ) {
466476
Self.executeTask() { connection, error in
467477
guard let connection = connection else {
468478
guard let error = error else {
@@ -520,9 +530,12 @@ public extension Model {
520530
}
521531
}
522532

533+
/// Allows custom functions on a model to query the database directly.
534+
/// - Parameter query: The `Query` to execute
535+
/// - Parameter parameters: An optional array of parameters to pass to the query
523536
/// - Parameter using: Optional Database to use
524-
/// - Returns: A tuple ([Model], RequestError)
525-
internal static func executeQuery(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping ([Self]?, RequestError?)-> Void ) {
537+
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of ([Self]?, RequestError?), of which one will be nil, depending on whether the query was successful.
538+
public static func executeQuery(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping ([Self]?, RequestError?)-> Void ) {
526539
Self.executeTask() { connection, error in
527540
guard let connection = connection else {
528541
guard let error = error else {
@@ -582,9 +595,12 @@ public extension Model {
582595
}
583596
}
584597

598+
/// Allows custom functions on a model to query the database directly.
599+
/// - Parameter query: The `Query` to execute
600+
/// - Parameter parameters: An optional array of parameters to pass to the query
585601
/// - Parameter using: Optional Database to use
586-
/// - Returns: A tuple ([Model], RequestError)
587-
internal static func executeQuery<I: Identifier>(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping ([(I, Self)]?, RequestError?) -> Void ) {
602+
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of ([Identifier, Self]?, RequestError?), of which one will be nil, depending on whether the query was successful.
603+
public static func executeQuery<I: Identifier>(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping ([(I, Self)]?, RequestError?) -> Void ) {
588604
Self.executeTask() { connection, error in
589605
guard let connection = connection else {
590606
guard let error = error else {
@@ -658,10 +674,12 @@ public extension Model {
658674
}
659675
}
660676

677+
/// Allows custom functions on a model to query the database directly.
678+
/// - Parameter query: The `Query` to execute
679+
/// - Parameter parameters: An optional array of parameters to pass to the query
661680
/// - Parameter using: Optional Database to use
662-
/// - Returns: An optional RequestError
663-
664-
internal static func executeQuery(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping (RequestError?) -> Void ) {
681+
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a RequestError? which may be nil, depending on whether the query was successful.
682+
public static func executeQuery(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping (RequestError?) -> Void ) {
665683
Self.executeTask() { connection, error in
666684
guard let connection = connection else {
667685
guard let error = error else {

0 commit comments

Comments
 (0)