[vhffs-dev] [2088] reworked Vhffs::Group::getall using prepared statements

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 2088
Author:   gradator
Date:     2012-03-02 10:41:53 +0100 (Fri, 02 Mar 2012)
Log Message:
-----------
reworked Vhffs::Group::getall using prepared statements

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Group.pm

Modified: trunk/vhffs-api/src/Vhffs/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Group.pm	2012-03-02 00:29:04 UTC (rev 2087)
+++ trunk/vhffs-api/src/Vhffs/Group.pm	2012-03-02 09:41:53 UTC (rev 2088)
@@ -203,28 +203,28 @@
 	my $state = shift;
 	my $name = shift;
 
-	my $db = $vhffs->get_db_object;
-	my @result;
-	my $query = 'SELECT groupname FROM vhffs_groups g INNER JOIN vhffs_object o ON g.object_id=o.object_id LEFT OUTER JOIN vhffs_users u ON u.username = g.groupname WHERE u.username IS NULL ';
+	my $groups = [];
+	my @params;
+	my $sql = 'SELECT groupname FROM vhffs_groups g INNER JOIN vhffs_object o ON g.object_id=o.object_id LEFT OUTER JOIN vhffs_users u ON u.username = g.groupname WHERE u.username IS NULL ';
 
-	$query .= " AND o.state=$state " if( defined $state );
-	$query .= " AND g.groupname LIKE '%".$name."%' " if( defined $name );
+	if(defined $state) {
+		$sql .= ' AND o.state = ?';
+		push @params, $state;
+	}
+	if(defined $name) {
+		$sql .= ' AND g.groupname LIKE ?';
+		push @params, '%'.$name.'%';
+	}
+	$sql .= ' ORDER BY g.groupname';
 
-	$query .= " ORDER BY g.groupname";
+	my $dbh = $vhffs->get_db_object();
+	my $sth = $dbh->prepare($sql);
+	$sth->execute(@params);
+	while(my @d = $sth->fetchrow_array) {
+		push @$groups, get_by_groupname($vhffs, $d[0]);
+	}
 
-	my $request = $db->prepare( $query );
-	my $rows = $request->execute;
-
-	return undef if( $rows <= 0 );
-
-	my $names = $request->fetchall_arrayref;
-
-	my $group;
-	foreach $name ( @{$names} ) {
-		$group = Vhffs::Group::get_by_groupname( $vhffs , $name->[0] );
-		push( @result , $group) if( defined $group );
-	}
-	return \@result;
+	return $groups;
 }
 
 =pod


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/