From b527ee280a326b45be0e6d792dbd5316593eb202 Mon Sep 17 00:00:00 2001 From: Timothy Gillespie Date: Sun, 21 Mar 2021 10:42:12 +0100 Subject: [PATCH 01/20] Add ModelSetup to unify setup codes --- .../orm/test/shared/setup/ModelSetup.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/test/java/org/javawebstack/orm/test/shared/setup/ModelSetup.java diff --git a/src/test/java/org/javawebstack/orm/test/shared/setup/ModelSetup.java b/src/test/java/org/javawebstack/orm/test/shared/setup/ModelSetup.java new file mode 100644 index 0000000..563fbc4 --- /dev/null +++ b/src/test/java/org/javawebstack/orm/test/shared/setup/ModelSetup.java @@ -0,0 +1,22 @@ +package org.javawebstack.orm.test.shared.setup; + +import org.javawebstack.orm.Model; +import org.javawebstack.orm.ORM; +import org.javawebstack.orm.Repo; +import org.javawebstack.orm.exception.ORMConfigurationException; +import org.javawebstack.orm.test.shared.settings.MySQLConnectionContainer; + +public class ModelSetup extends MySQLConnectionContainer { + + public static Repo setUpModel(Class clazz) { + + // Converting to Runtime exception to avoid having to declare the thrown error which has no utility + try { + ORM.register(clazz, sql()); + } catch (ORMConfigurationException e) { + throw new RuntimeException(e); + } + + return Repo.get(clazz); + } +} From 49e4fa04b359b74ca8908478bf1d21d3048a4175 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 04:54:32 +0200 Subject: [PATCH 02/20] Add assertOrderByContains method --- .../test/shared/verification/QueryVerification.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index 0c2f5ec..2fb3c16 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -22,7 +22,6 @@ public class QueryVerification { public QueryVerification(Query query) { this.query = query; - } /** @@ -36,6 +35,16 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr this.assertSectionContains(topLevelKeyword, containedSubstring, 0); } + /** + * Asserts that in ORDER BY section the given string is contained. + * This method uses the String.contains method internally and is therefore case sensitive. + * + * @param containedSubstring The substring which should be contained in ORDER BY section. + */ + public void assertOrderByContains(String containedSubstring) { + this.assertSectionContains("ORDER BY", containedSubstring); + } + /** * Asserts that in the i-th occurring section of a given top level keyword, the given string is contained. * This method uses the String.contains method internally and is therefore case sensitive. From 8aaedb258cdda939209f55aa3b386914df541330 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 06:11:58 +0200 Subject: [PATCH 03/20] Add SectionIndexOutOfBoundException --- .../SectionIndexOutOfBoundException.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/java/org/javawebstack/orm/test/exception/SectionIndexOutOfBoundException.java diff --git a/src/test/java/org/javawebstack/orm/test/exception/SectionIndexOutOfBoundException.java b/src/test/java/org/javawebstack/orm/test/exception/SectionIndexOutOfBoundException.java new file mode 100644 index 0000000..1a40ee7 --- /dev/null +++ b/src/test/java/org/javawebstack/orm/test/exception/SectionIndexOutOfBoundException.java @@ -0,0 +1,17 @@ +package org.javawebstack.orm.test.exception; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +/** + * Only to be used for tests. + * This exception should be thrown when a SQL Query String is manually parsed and sections and section types are defined, and + * a type of section is attempted to be retrieved which does not exist in this number. + */ +public class SectionIndexOutOfBoundException extends Exception { + private int sectionCount; + private int attemptedIndex; + private String topLevelKeyword; +} From 1ae8ac89ea87dcb3cbb58eaa115f5cd810339c1b Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 06:18:26 +0200 Subject: [PATCH 04/20] Add retrieval methods for the sections --- .../verification/QueryVerification.java | 64 +++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index 2fb3c16..af31a26 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -1,9 +1,11 @@ package org.javawebstack.orm.test.shared.verification; import org.javawebstack.orm.query.Query; +import org.javawebstack.orm.test.exception.SectionIndexOutOfBoundException; import org.javawebstack.orm.test.shared.util.QueryStringUtil; import java.util.HashSet; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -51,24 +53,19 @@ public void assertOrderByContains(String containedSubstring) { * * @param topLevelKeyword The top level keyword that prefaces the section. * @param containedSubstring The substring which should be contained in the first section of the given type. - * @param sectionIndex The index of the section to be checked, thusly 0 refers to the first occurrence etc. + * @param sectionIndex The index of the section to be checked, thus 0 refers to the first occurrence etc. */ public void assertSectionContains(String topLevelKeyword, String containedSubstring, int sectionIndex) { - String sectionString; - + String sectionString = null; try { - sectionString = new QueryStringUtil(this.query.getQueryString().getQuery()) - .getTopLevelSectionsByKeyword(topLevelKeyword) - .get(sectionIndex); - } catch (IndexOutOfBoundsException ignored) { + sectionString = getSection(topLevelKeyword, sectionIndex); + } catch (SectionIndexOutOfBoundException e) { fail(String.format( "A top level section of type %s and index %d was tested but only %d sections of that type existed.", - sectionIndex, - topLevelKeyword, - sectionIndex + 1 + e.getTopLevelKeyword(), + e.getAttemptedIndex(), + e.getSectionCount() )); - - return; } assertTrue( @@ -77,4 +74,47 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr ); } + /** + * Retrieves the inner part of a section by its keyword. With multiple occurrences it will only retrieve the first + * one. It does not include the keyword and one whitespaces at start and end. + * + * @param topLevelKeyword The top level keyword that prefaces the section. + * @return The inner part of the first section as specified. + * @throws SectionIndexOutOfBoundException if no section by that top level keyword exists. + */ + public String getSection(String topLevelKeyword) throws SectionIndexOutOfBoundException { + return this.getSection(topLevelKeyword, 0); + } + + /** + * Retrieves the inner part of a section by its keyword and index specifying which occurrence should be retrieved. + * It does not include the keyword and one whitespaces at start and end. + * + * @param topLevelKeyword The top level keyword that prefaces the section. + * @param sectionIndex The index of the section to be retrieved, thus 0 refers to the first occurrence etc. + * @return The inner part of the first section as specified. + * @throws SectionIndexOutOfBoundException if there are less than sectionIndex + 1 elements + */ + public String getSection(String topLevelKeyword, int sectionIndex) throws SectionIndexOutOfBoundException { + List sectionList = this.getSectionList(topLevelKeyword); + try { + return sectionList.get(sectionIndex); + } catch (IndexOutOfBoundsException converted) { + + SectionIndexOutOfBoundException exception = new SectionIndexOutOfBoundException(); + + exception.setSectionCount(sectionList.size()); + exception.setAttemptedIndex(sectionIndex); + exception.setTopLevelKeyword(topLevelKeyword); + + throw exception; + } + } + + + public List getSectionList(String topLevelKeyword) { + return new QueryStringUtil(this.query.getQueryString().getQuery()) + .getTopLevelSectionsByKeyword(topLevelKeyword); + } + } From 5d4e23331053a01974752c670be5b4e3f4604860 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 06:21:04 +0200 Subject: [PATCH 05/20] Add JavaDocs for getSectionList too --- .../orm/test/shared/verification/QueryVerification.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index af31a26..2fc4a6a 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -111,7 +111,13 @@ public String getSection(String topLevelKeyword, int sectionIndex) throws Sectio } } - + /** + * Retrieve list of all sections prefaced with the specified top level keyword. The list will have the same order + * as the occurrences of each section. + * + * @param topLevelKeyword The top level keyword that prefaces the sections. + * @return The order sensitive list of inner sections. + */ public List getSectionList(String topLevelKeyword) { return new QueryStringUtil(this.query.getQueryString().getQuery()) .getTopLevelSectionsByKeyword(topLevelKeyword); From 89c48dc82adbacb279da9d6372bf6355c24e83bb Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 06:21:04 +0200 Subject: [PATCH 06/20] Add JavaDocs for getSectionList too --- .../orm/test/shared/verification/QueryVerification.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index af31a26..27a4bda 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -111,7 +111,13 @@ public String getSection(String topLevelKeyword, int sectionIndex) throws Sectio } } - + /** + * Retrieve list of all sections prefaced with the specified top level keyword. The list will have the same order + * as the occurrences of each section. + * + * @param topLevelKeyword The top level keyword that prefaces the sections. + * @return The order sensitive string list of inner sections. + */ public List getSectionList(String topLevelKeyword) { return new QueryStringUtil(this.query.getQueryString().getQuery()) .getTopLevelSectionsByKeyword(topLevelKeyword); From 51907a4707def735c80528faa8fb8c1462092af8 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 06:25:44 +0200 Subject: [PATCH 07/20] Reorder methods more logically --- .../verification/QueryVerification.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index 27a4bda..b712c0d 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -37,16 +37,6 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr this.assertSectionContains(topLevelKeyword, containedSubstring, 0); } - /** - * Asserts that in ORDER BY section the given string is contained. - * This method uses the String.contains method internally and is therefore case sensitive. - * - * @param containedSubstring The substring which should be contained in ORDER BY section. - */ - public void assertOrderByContains(String containedSubstring) { - this.assertSectionContains("ORDER BY", containedSubstring); - } - /** * Asserts that in the i-th occurring section of a given top level keyword, the given string is contained. * This method uses the String.contains method internally and is therefore case sensitive. @@ -61,19 +51,29 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr sectionString = getSection(topLevelKeyword, sectionIndex); } catch (SectionIndexOutOfBoundException e) { fail(String.format( - "A top level section of type %s and index %d was tested but only %d sections of that type existed.", - e.getTopLevelKeyword(), - e.getAttemptedIndex(), - e.getSectionCount() + "A top level section of type %s and index %d was tested but only %d sections of that type existed.", + e.getTopLevelKeyword(), + e.getAttemptedIndex(), + e.getSectionCount() )); } assertTrue( - sectionString.contains(containedSubstring), - String.format("The occurrence of index %d of %s section of the query did not contain a substring %s but looked like this: %s. Note that the match is case-sensitive.", sectionIndex, topLevelKeyword, containedSubstring, sectionString) + sectionString.contains(containedSubstring), + String.format("The occurrence of index %d of %s section of the query did not contain a substring %s but looked like this: %s. Note that the match is case-sensitive.", sectionIndex, topLevelKeyword, containedSubstring, sectionString) ); } + /** + * Asserts that in ORDER BY section the given string is contained. + * This method uses the String.contains method internally and is therefore case sensitive. + * + * @param containedSubstring The substring which should be contained in ORDER BY section. + */ + public void assertOrderByContains(String containedSubstring) { + this.assertSectionContains("ORDER BY", containedSubstring); + } + /** * Retrieves the inner part of a section by its keyword. With multiple occurrences it will only retrieve the first * one. It does not include the keyword and one whitespaces at start and end. From ac7140ef85e6376b5e37fa7ba45f1ddd8f7871d8 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 06:33:49 +0200 Subject: [PATCH 08/20] Add assertSectionEquals method --- .../verification/QueryVerification.java | 49 ++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index b712c0d..d0b8de4 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -50,12 +50,7 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr try { sectionString = getSection(topLevelKeyword, sectionIndex); } catch (SectionIndexOutOfBoundException e) { - fail(String.format( - "A top level section of type %s and index %d was tested but only %d sections of that type existed.", - e.getTopLevelKeyword(), - e.getAttemptedIndex(), - e.getSectionCount() - )); + this.failDueToSectionIndexOutOfBounds(e); } assertTrue( @@ -74,6 +69,39 @@ public void assertOrderByContains(String containedSubstring) { this.assertSectionContains("ORDER BY", containedSubstring); } + /** + * Asserts that the first occurring section of a given top level keyword is equal to the given string. + * This method uses the String.equals method internally and is therefore case sensitive. + * + * @param topLevelKeyword The top level keyword that prefaces the section. + * @param expectedString The substring which the first section of the given type should be equal to. + */ + public void assertSectionEquals(String topLevelKeyword, String expectedString) { + this.assertSectionEquals(topLevelKeyword, expectedString, 0); + } + + /** + * Asserts that the i-th occurring section of a given top level keyword equal to the given string. + * This method uses the String.equals method internally and is therefore case sensitive. + * + * @param topLevelKeyword The top level keyword that prefaces the section. + * @param expectedString The substring which the specified section of the given type should be equal to. + * @param sectionIndex The index of the section to be checked, thus 0 refers to the first occurrence etc. + */ + public void assertSectionEquals(String topLevelKeyword, String expectedString, int sectionIndex) { + String sectionString = null; + try { + sectionString = getSection(topLevelKeyword, sectionIndex); + } catch (SectionIndexOutOfBoundException e) { + this.failDueToSectionIndexOutOfBounds(e); + } + + assertTrue( + sectionString.equals(expectedString), + String.format("The occurrence of index %d of %s section of the query was not equal to the string %s but looked like this: %s. Note that the match is case-sensitive.", sectionIndex, topLevelKeyword, expectedString, sectionString) + ); + } + /** * Retrieves the inner part of a section by its keyword. With multiple occurrences it will only retrieve the first * one. It does not include the keyword and one whitespaces at start and end. @@ -123,4 +151,13 @@ public List getSectionList(String topLevelKeyword) { .getTopLevelSectionsByKeyword(topLevelKeyword); } + private void failDueToSectionIndexOutOfBounds(SectionIndexOutOfBoundException exception) { + fail(String.format( + "A top level section of type %s and index %d was tested but only %d sections of that type existed.", + exception.getTopLevelKeyword(), + exception.getAttemptedIndex(), + exception.getSectionCount() + )); + } + } From 5491a48a5755413f155d7c8b92ecd1ed6d710909 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 06:39:52 +0200 Subject: [PATCH 09/20] Remove OrderBy specific contains method --- .../test/shared/verification/QueryVerification.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index d0b8de4..8846069 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -59,16 +59,6 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr ); } - /** - * Asserts that in ORDER BY section the given string is contained. - * This method uses the String.contains method internally and is therefore case sensitive. - * - * @param containedSubstring The substring which should be contained in ORDER BY section. - */ - public void assertOrderByContains(String containedSubstring) { - this.assertSectionContains("ORDER BY", containedSubstring); - } - /** * Asserts that the first occurring section of a given top level keyword is equal to the given string. * This method uses the String.equals method internally and is therefore case sensitive. From 6f97433348aac85564ac8ca1fad4eef4b274a2d1 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 07:06:46 +0200 Subject: [PATCH 10/20] Make assertion methods chainable --- .../shared/verification/QueryVerification.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index 8846069..e161491 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -33,8 +33,9 @@ public QueryVerification(Query query) { * @param topLevelKeyword The top level keyword that prefaces the section. * @param containedSubstring The substring which should be contained in the first section of the given type. */ - public void assertSectionContains(String topLevelKeyword, String containedSubstring) { + public QueryVerification assertSectionContains(String topLevelKeyword, String containedSubstring) { this.assertSectionContains(topLevelKeyword, containedSubstring, 0); + return this; } /** @@ -45,18 +46,21 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr * @param containedSubstring The substring which should be contained in the first section of the given type. * @param sectionIndex The index of the section to be checked, thus 0 refers to the first occurrence etc. */ - public void assertSectionContains(String topLevelKeyword, String containedSubstring, int sectionIndex) { + public QueryVerification assertSectionContains(String topLevelKeyword, String containedSubstring, int sectionIndex) { String sectionString = null; try { sectionString = getSection(topLevelKeyword, sectionIndex); } catch (SectionIndexOutOfBoundException e) { this.failDueToSectionIndexOutOfBounds(e); + return this; } assertTrue( sectionString.contains(containedSubstring), String.format("The occurrence of index %d of %s section of the query did not contain a substring %s but looked like this: %s. Note that the match is case-sensitive.", sectionIndex, topLevelKeyword, containedSubstring, sectionString) ); + + return this; } /** @@ -66,8 +70,9 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr * @param topLevelKeyword The top level keyword that prefaces the section. * @param expectedString The substring which the first section of the given type should be equal to. */ - public void assertSectionEquals(String topLevelKeyword, String expectedString) { + public QueryVerification assertSectionEquals(String topLevelKeyword, String expectedString) { this.assertSectionEquals(topLevelKeyword, expectedString, 0); + return this; } /** @@ -78,18 +83,21 @@ public void assertSectionEquals(String topLevelKeyword, String expectedString) { * @param expectedString The substring which the specified section of the given type should be equal to. * @param sectionIndex The index of the section to be checked, thus 0 refers to the first occurrence etc. */ - public void assertSectionEquals(String topLevelKeyword, String expectedString, int sectionIndex) { + public QueryVerification assertSectionEquals(String topLevelKeyword, String expectedString, int sectionIndex) { String sectionString = null; try { sectionString = getSection(topLevelKeyword, sectionIndex); } catch (SectionIndexOutOfBoundException e) { this.failDueToSectionIndexOutOfBounds(e); + return this; } assertTrue( sectionString.equals(expectedString), String.format("The occurrence of index %d of %s section of the query was not equal to the string %s but looked like this: %s. Note that the match is case-sensitive.", sectionIndex, topLevelKeyword, expectedString, sectionString) ); + + return this; } /** From b73d7dbf80fde8fb593ccdfc330cabe0714fe34a Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 07:12:50 +0200 Subject: [PATCH 11/20] Add better fitting internal assert for the equal assertion --- .../orm/test/shared/verification/QueryVerification.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java index e161491..d9b6d05 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java +++ b/src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java @@ -7,8 +7,7 @@ import java.util.HashSet; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.*; /** * The QueryVerification class wraps an JWS Query Object and provides assertion methods regarding the raw query string. @@ -92,8 +91,9 @@ public QueryVerification assertSectionEquals(String topLevelKeyword, String expe return this; } - assertTrue( - sectionString.equals(expectedString), + assertEquals( + expectedString, + sectionString, String.format("The occurrence of index %d of %s section of the query was not equal to the string %s but looked like this: %s. Note that the match is case-sensitive.", sectionIndex, topLevelKeyword, expectedString, sectionString) ); From 710b6c5bb9602b6d0ad95e287be0e2edc39de62d Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Wed, 31 Mar 2021 07:14:34 +0200 Subject: [PATCH 12/20] Add tests for OrderBy Clauses --- .../test/querybuilding/OrderByClauseTest.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java diff --git a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java new file mode 100644 index 0000000..f2108e9 --- /dev/null +++ b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java @@ -0,0 +1,98 @@ +package org.javawebstack.orm.test.querybuilding; + +import org.javawebstack.orm.query.Query; +import org.javawebstack.orm.test.shared.models.Datatype; +import org.javawebstack.orm.test.shared.verification.QueryVerification; +import org.junit.jupiter.api.Test; + +import static org.javawebstack.orm.test.shared.setup.ModelSetup.setUpModel; + +public class OrderByClauseTest { + + @Test + void testOneExistingColumnDefaultOrderBy() { + Query query = setUpModel(Datatype.class).query() + .order("wrapper_integer"); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`wrapper_integer` ASC"); + } + + @Test + void testOneNonExistingColumnDefaultOrderBy() { + Query query = setUpModel(Datatype.class).query() + .order("does_not_exist"); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`does_not_exist` ASC"); + } + + @Test + void testOneExistingColumnASCOrderBy() { + Query query = setUpModel(Datatype.class).query() + .order("wrapper_integer", false); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`wrapper_integer` ASC"); + } + + @Test + void testOneNonExistingColumnASCOrderBy() { + Query query = setUpModel(Datatype.class).query() + .order("does_not_exist", false); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`does_not_exist` ASC"); + } + + @Test + void testOneExistingColumnDESCOrderBy() { + Query query = setUpModel(Datatype.class).query() + .order("wrapper_integer", true); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`wrapper_integer` DESC"); + } + + @Test + void testOneNonExistingColumnDESCOrderBy() { + Query query = setUpModel(Datatype.class).query() + .order("does_not_exist", true); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`does_not_exist` DESC"); + } + + @Test + void testMultipleOrderByClausesOfASCOrder() { + Query query = setUpModel(Datatype.class).query() + .order("wrapper_integer") + .order("primitive_integer"); + + new QueryVerification(query) + .assertSectionContains("ORDER BY", "`wrapper_integer` ASC") + .assertSectionContains("ORDER BY", "`primitive_integer` ASC"); + } + + @Test + void testMultipleOrderByClausesOfDESCOrder() { + Query query = setUpModel(Datatype.class).query() + .order("wrapper_integer") + .order("primitive_integer"); + + new QueryVerification(query) + .assertSectionContains("ORDER BY", "`wrapper_integer` DESC") + .assertSectionContains("ORDER BY", "`primitive_integer` DESC"); + } + + @Test + void testMultipleOrderByClausesOfMixedOrder() { + Query query = setUpModel(Datatype.class).query() + .order("wrapper_integer", false) + .order("primitive_integer", true); + + new QueryVerification(query) + .assertSectionContains("ORDER BY", "`wrapper_integer` ASC") + .assertSectionContains("ORDER BY", "`primitive_integer` DESC"); + } + + @Test + void testMultipleOrderByClausesOfMixedOrderReversed() { + Query query = setUpModel(Datatype.class).query() + .order("primitive_integer", true) + .order("wrapper_integer", false); + + new QueryVerification(query) + .assertSectionContains("ORDER BY", "`primitive_integer` DESC") + .assertSectionContains("ORDER BY", "`wrapper_integer` ASC"); + } + +} From e4d3fc03c28e62a5e853d44130eb7fb3c4990119 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Thu, 1 Apr 2021 21:26:18 +0200 Subject: [PATCH 13/20] Remove explicit ASC as discussed --- .../test/querybuilding/OrderByClauseTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java index f2108e9..c71516f 100644 --- a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java +++ b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java @@ -13,28 +13,28 @@ public class OrderByClauseTest { void testOneExistingColumnDefaultOrderBy() { Query query = setUpModel(Datatype.class).query() .order("wrapper_integer"); - new QueryVerification(query).assertSectionEquals("ORDER BY", "`wrapper_integer` ASC"); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`wrapper_integer`"); } @Test void testOneNonExistingColumnDefaultOrderBy() { Query query = setUpModel(Datatype.class).query() .order("does_not_exist"); - new QueryVerification(query).assertSectionEquals("ORDER BY", "`does_not_exist` ASC"); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`does_not_exist`"); } @Test void testOneExistingColumnASCOrderBy() { Query query = setUpModel(Datatype.class).query() .order("wrapper_integer", false); - new QueryVerification(query).assertSectionEquals("ORDER BY", "`wrapper_integer` ASC"); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`wrapper_integer`"); } @Test void testOneNonExistingColumnASCOrderBy() { Query query = setUpModel(Datatype.class).query() .order("does_not_exist", false); - new QueryVerification(query).assertSectionEquals("ORDER BY", "`does_not_exist` ASC"); + new QueryVerification(query).assertSectionEquals("ORDER BY", "`does_not_exist`"); } @Test @@ -58,8 +58,8 @@ void testMultipleOrderByClausesOfASCOrder() { .order("primitive_integer"); new QueryVerification(query) - .assertSectionContains("ORDER BY", "`wrapper_integer` ASC") - .assertSectionContains("ORDER BY", "`primitive_integer` ASC"); + .assertSectionContains("ORDER BY", "`wrapper_integer`") + .assertSectionContains("ORDER BY", "`primitive_integer`"); } @Test @@ -80,7 +80,7 @@ void testMultipleOrderByClausesOfMixedOrder() { .order("primitive_integer", true); new QueryVerification(query) - .assertSectionContains("ORDER BY", "`wrapper_integer` ASC") + .assertSectionContains("ORDER BY", "`wrapper_integer`") .assertSectionContains("ORDER BY", "`primitive_integer` DESC"); } @@ -92,7 +92,7 @@ void testMultipleOrderByClausesOfMixedOrderReversed() { new QueryVerification(query) .assertSectionContains("ORDER BY", "`primitive_integer` DESC") - .assertSectionContains("ORDER BY", "`wrapper_integer` ASC"); + .assertSectionContains("ORDER BY", "`wrapper_integer`"); } } From fb00e4d46402a6524898a6c547e8eaa6aa488deb Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Fri, 2 Apr 2021 16:13:50 +0200 Subject: [PATCH 14/20] Adjust MySQL Query Building for the new OrderBy and fix OrderBy test --- .../javawebstack/orm/query/QueryOrderBy.java | 15 ++++++++++++++- .../orm/query/QueryOrderByElement.java | 19 +++++++++++++++++-- .../builder/MySQLQueryStringBuilder.java | 10 ++++++---- .../test/querybuilding/OrderByClauseTest.java | 4 ++-- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java b/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java index d35529f..fc05582 100644 --- a/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java +++ b/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java @@ -1,7 +1,9 @@ package org.javawebstack.orm.query; +import org.javawebstack.orm.TableInfo; + import java.util.LinkedList; -import java.util.List; +import java.util.stream.Collectors; public class QueryOrderBy extends LinkedList{ @@ -25,4 +27,15 @@ public boolean add(QueryOrderByElement element) { private boolean willOverwrite(QueryOrderByElement element) { return this.stream().anyMatch(element::hasEqualColumn); } + + @Override + public String toString() { + return toString(null); + } + + public String toString(TableInfo info) { + return this.stream() + .map(QueryOrderByElement::toString) + .collect(Collectors.joining(",")); + } } diff --git a/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java b/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java index 23bf426..e90897b 100644 --- a/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java +++ b/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java @@ -1,10 +1,12 @@ package org.javawebstack.orm.query; +import org.javawebstack.orm.TableInfo; + import java.util.Objects; public class QueryOrderByElement { - private QueryColumn queryColumn; - private boolean desc; + private final QueryColumn queryColumn; + private final boolean desc; QueryOrderByElement(String columnName, boolean desc) { queryColumn = new QueryColumn(columnName); @@ -50,4 +52,17 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(getQueryColumn(), isDesc()); } + + @Override + public String toString() { + return this.toString(null); + } + + public String toString(TableInfo info) { + String stringifiedOrderBy = getQueryColumn().toString(info); + if (isDesc()) + stringifiedOrderBy += " DESC"; + + return stringifiedOrderBy; + } } diff --git a/src/main/java/org/javawebstack/orm/wrapper/builder/MySQLQueryStringBuilder.java b/src/main/java/org/javawebstack/orm/wrapper/builder/MySQLQueryStringBuilder.java index bfc1771..3b1faef 100644 --- a/src/main/java/org/javawebstack/orm/wrapper/builder/MySQLQueryStringBuilder.java +++ b/src/main/java/org/javawebstack/orm/wrapper/builder/MySQLQueryStringBuilder.java @@ -50,11 +50,13 @@ public SQLQueryString buildQuery(Query query, boolean count) { sb.append(" WHERE ").append(qs.getQuery()); parameters.addAll(qs.getParameters()); } - if (query.getOrder() != null) { - sb.append(" ORDER BY ").append(query.getOrder().toString(repo.getInfo())); - if (query.isDescOrder()) - sb.append(" DESC"); + + QueryOrderBy orderBy = query.getOrder(); + if (!orderBy.isEmpty()) { + sb.append(" ORDER BY ") + .append(orderBy.toString()); } + Integer offset = query.getOffset(); Integer limit = query.getLimit(); if (offset != null && limit == null) diff --git a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java index c71516f..1d16432 100644 --- a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java +++ b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java @@ -65,8 +65,8 @@ void testMultipleOrderByClausesOfASCOrder() { @Test void testMultipleOrderByClausesOfDESCOrder() { Query query = setUpModel(Datatype.class).query() - .order("wrapper_integer") - .order("primitive_integer"); + .order("wrapper_integer", true) + .order("primitive_integer", true); new QueryVerification(query) .assertSectionContains("ORDER BY", "`wrapper_integer` DESC") From 018ce02873a5078ed6a792e9c15ac58c3afce217 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Fri, 2 Apr 2021 16:56:02 +0200 Subject: [PATCH 15/20] Add test to assert the correct sort priority --- .../test/querybuilding/OrderByClauseTest.java | 46 ++++++++++++++++++- .../orm/test/shared/models/Datatype.java | 28 +++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java index 1d16432..bb3a9d4 100644 --- a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java +++ b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java @@ -1,11 +1,19 @@ package org.javawebstack.orm.test.querybuilding; import org.javawebstack.orm.query.Query; +import org.javawebstack.orm.test.exception.SectionIndexOutOfBoundException; import org.javawebstack.orm.test.shared.models.Datatype; import org.javawebstack.orm.test.shared.verification.QueryVerification; import org.junit.jupiter.api.Test; +import javax.xml.crypto.Data; + +import java.util.*; +import java.util.stream.Collectors; + import static org.javawebstack.orm.test.shared.setup.ModelSetup.setUpModel; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class OrderByClauseTest { @@ -84,6 +92,43 @@ void testMultipleOrderByClausesOfMixedOrder() { .assertSectionContains("ORDER BY", "`primitive_integer` DESC"); } + + @Test + // This test is important because putting the order by statements in different order is relevant (they set priorities) + void testMultipleOrderByClausesOfRandomOrderForCorrectOrder() throws SectionIndexOutOfBoundException { + Query query = setUpModel(Datatype.class).query(); + ArrayList columnNames = new ArrayList<>(Datatype.columnNames); + + LinkedList callOrder = new LinkedList<>(); + + Random r = new Random(); + columnNames.stream().unordered().forEach((singleColumn) -> { + query.order(singleColumn, r.nextBoolean()); + callOrder.add(singleColumn); + }); + + String queryString = new QueryVerification(query).getSection("ORDER BY"); + int lastIndex = 0; + int foundIndex = -1; + for (String nextInCallOrder : callOrder) { + foundIndex = queryString.indexOf("`" + nextInCallOrder + "`"); + if(foundIndex < lastIndex) { + if (foundIndex == -1) + fail("Not all columns occurred in the query string."); + else + fail("The columns did not appear an the correct order."); + + break; + } + + lastIndex = foundIndex; + } + + // If it came until here the test should count as passed. + assertTrue(true); + + } + @Test void testMultipleOrderByClausesOfMixedOrderReversed() { Query query = setUpModel(Datatype.class).query() @@ -94,5 +139,4 @@ void testMultipleOrderByClausesOfMixedOrderReversed() { .assertSectionContains("ORDER BY", "`primitive_integer` DESC") .assertSectionContains("ORDER BY", "`wrapper_integer`"); } - } diff --git a/src/test/java/org/javawebstack/orm/test/shared/models/Datatype.java b/src/test/java/org/javawebstack/orm/test/shared/models/Datatype.java index 7b4aa9d..5d79a8d 100644 --- a/src/test/java/org/javawebstack/orm/test/shared/models/Datatype.java +++ b/src/test/java/org/javawebstack/orm/test/shared/models/Datatype.java @@ -5,6 +5,8 @@ import java.sql.Date; import java.sql.Timestamp; +import java.util.Arrays; +import java.util.List; import java.util.UUID; /* @@ -84,4 +86,30 @@ public enum OptionEnum { OPTION1, OPTION2, } + + public static final List columnNames = Arrays.asList( + "id", + "primitive_boolean", + "wrapper_boolean", + "primitive_byte", + "wrapper_byte", + "primitive_short", + "wrapper_short", + "primitive_integer", + "wrapper_integer", + "primitive_long", + "wrapper_long", + "primitive_float", + "wrapper_float", + "primitive_double", + "wrapper_double", + "primitive_char", + "wrapper_string", + "char_array", + "byte_array", + "timestamp", + "date", + "uuid", + "option_enum" + ); } \ No newline at end of file From c8e9a408d51e3c63b277b3280dfb6ec7f1cf9ad6 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Fri, 2 Apr 2021 16:59:19 +0200 Subject: [PATCH 16/20] Add error case for calling order on same column twice --- .../test/querybuilding/OrderByClauseTest.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java index bb3a9d4..76d5b18 100644 --- a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java +++ b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java @@ -1,5 +1,6 @@ package org.javawebstack.orm.test.querybuilding; +import org.javawebstack.orm.exception.ORMQueryException; import org.javawebstack.orm.query.Query; import org.javawebstack.orm.test.exception.SectionIndexOutOfBoundException; import org.javawebstack.orm.test.shared.models.Datatype; @@ -12,8 +13,7 @@ import java.util.stream.Collectors; import static org.javawebstack.orm.test.shared.setup.ModelSetup.setUpModel; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.*; public class OrderByClauseTest { @@ -92,6 +92,17 @@ void testMultipleOrderByClausesOfMixedOrder() { .assertSectionContains("ORDER BY", "`primitive_integer` DESC"); } + @Test + void testMultipleOrderByClausesOfMixedOrderReversed() { + Query query = setUpModel(Datatype.class).query() + .order("primitive_integer", true) + .order("wrapper_integer", false); + + new QueryVerification(query) + .assertSectionContains("ORDER BY", "`primitive_integer` DESC") + .assertSectionContains("ORDER BY", "`wrapper_integer`"); + } + @Test // This test is important because putting the order by statements in different order is relevant (they set priorities) @@ -129,14 +140,14 @@ void testMultipleOrderByClausesOfRandomOrderForCorrectOrder() throws SectionInde } + /* + * Error Cases + */ @Test - void testMultipleOrderByClausesOfMixedOrderReversed() { + void testCannotCallOrderOnSameColumnTwice() { Query query = setUpModel(Datatype.class).query() - .order("primitive_integer", true) - .order("wrapper_integer", false); + .order("primitive_integer", true); - new QueryVerification(query) - .assertSectionContains("ORDER BY", "`primitive_integer` DESC") - .assertSectionContains("ORDER BY", "`wrapper_integer`"); + assertThrows(ORMQueryException.class, () -> query.order("primitive_integer")); } } From 37ed3120ce176f5a6f43652ff7530ed407d7a803 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Fri, 2 Apr 2021 17:04:55 +0200 Subject: [PATCH 17/20] Add note of purpose of OrderByClauseTest --- .../javawebstack/orm/test/querybuilding/OrderByClauseTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java index 76d5b18..d411536 100644 --- a/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java +++ b/src/test/java/org/javawebstack/orm/test/querybuilding/OrderByClauseTest.java @@ -15,6 +15,7 @@ import static org.javawebstack.orm.test.shared.setup.ModelSetup.setUpModel; import static org.junit.jupiter.api.Assertions.*; +// This class tests the query generation for order by statements an MySQL public class OrderByClauseTest { @Test @@ -143,6 +144,8 @@ void testMultipleOrderByClausesOfRandomOrderForCorrectOrder() throws SectionInde /* * Error Cases */ + + // This test might not be correct here as it does not purely look at the query @Test void testCannotCallOrderOnSameColumnTwice() { Query query = setUpModel(Datatype.class).query() From 5a74b5380ce9b7d10cf772abd663e2dd8b305ab7 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Fri, 2 Apr 2021 17:15:25 +0200 Subject: [PATCH 18/20] Add JavaDocs to QueryOrderBy --- .../javawebstack/orm/query/QueryOrderBy.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java b/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java index fc05582..7cc6e24 100644 --- a/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java +++ b/src/main/java/org/javawebstack/orm/query/QueryOrderBy.java @@ -5,17 +5,44 @@ import java.util.LinkedList; import java.util.stream.Collectors; +/** + * The QueryOrderBy class serves as an aggregation of order by elements. It extends a list, because the order of the + * order by statements is of relevance. + */ public class QueryOrderBy extends LinkedList{ + /** + * Add a new order by statement. If a statement with the same column name already exists it will not add the + * statement. + * + * @param columnName The column name to order by. + * @param desc If the column should be order descendingly. + * @return True if adding the statement was successful. False otherwise. + */ public boolean add(String columnName, boolean desc) { return this.add(new QueryColumn(columnName), desc); } + /** + * Add a new order by statement. If a statement with the same column name already exists it will not add the + * statement. + * + * @param column The column to be ordered by. It will retrieve the name from the QueryColumn. + * @param desc If the column should be order descendingly. + * @return True if adding the statement was successful. False otherwise. + */ public boolean add(QueryColumn column, boolean desc) { return this.add(new QueryOrderByElement(column, desc)); } @Override + /** + * Add a new order by statement. If a statement with the same column name already exists it will not add the + * statement. + * + * @param element The direct QueryOrderByElement which encodes the order by statement. + * @return True if adding the statement was successful. False otherwise. + */ public boolean add(QueryOrderByElement element) { boolean hasBeenAdded = false; if(!willOverwrite(element)) @@ -28,6 +55,8 @@ private boolean willOverwrite(QueryOrderByElement element) { return this.stream().anyMatch(element::hasEqualColumn); } + + // The toString methods are specific to MySQL so they might have to be replaced later on. @Override public String toString() { return toString(null); From 5210447f3cd949e632abbc0e1c5f7a63d1b8eb9f Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Fri, 2 Apr 2021 17:21:09 +0200 Subject: [PATCH 19/20] Add JavaDocs to QueryOrderByElement --- .../orm/query/QueryOrderByElement.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java b/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java index e90897b..915284f 100644 --- a/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java +++ b/src/main/java/org/javawebstack/orm/query/QueryOrderByElement.java @@ -4,6 +4,9 @@ import java.util.Objects; +/** + * The QueryOrderByElement class encodes an Order By Statement. + */ public class QueryOrderByElement { private final QueryColumn queryColumn; private final boolean desc; @@ -18,14 +21,30 @@ public class QueryOrderByElement { this.desc = desc; } + /** + * Retrieves the QueryColumn of the statement which encodes the column name. + * + * @return The encoding QueryColumn object. + */ public QueryColumn getQueryColumn() { return queryColumn; } + /** + * Retrieves the information if this column is ordered ascendingly or descendingly. + * + * @return false if ascending, true if descending. + */ public boolean isDesc() { return desc; } + /** + * Compares the encoded column name. + * + * @param o An object to compare to. + * @return True if the object is a QueryOrderByElement with a QueryColumn with generates the same identifier. + */ public boolean hasEqualColumn(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; @@ -33,13 +52,6 @@ public boolean hasEqualColumn(Object o) { return getQueryColumn().equals(that.getQueryColumn()); } - public boolean hasEqualOrderDirection(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - QueryOrderByElement that = (QueryOrderByElement) o; - return isDesc() == that.isDesc(); - } - @Override public boolean equals(Object o) { if (this == o) return true; From 3bff425c49f79e32f3ce5e4584a86be15856b3b9 Mon Sep 17 00:00:00 2001 From: TimothyGillespie Date: Fri, 2 Apr 2021 17:28:34 +0200 Subject: [PATCH 20/20] Add JavaDocs to the order method on Query --- .../org/javawebstack/orm/query/Query.java | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/javawebstack/orm/query/Query.java b/src/main/java/org/javawebstack/orm/query/Query.java index fac766c..9bbd617 100644 --- a/src/main/java/org/javawebstack/orm/query/Query.java +++ b/src/main/java/org/javawebstack/orm/query/Query.java @@ -296,20 +296,43 @@ public Query search(String search) { return this; } - public Query order(String orderBy) { - return order(orderBy, false); - } - - public Query order(String orderBy, boolean desc) { - return order(new QueryColumn(orderBy), desc); - } - - public Query order(QueryColumn orderBy, boolean desc) { - boolean success = this.order.add(orderBy, desc); + /** + * Sorts the results by the given column name ascendingly. + * + * @param columnName The name of the column to sort ascendingly by. + * @return The Query object with the given order by information added. + * @throws ORMQueryException if the order operation is called twice on a column specification with the same name. + */ + public Query order(String columnName) throws ORMQueryException { + return order(columnName, false); + } + + /** + * Sorts the results by the given column name with the given order direction. + * + * @param columnName The name of the column to sort ascendingly by. + * @param desc If true it will order descendingly, if false it will order ascendingly. + * @return The Query object with the given order by information added. + * @throws ORMQueryException if the order operation is called twice on a column specification with the same name. + */ + public Query order(String columnName, boolean desc) throws ORMQueryException { + return order(new QueryColumn(columnName), desc); + } + + /** + * Sorts the results by the given column with the given order direction. + * + * @param column The column encoded as QueryColumn object. + * @param desc If true it will order descendingly, if false it will order ascendingly. + * @return The Query object with the given order by information added. + * @throws ORMQueryException if the order operation is called twice on a column specification with the same name. + */ + public Query order(QueryColumn column, boolean desc) throws ORMQueryException{ + boolean success = this.order.add(column, desc); if(!success) { throw new ORMQueryException(String.format( "The column %s could not be ordered %s. This is probably caused by calling .order() on this column twice.", - orderBy.toString(), + column.toString(), desc ? "descendingly" : "ascendingly" )); }