[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

mkmf and enum values

djberg96

2/9/2005 4:32:00 PM

Hi all,

What's the best way to tell if an enum member contains a particular
value?

My question in particular centers around the idtype_t enum, and I'm
writing some code that I want to make cross platform. On Solaris 9,
the enum contains values like P_ALL, P_CID, etc. I don't know that all
of these exist, or if they have slightly different names, on other
platforms. I think in older versions of Linux they were PS_, instead
of P_, for example.

With a struct, for example, I know you can do something like this:

have_struct_member("struct type","member","someheader.h")

However, I didn't see anything like a "have_enum_value".

Any suggestions?

Regards,

Dan

5 Answers

Barry Sperling

2/9/2005 5:20:00 PM

0

Hello,
I am using the Ruby DBI Module with Access on a Win 2K machine. I
haven't found a way to use a variable field name with the INSERT
statement. The "?" can be used for a placeholder in the VALUES
according to the manual written by Paul DuBois, and that works, but I
can't find a way to make it work with the field names.

I have tried the following inside of a loop with "indx" as the loop
variable:

name = $Names.index(indx) # THIS IS A HASH
rows = dbh_out.do( "INSERT INTO Tile (?) VALUES
(?)",name,$Sum_By_Line[indx])

and also:

sth = dbh_out.prepare("INSERT INTO Tile (?) VALUES (?)")
followed by the loop with:
sth.execute( name, $Sum_By_Line[indx] )

but they give an error which sounds like it won't allow the "?" as a
field name place holder:

The INSERT INTO statement contains the following unknown field name:
'Pa_RaM000'. Make sure you have typed the name correctly, and try the
operation again. (DBI::DatabaseError)
from e:/ruby/lib/ruby/site_ruby/1.8/dbi/dbi.rb:777:in `execute'

Is it possible to have a variable place holder for fields or do you
have to list each field explicitly?
Thanks,
Barry




Gavin Kistner

2/9/2005 5:36:00 PM

0

I'm on WinXP, using Ruby 1.8.2 with DBI to connect to a MSSQL2k DB via
ODBC DSN.

The following works for me:

LIVE_DB_DSN_LOGIN = [ 'dbi:ODBC:LiveServer', 'myusername',
'mypassword' ]

begin
require 'dbi'

dbh = DBI.connect( *LIVE_DB_DSN_LOGIN )

add_article = dbh.prepare( <<-ENDSQL
INSERT INTO tblArticles
( title, date_updated, author )
VALUES ( ?, #{Time.at(0).to_sql}, '#{AUTHOR}' )
ENDSQL
)

#...later

add_article.execute( title )


rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"


ensure
#close out statements
add_article.finish

#unplug from the database
dbh.disconnect

end

Barry Sperling

2/9/2005 5:54:00 PM

0

Thanks for the quick response, Gavin, but I don't see how you are making
the field names variables. The names "title", "date_updated" and
"author" seem to be the same with only the value of the title changing.
I would like to loop through a list of field names assigning something
to one field, and they something else to another field, which would
require a variable of some kind in the field section, not just the value
section. Any examples?
Barry


gavin@refinery.com wrote:

> I'm on WinXP, using Ruby 1.8.2 with DBI to connect to a MSSQL2k DB via
> ODBC DSN.
>
> The following works for me:
>
> LIVE_DB_DSN_LOGIN = [ 'dbi:ODBC:LiveServer', 'myusername',
> 'mypassword' ]
>
> begin
> require 'dbi'
>
> dbh = DBI.connect( *LIVE_DB_DSN_LOGIN )
>
> add_article = dbh.prepare( <<-ENDSQL
> INSERT INTO tblArticles
> ( title, date_updated, author )
> VALUES ( ?, #{Time.at(0).to_sql}, '#{AUTHOR}' )
> ENDSQL
> )
>
> #...later
>
> add_article.execute( title )
>
>
> rescue DBI::DatabaseError => e
> puts "An error occurred"
> puts "Error code: #{e.err}"
> puts "Error message: #{e.errstr}"
>
>
> ensure
> #close out statements
> add_article.finish
>
> #unplug from the database
> dbh.disconnect
>
> end
>
>



Gavin Kistner

2/9/2005 7:10:00 PM

0

I'm sorry, I completely failed to see the problem you were facing, so
sure was I that I had a solution :)

I can't see an easy solution myself. The problem, is that if you do
something like:

get_article = dbh.prepare( "SELECT ? FROM tblFoo" )
get_article.execute( 'aTitle' )

then DBI does its 'magic' of properly turning the values you pass to
execute into the a valid SQL representation of that item. For the
above, DBI creates the SQL equivalent of:

SELECT 'aTitle' FROM tblFoo
instead of
SELECT aTitle FROM tblFoo
or
SELECT [aTitle] FROM tblFoo

I don't see any way to get DBI to 'cast' your value from a string to a
column name. To reap the benefits of single-source location for your
statement, but not the statement preparation aspect (does Access even
support this?), you'll need to do some string substitution on your own.
(AFAICT)

nobu.nokada

2/10/2005 4:39:00 PM

0

Hi,

At Thu, 10 Feb 2005 01:35:11 +0900,
Daniel Berger wrote in [ruby-talk:130253]:
> What's the best way to tell if an enum member contains a particular
> value?

Enum values are used as just integers, so it would be difficult
to check if a symbol belongs to an enum.

> My question in particular centers around the idtype_t enum, and I'm
> writing some code that I want to make cross platform. On Solaris 9,
> the enum contains values like P_ALL, P_CID, etc. I don't know that all
> of these exist, or if they have slightly different names, on other
> platforms. I think in older versions of Linux they were PS_, instead
> of P_, for example.
>
> With a struct, for example, I know you can do something like this:
>
> have_struct_member("struct type","member","someheader.h")
>
> However, I didn't see anything like a "have_enum_value".

def have_enum_value(enum, val, headers = nil)
try_compile(cpp_include(headers)+"enum #{enum} conftest_val = #{val};")
end

--
Nobu Nakada