+#######################
+# _process_local_srch #
+#######################
+
+# Makes the local part of the db search query
+# Puts search prams local to this table in where array.
+# Returns a where array ref and search criteria string.
+# This is factored out of do_search so sub classes can override this part
+sub _process_local_srch {
+ my ($self, $hashed) = @_;
+ my %fields = map { $_ => 1 } $self->columns;
+ my $moniker = $self->moniker;
+ my %colnames = $self->column_names;
+ my $srch_crit = '';
+ my ($oper, $wc);
+ my @where = map {
+ # prelim
+ $srch_crit .= ' '.$colnames{$_}." = '".$hashed->{$_}."'";
+ $oper = $self->sql_search_oper($_);
+ $wc = $oper =~ /LIKE/i ? '%':''; # match any substr
+ "$moniker.$_ $oper '$wc" . $hashed->{$_} . "$wc'"; #the where clause
+ }
+ grep { defined $hashed->{$_} && length ($hashed->{$_}) && $fields{$_} }
+ keys %$hashed;
+
+ return (\@where, $srch_crit);
+}
+
+#########################
+# _process_foreign_srch #
+#########################
+
+# puts foreign search fields into select statement
+# changes @where by ref and return sel and srch_criteria string
+sub _process_foreign_srch {
+ my ($self, $hashed, $sel, $where, $srch_crit) = @_;
+ my %colnames = $self->column_names;
+ my $moniker = $self->moniker;
+ my %foreign;
+ foreach (keys %$hashed) {
+ $foreign{$_} = delete $hashed->{$_} if ref $hashed->{$_};
+ }
+ my %accssr_class = %{$self->accessor_classes};
+ while (my ( $accssr, $prms) = each %foreign ) {
+ my $fclass = $accssr_class{$accssr};
+ my %fields = map { $_ => 1 } $fclass->columns;
+ my %colnames = $fclass->column_names;
+ my ($oper, $wc);
+ my @this_where =
+ # TODO make field name match in all cases in srch crit
+ map {
+ # prelim
+ $srch_crit.= ' '.$colnames{$_}." = '".$prms->{$_}."'";
+ $oper = $fclass->sql_search_oper($_);
+ $wc = $oper =~ /LIKE/i ? '%':'';
+ "$accssr.$_ $oper '$wc".$prms->{$_}."$wc'"; # the where
+ }
+ grep { defined $prms->{$_} && length ($prms->{$_}) && $fields{$_} }
+ keys %$prms;
+
+ next unless @this_where;
+ $sel .= ", " . $fclass->table . " $accssr"; # add foreign tables to from
+
+ # map relationships -- TODO use constraints in has_many and mhaves
+ # and make general
+ my $pk = $self->primary_column;
+ if ($fclass->find_column('owner_id') && $fclass->find_column('owner_table') ) {
+ unshift @this_where, ("$accssr.owner_id = $moniker.$pk",
+ "$accssr.owner_table = '" . $self->table ."'");
+ }
+ # for has_own, has_a where foreign id is in self's table
+ elsif ( my $fk = $self->find_column($fclass->primary_column) ) {
+ unshift @this_where, "$accssr." . $fk->name . " = $moniker." . $fk->name;
+ }
+ push @$where, @this_where;
+ }
+ return ($sel, $srch_crit);