Search in sources :

Example 61 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class JdbcPersistenceManager 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) SqlException(org.jumpmind.db.sql.SqlException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 62 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class JdbcPersistenceManager method refresh.

@Override
public void refresh(Object object, String catalogName, String schemaName, String tableName) {
    try {
        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);
            }
        }
        DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT, table.getCatalog(), table.getSchema(), table.getName(), keys.toArray(new Column[keys.size()]), columns, null, null);
        String sql = statement.getSql();
        Object[] values = statement.getValueArray(objectValuesByColumnName);
        Row row = databasePlatform.getSqlTemplate().queryForRow(sql, values);
        if (row != null) {
            Set<String> propertyNames = objectToTableMapping.keySet();
            for (String propertyName : propertyNames) {
                Object value = row.get(objectToTableMapping.get(propertyName).getName());
                BeanUtils.copyProperty(object, propertyName, value);
            }
        }
    } catch (Exception e) {
        throw toRuntimeException(e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) SqlException(org.jumpmind.db.sql.SqlException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 63 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class DataGapRouteReader method prepareCursor.

protected ISqlReadCursor<Data> prepareCursor() {
    IParameterService parameterService = engine.getParameterService();
    int numberOfGapsToQualify = parameterService.getInt(ParameterConstants.ROUTING_MAX_GAPS_TO_QUALIFY_IN_SQL, 100);
    int maxGapsBeforeGreaterThanQuery = parameterService.getInt(ParameterConstants.ROUTING_DATA_READER_THRESHOLD_GAPS_TO_USE_GREATER_QUERY, 100);
    this.dataGaps = engine.getDataService().findDataGaps();
    if (this.dataGaps != null) {
        context.setDataGaps(new ArrayList<DataGap>(this.dataGaps));
    }
    boolean useGreaterThanDataId = false;
    if (maxGapsBeforeGreaterThanQuery > 0 && this.dataGaps.size() > maxGapsBeforeGreaterThanQuery) {
        useGreaterThanDataId = true;
    }
    String channelId = context.getChannel().getChannelId();
    String sql = null;
    Boolean lastSelectUsedGreaterThanQuery = lastSelectUsedGreaterThanQueryByEngineName.get(parameterService.getEngineName());
    if (lastSelectUsedGreaterThanQuery == null) {
        lastSelectUsedGreaterThanQuery = Boolean.FALSE;
    }
    if (useGreaterThanDataId) {
        sql = getSql("selectDataUsingStartDataId", context.getChannel().getChannel());
        if (!lastSelectUsedGreaterThanQuery) {
            log.info("Switching to select from the data table where data_id >= start gap because there were {} gaps found " + "which was more than the configured threshold of {}", dataGaps.size(), maxGapsBeforeGreaterThanQuery);
            lastSelectUsedGreaterThanQueryByEngineName.put(parameterService.getEngineName(), Boolean.TRUE);
        }
    } else {
        sql = qualifyUsingDataGaps(dataGaps, numberOfGapsToQualify, getSql("selectDataUsingGapsSql", context.getChannel().getChannel()));
        if (lastSelectUsedGreaterThanQuery) {
            log.info("Switching to select from the data table where data_id between gaps");
            lastSelectUsedGreaterThanQueryByEngineName.put(parameterService.getEngineName(), Boolean.FALSE);
        }
    }
    if (parameterService.is(ParameterConstants.ROUTING_DATA_READER_ORDER_BY_DATA_ID_ENABLED, true)) {
        sql = String.format("%s %s", sql, engine.getRouterService().getSql("orderByDataId"));
    }
    ISqlTemplate sqlTemplate = engine.getSymmetricDialect().getPlatform().getSqlTemplate();
    Object[] args = null;
    int[] types = null;
    int dataIdSqlType = engine.getSymmetricDialect().getSqlTypeForIds();
    if (useGreaterThanDataId) {
        args = new Object[] { channelId, dataGaps.get(0).getStartId() };
        types = new int[] { Types.VARCHAR, dataIdSqlType };
    } else {
        int numberOfArgs = 1 + 2 * (numberOfGapsToQualify < dataGaps.size() ? numberOfGapsToQualify : dataGaps.size());
        args = new Object[numberOfArgs];
        types = new int[numberOfArgs];
        args[0] = channelId;
        types[0] = Types.VARCHAR;
        for (int i = 0; i < numberOfGapsToQualify && i < dataGaps.size(); i++) {
            DataGap gap = dataGaps.get(i);
            args[i * 2 + 1] = gap.getStartId();
            types[i * 2 + 1] = dataIdSqlType;
            if ((i + 1) == numberOfGapsToQualify && (i + 1) < dataGaps.size()) {
                /*
                     * there were more gaps than we are going to use in the SQL.
                     * use the last gap as the end data id for the last range
                     */
                args[i * 2 + 2] = dataGaps.get(dataGaps.size() - 1).getEndId();
            } else {
                args[i * 2 + 2] = gap.getEndId();
            }
            types[i * 2 + 2] = dataIdSqlType;
        }
    }
    this.currentGap = dataGaps.remove(0);
    return sqlTemplate.queryForCursor(sql, new ISqlRowMapper<Data>() {

        public Data mapRow(Row row) {
            return engine.getDataService().mapData(row);
        }
    }, args, types);
}
Also used : Data(org.jumpmind.symmetric.model.Data) IParameterService(org.jumpmind.symmetric.service.IParameterService) DataGap(org.jumpmind.symmetric.model.DataGap) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) Row(org.jumpmind.db.sql.Row)

Example 64 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class JdbcPersistenceManagerTest method testInsert.

@Test
public void testInsert() {
    Date date = new Date();
    manager.insert(new A(1, date, "Hello"), null, null, "A");
    Row row = getRow(1);
    assertEquals(1, row.get("id"));
    assertEquals("Hello", row.get("note"));
    assertEquals(date, row.get("last_update_time"));
}
Also used : Row(org.jumpmind.db.sql.Row) Date(java.util.Date) Test(org.junit.Test)

Example 65 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class JdbcPersistenceManagerTest method testUpdate.

@Test
public void testUpdate() {
    Date date = new Date();
    A a = new A(1, date, "Hello");
    manager.insert(a, null, null, "A");
    Row row = getRow(1);
    assertEquals("Hello", row.get("note"));
    a.setNote("Goodbye");
    manager.update(a, null, null, "A");
    row = getRow(1);
    assertEquals("Goodbye", row.get("note"));
}
Also used : Row(org.jumpmind.db.sql.Row) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Row (org.jumpmind.db.sql.Row)78 ArrayList (java.util.ArrayList)31 ISqlRowMapper (org.jumpmind.db.sql.ISqlRowMapper)23 Trigger (org.jumpmind.db.model.Trigger)19 DmlStatement (org.jumpmind.db.sql.DmlStatement)19 JdbcSqlTemplate (org.jumpmind.db.sql.JdbcSqlTemplate)19 Column (org.jumpmind.db.model.Column)15 Table (org.jumpmind.db.model.Table)12 Date (java.util.Date)11 SqlException (org.jumpmind.db.sql.SqlException)9 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)3 IoException (org.jumpmind.exception.IoException)3 NetworkedNode (org.jumpmind.symmetric.model.NetworkedNode)3 Node (org.jumpmind.symmetric.model.Node)3