Search in sources :

Example 71 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 ISqlRowMapper<NodeChannel>() {

                        public NodeChannel mapRow(Row row) {
                            NodeChannel nodeChannel = new NodeChannel();
                            nodeChannel.setChannelId(row.getString("channel_id"));
                            nodeChannel.setNodeId(nodeId);
                            nodeChannel.setIgnoreEnabled(row.getBoolean("ignore_enabled"));
                            nodeChannel.setSuspendEnabled(row.getBoolean("suspend_enabled"));
                            nodeChannel.setProcessingOrder(row.getInt("processing_order"));
                            nodeChannel.setMaxBatchSize(row.getInt("max_batch_size"));
                            nodeChannel.setEnabled(row.getBoolean("enabled"));
                            nodeChannel.setMaxBatchToSend(row.getInt("max_batch_to_send"));
                            nodeChannel.setMaxDataToRoute(row.getInt("max_data_to_route"));
                            nodeChannel.setUseOldDataToRoute(row.getBoolean("use_old_data_to_route"));
                            nodeChannel.setUseRowDataToRoute(row.getBoolean("use_row_data_to_route"));
                            nodeChannel.setUsePkDataToRoute(row.getBoolean("use_pk_data_to_route"));
                            nodeChannel.setContainsBigLob(row.getBoolean("contains_big_lob"));
                            nodeChannel.setBatchAlgorithm(row.getString("batch_algorithm"));
                            nodeChannel.setLastExtractTime(row.getDateTime("last_extract_time"));
                            nodeChannel.setExtractPeriodMillis(row.getLong("extract_period_millis"));
                            nodeChannel.setDataLoaderType(row.getString("data_loader_type"));
                            nodeChannel.setCreateTime(row.getDateTime("create_time"));
                            nodeChannel.setLastUpdateBy(row.getString("last_update_by"));
                            nodeChannel.setLastUpdateTime(row.getDateTime("last_update_time"));
                            nodeChannel.setFileSyncFlag(row.getBoolean("file_sync_flag"));
                            nodeChannel.setReloadFlag(row.getBoolean("reload_flag"));
                            return nodeChannel;
                        }
                    }, 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>();
        for (NodeChannel nc : nodeChannels) {
            nodeChannelsMap.put(nc.getChannelId(), nc);
        }
        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) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) Row(org.jumpmind.db.sql.Row) NodeChannel(org.jumpmind.symmetric.model.NodeChannel) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper)

Example 72 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 73 with Row

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

the class SqliteDdlReader method readTable.

public Table readTable(String catalog, String schema, String tableName) {
    Table table = null;
    List<Column> columns = platform.getSqlTemplate().query("pragma table_info(" + tableName + ")", COLUMN_MAPPER);
    checkForAutoIncrementColumn(columns, tableName);
    if (columns != null && columns.size() > 0) {
        table = new Table(tableName);
        for (Column column : columns) {
            table.addColumn(column);
        }
        List<IIndex> indexes = platform.getSqlTemplate().query("pragma index_list(" + tableName + ")", INDEX_MAPPER);
        for (IIndex index : indexes) {
            List<IndexColumn> indexColumns = platform.getSqlTemplate().query("pragma index_info(" + index.getName() + ")", INDEX_COLUMN_MAPPER);
            for (IndexColumn indexColumn : indexColumns) {
                /* Ignore auto index columns */
                if (!indexColumn.getName().startsWith("sqlite_autoindex_")) {
                    index.addColumn(indexColumn);
                    indexColumn.setColumn(table.getColumnWithName(indexColumn.getName()));
                }
            }
            if (!(index.hasAllPrimaryKeys() && index.getName().toLowerCase().contains("autoindex"))) {
                table.addIndex(index);
            }
        }
        Map<Integer, ForeignKey> keys = new HashMap<Integer, ForeignKey>();
        List<Row> rows = platform.getSqlTemplate().query("pragma foreign_key_list(" + tableName + ")", new RowMapper());
        for (Row row : rows) {
            Integer id = row.getInt("id");
            ForeignKey fk = keys.get(id);
            if (fk == null) {
                fk = new ForeignKey();
                fk.setForeignTable(new Table(row.getString("table")));
                keys.put(id, fk);
                table.addForeignKey(fk);
            }
            fk.addReference(new Reference(new Column(row.getString("from")), new Column(row.getString("to"))));
        }
    }
    return table;
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Table(org.jumpmind.db.model.Table) HashMap(java.util.HashMap) Reference(org.jumpmind.db.model.Reference) ForeignKey(org.jumpmind.db.model.ForeignKey) IndexColumn(org.jumpmind.db.model.IndexColumn) IndexColumn(org.jumpmind.db.model.IndexColumn) Column(org.jumpmind.db.model.Column) Row(org.jumpmind.db.sql.Row) RowMapper(org.jumpmind.db.sql.mapper.RowMapper)

Example 74 with Row

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

the class DbFill method insertRandomRecord.

/**
 * Select a random row from the table and update all columns except for primary and foreign keys.
 *
 * @param sqlTemplate
 * @param table
 */
private void insertRandomRecord(Table table) {
    DmlStatement insertStatement = createInsertDmlStatement(table);
    Row row = createRandomInsertValues(insertStatement, table);
    try {
        platform.getSqlTemplate().update(insertStatement.getSql(), insertStatement.getValueArray(row.toArray(table.getColumnNames()), row.toArray(table.getPrimaryKeyColumnNames())));
        if (verbose) {
            log.info("Successful update in " + table.getName());
        }
    } catch (SqlException ex) {
        log.info("Failed to process {} with values of {}", insertStatement.getSql(), ArrayUtils.toString(row.toArray(table.getColumnNames())));
        if (continueOnError) {
            if (debug) {
                log.info("", ex);
            }
        } else {
            throw ex;
        }
    }
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 75 with Row

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

the class DbFill method deleteRandomRecord.

/**
 * Delete a random row in the given table or delete all rows matching selectColumns
 * in the given table.
 *
 * @param table Table to delete from.
 * @param selectColumns If provided, the rows that match this criteria are deleted.
 */
private void deleteRandomRecord(Table table) {
    DmlStatement deleteStatement = createDeleteDmlStatement(table);
    Row row = selectRandomRow(table);
    try {
        platform.getSqlTemplate().update(deleteStatement.getSql(), row.toArray(table.getColumnNames()));
        if (verbose) {
            log.info("Successful update in " + table.getName());
        }
    } catch (SqlException ex) {
        log.info("Failed to process {} with values of {}", deleteStatement.getSql(), ArrayUtils.toString(row.toArray(table.getColumnNames())));
        if (continueOnError) {
            if (debug) {
                log.info("", ex);
            }
        } else {
            throw ex;
        }
    }
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

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