diff --git a/lib/WebGUI/Crud.pm b/lib/WebGUI/Crud.pm index d53ce5d0b..de38ed527 100644 --- a/lib/WebGUI/Crud.pm +++ b/lib/WebGUI/Crud.pm @@ -402,7 +402,7 @@ sub getAllIterator { my @objects; my $ids = $class->getAllIds($session, $options, @_); my $sub = sub { - my ($id) = $ids->array; + my ($id) = shift @{$ids}; return if !$id; my $object = $class->new($session, $id); if (!$object) { @@ -453,8 +453,8 @@ sub getAllSql { # limit to our sequence my $sequenceKey = $class->crud_getSequenceKey; - unless ($options->{sequenceKeyValue} && $sequenceKey) { - $sql .= $dbh->quote_identifier($sequenceKey)."=?"; + if ($options->{sequenceKeyValue} && $sequenceKey) { + push @where, $dbh->quote_identifier($sequenceKey)."=?"; } # merge all clauses with the main query @@ -599,9 +599,13 @@ sub reorder { my $dbh = $db->dbh; # find all the items in this sequence + my @params = (); + if ($sequenceKey) { + push @params, $sequenceKeyValue; + } my $clause = ($sequenceKey) ? "where ".$dbh->quote_identifier($sequenceKey)."=?" : ''; my $current = $db->read("select ".$dbh->quote_identifier($tableKey)." from ".$dbh->quote_identifier($tableName)." - $clause order by sequenceNumber", [$sequenceKeyValue]); + $clause order by sequenceNumber", \@params); # query to update items in the sequence $clause = ($sequenceKey) ? "and ".$dbh->quote_identifier($sequenceKey)."=?" : ''; @@ -614,7 +618,11 @@ sub reorder { if ($id eq $self->getId) { $objectData{id $self} = $i; } - $change->execute([$i, $id, $sequenceKeyValue]); + my @params = ($i, $id); + if ($sequenceKey) { + push @params, $sequenceKeyValue; + } + $change->execute(\@params); $i++; } $db->commit; diff --git a/t/Crud.t b/t/Crud.t index c300e428c..ba65cc498 100644 --- a/t/Crud.t +++ b/t/Crud.t @@ -28,7 +28,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 29; # Increment this number for each test you create +plan tests => 43; # Increment this number for each test you create #---------------------------------------------------------------------------- @@ -87,6 +87,29 @@ is($record2->get('sequenceNumber'), 3, "demotion from middle works"); $record2->promote; is($record2->get('sequenceNumber'), 2, "promotion from middle works"); +# deleting +ok($record2->delete, "deletion reports success"); +my $copyOfRecord3 = WebGUI::Crud->new($session, $record3->getId); +my $copyOfRecord4 = WebGUI::Crud->new($session, $record4->getId); +is($copyOfRecord3->get('sequenceNumber'), '2', "deletion of record 2 moved record 3 to sequence 2"); +is($copyOfRecord4->get('sequenceNumber'), '3', "deletion of record 2 moved record 4 to sequence 3"); + +# updating +sleep 1; +ok($copyOfRecord4->update, "update returns success"); +isnt($copyOfRecord4->get('lastUpdated'), $copyOfRecord4->get('dateCreated'), "updates work"); + +# retrieve data +is(WebGUI::Crud->getAllSql($session), "select `id` from `unnamed_crud_table` order by sequenceNumber", "getAllSql() no options"); +is(WebGUI::Crud->getAllSql($session,{sequenceKeyValue=>1}), "select `id` from `unnamed_crud_table` order by sequenceNumber", "getAllSql() sequence key value with no key specified"); +is(WebGUI::Crud->getAllSql($session,{limit=>5}), "select `id` from `unnamed_crud_table` order by sequenceNumber limit 5", "getAllSql() with a row limit"); +is(WebGUI::Crud->getAllSql($session,{limit=>[10,20]}), "select `id` from `unnamed_crud_table` order by sequenceNumber limit 10,20", "getAllSql() with a start and row limit"); +is(WebGUI::Crud->getAllSql($session,{orderBy=>'lastUpdated'}), "select `id` from `unnamed_crud_table` order by `lastUpdated`", "getAllSql() with a custom order by clause"); +is(scalar(@{WebGUI::Crud->getAllIds($session)}), 3, "getAllIds()"); +my $iterator = WebGUI::Crud->getAllIterator($session); +while (my $object = $iterator->()) { + isa_ok($object, 'WebGUI::Crud'); +} #---------------------------------------------------------------------------- # Cleanup