| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XAO::DO::FS::Glue::Connect_DBI - DBI/DBD base for XAO::FS drivers | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use XAO::Objects; | ||
| 8 | use base XAO::Objects->load(objname => 'FS::Glue::SQL_DBI'); | ||
| 9 | |||
| 10 | sub foo { | ||
| 11 | my $self=shift; | ||
| 12 | $self->sql_connect( | ||
| 13 | dsn => 'DBI:mysql:test_fs', | ||
| 14 | user => 'test', | ||
| 15 | password => 'test', | ||
| 16 | ); | ||
| 17 | } | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This module provides a base for all XAO::FS SQL drivers that choose to | ||
| 22 | be based on DBI/DBD foundation. | ||
| 23 | |||
| 24 | =head1 METHODS | ||
| 25 | |||
| 26 | =over | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | ############################################################################### | ||
| 31 | package XAO::DO::FS::Glue::Connect_DBI; | ||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | use XAO::Objects; | ||
| 35 | use XAO::Utils; | ||
| 36 | use Encode; | ||
| 37 | use DBI; | ||
| 38 | |||
| 39 | use base XAO::Objects->load(objname => 'FS::Glue::Connect_SQL'); | ||
| 40 | |||
| 41 | our $VERSION='2.003'; | ||
| 42 | |||
| 43 | ############################################################################### | ||
| 44 | |||
| 45 | =item sql_connect (%) | ||
| 46 | |||
| 47 | Establishes a connection to the database engine given. Arguments are: | ||
| 48 | |||
| 49 | dsn => standard DBI data source | ||
| 50 | user => optional user name | ||
| 51 | password => optional password | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub sql_connect ($%) { | ||
| 56 | my $self=shift; | ||
| 57 | my $args=get_args(\@_); | ||
| 58 | |||
| 59 | if(%$args) { | ||
| 60 | $self->{'dsn'}=$args->{'dsn'} || throw $self "- no 'dsn' given"; | ||
| 61 | $self->{'user'}=$args->{'user'}; | ||
| 62 | $self->{'password'}=$args->{'password'}; | ||
| 63 | $self->{'driver'}=lc($args->{'driver'} // ''); | ||
| 64 | } | ||
| 65 | |||
| 66 | if(!$self->{'driver'}) { | ||
| 67 | $self->{'dsn'}=~/DBI:(\w+):/i || throw $self "- invalid dsn '$self->{'dsn'}' and no driver"; | ||
| 68 | $self->{'driver'}=lc($1); | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->{'sql'}=DBI->connect( | ||
| 72 | $self->{'dsn'}, | ||
| 73 | $self->{'user'}, | ||
| 74 | $self->{'password'}, | ||
| 75 | ) || throw $self "- can't connect to dsn='$self->{'dsn'}'"; | ||
| 76 | } | ||
| 77 | |||
| 78 | ############################################################################### | ||
| 79 | |||
| 80 | =item sql_connected () | ||
| 81 | |||
| 82 | Checks and returns true if the database connection is currently | ||
| 83 | established. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub sql_connected ($) { | ||
| 88 | my $self=shift; | ||
| 89 | ### dprint "Sql_connected: ".join('|',caller(1)); | ||
| 90 | |||
| 91 | return undef unless $self->{'sql'}; | ||
| 92 | |||
| 93 | my $rc=eval { $self->{'sql'}->do('select 1') }; | ||
| 94 | |||
| 95 | return $rc ? 1 : 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | ############################################################################### | ||
| 99 | |||
| 100 | =item sql_disconnect () | ||
| 101 | |||
| 102 | Closes connection to the database. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub sql_disconnect ($) { | ||
| 107 | my $self=shift; | ||
| 108 | |||
| 109 | $self->{'sql'}->disconnect if $self->{'sql'}; | ||
| 110 | $self->{'sql'}=undef; | ||
| 111 | } | ||
| 112 | |||
| 113 | ############################################################################### | ||
| 114 | |||
| 115 | =item sql_do ($;@) | ||
| 116 | |||
| 117 | Sends a single query to the database with no anticipation of any | ||
| 118 | results. Can take arguments just the way prepare/execute do. | ||
| 119 | |||
| 120 | It is allowed to call sql_do() with an array reference in the second | ||
| 121 | argument. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub sql_do ($$;@) { | ||
| 126 | my ($self,$query,@values)=@_; | ||
| 127 | |||
| 128 | ### dprint "SQL: $query"; | ||
| 129 | ### use Data::Dumper; | ||
| 130 | |||
| 131 | if(!@values) { | ||
| 132 | $self->{'sql'}->do($query) || | ||
| 133 | throw $self "- SQL error: ".$self->{'sql'}->errstr; | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | @values=@{$values[0]} if ref $values[0]; | ||
| 137 | ### dprint Dumper(\@values); | ||
| 138 | |||
| 139 | if($self->{'driver'} eq 'mysql') { | ||
| 140 | $self->{'sql'}->do($query,undef,@values) || | ||
| 141 | throw $self "- SQL error: ".$self->{'sql'}->errstr; | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | my $dbh=$self->{'sql'}; | ||
| 145 | my $sth=$dbh->prepare($query); | ||
| 146 | my $i=1; | ||
| 147 | foreach my $v (@values) { | ||
| 148 | $sth->bind_param($i++, $v, Encode::is_utf8($v) ? undef : DBI::SQL_BINARY); | ||
| 149 | } | ||
| 150 | $sth->execute() || | ||
| 151 | throw $self "- SQL error: ".$self->{'sql'}->errstr; | ||
| 152 | $sth->finish(); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub sql_do_no_error ($$;@) { | ||
| 158 | my ($self,$query)=@_; | ||
| 159 | |||
| 160 | ### dprint "Sql_do_no_error($query): ".join('|',caller(1)); | ||
| 161 | |||
| 162 | $self->{'sql'}->do($query); | ||
| 163 | |||
| 164 | ### dprint "Sql_do_no_error($query): DONE"; | ||
| 165 | } | ||
| 166 | |||
| 167 | ############################################################################### | ||
| 168 | |||
| 169 | =item sql_execute ($;@) | ||
| 170 | |||
| 171 | Executes a previously prepared statement optionally substituting some | ||
| 172 | values, see sql_prepare(). Example: | ||
| 173 | |||
| 174 | my $pq=$self->sql_prepare("SELECT a,b FROM c WHERE d=?"); | ||
| 175 | foreach my $value (1..10) { | ||
| 176 | $self->sql_execute($pq,$i); | ||
| 177 | ... | ||
| 178 | } | ||
| 179 | |||
| 180 | As a shortcut it can also accept a text query in the first argument | ||
| 181 | instead of manually calling sql_prepare() first. It is suggested to do | ||
| 182 | so whenever you plan to call sql_execute() just once. | ||
| 183 | |||
| 184 | It is allowed to call sql_execute() with an array reference in the | ||
| 185 | second argument. | ||
| 186 | |||
| 187 | Returns a piece of data that should be passed into sql_fetch_row() | ||
| 188 | method. | ||
| 189 | |||
| 190 | When done sql_finish() should be called with the return value of | ||
| 191 | sql_execute() as a parameter. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub sql_execute ($$;@) { | ||
| 196 | my ($self,$sth,@values)=@_; | ||
| 197 | |||
| 198 | $sth=$self->sql_prepare($sth) unless ref $sth; | ||
| 199 | |||
| 200 | ### use Data::Dumper; | ||
| 201 | ### dprint "Executed with: ".Dumper(@values && ref($values[0]) ? $values[0] : \@values); | ||
| 202 | |||
| 203 | if(@values) { | ||
| 204 | my $i=1; | ||
| 205 | foreach my $v (@values && ref($values[0]) ? @{$values[0]} : @values) { | ||
| 206 | $sth->bind_param($i++, $v, Encode::is_utf8($v) ? undef : DBI::SQL_BINARY); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | $sth->execute() || | ||
| 211 | throw $self "- SQL error: ".$sth->errstr; | ||
| 212 | |||
| 213 | return $sth; | ||
| 214 | } | ||
| 215 | |||
| 216 | ############################################################################### | ||
| 217 | |||
| 218 | =item sql_fetch_row ($) | ||
| 219 | |||
| 220 | Returns a reference to an array containing next retrieved row. Example: | ||
| 221 | |||
| 222 | my $qr=$self->sql_execute("SELECT a,b FROM c"); | ||
| 223 | my $row=$self->sql_fetch_row($qr); | ||
| 224 | |||
| 225 | Don't forget to call sql_finish() when you're done. | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub sql_fetch_row ($$) { | ||
| 230 | my ($self,$qr)=@_; | ||
| 231 | return $qr->fetchrow_arrayref; | ||
| 232 | } | ||
| 233 | |||
| 234 | ############################################################################### | ||
| 235 | |||
| 236 | =item sql_finish ($) | ||
| 237 | |||
| 238 | Frees up whatever internal structures might be occupied as a result of | ||
| 239 | the previous call to sql_execute(). Example: | ||
| 240 | |||
| 241 | my $qr=$self->sql_execute("SHOW TABLES"); | ||
| 242 | ... | ||
| 243 | $self->sql_finish($qr); | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | sub sql_finish ($$) { | ||
| 248 | my ($self,$qr)=@_; | ||
| 249 | $qr->finish; | ||
| 250 | } | ||
| 251 | |||
| 252 | ############################################################################### | ||
| 253 | |||
| 254 | =item sql_first_column ($) | ||
| 255 | |||
| 256 | An optimisation method -- returns a reference to the array containing | ||
| 257 | all first elements of each row of the results set. Example: | ||
| 258 | |||
| 259 | my $qr=$self->sql_execute("SELECT unique_id FROM a"); | ||
| 260 | my $ids=$self->sql_first_column($qr); | ||
| 261 | |||
| 262 | There is no need to call sql_finish() after sql_first_column(). | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub sql_first_column ($$) { | ||
| 267 | my ($self,$qr)=@_; | ||
| 268 | my @col; | ||
| 269 | while(my $row=$qr->fetchrow_arrayref) { | ||
| 270 | push @col,$row->[0]; | ||
| 271 | } | ||
| 272 | $qr->finish; | ||
| 273 | return \@col; | ||
| 274 | } | ||
| 275 | |||
| 276 | ############################################################################### | ||
| 277 | |||
| 278 | =item sql_first_row ($) | ||
| 279 | |||
| 280 | An optimisation method -- returns a reference to the first row and | ||
| 281 | finished the query. | ||
| 282 | |||
| 283 | my $qr=$self->sql_execute("SELECT a,b,c FROM t WHERE d=?",$uid); | ||
| 284 | my $row=$self->sql_first_row($qr); | ||
| 285 | |||
| 286 | There is no need to call sql_finish() after sql_first_row(). | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | ############################################################################### | ||
| 291 | |||
| 292 | =item sql_prepare ($) | ||
| 293 | |||
| 294 | Prepares a query for subsequent execution using sql_execute() | ||
| 295 | method. Guaranteed to return a reference of some sort. Example: | ||
| 296 | |||
| 297 | my $pq=$self->sql_prepare("SELECT a,b FROM c WHERE d=?"); | ||
| 298 | $self->sql_execute($pq,123); | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub sql_prepare ($$) { | ||
| 303 | my ($self,$query)=@_; | ||
| 304 | ### dprint "SQL_PREPARE: $query"; | ||
| 305 | return $self->{'sql'}->prepare($query) || | ||
| 306 | throw $self "- SQL error: ".$self->{'sql'}->errstr; | ||
| 307 | } | ||
| 308 | |||
| 309 | ############################################################################### | ||
| 310 | |||
| 311 | sub need_unlock_on_error ($) { | ||
| 312 | return 1; | ||
| 313 | } | ||
| 314 | |||
| 315 | ############################################################################### | ||
| 316 | 1; | ||
| 317 | __END__ |