The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::PObject::Driver::mysql - MySQL Pobject Driver

SYNOPSIS

    use Class::PObject;
    pobject Person => {
        columns => ['id', 'name', 'email'],
        driver  => 'mysql',
        datasource => {
            DSN => 'dbi:mysql:db_name',
            User => 'sherzodr',
            Password => 'marley01'
        }
    };

DESCRIPTION

Class::PObject::Driver::mysql is a direct subclass of Class::PObjecet::Driver::DBI. It inherits all the base functionality needed for all the DBI-related classes. For details of these methods and their specifications refer to Class::PObject::Driver and Class::PObject::Driver::DBI.

DATASOURCE

datasource attribute should be in the form of a hashref. The following keys are supported

  • DSN - provides a DSN string suitable for the first argument of DBI->connect(...). Usually it should be dbi:mysql:$database_name.

  • User - username to connect to the database.

  • Password - password required by the User to connect to the database. If the user doesn't require any passwords, you can set it to undef.

  • Table - defines the name of the table that objects will be stored in. If this is missing will default to the name of the object, non-alphanumeric characters replaced with underscore (_).

  • Handle attribute is useful if you already have $dbh handy. If $dbh is used, DSN, User and Password attributes will be obsolete nor make sense.

METHODS

Class::PObject::Driver::mysql (re-)defines following methods of its own

  • dbh() base DBI method is overridden with the version that creates a DBI handle using DSN datasource attribute.

  • save() - stores/updates the object

  • _prepare_insert() redefines base method of the same name with the version that generates a REPLACE SQL statement instead of default INSERT SQL statement. This allows the driver to either leave "insert or update?" problem to MySQL.

    This implies that table's id column should be of AUTO_INCREMENT type. This will ensure that MySQL will take care of creating auto-incrementing unique object ids for you.

NOTES

If the table is detected to be missing in the database, it will attempt to create proper table for you. To have more control over how it creates this table, you can fill-in column types using tmap argument.

SEE ALSO

Class::PObject, Class::PObject::Driver::csv, Class::PObject::Driver::file

COPYRIGHT AND LICENSE

For author and copyright information refer to Class::PObject's online manual.