2017-07-01 5 views
0

Comment puis-je ajouter des clauses au générateur de requêtes Fluents à l'intérieur d'une boucle? Je veux juste faire une boucle dans un tableau de dictionnaire et ajouter les prédicats selon le contenu du dictionnaire. Par exemple, pour ce tableau:Ajout de groupes AND imbriqués dans un groupe OR à partir d'un tableau de dictionnaire dans Fluent (Swift)

var data: [[String: String]] = [ 
     [ 
       "name": "Joe", 
       "email": "[email protected]" 
     ], 
     [ 
       "name": "John", 
       "email": "[email protected]" 
     ] 
] 

je voudrais produire cette requête:

var contacts = try Contact.makeQuery().or { orGroup in 
     try orGroup.and { andGroup in 
      try andGroup.filter("name", "Joe") 
      try andGroup.filter("email", "[email protected]") 
     } 
     try orGroup.and { andGroup in 
      try andGroup.filter("name", "John") 
      try andGroup.filter("email", "[email protected]") 
     } 
}.all() 

Merci.

Répondre

0

Je voudrais simplement faire:

var contacts = try Contact.makeQuery().or { orGroup in 
    try data.forEach { inner in 
     guard let name = inner["name"], let email = inner["email"] else { 
      throw SomeError 
     } 
     try orGroup.and { andGroup in 
      try andGroup.filter("name", name) 
      try andGroup.filter("email", email) 
     } 
    } 
}.all()