Skip to content

Commit

Permalink
Merge pull request #7 from jpmonette/refactor-build-methods
Browse files Browse the repository at this point in the history
Refactor query builder + rename toSOQL() to build()
  • Loading branch information
jpmonette committed Apr 6, 2017
2 parents 54e23a7 + 5fda582 commit 1d7baa5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
67 changes: 47 additions & 20 deletions src/classes/Q.cls
Original file line number Diff line number Diff line change
Expand Up @@ -80,54 +80,81 @@ public class Q {
}

/**
* Return SOQL Q
* @return String
* @access public
* Build the SELECT statement
*/
public String toSOQL() {
String query = 'SELECT Id ';

public String buildSelect() {
for (Q qb : this.subQueries) {
this.fieldList.add('(' + qb.toSOQL() + ')');
this.fieldList.add('(' + qb.build() + ')');
}

if (!this.fieldList.isEmpty()) {
query = 'SELECT ' + String.join(new List<String>(this.fieldList), ', ') + ' ';
return 'SELECT ' + String.join(new List<String>(this.fieldList), ', ');
} else {
return 'SELECT Id';
}
}

query += 'FROM ' + this.fromText;
/**
* Build the WHERE statement
*/
public String buildConditions() {
List<String> condList = new List<String>();

for (QCondition cond : this.conditions) {
condList.add(cond.build());
}

if (!this.conditions.isEmpty()) {
return 'WHERE ' + String.join(condList, ' AND ');
} else {
return null;
}
}

// Order
/**
* Build the ORDER BY statement
*/
public String buildOrderBy() {
List<String> orderList = new List<String>();

for (QOrder order : this.orders) {
orderList.add(order.build());
}

if (!this.orders.isEmpty()) {
query += ' ORDER BY ' + String.join(orderList, ', ');
return 'ORDER BY ' + String.join(orderList, ', ');
} else {
return '';
}

// Conditions
List<String> condList = new List<String>();
}

for (QCondition cond : this.conditions) {
condList.add(cond.build());
}
/**
* Build the SOQL query
*/
public String build() {
List<String> queryParts = new List<String>();

queryParts.add(this.buildSelect());
queryParts.add('FROM ' + this.fromText);

if (!this.conditions.isEmpty()) {
query += ' WHERE ' + String.join(condList, ' AND ');
queryParts.add(this.buildConditions());
}

if (!this.orders.isEmpty()) {
queryParts.add(this.buildOrderBy());
}

if (this.numberOfRows != null) {
query += ' LIMIT ' + this.numberOfRows;
queryParts.add('LIMIT ' + this.numberOfRows);
}

if (this.numberOfRowsToSkip != null) {
query += ' OFFSET ' + this.numberOfRowsToSkip;
queryParts.add('OFFSET ' + this.numberOfRowsToSkip);
}

return query;
return String.join(queryParts, ' ');
}

}
16 changes: 8 additions & 8 deletions src/classes/QTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private class QTest {
static void it_should_generate_query() {
String query =
new Q(Account.SObjectType)
.toSOQL();
.build();

System.assertEquals('SELECT Id FROM Account', query);
Database.query(query);
Expand All @@ -16,7 +16,7 @@ private class QTest {
String query =
new Q(Account.SObjectType)
.addSubquery(new Q('Contacts'))
.toSOQL();
.build();

System.assertEquals('SELECT (SELECT Id FROM Contacts) FROM Account', query);
Database.query(query);
Expand All @@ -27,7 +27,7 @@ private class QTest {
String query =
new Q(Account.SObjectType)
.selectFields(SObjectType.Account.fieldSets.Example)
.toSOQL();
.build();

System.assertEquals('SELECT CreatedById, Description, Owner.Email FROM Account', query);
Database.query(query);
Expand All @@ -39,7 +39,7 @@ private class QTest {
new Q(Account.SObjectType)
.add(Q.orderBy('Name').nullsLast())
.add(Q.orderBy('BillingCountry').descending())
.toSOQL();
.build();

System.assertEquals('SELECT Id FROM Account ORDER BY Name ASC NULLS LAST, BillingCountry DESC', query);
Database.query(query);
Expand All @@ -50,7 +50,7 @@ private class QTest {
String query =
new Q(Account.SObjectType)
.add(Q.condition('BillingCountry').isNotNull())
.toSOQL();
.build();

System.assertEquals('SELECT Id FROM Account WHERE BillingCountry != null', query);
Database.query(query);
Expand All @@ -61,7 +61,7 @@ private class QTest {
String query =
new Q(Account.SObjectType)
.add(Q.condition('Name').isLike('%Acme%'))
.toSOQL();
.build();

System.assertEquals('SELECT Id FROM Account WHERE Name LIKE \'%Acme%\'', query);
Database.query(query);
Expand All @@ -72,7 +72,7 @@ private class QTest {
String query =
new Q(Account.SObjectType)
.addLimit(5)
.toSOQL();
.build();

System.assertEquals('SELECT Id FROM Account LIMIT 5', query);
Database.query(query);
Expand All @@ -83,7 +83,7 @@ private class QTest {
String query =
new Q(Account.SObjectType)
.addOffset(5)
.toSOQL();
.build();

System.assertEquals('SELECT Id FROM Account OFFSET 5', query);
Database.query(query);
Expand Down

0 comments on commit 1d7baa5

Please sign in to comment.