Package com.osohq.oso_cloud.internal
Class QueryBuilderImpl
- java.lang.Object
-
- com.osohq.oso_cloud.internal.QueryBuilderImpl
-
- All Implemented Interfaces:
QueryBuilder,QueryComposer<QueryBuilder>
public class QueryBuilderImpl extends java.lang.Object implements QueryBuilder
Internal helper class to support building a custom Oso query. Not intended for public use.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classQueryBuilderImpl.EvaluateArg<T>Represents the structure of an argument passed toQueryBuilder.evaluate(EvaluateArg), determining the shape of the expected result.static classQueryBuilderImpl.ExistsArgArgument for checking query existence.static classQueryBuilderImpl.MapArg<SubT>Argument for retrieving nested map structures.static classQueryBuilderImpl.TypedVarArgArgument for retrieving values of a single variable.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description QueryBuilderImpland(java.lang.String predicate, QueryArg... args)Add another condition that must be true of the query results.<T> Tevaluate(QueryBuilderImpl.EvaluateArg<T> arg)Evaluate the query based on the structure of the providedQueryBuilderImpl.EvaluateArg.java.lang.StringevaluateLocalFilter(java.lang.String columnName, TypedVar queryVar)Fetches a SQL fragment, which you can embed into theWHEREclause of a SQL query against your database to filter out unauthorized rows (ie.java.lang.StringevaluateLocalSelect()Fetches a complete SQL query that can be run against your database, selecting a single row with a boolean column calledresult.java.lang.StringevaluateLocalSelect(java.util.Map<java.lang.String,TypedVar> columnNamesToQueryVars)Fetches a complete SQL query that can be run against your database, selecting a row for each authorized combination of the query variables incolumnNamesToQueryVars(ie.QueryBuilderImplin(TypedVar variable, java.util.List<java.lang.String> values)Constrain a query variable to be one of a set of values.static QueryBuilderImplinit(Api api, java.lang.String predicate, QueryArg... args)Creates a new QueryBuilder with the specified predicate and arguments.QueryBuilderImplwithContextFacts(java.util.List<Fact> facts)Add context facts to the query.
-
-
-
Method Detail
-
init
public static QueryBuilderImpl init(Api api, java.lang.String predicate, QueryArg... args)
Creates a new QueryBuilder with the specified predicate and arguments.- Parameters:
api- The API instance to usepredicate- The predicate of the queryargs- The arguments to the predicate- Returns:
- A new QueryBuilder
-
and
public QueryBuilderImpl and(java.lang.String predicate, QueryArg... args)
Description copied from interface:QueryComposerAdd another condition that must be true of the query results. For example:// Query for all the repos on which the user can perform read action, // and require the repos to belong to a specific folder TypedVar repo = new TypedVar("Repo"); List<String> authorizedReposInFolder = oso .buildQuery("allow", new Value("User", "user123"), new Value("read"), repo) .and("has_relation", repo, new Value("folder"), new Value("Folder", "folder456")) .evaluate(repo);- Specified by:
andin interfaceQueryComposer<QueryBuilder>- Parameters:
predicate- The predicate of the conditionargs- The arguments to the condition- Returns:
- A new QueryBuilder with the added condition
-
in
public QueryBuilderImpl in(TypedVar variable, java.util.List<java.lang.String> values)
Description copied from interface:QueryComposerConstrain a query variable to be one of a set of values. For example:TypedVar repo = new TypedVar("Repo"); TypedVar action = new TypedVar("String"); // Get all the actions a user can perform on specific repos List<String> authorizedActions = oso .buildQuery("allow", new Value("User", "user123"), action, repo) .in(repo, Arrays.asList("acme", "anvil")) .evaluate(action);- Specified by:
inin interfaceQueryComposer<QueryBuilder>- Parameters:
variable- The variable to constrainvalues- The set of allowed values for the constrained variable- Returns:
- A new QueryBuilder with the added constraint
-
withContextFacts
public QueryBuilderImpl withContextFacts(java.util.List<Fact> facts)
Description copied from interface:QueryComposerAdd context facts to the query.- Specified by:
withContextFactsin interfaceQueryComposer<QueryBuilder>- Parameters:
facts- The context facts to add- Returns:
- A new QueryBuilder with the added context facts
-
evaluate
public <T> T evaluate(QueryBuilderImpl.EvaluateArg<T> arg) throws java.io.IOException, ApiException
Description copied from interface:QueryBuilderEvaluate the query based on the structure of the providedQueryBuilderImpl.EvaluateArg. The shape of the return value is determined by the specificEvaluateArgused, constructed typically via theEvaluateArgsstatic factory methods.EvaluateArgs.exists(): ReturnsBooleanindicating if the query is satisfiable.EvaluateArgs.values(TypedVar): ReturnsList<String>of values for the variable.EvaluateArgs.map(TypedVar, EvaluateArg): ReturnsMap<String, SubT>where keys are unique values of the key variable, and values are the results of recursively evaluating the nested value argument (SubT). This allows arbitrarily nested structures.
- Specified by:
evaluatein interfaceQueryBuilder- Type Parameters:
T- The expected result type, inferred from thearg.- Parameters:
arg- AnQueryBuilderImpl.EvaluateArgspecifying the desired structure of the results. UseEvaluateArgsfactory methods.- Returns:
- An object of type
Trepresenting the evaluated results, matching the structure requested byarg. - Throws:
java.io.IOException- if an I/O error occurs during the API call.ApiException- if an API error occurs during the API call.
-
evaluateLocalSelect
public java.lang.String evaluateLocalSelect(java.util.Map<java.lang.String,TypedVar> columnNamesToQueryVars) throws java.io.IOException, ApiException
Description copied from interface:QueryBuilderFetches a complete SQL query that can be run against your database, selecting a row for each authorized combination of the query variables incolumnNamesToQueryVars(ie. combinations of variables that satisfy the Oso query).See the Oso documentation for examples and limitations.
- Specified by:
evaluateLocalSelectin interfaceQueryBuilder- Parameters:
columnNamesToQueryVars- A mapping of the desired column names to the query variables whose values will be selected into those columns in the returned SQL query.- Returns:
- SQL query containing the desired columns.
- Throws:
java.io.IOException- if an I/O error occurs during the API call.ApiException- if an API error occurs during the API call.
-
evaluateLocalSelect
public java.lang.String evaluateLocalSelect() throws java.io.IOException, ApiExceptionDescription copied from interface:QueryBuilderFetches a complete SQL query that can be run against your database, selecting a single row with a boolean column calledresult.See the Oso documentation for examples and limitations.
- Specified by:
evaluateLocalSelectin interfaceQueryBuilder- Returns:
- SQL query containing the
resultcolumn. - Throws:
java.io.IOException- if an I/O error occurs during the API call.ApiException- if an API error occurs during the API call.
-
evaluateLocalFilter
public java.lang.String evaluateLocalFilter(java.lang.String columnName, TypedVar queryVar) throws java.io.IOException, ApiExceptionDescription copied from interface:QueryBuilderFetches a SQL fragment, which you can embed into theWHEREclause of a SQL query against your database to filter out unauthorized rows (ie. rows that don't satisfy the Oso query).See the Oso documentation for examples and limitations.
- Specified by:
evaluateLocalFilterin interfaceQueryBuilder- Parameters:
columnName- The name of the SQL column to filter.queryVar- The variable corresponding to the column to filter.- Returns:
- SQL fragment, suitable for embedding in a
WHEREclause. - Throws:
java.io.IOException- if an I/O error occurs during the API call.ApiException- if an API error occurs during the API call.
-
-