Search in sources :

Example 66 with EntityMeta

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

the class KingbaseDialect 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 Integer dbType, final String dialect, final String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    boolean isAssignPK = KingbaseDialectUtils.isAssignPKValue(entityMeta.getIdStrategy());
    String insertSql = DialectExtUtils.generateInsertSql(dbType, 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() {

        @Override
        public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
            return DialectExtUtils.generateInsertSql(dbType, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), KingbaseDialectUtils.isAssignPKValue(entityMeta.getIdStrategy()), null);
        }
    }, new GenerateSavePKStrategy() {

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

Example 67 with EntityMeta

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

the class OceanBaseDialect 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 Integer dbType, final String dialect, final String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    PKStrategy pkStrategy = entityMeta.getIdStrategy();
    String sequence = entityMeta.getSequence().concat(".nextval");
    if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
        pkStrategy = PKStrategy.SEQUENCE;
        sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
    }
    String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, pkStrategy, NVL_FUNCTION, sequence, OracleDialectUtils.isAssignPKValue(pkStrategy), tableName);
    return DialectUtils.save(sqlToyContext, entityMeta, pkStrategy, OracleDialectUtils.isAssignPKValue(pkStrategy), ReturnPkType.PREPARD_ID, insertSql, entity, new GenerateSqlHandler() {

        @Override
        public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
            PKStrategy pkStrategy = entityMeta.getIdStrategy();
            String sequence = entityMeta.getSequence().concat(".nextval");
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
                sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
            }
            return DialectExtUtils.generateInsertSql(dbType, entityMeta, pkStrategy, NVL_FUNCTION, sequence, OracleDialectUtils.isAssignPKValue(pkStrategy), null);
        }
    }, new GenerateSavePKStrategy() {

        @Override
        public SavePKStrategy generate(EntityMeta entityMeta) {
            PKStrategy pkStrategy = entityMeta.getIdStrategy();
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
            }
            return new SavePKStrategy(pkStrategy, OracleDialectUtils.isAssignPKValue(pkStrategy));
        }
    }, conn, dbType);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.callback.GenerateSqlHandler) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy)

Example 68 with EntityMeta

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

the class ClickHouseDialect method saveAll.

@Override
public Long saveAll(SqlToyContext sqlToyContext, List<?> entities, int batchSize, ReflectPropsHandler reflectPropsHandler, Connection conn, Integer dbType, String dialect, Boolean autoCommit, String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    // clickhouse 不支持sequence,支持identity自增模式
    String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), ClickHouseDialectUtils.isAssignPKValue(entityMeta.getIdStrategy()), tableName);
    return ClickHouseDialectUtils.saveAll(sqlToyContext, entityMeta, insertSql, entities, batchSize, reflectPropsHandler, conn, dbType, autoCommit);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta)

Example 69 with EntityMeta

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

the class ClickHouseDialect method load.

@Override
public Serializable load(SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, Integer dbType, String dialect, String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    // 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search, dialect);
    String loadSql = sqlToyConfig.getSql(dialect);
    return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn, dbType);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) Serializable(java.io.Serializable) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 70 with EntityMeta

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

the class DMDialect 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 Integer dbType, final String dialect, final String tableName) throws Exception {
    return DialectUtils.update(sqlToyContext, entity, NVL_FUNCTION, forceUpdateFields, cascade, (cascade == false) ? null : new GenerateSqlHandler() {

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

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