DBI/Handle/mysql.pm

filter: Statementstmt 28.2% 11 / 39 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 3 / 8 Podpod 100% 4 / 4 totaltotal 27.6% 18 / 65 CC 8 SCAR 24.8
CC
8
Cov
21%
insert
20.0
rename_column
20.0
_optimize_joins
6.0
CRAP 11.9
« DBI/Handle/Pg.pm ↑ Summary DBI/SchemaGenerator.pm »
1 package Jifty::DBI::Handle::mysql;
2 1 ▶use Jifty::DBI::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 @ISA = qw(Jifty::DBI::Handle);
4  
5 1 ▶use vars qw($VERSION @ISA $DBIHandle $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Jifty::DBI::Handle::mysql - A mysql specific Handle object
11  
12 =head1 SYNOPSIS
13  
14  
15 =head1 DESCRIPTION
16  
17 This module provides a subclass of L<Jifty::DBI::Handle> that
18 compensates for some of the idiosyncrasies of MySQL.
19  
20 =head1 METHODS
21  
22 =cut
23  
24 =head2 insert
25  
26 Takes a table name as the first argument and assumes that the rest of
27 the arguments are an array of key-value pairs to be inserted.
28  
29 If the insert succeeds, returns the id of the insert, otherwise,
30 returns a L<Class::ReturnValue> object with the error reported.
31  
32 =cut
33  
34 sub insert {
35 0 ▶ my $self = shift;
Subroutine insert: not called CC=4 SCAR=30.0
Pod documented
36  
37 0 my $sth = $self->SUPER::insert(@_);
38 0 ▶ if ( !$sth ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
39 0 return ($sth);
40 }
41  
42 0 $self->{'id'} = $self->dbh->{'mysql_insertid'};
43  
44 # Yay. we get to work around mysql_insertid being null some of the time :/
45 0 ▶ unless ( $self->{'id'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"id"}) 0 0 0
46 0 $self->{'id'} = $self->fetch_result('SELECT LAST_INSERT_ID()');
47 }
48 0 ▶ warn "$self no row id returned on row creation" unless ( $self->{'id'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"id"} 0 0 0
49  
50 0 return ( $self->{'id'} ); #Add Succeded. return the id
51 }
52  
53 =head2 database_version
54  
55 Returns the mysql version, trimming off any -foo identifier
56  
57 =cut
58  
59 sub database_version {
60 0 ▶ my $self = shift;
Subroutine database_version: not called CC=1 SCAR=6.9
Pod documented
61 0 my $v = $self->SUPER::database_version(@_);
62  
63 0 $v =~ s/\-.*$//;
64 0 return ($v);
65 }
66  
67 =head2 case_sensitive
68  
69 Returns undef, since mysql's searches are not case sensitive by default
70  
71 =cut
72  
73 sub case_sensitive {
74 0 ▶ my $self = shift;
Subroutine case_sensitive: not called CC=1 SCAR=6.9
Pod documented
75 0 return (undef);
76 }
77  
78 sub _optimize_joins {
79 0 ▶ my $self = shift;
Subroutine _optimize_joins: not called CC=2 SCAR=17.9
80 0 ▶ return $self->SUPER::_optimize_joins if $self->database_version =~ /^[34]/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->database_version =~ /^[34]/ 0 0 0
81 0 return;
82 }
83  
84 =head2 rename_column ( table => $table, column => $old_column, to => $new_column )
85  
86 rename column, die if fails
87  
88 =cut
89  
90 sub rename_column {
91 0 ▶ my $self = shift;
Subroutine rename_column: not called CC=4 SCAR=30.0
Pod documented
92 0 my %args = (
93 table => undef,
94 column => undef,
95 to => undef,
96 @_
97 );
98  
99 0 my ($table, $column, $to) = @args{'table', 'column', 'to'};
100  
101 # XXX, FIXME, TODO: this is stupid parser of CREATE TABLE, this should be something based on
102 # column_info, schema tables and show fields. The closest thing is RT 3.8/etc/upgrade/upgrade-mysql-schema.pl
103  
104 0 my $create_table = ($self->simple_query("SHOW CREATE TABLE $table")->fetchrow_array)[1];
105 0 ▶ $create_table =~ /create\s+table\s+\S+\s*\((.*)\)/ims
Branch: 1 branch
BranchTrueFalseTotal
$create_table =~ /create\s+table\s+\S+\s*\((.*)\)/ism or die "Cannot find 'CREATE TABLE' statement in schema for '${table}': $create_table" 0 0 0
106 or die "Cannot find 'CREATE TABLE' statement in schema for '$table': $create_table";
107 0 $create_table = $1;
108  
109 0 ▶ my ($column_info) = ($create_table =~ /`$column`(.*?)(?:,|$)/i)
Branch: 1 branch
BranchTrueFalseTotal
my($column_info) = $create_table =~ /`$column`(.*?)(?:,|$)/i or die "Cannot find column '${column}' in $create_table" 0 0 0
110 or die "Cannot find column '$column' in $create_table";
111 0 my $sth = $self->simple_query("ALTER TABLE $table CHANGE $column $to $column_info");
112 0 ▶ die "Cannot rename column '$column' in table '$table' to '$to': ". $self->dbh->errstr
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
113 unless $sth;
114 0 return $sth;
115 }
116  
117 1 1;
118  
119 __END__
120  
121 =head1 AUTHOR
122  
123 Jesse Vincent, jesse@fsck.com
124  
125 =head1 SEE ALSO
126  
127 L<Jifty::DBI>, L<Jifty::DBI::Handle>, L<DBD::mysql>
128  
129 =cut
130  
« DBI/Handle/Pg.pm ↑ Summary DBI/SchemaGenerator.pm »