DBIx-Raw
view release on metacpan or search on metacpan
encrypt
In order to encrypt values, the values that you want to encrypt must be
in the bind values array reference that you pass into vals. Note that
for the values that you want to encrypt, you should put their index
into the encrypt array that you pass in. For example:
my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?,age=?,height=? WHERE id=1", vals => ['Zoe', 24, "5'11"], encrypt => [0, 2]);
In the above example, only name and height will be encrypted. You can
easily encrypt all values by using '*', like so:
my $num_rows_updated = $db->raw(query => "UPDATE people SET name=?,height=? WHERE id=1", vals => ['Zoe', "5'11"], encrypt => '*');
And this will encrypt both name and height.
The only exception to the "encrypt" syntax that is a little different
is for "update". See "update encrypt" for how to encrypt when using
"update".
decrypt
When decrypting values, there are two possible different syntaxes.
DECRYPT LIST CONTEXT
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 order that the columns were listed in. For instance:
my $name = $db->raw(query => "SELECT name FROM people WHERE id=1", decrypt => [0]);
my ($name, $age) = $db->raw(query => "SELECT name, age FROM people WHERE id=1", decrypt => [0,1]);
DECRYPT HASH CONTEXT
When your query has 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:
my $person = $db->raw(query => "SELECT name, age FROM people WHERE id=1", decrypt => ['name', 'age'])
my $aoh = $db->aoh(query => "SELECT name, age FROM people", decrypt => ['name', 'age']);
Note that for either "LIST CONTEXT" or "HASH CONTEXT", it is possible
to use '*' to decrypt all columns:
my ($name, $height) = $db->raw(query => "SELECT name, height FROM people WHERE id=1", decrypt => '*');
crypt_key
DBIx::Raw uses "crypt_key" to encrypt and decrypt all values. You can
set the crypt key when you create your DBIx::Raw object by passing it
into "new", providing it to CONFIGURATION FILE, or by setting it with
its setter method:
$db->crypt_key("1234");
It is strongly recommended that you do not use the default "crypt_key".
The "crypt_key" should be the appropriate length for the "crypt" that
is set. The default "crypt" uses Crypt::Mode::CBC::Easy, which uses
Crypt::Cipher::Twofish, which allows key sizes of 128/192/256 bits.
crypt
The Crypt::Mode::CBC::Easy object to use for encryption. Default is the
default Crypt::Mode::CBC::Easy object created with the key "crypt_key".
use_old_crypt
In version 0.16 DBIx::Raw started using Crypt::Mode::CBC::Easy instead
of DBIx::Raw::Crypt. Setting this to 1 uses the old encryption instead.
Make sure to set "old_crypt_key" if you previously used "crypt_key" for
encryption.
old_crypt_key
This sets the crypt key to use if "use_old_crypt" is set to true.
Default is the previous crypt key.
SUBROUTINES/METHODS
raw
"raw" is a very versitile subroutine, and it can be called in three
contexts. "raw" should only be used to make a query that returns values
for one record, or a query that returns no results (such as an INSERT
query). If you need to have multiple results returned, see one of the
subroutines below.
SCALAR CONTEXT
"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.
#select
my $name = $db->raw("SELECT name FROM people WHERE id=1");
#update with number of rows updated returned
my $num_rows_updated = $db->raw("UPDATE people SET name=? WHERE id=1", 'Frank');
#update in undef context, nothing returned.
$db->raw("UPDATE people SET name=? WHERE id=1", 'Frank');
#insert
$db->raw("INSERT INTO people (name, age) VALUES ('Jenny', 34)");
Note that to "decrypt" for "SCALAR CONTEXT" for "raw", you would use
"DECRYPT LIST CONTEXT".
LIST CONTEXT
"raw" can also be called in a list context to return multiple columns
for one row.
my ($name, $age) = $db->raw("SELECT name, age FROM people WHERE id=1");
#or
my @person = $db->raw("SELECT name, age FROM people WHERE id=1");
Note that to "decrypt" for "LIST CONTEXT" for "raw", you would use
"DECRYPT LIST CONTEXT".
HASH CONTEXT
"raw" will return a hash if you are selecting more than one column for
a single record.
my $person = $db->raw("SELECT name, age FROM people WHERE id=1");
my $name = $person->{name};
my $age = $person->{age};
( run in 0.883 second using v1.01-cache-2.11-cpan-df04353d9ac )