summaryrefslogtreecommitdiff
path: root/FS/FS/Record.pm
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2015-03-17 20:46:35 -0700
committerMark Wells <mark@freeside.biz>2015-03-17 20:46:35 -0700
commit2fea4708e3e52c53e070423e53716b4cced632e1 (patch)
tree4fc6f498e22acdecbd1af08e64d457d11fda51ae /FS/FS/Record.pm
parente56766d7199b258f943c33dc471a0f0f33ceaf7e (diff)
qualify column names correctly from sql_h_search, #20688, #22232
Diffstat (limited to 'FS/FS/Record.pm')
-rw-r--r--FS/FS/Record.pm15
1 files changed, 12 insertions, 3 deletions
diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm
index 0f3685b..5f76718 100644
--- a/FS/FS/Record.pm
+++ b/FS/FS/Record.pm
@@ -399,10 +399,17 @@ sub qsearch {
my @real_fields = grep exists($record->{$_}), real_fields($table);
my $statement .= "SELECT $select FROM $stable";
- $statement .= " $addl_from" if $addl_from;
+ my $alias_main;
+ if ( $addl_from ) {
+ $statement .= " $addl_from";
+ # detect aliasing of the main table
+ if ( $addl_from =~ /^\s*AS\s+(\w+)/i ) {
+ $alias_main = $1;
+ }
+ }
if ( @real_fields ) {
$statement .= ' WHERE '. join(' AND ',
- get_real_fields($table, $record, \@real_fields));
+ get_real_fields($table, $record, \@real_fields, $alias_main));
}
$statement .= " $extra_sql" if defined($extra_sql);
@@ -751,6 +758,8 @@ sub get_real_fields {
my $table = shift;
my $record = shift;
my $real_fields = shift;
+ my $alias_main = shift; # defaults to undef
+ $alias_main ||= $table;
## could be optimized more for readability
return (
@@ -758,7 +767,7 @@ sub get_real_fields {
my $op = '=';
my $column = $_;
- my $table_column = $qsearch_qualify_columns ? "$table.$column" : $column;
+ my $table_column = $qsearch_qualify_columns ? "$alias_main.$column" : $column;
my $type = dbdef->table($table)->column($column)->type;
my $value = $record->{$column};
$value = $value->{'value'} if ref($value);