List of usage examples for org.springframework.jdbc.support GeneratedKeyHolder GeneratedKeyHolder
public GeneratedKeyHolder(List<Map<String, Object>> keyList)
From source file:net.paoding.rose.jade.statement.UpdateQuerier.java
private Object executeSingle(StatementRuntime runtime, Class<?> returnType) { Number result;/*w w w . j a va 2 s . c om*/ DataAccess dataAccess = dataAccessProvider.getDataAccess(// runtime.getMetaData(), runtime.getProperties()); if (returnGeneratedKeys) { ArrayList<Number> keys = new ArrayList<Number>(1); KeyHolder generatedKeyHolder = new GeneratedKeyHolder(keys); dataAccess.update(runtime.getSQL(), runtime.getArgs(), generatedKeyHolder); if (keys.size() > 0) { result = generatedKeyHolder.getKey(); } else { result = null; } } else { result = new Integer(dataAccess.update(runtime.getSQL(), runtime.getArgs(), null)); } // if (result == null || returnType == void.class) { return null; } if (returnType == result.getClass()) { return result; } // ? if (returnType == Integer.class) { return result.intValue(); } else if (returnType == Long.class) { return result.longValue(); } else if (returnType == Boolean.class) { return result.intValue() > 0 ? Boolean.TRUE : Boolean.FALSE; } else if (returnType == Double.class) { return result.doubleValue(); } else if (returnType == Float.class) { return result.floatValue(); } else if (Number.class.isAssignableFrom(returnType)) { return result; } else { throw new DataRetrievalFailureException("The generated key is not of a supported numeric type. " + "Unable to cast [" + result.getClass().getName() + "] to [" + Number.class.getName() + "]"); } }
From source file:com.gzj.tulip.jade.statement.UpdateQuerier.java
private Object executeSingle(StatementRuntime runtime) { Number result;//from www .j a va 2 s. c o m DataAccess dataAccess = dataAccessFactory.getDataAccess(// runtime.getMetaData(), runtime.getAttributes()); if (returnGeneratedKeys.shouldReturnGerneratedKeys(runtime)) { ArrayList<Map<String, Object>> keys = new ArrayList<Map<String, Object>>(1); KeyHolder generatedKeyHolder = new GeneratedKeyHolder(keys); dataAccess.update(runtime.getSQL(), runtime.getArgs(), generatedKeyHolder); if (keys.size() > 0) { result = generatedKeyHolder.getKey(); } else { result = null; } } else { result = new Integer(dataAccess.update(runtime.getSQL(), runtime.getArgs(), null)); } // if (result == null || returnType == void.class) { return null; } if (returnType == result.getClass()) { return result; } // ? if (returnType == Integer.class) { return result.intValue(); } else if (returnType == Long.class) { return result.longValue(); } else if (returnType == Boolean.class) { return result.intValue() > 0 ? Boolean.TRUE : Boolean.FALSE; } else if (returnType == Double.class) { return result.doubleValue(); } else if (returnType == Float.class) { return result.floatValue(); } else if (returnType == Number.class) { return result; } else if (returnType == String.class || returnType == CharSequence.class) { return String.valueOf(result); } else { throw new DataRetrievalFailureException( "The generated key is not of a supported numeric type: " + returnType.getName()); } }
From source file:com.sinosoft.one.data.jade.statement.UpdateQuerier.java
private Object executeSingle(StatementRuntime runtime, Class<?> returnType) { // DAOIdentity?1/20 if (returnType == Identity.class) { if (new Random().nextInt(20) == 1) { new IllegalArgumentException( "message by zhiliang.wang: change the deprecated Identity to @ReturnGeneratedKeys please: " + runtime.getMetaData()).printStackTrace(); }//from ww w .ja va 2 s .c om } Number result; DataAccess dataAccess = new DataAccessImpl(em); if (returnGeneratedKeys) { //ArrayList<Number> keys = new ArrayList<Number>(1); List<Map<String, Object>> keys = new ArrayList<Map<String, Object>>(); KeyHolder generatedKeyHolder = new GeneratedKeyHolder(keys); dataAccess.update(runtime.getSQL(), runtime.getArgs(), generatedKeyHolder); if (keys.size() > 0) { result = generatedKeyHolder.getKey(); } else { result = null; } } else { result = new Integer(dataAccess.update(runtime.getSQL(), runtime.getArgs(), null)); } // if (result == null || returnType == void.class) { return null; } if (returnType == result.getClass()) { return result; } // ? if (returnType == Integer.class) { return result.intValue(); } else if (returnType == Long.class) { return result.longValue(); } else if (returnType == Boolean.class) { return result.intValue() > 0 ? Boolean.TRUE : Boolean.FALSE; } else if (returnType == Double.class) { return result.doubleValue(); } else if (returnType == Float.class) { return result.floatValue(); } else if (returnType == Identity.class) { return new Identity((Number) result); } else if (Number.class.isAssignableFrom(returnType)) { return result; } else { throw new DataRetrievalFailureException("The generated key is not of a supported numeric type. " + "Unable to cast [" + result.getClass().getName() + "] to [" + Number.class.getName() + "]"); } }
From source file:edu.jhuapl.openessence.datasource.jdbc.entry.JdbcOeDataEntrySource.java
/** * Executes INSERT SQL Statment using Spring's JdbcTemplate. * * @param tableName table to insert values into * @param ignoreSpecialSql the flag to ignore specialSql definitions in the groovy def file. In general, set false * during add* and set true during update* * @param dimIds DimensionIds that we will insert data for //todo ?? why is this needed? * @param editDims editable DimensionIds that we will insert data for * @param values values that correspond to the editable DimensionIds. These values get written into the * database//from ww w. j ava2s . co m * @return Map of the primary keys and values for the inserted record -- only for the Parent Record - children return * null * @throws OeDataSourceAccessException if error occurs at database level * @throws OeDataSourceException if error occurs during processing */ private Map editableInsertQuery(String tableName, boolean ignoreSpecialSql, List<String> dimIds, Map<String, Dimension> editDims, Map<String, Object> values) throws OeDataSourceAccessException, OeDataSourceException { List<String> generatedKeys = new ArrayList<String>(); Set<String> tablePkIds; // insert on parent table if (tableName.equals(parentTableDetails.getTableName())) { // setup KeyHolder from pk_dimIds tablePkIds = parentTableDetails.getPks(); // setup autogen to sqlcol map Map<String, Object> superEditCopy = new LinkedHashMap<String, Object>(superEditMap); Set<String> superEditKeys = superEditCopy.keySet(); DualHashBidiMap bidimap = new DualHashBidiMap(); superEditKeys.retainAll(tablePkIds); for (Map.Entry<String, Object> e : superEditCopy.entrySet()) { e.setValue(((DimensionBean) e.getValue()).getSqlCol()); bidimap.put(e.getKey(), e.getValue()); } // setup KeyHolder from pk_dimIds generatedKeys.addAll(tablePkIds); // NOTE: jdbc driver clears this and puts in the autoincs it finds. Map<String, Object> generatedKeyMap = new HashMap<String, Object>(); for (String eachKey : generatedKeys) { generatedKeyMap.put(eachKey, null); } List<Map<String, Object>> keyMapList = new ArrayList<Map<String, Object>>(); keyMapList.add(generatedKeyMap); KeyHolder keyHolder = new GeneratedKeyHolder(keyMapList); jdbcTemplate.update(new MultiTableInsertPreparedStatementCreator(tableName, ignoreSpecialSql, dimIds, editDims, values), keyHolder); Map<String, Object> keyMap = keyHolder.getKeys(); // TODO: current implementation of getGeneratedKeys for PGSQL 8.4 returns ALL column/vals...we just want the pk's we know about // TODO: CHECK FOR WHAT HAPPENS WITH LOWER/UPPER CASE //http://archives.postgresql.org/pgsql-jdbc/2010-04/msg00061.php boolean isPostgreSql = isPostgreSqlDBMS(); if (isPostgreSql) { // postgres' implementation of keyholder lowercases the key column DbKeyValMap dbkvm = new DbKeyValMap(bidimap); Set<String> kyids = dbkvm.keySet(); for (String ky : kyids) { dbkvm.put(ky, keyMap.get(bidimap.get(ky))); } kyids.retainAll(tablePkIds); keyMap = dbkvm; } // -OR- // if table had no auto-gen keys but the INSERT suceedes, means the pks taken from the 'values' worked. // therefore, safe to use these as the "generated" PKs. retains the values that are designated "PK" dimensions // else if (keyMap == null || keyMap.size() == 0) { DbKeyValMap dbkvm = new DbKeyValMap(values); Set<String> kyids = dbkvm.keySet(); kyids.retainAll(tablePkIds); keyMap = dbkvm; } // make sure got *ALL* pkIds/values configured in the ds def. List<Map> allkeys = getAllGeneratedKeys(tableName, tablePkIds, new DbKeyValMap(keyMap)); return (allkeys.size() > 0 ? allkeys.get(0) : null); } else { // insert on child table. // don't need to know the returned PK ids & vals for children. just do typical INSERT jdbcTemplate.update(new MultiTableInsertPreparedStatementCreator(tableName, ignoreSpecialSql, dimIds, editDims, values)); return null; } }