App::ZofCMS::Plugin::FormToDatabase - simple insertion of query into database


App-ZofCMS-Plugin-FormToDatabase documentation Contained in the App-ZofCMS-Plugin-FormToDatabase distribution.

Index


Code Index:

NAME

Top

App::ZofCMS::Plugin::FormToDatabase - simple insertion of query into database

SYNOPSIS

Top

In your Main Config file or ZofCMS template:

    plugins => [ qw/FormToDatabase/ ],
    plug_form_to_database => {
        go_field   => 'd|foo',
        values   => [ qw/one two/ ],
        table   => 'form',
        dsn     => "DBI:mysql:database=test;host=localhost",
        user    => 'test',
        pass    => 'test',
        opt     => { RaiseError => 1, AutoCommit => 0 },
    },

DESCRIPTION

Top

The module is a simple drop in to stick query into database. The module does not provide any parameter checking and is very basic. For anything more advanced check out App::ZofCMS::Plugin::DBI

This documentation assumes you have read App::ZofCMS, App::ZofCMS::Config and App::ZofCMS::Template

MAIN CONFIG FILE OR ZofCMS TEMPLATE FIRST LEVEL KEYS

Top

    plug_form_to_database => {
        go_field   => 'd|foo',
        values   => [ qw/one two/ ],
        table   => 'form',
        dsn     => "DBI:mysql:database=test;host=localhost",
        user    => 'test',
        pass    => 'test',
        opt     => { RaiseError => 1, AutoCommit => 0 },
    },

Plugin uses the plug_form_to_database first-level key in ZofCMS template or your main config file. The key takes a hashref as a value. Values set under this key in ZofCMS template will override values set in main config file. Possible keys/values are as follows.

go_field

    go_field => 'd|foo',

Optional. Defaults to: d|form_to_database. The go_field key specifies the "go" to the plugin; in other words, if value referenced by the string set under go_field key the plugin will proceed with stuffing your database, otherwise it will not do anything. Generally, you'd do some query checking with a plugin (e.g. App::ZofCMS::Plugin::FormChecker) with lower priority number (so it would be run first) and then set the value referenced by the go_field.

The go_field key takes a string as a value. The string is in format s|key_name - where s is the name of the "source". What follows the "source" letter is a pipe (|) and then they name of the key. The special value of source q (note that it is lowercase) means "query". That is q|foo means, "query parameter 'foo'". Other values of the "source" will be looked for inside ZofCMS template hash, e.g. d|foo means key foo in ZofCMS template special first-level key {d} - this is probably where you'd want to check that for.

Example:

    # ZofCMS template:
    plugins => [ qw/FormToDatabase/ ],
    d       => { foo => 1 },
    plug_form_to_database => {
        go_field => 'd|foo',
        ..... # omited for brevity
    },

The example above will always stuff the query data into the database because key foo under key d is set to a true value and go_field references that value with d|foo.

values

        values => [ qw/one two/ ],

Mandatory. The values key takes an arrayref as a value. The elements of that arrayref represent the names of query parameters that you wish to stick into the database. Under the hood of the module the following is being called:

    $dbh->do(
        "INSERT INTO $conf{table} VALUES(" . join(q|, |, ('?')x@values) . ');',
        undef,
        @$query{ @values },
    );

Where @values contains values you passed via values key and $dbh is the database handle created by DBI. If you want something more advanced consider using App::ZofCMS::Plugin::DBI instead.

table

    table => 'form',

Mandatory. Specifies the name of the table into which you wish to store the data.

dsn

    dsn => "DBI:mysql:database=test;host=localhost",

Mandatory. Specifies the dsn to use in DBI connect call. See documentation for DBI and DBD::your_database for proper syntax for this string.

user

    user => 'test',

Mandatory. Specifies the user name (login) to use when connecting to the database.

pass

    pass => 'test',

Mandatory. Specifies the password to use when connecting to the database.

opt

Optional. Specifies extra options to use in DBI's connect_cached() call. Defaults to: { RaiseError => 1, AutoCommit => 0 }

SEE ALSO

Top

DBI, App::ZofCMS::Plugin::DBI

AUTHOR

Top

'Zoffix, <'zoffix at cpan.org'> (http://zoffix.com/, http://haslayout.net/, http://zofdesign.com/)

BUGS

Top

Please report any bugs or feature requests to bug-app-zofcms-plugin-formtodatabase at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-ZofCMS-Plugin-FormToDatabase. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc App::ZofCMS::Plugin::FormToDatabase

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-ZofCMS-Plugin-FormToDatabase

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/App-ZofCMS-Plugin-FormToDatabase

* CPAN Ratings

http://cpanratings.perl.org/d/App-ZofCMS-Plugin-FormToDatabase

* Search CPAN

http://search.cpan.org/dist/App-ZofCMS-Plugin-FormToDatabase

COPYRIGHT & LICENSE

Top


App-ZofCMS-Plugin-FormToDatabase documentation Contained in the App-ZofCMS-Plugin-FormToDatabase distribution.

package App::ZofCMS::Plugin::FormToDatabase;

use warnings;
use strict;

our $VERSION = '0.0101';

use DBI;

sub new { bless {}, shift }

sub process {
    my ( $self, $template, $query, $config ) = @_;

    return
        unless $template->{plug_form_to_database}
            or $config->conf->{plug_form_to_database};

    my %conf = (
        opt         => { RaiseError => 1, AutoCommit => 0 },
        go_field    => 'd|form_to_database',
        %{ delete $config->conf->{plug_form_to_database} || {} },
        %{ delete $template->{plug_form_to_database}     || {} },
    );

#         dsn     => "DBI:mysql:database=test;host=localhost",
#         user    => 'test', # user,
#         pass    => 'test', # pass
#         opt     => { RaiseError => 1, AutoCommit => 0 },

    my ( $source, $go_name ) = split /\|/, $conf{go_field}, 2;

    return
        unless ( $source eq 'q' and $query->{$go_name} )
            or
            $template->{$source}{$go_name};

    my @values = @{ $conf{values} };

    my $dbh = DBI->connect_cached(
        @conf{ qw/dsn user pass opt/ },
    );

    $dbh->do(
        "INSERT INTO $conf{table} VALUES(" . join(q|, |, ('?')x@values) . ');',
        undef,
        @$query{ @values },
    );
    $dbh->disconnect;

    return 1;
}

1;
__END__