How to operate database with eql statement

Keywords: Programming SQL Database

  • Query database with eql statement
string sql = " SELECT * FROM XX x";

        string[] wheres;
        if(c.length)//(string type)
        {
            wheres ~= "x.c '%" ~ c ~ "%'";//% "~"% is used to concatenate strings 
        }

        if (z)
        {
            wheres ~= "x.z = :z"; 
        }

        

        if (wheres.length > 0)
        {
            sql ~= " WHERE ";

            for (int i = 0; i < wheres.length; i++)
            {
                if (i > 0)
                {
                    sql ~= " AND ";
                }

                sql ~= wheres[i];
            }
        }

        sql ~= " ORDER BY x.created DESC(ASC)";//This part is to sort the query conditions according to the creation time, and to replace the fields according to the requirements (DESC is in reverse order, ASC is in positive order).
        auto query = _manager.createQuery!(XX)(sql, new pageable(page-1, limit));
        query.setParameter("c",c);
        query.setParameter("z",z);
        return query.getPageResult;
        
  • Modifying database information with eql statement

Here's an example of a modification operation

auto temp1 = _manager.createQuery!(Post)(" UPDATE Post p set p.deleted = :now WHERE p.id = :deleted ")
        .setParameter("now", time)
        .setParameter("deleted", deleted)
        .exec();
        
        logError(temp1);
        return true;
        
        //This section modifies the deletion time in the Post table
  • Delete database information with eql statement

Here's an example of a delete operation

auto del = _manager.createQuery!(ItemMini)(" delete User u where u.id = :Id ")
            .setParameter("Id", Id)
	        .exec();
  • Add database information with eql statement

Here is an example of an add operation

auto insert = em.createQuery!(UInfo)("  INSERT INTO UInfo u(u.nickName,u.age) values (:name,:age)"); 
	insert.setParameter("name","momomo");
	insert.setParameter("age",666);
logDebug(" insert result : ",insert.exec());
  • If two or more tables are queried, it can be processed in the following way

Here is an example of a multi-table Association query

string sql = " SELECT x,c,u FROM XX x LEFT JOIN  CC c ON c.id = x.cc_id LEFT JOIN UU u ON u.id = x.uid ";
//Then you can use the template of the query statement.
  • How to conduct fuzzy queries on data tables Here's an example of a fuzzy query
Select * from like'%'~xx%'to splice strings with%'~'%
// Fuzzy Query Statement
  • How to Page in D Language

Here is an example of paging operations

Page!XXX findPageByXXX(string class_name,int teacherId,int classId, int page = 1, int limit = 10)
    {
       
             auto result =  _manager.createQuery!(Classes)(" SELECT * FROM XXX x ORDER BY x.created DESC ", new Pageable(page-1, limit))
            .getPageResult();
            return result;
        }
        //Use create DESC (reverse order) and ASC (positive order) if sorted by creation time. If sorted by other fields, use the actual situation.

Posted by ahsanmani on Fri, 04 Oct 2019 15:16:04 -0700