[interchange-cvs] interchange - heins modified code/SystemTag/tree.coretag

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Sun Dec 8 01:16:01 2002


User:      heins
Date:      2002-12-08 06:15:55 GMT
Modified:  code/SystemTag tree.coretag
Log:
* Rework [tree ...] tag to use DBI placeholders if supported.
  While this doesn't seem to save too much CPU on a machine that
  has the database and IC on the same machine, it should relieve
  some network bandwidth on distributed setups.

  If it is an IC database, uses $db->query() as before.

Revision  Changes    Path
1.4       +42 -9     interchange/code/SystemTag/tree.coretag


rev 1.4, prev_rev 1.3
Index: tree.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/tree.coretag,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- tree.coretag	20 Aug 2002 04:21:55 -0000	1.3
+++ tree.coretag	8 Dec 2002 06:15:55 -0000	1.4
@@ -54,7 +54,7 @@
 		$where .= " AND ($opt->{where})";
 	}
 
-	my $qb = "select * from $table where $parent = $qkey$where$sort";
+	my $qb = "SELECT * FROM $table WHERE $parent = $qkey$where$sort";
 
 	my $ary = $db->query( {
 							hashref => 1,
@@ -82,6 +82,46 @@
 
 	my $enable;
 
+	my $qsub;
+
+	my $donemsg;
+	my $dbh = $db->dbh();
+	my $qs_query = "SELECT * FROM $table WHERE $parent = ?$where$sort";
+	if($dbh and $db->config('Class') eq 'DBI') {
+		my $sth = $dbh->prepare($qs_query)
+				or die errmsg(
+						"tree failed to prepare query: %s\nError was: %s",
+						$qs_query,
+						$DBI::errstr,
+						);
+		$qsub = sub {
+#::logDebug("executing query sub DBI style"); # while ! $donemsg++;
+			my $parm = shift;
+			my @ary;
+			$sth->execute($parm)
+				or die errmsg(
+						"tree failed to prepare query for '%s': %s\nError was: %s",
+						$parm,
+						$qs_query,
+						$DBI::errstr,
+						);
+			while(my $ref = $sth->fetchrow_hashref()) {
+				push @ary, { %$ref };
+			}
+			return \@ary;
+		};
+	}
+	else {
+		$qsub = sub {
+			my $parm = shift;
+#::logDebug("executing query sub regular style"); # while ! $donemsg++;
+			$parm = $db->quote($parm, $parent);
+			my $q = $qs_query;
+			$q =~ s/\s\?\s/ $parm /;
+			$db->query( { hashref => 1, sql => $q });
+		};
+	}
+
 
 	$memo = {} if ! $memo;
 
@@ -146,15 +186,8 @@
 
 			my $a;
 			if ($opt->{autodetect} or ! $stop) {
-				my $key = $db->quote($next, $parent);
-				my $q = "SELECT * FROM $table WHERE $parent = $key$where$sort";
 #::logDebug("next row query=$q");
-				$a = $db->query(
-									{ 
-										hashref => 1,
-										sql => $q,
-									}
-						);
+				$a = $qsub->($next);
 				$above->{$next} = 1 if $a and scalar @{$a};
 			}