Skip to content

Update_en

رامي مناف edited this page Aug 27, 2020 · 4 revisions

Update Executable Command

When the user wants to change his name in your website your code should change it from the database. This mission is for the Update command which will let you update the value of one object or more in the database and putting some conditions on what object you want to update (this will be explained later). You can pass the updated object to the set(Object) method to replace the old object/s. This command has two constructors. The first one has a Class parameter and executing this will update all the instances from the passed class if there are no conditions. The second one has an Object parameter and when executing this the command will search for any object that equals this object and will update it. So, you should overwrite the equal(Object) method to suit your needs. This example will show you how to use this command.

public class Updating {
    public static void main(String[] args) throws SofofException {
        Server s = new Server(new File("sofof"), 6969, false);
        s.createDatabase();
        s.getUsers().add(new User("rami", "secret"));
        s.startUp();
        Session sess = SessionManager.startSession("sofof:localhost:6969", new User("rami", "secret"), false);
        Student rami = new Student(0, "Rami");
        sess.execute(new Bind(rami).to("students"));
        rami.setName("Hani");
        sess.execute(new Update(rami).from("students").set(rami));
    }
    
    public static class Student{
        private int id;
        private String name;

        public Student() {}

        public Student(int id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public boolean equals(Object obj) {
            if(obj == null)return false;
            if(!(obj instanceof Student))return false;
            Student s2 = (Student)obj;
            return s2.id == id;
        }

        public void setName(String name) {
            this.name = name;
        }
        
    }
}

Clone this wiki locally