Search in sources :

Example 96 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class SqlPersistenceManager method excecuteDml.

protected int excecuteDml(DmlType type, Object object, String catalogName, String schemaName, String tableName) {
    Table table = findTable(catalogName, schemaName, tableName);
    LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
    LinkedHashMap<String, Object> objectValuesByColumnName = getObjectValuesByColumnName(object, objectToTableMapping);
    Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
    List<Column> keys = new ArrayList<Column>(1);
    for (Column column : columns) {
        if (column.isPrimaryKey()) {
            keys.add(column);
        }
    }
    boolean[] nullKeyValues = new boolean[keys.size()];
    int i = 0;
    for (Column column : keys) {
        nullKeyValues[i++] = objectValuesByColumnName.get(column.getName()) == null;
    }
    DmlStatement statement = databasePlatform.createDmlStatement(type, table.getCatalog(), table.getSchema(), table.getName(), keys.toArray(new Column[keys.size()]), columns, nullKeyValues, null);
    String sql = statement.getSql();
    Object[] values = statement.getValueArray(objectValuesByColumnName);
    int[] types = statement.getTypes();
    return databasePlatform.getSqlTemplate().update(sql, values, types);
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) Column(org.jumpmind.db.model.Column)

Example 97 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class SqlPersistenceManager method mapObjectToTable.

protected LinkedHashMap<String, Column> mapObjectToTable(Object object, Table table) {
    LinkedHashMap<String, Column> columnNames = new LinkedHashMap<String, Column>();
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(object);
    for (int i = 0; i < pds.length; i++) {
        String propName = pds[i].getName();
        Column column = table.getColumnWithName(camelCaseToUnderScores(propName));
        if (column != null) {
            columnNames.put(propName, column);
        }
    }
    return columnNames;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Column(org.jumpmind.db.model.Column) LinkedHashMap(java.util.LinkedHashMap)

Example 98 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class SqlPersistenceManager method map.

@Override
public <T> T map(Map<String, Object> row, Class<T> clazz, String catalogName, String schemaName, String tableName) {
    try {
        T object = clazz.newInstance();
        Table table = findTable(catalogName, schemaName, tableName);
        LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
        Set<String> propertyNames = objectToTableMapping.keySet();
        for (String propertyName : propertyNames) {
            Object value = row.get(objectToTableMapping.get(propertyName).getName());
            BeanUtils.copyProperty(object, propertyName, value);
        }
        return object;
    } catch (Exception e) {
        throw toRuntimeException(e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 99 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class SqlPersistenceManager method find.

@Override
public <T> List<T> find(Class<T> clazz, String catalogName, String schemaName, String tableName) {
    try {
        Table table = findTable(catalogName, schemaName, tableName);
        T object = clazz.newInstance();
        LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
        Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
        DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT_ALL, table.getCatalog(), table.getSchema(), table.getName(), null, columns, null, null);
        String sql = statement.getSql();
        List<Row> rows = databasePlatform.getSqlTemplate().query(sql);
        List<T> objects = new ArrayList<T>();
        for (Row row : rows) {
            object = clazz.newInstance();
            Set<String> propertyNames = objectToTableMapping.keySet();
            for (String propertyName : propertyNames) {
                Object value = row.get(objectToTableMapping.get(propertyName).getName());
                BeanUtils.copyProperty(object, propertyName, value);
            }
            objects.add(object);
        }
        return objects;
    } catch (Exception e) {
        throw toRuntimeException(e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Column(org.jumpmind.db.model.Column)

Example 100 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class DbFill method getDependentColumnValues.

private Map<Column, Object> getDependentColumnValues(Table table) {
    Map<Column, Object> columnValues = new HashMap<Column, Object>();
    for (Column column : table.getColumns()) {
        String key = table.getName() + "." + column.getName();
        List<Object> commonValue = commonDependencyValues.get(key);
        if (commonValue != null && commonValue.size() != 0) {
            columnValues.put(column, commonValue.get(0));
        }
    }
    return columnValues;
}
Also used : HashMap(java.util.HashMap) Column(org.jumpmind.db.model.Column)

Aggregations

Column (org.jumpmind.db.model.Column)179 Table (org.jumpmind.db.model.Table)78 ArrayList (java.util.ArrayList)34 IndexColumn (org.jumpmind.db.model.IndexColumn)23 PlatformColumn (org.jumpmind.db.model.PlatformColumn)21 Test (org.junit.Test)16 Row (org.jumpmind.db.sql.Row)15 LinkedHashMap (java.util.LinkedHashMap)12 ResultSet (java.sql.ResultSet)11 DmlStatement (org.jumpmind.db.sql.DmlStatement)10 SqlException (org.jumpmind.db.sql.SqlException)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 IIndex (org.jumpmind.db.model.IIndex)9 HashMap (java.util.HashMap)8 ForeignKey (org.jumpmind.db.model.ForeignKey)8 CsvData (org.jumpmind.symmetric.io.data.CsvData)8 PreparedStatement (java.sql.PreparedStatement)7 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 Reference (org.jumpmind.db.model.Reference)6