#parsing quires

40 messages · Page 1 of 1 (latest)

south flare
#

i have a class called query and subqurey. i have method called read queries(List<String> queryTokens) how to make this method that each time it sees select it make a new query?

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

sturdy fableBOT
#

This post has been reserved for your question.

Hey @south flare! 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.

fringe salmon
#

What would each item in the list have?

south flare
#

ok so what queryTokens contain queryTokens[0] = select , queryTokens[1] = epl , queryTokens[2] = where

#

and so on

#

i am pretty sure that this code do this : for (int i = 0; i < queryTokens.size(); i++) {
String queryToken = queryTokens.get(i);
if (queryToken.equals("select"))

#

but how to make it like each time it creates a new query

fringe salmon
#

Just like that but under the if statement you say new Query()

#
for (int i = 0; i < queryTokens.size(); i++) {
  String queryToken = queryTokens.get(i);
  if (queryToken.equals("select")) {
    Query query = new Query();
    // whatever you need to do with the Query
  }
}
south flare
#

public List<Query> readQueries(List<String> queryTokens) throws IllegalArgumentException {
List<Query> queries = new ArrayList<>();
League league = null;
List<SubQuery> subQueries = new ArrayList<>();
for (int i = 0; i < queryTokens.size(); i++) {
String queryToken = queryTokens.get(i);
if (queryToken.equals("select")) {
if (league != null || !subQueries.isEmpty()) {
queries.add(new Query(subQueries, league));
}

#

because i want at the end to store each query created in a big list which is queries

fringe salmon
#

The if statement would not pass here because the subQueries list is empty

south flare
#

yeah

fringe salmon
#

So then you need to instantiate the League object and have the subQuery contain some data

#

Either that or remove the if statement, which would probably throw an exception if league is null

south flare
#

ok wait i want to show you something

#

i made this logic but without select : public List<Query> readQueries(List<String> queryTokens) throws IllegalArgumentException {
List<Query> queries = new ArrayList<>();
League league = null;
List<SubQuery> subQueries = new ArrayList<>();
int valueOfWhere = -1;
for (int i = 0; i < queryTokens.size(); i++) {
String queryToken = queryTokens.get(i);
if (queryToken.equals("where")) {
valueOfWhere = i;
break;
} else if ( (queryTokens.get(2).equals("or"))) {
league = League.ALL;
} else if (queryTokens.get(1).equals("epl")) {
league = League.EPL;
} else if (queryTokens.get(1).equals("liga")) {
league = League.LIGA;
} else {
throw new IllegalArgumentException("Invalid query");
}
}
if (valueOfWhere >= 0) {
subQueries.add(new SubQuery(PlayerProperty.fromName(queryTokens.get(valueOfWhere+1)), queryTokens.get(valueOfWhere+2) , Double.parseDouble(queryTokens.get(valueOfWhere+3))));
for (int j = valueOfWhere; j < queryTokens.size(); j++) {
if (queryTokens.get(j).equals("and")) {
subQueries.add(new SubQuery(PlayerProperty.fromName(queryTokens.get(j +1)), queryTokens.get(j+2), Double.parseDouble(queryTokens.get(j+3))));
}
}
}
if (league != null || !subQueries.isEmpty()) {
queries.add(new Query(subQueries, league));
}
return queries;
}

sturdy fableBOT
#
i made this logic but without select :                                                                                         public List<Query> readQueries(List<String> queryTokens) throws IllegalArgumentException {
        List<Query> queries = new ArrayList<>();
        League league = null;
        List<SubQuery> subQueries = new ArrayList<>();
        int valueOfWhere = -1;
        for (int i = 0; i < queryTokens.size(); i++) {
            String queryToken = queryTokens.get(i);
            if (queryToken.equals("where")) {
                valueOfWhere = i;
                break;
            } else if (  (queryTokens.get(2).equals("or"))) {
                league = League.ALL;
            } else if (queryTokens.get(1).equals("epl")) {
                league = League.EPL;
            } else if (queryTokens.get(1).equals("liga")) {
                league = League.LIGA;
            } else {
                throw new IllegalArgumentException("Invalid query");
            }
        }
        if (valueOfWhere >= 0) {
            subQueries.add(new SubQuery(PlayerProperty.fromName(queryTokens.get(valueOfWhere+1)), queryTokens.get(valueOfWhere+2)  , Double.parseDouble(queryTokens.get(valueOfWhere+3))));
            for (int j = valueOfWhere; j < queryTokens.size(); j++) {
                if (queryTokens.get(j).equals("and")) {
                    subQueries.add(new SubQuery(PlayerProperty.fromName(queryTokens.get(j +1)), queryTokens.get(j+2), Double.parseDouble(queryTokens.get(j+3))));
                }
            }
        }
        if (league != null || !subQueries.isEmpty()) {
            queries.add(new Query(subQueries, league));
        }
        return queries;
    }
south flare
#

but it need to be modified that to do this each time it sees select it made this all code

#

like until the next select

fringe salmon
#

Ok hold on...

south flare
#

the out put is this : In total, 1 queries were found.

Executing queries...
Query: EPL{GOALS > 2.0, GOALS < 5.0, ASSISTS > 0.1, MATCHES != 4.0, MATCHES != 6.0, PKATTEMPTS > 0.0, GOALS <= 6.0, MINUTES >= 1245.0, OWNGOALS > 0.0}

#

it should be 4

fringe salmon
#

Ok im just checking something... give me a few mins...

south flare
#

ok

fringe salmon
#
List<String> queries = new ArrayList<>();

    for (int i = 0; i < queryTokens.size(); i++) {
      if (queryTokens.get(i).equals("select")) {      // new query
        String thisQuery = "select ";       // this is the full query string
        int selectIndex = i ;         // keep track of the index of the select keyword
        int lastIndex = -1;       // keep track of the current index we are at
        for (int j = selectIndex + 1; j < queryTokens.size(); j++) {    // loop from select onwards
          lastIndex = j;
          if (queryTokens.get(j).equals("select")) {  // another query, so go out of the inner loop
            queries.add(thisQuery);
            break;
          }
          if (j == queryTokens.size() - 1) {    // end of the list, so just add the final element
            queries.add(thisQuery + " " + queryTokens.get(j));
          }
          thisQuery += queryTokens.get(j)  + " ";   // if not end of list or new query, add the word
        }
        i = lastIndex - 1;  // so we can continue onwards with next element
      }
    }
#

NOTE: This produces the queries as a string, and stores the result in the List called queries. You can then use this list to create your Query objects

sturdy fableBOT
#

💤 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.

south flare
#

what is the purpose of this line : queries.add(thisQuery);

#

@fringe salmon

fringe salmon
#

So that we can keep track of the current select statement before moving on to the next one

sturdy fableBOT
#

💤 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.

south flare
#

ok

#

public List<Query> readQueries(List<String> queryTokens) throws IllegalArgumentException {
List<Query> queries = new ArrayList<>();
League league = null;
List<SubQuery> subQueries = new ArrayList<>();
for (int i = 0; i < queryTokens.size(); i++) {
if (queryTokens.get(i).equals("select")) {
for (int j = i + 1; j < queryTokens.size(); j++) {
if (queryTokens.get(j).equals("select")) {
break;
}
if (queryTokens.get(j+1).equals("or")) {
league = League.ALL;
} else if (queryTokens.get(j).equals("epl") && queryTokens.get(j+1).equals("where")) {
league = League.EPL;
} else if (queryTokens.get(j).equals("liga") && queryTokens.get(j+1).equals("where")) {
league= League.LIGA;
} else if (SubQuery.isValidOperator(queryTokens.get(j)) ) {
subQueries.add(new SubQuery(PlayerProperty.fromName(queryTokens.get(j-1)), queryTokens.get(j), Double.parseDouble(queryTokens.get(j+1))));
} else {
throw new IllegalArgumentException("Invalid query");
}
queries.add(new Query(subQueries, league));
}
}
}
return queries;
}

#

what is wrong here?

#

it gives this error Process 'command '/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

#

@fringe salmon