-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from fauna/BT-4347-query-building
BT-4347 Query Building
- Loading branch information
Showing
8 changed files
with
334 additions
and
23 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
faunaJava/src/main/java/com/fauna/query/builder/Fragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.fauna.query.builder; | ||
|
||
/** | ||
* An abstract class serving as a base for different types of query fragments. | ||
*/ | ||
abstract class Fragment { | ||
|
||
/** | ||
* Retrieves the value represented by this fragment. | ||
* | ||
* @return the value of this fragment. | ||
*/ | ||
public abstract Object get(); | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
faunaJava/src/main/java/com/fauna/query/builder/LiteralFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.fauna.query.builder; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a literal fragment of a Fauna query. | ||
* This class encapsulates a fixed string that does not contain any variables. | ||
*/ | ||
class LiteralFragment extends Fragment { | ||
|
||
private final String value; | ||
|
||
/** | ||
* Constructs a new {@code LiteralFragment} with the given literal value. | ||
* | ||
* @param value the string value of this fragment; must not be null. | ||
* @throws IllegalArgumentException if {@code value} is null. | ||
*/ | ||
LiteralFragment(String value) { | ||
if (value == null) { | ||
throw new IllegalArgumentException("A literal value must not be null"); | ||
} | ||
this.value = value; | ||
} | ||
|
||
/** | ||
* Retrieves the string value of this fragment. | ||
* | ||
* @return the string value. | ||
*/ | ||
@Override | ||
public String get() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
LiteralFragment that = (LiteralFragment) o; | ||
|
||
return Objects.equals(value, that.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value != null ? value.hashCode() : 0; | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
faunaJava/src/main/java/com/fauna/query/builder/Query.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.fauna.query.builder; | ||
|
||
import com.fauna.query.template.FaunaTemplate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents a Fauna query that is constructed from fragments. | ||
* This class allows the building of queries from literal and variable parts. | ||
*/ | ||
public class Query { | ||
|
||
private final List<Fragment> fragments; | ||
|
||
/** | ||
* Constructs an empty Query instance. | ||
*/ | ||
public Query() { | ||
this.fragments = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Adds a fragment to the query. | ||
* | ||
* @param fragment the Fragment to add to the query. | ||
*/ | ||
void addFragment(Fragment fragment) { | ||
fragments.add(fragment); | ||
} | ||
|
||
/** | ||
* Creates a Query instance from a string template and arguments. | ||
* The template string can contain literals and variable placeholders. | ||
* | ||
* @param query the string template of the query. | ||
* @param args the arguments to replace the variable placeholders in the query. | ||
* @return a Query instance representing the complete query. | ||
* @throws IllegalArgumentException if a template variable does not have a corresponding entry in the provided args. | ||
*/ | ||
public static Query fql(String query, Map<String, Object> args) throws IllegalArgumentException { | ||
Query faunaQuery = new Query(); | ||
FaunaTemplate template = new FaunaTemplate(query); | ||
|
||
for (FaunaTemplate.TemplatePart part : template) { | ||
switch (part.getType()) { | ||
case LITERAL: { | ||
faunaQuery.addFragment(new LiteralFragment(part.getPart())); | ||
break; | ||
} | ||
case VARIABLE: { | ||
if (!args.containsKey(part.getPart())) { | ||
throw new IllegalArgumentException("Template variable `" + part.getPart() + "` not found in provided args"); | ||
} | ||
faunaQuery.addFragment(new ValueFragment(args.get(part.getPart()))); | ||
break; | ||
} | ||
} | ||
} | ||
return faunaQuery; | ||
} | ||
|
||
/** | ||
* Retrieves the list of fragments that make up this query. | ||
* | ||
* @return a list of Fragments. | ||
*/ | ||
List<Fragment> getFragments() { | ||
return fragments; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
faunaJava/src/main/java/com/fauna/query/builder/ValueFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.fauna.query.builder; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a value fragment of a Fauna query. | ||
* This class encapsulates a value that can be a variable in the query. | ||
*/ | ||
class ValueFragment extends Fragment { | ||
|
||
private final Object value; | ||
|
||
/** | ||
* Constructs a ValueFragment with the specified value. | ||
* | ||
* @param value the value to encapsulate, which can be any object. | ||
*/ | ||
public ValueFragment(Object value) { | ||
this.value = value; | ||
} | ||
|
||
/** | ||
* Retrieves the encapsulated value of this fragment. | ||
* | ||
* @return the encapsulated object. | ||
*/ | ||
@Override | ||
public Object get() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
ValueFragment that = (ValueFragment) o; | ||
|
||
return Objects.equals(value, that.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value != null ? value.hashCode() : 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
faunaJava/src/main/java/com/fauna/query/template/TemplatePartType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
package com.fauna.query.template; | ||
|
||
/** | ||
* Represents the type of template part within a FaunaTemplate. | ||
*/ | ||
public enum TemplatePartType { | ||
|
||
/** | ||
* Indicates a literal text part of the template. | ||
*/ | ||
LITERAL, | ||
|
||
/** | ||
* Indicates a variable placeholder part of the template. | ||
*/ | ||
VARIABLE | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.