# How to use the type in the Mysql::Field object?

**URL:** <https://www.ruby-forum.com/t/how-to-use-the-type-in-the-mysql-field-object/158365>\
**Category:** Ruby\
**Created:** [January 22, 2009, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-use-the-type-in-the-mysql-field-object/158365 "2009-01-22T14:26:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [January 22, 2009, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-use-the-type-in-the-mysql-field-object/158365/1 "2009-01-22T14:26:42Z")

</div>

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)  
etc.

How can I translate the first part to something presentable? I cannot  
find any information on how to translate the integer values to something  
like FLOAT, SMALLINT, TIMESTAMP, etc.

Does anybody have some information about this?

---

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [January 22, 2009, 4:35pm UTC](https://www.ruby-forum.com/t/how-to-use-the-type-in-the-mysql-field-object/158365/2 "2009-01-22T16:35:41Z")

</div>

Patrick P. 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.

---

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [January 22, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/how-to-use-the-type-in-the-mysql-field-object/158365/3 "2009-01-22T17:38:35Z")

</div>

Thank you Alby!

Inspired by your reaction I’ve found the MYSQL\_TYPES:

[http://sequel.rubyforge.org/rdoc/classes/Mysql/Result.html](http://sequel.rubyforge.org/rdoc/classes/Mysql/Result.html)

I have to admit I could not find a file like mysql.c on my system… ☹  
It’s Fedora Core 5. I remember that I had to add this library because it  
was not in the standard installation.

My problem is solved however.

---

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [January 25, 2009, 12:02am UTC](https://www.ruby-forum.com/t/how-to-use-the-type-in-the-mysql-field-object/158365/4 "2009-01-25T00:02:13Z")

</div>

Albert S. wrote:

> Patrick P. wrote:
> 
> > I have to admit I could not find a file like mysql.c on my system… ☹
> 
> I was refering to your Ruby extension’s .c code. On my system it’s:
> 
> /usr/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.c

And by “these constants are defined in some MySQL header file” I was  
refering to MySQL’s own header files. The constants are defined in:

/usr/include/mysql/mysql\_com.h

---

<div class="post-metadata">

**Author:** ![pjpatten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pjpatten](https://www.ruby-forum.com/u/pjpatten)\
**Post date:** [January 25, 2009, 12:00am UTC](https://www.ruby-forum.com/t/how-to-use-the-type-in-the-mysql-field-object/158365/5 "2009-01-25T00:00:04Z")

</div>

Patrick P. wrote:

> I have to admit I could not find a file like mysql.c on my system… ☹

I was refering to your Ruby extension’s .c code. On my system it’s:

/usr/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.c
