#===========================================================================
Copyright (C) 2008 by Nik Ogura. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Bug reports and comments to nik.ogura@gmail.com.
CGI::Lazy::RecordSet
use CGI::Lazy; our $q = CGI::Lazy->new({ tmplDir => "/templates", jsDir => "/js", plugins => { mod_perl => { PerlHandler => "ModPerl::Registry", saveOnCleanup => 1, }, ajax => 1, dbh => { dbDatasource => "dbi:mysql:somedatabase:localhost", dbUser => "dbuser", dbPasswd => "letmein", dbArgs => {"RaiseError" => 1}, }, session => { sessionTable => 'SessionData', sessionCookie => 'frobnostication', saveOnDestroy => 1, expires => '+15m', }, }, }); my $recordset = $q->db->recordset({ table => 'detail', #table where records are coming from mysqlAuto => 1, fieldlist => [ {name => 'detail.ID', #name of field hidden => 1}, #do not display to screen. Recordset cant do any operations on fields that are not a part of itself, however all fields need not be displayed {name => 'invoiceid', hidden => 1}, {name => 'prodCode', label => 'Product Code', validator => {rules => ['/\d+/'], msg => 'number only, and is required'}}, #validator for filed. msg is not implemented at present. {name => 'quantity', label => 'Quantity', validator => {rules => ['/\d+/'], msg => 'number only, and is required'}, outputMask => "%.1f", #formatting to data applied on output to browser }, {name => 'unitPrice', label => 'Unit Price' , validator => {rules => ['/\d+/'], msg => 'number only, and is required'}, inputMask => "%.1f", #formatting to data applied on input to database }, {name => 'productGross', label => 'Product Gross' , validator => {rules => ['/\d+/'], msg => 'number only, and is required'}}, {name => 'prodCodeLookup.description', label => 'Product Description', readOnly => 1 }, #readOnly values display to the screen, but never get written to the db ], basewhere => '', #baseline where clause for the select query. this is used in all selects, even if 'where is set later. joins => [ #table joins {type => 'inner', table => 'prodCodeLookup', field1 => 'prodCode', field2 => 'prodCodeLookup.ID',}, ], orderby => 'detail.ID', #order by clause for select wuery primarykey => 'detail.ID', #primary key for recordset. This value is looked for for all updates and deletes insertdefaults => { unitprice => { value => 'lots', handle => $ref, }, invoiceid => { sql => 'select something.nextval from dual', primarykey => 1, }, }, }); my $thing = $q->ajax->dataset({ id => 'detailBlock', type => 'multi', template => "UsbInternalPOCDetailBlock.tmpl", lookups => { prodcodeLookup => { sql => 'select ID, description from prodCodeLookup', preload => 1, orderby => ['ID'], output => 'hash', primarykey => 'ID', }, }, recordset => $recordset, });
CGI::Lazy::DB::Recordset is a container object for handling a set of records pulled out of a database. The big difference between using the Recordset object and just using a standard query is the Recordset, with it's defined internal structure allows for automated transformations to the data. The object builds the queries on the fly, and remembers where it got all the data in question, so it can edit it and put it back. Much of this functionality is seen in the Ajax::Dataset object, for which the Recordset object was originally written.
Returns the basewhere string for the recordset.
Creates the Select statement out of the structure of the Recordset.
Returns data reference from Recordset. Will always be present, but will be empty until select() is called.
Deletes records with primary keys in data.
Hashref who's keys are the primary keys of the records to be deleted.
Returns true if field has displayOnly key set to a true value.
name of field to test
Returns reference to CGI::Lazy::DB object
Returns array ref of field list with which recordset was built.
Returns reference used as handle to value of field.
Name of field who's handle to retrieve
Returns true if field in question has been set to hidden
Returns inputMask for field of given name, if one has been set.
Name of field to test.
Inserts data modified by vars into table accessed by Recordset.
Hashref of data to be inserted. Each key corresponds to a row of data
modifiers for data to be inserted
Returns reference of additional information to be inserted with each new record
Returns reference of default values to be inserted with each new record
Returns either list or arrayref of joins for Recordset
Returns label set for field, or name of field if no label has been specified
field name to test.
Constructor
Hashref with construction properties.
Options:
{ table => $table, mysqlAuto => 1, basewhere => $where, orderby => $order by, primarykey => $keyfield, fieldlist => [ { name => 'field1', label => 'some field', validator => { rules => ['/\d+/'}, outputMask => "%1.f", inputMask => "%1.f", }, { name => 'field2', label => 'some other field', outputMask => "%1.f", readOnly => 1, }, { name => 'post_date', label => 'Post Date', readfunc => "to_char(post_date, 'YYYY-MM-DD')", writefunc => "to_date(?, 'YYYY-MM-DD')", }, ], insertdefaults => { field1 => { value => 'some value', }, field2 => { sql => 'select foo from bar', }, field3 => { sql => 'select foo.nextvar from dual', handle => $ref, } updatedefaults => { field1 => { value => 'some value', }, field2 => { sql => 'select foo from bar', }, field3 => { sql => 'select foo.nextvar from dual', handle => $ref, } } }
string. name of table
set flag if primary key for this recordset is created by mysql auto_increment column. If set, composite widgets will automatically make this value available to member widgets on insert.
Sql string. This forms the base where clause for all selects. This string should not contain any variables from the outside world, as it is NOT bound, and could be used in sql injection attacks were cgi parameters used here. If you want to use cgi params, see the 'where' method which is intended to be dynamic, and can take binds.
string. orderby clause
field name of primary key for table
array ref. list of fields with their attributes
name => name of field hidden => if true, field is never displayed, but is selected readOnly => if true, field is displayed, but never written label => displayed label of field. if blank label defaults to fieldname noLabel => if true, no label is displayed for field. oututMask => sprintf string that transforms data on the way out of db to screen inputMask => sprintf string that transforms data on the way into db validator => rules for field validation rules => arrayref of tests to check. if all return true, field is valid. Currently only supports regexes. message => canned error message to display onerror. (not currently used by anything, but you can grab this in your own code and display it) readfunc => database function to perform on read writefunc => db function to perform on write insertdefaults => default values inserted on insert value => value to insert sql => sql to generate value to insert handle => reference whose referrent will contain whatever value is set into db. Useful for later use in cgi. updatedefaults => default values updated on update value => value to insert sql => sql to generate value to insert handle => reference whose referrent will contain whatever value is set into db. Useful for later use in cgi.
means for setting default values on any update
means for setting default values on any insert
Returns true if field in question has been set with the noLabel option
Name of field to test. =head2 orderby ( sql )
returns or sets the order by clause
sql string
Returns outputMask set for field.
Returns true if field in question has multipleField option set (i.e. it's supposed to turn up on the mulitple record screen)
Returns arrayref or array of fields flagged to show up on multiple records page
Returns arrayref or array of labels for fields chosen to appear on multiple record pages.
returns or sets the primary key for the object
The name of the field in the database
Returns scalar ref to primary key if set via default or mysqlAuto.
Returns true if recordset was created with mysqlAuto => 1 .
returns reference to CGI::Lazy object.
Returns readfunction set for field in question, if any.
field to be tested.
Returns true if field in question has been set to readOnly.
field to be tested
Runs select query based on $self->createSelect, fills $self->{_data}, and returns same.
If where clause is set up with bind placeholders, and select is called with bind variables as arguments, it will bind them and be safe from injection. if called with straight up variables from the net, it will be vulnerable. As you will.
gets or sets the table queried
string.
Updates fields in data, modified by vars
Hashref of data. Each key is the primary key off a record, and the value is a hash whose keys are fieldnames and values are field contents.
modifiers to data
Returns updateadditional information for recordset.
Returns updatedefaults information for recordset
Returns validator hashref for field.
Name of field to be tested.
Untaints and returns true only if the given string is a field included in the database
Due to the dynamic nature of the Ajax objects, it's not possible to bind all variables coming in from the web. This is not ideal. However, we can guard from sql injection attacks by refusing to include strings that contain characters beyond A-Za-z0-9_-, and verify that the field in question is part of your recordset. If your database structure has special characters in it's table names, go out back and hit yourself with a brick. Shame on you.
Returns array or arrayref of labels for non-hidden fields.
Returns array or arrayref of field names that are not hidden
Gets or sets the where clause. If called with a single argument, argument is assumed to be sql string for the where clause. If called with multiple args, first element is sql string, everything else is bind values. When called without arguments, it returns whatever's been set. This could be a single string, or an array ref, depending on how it was last called.
list of bind vars
Returns writefunc set for field.
To install CGI::Lazy, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Lazy
CPAN shell
perl -MCPAN -e shell install CGI::Lazy
For more information on module installation, please visit the detailed CPAN module installation guide.