Skip to content

Add DB::ResultSet#table_name(index : Int32) : String #175

@matthewmcgarvey

Description

@matthewmcgarvey

I'm working on an API that allows fetching data from the ResultSet by name, but with only the column_name method it breaks if there are overlapping column names due to joins.

I've been looking into java's sql stuff lately and found that this is a method on their result set metadata (doc link).

I've also been looking at implementations of this method and I know this won't exactly be easy. Well, it will be easy for sqlite probably. Here's the java adapter's implementation for it. On the other hand, Postgres doesn't have this functionality and so the pgjdbc library issues a query for the information (link). I did not look into any databases other than that.

We could even make it nilable so that implementations don't have to provide table name if they don't want to at first.

rs = db.query("SELECT * FROM users")
rs.each do |row|
  row.column_count.times do
    idx = row.next_column_index
    puts "#{row.table_name(idx)}.#{row.column_name(idx)} => #{row.read}"
  end
end
# => "users.id => 1"
# => "users.name => billy"
# => "users.role => admin"
# => "users.id => 2"
# => "users.name => jennifer"
# => "users.role => superuser"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions