#parsing query

6 messages · Page 1 of 1 (latest)

sharp bloom
#

if i have a txt file contain the following :

select [epl | liga | epl or liga | liga or epl] where [subqueries]

select epl where Goals > 2
select epl or liga where Goals > 2 and Goals < 5 and Assists > 0.1
select epl where Matches != 2 and Matches != 4 and Matches != 6 and PKAttempts > 0
select epl or liga where Goals > 5 and Goals <= 6 and Minutes >= 1245
select liga where Goals = 12 and OwnGoals > 0

i want to read what is after where (subqueries) what to do?

the following method should do so
/**
* 1 - receives the List of Strings, each is a single token
* 2 - assesses their content, creates the relevant Query & SubQuery objects
* 3 - and then returns a List of the Query objects
*
* @param queryTokens The List of tokenized Strings from the readQueryFile method
* @return List of all Query objects
* @throws IllegalArgumentException if the provided query tokens are invalid (e.g. non-numbers
* as boundary values, invalid operators, etc)
*/
public abstract List<Query> readQueries(List<String> queryTokens) throws IllegalArgumentException;

sinful kelpBOT
#

This post has been reserved for your question.

Hey @sharp bloom! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

torn phoenix
#

You can first read the text file, extract the subqueries, and then use the readQueries method to create the list of Query objects.

Here's a possible implementation:

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class QueryReader {

    // Read the text file and extract the subqueries
    public static List<String> readQueryFile(String fileName) throws IOException {
        List<String> subqueries = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
                int whereIndex = line.indexOf("where");
                if (whereIndex != -1) {
                    String subquery = line.substring(whereIndex + 5).trim();
                    subqueries.add(subquery);
                }
            }
        }
        return subqueries;
    }

    public abstract List<Query> readQueries(List<String> queryTokens) throws IllegalArgumentException;

    public static void main(String[] args) throws IOException {
        String fileName = "path/to/your/txtfile.txt"; // Replace with the path to your text file
        List<String> subqueries = readQueryFile(fileName);

        // Create a new instance of your QueryReader subclass and call the readQueries method
        // YourQueryReaderClass queryReader = new YourQueryReaderClass();
        // List<Query> queries = queryReader.readQueries(subqueries);

        // Print the subqueries for now
        for (String subquery : subqueries) {
            System.out.println(subquery);
        }
    }
}```
Replace path/to/your/txtfile.txt with the path to your text file.

The readQueryFile method reads the text file, extracts the subqueries, and returns them as a list of strings. You can then pass this list of strings to your readQueries method to create the list of Query objs
sinful kelpBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

sharp bloom
#

but reading is already been provided for me : public static List<String> readQueryFile(String queryFile) {

    List<String> queryTokens = new ArrayList<>();
    String split = " ";

    try (BufferedReader br = new BufferedReader(new FileReader(queryFile))) {
        String line = br.readLine();
        if (line == null) {
            throw new IllegalArgumentException("File is empty");
        }
        while (line != null) {
            String[] query = line.toLowerCase().split(split);
            queryTokens.addAll(Arrays.asList(query));
            line = br.readLine();
        }

    } catch (FileNotFoundException e) {
        System.err.println(queryFile + " could not be found");
    } catch (IOException e) {
        System.err.println("File could not be handled");
        // enable the next line for debugging purposes
        // e.printStackTrace();
    }
    return queryTokens;
}