Converting rows into structs

I have a methods that converts all DB rows into structs using Struct
class.
First version was:

res - result objects from DB connector

def create_structs( res )
row_class = Struct.new( ‘SQLRow’, *res.names )
data = []
res.each do |row|
data << row_class.new( row )
end
data
end

res1 = conn.exec( <<-SQL )
SELECT
name,
surname
FROM
user
SQL
data1 = create_structs( res1 )

here data1[0] has methods ‘name’ and ‘surname’

res2 = conn.exec( <<-SQL )
SELECT
age
FROM
user
SQL
data2 = create_structs( res2 )

here both data1 & data2 have method ‘age’ only

it is due to redefinition of Struct::SQLRow methods

I would like to rewrite method create_structs to avoid this problem.

···


Best regards,
Eugene [team Enticla] mailto:Eugene.Scripnik@itgrp.net

Can I ask what the advantage of storing it as a Struct is?

In all my Ruby/DBI code, I do something like this:

def select(sql)
    sth = $dbh.prepare(sql)
    sth.execute
    results = []
    sth.fetch_hash { |row| results << row.dup }
    sth.finish
    results
end

Is there any advantage to having it be a Struct instead of an
Array of Hash’es?

– Dossy

···

On 2002.08.23, Eugene Scripnik Eugene.Scripnik@itgrp.net wrote:

I have a methods that converts all DB rows into structs using Struct
class.


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

I have a methods that converts all DB rows into structs using Struct
class. First version was:

res - result objects from DB connector

def create_structs( res )
row_class = Struct.new( ‘SQLRow’, *res.names )
data =
res.each do |row|
data << row_class.new( row )
end
data
end

(…)

it is due to redefinition of Struct::SQLRow methods

I would like to rewrite method create_structs to avoid this problem.

If you pass a name as the first parameter to Struct.new, those
names have to be unique. You can solve your problem by passing
nil as the first argument to Struct.new:

  row_class = Struct.new( nil, *res.names )

I suppose you don’t refer to the name “SQLRow” anywhere else in
your program, do you?

Regards,
Pit

···

On 23 Aug 2002, at 17:56, Eugene Scripnik wrote:

Friday, August 23, 2002, 2:19:53 PM, you wrote:

Can I ask what the advantage of storing it as a Struct is?

In all my Ruby/DBI code, I do something like this:

def select(sql)
    sth = $dbh.prepare(sql)
    sth.execute
    results = []
    sth.fetch_hash { |row| results << row.dup }
    sth.finish
    results
end

Is there any advantage to having it be a Struct instead of an
Array of Hash’es?
I think there is no advantage, but I really like this syntax:
people.each { |man| puts “#{man.name} #{man.surname}” }

This one is not so nice, isn’t it?
people.each { |man| puts “#{man[‘name’]} #{man[‘surname’]}” }

···


Best regards,
Eugene [team Enticla] mailto:Eugene.Scripnik@itgrp.net

class Hash
def method_missing(*args)
self[args[0].to_s]
end
end

:wink:

– Dossy

···

On 2002.08.23, Eugene Scripnik Eugene.Scripnik@itgrp.net wrote:

Friday, August 23, 2002, 2:19:53 PM, you wrote:

Can I ask what the advantage of storing it as a Struct is?

In all my Ruby/DBI code, I do something like this:

def select(sql)
    sth = $dbh.prepare(sql)
    sth.execute
    results = []
    sth.fetch_hash { |row| results << row.dup }
    sth.finish
    results
end

Is there any advantage to having it be a Struct instead of an
Array of Hash’es?
I think there is no advantage, but I really like this syntax:
people.each { |man| puts “#{man.name} #{man.surname}” }

This one is not so nice, isn’t it?
people.each { |man| puts “#{man[‘name’]} #{man[‘surname’]}” }


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Friday, August 23, 2002, 2:52:04 PM, you wrote:

Is there any advantage to having it be a Struct instead of an
Array of Hash’es?
I think there is no advantage, but I really like this syntax:
people.each { |man| puts “#{man.name} #{man.surname}” }

This one is not so nice, isn’t it?
people.each { |man| puts “#{man[‘name’]} #{man[‘surname’]}” }

class Hash
  def method_missing(*args)
    self[args[0].to_s]
  end
end

:wink:
You can easily misspell method name (man.sername) with method_missing
and will not notice this. In this case better way to use structs as
for me.

···


Best regards,
Eugene [team Enticla] mailto:Eugene.Scripnik@itgrp.net

You can easily misspell method name (man.sername) with method_missing
and will not notice this. In this case better way to use structs as
for me.

Well, in this case you can use anonymous struct

   row_class = Struct.new(nil, *res.names)

ruby will create a new class for each call, or if you want named struct,
something like this (not tested)

   @structname = 'SQLRow00'

   def find_struct(*names)
      ObjectSpace.each_object(Class) do |x|
         if x < Struct && /SQLRow\d\d\Z/ =~ x.to_s && x.members == names
            return x
         end
      end
      Struct.new(@structname.succ!, *names)
   end

   def create_struct(res)
      row_class = find_struct(*res.names)
      # ...
   end

Guy Decoux

class Hash
def method_missing(id, *args, &block)
if has_key?(id.to_s)
self(id.to_s)
else
super
end
end
end

Massimiliano

···

On Fri, Aug 23, 2002 at 09:16:29PM +0900, Eugene Scripnik wrote:

class Hash
  def method_missing(*args)
    self[args[0].to_s]
  end
end

You can easily misspell method name (man.sername) with method_missing
and will not notice this.

Friday, August 23, 2002, 3:26:42 PM, you wrote:

You can easily misspell method name (man.sername) with method_missing
and will not notice this. In this case better way to use structs as
for me.

Well, in this case you can use anonymous struct

row_class = Struct.new(nil, *res.names)

ruby will create a new class for each call, or if you want named struct,
something like this (not tested)
Yes, this is exactly what I want. Thank you.

···


Best regards,
Eugene [team Enticla] mailto:Eugene.Scripnik@itgrp.net