From: Albert Schlef Date: 2009-01-23T00:34:10+09:00 Subject: Re: How to use the type in the Mysql::Field object? Patrick Put wrote: > I'm trying to do something like this (not really, I want to use the type > info instead of just displaying it...): > > fields = $db.list_fields(tablename).fetch_fields > fields.each do |field| > puts "Type = #{field.type} (#{field.type.class})" > end > > Result: > Type = 1 (Fixnum) > Type = 253 (Fixnum) > Type = 2 (Fixnum) > Type = 254 (Fixnum) These constants are defined in some MySQL header file (And perhaps MySQL's manual lists their numerical values in its C API section, I don't know). That Ruby extension you're using exports them as Ruby constants as well; e.g. Mysql::Field::TYPE_SHORT, Mysql::Field::TYPE_FLOAT, etc. Note that there are numerous Mysql Ruby extensions: DBI's, DataMapper's, ActiveRecord's. If you look at do_mysql's 'do_mysql_ext.c' (That's DataObjects's, which is DataMapper's), you'll find a useful infer_ruby_type() with a big case statemnt. Here it is: // Figures out what we should cast a given mysql field type to static VALUE infer_ruby_type(MYSQL_FIELD *field) { char* ruby_type; switch(field->type) { case MYSQL_TYPE_NULL: { ruby_type = NULL; break; } case MYSQL_TYPE_TINY: { ruby_type = "TrueClass"; break; } case MYSQL_TYPE_BIT: case MYSQL_TYPE_SHORT: case MYSQL_TYPE_LONG: case MYSQL_TYPE_INT24: case MYSQL_TYPE_LONGLONG: case MYSQL_TYPE_YEAR: { ruby_type = "Fixnum"; break; } case MYSQL_TYPE_DECIMAL: case MYSQL_TYPE_NEWDECIMAL: { ruby_type = "BigDecimal"; break; } case MYSQL_TYPE_FLOAT: case MYSQL_TYPE_DOUBLE: { ruby_type = "Float"; break; } case MYSQL_TYPE_TIMESTAMP: case MYSQL_TYPE_DATETIME: { ruby_type = "DateTime"; break; } case MYSQL_TYPE_TIME: { ruby_type = "DateTime"; break; } case MYSQL_TYPE_DATE: case MYSQL_TYPE_NEWDATE: { ruby_type = "Date"; break; } default: { ruby_type = "String"; break; } } return rb_str_new2(ruby_type); } Maybe your extension has a similar funcion. Look in its .c file. E.g., search for "_TYPE_DOUBLE" to arrive at the relevant places. -- Posted via http://www.ruby-forum.com/.