[svn:p5ee] r9452 - p5ee/trunk/App-Repository/lib/App/ValueDomain

[email protected]
Newsgroups perl.cvs.p5ee
Message-ID <[email protected]>
Author: spadkins
Date: Wed Apr 25 12:40:06 2007
New Revision: 9452

Modified:
   p5ee/trunk/App-Repository/lib/App/ValueDomain/Repository.pm

Log:
add support for values_string_template, the : syntax on values_string, and Service.substitute() instead of homegrown

Modified: p5ee/trunk/App-Repository/lib/App/ValueDomain/Repository.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/ValueDomain/Repository.pm	(original)
+++ p5ee/trunk/App-Repository/lib/App/ValueDomain/Repository.pm	Wed Apr 25 12:40:06 2007
@@ -67,7 +67,14 @@
     my ($values, $labels, $needs_loading, $refresh_interval, $time);
     my ($method, $args, $rows, $row);
 
-    $values_string ||= "";
+    if (!defined $values_string) {
+        if (defined $self->{values_string_template}) {
+            $values_string = $self->substitute($self->{values_string_template});
+        }
+        else {
+            $values_string = "";
+        }
+    }
 
     $values = $self->{values};
     $labels = $self->{labels};
@@ -79,9 +86,9 @@
     # then the values/labels indeed need loading
     my $repository  = $self->{repository};
 
-    if (!defined $values || !defined $labels ||                    # never loaded them yet
-        (!$values_string && $self->{values_string}) ||   # asking for the whole domain, only subset loaded
-        (defined $values_string && defined $self->{values_string} &&  # asking for a different subset than is loaded
+    if (!defined $values || !defined $labels ||                     # never loaded them yet
+        ($values_string eq "" && $self->{values_string}) ||         # asking for the whole domain, only subset loaded
+        ($values_string ne "" && defined $self->{values_string} &&  # asking for a different subset than is loaded
          $values_string ne $self->{values_string})) {
         $needs_loading = 1;
     }
@@ -108,39 +115,24 @@
         my $labelcolumn = $self->{labelcolumn};
         $labelcolumn = "" if ($labelcolumn eq $valuecolumn);
         my $params      = $self->{params} || {};
-        my %params      = %$params;
         my $sql         = $self->{sql};
 
-        # TODO. I might want to use [$params = $self->substitute($params);] here
-        my ($key, $keyvalue, $wname, $wvalue);
-        foreach my $key (keys %params) {
-            $keyvalue = $params{$key};
-            while ($keyvalue =~ /\{([A-Za-z0-9\._-]+)\}/) {
-                $wname = $1;
-                $wvalue = $context->so_get($wname);
-                if (defined $wvalue) {
-                    $keyvalue =~ s/\{$wname\}/$wvalue/g;
-                }
-                else {
-                    $keyvalue =~ s/\{$wname\}/NULL/g;
-                }
-            }
-            $params{$key} = $keyvalue;
-        }
+        my ($key);
+        my $subst_params = $self->substitute($params, undef, {undef_value => "NULL"});
 
-        if (defined $values_string && $values_string ne "") {
+        if (defined $values_string && $values_string ne "" && $values_string !~ /^:/) {
             my $values_string_columns = $self->{values_string_columns};
             if ($values_string_columns) {
                 $values_string_columns = [split(/,/, $values_string_columns)] if (!ref($values_string_columns));
                 my @values_string_values = split(/,/, $values_string);
                 if ($#values_string_values > -1 && $#$values_string_columns == $#values_string_values) {
                     for (my $i = 0; $i <= $#$values_string_columns; $i++) {
-                        $params{$values_string_columns->[$i]} = $values_string_values[$i];
+                        $subst_params->{$values_string_columns->[$i]} = $values_string_values[$i];
                     }
                 }
             }
             else {
-                $params{$valuecolumn} = $values_string;
+                $subst_params->{$valuecolumn} = $values_string;
             }
         }
 
@@ -151,7 +143,7 @@
             if ($self->{order_by}) {
                 $options = { order_by => $self->{order_by} };
             }
-            $rows   = $rep->get_rows($table, \%params, \@cols, $options);
+            $rows   = $rep->get_rows($table, $subst_params, \@cols, $options);
             $values = [];
             $labels = {};
             foreach $row (@$rows) {
@@ -183,17 +175,9 @@
         }
 
         if ($sql) {
-            # TODO. I might want to use [$params = $self->substitute($params);] here
-            while ($sql =~ /\{([A-Za-z0-9\._-]+)\}/g) {
-                $wname = $1;
-                $wvalue = $context->so_get($wname);
-                if (defined $wvalue && $wvalue ne "") {
-                    $sql =~ s/\{$wname\}/$wvalue/g;
-                }
-                else {
-                    $sql =~ s/\{$wname\}/NULL/g;
-                }
-            }
+            $sql = $self->substitute($sql, undef, {undef_value => "NULL"});
+
+            # TODO. I might want to use $db->_do($sql) here.
             my $dbh = $rep->{dbh};
             my $sth = $dbh->prepare($sql);
             return("Prepare error: $DBI::errstr\n") if ($DBI::err);
@@ -225,12 +209,6 @@
     $values = [] if (! defined $values);
     $labels = {} if (! defined $labels);
 
-    if (defined $values_string && $values_string ne "") {
-        return($labels);
-    }
-    else {
-        return($values,$labels);
-    }
     &App::sub_exit() if ($App::trace);
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.