package h2o.dao.advanced;

import h2o.common.util.lang.StringUtil;
import h2o.dao.Dao;
import h2o.dao.DbUtil;

import java.util.List;

/**
 * Created by zhangjianwei on 2017/7/1.
 */
public final class DaoBasicUtil {

    private final Dao dao;

    public DaoBasicUtil() {
        this.dao = DbUtil.getDao();
    }

    public DaoBasicUtil( Dao dao ) {
        this.dao = dao;
    }


    public void add( Object entity ) {
        dao.update( DbUtil.sqlBuilder.buildInsertSql(entity) , entity );
    }

    public void batAdd( List<Object> entities ) {
        dao.update( DbUtil.sqlBuilder.buildInsertSqlIncludeNull(entities.get(0)) , entities );
    }

    public int edit( AbstractEntity entity ) {
        return dao.update( DbUtil.sqlBuilder.buildUpdateSql3( entity , entity.getW() , entity.getPks() ) , entity );
    }

    public int edit(Object entity , String w , String... skip ) {
        return dao.update( DbUtil.sqlBuilder.buildUpdateSql3( entity , w , skip ) , entity );
    }

    public int del( AbstractEntity entity ) {
        return dao.update( "delete from " + entity.getTableName() +  " where " + entity.getW() , entity );
    }

    public <T extends AbstractEntity> T get( T entity ) {
        return this.get( entity , false );
    }

    public <T extends AbstractEntity> T getAndLock( T entity ) {
        return this.get( entity , true );
    }

    public <T extends AbstractEntity> T get( T entity , boolean lock ) {

        StringBuilder sql = new StringBuilder();

        StringUtil.append( sql , "select * from " , entity.getTableName() ,  " where " , entity.getW() );
        if( lock ) {
            sql.append(" for update ");
        }

        return (T)dao.get( entity.getClass() , sql.toString() , entity );
    }

}

@@ -770,33 +770,34 @@
 ity , entity.get
-W
+_w
 () , entity.getP
@@ -799,9 +799,10 @@
 .get
-P
+_p
 ks()
@@ -1082,33 +1082,34 @@
 m %22 + entity.get
-T
+_t
 ableName() +  %22 
@@ -1128,17 +1128,18 @@
 tity.get
-W
+_w
 () , ent
@@ -1560,17 +1560,18 @@
 tity.get
-T
+_t
 ableName
@@ -1602,9 +1602,10 @@
 .get
-W
+_w
 () )

修改getter名,以免冲突