Search in sources :

Example 1 with EntityMeta

use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.

the class MySqlDialect method saveAllIgnoreExist.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#saveAllNotExist(org.sagacity.sqltoy.
	 * SqlToyContext, java.util.List,
	 * org.sagacity.sqltoy.callback.ReflectPropertyHandler, java.sql.Connection,
	 * java.lang.Boolean)
	 */
@Override
public Long saveAllIgnoreExist(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, ReflectPropertyHandler reflectPropertyHandler, Connection conn, final Boolean autoCommit, final String tableName) throws Exception {
    // mysql只支持identity,sequence 值忽略
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    boolean isAssignPK = isAssignPKValue(entityMeta.getIdStrategy());
    String insertSql = DialectUtils.generateInsertSql(DBType.MYSQL, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPK, tableName).replaceFirst("(?i)insert ", "insert ignore ");
    return DialectUtils.saveAll(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPK, insertSql, entities, batchSize, reflectPropertyHandler, conn, autoCommit);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta)

Example 2 with EntityMeta

use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.

the class Oracle12Dialect method update.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#update(org.sagacity.sqltoy.
	 * SqlToyContext , java.io.Serializable, java.lang.String[],
	 * java.sql.Connection)
	 */
@Override
public Long update(SqlToyContext sqlToyContext, Serializable entity, String[] forceUpdateFields, final boolean cascade, final Class[] forceCascadeClass, final HashMap<Class, String[]> subTableForceUpdateProps, Connection conn, final String tableName) throws Exception {
    return DialectUtils.update(sqlToyContext, entity, NVL_FUNCTION, forceUpdateFields, cascade, (cascade == false) ? null : new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
            PKStrategy pkStrategy = entityMeta.getIdStrategy();
            String sequence = entityMeta.getSequence() + ".nextval";
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
                sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
            }
            return DialectUtils.getSaveOrUpdateSql(DBType.ORACLE12, entityMeta, pkStrategy, forceUpdateFields, "dual", NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), null);
        }
    }, forceCascadeClass, subTableForceUpdateProps, conn, tableName);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 3 with EntityMeta

use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.

the class DB2Dialect method loadAll.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#loadAll(java.util.List,
	 * java.util.List, java.sql.Connection)
	 */
@Override
public List<?> loadAll(final SqlToyContext sqlToyContext, List<?> entities, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
    if (null == entities || entities.isEmpty())
        return null;
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    // 判断是否存在主键
    if (null == entityMeta.getIdArray())
        throw new Exception(entities.get(0).getClass().getName() + "Entity Object hasn't primary key,cann't use load method!");
    StringBuilder loadSql = new StringBuilder();
    loadSql.append("select * from ");
    loadSql.append(tableName == null ? entityMeta.getSchemaTable() : tableName);
    loadSql.append(" where ");
    String field;
    for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
        field = entityMeta.getIdArray()[i];
        if (i > 0)
            loadSql.append(" and ");
        loadSql.append(entityMeta.getColumnName(field));
        loadSql.append(" in (:").append(field).append(") ");
    }
    if (lockMode != null) {
        switch(lockMode) {
            case UPGRADE_NOWAIT:
            case UPGRADE:
                loadSql.append(" for update with rs ");
                break;
        }
    }
    return DialectUtils.loadAll(sqlToyContext, loadSql.toString(), entities, cascadeTypes, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta)

Example 4 with EntityMeta

use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.

the class MySqlDialect method loadAll.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#loadAll(java.util.List,
	 * java.util.List, java.sql.Connection)
	 */
@Override
public List<?> loadAll(final SqlToyContext sqlToyContext, List<?> entities, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
    if (null == entities || entities.isEmpty())
        return null;
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    // 判断是否存在主键
    if (null == entityMeta.getIdArray() || entityMeta.getIdArray().length < 1)
        throw new Exception(entities.get(0).getClass().getName() + " Entity Object hasn't primary key,cann't use load method!");
    StringBuilder loadSql = new StringBuilder();
    loadSql.append("select * from ");
    loadSql.append(entityMeta.getSchemaTable());
    loadSql.append(" where ");
    String field;
    for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
        field = entityMeta.getIdArray()[i];
        if (i > 0)
            loadSql.append(" and ");
        loadSql.append(entityMeta.getColumnName(field));
        loadSql.append(" in (:").append(field).append(") ");
    }
    if (lockMode != null) {
        switch(lockMode) {
            case UPGRADE_NOWAIT:
            case UPGRADE:
                loadSql.append(" for update ");
                break;
        }
    }
    return DialectUtils.loadAll(sqlToyContext, loadSql.toString(), entities, cascadeTypes, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta)

Example 5 with EntityMeta

use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.

the class MySqlDialect method save.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#save(org.sagacity.sqltoy.
	 * SqlToyContext , java.io.Serializable, java.util.List, java.sql.Connection)
	 */
@Override
public Object save(SqlToyContext sqlToyContext, Serializable entity, Connection conn, final String tableName) throws Exception {
    // mysql只支持identity,sequence 值忽略,mysql identity可以手工插入
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    boolean isAssignPK = isAssignPKValue(entityMeta.getIdStrategy());
    String insertSql = DialectUtils.generateInsertSql(DBType.MYSQL, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPK, tableName);
    ReturnPkType returnPkType = (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.SEQUENCE)) ? ReturnPkType.GENERATED_KEYS : ReturnPkType.PREPARD_ID;
    return DialectUtils.save(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPK, returnPkType, insertSql, entity, new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
            return DialectUtils.generateInsertSql(DBType.MYSQL, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPKValue(entityMeta.getIdStrategy()), null);
        }
    }, new GenerateSavePKStrategy() {

        public SavePKStrategy generate(EntityMeta entityMeta) {
            return new SavePKStrategy(entityMeta.getIdStrategy(), isAssignPKValue(entityMeta.getIdStrategy()));
        }
    }, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) ReturnPkType(org.sagacity.sqltoy.dialect.model.ReturnPkType) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy)

Aggregations

EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)141 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)36 PKStrategy (org.sagacity.sqltoy.config.model.PKStrategy)28 GenerateSavePKStrategy (org.sagacity.sqltoy.callback.GenerateSavePKStrategy)24 ArrayList (java.util.ArrayList)23 Serializable (java.io.Serializable)22 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)22 List (java.util.List)20 GenerateSqlHandler (org.sagacity.sqltoy.callback.GenerateSqlHandler)20 GenerateSqlHandler (org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)13 GenerateSavePKStrategy (org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy)12 IOException (java.io.IOException)11 SQLException (java.sql.SQLException)11 HashMap (java.util.HashMap)11 TableCascadeModel (org.sagacity.sqltoy.config.model.TableCascadeModel)10 ReturnPkType (org.sagacity.sqltoy.dialect.model.ReturnPkType)10 OneToManyModel (org.sagacity.sqltoy.config.model.OneToManyModel)8 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)8 Type (java.lang.reflect.Type)6 PreparedStatement (java.sql.PreparedStatement)6