| 1 | package DBIx::SQLHandler; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 6 | ▶ | use Abstract::Meta::Class ':all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 6 | $VERSION = 0.03; | |||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 NAME | ||||||||||||||
| 12 | |||||||||||||||
| 13 | DBIx::SQLHandler - Sql statement handler. | ||||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||
| 16 | |||||||||||||||
| 17 | use DBIx::SQLHandler | ||||||||||||||
| 18 | my $sql_handler = new DBIx::SQLHandler( | ||||||||||||||
| 19 | name => 'emp_ins', | ||||||||||||||
| 20 | connection => $connection, | ||||||||||||||
| 21 | sql => "INSERT INTO emp(empno, ename) VALUES(?, ?)" | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | $sql_handler->execute(1, 'Smith'); | ||||||||||||||
| 24 | $sql_handler->execute(2, 'Witek'); | ||||||||||||||
| 25 | |||||||||||||||
| 26 | |||||||||||||||
| 27 | my $sql_handler = new DBIx::SQLHandler( | ||||||||||||||
| 28 | name => 'emp_upd', | ||||||||||||||
| 29 | connection => $connection, | ||||||||||||||
| 30 | sql => "UPDATE emp SET ename = ? WHERE empno = ?" | ||||||||||||||
| 31 | ); | ||||||||||||||
| 32 | $sql_handler->execute('Smith2',1); | ||||||||||||||
| 33 | |||||||||||||||
| 34 | or | ||||||||||||||
| 35 | |||||||||||||||
| 36 | use DBIx::Connection; | ||||||||||||||
| 37 | |||||||||||||||
| 38 | ... | ||||||||||||||
| 39 | |||||||||||||||
| 40 | my $sql_handler = $connection->sql_handler( | ||||||||||||||
| 41 | name => 'emp_ins' | ||||||||||||||
| 42 | sql => "INSERT INTO emp(empno, ename) VALUES(?, ?)", | ||||||||||||||
| 43 | ); | ||||||||||||||
| 44 | $sql_handler->execute(1, 'Smith'); | ||||||||||||||
| 45 | |||||||||||||||
| 46 | |||||||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||||||
| 48 | |||||||||||||||
| 49 | Represents wrapper for sql statments. | ||||||||||||||
| 50 | It manages sql statement's information within its state. | ||||||||||||||
| 51 | If an error occurs on any stage (prepare, execute, binding) you will get full information | ||||||||||||||
| 52 | including connection name, sql, binding variables. | ||||||||||||||
| 53 | Caching statements (if using name). | ||||||||||||||
| 54 | It gathers performance statistics (optionaly). | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =head1 EXPORT | ||||||||||||||
| 57 | |||||||||||||||
| 58 | None. | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =head2 ATTRIBUTES | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =over | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =item name | ||||||||||||||
| 65 | |||||||||||||||
| 66 | Statement name. | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =cut | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 6 | has '$.name'; | |||||||||||||
| 71 | |||||||||||||||
| 72 | =item connection | ||||||||||||||
| 73 | |||||||||||||||
| 74 | Database connection. | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =cut | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 6 | has '$.connection' => (associated_class => 'DBIx::Connection'); | |||||||||||||
| 79 | |||||||||||||||
| 80 | |||||||||||||||
| 81 | =item sth | ||||||||||||||
| 82 | |||||||||||||||
| 83 | Cursor handler. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =cut | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 6 | has '$.sth'; | |||||||||||||
| 88 | |||||||||||||||
| 89 | |||||||||||||||
| 90 | =item sql | ||||||||||||||
| 91 | |||||||||||||||
| 92 | SQL text for cursor. | ||||||||||||||
| 93 | |||||||||||||||
| 94 | =cut | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 6 | has '$.sql'; | |||||||||||||
| 97 | |||||||||||||||
| 98 | |||||||||||||||
| 99 | =back | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =head2 METHODS | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =over | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =item initialise | ||||||||||||||
| 106 | |||||||||||||||
| 107 | Prepares the sql statement. | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =cut | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | ▶ | sub initialise { | ||||||||||||
|
Subroutine
initialise: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 112 | 0 | my ($self) = @_; | |||||||||||||
| 113 | 0 | $self->prepare(); | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | |||||||||||||||
| 117 | =item prepare | ||||||||||||||
| 118 | |||||||||||||||
| 119 | Prepare the statement. | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =cut | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | ▶ | sub prepare { | ||||||||||||
|
Subroutine
prepare: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 124 | 0 | my ($self) = @_; | |||||||||||||
| 125 | 0 | my $connection = $self->connection; | |||||||||||||
| 126 | 0 | $connection->record_action_start_time; | |||||||||||||
| 127 | 0 | ▶ | my $sth = $connection->dbh->prepare($self->sql) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | or $self->error_handler; | ||||||||||||||
| 129 | 0 | $connection->record_action_end_time($self->sql); | |||||||||||||
| 130 | 0 | $self->set_sth($sth); | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | |||||||||||||||
| 134 | =item execute | ||||||||||||||
| 135 | |||||||||||||||
| 136 | Executes the statement. | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =cut | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 0 | ▶ | sub execute { | ||||||||||||
|
Subroutine
execute: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||||
| 142 | 0 | my $connection = $self->connection; | |||||||||||||
| 143 | 0 | my $sth = $self->sth; | |||||||||||||
| 144 | 0 | $connection->record_action_start_time; | |||||||||||||
| 145 | 0 | ▶ | $sth->execute(@_) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | or $self->error_handler(\@_); | ||||||||||||||
| 147 | 0 | $connection->record_action_end_time($self->sql); | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | |||||||||||||||
| 151 | =item bind_columns | ||||||||||||||
| 152 | |||||||||||||||
| 153 | Binds column to the statement. | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =cut | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 0 | ▶ | sub bind_columns { | ||||||||||||
|
Subroutine
bind_columns: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 158 | 0 | my ($self, $bind_results) = @_; | |||||||||||||
| 159 | 0 | ▶ | if ($bind_results) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 0 | my $sth = $self->sth; | |||||||||||||
| 161 | $sth->bind_columns( | ||||||||||||||
| 162 | ref($bind_results) eq 'HASH' | ||||||||||||||
| 163 | 0 | ▶ | ? \(@{$bind_results}{@{$sth->{NAME_lc}}}) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | : ref($bind_results) eq 'ARRAY' | ||||||||||||||
| 165 | 0 | ▶ | ? \(@{$bind_results}[0..$#{$sth->{NAME_lc}}]) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 166 | : () | ||||||||||||||
| 167 | ) or $self->error_handler; | ||||||||||||||
| 168 | } | ||||||||||||||
| 169 | } | ||||||||||||||
| 170 | |||||||||||||||
| 171 | |||||||||||||||
| 172 | =item bind_params_inout | ||||||||||||||
| 173 | |||||||||||||||
| 174 | Bind parameters to the statement. | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =cut | ||||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | ▶ | sub bind_params_inout { | ||||||||||||
|
Subroutine
bind_params_inout: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 179 | 0 | my ($self, $params) = @_; | |||||||||||||
| 180 | 0 | my $sth = $self->sth; | |||||||||||||
| 181 | 0 | ▶ | eval { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 182 | $sth->bind_param_inout(":".$_, \$params->{$_}, 32000) | ||||||||||||||
| 183 | 0 | for keys %$params; | |||||||||||||
| 184 | 0 | $self; | |||||||||||||
| 185 | } or $self->error_handler; | ||||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | |||||||||||||||
| 189 | =item bind_params | ||||||||||||||
| 190 | |||||||||||||||
| 191 | Bind parameters to the statement. | ||||||||||||||
| 192 | |||||||||||||||
| 193 | =cut | ||||||||||||||
| 194 | |||||||||||||||
| 195 | 0 | ▶ | sub bind_params { | ||||||||||||
|
Subroutine
bind_params: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 196 | 0 | my ($self, $params) = @_; | |||||||||||||
| 197 | 0 | my $sth = $self->sth; | |||||||||||||
| 198 | 0 | ▶ | eval { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 199 | $sth->bind_param(":".$_, $params->{$_}) | ||||||||||||||
| 200 | 0 | for keys %$params; | |||||||||||||
| 201 | 0 | $self; | |||||||||||||
| 202 | } or $self->error_handler; | ||||||||||||||
| 203 | } | ||||||||||||||
| 204 | |||||||||||||||
| 205 | |||||||||||||||
| 206 | |||||||||||||||
| 207 | =item error_handler | ||||||||||||||
| 208 | |||||||||||||||
| 209 | Returns error messagem, takes error message, and optionally bind variables. | ||||||||||||||
| 210 | If bind variables are passed in the sql's place holders are replaced with the bind_variables. | ||||||||||||||
| 211 | |||||||||||||||
| 212 | =cut | ||||||||||||||
| 213 | |||||||||||||||
| 214 | 0 | ▶ | sub error_handler { | ||||||||||||
|
Subroutine
error_handler: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 215 | 0 | my ($self, $bind_params) = @_; | |||||||||||||
| 216 | 0 | my $connection = $self->connection; | |||||||||||||
| 217 | 0 | my $sql = $self->sql; | |||||||||||||
| 218 | 0 | ▶ | if (defined($bind_params)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | $sql =~ s/\?/'$_'/ | ||||||||||||||
| 220 | 0 | ▶ | for map {ref($_) eq 'CODE' ? $_->() : defined $_ ? $_ : ''} @$bind_params | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 221 | } | ||||||||||||||
| 222 | 0 | $connection->error_handler($sql); | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | |||||||||||||||
| 225 | |||||||||||||||
| 226 | =item cleanup | ||||||||||||||
| 227 | |||||||||||||||
| 228 | =cut | ||||||||||||||
| 229 | |||||||||||||||
| 230 | 0 | ▶ | sub cleanup { | ||||||||||||
|
Subroutine
cleanup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 231 | 0 | my ($self) = @_; | |||||||||||||
| 232 | 0 | $self->finish; | |||||||||||||
| 233 | 0 | $self->set_sth; | |||||||||||||
| 234 | 0 | $self->set_dbh; | |||||||||||||
| 235 | } | ||||||||||||||
| 236 | |||||||||||||||
| 237 | 6 | 1; | |||||||||||||
| 238 | |||||||||||||||
| 239 | __END__ | ||||||||||||||