How do I cleanly extract MySQL enum values in Perl?
Asked Answered
P

3

6

I have some code which needs to ensure some data is in a mysql enum prior to insertion in the database. The cleanest way I've found of doing this is the following code:

sub enum_values {
    my ( $self, $schema, $table, $column ) = @_;

    # don't eval to let the error bubble up
    my $columns = $schema->storage->dbh->selectrow_hashref(
        "SHOW COLUMNS FROM `$table` like ?",
        {},
        $column
    );

    unless ($columns) {
        X::Internal::Database::UnknownColumn->throw(
            column => $column,
            table  => $table,
        );
    }

    my $type = $columns->{Type} or X::Panic->throw(
        details => "Could not determine type for $table.$column",
    );

    unless ( $type =~ /\Aenum\((.*)\)\z/ ) {
        X::Internal::Database::IncorrectTypeForColumn->throw(
            type_wanted => 'enum',
            type_found  => $type,
        );
    }
    $type = $1;

    require Text::CSV_XS;
    my $csv = Text::CSV_XS->new;
    $csv->parse($type) or X::Panic->throw(
        details => "Could not parse enum CSV data: ".$csv->error_input,
    );
    return map { /\A'(.*)'\z/; $1 }$csv->fields;
}

We're using DBIx::Class. Surely there is a better way of accomplishing this? (Note that the $table variable is coming from our code, not from any external source. Thus, no security issue).

Postboy answered 23/10, 2008 at 10:11 Comment(0)
Z
13

No need to be so heroic. Using a reasonably modern version of DBD::mysql, the hash returned by DBI's column info method contains a pre-split version of the valid enum values in the key mysql_values:

my $sth = $dbh->column_info(undef, undef, 'mytable', '%');

foreach my $col_info ($sth->fetchrow_hashref)
{
  if($col_info->{'TYPE_NAME'} eq 'ENUM')
  {
    # The mysql_values key contains a reference to an array of valid enum values
    print "Valid enum values for $col_info->{'COLUMN_NAME'}: ", 
          join(', ', @{$col_info->{'mysql_values'}}), "\n";
  }
  ...
}
Zoroastrian answered 23/10, 2008 at 12:40 Comment(3)
Nice :-). Someone should document that in an obvious place though.Ber
FWIW, I pulled that answer out of Rose::DB::Object, which will introspect and automatically configure MySQL enums, Postgres array columns, and many other such types. Its code is a good source of answers when the DBD::* docs fall short.Zoroastrian
Ugh. Upgrading from DBD::mysql 3.006 to the latest version resulted added 35 minutes to our acceptance test run (up from 50 minutes).Postboy
B
3

I'd say using Text::CSV_XS may be an overkill, unless you have weird things like commas in enums (a bad idea anyway if you ask me). I'd probably use this instead.

my @fields = $type =~ / ' ([^']+) ' (?:,|\z) /msgx;

Other than that, I don't think there are shortcuts.

Ber answered 23/10, 2008 at 10:41 Comment(2)
We do have pretty strict constraints that we try to follow about naming conventions, so that seems like a nice simplification. Thanks!Postboy
One small correction though: it will handle a comma in the enum, it won't handle an apostrophe though.Ber
E
0

I spent part of the day asking the #dbix-class channel over on MagNet the same question and came across this lack of answer. Since I found the answer and nobody else seems to have done so yet, I'll paste the transcript below the TL;DR here:

my $cfg = new Config::Simple( $rc_file );
my $mysql = $cfg->get_block('mysql');
my $dsn =
  "DBI:mysql:database=$mysql->{database};".
  "host=$mysql->{hostname};port=$mysql->{port}";

my $schema  =
  DTSS::CDN::Schema->connect( $dsn, $mysql->{user}, $mysql->{password} );

my $valid_enum_values =
  $schema->source('Cdnurl')->column_info('scheme')->{extra}->{list};

And now the IRC log of me beating my head against a wall:

14:40 < cj> is there a cross-platform way to get the valid values of an 
            enum?
15:11 < cj> it looks like I could add 'InflateColumn::Object::Enum' to the 
            __PACKAGE__->load_components(...) list for tables with enum 
            columns
15:12 < cj> and then call values() on the enum column
15:13 < cj> but how do I get dbic-dump to add 
            'InflateColumn::Object::Enum' to 
            __PACKAGE__->load_components(...) for only tables with enum 
            columns?
15:20 < cj> I guess I could just add it for all tables, since I'm doing 
            the same for InflateColumn::DateTime
15:39 < cj> hurm... is there a way to get a column without making a 
            request to the db?
15:40 < cj> I know that we store in the DTSS::CDN::Schema::Result::Cdnurl 
            class all of the information that I need to know about the 
            scheme column before any request is issued
15:42 <@ilmari> cj: for Pg and mysql Schema::Loader will add the list of 
                valid values to the ->{extra}->{list} column attribute
15:43 <@ilmari> cj: if you're using some other database that has enums, 
                patches welcome :)
15:43 <@ilmari> or even just a link to the documentation on how to extract 
                the values
15:43 <@ilmari> and a willingness to test if it's not a database I have 
                access to
15:43 < cj> thanks, but I'm using mysql.  if I were using sqlite for this 
            project, I'd probably oblige :-)
15:44 <@ilmari> cj: to add components to only some tables, use 
                result_components_map
15:44 < cj> and is there a way to get at those attributes without making a 
            query?
15:45 < cj> can we do $schema->resultset('Cdnurl') without having it issue 
            a query, for instance?
15:45 <@ilmari> $result_source->column_info('colname')->{extra}->{list}
15:45 < cj> and $result_source is $schema->resultset('Cdnurl') ?
15:45 <@ilmari> dbic never issues a query until you start retrieving the 
                results
15:45 < cj> oh, nice.
15:46 <@ilmari> $schema->source('Cdnurl')
15:46 <@ilmari> the result source is where the result set gets the results 
                from when they are needed
15:47 <@ilmari> names have meanings :)
Emptor answered 27/2, 2015 at 2:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.