Skip to content

Commit

Permalink
toCursor
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr PG Gajek <[email protected]>
  • Loading branch information
pgajek2 committed Sep 27, 2024
1 parent 4f02835 commit a2770ea
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 15 deletions.
39 changes: 26 additions & 13 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1957,60 +1957,73 @@ public virtual inherited sharing class SOQL implements Queryable {
return sharingExecutor.toInteger(builder.toString(), binder.getBindingMap(), accessMode);
}

public Database.Cursor toCursor() {
return Database.getCursorWithBinds(builder.toString(), binder.getBindingMap(), accessMode);
public Database.QueryLocator toQueryLocator() {
return sharingExecutor.toQueryLocator(builder.toString(), binder.getBindingMap(), accessMode);
}

public Database.QueryLocator toQueryLocator() {
return Database.getQueryLocatorWithBinds(builder.toString(), binder.getBindingMap(), accessMode);
public Database.Cursor toCursor() {
return sharingExecutor.toCursor(builder.toString(), binder.getBindingMap(), accessMode);
}
}

private interface DatabaseQuery {
List<SObject> toSObjects(String query, Map<String, Object> binding, AccessLevel accessLevel);
Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel);
Integer toInteger(String query, Map<String, Object> binding, AccessLevel accessLevel);
Database.QueryLocator toQueryLocator(String query, Map<String, Object> binding, AccessLevel accessLevel);
Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel);
}

private inherited sharing class InheritedSharing implements DatabaseQuery {
public List<SObject> toSObjects(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.queryWithBinds(query, binding, accessLevel);
}

public Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getCursorWithBinds(query, binding, accessLevel);
}

public Integer toInteger(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.countQueryWithBinds(query, binding, accessLevel);
}

public Database.QueryLocator toQueryLocator(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getQueryLocatorWithBinds(query, binding, accessLevel);
}

public Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getCursorWithBinds(query, binding, accessLevel);
}
}

private without sharing class WithoutSharing implements DatabaseQuery {
public List<SObject> toSObjects(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.queryWithBinds(query, binding, accessLevel);
}

public Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getCursorWithBinds(query, binding, accessLevel);
public Database.QueryLocator toQueryLocator(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getQueryLocatorWithBinds(query, binding, accessLevel);
}

public Integer toInteger(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.countQueryWithBinds(query, binding, accessLevel);
}

public Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getCursorWithBinds(query, binding, accessLevel);
}
}

private with sharing class WithSharing implements DatabaseQuery {
public List<SObject> toSObjects(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.queryWithBinds(query, binding, accessLevel);
}

public Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getCursorWithBinds(query, binding, accessLevel);
public Database.QueryLocator toQueryLocator(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getQueryLocatorWithBinds(query, binding, accessLevel);
}

public Integer toInteger(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.countQueryWithBinds(query, binding, accessLevel);
}

public Database.Cursor toCursor(String query, Map<String, Object> binding, AccessLevel accessLevel) {
return Database.getCursorWithBinds(query, binding, accessLevel);
}
}
}
64 changes: 62 additions & 2 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2648,16 +2648,76 @@ private class SOQL_Test {
Assert.areEqual('SELECT Id, Name FROM Account', queryLocator.getQuery());
}

@IsTest
static void toQueryLocatorWithSharing() {
// Setup
insertTasks();

System.runAs(minimumAccessUser()) {
// Test
Database.QueryLocator queryLocator = SOQL.of(Task.SObjectType).systemMode().withSharing().toQueryLocator();

// Verify
Assert.isFalse(queryLocator.iterator().hasNext());
}
}

@IsTest
static void toQueryLocatorWithoutSharing() {
// Setup
insertTasks();

System.runAs(minimumAccessUser()) {
// Test
Database.QueryLocator queryLocator = SOQL.of(Task.SObjectType).systemMode().withoutSharing().toQueryLocator();

// Verify
Assert.isTrue(queryLocator.iterator().hasNext());
}
}

@IsTest
static void toCursor() {
// Setup
insertAccounts();

// Test
Database.Cursor queryCursor = SOQL.of(Account.SObjectType)
.with(Account.Id)
.with(Account.Name)
.toCursor();

// Verify
Assert.areEqual(0, queryCursor.getNumRecords());
Assert.areEqual(2, queryCursor.getNumRecords());
}

@IsTest
static void toCursorWithSharing() {
// Setup
insertTasks();

System.runAs(minimumAccessUser()) {
insert new Task(Subject = 'Test', Type = 'Other');

// Test
Database.Cursor queryCursor = SOQL.of(Task.SObjectType).systemMode().withSharing().toCursor();

// Verify
Assert.areEqual(1, queryCursor.getNumRecords());
}
}

@IsTest
static void toCursorWithoutSharing() {
// Setup
insertTasks();

System.runAs(minimumAccessUser()) {
// Test
Database.Cursor queryCursor = SOQL.of(Task.SObjectType).systemMode().withoutSharing().toCursor();

// Verify
Assert.areEqual(2, queryCursor.getNumRecords());
}
}

static List<Account> insertAccounts() {
Expand Down

0 comments on commit a2770ea

Please sign in to comment.