-
Notifications
You must be signed in to change notification settings - Fork 0
Conditions_en
Sometimes when you use commands to do processes on your objects you want to only affect specific objects that have special properties from other objects e.g: you want to query about the users who live in Syria or you want to update customers information who has more than 1000$. For such cases, you should use a certain implementation of the Conditino interface from the org.sofof.command.condition package.
This condition is the simplest condition and depends on a boolean value to determine if the condition will accept all the objects or will reject them. You can use this any condition by passing it to the common method in the commands where(Condition). If you used this condition with the Select query then if the boolean passed value to the BooleanCondition is false then the returned list of the query will be empty. You can use this condition with Update and Unbind in the same way as using it with Select query. The main idea is that the condition will check if it will accept the affected object of the command or will reject it. This example will show you how to use this condition with Update command:
import org.sofof.command.Update;
import org.sofof.command.condition.BooleanCondition;
import org.sofof.permission.User;
import java.io.File;
public class BooleanConditionTesting {
public static void main(String[] args) throws SofofException {
Database.createDatabase(new File("sofof"));
Server s = new Server(new File("sofof"), 6969, false);
s.getUsers().add(new User("rami", "secret"));
s.startUp();
Session sess = new Database("localhost", 6969).startSession(new User("rami", "secret"), false);
int affected = sess.execute(new Update(String.class).from("comments").set("unavilabel").where(new BooleanCondition(false)));
System.out.println(affected);
}
}
//result
0This condition goal is to select a certain number of affected objects. Using this condition for example with Unbind command after passing a certain number to it -e.g:5- will unbind that number of objects from the database -5 objects in our case-. If the number of existing objects that are affected by the command is less than the passed number to this condition then all objects will be affected by that command and no exception will be thrown. This example will show you how to use this condition with the Select query:
import org.sofof.command.Select;
import org.sofof.command.condition.LimitCondition;
import org.sofof.permission.User;
import java.io.File;
public class LimitedProjects {
public static void main(String[] args) throws SofofException {
Database.createDatabase(new File("sofof"));
Server s = new Server(new File("sofof"), 6969, false);
s.getUsers().add(new User("rami", "secret"));
s.startUp();
Session sess = new Database("localhost", 6969).startSession(new User("rami", "secret"));
sess.query(new Select(Project.class).from("website").where(new LimitCondition(5)));
}
}This condition is considered of the most important conditions that are used with any application. It depends on the executable expression to write conditions that let this condition to have high flexibility to put conditions on objects by accessing fields and calling methods. It has one constructor that recieves three parameters. The first one and the third one are an Object and can be interpreted as executable expression if they are Strings and starts with the symbol # (if you wanted to escape a String that starts with the symbol # you should write the symbol # twice and the first one will be removed and the String will not be interpreted as an executable expression). The second parameter is an Operation enumeration which represents the operation that will be done on the first and the third parameters. The Operation values are list here:
- Equal: this operation will call the equals method on the two objects.
- NotEqual: opposite to Equal.
- Greater: used to check if the first parameter is greater than the third parameter and is only available to compare numbers.
- GreaterOrEqual: like Greater but will return true if the two numbers are equals.
- Less: opposite to Greater.
- LessOrEqual: like Less but will return true if the two numbers are equals. To obvious that we can imagine that we are building a website and we want to select the articles that have more than 100 reads. Regardless of how the Essay object will work the code will be as follows:
List<Essay> essays = session.query(new Select(Essay.class).from("essays").where(new ObjectCondition("#getViews()", Operation.Greater, 100)));The interface Condition has many methods that let you link two conditions with a specific relation. These methods don't affect the condition objects but will create a new condition object that represents the relation between the two conditions. These relations are logical relations (and, or, xor, not) and are used in some complex causes e.g: when you want to select the players who passed the fifth level and have less than 10,000 coins to duplicate their coins as a gift the code will be as follows:
List<Player> players = session.query(new Select(Player.class).from("players").where(new ObjectCondition("#getLevel()", Operation.Greater, 5).and(new ObjectCondition("#getResources().getGold()", Operation.Less, 10000))));