package org.ruler.sample;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ruler.Client;
import org.ruler.Mode;
import org.ruler.Provider;
import org.ruler.Server;
public class Main {
public static void main
(String[] args
) { try {
new Main().test();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
Server server = new Server(Mode.tcp); // HSQL localhost
server.start();
// Provider likes EntityManagerFactory.class of JPA or Hibernate etc...
provider.bind(User.class, () -> new User());
provider.open();
// Cliant likes EntityManager.class of JPA or Hibernate etc...
try(Client client = provider.open()){ // AutoCloseable.close().
// 前回のデータが残っていたらドロップする。
client.table.drop(User.class);
client.table.create(User.class);
for(int i=0; i<100; i++){
// CRUD(Create)
User user = client.insert(User.class);
// CRUD(Update)
user.name.update("Java Entity Ruler");
user.age.update(i);
client.update(user);
}
Map
<Integer, User
> map
= new HashMap
<>(); List<User> list = new ArrayList<>();
// CRUD(Read)
client.find(User.class, (e) -> {
// Event e == JDBC ResultSet, that will be closed after lambda.
int count = e.count(); // all size (SQL COUNT(*))
int index = e.index();
User user = e.value();
// from 25% to 75%
if(index > count/4 && index < count*3/4){
// CRUD(Delete)
client.remove(user);
// return value with Map<int, Entity>.
int age = user.age.value();
map.put(age, user);
// Entity value with List<Entity>.
list.add(user);
}
});
for(User user : list){
long id = user.id.value();
String msg
= "User id("+id
+") is deleted."; }
int age = 99;
User user = map.get(age);
if( user != null ){
String msg
= "User age("+age
+") is deleted."; }
client.table.drop(User.class);
}
provider.close();
server.stop();
server.dispose();
}
}
cGFja2FnZSBvcmcucnVsZXIuc2FtcGxlOwoKaW1wb3J0IGphdmEuc3FsLlNRTEV4Y2VwdGlvbjsKaW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuSGFzaE1hcDsKaW1wb3J0IGphdmEudXRpbC5MaXN0OwppbXBvcnQgamF2YS51dGlsLk1hcDsKCmltcG9ydCBvcmcucnVsZXIuQ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gaW5kZXggPCBjb3VudCozLzQpewoJCQkJCS8vIENSVUQoRGVsZXRlKQoJCQkJCWNsaWVudC5yZW1vdmUodXNlcik7CgoJCQkJCS8vIHJldHVybiB2YWx1ZSB3aXRoIE1hcDxpbnQsIEVudGl0eT4uCgkJCQkJaW50IGFnZSA9IHVzZXIuYWdlLnZhbHVlKCk7CgkJCQkJbWFwLnB1dChhZ2UsIHVzZXIpOwoKCQkJCQkvLyBFbnRpdHkgdmFsdWUgd2l0aCBMaXN0PEVudGl0eT4uCgkJCQkJbGlzdC5hZGQodXNlcik7CgkJCQl9CgkJCX0pOwoKCQkJZm9yKFVzZXIgdXNlciA6IGxpc3QpewoJCQkJbG9uZyBpZCA9IHVzZXIuaWQudmFsdWUoKTsKCQkJCVN0cmluZyBtc2cgPSAiVXNlciBpZCgiK2lkKyIpIGlzIGRlbGV0ZWQuIjsKCQkJCVN5c3RlbS5vdXQucHJpbnRsbihtc2cpOwoJCQl9CgoJCQlpbnQgYWdlID0gOTk7CgkJCVVzZXIgdXNlciA9IG1hcC5nZXQoYWdlKTsKCQkJaWYoIHVzZXIgIT0gbnVsbCApewoJCQkJU3RyaW5nIG1zZyA9ICJVc2VyIGFnZSgiK2FnZSsiKSBpcyBkZWxldGVkLiI7CgkJCQlTeXN0ZW0ub3V0LnByaW50bG4obXNnKTsKCQkJfQoKCgkJCWNsaWVudC50YWJsZS5kcm9wKFVzZXIuY2xhc3MpOwoKCQl9CgoJCXByb3ZpZGVyLmNsb3NlKCk7CgoJCXNlcnZlci5zdG9wKCk7CgkJc2VydmVyLmRpc3Bvc2UoKTsKCX0KfQ==
Main.java:34: error: illegal start of expression
provider.bind(User.class, () -> new User());
^
Main.java:34: error: illegal start of expression
provider.bind(User.class, () -> new User());
^
Main.java:62: error: illegal start of expression
client.find(User.class, (e) -> {
^
Main.java:62: error: illegal start of expression
client.find(User.class, (e) -> {
^
Main.java:62: error: ';' expected
client.find(User.class, (e) -> {
^
Main.java:82: error: illegal start of expression
});
^
Main.java:102: error: <identifier> expected
provider.close();
^
Main.java:104: error: <identifier> expected
server.stop();
^
Main.java:105: error: <identifier> expected
server.dispose();
^
Main.java:107: error: class, interface, or enum expected
}
^
10 errors