Search in sources :

Example 41 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);
    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 42 with Row

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

the class LookupTableDataRouter method getLookupTable.

@SuppressWarnings("unchecked")
protected Map<String, Set<String>> getLookupTable(final Map<String, String> params, Router router, SimpleRouterContext routingContext) {
    final String CTX_CACHE_KEY = LOOKUP_TABLE_KEY + "." + params.get(PARAM_TABLE);
    Map<String, Set<String>> lookupMap = (Map<String, Set<String>>) routingContext.getContextCache().get(CTX_CACHE_KEY);
    if (lookupMap == null) {
        ISqlTemplate template = symmetricDialect.getPlatform().getSqlTemplate();
        final Map<String, Set<String>> fillMap = new HashMap<String, Set<String>>();
        template.query(String.format("select %s, %s from %s", params.get(PARAM_MAPPED_KEY_COLUMN), params.get(PARAM_EXTERNAL_ID_COLUMN), params.get(PARAM_TABLE)), new ISqlRowMapper<Object>() {

            public Object mapRow(Row rs) {
                String key = rs.getString(params.get(PARAM_MAPPED_KEY_COLUMN));
                String value = rs.getString(params.get(PARAM_EXTERNAL_ID_COLUMN));
                Set<String> ids = fillMap.get(key);
                if (ids == null) {
                    ids = new HashSet<String>();
                    fillMap.put(key, ids);
                }
                ids.add(value);
                return value;
            }
        });
        lookupMap = fillMap;
        routingContext.getContextCache().put(CTX_CACHE_KEY, lookupMap);
    }
    return lookupMap;
}
Also used : ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Row(org.jumpmind.db.sql.Row) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 43 with Row

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

the class ConfigurationService method getChannels.

public Map<String, Channel> getChannels(boolean refreshCache) {
    long channelCacheTimeoutInMs = parameterService.getLong(ParameterConstants.CACHE_TIMEOUT_CHANNEL_IN_MS, 60000);
    Map<String, Channel> channels = channelsCache;
    if (System.currentTimeMillis() - channelCacheTime >= channelCacheTimeoutInMs || channels == null || refreshCache) {
        synchronized (this) {
            channels = channelsCache;
            if (System.currentTimeMillis() - channelCacheTime >= channelCacheTimeoutInMs || channels == null || refreshCache) {
                channels = new HashMap<String, Channel>();
                List<Channel> list = sqlTemplate.query(getSql("selectChannelsSql"), new ISqlRowMapper<Channel>() {

                    public Channel mapRow(Row row) {
                        Channel channel = new Channel();
                        channel.setChannelId(row.getString("channel_id"));
                        channel.setProcessingOrder(row.getInt("processing_order"));
                        channel.setMaxBatchSize(row.getInt("max_batch_size"));
                        channel.setEnabled(row.getBoolean("enabled"));
                        channel.setMaxBatchToSend(row.getInt("max_batch_to_send"));
                        channel.setMaxDataToRoute(row.getInt("max_data_to_route"));
                        channel.setUseOldDataToRoute(row.getBoolean("use_old_data_to_route"));
                        channel.setUseRowDataToRoute(row.getBoolean("use_row_data_to_route"));
                        channel.setUsePkDataToRoute(row.getBoolean("use_pk_data_to_route"));
                        channel.setContainsBigLob(row.getBoolean("contains_big_lob"));
                        channel.setBatchAlgorithm(row.getString("batch_algorithm"));
                        channel.setExtractPeriodMillis(row.getLong("extract_period_millis"));
                        channel.setDataLoaderType(row.getString("data_loader_type"));
                        channel.setCreateTime(row.getDateTime("create_time"));
                        channel.setLastUpdateBy(row.getString("last_update_by"));
                        channel.setLastUpdateTime(row.getDateTime("last_update_time"));
                        channel.setReloadFlag(row.getBoolean("reload_flag"));
                        channel.setFileSyncFlag(row.getBoolean("file_sync_flag"));
                        channel.setQueue(row.getString("queue"));
                        channel.setMaxKBytesPerSecond(row.getBigDecimal("max_network_kbps"));
                        channel.setDataEventAction(NodeGroupLinkAction.fromCode(row.getString("data_event_action")));
                        return channel;
                    }
                });
                for (Channel channel : list) {
                    channels.put(channel.getChannelId(), channel);
                }
                channelsCache = channels;
                channelCacheTime = System.currentTimeMillis();
            }
        }
    }
    return channels;
}
Also used : NodeChannel(org.jumpmind.symmetric.model.NodeChannel) Channel(org.jumpmind.symmetric.model.Channel) Row(org.jumpmind.db.sql.Row)

Example 44 with Row

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

the class ConfigurationService method getNodeChannels.

public List<NodeChannel> getNodeChannels(final String nodeId, boolean refreshExtractMillis) {
    boolean loaded = false;
    long channelCacheTimeoutInMs = parameterService.getLong(ParameterConstants.CACHE_TIMEOUT_CHANNEL_IN_MS);
    List<NodeChannel> nodeChannels = nodeChannelCache != null ? nodeChannelCache.get(nodeId) : null;
    if (System.currentTimeMillis() - nodeChannelCacheTime >= channelCacheTimeoutInMs || nodeChannels == null) {
        synchronized (this) {
            if (System.currentTimeMillis() - nodeChannelCacheTime >= channelCacheTimeoutInMs || nodeChannelCache == null || nodeChannelCache.get(nodeId) == null || nodeChannels == null) {
                if (System.currentTimeMillis() - nodeChannelCacheTime >= channelCacheTimeoutInMs || nodeChannelCache == null) {
                    nodeChannelCache = new HashMap<String, List<NodeChannel>>();
                    nodeChannelCacheTime = System.currentTimeMillis();
                }
                if (nodeId != null) {
                    nodeChannels = sqlTemplate.query(getSql("selectNodeChannelsSql"), new NodeChannelMapper(nodeId), nodeId);
                    nodeChannelCache.put(nodeId, nodeChannels);
                    loaded = true;
                } else {
                    nodeChannels = new ArrayList<NodeChannel>(0);
                }
            }
        }
    }
    if (!loaded && refreshExtractMillis) {
        /*
             * need to read last extracted time from database regardless of
             * whether we used the cache or not. locate the nodes in the cache,
             * and update it.
             */
        final Map<String, NodeChannel> nodeChannelsMap = new HashMap<String, NodeChannel>();
        boolean usingExtractPeriod = false;
        for (NodeChannel nc : nodeChannels) {
            nodeChannelsMap.put(nc.getChannelId(), nc);
            usingExtractPeriod |= nc.getExtractPeriodMillis() > 0;
        }
        if (usingExtractPeriod) {
            sqlTemplate.query(getSql("selectNodeChannelControlLastExtractTimeSql"), new ISqlRowMapper<Object>() {

                public Object mapRow(Row row) {
                    String channelId = row.getString("channel_id");
                    Date extractTime = row.getDateTime("last_extract_time");
                    NodeChannel nodeChannel = nodeChannelsMap.get(channelId);
                    if (nodeChannel != null) {
                        nodeChannel.setLastExtractTime(extractTime);
                    }
                    return nodeChannelsMap;
                }

                ;
            }, nodeId);
        }
    }
    return nodeChannels;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) Row(org.jumpmind.db.sql.Row) NodeChannel(org.jumpmind.symmetric.model.NodeChannel)

Example 45 with Row

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

the class DataService method getCsvDataFor.

protected String getCsvDataFor(ISqlTransaction transaction, Trigger trigger, TriggerHistory triggerHistory, String whereClause, boolean pkOnly) {
    String data = null;
    String sql = null;
    try {
        if (pkOnly) {
            sql = symmetricDialect.createCsvPrimaryKeySql(trigger, triggerHistory, engine.getConfigurationService().getChannel(trigger.getChannelId()), whereClause);
        } else {
            sql = symmetricDialect.createCsvDataSql(trigger, triggerHistory, engine.getConfigurationService().getChannel(trigger.getChannelId()), whereClause);
        }
    } catch (NotImplementedException e) {
    }
    if (isNotBlank(sql)) {
        data = transaction.queryForObject(sql, String.class);
    } else {
        DatabaseInfo databaseInfo = platform.getDatabaseInfo();
        String quote = databaseInfo.getDelimiterToken() == null || !parameterService.is(ParameterConstants.DB_DELIMITED_IDENTIFIER_MODE) ? "" : databaseInfo.getDelimiterToken();
        sql = "select " + triggerHistory.getColumnNames() + " from " + Table.getFullyQualifiedTableName(triggerHistory.getSourceCatalogName(), triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), quote, databaseInfo.getCatalogSeparator(), databaseInfo.getSchemaSeparator()) + " t where " + whereClause;
        Row row = transaction.queryForRow(sql);
        if (row != null) {
            data = row.csvValue();
        }
    }
    if (data != null) {
        data = data.trim();
    }
    return data;
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) NotImplementedException(org.apache.commons.lang.NotImplementedException) Row(org.jumpmind.db.sql.Row)

Aggregations

Row (org.jumpmind.db.sql.Row)66 ArrayList (java.util.ArrayList)27 ISqlRowMapper (org.jumpmind.db.sql.ISqlRowMapper)23 Trigger (org.jumpmind.db.model.Trigger)19 JdbcSqlTemplate (org.jumpmind.db.sql.JdbcSqlTemplate)19 DmlStatement (org.jumpmind.db.sql.DmlStatement)13 Column (org.jumpmind.db.model.Column)11 Table (org.jumpmind.db.model.Table)9 Date (java.util.Date)8 HashMap (java.util.HashMap)7 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)7 Test (org.junit.Test)5 HashSet (java.util.HashSet)3 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)3 SqlException (org.jumpmind.db.sql.SqlException)3 NetworkedNode (org.jumpmind.symmetric.model.NetworkedNode)3 Node (org.jumpmind.symmetric.model.Node)3 Map (java.util.Map)2 NotImplementedException (org.apache.commons.lang.NotImplementedException)2 Database (org.jumpmind.db.model.Database)2