blib/lib/Hailo/Storage/MySQL.pm

filter: Statementstmt 61.1% 22 / 36 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 6 / 10 Podpod 0.0% 0 / 1 totaltotal 47.4% 28 / 59 CC 6 SCAR 28.7
CC
6
Cov
48%
_build_dbi_options
42.0
_build_dbd
2.0
ready
2.0
CRAP 17.6
« blib/lib/Hailo/Engine/Default.pm ↑ Summary blib/lib/Hailo/Tokenizer/Words.pm »
1 package Hailo::Storage::MySQL;
2 2 our $AUTHORITY = 'cpan:AVAR';
3 2 $Hailo::Storage::MySQL::VERSION = '0.74';
4 2 ▶use v5.28.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use List::MoreUtils qw< all >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 extends 'Hailo::Storage';
11 2 with qw(Hailo::Role::Arguments Hailo::Role::Storage);
12  
13 0 ▶sub _build_dbd { return 'mysql' };
Subroutine _build_dbd: not called CC=1 SCAR=6.9
14  
15 override _build_dbd_options => sub {
16 return {
17 1 ▶ %{ super() },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 mysql_enable_utf8 => 1,
19 };
20 2 };
21  
22 sub _build_dbi_options {
23 0 ▶ my ($self) = @_;
Subroutine _build_dbi_options: not called CC=6 SCAR=37.4
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 ▶ $conn_line .= ":database=$args->{database}" if exists $args->{database};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"database"} 0 0 0
30 0 ▶ $conn_line .= ";host=$args->{host}" if exists $args->{host};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"host"} 0 0 0
31 0 ▶ $conn_line .= ";port=$args->{port}" if exists $args->{port};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"port"} 0 0 0
32  
33 my @options = (
34 $conn_line,
35 ($args->{username} || ''),
36 0 ▶ ($args->{password} || ''),
Condition0%0/4
$args->{'username'} || ''
l!l
00
$args->{'password'} || ''
l!l
00
MC/DC0%0/1
$args->{'username'} || ''
$args->{'username'}
MC/DC0%0/1
$args->{'password'} || ''
$args->{'password'}
Truth table0%0/2
A
A= $args->{'username'}
Aresult
11
00
Truth table0%0/2
A
A= $args->{'password'}
Aresult
11
00
37 $dbd_options,
38 );
39  
40 0 return \@options;
41 }
42  
43 sub ready {
44 0 ▶ my ($self) = @_;
Subroutine ready: not called CC=1 SCAR=6.9
Pod undocumented
45  
46 0 ▶ return all { exists $self->arguments->{$_} } qw(database username password);
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 }
48  
49 2 __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
« blib/lib/Hailo/Engine/Default.pm ↑ Summary blib/lib/Hailo/Tokenizer/Words.pm »