4 use FS::Record; # don't import qsearch
9 FS::Query - A thin wrapper around qsearch argument hashes.
13 This module exists because we pass qsearch argument lists around a lot,
14 and add new joins or WHERE expressions in several stages, and I got tired
17 my $andwhere = "mycolumn IN('perl','python','javascript')";
18 if ( ($search->{hashref} and keys( %{$search->{hashref}} ))
19 or $search->{extra_sql} =~ /^\s*WHERE/ ) {
20 $search->{extra_sql} .= " AND $andwhere";
22 $search->{extra_sql} = " WHERE $andwhere ";
25 and then having it fail under some conditions if it's done wrong (as the above
26 example is, obviously).
28 We may eventually switch over to SQL::Abstract or something for this, but for
29 now it's a couple of crude manipulations and a wrapper to qsearch.
37 Turns HASHREF (a qsearch argument list) into an FS::Query object. None of
38 the params are really required, but you should at least supply C<table>.
40 In the Future this may do a lot more stuff.
45 my ($class, $hashref) = @_;
56 # load FS::$table? validate anything?
62 Returns another object that's a copy of this one.
68 $self->new( dclone($self) );
73 Adds a constraint to the WHERE clause of the query. All other constraints in
74 the WHERE clause should be joined with AND already; if not, they should be
75 grouped with parentheses.
83 if ($self->{extra_sql} =~ /^\s*(?:WHERE|AND)\s+(.*)/is) {
84 $where = "($where) AND $1";
86 if (keys %{ $self->{hashref} }) {
87 $where = " AND $where";
89 $where = " WHERE $where";
91 $self->{extra_sql} = $where;
98 Runs the query and returns all results.
104 FS::Record::qsearch({ %$self });
109 Runs the query and returns only one result.
115 FS::Record::qsearchs({ %$self });