Linux.pl
Opcje wyszukiwania podręcznika man:
Lista stron man zaczynających się od znaku:
A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   ALPHA   NUM   OTHER   ALL
SQL::Abstract(3pm)    User Contributed Perl Documentation   SQL::Abstract(3pm)

NAME
       SQL::Abstract - Generate SQL from Perl data structures

SYNOPSIS
           use SQL::Abstract;

           my $sql = SQL::Abstract->new;

           my($stmt, @bind) = $sql->select($table, \@fields, \%where, \@order);

           my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);

           my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);

           my($stmt, @bind) = $sql->delete($table, \%where);

           # Then, use these in your DBI statements
           my $sth = $dbh->prepare($stmt);
           $sth->execute(@bind);

           # Just generate the WHERE clause
           my($stmt, @bind) = $sql->where(\%where, \@order);

           # Return values in the same order, for hashed queries
           # See PERFORMANCE section for more details
           my @bind = $sql->values(\%fieldvals);

DESCRIPTION
       This module was inspired by the excellent DBIx::Abstract.  However, in
       using that module I found that what I really wanted to do was generate
       SQL, but still retain complete control over my statement handles and
       use the DBI interface. So, I set out to create an abstract SQL genera-
       tion module.

       While based on the concepts used by DBIx::Abstract, there are several
       important differences, especially when it comes to WHERE clauses. I
       have modified the concepts used to make the SQL easier to generate from
       Perl data structures and, IMO, more intuitive.  The underlying idea is
       for this module to do what you mean, based on the data structures you
       provide it. The big advantage is that you don't have to modify your
       code every time your data changes, as this module figures it out.

       To begin with, an SQL INSERT is as easy as just specifying a hash of
       "key=value" pairs:

           my %data = (
               name => 'Jimbo Bobson',
               phone => '123-456-7890',
               address => '42 Sister Lane',
               city => 'St. Louis',
               state => 'Louisiana',
           );

       The SQL can then be generated with this:

           my($stmt, @bind) = $sql->insert('people', \%data);

       Which would give you something like this:

           $stmt = "INSERT INTO people
                           (address, city, name, phone, state)
                           VALUES (?, ?, ?, ?, ?)";
           @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
                    '123-456-7890', 'Louisiana');

       These are then used directly in your DBI code:

           my $sth = $dbh->prepare($stmt);
           $sth->execute(@bind);

       In addition, you can apply SQL functions to elements of your %data by
       specifying an arrayref for the given hash value. For example, if you
       need to execute the Oracle "to_date" function on a value, you can say
       something like this:

           my %data = (
               name => 'Bill',
               date_entered => ["to_date(?,'MM/DD/YYYY')", "03/02/2003"],
           );

       The first value in the array is the actual SQL. Any other values are
       optional and would be included in the bind values array. This gives
       you:

           my($stmt, @bind) = $sql->insert('people', \%data);

           $stmt = "INSERT INTO people (name, date_entered)
                       VALUES (?, to_date(?,'MM/DD/YYYY'))";
           @bind = ('Bill', '03/02/2003');

       An UPDATE is just as easy, all you change is the name of the function:

           my($stmt, @bind) = $sql->update('people', \%data);

       Notice that your %data isn't touched; the module will generate the
       appropriately quirky SQL for you automatically. Usually you'll want to
       specify a WHERE clause for your UPDATE, though, which is where handling
       %where hashes comes in handy...

       This module can generate pretty complicated WHERE statements easily.
       For example, simple "key=value" pairs are taken to mean equality, and
       if you want to see if a field is within a set of values, you can use an
       arrayref. Let's say we wanted to SELECT some data based on this crite-
       ria:

           my %where = (
              requestor => 'inna',
              worker => ['nwiger', 'rcwe', 'sfz'],
              status => { '!=', 'completed' }
           );

           my($stmt, @bin

perl v5.8.8                       2006-11-30                SQL::Abstract(3pm)

Time taken: 0.00042 seconds


Created with the man page lookup class by Andrew Collington, php@amnuts.com