On 2/19/06, ngw <nicholas_wieland / noyahoospam.it> wrote:

> I mean, I can resolve the problem by using a "state" variable inside a
> block and turn it to true if it finds the table, but I guess there's a
> more appropriate way to handle this kind of problems.

Using Ruport as it is in the svn trunk (should be released today, i'd
do something like this:
-----------------------------

require "ruport"

Ruport::Config.source :default,
  :dsn => "dbi:mysql:my_dbname", :user => "myuser", :password => "pass"

table = "some_table_i'm_looking for"

show_query = Ruport::Query.new("show tables")
creation_query = Ruport::Query.new("create table .... ")

exit if show_query.result.any? { |r| r[0].eql? table }
creation_query.execute

-------------------------

To demonstrate this, here it is in irb below.
The same approach can be taken with any enumerable result set (Including DBI's)

irb(main):001:0> require "ruport"
=> true

irb(main):002:0> Ruport::Config.source :default,
irb(main):003:0* :dsn => "dbi:mysql:bills_development", :user => "root"
=> nil

irb(main):004:0> show_tables = Ruport::Query.new "show tables"
=> #<Ruport::Query:0xb778678c @cached_data=nil, @raw_data=nil,
@password=nil, @statements=["show tables"], @user="root",
@dsn="dbi:mysql:bills_development", @sql="show tables",
@cache_enabled=nil>

irb(main):005:0> puts show_tables.result
fields: ( Tables_in_bills_development )
row0: ( users )
=> nil

irb(main):006:0> table = "apple"
=> "apple"

irb(main):007:0> show_tables.result.any? { |r| r[0].eql? table }
=> false

irb(main):008:0> Ruport::Query.new("create table #{table}").execute
=> nil

irb(main):009:0> show_tables.result.any? { |r| r[0].eql? table }
=> false

irb(main):013:0> Ruport::Query.new("create table #{table} ( x int );").execute
=> nil

irb(main):014:0> show_tables.result.any? { |r| r[0].eql? table }
=> true