File Coverage

blib/lib/DBIx/Raw.pm
Criterion Covered Total %
statement 289 327 88.3
branch 90 124 72.5
condition 33 72 45.8
subroutine 32 37 86.4
pod 12 13 92.3
total 456 573 79.5


line stmt bran cond sub pod time code
1             package DBIx::Raw;
2              
3 71     71   166477 use 5.008_005;
  71         431  
4             our $VERSION = '0.22';
5              
6 71     71   31733 use strictures 2;
  71         114354  
  71         2996  
7 71     71   48221 use Moo;
  71         776294  
  71         367  
8 71     71   139464 use Types::Standard qw/Bool HashRef InstanceOf Str/;
  71         5203959  
  71         817  
9 71     71   190340 use DBI;
  71         1227279  
  71         5164  
10 71     71   37557 use Config::Any;
  71         630725  
  71         2568  
11 71     71   33187 use DBIx::Raw::Crypt;
  71         293  
  71         2388  
12 71     71   491 use Carp;
  71         1075  
  71         4276  
13 71     71   931 use List::Util qw/first/;
  71         154  
  71         6259  
14 71     71   31388 use Crypt::Mode::CBC::Easy;
  71         3335926  
  71         303558  
15              
16             #have an errors file to write to
17             has 'dsn' => is => 'rw';
18             has 'user' => is => 'rw';
19             has 'password' => is => 'rw';
20             has 'conf' => is => 'rw';
21             has 'prev_conf' => (
22             is => 'rw',
23             isa => Str,
24             default => '',
25             );
26              
27             has 'crypt' => (
28             is => 'ro',
29             isa => InstanceOf['Crypt::Mode::CBC::Easy'],
30             lazy => 1,
31             builder => sub {
32 26     26   578 my ($self) = @_;
33 26         743 return Crypt::Mode::CBC::Easy->new(key => $self->crypt_key);
34             },
35             );
36              
37             has 'crypt_key' => (
38             is => 'rw',
39             isa => Str,
40             lazy => 1,
41             builder => sub {
42 26     26   632 my $crypt_key_hex = 'aea77496999d37bf47aedff9c0d44fdf2d2bbfa848ee6652abe9891b43e0f331';
43 26         845 return pack "H*", $crypt_key_hex;
44             },
45             );
46              
47             has 'use_old_crypt' => (
48             is => 'rw',
49             isa => Bool,
50             );
51              
52             has 'old_crypt_key' => (
53             is => 'rw',
54             isa => Str,
55             lazy => 1,
56             default => '6883868834006296591264051568595813693328016796531185824375212916576042669669556288781800326542091901603033335703884439231366552922364658270813734165084102xfasdfa8823423sfasdfalkj!@#$$CCCFFF!09xxxxlai3847lol13234408!!@#$_+-083dxje380-=0'
57             );
58              
59             has 'old_crypt' => (
60             is => 'ro',
61             isa => InstanceOf['DBIx::Raw::Crypt'],
62             lazy => 1,
63             builder => sub {
64 0     0   0 my ($self) = @_;
65 0         0 return DBIx::Raw::Crypt->new( { secret => $self->old_crypt_key });
66             },
67             );
68              
69             # LAST STH USED
70             has 'sth' => is => 'rw';
71              
72             #find out what DBH is specifically
73             has 'dbh' => (
74             is => 'rw',
75             lazy => 1,
76             default => sub { shift->connect }
77             );
78              
79             has 'keys' => (
80             is => 'ro',
81             isa => HashRef[Str],
82             default => sub { {
83             query => 1,
84             vals => 1,
85             encrypt => 1,
86             decrypt => 1,
87             key => 1,
88             href => 1,
89             table => 1,
90             where => 1,
91             pk => 1,
92             rows => 1,
93             id => 1,
94             } },
95             );
96              
97             sub BUILD {
98 70     70 0 7486 my ($self) = @_;
99 70         2716 $self->_parse_conf;
100 70         6265 $self->_validate_connect_info;
101             }
102              
103             =head1 NAME
104              
105             DBIx::Raw - Maintain control of SQL queries while still having a layer of abstraction above DBI
106              
107             =head1 SYNOPSIS
108              
109             DBIx::Raw allows you to have complete control over your SQL, while still providing useful functionality so you don't have to deal directly with L.
110              
111             use DBIx::Raw;
112             my $db = DBIx::Raw->new(dsn => $dsn, user => $user, password => $password);
113              
114             #alternatively, use a conf file
115             my $db = DBIx::Raw->new(conf => '/path/to/conf.pl');
116              
117             #get single values in scalar context
118             my $name = $db->raw("SELECT name FROM people WHERE id=1");
119              
120             #get multiple values in list context
121             my ($name, $age) = $db->raw("SELECT name, age FROM people WHERE id=1");
122            
123             #or
124             my @person = $db->raw("SELECT name, age FROM people WHERE id=1");
125              
126             #get hash when using scalar context but requesting multiple values
127             my $person = $db->raw("SELECT name, age FROM people where id=1");
128             my $name = $person->{name};
129             my $age = $person->{age};
130              
131             #also get hash in scalar context when selecting multiple values using '*'
132             my $person = $db->raw("SELECT * FROM people where id=1");
133             my $name = $person->{name};
134             my $age = $person->{age};
135              
136             #insert a record
137             $db->raw("INSERT INTO people (name, age) VALUES ('Sally', 26)");
138              
139             #insert a record with bind values to help prevent SQL injection
140             $db->raw("INSERT INTO people (name, age) VALUES (?, ?)", 'Sally', 26);
141              
142             #update records
143             my $num_rows_updated = $db->raw("UPDATE people SET name='Joe',age=34 WHERE id=1");
144              
145             #use bind values to help prevent SQL injection
146             my $num_rows_updated = $db->raw("UPDATE people SET name=?,age=? WHERE id=?", 'Joe', 34, 1);
147              
148             #also use bind values when selecting
149             my $name = $db->raw("SELECT name FROM people WHERE id=?", 1);
150              
151             #get multiple records as an array of hashes
152             my $people = $db->aoh("SELECT name, age FROM people");
153            
154             for my $person (@$people) {
155             print "$person->{name} is $person->{age} years old\n";
156             }
157              
158             #update a record easily with a hash
159             my %update = (
160             name => 'Joe',
161             age => 34,
162             );
163              
164             #record with id=1 now has name=Joe an age=34
165             $db->update(href=>\%update, table => 'people', id=>1);
166              
167             #use alternate syntax to encrypt and decrypt data
168             my $num_rows_updated = $db->raw(query => "UPDATE people SET name=? WHERE id=1", vals => ['Joe'], encrypt => [0]);
169              
170             my $decrypted_name = $db->raw(query => "SELECT name FROM people WHERE id=1", decrypt => [0]);
171              
172             #when being returned a hash, use names of field for decryption
173             my $decrypted_person = $db->raw(query => "SELECT name, age FROM people WHERE id=1", decrypt => ['name']);
174             my $decrypted_name = $decrypted_person->{name};
175              
176              
177             =head1 INITIALIZATION
178              
179             There are three ways to intialize a L object:
180              
181             =head2 dsn, user, password
182              
183             You can initialize a L object by passing in the dsn, user, and password connection information.
184              
185             my $db = DBIx::Raw->new(dsn => 'dbi:mysql:test:localhost:3306', user => 'user', password => 'password');
186              
187             =head2 dbh
188              
189             You can also initialize a L object by passing in an existing database handle.
190              
191             my $db = DBIx::Raw->new(dbh => $dbh);
192              
193             =head2 conf
194              
195             If you're going to using the same connection information a lot, it's useful to store it in a configuration file and then
196             use that when creating a L object.
197              
198             my $db = DBIx::Raw->new(conf => '/path/to/conf.pl');
199              
200             See L for more information on how to set up a configuration file.
201              
202             =head1 CONFIGURATION FILE
203              
204             You can use a configuration file to store settings for L instead of passing them into new or setting them.
205             L uses L, so you can use any configuration format that is acceptable for L. Variables
206             that you might want to store in your configuration file are C, C, C, and L.
207              
208             Below is an example configuration file in perl format:
209              
210             =head2 conf.pl
211              
212             {
213             dsn => 'dbi:mysql:test:localhost:3306',
214             user => 'root',
215             password => 'password',
216             crypt_key => 'lxsafadsfadskl23239210453453802xxx02-487900-=+1!:)',
217             }
218              
219             =head2 conf.yaml
220              
221             ---
222             dsn: 'dbi:mysql:test:localhost:3306'
223             user: 'root'
224             password: 'password'
225             crypt_key: 'lxsafadsfadskl23239210453453802xxx02-487900-=+1!:)'
226              
227             Note that you do not need to include L if you just if you just want to use the file for configuration settings.
228              
229             =head1 SYNTAXES
230              
231             DBIx::Raw provides two different possible syntaxes when making queries.
232              
233             =head2 SIMPLE SYNTAX
234              
235             Simple syntax is an easy way to write queries. It is always in the format:
236              
237             ("QUERY");
238              
239             or
240              
241             ("QUERY", "VAL1", "VAL2", ...);
242              
243             Below are some examples:
244              
245             my $num_rows_updated = $db->raw("UPDATE people SET name='Fred'");
246              
247             my $name = $db->raw("SELECT name FROM people WHERE id=1");
248            
249             DBIx::Raw also supports L for L. These can be useful to help prevent SQL injection. Below are
250             some examples of how to use placeholders and bind values with L.
251              
252             my $num_rows_updated = $db->raw("UPDATE people SET name=?", 'Fred');
253              
254             my $name = $db->raw("SELECT name FROM people WHERE id=?", 1);
255              
256             $db->raw("INSERT INTO people (name, age) VALUES (?, ?)", 'Frank', 44);
257            
258             Note that L cannot be used for L, L, L, or L because of the extra parameters that they require.
259              
260             =head2 ADVANCED SYNTAX
261              
262             Advanced syntax is used whenever a subroutine requires extra parameters besides just the query and bind values, or whenever you need to use L
263             or L. A simple example of the advanced syntax is:
264              
265             my $num_rows_updated = $db->raw(query => "UPDATE people SET name='Fred'");
266              
267             This is equivalent to:
268              
269             my $num_rows_updated = $db->raw("UPDATE people SET name='Fred'");
270              
271             A slightly more complex example adds in bind values:
272              
273             my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?", vals => ['Fred']);
274              
275             This is equivalent to the simple syntax:
276              
277             my $num_rows_updated = $db->raw("UPDATE people SET name=?", 'Fred');
278              
279             Also, advanced syntax is required whenevery you want to L or L values.
280              
281             my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?", vals => ['Fred'], encrypt => [0]);
282              
283             my $decrypted_name = $db->raw(query => "SELECT name FROM people WHERE id=1", decrypt => [0]);
284              
285             Note that L is required for L, L, L, or L because of the extra parameters that they require.
286              
287             =head1 ENCRYPT AND DECRYPT
288              
289             You can use L to encrypt values when putting them into the database and decrypt values when removing them from the database.
290             Note that in order to store an encrypted value in the database, you should have the field be of type C or some type of character
291             or text field where the encryption will fit. In order to encrypt and decrypt your values, L requires a L. It contains a default
292             key, but it is recommended that you change it either by having a different one in your L file, or passing it in on creation with C or setting it using the
293             L method. It is recommended that you use a module like L to generate a secure key.
294             One thing to note is that both L and L require L.
295              
296             =head2 encrypt
297              
298             In order to encrypt values, the values that you want to encrypt must be in the bind values array reference that you pass into C. Note that for the values that you want to
299             encrypt, you should put their index into the encrypt array that you pass in. For example:
300              
301             my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?,age=?,height=? WHERE id=1", vals => ['Zoe', 24, "5'11"], encrypt => [0, 2]);
302              
303             In the above example, only C and C will be encrypted. You can easily encrypt all values by using '*', like so:
304              
305             my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?,height=? WHERE id=1", vals => ['Zoe', "5'11"], encrypt => '*');
306              
307             And this will encrypt both C and C.
308              
309             The only exception to the L syntax that is a little different is for L. See L for how to encrypt when using L.
310              
311             =head2 decrypt
312              
313             When decrypting values, there are two possible different syntaxes.
314              
315             =head3 DECRYPT LIST CONTEXT
316              
317             If your query is returning a single value or values in a list context, then the array reference that you pass in for decrypt will contain the indices for the
318             order that the columns were listed in. For instance:
319              
320             my $name = $db->raw(query => "SELECT name FROM people WHERE id=1", decrypt => [0]);
321              
322             my ($name, $age) = $db->raw(query => "SELECT name, age FROM people WHERE id=1", decrypt => [0,1]);
323              
324             =head3 DECRYPT HASH CONTEXT
325              
326             When your query has L return your values in a hash context, then the columns that you want decrypted must be listed by name in the array reference:
327              
328             my $person = $db->raw(query => "SELECT name, age FROM people WHERE id=1", decrypt => ['name', 'age'])
329              
330             my $aoh = $db->aoh(query => "SELECT name, age FROM people", decrypt => ['name', 'age']);
331              
332             Note that for either L or L, it is possible to use '*' to decrypt all columns:
333              
334             my ($name, $height) = $db->raw(query => "SELECT name, height FROM people WHERE id=1", decrypt => '*');
335              
336             =head2 crypt_key
337              
338             L uses L to encrypt and decrypt all values. You can set the crypt key when you create your
339             L object by passing it into L, providing it to L,
340             or by setting it with its setter method:
341              
342             $db->crypt_key("1234");
343              
344             It is strongly recommended that you do not use the default L. The L should be the appropriate length
345             for the L that is set. The default L uses L, which uses L, which
346             allows key sizes of 128/192/256 bits.
347              
348             =head2 crypt
349              
350             The L object to use for encryption. Default is the default L object
351             created with the key L.
352              
353             =head2 use_old_crypt
354              
355             In version 0.16 L started using L instead of L. Setting this to 1 uses the old encryption instead.
356             Make sure to set L if you previously used L for encryption.
357              
358             =head2 old_crypt_key
359              
360             This sets the crypt key to use if L is set to true. Default is the previous crypt key.
361              
362             =head1 SUBROUTINES/METHODS
363              
364             =head2 raw
365              
366             L is a very versitile subroutine, and it can be called in three contexts. L should only be used to make a query that
367             returns values for one record, or a query that returns no results (such as an INSERT query). If you need to have multiple
368             results returned, see one of the subroutines below.
369              
370             =head3 SCALAR CONTEXT
371              
372             L can be called in a scalar context to only return one value, or in a undef context to return no value. Below are some examples.
373              
374             #select
375             my $name = $db->raw("SELECT name FROM people WHERE id=1");
376              
377             #update with number of rows updated returned
378             my $num_rows_updated = $db->raw("UPDATE people SET name=? WHERE id=1", 'Frank');
379            
380             #update in undef context, nothing returned.
381             $db->raw("UPDATE people SET name=? WHERE id=1", 'Frank');
382              
383             #insert
384             $db->raw("INSERT INTO people (name, age) VALUES ('Jenny', 34)");
385              
386             Note that to L for L for L, you would use L.
387              
388             =head3 LIST CONTEXT
389              
390             L can also be called in a list context to return multiple columns for one row.
391              
392             my ($name, $age) = $db->raw("SELECT name, age FROM people WHERE id=1");
393              
394             #or
395             my @person = $db->raw("SELECT name, age FROM people WHERE id=1");
396              
397             Note that to L for L for L, you would use L.
398              
399             =head3 HASH CONTEXT
400              
401             L will return a hash if you are selecting more than one column for a single record.
402              
403             my $person = $db->raw("SELECT name, age FROM people WHERE id=1");
404             my $name = $person->{name};
405             my $age = $person->{age};
406              
407             Note that L's L works when using * in your query.
408              
409             my $person = $db->raw("SELECT * FROM people WHERE id=1");
410             my $name = $person->{name};
411             my $age = $person->{age};
412              
413             Note that to L for L for L, you would use L.
414             =cut
415              
416             sub raw {
417 282     282 1 9671 my $self = shift;
418              
419 282         1800 my $params = $self->_params(@_);
420              
421 282         780 my (@return_values, $return_type);
422 282 50       8267 $self->sth($self->dbh->prepare($params->{query})) or $self->_perish($params);
423              
424             #if user asked for values to be encrypted
425 282 100       1057834 if($params->{encrypt}) {
426 157         962 $self->_crypt_encrypt($params);
427             }
428              
429 282         1624 $self->_query($params);
430              
431 282 100       1547 if(not defined wantarray) {
    100          
432 224 50       2213 $self->sth->finish or $self->_perish($params);
433 224         2282 return;
434             }
435             elsif(wantarray) {
436 11         49 $return_type = 'array';
437             }
438             else {
439 47         213 $return_type = 'scalar';
440              
441 47 50       573 if($params->{query} =~ /SELECT\s+(.*?)\s+FROM/i) {
442 47         203 my $match = $1;
443 47         432 my $num_commas=()= $match =~ /,/g;
444 47         214 my $num_stars=()= $match =~ /\*/g;
445              
446 47 100 100     342 if($num_commas > 0 or $num_stars > 0) { $return_type = 'hash' }
  37         121  
447             }
448             }
449              
450 58 50 33     965 if($params->{query} =~ /^(\n*?| *?|\r*?)UPDATE /si) {
    50          
451 0         0 my $return_value = $self->sth->rows();
452 0         0 push @return_values, $return_value;
453             }
454             elsif(($params->{query} =~ /SELECT /sig) || ($params->{query} =~ /SHOW /sig)) {
455 58 50       544 unless($params->{query} =~ /INSERT INTO (.*?)SELECT /sig) {
456 58 100       217 if($return_type eq 'hash') {
457 37 100       1637 return unless $params->{href} = $self->sth->fetchrow_hashref; #handles undef case
458              
459 36 100       266 if($params->{decrypt}) {
460 8         42 $self->_crypt_decrypt($params);
461             }
462              
463 36         733 push @return_values, $params->{href};
464             }
465             else {
466 21 100       608 return unless @return_values = $self->sth->fetchrow_array(); #handles undef cases
467              
468 19 100       123 if($params->{decrypt}) {
469 5         15 $params->{return_values} = \@return_values;
470 5         33 $self->_crypt_decrypt($params);
471             }
472             }
473             }
474             }
475              
476 55 50       1322 $self->sth->finish or $self->_perish($params);
477              
478 55 100       299 unless($return_type eq 'array') {
479 45         347 return $return_values[0];
480             }
481             else {
482 10         77 return @return_values;
483             }
484             }
485              
486             =head2 aoh (array_of_hashes)
487              
488             L can be used to select multiple rows from the database. It returns an array reference of hashes, where each row is a hash in the array.
489              
490             my $people = $db->aoh("SELECT * FROM people");
491              
492             for my $person (@$people) {
493             print "$person->{name} is $person->{age} years old\n";
494             }
495              
496             Note that to L for L, you would use L.
497             =cut
498              
499             sub aoh {
500 4     4 1 84 my $self = shift;
501 4         17 my $params = $self->_params(@_);
502 4         11 my ($href,@a);
503              
504 4         24 $self->_query($params);
505              
506 4 100       24 if($params->{decrypt}) {
507 2         120 while($href=$self->sth->fetchrow_hashref){
508 4         22 $params->{href} = $href;
509 4         22 $self->_crypt_decrypt($params);
510 4         374 push @a, $href;
511             }
512             }
513             else {
514 2         121 while($href=$self->sth->fetchrow_hashref){
515 4         101 push @a, $href;
516             }
517             }
518              
519 4         36 return \@a;
520             }
521              
522             =head2 aoa (array_of_arrays)
523              
524             L can be used to select multiple rows from the database. It returns an array reference of array references, where each row is an array within the array.
525              
526             my $people = $db->aoa("SELECT name,age FROM people");
527              
528             for my $person (@$people) {
529             my $name = $person->[0];
530             my $age = $person->[1];
531             print "$name is $age years old\n";
532             }
533              
534             Note that to L for L, you would use L.
535             =cut
536              
537             sub aoa {
538 3     3 1 67 my $self = shift;
539 3         20 my $params = $self->_params(@_);
540 3         11 my (@return_values);
541              
542 3         18 $self->_query($params);
543              
544 3 100       19 if($params->{decrypt}) {
545 1         34 while(my @a=$self->sth->fetchrow_array){
546 2         13 $params->{return_values} = \@a;
547 2         16 $self->_crypt_decrypt($params);
548 2         188 push @return_values, \@a;
549             }
550             }
551             else {
552 2         60 while(my @a=$self->sth->fetchrow_array){
553 4         47 push @return_values, \@a;
554             }
555             }
556              
557 3         17 return \@return_values;
558             }
559              
560              
561              
562             =head2 hoh (hash_of_hashes)
563              
564             =over
565              
566             =item
567              
568             B - the query
569              
570             =item
571              
572             B - the name of the column that will serve as the key to access each row
573              
574             =item
575              
576             B - the hash reference that you would like to have the results added to
577              
578             =back
579              
580             L can be used when you want to be able to access an individual row behind a unique key, where each row is represented as a hash. For instance,
581             this subroutine can be useful if you would like to be able to access rows by their id in the database. L returns a hash reference of hash references.
582              
583             my $people = $db->hoh(query => "SELECT id, name, age FROM people", key => "id");
584              
585             for my $key(keys %$people) {
586             my $person = $people->{$key};
587             print "$person->{name} is $person->{age} years old\n";
588             }
589              
590             #or
591             while(my ($key, $person) = each %$people) {
592             print "$person->{name} is $person->{age} years old\n";
593             }
594              
595             So if you wanted to access the person with an id of 1, you could do so like this:
596              
597             my $person1 = $people->{1};
598             my $person1_name = $person1->{name};
599             my $person1_age = $person1->{age};
600              
601             Also, with L it is possible to add to a previous hash of hashes that you alread have by passing it in with the C key:
602              
603             #$people was previously retrieved, and results will now be added to $people
604             $db->hoh(query => "SELECT id, name, age FROM people", key => "id", href => $people);
605              
606             Note that you must select whatever column you want to be the key. So if you want to use "id" as the key, then you must select id in your query.
607             Also, keys must be unique or the records will overwrite one another. To retrieve multiple records and access them by the same key, see L<"hoaoh (hash_of_array_of_hashes)"/hoaoh>.
608             To L for L, you would use L.
609              
610             =cut
611              
612             sub hoh {
613 3     3 1 103 my $self = shift;
614 3         13 my $params = $self->_params(@_);
615 3         9 my ($href);
616              
617 3         15 my $hoh = $params->{href}; #if hashref is passed in, it will just add to it
618              
619 3         23 $self->_query($params);
620              
621 3 100       21 if($params->{decrypt}) {
622 1         57 while($href=$self->sth->fetchrow_hashref){
623 2         14 $params->{href} = $href;
624 2         15 $self->_crypt_decrypt($params);
625 2         229 $hoh->{$href->{$params->{key}}} = $href;
626             }
627             }
628             else {
629 2         120 while($href=$self->sth->fetchrow_hashref){
630 4         112 $hoh->{$href->{$params->{key}}} = $href;
631             }
632             }
633              
634 3         26 return $hoh;
635             }
636              
637             =head2 hoa (hash_of_arrays)
638              
639             =over
640              
641             =item
642              
643             B - the query
644              
645             =item
646              
647             B - the name of the column that will serve as the key to store the values behind
648              
649             =item
650              
651             B - the name of the column whose values you want to be stored behind key
652              
653             =item
654              
655             B - the hash reference that you would like to have the results added to
656              
657             =back
658              
659             L is useful when you want to store a list of values for one column behind a key. For instance,
660             say that you wanted the id's of all people who have the same name grouped together. You could perform that query like so:
661              
662             my $hoa = $db->hoa(query => "SELECT id, name FROM people", key => "name", val => "id");
663              
664             for my $name (%$hoa) {
665             my $ids = $hoa->{$name};
666              
667             print "$name has ids ";
668             for my $id (@$ids) {
669             print " $id,";
670             }
671              
672             print "\n";
673             }
674              
675             Note that you must select whatever column you want to be the key. So if you want to use "name" as the key, then you must select name in your query.
676             To L for L, you would use L.
677              
678             =cut
679              
680             sub hoa {
681 3     3 1 82 my $self = shift;
682 3         15 my $params = $self->_params(@_);
683 3         9 my ($href);
684              
685 3 50 33     68 croak "query, key, and val are required for hoa" unless $params->{query} and $params->{key} and $params->{val};
      33        
686              
687 3         10 my $hash = $params->{href}; #if hash is passed in, it will just add to it
688              
689 3         13 $self->_query($params);
690              
691 3 100       18 if($params->{decrypt}) {
692 1         67 while($href=$self->sth->fetchrow_hashref){
693 4         18 $params->{href} = $href;
694 4         14 $self->_crypt_decrypt($params);
695 4         374 push @{$hash->{$href->{$params->{key}}}}, $href->{$params->{val}};
  4         82  
696             }
697             }
698             else {
699 2         116 while($href=$self->sth->fetchrow_hashref){
700 8         24 push @{$hash->{$href->{$params->{key}}}}, $href->{$params->{val}};
  8         102  
701             }
702             }
703              
704 3         18 return $hash;
705             }
706              
707             =head2 hoaoh (hash_of_array_of_hashes)
708              
709             =over
710              
711             =item
712              
713             B - the query
714              
715             =item
716              
717             B - the name of the column that will serve as the key to store the array of hashes behind
718              
719             =item
720              
721             B - the hash reference that you would like to have the results added to
722              
723             =back
724              
725             L can be used when you want to store multiple rows behind a key that they all have in common. For
726             example, say that we wanted to have access to all rows for people that have the same name. That could be
727             done like so:
728              
729             my $hoaoh = $db->hoaoh(query => "SELECT id, name, age FROM people", key => "name");
730              
731             for my $name (keys %$hoaoh) {
732             my $people = $hoaoh->{$name};
733              
734             print "People named $name: ";
735             for my $person (@$people) {
736             print " $person->{name} is $person->{age} years old\n";
737             }
738              
739             print "\n";
740             }
741              
742             So to get the array of rows for all people named Fred, we could simply do:
743              
744             my @freds = $hoaoh->{Fred};
745              
746             for my $fred (@freds) { ... }
747              
748             Note that you must select whatever column you want to be the key. So if you want to use "name" as the key, then you must select name in your query.
749             To L for L, you would use L.
750              
751             =cut
752              
753             sub hoaoh {
754 3     3 1 92 my $self = shift;
755 3         17 my $params = $self->_params(@_);
756 3         7 my ($href);
757              
758 3 50 33     55 croak "query and key are required for hoaoh" unless $params->{query} and $params->{key};
759              
760 3         12 my $hoa = $params->{href}; #if hashref is passed it, it will just add to it
761              
762 3         12 $self->_query($params);
763              
764 3 100       25 if($params->{decrypt}) {
765 1         56 while($href=$self->sth->fetchrow_hashref){
766 4         16 $params->{href} = $href;
767 4         16 $self->_crypt_decrypt($params);
768 4         384 push @{$hoa->{$href->{$params->{key}}}},$href;
  4         89  
769             }
770             }
771             else {
772 2         121 while($href=$self->sth->fetchrow_hashref){
773 8         23 push @{$hoa->{$href->{$params->{key}}}},$href;
  8         99  
774             }
775             }
776              
777 3         18 return $hoa;
778             }
779              
780             =head2 array
781              
782             L can be used for selecting one value from multiple rows. Say for instance that we wanted all the ids for anyone named Susie.
783             We could do that like so:
784              
785             my $ids = $db->array("SELECT id FROM people WHERE name='Susie'");
786              
787             print "Susie ids: \n";
788             for my $id (@$ids) {
789             print "$id\n";
790             }
791              
792             To L for L, you would use L.
793              
794             =cut
795              
796             sub array {
797 2     2 1 47 my $self = shift;
798 2         9 my $params = $self->_params(@_);
799 2         10 my ($r,@a);
800              
801             # Get the Array of results:
802 2         11 $self->_query($params);
803 2 100       9 if($params->{decrypt}) {
804 1         27 while(($r) = $self->sth->fetchrow_array()){
805 4         14 $r = $self->_decrypt($r);
806 4         409 push @a, $r;
807             }
808             }
809             else {
810 1         29 while(($r) = $self->sth->fetchrow_array()){
811 4         22 push @a, $r;
812             }
813             }
814              
815 2         25 return \@a;
816             }
817              
818             =head2 hash
819              
820             =over
821              
822             =item
823              
824             B - the query
825              
826             =item
827              
828             B - the name of the column that will serve as the key
829              
830             =item
831              
832             B - the name of the column that will be stored behind the key
833              
834             =item
835              
836             B - the hash reference that you would like to have the results added to
837              
838             =back
839              
840             L can be used if you want to map one key to one value for multiple rows. For instance, let's say
841             we wanted to map each person's id to their name:
842              
843             my $ids_to_names = $db->hash(query => "SELECT id, name FROM people", key => "id", val => "name");
844              
845             my $name_1 = $ids_to_names->{1};
846              
847             print "$name_1\n"; #prints 'Fred'
848              
849              
850             To have L add to an existing hash, just pass in the existing hash with C:
851              
852              
853             $db->hash(query => "SELECT id, name FROM people", key => "id", val => "name", href => $ids_to_names);
854              
855             To L for L, you would use L.
856              
857             =cut
858              
859             sub hash {
860 3     3 1 110 my $self = shift;
861 3         21 my $params = $self->_params(@_);
862 3         8 my ($href);
863              
864 3 50 33     59 croak "query, key, and val are required for hash" unless $params->{query} and $params->{key} and $params->{val};
      33        
865              
866 3         31 my $hash = $params->{href}; #if hash is passed in, it will just add to it
867              
868 3         16 $self->_query($params);
869              
870 3 100       22 if($params->{decrypt}) {
871 1         57 while($href=$self->sth->fetchrow_hashref){
872 2         12 $params->{href} = $href;
873 2         9 $self->_crypt_decrypt($params);
874 2         284 $hash->{$href->{$params->{key}}} = $href->{$params->{val}};
875             }
876             }
877             else {
878 2         136 while($href=$self->sth->fetchrow_hashref){
879 4         119 $hash->{$href->{$params->{key}}} = $href->{$params->{val}};
880             }
881             }
882              
883 3         24 return $hash;
884             }
885              
886             =head2 insert
887              
888             =over
889              
890             =item
891              
892             B - the hash reference that will be used to insert the row, with the columns as the keys and the new values as the values
893              
894             =item
895              
896             B - the name of the table that the row will be inserted into
897              
898             =back
899              
900             L can be used to insert a single row with a hash. This can be useful if you already have the values you need
901             to insert the row with in a hash, where the keys are the column names and the values are the new values. This function
902             might be useful for submitting forms easily.
903              
904             my %person_to_insert = (
905             name => 'Billy',
906             age => '32',
907             favorite_color => 'blue',
908             );
909              
910             $db->insert(href => \%person_to_insert, table => 'people');
911              
912             If you need to have literal SQL into your insert query, then you just need to pass in a scalar reference. For example:
913              
914             "INSERT INTO people (name, update_time) VALUES('Billy', NOW())"
915              
916             If we had this:
917              
918             my %person_to_insert = (
919             name => 'Billy',
920             update_time => 'NOW()',
921             );
922              
923             $db->insert(href => \%person_to_insert, table => 'people');
924              
925             This would effectively evaluate to:
926              
927             $db->raw(query => "INSERT INTO people (name, update_time) VALUES(?, ?)", vals => ['Billy', 'NOW()']);
928              
929             However, this will not work. Instead, we need to do:
930              
931             my %person_to_insert = (
932             name => 'Billy',
933             update_time => \'NOW()',
934             );
935              
936             $db->insert(href => \%person_to_insert, table => 'people');
937              
938             Which evaluates to:
939              
940             $db->raw(query => "INSERT INTO people (name, update_time) VALUES(?, NOW())", vals => ['Billy']);
941              
942             And this is what we want.
943              
944             =head3 insert encrypt
945              
946             When encrypting for insert, because a hash is passed in you need to have the encrypt array reference contain the names of the columns that you want to encrypt
947             instead of the indices for the order in which the columns are listed:
948              
949             my %person_to_insert = (
950             name => 'Billy',
951             age => '32',
952             favorite_color => 'blue',
953             );
954              
955             $db->insert(href => \%person_to_insert, table => 'people', encrypt => ['name', 'favorite_color']);
956              
957             Note we do not ecnrypt age because it is most likely stored as an integer in the database.
958              
959             =cut
960              
961             # TODO: write insert tests
962             sub insert {
963 0     0 1 0 my $self = shift;
964 0         0 my $params = $self->_params(@_);
965              
966 0 0 0     0 croak "href and table are required for insert" unless $params->{href} and $params->{table};
967              
968 0         0 my @vals;
969 0         0 my $column_names = '';
970 0         0 my $values_string = '';
971 0         0 my @encrypt;
972 0         0 while(my ($key,$val) = each %{$params->{href}}) {
  0         0  
973 0         0 my $append = '?';
974 0 0       0 if (ref $val eq 'SCALAR') {
975 0         0 $append = $$val;
976             }
977             else {
978 0 0 0 0   0 if ($params->{encrypt} and first { $_ eq $key } @{$params->{encrypt}}) {
  0         0  
  0         0  
979 0         0 push @encrypt, scalar(@vals);
980             }
981              
982 0         0 push @vals, $val;
983             }
984              
985 0         0 $column_names .= "$key,";
986 0         0 $values_string .= "$append,";
987             }
988            
989 0         0 $column_names = substr $column_names, 0, -1;
990 0         0 $values_string = substr $values_string, 0, -1;
991              
992 0         0 $params->{query} = "INSERT INTO $params->{table} ($column_names) VALUES($values_string)";
993 0         0 $params->{vals} = \@vals;
994              
995 0 0 0     0 if ($params->{encrypt} and @encrypt) {
996 0         0 $params->{encrypt} = \@encrypt;
997 0         0 $self->_crypt_encrypt($params);
998             }
999              
1000 0         0 $self->_query($params);
1001             }
1002              
1003             =head2 update
1004              
1005             =over
1006              
1007             =item
1008              
1009             B - the hash reference that will be used to update the row, with the columns as the keys and the new values as the values
1010              
1011             =item
1012              
1013             B - the name of the table that the updated row is in
1014              
1015             =item
1016              
1017             B - specifies the id of the item that we are updating (note, column must be called "id"). Should not be used if C is used
1018              
1019             =item
1020              
1021             B - A hash reference of the form C<{name =E 'column_name', val =E 'unique_val'}>. Can be used instead of C. Should not be used if C is used
1022              
1023             =item
1024              
1025             B - A where clause to help decide what row to update. Any bind values can be passed in with C
1026              
1027             =back
1028              
1029             L can be used to update a single row with a hash, and returns the number of rows updated. This can be useful if you already have the values you need
1030             to update the row with in a hash, where the keys are the column names and the values are the new values. This function
1031             might be useful for submitting forms easily.
1032              
1033             my %updated_person = (
1034             name => 'Billy',
1035             age => '32',
1036             favorite_color => 'blue',
1037             );
1038              
1039             my $num_rows_updated = $db->update(href => \%updated_person, table => 'people', id => 1);
1040              
1041             # or in list context
1042             my ($num_rows_updated) = $db->update(href => \%updated_person, table => 'people', id => 1);
1043              
1044             Note that above for "id", the column must actually be named id for it to work. If you have a primary key or unique
1045             identifying column that is named something different than id, then you can use the C parameter:
1046              
1047             my $num_rows_updated = $db->update(href => \%updated_person, table => 'people', pk => {name => 'person_id', val => 1});
1048              
1049             If you need to specify more constraints for the row that you are updating instead of just the id, you can pass in a where clause:
1050              
1051             my $num_rows_updated = $db->update(href => \%updated_person, table => 'people', where => 'name=? AND favorite_color=? AND age=?', vals => ['Joe', 'green', 61]);
1052            
1053             Note that any bind values used in a where clause can just be passed into the C as usual. It is possible to use a where clause and an id or pk together:
1054              
1055             my $num_rows_updated = $db->update(href => \%updated_person, table => 'people', where => 'name=? AND favorite_color=? AND age=?', vals => ['Joe', 'green', 61], id => 1);
1056              
1057             Alternatively, you could just put the C or C in your where clause.
1058              
1059             If you need to have literal SQL into your update query, then you just need to pass in a scalar reference. For example:
1060              
1061             "UPDATE people SET name='Billy', update_time=NOW() WHERE id=1"
1062              
1063             If we had this:
1064              
1065             my %updated_person = (
1066             name => 'Billy',
1067             update_time => 'NOW()',
1068             );
1069              
1070             my $num_rows_updated = $db->update(href => \%updated_person, table => 'people', id => 1);
1071              
1072             This would effectively evaluate to:
1073              
1074             my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?, update_time=? WHERE id=?", vals => ['Billy', 'NOW()', 1]);
1075              
1076             However, this will not work. Instead, we need to do:
1077              
1078             my %updated_person = (
1079             name => 'Billy',
1080             update_time => \'NOW()',
1081             );
1082              
1083             my $num_rows_updated = $db->update(href => \%updated_person, table => 'people', id => 1);
1084              
1085             Which evaluates to:
1086              
1087             my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?, update_time=NOW() WHERE id=?", vals => ['Billy', 1]);
1088              
1089             And this is what we want.
1090              
1091             =head3 update encrypt
1092              
1093             When encrypting for update, because a hash is passed in you need to have the encrypt array reference contain the names of the columns that you want to encrypt
1094             instead of the indices for the order in which the columns are listed:
1095              
1096             my %updated_person = (
1097             name => 'Billy',
1098             age => '32',
1099             favorite_color => 'blue',
1100             );
1101              
1102             my $num_rows_updated = $db->update(href => \%updated_person, table => 'people', id => 1, encrypt => ['name', 'favorite_color']);
1103              
1104             Note we do not ecnrypt age because it is most likely stored as an integer in the database.
1105              
1106             =cut
1107              
1108             sub update {
1109 17     17 1 1003 my $self = shift;
1110 17         100 my $params = $self->_params(@_);
1111              
1112 17 50 33     332 croak "href and table are required for update" unless $params->{href} and $params->{table};
1113              
1114 17         111 my @vals;
1115 17         94 my $string = '';
1116 17         60 my @encrypt;
1117 17         74 while(my ($key,$val) = each %{$params->{href}}) {
  68         338  
1118 51         141 my $append = '?';
1119 51 100       159 if (ref $val eq 'SCALAR') {
1120 3         13 $append = $$val;
1121             }
1122             else {
1123             # TODO: write update encrypt tests
1124 48 100 66     509 if ((defined $params->{encrypt} and $params->{encrypt} eq '*')
      33        
      66        
1125 0     0   0 or ($params->{encrypt} and first { $_ eq $key } @{$params->{encrypt}})) {
  0         0  
1126 24         74 push @encrypt, scalar(@vals);
1127             }
1128              
1129 48         151 push @vals, $val;
1130             }
1131              
1132 51         187 $string .= "$key=$append,";
1133             }
1134            
1135 17         86 $string = substr $string, 0, -1;
1136              
1137 17 100       99 $params->{vals} = [] unless $params->{vals};
1138 17         74 my $where = '';
1139 17 100       80 if($params->{where}) {
1140 10         40 $where = " WHERE $params->{where}";
1141 10         45 push @vals, @{$params->{vals}};
  10         31  
1142             }
1143              
1144 17 100       100 if($params->{id}) {
    100          
1145 5 100       27 if($where eq '') {
1146 3         16 $where = " WHERE id=? ";
1147             }
1148             else {
1149 2         6 $where .= " AND id=? ";
1150             }
1151              
1152 5         20 push @vals, $params->{id};
1153             }
1154             elsif($params->{pk}) {
1155 6         23 my $name = $params->{pk}->{name};
1156 6         29 my $val = $params->{pk}->{val};
1157 6 100       29 if($where eq '') {
1158 2         12 $where = " WHERE $name=? ";
1159             }
1160             else {
1161 4         20 $where .= " AND $name=? ";
1162             }
1163              
1164 6         24 push @vals, $val;
1165             }
1166              
1167 17         110 $params->{query} = "UPDATE $params->{table} SET $string $where";
1168 17         70 $params->{vals} = \@vals;
1169              
1170 17 100 66     163 if ($params->{encrypt} and @encrypt) {
1171 8         32 $params->{encrypt} = \@encrypt;
1172 8         39 $self->_crypt_encrypt($params);
1173             }
1174              
1175 17         82 $self->_query($params);
1176              
1177 17 50       254 return unless defined wantarray;
1178 0 0       0 return wantarray ? ($self->sth->rows()) : $self->sth->rows();
1179             }
1180              
1181             =head2 insert_multiple
1182              
1183             =over
1184              
1185             =item
1186              
1187             B - the array reference of array references, where each inner array reference holds the values to be inserted for one row
1188              
1189             =item
1190              
1191             B - the name of the table that the rows are to be inserted into
1192              
1193             =item
1194              
1195             B - The names of the columns that values are being inserted for
1196              
1197             =back
1198              
1199             L can be used to insert multiple rows with one query. For instance:
1200              
1201             my $rows = [
1202             [
1203             1,
1204             'Joe',
1205             23,
1206             ],
1207             [
1208             2,
1209             'Ralph,
1210             50,
1211             ],
1212             ];
1213              
1214             $db->insert_multiple(table => 'people', columns => [qw/id name age/], rows => $rows);
1215              
1216             This can be translated into the SQL query:
1217              
1218             INSERT INTO people (id, name, age) VALUES (1, 'Joe', 23), (2, 'Ralph', 50);
1219              
1220             Note that L does not yet support encrypt. I'm planning to add this feature later. If you need it now, please shoot me an email and I will
1221             try to speed things up!
1222              
1223             =cut
1224              
1225             sub insert_multiple {
1226 1     1 1 49 my $self = shift;
1227 1         5 my $params = $self->_params(@_);
1228              
1229 1         10 while(my ($key, $val) = each %$params) {
1230 3         85 print "$key=$val\n";
1231             }
1232              
1233 1 50 33     24 croak "columns, table, and rows are required for insert_multiple" unless $params->{columns} and $params->{table} and $params->{rows};
      33        
1234              
1235 1         3 my $values_string = '';
1236 1         2 my @vals;
1237              
1238 1         4 my $columns = join ',', @{$params->{columns}};
  1         9  
1239 1         7 my $row_string = '?,' x @{$params->{columns}};
  1         4  
1240 1         4 $row_string = substr $row_string, 0, -1;
1241              
1242 1         2 for my $row (@{$params->{rows}}) {
  1         7  
1243 2         13 push @vals, @$row;
1244 2         13 $values_string .= "($row_string),";
1245             }
1246              
1247 1         9 $values_string = substr $values_string, 0, -1;
1248              
1249 1         6 $params->{query} = "INSERT INTO $params->{table} ($columns) VALUES $values_string";
1250 1         9 print $params->{query} . "\n";
1251 1         4 $params->{vals} = \@vals;
1252              
1253 1         5 $self->_query($params);
1254             }
1255              
1256             =head2 sth
1257              
1258             L returns the statement handle from the previous query.
1259              
1260             my $sth = $db->sth;
1261              
1262             This can be useful if you need a statement handle to perform a function, like to get
1263             the id of the last inserted row.
1264              
1265             =cut
1266              
1267             =head2 dbh
1268              
1269             L returns the database handle that L is using.
1270              
1271             my $dbh = $db->dbh;
1272              
1273             L can also be used to set a new database handle for L to use.
1274              
1275             $db->dbh($new_dbh);
1276              
1277             =cut
1278              
1279             =head2 dsn
1280              
1281             L returns the dsn that was provided.
1282              
1283             my $dsn = $db->dsn;
1284              
1285             L can also be used to set a new C.
1286              
1287             $db->dsn($new_dsn);
1288              
1289             When setting a new C, it's likely you'll want to use L.
1290              
1291             =cut
1292              
1293             =head2 user
1294              
1295             L returns the user that was provided.
1296              
1297             my $user = $db->user;
1298              
1299             L can also be used to set a new C.
1300              
1301             $db->user($new_user);
1302              
1303             When setting a new C, it's likely you'll want to use L.
1304              
1305             =cut
1306              
1307             =head2 password
1308              
1309             L returns the password that was provided.
1310              
1311             my $password = $db->password;
1312              
1313             L can also be used to set a new C.
1314              
1315             $db->password($new_password);
1316              
1317             When setting a new C, it's likely you'll want to use L.
1318              
1319             =cut
1320              
1321             =head2 conf
1322              
1323             L returns the conf file that was provided.
1324              
1325             my $conf = $db->conf;
1326              
1327             L can also be used to set a new C file.
1328              
1329             $db->conf($new_conf);
1330              
1331             When setting a new C, it's likely you'll want to use L.
1332              
1333             =cut
1334              
1335             =head2 connect
1336              
1337             L can be used to keep the same L object, but get a new L. You can call connect to get a new dbh with the same settings that you have provided:
1338              
1339             #now there is a new dbh with the same DBIx::Raw object using the same settings
1340             $db->connect;
1341              
1342             Or you can change the connect info.
1343             For example, if you update C, C, C:
1344              
1345             $db->dsn('new_dsn');
1346             $db->user('user');
1347             $db->password('password');
1348              
1349             #get new dbh but keep same DBIx::Raw object
1350             $db->connect;
1351              
1352             Or if you update the conf file:
1353              
1354             $db->conf('/path/to/new_conf.pl');
1355            
1356             #get new dbh but keep same DBIx::Raw object
1357             $db->connect;
1358              
1359             =cut
1360              
1361             sub connect {
1362 70     70 1 536 my ($self) = @_;
1363              
1364 70         632 $self->_parse_conf;
1365 70         7010 $self->_validate_connect_info;
1366 70   33     3926 return $self->dbh(DBI->connect($self->dsn, $self->user, $self->password) or croak($DBI::errstr));
1367             }
1368              
1369             sub _params {
1370 321     321   924 my $self = shift;
1371              
1372 321         854 my %params;
1373 321 100       2312 unless($self->keys->{$_[0]}) {
1374 43         270 $params{query} = shift;
1375 43         188 $params{vals} = [@_];
1376             }
1377             else {
1378 278         3020 %params = @_;
1379             }
1380              
1381 321         1669 return \%params;
1382             }
1383              
1384             sub _query {
1385 321     321   857 my ($self, $params) = (@_);
1386              
1387 321 50       7884 $self->sth($self->dbh->prepare($params->{query})) or $self->_perish($params);
1388              
1389 321 100       27852 if($params->{'vals'}){
1390 227 50       840 $self->sth->execute(@{$params->{'vals'}}) or $self->_perish($params);
  227         6579  
1391             }
1392             else {
1393 94 50       13226 $self->sth->execute() or $self->_perish($params);
1394             }
1395             }
1396              
1397             sub _perish {
1398 0     0   0 my ($self, $params) = @_;
1399 0         0 croak "ERROR: Can't prepare query.\n\n$DBI::errstr\n\nquery='" . $params->{query} . "'\n";
1400             }
1401              
1402             sub _crypt_decrypt {
1403 31     31   279 my ($self, $params) = @_;
1404 31         139 my @keys;
1405 31 100       144 if($params->{decrypt} eq '*') {
1406 8 100       56 if($params->{href}) {
1407 6         42 @keys = keys %{$params->{href}};
  6         39  
1408             }
1409             else {
1410 2         6 @keys = 0..$#{$params->{return_values}};
  2         13  
1411             }
1412             }
1413             else {
1414 23         94 @keys = @{$params->{decrypt}};
  23         82  
1415             }
1416              
1417 31 100       168 if($params->{href}) {
1418 24         158 for my $key (@keys) {
1419 47 50       3164 $params->{href}->{$key} = $self->_decrypt($params->{href}->{$key}) if $params->{href}->{$key};
1420             }
1421             }
1422             else {
1423 7         49 for my $index (@keys) {
1424 12 50       767 $params->{return_values}->[$index] = $self->_decrypt( $params->{return_values}->[$index] ) if $params->{return_values}->[$index];
1425             }
1426             }
1427             }
1428              
1429             sub _crypt_encrypt {
1430 165     165   573 my ($self, $params) = @_;
1431 165         353 my @indices;
1432              
1433 165 100       785 if($params->{encrypt} eq '*') {
1434 4         18 my $num_question_marks = 0;
1435             #don't want to encrypt where conditions! Might be buggy...should look into this more
1436 4 50       95 if($params->{query} =~ /WHERE\s+(.*)/i) {
1437 4         66 $num_question_marks =()= $1 =~ /=\s*?\?/g;
1438             }
1439              
1440 4         19 @indices = 0..($#{$params->{vals}} - $num_question_marks);
  4         19  
1441             }
1442             else {
1443 161         353 @indices = @{$params->{encrypt}};
  161         544  
1444             }
1445              
1446 165         952 for my $index (@indices) {
1447 80         207 @{$params->{vals}}[$index] = $self->_encrypt( @{$params->{vals}}[$index] );
  80         45655  
  80         325  
1448             }
1449             }
1450              
1451             sub _encrypt {
1452 80     80   205 my ($self, $text) = @_;
1453              
1454 80 50       2269 if ($self->use_old_crypt) {
1455 0         0 return $self->old_crypt->encrypt($text);
1456             }
1457              
1458 80         2285 return $self->crypt->encrypt($text);
1459             }
1460              
1461             sub _decrypt {
1462 63     63   214 my ($self, $text) = @_;
1463            
1464 63 50       1477 if ($self->use_old_crypt) {
1465 0         0 return $self->old_crypt->decrypt($text);
1466             }
1467              
1468 63         1452 return $self->crypt->decrypt($text);
1469             }
1470              
1471             sub _parse_conf {
1472 140     140   1693 my ($self) = @_;
1473              
1474             #load in configuration if it exists
1475 140 100       2008 if($self->conf) {
1476              
1477             #no need to read in settings again if conf hasn't changed, unless dsn, user, or password is unset
1478 137 50 100     5854 return if $self->conf eq $self->prev_conf and $self->dsn and $self->user and $self->password;
      66        
      66        
1479              
1480 137         11175 my $config = Config::Any->load_files({files =>[$self->conf],use_ext => 1 });
1481              
1482 137         1633771 for my $c (@$config){
1483 137         1399 for my $file (keys %$c){
1484 137         912 for my $attribute (keys %{$c->{$file}}){
  137         1031  
1485 412 50       3562 if($self->can($attribute)) {
1486 412         4279 $self->$attribute($c->{$file}->{$attribute});
1487             }
1488             }
1489             }
1490             }
1491              
1492 137         11354 $self->prev_conf($self->conf);
1493             }
1494             }
1495              
1496             sub _validate_connect_info {
1497 140     140   801 my ($self) = @_;
1498 140 50 66     10536 croak "Need to specify 'dsn', 'user', and 'password' either when you create the object or by passing in a configuration file in 'conf'! Or, pass in an existing dbh"
      66        
      66        
1499             unless (defined $self->dsn and defined $self->user and defined $self->password) or defined $self->dbh;
1500             }
1501              
1502             =head1 AUTHOR
1503              
1504             Adam Hopkins, C<< >>
1505              
1506             =head1 BUGS
1507              
1508             Please report any bugs or feature requests to C, or through
1509             the web interface at L. I will be notified, and then you'll
1510             automatically be notified of progress on your bug as I make changes.
1511              
1512             =head1 SUPPORT
1513              
1514             You can find documentation for this module with the perldoc command.
1515              
1516             perldoc DBIx::Raw
1517              
1518              
1519             You can also look for information at:
1520              
1521             =over 4
1522              
1523             =item * RT: CPAN's request tracker (report bugs here)
1524              
1525             L
1526              
1527             =item * AnnoCPAN: Annotated CPAN documentation
1528              
1529             L
1530              
1531             =item * CPAN Ratings
1532              
1533             L
1534              
1535             =item * Search CPAN
1536              
1537             L
1538              
1539             =back
1540              
1541              
1542             =head1 ACKNOWLEDGEMENTS
1543              
1544             Special thanks to Jay Davis who wrote a lot of the original code that this module is based on.
1545              
1546             =head1 LICENSE
1547              
1548             This library is free software; you can redistribute it and/or modify
1549             it under the same terms as Perl itself.
1550              
1551             =cut
1552              
1553             1;