blib/lib/DBIx/Raw.pm

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