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

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

Open
matthewmcgarvey opened this issue Nov 8, 2022 · 0 comments
Open

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

matthewmcgarvey opened this issue Nov 8, 2022 · 0 comments

Comments

@matthewmcgarvey
Copy link

matthewmcgarvey commented Nov 8, 2022

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"
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

1 participant