Package org.springmodules.datamap.jdbc.sqlmap

Examples of org.springmodules.datamap.jdbc.sqlmap.PersistentObject


        }
        return "set" + propertyName.toString();
    }

    public static PersistentObject getPersistenceMetaData(Class clazz, final DataSource dataSource, Map pluralExceptions) {
        final PersistentObject newPo = new PersistentObject();
        String baseName = clazz.getName();
        baseName = baseName.substring(baseName.lastIndexOf(".") + 1);
        String tableName;
        if (pluralExceptions.containsKey(baseName)) {
            tableName = underscoreName((String)pluralExceptions.get(baseName));
        }
        else {
            tableName = underscoreName(baseName + "s");
        }
        newPo.setBaseName(baseName);
        newPo.setTableName(tableName);
        final String sqlTableName = tableName;
        final Map newMetaData = new HashMap(10);
        Field[] f = clazz.getDeclaredFields();
        for (int i = 0; i < f.length; i++) {
            PersistentField pf = new PersistentField();
            pf.setFieldName(f[i].getName());
            pf.setColumnName(ActiveMapperUtils.underscoreName(f[i].getName()));
            pf.setJavaType(f[i].getType());
            if ("id".equals(f[i].getName())) {
                if (!pf.isIdField())
                    pf.setIdField(true);
            }
            newMetaData.put(pf.getColumnName(), pf);
        }

        try {
            JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() {
                public Object processMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
                    newPo.setUsingLowerCaseIdentifiers(databaseMetaData.storesLowerCaseIdentifiers());
                    newPo.setDatabaseProductName(databaseMetaData.getDatabaseProductName());
                    if ("PostgreSQL".equals(newPo.getDatabaseProductName())) {
                        newPo.setUsingGeneratedKeysStrategy(false);
                        newPo.setIncrementer(new PostgreSQLSequenceMaxValueIncrementer(dataSource, newPo.getBaseName() + "_seq"));
                    }
                    else if ("HSQL Database Engine".equals(newPo.getDatabaseProductName())) {
                        newPo.setUsingGeneratedKeysStrategy(false);
                        newPo.setIncrementer(new HsqlMaxValueIncrementer(dataSource, newPo.getBaseName() + "_seq", "value"));
                    }
                    else if ("Oracle".equals(newPo.getDatabaseProductName())) {
                        newPo.setUsingGeneratedKeysStrategy(false);
                        newPo.setIncrementer(new OracleSequenceMaxValueIncrementer(dataSource, newPo.getBaseName() + "_seq"));
                    }
                    else {
                        newPo.setUsingGeneratedKeysStrategy(true);
                    }
                    String metaDataTableName;
                    if (newPo.isUsingLowerCaseIdentifiers())
                        metaDataTableName = sqlTableName.toLowerCase();
                    else
                        metaDataTableName = sqlTableName.toUpperCase();
                    ResultSet pkrs = databaseMetaData.getPrimaryKeys(null, null, metaDataTableName);
                    Set primaryKeys = new HashSet();
                    while (pkrs.next()) {
                        primaryKeys.add(pkrs.getString(4));
                    }
                    pkrs.close();
                    newPo.setPrimaryKeyColumns(primaryKeys);
                    int unmappedKey = primaryKeys.size();
                    ResultSet crs = databaseMetaData.getColumns(null, null, metaDataTableName, null);
                    while (crs.next()) {
                        PersistentField pf = (PersistentField)newMetaData.get(crs.getString(4).toLowerCase());
                        if (pf != null) {
                            pf.setSqlType(crs.getInt(5));
                            if (primaryKeys.contains(pf.getColumnName())) {
                                pf.setIdField(true);
                                unmappedKey--;
                            }
                        }
                    }
                    crs.close();
                    if (primaryKeys.size() > 1 || unmappedKey > 0) {
                        newPo.setDependentObject(true);
                    }
                    return null;
                }
            });
        } catch (MetaDataAccessException e) {
            throw new DataAccessResourceFailureException("Error retreiving metadata", e);
        }
        newPo.setPersistentFields(newMetaData);
        return newPo;
    }
View Full Code Here

TOP

Related Classes of org.springmodules.datamap.jdbc.sqlmap.PersistentObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.