File Coverage

blib/lib/Hailo/Storage/MySQL.pm
Criterion Covered Total %
statement 14 28 50.0
branch 0 6 0.0
condition 0 4 0.0
subroutine 5 9 55.5
pod 0 1 0.0
total 19 48 39.5


line stmt bran cond sub pod time code
1             package Hailo::Storage::MySQL;
2             our $AUTHORITY = 'cpan:AVAR';
3             $Hailo::Storage::MySQL::VERSION = '0.75';
4 2     2   39109 use v5.10.0;
  2         7  
5 2     2   10 use Moose;
  2         4  
  2         14  
6 2     2   12750 use MooseX::StrictConstructor;
  2         3  
  2         15  
7 2     2   7064 use List::MoreUtils qw< all >;
  2         22934  
  2         18  
8 2     2   1879 use namespace::clean -except => 'meta';
  2         5  
  2         16  
9              
10             extends 'Hailo::Storage';
11             with qw(Hailo::Role::Arguments Hailo::Role::Storage);
12              
13 0     0     sub _build_dbd { return 'mysql' };
14              
15             override _build_dbd_options => sub {
16             return {
17             %{ super() },
18             mysql_enable_utf8 => 1,
19             };
20             };
21              
22             sub _build_dbi_options {
23 0     0     my ($self) = @_;
24 0           my $dbd = $self->dbd;
25 0           my $dbd_options = $self->dbd_options;
26 0           my $args = $self->arguments;
27              
28 0           my $conn_line = "dbi:$dbd";
29 0 0         $conn_line .= ":database=$args->{database}" if exists $args->{database};
30 0 0         $conn_line .= ";host=$args->{host}" if exists $args->{host};
31 0 0         $conn_line .= ";port=$args->{port}" if exists $args->{port};
32              
33             my @options = (
34             $conn_line,
35             ($args->{username} || ''),
36 0   0       ($args->{password} || ''),
      0        
37             $dbd_options,
38             );
39              
40 0           return \@options;
41             }
42              
43             sub ready {
44 0     0 0   my ($self) = @_;
45              
46 0     0     return all { exists $self->arguments->{$_} } qw(database username password);
  0            
47             }
48              
49             __PACKAGE__->meta->make_immutable;
50              
51             =encoding utf8
52              
53             =head1 NAME
54              
55             Hailo::Storage::MySQL - A storage backend for L<Hailo|Hailo> using
56             L<DBD::mysql|DBD::mysql>
57              
58             =head1 SYNOPSIS
59              
60             As a module:
61              
62             my $hailo = Hailo->new(
63             storage_class => 'mysql',
64             storage_args => {
65             database => 'hailo',
66             host => 'localhost',
67             port => '3306',
68             username => 'hailo',
69             password => 'hailo'
70             },
71             );
72             $hailo->train("hailo.trn");
73              
74             From the command line:
75              
76             hailo --train hailo.trn \
77             --storage mysql \
78             --storage-args database=hailo \
79             --storage-args host=localhost \
80             --storage-args port=3306 \
81             --storage-args username=hailo \
82             --storage-args password=hailo
83              
84             Almost all of these options can be omitted, see L<DBD::mysql's
85             documentation|DBD::mysql> for the default values.
86              
87             See L<Hailo's documentation|Hailo> for other non-MySQL specific options.
88              
89             =head1 DESCRIPTION
90              
91             This backend maintains information in a MySQL database.
92              
93             =head1 ATTRIBUTES
94              
95             =head2 C<storage_args>
96              
97             This is a hash reference which can have the following keys:
98              
99             B<'database'>, the name of the database to use (required).
100              
101             B<'host'>, the host to connect to (required).
102              
103             B<'port'>, the port to connect to (required).
104              
105             B<'username'>, the username to use.
106              
107             B<'password'>, the password to use.
108              
109             =head1 CAVEATS
110              
111             MySQL sucks.
112              
113             =head1 MySQL setup
114              
115             Before creating a database for Hailo you need to ensure that the
116             B<collation_connection>, B<collation_database> and B<collation_server>
117             for the new database will be equivalent, you can do this by adding
118             this to your C<[mysqld]> section in F<my.cnf>:
119              
120             skip-character-set-client-handshake
121             collation_server=utf8_unicode_ci
122             character_set_server=utf8
123              
124             Now when you create the database you should get something like this:
125              
126             mysql> show variables like 'coll%';
127             +----------------------+-----------------+
128             | Variable_name | Value |
129             +----------------------+-----------------+
130             | collation_connection | utf8_unicode_ci |
131             | collation_database | utf8_unicode_ci |
132             | collation_server | utf8_unicode_ci |
133             +----------------------+-----------------+
134              
135             If you instead get this:
136              
137             +----------------------+-------------------+
138             | Variable_name | Value |
139             +----------------------+-------------------+
140             | collation_connection | utf8_unicode_ci |
141             | collation_database | latin1_swedish_ci |
142             | collation_server | utf8_unicode_ci |
143             +----------------------+-------------------+
144              
145             Then Hailo will eventually die when you train it on an error similar
146             to this:
147              
148             DBD::mysql::st execute failed: Illegal mix of collations (latin1_swedish_ci,IMPLICIT)
149             and (utf8_unicode_ci,COERCIBLE) for operation '=' at [...]
150              
151             After taking care of that create a MySQL database for Hailo using
152             something like these commands:
153              
154             mysql -u root -p
155             CREATE DATABASE hailo;
156             GRANT USAGE ON *.* TO hailo@localhost IDENTIFIED BY 'hailo';
157             GRANT ALL ON hailo.* TO hailo@localhost IDENTIFIED BY 'hailo';
158             FLUSH PRIVILEGES;
159              
160             =head1 AUTHOR
161              
162             E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org>
163              
164             =head1 LICENSE AND COPYRIGHT
165              
166             Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason.
167              
168             This program is free software, you can redistribute it and/or modify
169             it under the same terms as Perl itself.
170              
171             =cut