You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+53-18Lines changed: 53 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -222,35 +222,70 @@ Grade.deleteAll { error in
222
222
}
223
223
```
224
224
225
-
### Customization
225
+
### Customizing your Model
226
226
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:
228
228
229
229
```swift
230
-
do {
231
-
let table = Grade.getTable()
232
-
} catch {
233
-
// Error
230
+
// define the Person struct
231
+
structPerson: Codable {
232
+
var firstname: String
233
+
var surname: String
234
+
var age: Int
234
235
}
235
-
```
236
236
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
+
extensionPerson: Model {
239
+
240
+
// Define a synchronous function to retrieve all records of Person with age > 20
241
+
publicstaticfuncgetOverTwenties() -> [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
+
guardlet results = results else {
256
+
// Handle error
257
+
}
258
+
overTwenties = results
259
+
wait.signal()
260
+
return
261
+
}
262
+
wait.wait()
263
+
return overTwenties
264
+
}
242
265
}
243
266
```
244
267
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:
internalfunc executeQuery(query:Query, parameters:[Any?], using db:Database?=nil, _ onCompletion:@escaping(Self?,RequestError?)->Void){
352
+
privatefunc executeQuery(query:Query, parameters:[Any?], using db:Database?=nil, _ onCompletion:@escaping(Self?,RequestError?)->Void){
353
353
Self.executeTask(){ connection, error in
354
354
guardlet connection = connection else{
355
355
guardlet error = error else{
@@ -372,7 +372,7 @@ public extension Model {
372
372
}
373
373
}
374
374
375
-
internalfunc executeQuery<I:Identifier>(query:Query, parameters:[Any?], using db:Database?=nil, _ onCompletion:@escaping(I?,Self?,RequestError?)->Void){
375
+
privatefunc executeQuery<I:Identifier>(query:Query, parameters:[Any?], using db:Database?=nil, _ onCompletion:@escaping(I?,Self?,RequestError?)->Void){
376
376
Self.executeTask(){ connection, error in
377
377
guardlet connection = connection else{
378
378
guardlet error = error else{
@@ -422,7 +422,12 @@ public extension Model {
422
422
}
423
423
}
424
424
425
-
internalstaticfunc 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
+
publicstaticfunc executeQuery(query:Query, parameters:[Any?], using db:Database?=nil, _ onCompletion:@escaping(Self?,RequestError?)->Void){
426
431
Self.executeTask(){ connection, error in
427
432
guardlet connection = connection else{
428
433
guardlet error = error else{
@@ -462,7 +467,12 @@ public extension Model {
462
467
}
463
468
}
464
469
465
-
internalstaticfunc 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
+
publicstaticfunc executeQuery<I:Identifier>(query:Query, parameters:[Any?], using db:Database?=nil, _ onCompletion:@escaping(I?,Self?,RequestError?)->Void){
466
476
Self.executeTask(){ connection, error in
467
477
guardlet connection = connection else{
468
478
guardlet error = error else{
@@ -520,9 +530,12 @@ public extension Model {
520
530
}
521
531
}
522
532
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
523
536
/// - Parameter using: Optional Database to use
524
-
/// - Returns: A tuple ([Model], RequestError)
525
-
internalstaticfunc 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
+
publicstaticfunc executeQuery(query:Query, parameters:[Any?]?=nil, using db:Database?=nil, _ onCompletion:@escaping([Self]?,RequestError?)->Void){
526
539
Self.executeTask(){ connection, error in
527
540
guardlet connection = connection else{
528
541
guardlet error = error else{
@@ -582,9 +595,12 @@ public extension Model {
582
595
}
583
596
}
584
597
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
585
601
/// - Parameter using: Optional Database to use
586
-
/// - Returns: A tuple ([Model], RequestError)
587
-
internalstaticfunc 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
+
publicstaticfunc executeQuery<I:Identifier>(query:Query, parameters:[Any?]?=nil, using db:Database?=nil, _ onCompletion:@escaping([(I,Self)]?,RequestError?)->Void){
588
604
Self.executeTask(){ connection, error in
589
605
guardlet connection = connection else{
590
606
guardlet error = error else{
@@ -658,10 +674,12 @@ public extension Model {
658
674
}
659
675
}
660
676
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
661
680
/// - Parameter using: Optional Database to use
662
-
/// - Returns: An optional RequestError
663
-
664
-
internalstaticfunc 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
+
publicstaticfunc executeQuery(query:Query, parameters:[Any?]?=nil, using db:Database?=nil, _ onCompletion:@escaping(RequestError?)->Void){
0 commit comments