Search in sources :

Example 1 with GenerateSqlHandler

use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler 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 2 with GenerateSqlHandler

use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler 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)

Example 3 with GenerateSqlHandler

use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.

the class SqliteDialect 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 {
    // sqlite 只提供autoincrement 机制,即identity模式,所以sequence可以忽略
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    boolean isAssignPk = isAssignPKValue(entityMeta.getIdStrategy());
    String insertSql = DialectUtils.generateInsertSql(DBType.SQLITE, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPk, tableName);
    return DialectUtils.save(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPk, ReturnPkType.PREPARD_ID, insertSql, entity, new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
            return DialectUtils.generateInsertSql(DBType.SQLITE, 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) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy)

Example 4 with GenerateSqlHandler

use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.

the class PostgreSqlDialect method update.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#update(org.sagacity.sqltoy.
	 * SqlToyContext, java.io.Serializable, java.lang.String[], boolean,
	 * java.lang.Class[], java.util.HashMap, java.sql.Connection)
	 */
@Override
public Long update(SqlToyContext sqlToyContext, Serializable entity, String[] forceUpdateFields, boolean cascade, Class[] emptyCascadeClasses, 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 = "nextval('" + entityMeta.getSequence() + "')";
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
                sequence = "nextval(" + entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue() + ")";
            }
            return PostgreSqlDialectUtils.getSaveOrUpdateSql(DBType.POSTGRESQL, entityMeta, pkStrategy, sequence, forceUpdateFields, null);
        }
    }, emptyCascadeClasses, subTableForceUpdateProps, conn, tableName);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 5 with GenerateSqlHandler

use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.

the class PostgreSqlDialectUtils method save.

/**
 * @todo 保存单条对象记录
 * @param sqlToyContext
 * @param entity
 * @param conn
 * @param tableName
 * @return
 * @throws Exception
 */
public static Object save(SqlToyContext sqlToyContext, Serializable entity, Connection conn, String tableName) throws Exception {
    // 只支持sequence模式
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    PKStrategy pkStrategy = entityMeta.getIdStrategy();
    String sequence = "nextval('" + entityMeta.getSequence() + "')";
    if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
        pkStrategy = PKStrategy.SEQUENCE;
        sequence = "nextval(" + entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue() + ")";
    }
    boolean isAssignPK = isAssignPKValue(pkStrategy);
    String insertSql = DialectUtils.generateInsertSql(DBType.POSTGRESQL, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPK, tableName);
    return DialectUtils.save(sqlToyContext, entityMeta, pkStrategy, isAssignPK, ReturnPkType.GENERATED_KEYS, insertSql, entity, new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
            PKStrategy pkStrategy = entityMeta.getIdStrategy();
            String sequence = "nextval('" + entityMeta.getSequence() + "')";
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
                sequence = "nextval(" + entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue() + ")";
            }
            return DialectUtils.generateInsertSql(DBType.POSTGRESQL, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), 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) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy)

Aggregations

EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)13 GenerateSqlHandler (org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)13 GenerateSavePKStrategy (org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy)8 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)8 PKStrategy (org.sagacity.sqltoy.config.model.PKStrategy)6 ReturnPkType (org.sagacity.sqltoy.dialect.model.ReturnPkType)2