Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 2 query method equivalent #53

Closed
Persilos opened this issue Sep 22, 2015 · 5 comments
Closed

Swift 2 query method equivalent #53

Persilos opened this issue Sep 22, 2015 · 5 comments

Comments

@Persilos
Copy link

Hi,
I used the query(string) method to make a generic method for select data.
With the update of Squeal for Swift2, i cant use this method.

var rows = try db.query(request)
for row in rows{
//Treat row
}

I dont find equivalent of this...

Regards

@nerdyc
Copy link
Owner

nerdyc commented Sep 22, 2015

for-loop support was removed to make handling errors more compatible with swift 2 errors. Instead of a for-loop, you use the next() method:

var results = try db.prepareStatement("SELECT * FROM people")
while try results.next() {
  var row = results.dictionaryValue
  // or whatever you want to do with the row
}

Since it's common to simply transform a row into some object value, you can also use the selectFrom method on the Database object, or the select method from the compiled statement. Pass these methods a block, and they will return an array of all treated rows. No need to call next() yourself.

@Persilos
Copy link
Author

Thank you for your response.
I have a second question: if in example you posted, i want to get values of dictionary in a String array
So i do:
var arrayValues = Array(row.values)
But values are Bindable. I search documentation to know how cast it in String (all values from my database are String) from Bindable but i dont find it.

@ghost
Copy link

ghost commented Sep 24, 2015

@Persilos, Bindable is just a protocol, so the underlying value may be any value that implements the Bindable protocol. If you know for sure that all the Bindable values are strings, you can do:
var arrayValues = row.values.map({ $0 as String })
That will simply cast all the values to strings. If you don't know for sure that all values are strings, you can do:
var arrayValues = row.values.map({ $0 as? String })
In that case you are dealing with optional values and you need keep that in mind as you use them.

These functions are not specific to Squeal, but because Squeal returns Bindable values, you need handle using the returned data correctly.

Hope that helps.

@nerdyc
Copy link
Owner

nerdyc commented Sep 24, 2015

There are also methods that take blocks, prefixed with select, that are likely more efficient. And the Statement object supports subscripting by column name or index too.

Something like:

let emails = try db.selectFrom("people") { $0["email"] as String }

// equivalent with a statement
let results = try db.prepareStatement("SELECT * FROM people")
let moreEmails = try results.select { $0["email"] as String }

@nerdyc nerdyc closed this as completed Sep 24, 2015
@Persilos
Copy link
Author

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants