Mojar-Mysql

 view release on metacpan or  search on metacpan

lib/Mojar/Mysql/Connector.pm  view on Meta::CPAN

# Class attribute

# Use a singleton object for holding use-time class defaults
have Defaults => sub { bless {} => ref $_[0] || $_[0] };

# Attributes

has quiesce_timeout => 500;

my @DbdFields = qw(RaiseError PrintError PrintWarn AutoCommit TraceLevel
    mysql_auto_reconnect mysql_enable_utf8);

has RaiseError => 1;
has PrintError => 0;
has PrintWarn => 0;
has AutoCommit => 1;
has TraceLevel => 0;
has mysql_auto_reconnect => 0;
has mysql_enable_utf8 => 1;

my @ConFields = qw(label cnfdir cnf cnfgroup);

has 'label';
has cnfdir => '.';
has 'cnf';
has 'cnfgroup';

my @DbiFields = qw(driver host port schema user password);

lib/Mojar/Mysql/Connector.pm  view on Meta::CPAN

 $dbh3 = Mojar::Mysql::Connector->connect;

Constructor for a connection (db handle).  If the first element passed has
prefix C<DBI:> then it is a DSN string (the traditional route) and so is passed
straight to C<DBI::connect> (L<DBI/"DBI Class Methods">).  Otherwise a DSN is
first constructed.  (The DSN tuple does not persist and is constructed fresh on
each call to C<connect>.)

In the examples above, $dbh1 and $dbh2 are not equivalent because the second
connector would also incorporate module defaults and use-time parameters, in
addition to the passed parameters.  So, for instance, mysql_enable_utf8 might be
included in the second connector.

=head2 C<dsn>

  @dbi_args = Mojar::Mysql::Connector->dsn(
    cnf => 'myuser_localhost', schema => 'test'
  );

A convenience method used internally by connect.  Takes a (possibly empty)
parameter hash.  Returns a four-element array to pass to C<DBI::connect>,

lib/Mojar/Mysql/Connector.pm  view on Meta::CPAN

of the connector's existing parameters.  Otherwise a DSN is first constructed.
(The DSN tuple does not persist and is constructed fresh on each call to
C<connect>.)

=head2 Attributes

All connector parameters are implemented as attributes with exactly the same
spelling.  So for example you can

  $connector->RaiseError(undef);  # disable RaiseError
  $connector->mysql_enable_utf8(0);  # disable mysql_enable_utf8

The attributes, with their coded defaults, are

  RaiseError => 1
  PrintError => 0
  PrintWarn => 0
  AutoCommit => 1
  TraceLevel => 0
  mysql_auto_reconnect => 0
  mysql_enable_utf8 => 1

  label
  cnfdir => '.'
  cnf
  cnfgroup

  driver => 'mysql'
  host
  port
  schema

lib/Mojar/Mysql/Connector.pm  view on Meta::CPAN


  SHOW TABLES

but excluding real tables.

=head1 CHARACTER ENCODINGS

To read/store characters encoded as non-ASCII, non-UTF8, you must disable
handling of UTF-8.

  $connector = Mojar::Mysql::Connector->new(mysql_enable_utf8 => 0);

This is essential, for example, when fetching high-latin (eg non-ASCII 8859-1)
characters.

=head1 DEBUGGING

You can enable DBI trace logging at use-time:

  use Mojar::Mysql::Connector (TraceLevel => '3|CON');

test/30-mysql-connector.t  view on Meta::CPAN

  is $mc0->port, 123, 'clone:port';
  ok $mc0->user('bart'), '->user()';
  is $mc0->user, 'bart', 'clone:user';
  is $mc->user, 'tester', 'orig:user';
};

subtest q{dsn} => sub {
  ok my $p = [ $mc->dsn ], 'object ->dsn';
  is_deeply $p, ['DBI:mysql:;port=123', 'tester', undef,
                 {AutoCommit => 1, PrintError => 0, PrintWarn => 0,
                  RaiseError => 0, TraceLevel => 0, mysql_enable_utf8 => 1,
                  mysql_auto_reconnect => 0}],
              'expected values';

  ok @$p = $mc->dsn(TraceLevel => 2), 'object ->dsn';
  is_deeply $p, ['DBI:mysql:;port=123', 'tester', undef,
                 {AutoCommit => 1, PrintError => 0, PrintWarn => 0,
                  RaiseError => 0, TraceLevel => 2, mysql_enable_utf8 => 1,
                  mysql_auto_reconnect => 0}],
              'expected values';
  is $mc->TraceLevel, 0, 'TraceLevel';
};

SKIP: {
  skip 'set TEST_MYSQL to enable this test (developer only!)', 2
    unless $ENV{TEST_MYSQL} || $ENV{TRAVIS};

my $dbh;



( run in 0.250 second using v1.01-cache-2.11-cpan-1dc43b0fbd2 )