Skip to content

Commit 7655931

Browse files
authored
Update FAQ.md (#562)
* Update FAQ.md Updated several inaccuracies, per Issue #561 * fix typo
1 parent e16fb4e commit 7655931

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

FAQ.md

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -207,48 +207,46 @@ Or do a `Database#prepare` to get the `Statement`, and then use either
207207
stmt.bind_params( "value", "name" => "bob" )
208208
```
209209

210-
## How do I discover metadata about a query?
210+
## How do I discover metadata about a query result?
211211

212-
If you ever want to know the names or types of the columns in a result
213-
set, you can do it in several ways.
212+
IMPORTANT: `Database#execute` returns an Array of Array of Strings
213+
which will have no metadata about the query or the result, such
214+
as column names.
214215

215216

216-
The first way is to ask the row object itself. Each row will have a
217-
property "fields" that returns an array of the column names. The row
218-
will also have a property "types" that returns an array of the column
219-
types:
217+
There are 2 main sources of query metadata:
220218

221-
222-
```ruby
223-
rows = db.execute( "select * from table" )
224-
p rows[0].fields
225-
p rows[0].types
226-
```
219+
* `Statement`
220+
* `ResultSet`
227221

228222

229-
Obviously, this approach requires you to execute a statement that actually
230-
returns data. If you don't know if the statement will return any rows, but
231-
you still need the metadata, you can use `Database#query` and ask the
232-
`ResultSet` object itself:
223+
You can get a `Statement` via `Database#prepare`, and you can get
224+
a `ResultSet` via `Statement#execute` or `Database#query`.
233225

234226

235227
```ruby
236-
db.query( "select * from table" ) do |result|
237-
p result.columns
238-
p result.types
239-
...
240-
end
241-
```
242-
243-
244-
Lastly, you can use `Database#prepare` and ask the `Statement` object what
245-
the metadata are:
246-
247-
248-
```ruby
249-
stmt = db.prepare( "select * from table" )
250-
p stmt.columns
251-
p stmt.types
228+
sql = 'select * from table'
229+
230+
# No metadata
231+
rows = db.execute(sql)
232+
rows.class # => Array, no metadata
233+
rows.first.class # => Array, no metadata
234+
rows.first.first.class #=> String, no metadata
235+
236+
# Statement has metadata
237+
stmt = db.prepare(sql)
238+
stmt.columns # => [ ... ]
239+
stmt.types # => [ ... ]
240+
241+
# ResultSet has metadata
242+
results = stmt.execute
243+
results.columns # => [ ... ]
244+
results.types # => [ ... ]
245+
246+
# ResultSet has metadata
247+
results = db.query(sql)
248+
results.columns # => [ ... ]
249+
results.types # => [ ... ]
252250
```
253251

254252
## I'd like the rows to be indexible by column name.
@@ -273,7 +271,18 @@ is unavailable on the row, although the "types" property remains.)
273271
```
274272

275273

276-
The other way is to use Ara Howard's
274+
A more granular way to do this is via `ResultSet#next_hash` or
275+
`ResultSet#each_hash`.
276+
277+
278+
```ruby
279+
results = db.query( "select * from table" )
280+
row = results.next_hash
281+
p row['column1']
282+
```
283+
284+
285+
Another way is to use Ara Howard's
277286
[`ArrayFields`](http://rubyforge.org/projects/arrayfields)
278287
module. Just `require "arrayfields"`, and all of your rows will be indexable
279288
by column name, even though they are still arrays!

0 commit comments

Comments
 (0)