@@ -3,6 +3,8 @@ public abstract class SObjectRepository implements ISObjectRepository {
33 public enum SortOrder { ASCENDING , DESCENDING }
44 public enum NullsSortOrder { FIRST , LAST }
55
6+ public enum SearchGroup { ALL_FIELDS , NAME_FIELDS , EMAIL_FIELDS , PHONE_FIELDS , SIDEBAR_FIELDS }
7+
68 private SObjectType sobjectType ;
79 private Map <String , Schema .SObjectField > sobjectTypeFieldMap ;
810 private Set <String > queryFields ;
@@ -75,25 +77,35 @@ public abstract class SObjectRepository implements ISObjectRepository {
7577 return this ;
7678 }
7779
78- protected SObjectRepository setAsUpdate (Boolean bool ) {
79- this .forUpdate = bool ;
80+ protected SObjectRepository setAsUpdate () {
81+ this .forUpdate = true ;
8082 return this ;
8183 }
8284
8385 protected String getQuery () {
84- this .query = ' SELECT ' + String .join (new List <String >(this .queryFields ), ' , ' )
85- + ' FROM ' + this .sobjectType ;
86+ this .query = ' SELECT ' + this .getQueryFieldString ()
87+ + ' FROM ' + this .sobjectType
88+ + this .getWhereClauseString ()
89+ + this .getOrderByString ()
90+ + this .getLimitCountString ()
91+ + this .getForUpdateString ();
92+
93+ System .debug (this .query );
8694
87- // Generate the WHERE clause
88- if ( ! this . whereClauseList . isEmpty ()) this . query += ' WHERE ' + String . join ( this . whereClauseList , ' AND ' );
95+ return this . query ;
96+ }
8997
90- // Generate the ORDER BY clause
91- if (! this .orderByList .isEmpty ()) this .query += ' ORDER BY ' + String .join (new List <String >(orderByList ), ' , ' );
98+ protected String getSearchQuery (String searchTerm , SObjectRepository.SearchGroup searchGroup ) {
99+ this .query = ' FIND ' + StringUtils .wrapInSingleQuotes (searchTerm )
100+ + ' IN ' + searchGroup .name ().replace (' _' , ' ' )
101+ + ' RETURNING ' + this .sobjectType + ' ('
102+ + this .getQueryFieldString ()
103+ + this .getWhereClauseString ()
104+ + this .getOrderByString ()
105+ + this .getLimitCountString ()
106+ + ' )' ;
92107
93- // Add the LIMIT if provided
94- if (this .limitCount != null ) this .query += ' LIMIT ' + this .limitCount ;
95- // Mark the query as FOR UPDATE if true. You can't use ORDER BY and FOR UPDATE together
96- if (this .orderByList .isEmpty () && this .forUpdate ) this .query += ' FOR UPDATE' ;
108+ if (this .forUpdate ) System .debug (LoggingLevel .WARN , ' getSearchQuery method flagged as FOR UPDATE. SOSL cannot use FOR UPDATE, ignoring' );
97109
98110 System .debug (this .query );
99111
@@ -121,4 +133,32 @@ public abstract class SObjectRepository implements ISObjectRepository {
121133 for (Schema .FieldSetMember field : this .fieldSet .getFields ()) this .queryFields .add (field .getFieldPath ());
122134 }
123135
136+ private String getQueryFieldString () {
137+ return String .join (new List <String >(this .queryFields ), ' ,' );
138+ }
139+
140+ private String getWhereClauseString () {
141+ String whereClauseString = ' ' ;
142+ if (! this .whereClauseList .isEmpty ()) whereClauseString = ' WHERE ' + String .join (this .whereClauseList , ' AND ' );
143+ return whereClauseString ;
144+ }
145+
146+ private String getOrderByString () {
147+ String orderByString = ' ' ;
148+ if (! this .orderByList .isEmpty ()) orderByString = ' ORDER BY ' + String .join (new List <String >(orderByList ), ' , ' );
149+ return orderByString ;
150+ }
151+
152+ private String getLimitCountString () {
153+ String limitString = ' ' ;
154+ if (this .limitCount != null ) limitString = ' LIMIT ' + this .limitCount ;
155+ return limitString ;
156+ }
157+
158+ private String getForUpdateString () {
159+ String forUpdateString = ' ' ;
160+ if (this .orderByList .isEmpty () && this .forUpdate ) forUpdateString = ' FOR UPDATE' ;
161+ return forUpdateString ;
162+ }
163+
124164}
0 commit comments