Skip to content

Commit

Permalink
Update FAQ.md
Browse files Browse the repository at this point in the history
Updated several inaccuracies, per Issue sparklemotion#561
  • Loading branch information
rickhull authored Sep 17, 2024
1 parent 4968ca4 commit 612e5a2
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,48 +207,46 @@ Or do a `Database#prepare` to get the `Statement`, and then use either
stmt.bind_params( "value", "name" => "bob" )
```

## How do I discover metadata about a query?
## How do I discover metadata about a query result?

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


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


```ruby
rows = db.execute( "select * from table" )
p rows[0].fields
p rows[0].types
```
* `Statement`
* `ResultSet`


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


```ruby
db.query( "select * from table" ) do |result|
p result.columns
p result.types
...
end
```


Lastly, you can use `Database#prepare` and ask the `Statement` object what
the metadata are:


```ruby
stmt = db.prepare( "select * from table" )
p stmt.columns
p stmt.types
sql = 'select * from table'

# No metadata
rows = db.execute(sql)
rows.class # => Array, no metadata
rows.first.class # => Array, no metadata
rows.first.first.class #=> String, no metadata

# Statement has metadata
stmt = db.prepare(sql)
stmt.columns # => [ ... ]
stmt.type # => [ ... ]

# ResultSet has metadata
results = stmt.execute
results.columns # => [ ... ]
results.types # => [ ... ]

# ResultSet has metadata
results = db.query(sql)
results.columns # => [ ... ]
results.types # => [ ... ]
```

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


The other way is to use Ara Howard's
A more granular way to do this is via `ResultSet#next_hash` or
`ResultSet#each_hash`.


```ruby
results = db.query( "select * from table" )
row = results.next_hash
p row['column1']
```


Another way is to use Ara Howard's
[`ArrayFields`](http://rubyforge.org/projects/arrayfields)
module. Just `require "arrayfields"`, and all of your rows will be indexable
by column name, even though they are still arrays!
Expand Down

0 comments on commit 612e5a2

Please sign in to comment.