From c2a445b71e1677110763302f9fdedba9ced5f501 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Sat, 20 Sep 2008 01:16:24 +0000 Subject: [PATCH] added pod added return 1 on success replaced getAll with getAllSql getAllIterator and getAllIds --- lib/WebGUI/Crud.pm | 272 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 257 insertions(+), 15 deletions(-) diff --git a/lib/WebGUI/Crud.pm b/lib/WebGUI/Crud.pm index 80a12821f..b1fa1d571 100644 --- a/lib/WebGUI/Crud.pm +++ b/lib/WebGUI/Crud.pm @@ -13,6 +13,21 @@ readonly session => my %session; #------------------------------------------------------------------- + +=head2 create ( session, [ properties ]) + +Constructor. Creates a new instance of this object. Returns a reference to the object. + +=head3 session + +A reference to a WebGUI::Session. + +=head3 properties + +The properties that you wish to create this object with. Note that if this object has a sequenceKey then that sequence key must be specified in these properties or it will throw an execption. See crud_definition() for a list of all the properties. + +=cut + sub create { my ($class, $session, $data) = @_; @@ -57,6 +72,17 @@ sub create { } #------------------------------------------------------------------- + +=head2 crud_createTable ( session ) + +A management class method used to create the database table using the crud_definition(). Returns 1 on successful completion. + +=head3 session + +A reference to a WebGUI::Session. + +=cut + sub crud_createTable { my ($class, $session) = @_; my $db = $session->db; @@ -68,10 +94,58 @@ sub crud_createTable { lastUpdated datetime )'); $class->crud_updateTable($session); + $db->write('alter table '.dbh->quote_identifier($class->crud_getTableName).' + add index '.$dbh->quote_identifier($class->crud_getTableKey).' ('.$dbh->quote_identifier($class->crud_getTableKey).')'); return 1; } #------------------------------------------------------------------- + +=head2 crud_definition () + +A management class method that returns the properties necessary to construct this object. This should be extended by all subclasses. + +B When you subclass WebGUI::Crud, note the properties you're defining in the POD of this method. That way it's in a consistent place for all subclasses. There are no settable properties by default, but all WebGUI::Crud objects have an id (who's name is set with tableKey), dateCreated, lastUpdated, and sequenceNumber. + +Returns a hash reference that looks like this: + + { + tableName => 'unamed_crud_table', + tableKey => 'id', + sequenceKey => '', + properties => {}, + } + +tableName is the name of the database table that will be used or created by this object. + +tableKey is the name of the column in the database table that will act as the primary key. + +sequenceKey is the name of any field in the table that will be used as a grouping mechanism to allow multiple sequences per table. For example, you might use an assetId so that all items attached to an asset can be ordered independent of other assets. + +properties is a hash reference tied to IxHash so that it maintains its order. It's used to define properties of this objects and columns in the table. It should look like this: + + { + companyName => { + fieldType => 'text', + defaultValue => 'Acme Widgets', + label => 'Company Name', + serialize => 0, + }, + companyWebSite => { + fieldType => 'url', + defaultValue => undef, + serialize => 0, + }, + } + +The properties of each field can be any property associated with a WebGUI::Form::Control. There are two special properties as well. They are fieldType and serialize. + +fieldType is the WebGUI::Form::Control type that you wish to associate with this field. It is required for all fields. Examples are 'HTMLarea', 'text', 'url', 'email', and 'selectBox'. + +serialize tells WebGUI::Crud to automatically serialize this field in a JSON wrapper before storing it to the database, and to convert it back to it's native structure upon retrieving it from the database. This is useful if you wish to persist hash references or array references. + +=cut + sub crud_definition { my $class = shift; tie my %properties, 'Tie::IxHash'; @@ -85,6 +159,17 @@ sub crud_definition { } #------------------------------------------------------------------- + +=head2 crud_dropTable ( session ) + +A management class method that will drop the table created by crud_createTable(). Returns 1 on success. + +=head3 session + +A reference to a WebGUI::Session. + +=cut + sub crud_dropTable { my ($class, $session) = @_; my $db = $session->db; @@ -94,12 +179,26 @@ sub crud_dropTable { } #------------------------------------------------------------------- + +=head2 crud_getProperties () + +A management class method that returns just the 'properties' from crud_definition(). + +=cut + sub crud_getProperties { my $class = shift; return $class->crud_definition->{properties}; } #------------------------------------------------------------------- + +=head2 crud_getSequenceKey () + +A management class method that returns just the 'sequenceKey' from crud_definition(). + +=cut + sub crud_getSequenceKey { my $class = shift; my $definition = $class->crud_definition; @@ -107,18 +206,45 @@ sub crud_getSequenceKey { } #------------------------------------------------------------------- + +=head2 crud_getTableName () + +A management class method that returns just the 'tableName' from crud_definition(). + +=cut + sub crud_getTableName { my $class = shift; return $class->crud_definition->{tableName}; } #------------------------------------------------------------------- + +=head2 crud_getTableKey () + +A management class method that returns just the 'tableKey' from crud_definition(). + +=cut + sub crud_getTableKey { my $class = shift; return $class->crud_definition->{tableKey}; } #------------------------------------------------------------------- + +=head2 crud_updateTable ( session ) + +A management class method that tries to resolve the differences between the database table and the definition. Returns 1 on success. + +B This works perfectly for adding new fields, but is not perfect at making changes to fields. For that reason we recommend you write your own upgrade scripts when making database changes rather than relying upon this API at this time. + +=head3 session + +A reference to a WebGUI::Session. + +=cut + sub crud_updateTable { my ($class, $session) = @_; my $db = $session->db; @@ -158,6 +284,13 @@ sub crud_updateTable { } #------------------------------------------------------------------- + +=head2 delete () + +Deletes this object from the database. Returns 1 on success. + +=cut + sub delete { my $self = shift; $self->session->db->deleteRow($self->crud_getTableName, $self->crud_getTableKey, $self->getId); @@ -166,6 +299,13 @@ sub delete { } #------------------------------------------------------------------- + +=head2 demote () + +Moves this object one position closer to the end of its sequence. If the object is already at the bottom of the sequence then no change will be made. Returns 1 on success. + +=cut + sub demote { my $self = shift; my $tableKey = $self->crud_getTableKey; @@ -194,6 +334,17 @@ sub demote { } #------------------------------------------------------------------- + +=head2 get ( [ property ] ) + +Returns a hash reference of all the properties of this object. + +=head3 property + +If specified, returns the value of the property associated with this this property name. Returns undef if the property doesn't exist. See crud_definition() in the subclass of this class for a complete list of properties. + +=cut + sub get { my ($self, $name) = @_; @@ -208,30 +359,96 @@ sub get { } #------------------------------------------------------------------- -sub getAll { - my ($class, $session, $options) = @_; - my $db = $session->db; - my $dbh = $db->dbh; + +=head2 getAllIds ( ) + +Returns a list of all the ids in this object type. Has the same signature of getAllSql(). + +=cut + +sub getAllIds { + my $class = shift; + my $session = shift; my @objects; - my $ids = $session->db->read("select ".$dbh->quote_identifier($class->crud_getTableKey)." from ".$dbh->quote_identifier($class->crud_getTableName)); + my $ids = $session->db->read($class->getAllSql($session, @_)); while (my ($id) = $ids->array) { - if ($options->{return} eq "ids") { - push @objects, $id; - } - else { - push @objects, $class->new($session, $id); - } + push @objects, $id; } return \@objects; } #------------------------------------------------------------------- + +=head2 getAllIterator ( ) + +Returns an iterator of all the instanciated objects in this object type. Has the same signature of getAllSql(). + +=cut + +sub getAllIterator { + my $class = shift; + my $session = shift; + my @objects; + my $ids = $class->getAllIds($session, @_); + my $sub = sub { + my ($id) = $ids->array; + return if !$id; + my $object = $class->new($session, $id); + if (!$object) { + WebGUI::Error::ObjectNotFound->throw(error=>'no such '.$class->getTableKey, id => $id); + } + return $object; + }; + return $sub; +} + +#------------------------------------------------------------------- + +=head2 getAllSql ( session ) + +Returns the SQL + +=head3 session + +A reference to a WebGUI::Session. + +=cut + +sub getAllSql { + my ($class, $session) = @_; + my $dbh = $session->db->dbh; + return "select ".$dbh->quote_identifier($class->crud_getTableKey)." from ".$dbh->quote_identifier($class->crud_getTableName); +} + +#------------------------------------------------------------------- + +=head2 getId ( ) + +Returns a guid, this object's unique identifier. + +=cut + sub getId { my $self = shift; return $self->objectData->{$self->crud_getTableKey}; } #------------------------------------------------------------------- + +=head2 new ( session, id ) + +Constructor. + +=head3 session + +A reference to a WebGUI::Session. + +=head3 id + +A guid, the unique identifier for this object. + +=cut + sub new { my ($class, $session, $id) = @_; my $tableKey = $class->crud_getTableKey; @@ -254,7 +471,7 @@ sub new { my $properties = $class->crud_getProperties; foreach my $name (keys %{$properties}) { if ($properties->{$name}{serialize}) { - $data->{$name} = JSON->new->decode($data->{$name}); + $data->{$name} = JSON->new->canonical->decode($data->{$name}); } } @@ -267,6 +484,13 @@ sub new { } #------------------------------------------------------------------- + +=head2 promote () + +Moves this object one position closer to the beginning of its sequence. If the object is already at the top of the sequence then no change will be made. Returns 1 on success. + +=cut + sub promote { my $self = shift; my $tableKey = $self->crud_getTableKey; @@ -288,14 +512,21 @@ sub promote { $db->beginTransaction; my ($id) = $db->quickArray("select ".$dbh->quote_identifier($tableKey)." from ".$dbh->quote_identifier($tableName)." where ".$dbh->quote_identifier($sequenceKey)."=? $clause", \@params); if ($id ne "") { - $db->write("update ".$dbh->quote_identifier($tableName)." set sequenceNumber=sequenceNumber-1 where ".$dbh->quote_identifier($tableKey)."=?",[$self->getId]); - $db->write("update ".$dbh->quote_identifier($tableName)." set sequenceNumber=sequenceNumber+1 where ".$dbh->quote_identifier($tableKey)."=?",[$id]); + $db->write("update ".$dbh->quote_identifier($tableName)." set sequenceNumber=sequenceNumber-1 where ".$dbh->quote_identifier($tableKey)."=?"); + $db->write("update ".$dbh->quote_identifier($tableName)." set sequenceNumber=sequenceNumber+1 where ".$dbh->quote_identifier($tableKey)."=?"); } $db->commit; return 1; } #------------------------------------------------------------------- + +=head2 reorder () + +Removes gaps in the sequence. Usually only called by delete(), but may be useful if you randomize a sequence. + +=cut + sub reorder { my ($self) = @_; my $tableKey = $self->crud_getTableKey; @@ -330,6 +561,17 @@ sub reorder { } #------------------------------------------------------------------- + +=head2 update ( properties ) + +Updates an object's properties. While doing so also validates default data and sets the lastUpdated date. + +=head3 properties + +A hash reference of properties to be set. See crud_definition() for a list of the properties available. + +=cut + sub update { my ($self, $data) = @_; @@ -348,7 +590,7 @@ sub update { # serialize if needed if ($properties->{$property}{serialize}) { - $data->{property} = JSON->new->encode($data->{property}); + $data->{property} = JSON->new->canonical->encode($data->{property}); } }