Skip to content

Commit

Permalink
Release v2.0.1 (#47)
Browse files Browse the repository at this point in the history
* To Field when empty (#45)

* Feature/35 use iterable set (#46)

* Use Iterable Set

* Use Iterable for List and Set

* Test update

* custom LIKE

* binder refactoring

* soql of string

* soql filter update

* documentation update
  • Loading branch information
pgajek2 authored Jun 27, 2023
1 parent ab6e6fa commit 1bd4e75
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 161 deletions.
131 changes: 38 additions & 93 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public inherited sharing class SOQL implements Queryable {

public static SubQuery SubQuery {
get {
return new QSubQuery(binder);
return new QSubQuery();
}
}

Expand All @@ -29,13 +29,13 @@ public inherited sharing class SOQL implements Queryable {

public static Filter Filter {
get {
return new QFilter(binder);
return new QFilter();
}
}

public static InnerJoin InnerJoin {
get {
return new QJoinQuery(binder);
return new QJoinQuery();
}
}

Expand All @@ -45,6 +45,7 @@ public inherited sharing class SOQL implements Queryable {

public interface Queryable {
Queryable of(SObjectType ofObject);
Queryable of(String ofObject); // Dynamic SOQL

Queryable with(SObjectField field);
Queryable with(SObjectField field1, SObjectField field2);
Expand Down Expand Up @@ -109,8 +110,7 @@ public inherited sharing class SOQL implements Queryable {

Queryable byId(SObject record);
Queryable byId(Id recordId);
Queryable byIds(Set<Id> recordIds);
Queryable byIds(List<Id> recordIds);
Queryable byIds(Iterable<Id> recordIds); // List or Set
Queryable byIds(List<SObject> records);

String toString();
Expand Down Expand Up @@ -183,10 +183,11 @@ public inherited sharing class SOQL implements Queryable {
Filter contains(String value); // LIKE :'%' + value + '%'
Filter endsWith(String value); // LIKE :'%' + value
Filter startsWith(String value); // LIKE :value + '%'
Filter isIn(Set<String> inSet); // IN :inSet
Filter contains(String prefix, String value, String suffix); // custom LIKE
Filter isIn(Iterable<Object> iterable); // IN :inList or inSet
Filter isIn(List<Object> inList); // IN :inList
Filter isIn(InnerJoin joinQuery); // SOQL.InnerJoin
Filter notIn(Set<String> inSet); // NOT IN :inSet
Filter notIn(Iterable<Object> iterable); // NOT IN :inList or inSet
Filter notIn(List<Object> inList); // NOT IN :inList
Filter notIn(InnerJoin joinQuery); // SOQL.InnerJoin

Expand Down Expand Up @@ -219,27 +220,26 @@ public inherited sharing class SOQL implements Queryable {
mock.setCountMock(mockId, amount);
}

@TestVisible
private static void setMock(String mockId, String staticResource) {
mock.setStaticResourceMock(mockId, staticResource);
}

// Config

private static Mock mock = new Mock();
private static Binder binder;

private QueryBuilder builder;
private static Binder binder;
private Executor executor;

public static SOQL of(sObjectType ofObject) {
return new SOQL(ofObject.getDescribe().getName());
}

public static SOQL of(String ofObject) {
return new SOQL(ofObject);
}

private SOQL(SObjectType ofObject) {
private SOQL(String ofObject) {
binder = new Binder();
executor = new Executor();
builder = new QueryBuilder(ofObject);
executor = new Executor(ofObject);
}

public SOQL with(SObjectField field) {
Expand Down Expand Up @@ -477,7 +477,7 @@ public inherited sharing class SOQL implements Queryable {
}

public Object toField(SObjectField fieldToExtract) {
return with(fieldToExtract).toObject().get(fieldToExtract);
return with(fieldToExtract).toObject()?.get(fieldToExtract);
}

public Integer toInteger() {
Expand Down Expand Up @@ -512,11 +512,7 @@ public inherited sharing class SOQL implements Queryable {
return whereAre(Filter.id().equal(recordId));
}

public SOQL byIds(Set<Id> recordIds) {
return byIds(new List<Id>(recordIds));
}

public SOQL byIds(List<Id> recordIds) {
public SOQL byIds(Iterable<Id> recordIds) {
return whereAre(Filter.id().isIn(recordIds));
}

Expand All @@ -531,10 +527,6 @@ public inherited sharing class SOQL implements Queryable {
private class QueryBuilder implements QueryClause {
private List<QueryClause> clauses = new QueryClause[10];

public QueryBuilder(SObjectType ofObject) {
this(ofObject.getDescribe().getName());
}

public QueryBuilder(String ofObject) {
clauses.set(0, new QFields());
clauses.set(2, new QFrom(ofObject));
Expand Down Expand Up @@ -701,11 +693,6 @@ public inherited sharing class SOQL implements Queryable {

private class QSubQuery implements SubQuery {
private QueryBuilder builder;
private Binder binder;

public QSubQuery(Binder binder) {
this.binder = binder;
}

public QSubQuery of(String ofObject) {
builder = new QueryBuilder(ofObject);
Expand Down Expand Up @@ -1016,12 +1003,7 @@ public inherited sharing class SOQL implements Queryable {
private String comperator;
private Object value;
private DisplayType fieldType;

private Binder binder;

public QFilter(Binder binder) {
this.binder = binder;
}
private Boolean skipBinding = false;

public Filter id() {
return with('Id');
Expand All @@ -1036,12 +1018,12 @@ public inherited sharing class SOQL implements Queryable {
}

public Filter with(SObjectField field) {
fieldType = field.getDescribe().getType();
skipBinding = TYPES_WITHOUT_BINDING.contains(field.getDescribe().getType());
return with(field.getDescribe().getName());
}

public Filter with(String relationshipName, SObjectField field) {
fieldType = field.getDescribe().getType();
skipBinding = TYPES_WITHOUT_BINDING.contains(field.getDescribe().getType());
return with(relationshipName + '.' + field);
}

Expand Down Expand Up @@ -1095,46 +1077,40 @@ public inherited sharing class SOQL implements Queryable {
}

public Filter contains(String value) {
return set('LIKE', '%' + formattedString(value) + '%');
return contains('%', formattedString(value), '%');
}

public Filter endsWith(String value) {
return set('LIKE', '%' + formattedString(value));
return contains('%', formattedString(value), '');
}

public Filter startsWith(String value) {
return set('LIKE', formattedString(value) + '%');
return contains('', formattedString(value), '%');
}

private String formattedString(String value) {
return value == null ? value : String.escapeSingleQuotes(value.trim());
public Filter contains(String prefix, String value, String suffix) {
return set('LIKE', prefix + formattedString(value) + suffix);
}

// The Set collection type does not allow to implicitly cast Set<Object>
public Filter isIn(Set<String> inSet) {
return set('IN', inSet);
private String formattedString(String value) {
return value == null ? value : String.escapeSingleQuotes(value.trim());
}

public Filter isIn(List<Object> inList) {
return set('IN', inList);
public Filter isIn(Iterable<Object> iterable) {
return set('IN', iterable);
}

public Filter isIn(InnerJoin joinQuery) {
binder = null;
skipBinding = true;
return set('IN', joinQuery);
}

// The Set collection type does not allow to implicitly cast Set<Object>
public Filter notIn(Set<String> inSet) {
return set('NOT IN', inSet);
}

public Filter notIn(List<Object> inList) {
return set('NOT IN', inList);
public Filter notIn(Iterable<Object> iterable) {
return set('NOT IN', iterable);
}

public Filter notIn(InnerJoin joinQuery) {
binder = null;
skipBinding = true;
return set('NOT IN', joinQuery);
}

Expand All @@ -1161,28 +1137,19 @@ public inherited sharing class SOQL implements Queryable {
}

public override String toString() {
if (buildWithoutBinding()) {
if (skipBinding) {
return field + ' ' + comperator + ' ' + value;
}

return field + ' ' + comperator + ' :' + binder.bind(value);
}

private Boolean buildWithoutBinding() {
return binder == null || (fieldType != null && TYPES_WITHOUT_BINDING.contains(fieldType));
}
}

private class QJoinQuery implements InnerJoin {
private QueryBuilder builder;
private Binder binder;

public QJoinQuery(Binder binder) {
this.binder = binder;
}

public QJoinQuery of(SObjectType ofObject) {
builder = new QueryBuilder(ofObject);
builder = new QueryBuilder(ofObject.getDescribe().getName());
return this;
}

Expand Down Expand Up @@ -1218,7 +1185,7 @@ public inherited sharing class SOQL implements Queryable {
}

public override String toString() {
return 'GROUP BY ' + String.join(new List<String>(groupByFields), ', ');
return 'GROUP BY ' + String.join(groupByFields, ', ');
}
}

Expand Down Expand Up @@ -1347,7 +1314,6 @@ public inherited sharing class SOQL implements Queryable {

private class Mock {
private final Map<String, List<SObject>> mocks = new Map<String, List<SObject>>();
private final Map<String, String> staticResourceMocks = new Map<String, String>();
private final Map<String, Integer> countMocks = new Map<String, Integer>();

public void setMock(String mockId, List<SObject> records) {
Expand All @@ -1358,30 +1324,18 @@ public inherited sharing class SOQL implements Queryable {
countMocks.put(mockId, amount);
}

public void setStaticResourceMock(String mockId, String staticResourceName) {
staticResourceMocks.put(mockId, staticResourceName);
}

public Boolean hasMock(String mockId) {
return mocks.containsKey(mockId);
}

public Boolean hasStaticResourceMock(String mockId) {
return staticResourceMocks.containsKey(mockId);
public Boolean hasCountMock(String mockId) {
return countMocks.containsKey(mockId);
}

public List<SObject> getMocks(String mockId) {
return mocks.get(mockId);
}

public List<SObject> getStaticResourceMocks(String mockId, SObjectType sObjectType) {
return Test.loadData(sObjectType, staticResourceMocks.get(mockId));
}

public Boolean hasCountMock(String mockId) {
return countMocks.containsKey(mockId);
}

public Integer getCountMock(String mockId) {
return countMocks.get(mockId);
}
Expand All @@ -1390,14 +1344,9 @@ public inherited sharing class SOQL implements Queryable {
private inherited sharing class Executor {
private AccessLevel accessMode = AccessLevel.USER_MODE; // The object permissions, field-level security, sharing rules are enforced.
private DatabaseQuery sharingExecutor = new InheritedSharing();
private SObjectType ofObject;
private AccessType accessType;
private String mockId;

public Executor(SObjectType ofObject) {
this.ofObject = ofObject;
}

public void withSharing() {
sharingExecutor = new WithSharing();
}
Expand Down Expand Up @@ -1439,10 +1388,6 @@ public inherited sharing class SOQL implements Queryable {
return mock.getMocks(mockId);
}

if (mock.hasStaticResourceMock(mockId)) {
return mock.getStaticResourceMocks(mockId, ofObject);
}

if (accessType == null) {
return sharingExecutor.execute(query, binding, accessMode);
}
Expand Down
4 changes: 2 additions & 2 deletions force-app/main/default/classes/SOQL.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>
</ApexClass>
Loading

1 comment on commit 1bd4e75

@vercel
Copy link

@vercel vercel bot commented on 1bd4e75 Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.