Class QueryBuilderImpl

    • 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 use
        predicate - The predicate of the query
        args - The arguments to the predicate
        Returns:
        A new QueryBuilder
      • and

        public QueryBuilderImpl and​(java.lang.String predicate,
                                    QueryArg... args)
        Description copied from interface: QueryComposer
        Add 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:
        and in interface QueryComposer<QueryBuilder>
        Parameters:
        predicate - The predicate of the condition
        args - 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: QueryComposer
        Constrain 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:
        in in interface QueryComposer<QueryBuilder>
        Parameters:
        variable - The variable to constrain
        values - The set of allowed values for the constrained variable
        Returns:
        A new QueryBuilder with the added constraint
      • evaluate

        public <T> T evaluate​(QueryBuilderImpl.EvaluateArg<T> arg)
                       throws java.io.IOException,
                              ApiException
        Description copied from interface: QueryBuilder
        Evaluate the query based on the structure of the provided QueryBuilderImpl.EvaluateArg. The shape of the return value is determined by the specific EvaluateArg used, constructed typically via the EvaluateArgs static factory methods.
        Specified by:
        evaluate in interface QueryBuilder
        Type Parameters:
        T - The expected result type, inferred from the arg.
        Parameters:
        arg - An QueryBuilderImpl.EvaluateArg specifying the desired structure of the results. Use EvaluateArgs factory methods.
        Returns:
        An object of type T representing the evaluated results, matching the structure requested by arg.
        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: QueryBuilder
        Fetches a complete SQL query that can be run against your database, selecting a row for each authorized combination of the query variables in columnNamesToQueryVars (ie. combinations of variables that satisfy the Oso query).

        See the Oso documentation for examples and limitations.

        Specified by:
        evaluateLocalSelect in interface QueryBuilder
        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,
                                                    ApiException
        Description copied from interface: QueryBuilder
        Fetches a complete SQL query that can be run against your database, selecting a single row with a boolean column called result.

        See the Oso documentation for examples and limitations.

        Specified by:
        evaluateLocalSelect in interface QueryBuilder
        Returns:
        SQL query containing the result column.
        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,
                                                    ApiException
        Description copied from interface: QueryBuilder
        Fetches a SQL fragment, which you can embed into the WHERE clause 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:
        evaluateLocalFilter in interface QueryBuilder
        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 WHERE clause.
        Throws:
        java.io.IOException - if an I/O error occurs during the API call.
        ApiException - if an API error occurs during the API call.