PObject/Driver/mysql.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 153 CC 12 SCAR 33.0
CC
12
Cov
0%
dbh
56.0
_tablename
30.0
save
6.0
CRAP 27.0
1 package Class::PObject::Driver::mysql;
2  
3 # mysql.pm,v 1.25 2003/12/12 05:17:37 sherzodr Exp
4  
5 use strict;
6 #use diagnostics;
7 use Log::Agent;
8 use vars ('@ISA', '$VERSION');
9 require Class::PObject::Driver::DBI;
10  
11 @ISA = ('Class::PObject::Driver::DBI');
12 $VERSION = '2.01';
13  
14 #
15 # overriding _prepare_insert() with the version that uses REPLACE
16 # statement of MySQL instead of usual INSERT
17 #
18 sub _prepare_insert {
19 my ($self, $table_name, $columns) = @_;
20  
21 my ($sql, @fields, @bind_params);
22 $sql = "REPLACE INTO $table_name SET ";
23 while ( my ($k, $v) = each %$columns ) {
24 push @fields, "`$k`=?";
25 push @bind_params, $v
26 }
27 $sql .= join(", ", @fields);
28 return ($sql, \@bind_params)
29 }
30  
31  
32  
33  
34  
35 sub save {
36 my ($self, $object_name, $props, $columns) = @_;
37
38 my $dbh = $self->dbh($object_name, $props) or return;
39 my $table = $self->_tablename($object_name, $props, $dbh) or return;
40 my ($sql, $bind_params) = $self->_prepare_insert($table, $columns);
41  
42 $self->_write_lock($dbh, $table) or return undef;
43 my $sth = $dbh->prepare( $sql );
44 unless ( $sth->execute(@$bind_params) ) {
45 $self->errstr("couldn't save/update the record: " . $sth->errstr);
46 logerr $self->errstr;
47 return undef
48 }
49 $self->_unlock($dbh) or return undef;
50 return $dbh->{mysql_insertid} || $dbh->{insertid}
51 }
52  
53  
54  
55  
56  
57  
58  
59  
60  
61 sub dbh {
62 my ($self, $object_name, $props) = @_;
63  
64 # checking if datasource provides adequate information to us
65 my $datasource = $props->{datasource};
66 unless ( ref($datasource) eq 'HASH' ) {
67 $self->errstr("'datasource' is invalid");
68 return undef
69 }
70 if ( defined $props->{datasource}->{Handle} ) {
71 return $props->{datasource}->{Handle}
72 }
73 my $stashed_name = $props->{datasource}->{DSN};
74 if ( defined $self->stash($stashed_name) ) {
75 return $self->stash($stashed_name)
76 }
77 if ( $datasource->{Handle} ) {
78 $self->stash($stashed_name, $datasource->{Handle});
79 return $datasource->{Handle}
80 }
81 my $dsn = $datasource->{DSN};
82 my $db_user = $datasource->{User} || $datasource->{UserName};
83 my $db_pass = $datasource->{Password};
84 unless ( $dsn ) {
85 $self->errstr("'DSN' is missing in 'datasource'");
86 return undef
87 }
88 require DBI;
89 my $dbh = DBI->connect($dsn, $db_user, $db_pass, {RaiseError=>1, PrintError=>0});
90 unless ( defined $dbh ) {
91 $self->errstr("couldn't connect to 'DSN': " . $DBI::errstr);
92 return undef
93 }
94 $dbh->{FetchHashKeyName} = 'NAME_lc';
95 $self->stash($stashed_name, $dbh);
96 $self->stash('close', 1);
97 return $dbh
98 }
99  
100  
101  
102 sub _tablename {
103 my $self = shift;
104 my ($object_name, $props, $dbh) = @_;
105  
106 unless ( defined $dbh ) {
107 $dbh = $self->dbh( $object_name, $props )
108 }
109 my $tablename = $self->SUPER::_tablename(@_);
110 my $exists= 0;
111 # if table by this name already exists in the database,
112 # no reason to go any further.Return the name
113 my $stashed = sprintf "exists:%s-%s", $dbh->{Name}, $tablename;
114 if ( $self->stash($stashed) ) {
115 logtrc 3, "'$stashed' was present. Table exists";
116 return $tablename
117 }
118
119 # if we come this far, we're still not sure if the database
120 # exists or not. so we peek into the catalog.
121 my %tables = map { s/\W//g; $_, 1 } $dbh->tables;
122 logtrc 3, "Retrieved %d tables from the database", scalar keys %tables;
123 if ( $tables{ $tablename } ) {
124 logtrc 3, "Table '%s' exists", $tablename;
125 $self->stash($stashed, 1);
126 return $tablename
127 }
128  
129 my $sql = $self->_prepare_create_table($object_name, $tablename);
130 unless ( $dbh->do( $sql ) ) {
131 $self->errstr( $dbh->errstr );
132 return undef
133 }
134 $self->stash($stashed, 1);
135 return $tablename
136 }
137  
138  
139  
140 sub _prepare_create_table {
141 my ($self, $object_name, $tablename) = @_;
142  
143 my $sql = $self->SUPER::_prepare_create_table($object_name, $tablename);
144 $sql =~ s/(id +INTEGER +PRIMARY +KEY)/$1 AUTO_INCREMENT/i;
145 return $sql
146 }
147  
148  
149  
150  
151  
152 1;
153 __END__;