-
Notifications
You must be signed in to change notification settings - Fork 11
EQL
Brian Zou edited this page Mar 6, 2019
·
3 revisions
class User
{
mixin MakeModel;
@PrimaryKey
@AutoIncrement
int id;
@Column("nickname")
string nickName;
int age;
}
class App
{
mixin MakeModel;
@PrimaryKey
@AutoIncrement
int id;
string name;
string desc;
}
class LoginInfo
{
mixin MakeModel;
@AutoIncrement
@PrimaryKey
int id;
int create_time;
int update_time;
// int uid;
@JoinColumn("uid", true)
User user;
@JoinColumn("appid")
App app;
}
EntityOption option = new EntityOption();
option.database.driver = "mysql";
option.database.host = "127.0.0.1";
option.database.port = 3306;
option.database.database = "eql_test";
option.database.username = "root";
option.database.password = "password";
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("mysql", option);
EntityManager em = entityManagerFactory.createEntityManager();
// Example for sample SELECT
auto query1 = em.createQuery!(User)("SELECT a FROM User a ;");
foreach(d ; query1.getResultList())
{
logDebug("User( %s , %s , %s ) ".format(d.id, d.nickName, d.age));
}
// Example for LEFO JOIN multi-entity result
auto query2 = em.createQuery!(LoginInfo)("SELECT a, b FROM LoginInfo a LEFT JOIN a.user b ;");
foreach(d ; query2.getResultList())
{
logDebug("LoginInfo.User( %s , %s , %s ) ".format(d.user.id, d.user.nickName, d.user.age));
logDebug("LoginInfo( %s , %s , %s ) ".format(d.id, d.create_time, d.update_time));
}
// Example for LEFT JOIN
auto query3 = em.createQuery!(LoginInfo)("SELECT b FROM LoginInfo a LEFT JOIN a.user b ;");
foreach(d ; query3.getResultList())
{
logDebug("LoginInfo.User( %s , %s , %s ) ".format(d.user.id, d.user.nickName, d.user.age));
}
// Example for LIMIT
auto query4 = em.createQuery!(LoginInfo)("SELECT a.id, a.create_time , b.nickName FROM LoginInfo a LEFT JOIN a.user b WHERE a.id in (:id1, :id2) ORDER by a.id DESC LIMIT 0, 1");
query4.setParameter("id1", 2);
query4.setParameter("id2", 1);
foreach(d ; query4.getResultList())
{
logDebug("Mixed Results( %s , %s , %s ) ".format(d.id, d.create_time, d.user.nickName));
}
// Example for ORDER BY
auto query5 = em.createQuery!(LoginInfo)("SELECT a, b , c FROM LoginInfo a LEFT JOIN a.user b JOIN a.app c WHERE a.id = :id ORDER BY a.id DESC;");
query5.setParameter("id", 2);
foreach(d ; query5.getResultList())
{
logDebug("LoginInfo.User( %s , %s , %s ) ".format(d.user.id, d.user.nickName, d.user.age));
logDebug("LoginInfo.App( %s , %s , %s ) ".format(d.app.id, d.app.name, d.app.desc));
logDebug("LoginInfo( %s , %s , %s ) ".format(d.id, d.create_time, d.update_time));
}
auto update = em.createQuery!(User)("UPDATE User u set u.age = :age WHERE u.id = :id "); // update User u set u.age = 5 where u.id = 2
update.setParameter("age", 2);
update.setParameter("id", 5);
logDebug(" update result : ", update.exec());
auto query = em.createQuery!(User)("DELETE User u WHERE u.id = :id ");
query.setParameter("id", 3);
logDebug(" del result : ", query.exec());