The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

EasyDB::Query::Update - EasyDB UPDATE query routines

SYNOPSIS

This file is used by EasyDB.

DESCRIPTION

Provides UPDATE functionality for the EasyDB packge.

CONSTRUCTOR

Takes as it's parameters a DBI database handle. Will return a UPDATE query object.

METHODS

debug ( [level] )

Sets the debugging level of this object. The standard debug level would normally be 1, and that would tell you what the program is up to. If you want you can go as high as 5, which spurts out reams and reams of useless debugging information.

    $easyDB->debug('1');
   

would be sufficient.

table ( [table name] )

When called with a table name this function will set the table that the query will execute on. Without a table name it will return the current table it is looking at.

    $easyDB->find->table('table1');

If you have changed the table then the new table is returned.

criteria ( hash of criteria )

This function defines what data you want to change in the given table. The language is partly functional and partly SQL based. You start by expressing what you want to changte:

   Change all people with names that have an 'a' in, who are 
   between 21 and 28 but don't have blue eyes.

We then break this down a little more into some basic facts that we want to know:

   Name must have an 'a' in it
   Age must be greater then 21
   Age must be less than 28
   Eyes must not be blue
   

We then convert this into some functional statements. You should use these symbols to convert your facts into functional facts:

   <     Less than
   >     Greater than
   =     Equal to
   !     Is Not
   

Some of these can be combined as well:

   <=    Less than or equal to
   >=    Greater than or equal to
   

We now re-write out facts like this:

    Name must have an 'a' in it
    Age > 21
    Age < 28
    Eyes ! 'blue'
    

For the final step we need to use the wildcard, '%'. The wildcard means 'anything', that is anything can replace the wildcard. If we had the words 'ball', 'bat', 'bag', and 'hag', we can say:

    Word has 'll' at the end
    Word is like '%ll'
    (This would return 'ball')

    Word has 'ba' at the start
    Word is like 'ba%'
    (This would return 'ball', 'bat' and 'bag')

    Word has an 'a' in the middle
    Word is like '%a%'
    (This would return all the words)

    Any word at all
    Word is like '%'
    (This would also return all the words)
        

So now our set of facts would look like:

    Name is like '%a'
    Age > 21
    Age < 28
    Eyes ! 'blue'

Now we need to convert this into our criteria hash. This step is quite simple once you've got the functional section:

    $easydb->change->criteria(
                            Name   => '%a',
                            Age    => ['> 21', '< 28'],
                            Eyes   => '! blue',
                            );

Note that we can have more than one criteria for Age. Simply put the list into an array by enclosing it in square brackets.

sql ( )

This function will either return the current SQL string, if one has been generated, or nothing if there is no SQL statment stored.

    my $string  = $easyDB->find->sql();

string now holds the SQL statement.

how_many ( )

Function to return the number of records fetched. Calls a utility function in EasyDB::Util to count the number of records that are affected by the query.

    my $rows    = $easyDB->find->how_many();

For more information view the EasyDB::Util documentation.

to ( hash of new values )

This function will allow you to change the values of some data in the database. Firstly you must select a table and which records you want to alter. This is done using table() and criteria() :

    $easyDB->change->table('table1');
    $easyDB->change->criteria(Eyes => 'b%',
                              Age  => '> 26' );
                              

The above statement will only change entries in table 1 where 'Eyes' has a 'b' at the start and the age is greater than 26.

You then specify what you want to change this information to, using the to() function:

    $easyDB->change->to( Age    => 'unknown',
                         Status => 'single' );
                         

The information has then been updated.

INTERNAL FUNCTIONS

_do_update_query ( $self object )

Function to execute an UPDATE query. This is only ever called by the UPDATE query type.

_debug ( error level, debug message[s] )

Internal function used to report error messages. When you specify the level of this message it is checked against the current debug level. If the debug level is equal or greater than the level of this message, it is displayed.

CAVEATS

Unsure as to how stable the SQL parsing engine is. I don't know its tolerance for bad syntax.

KNOWN BUGS

I'll have to get back to you on that one

SEE ALSO

EasyDB

ABOUT

This is part of Gaby Vanhegan's third year project for the University Of Leeds.

AUTHOR

Gaby Vanhegan <gaby@vanhegan.com>