public AnalysisJobBuilder setDatastore(final Datastore datastore) { _datastore = datastore; final DatastoreConnection datastoreConnection; if (datastore == null) { datastoreConnection = null; } else { datastoreConnection = datastore.openConnection(); } return setDatastoreConnection(datastoreConnection); }
@Override protected UsageAwareDatastoreConnection<DataContext> createDatastoreConnection() { final List<DataContext> dataContexts = new ArrayList<>(_datastores.size()); final List<Closeable> closeables = new ArrayList<>(_datastores.size()); for (final Datastore datastore : _datastores) { final DatastoreConnection con = datastore.openConnection(); final DataContext dc = con.getDataContext(); closeables.add(con); dataContexts.add(dc); } final Closeable[] closeablesArray = closeables.toArray(new Closeable[closeables.size()]); return new DatastoreConnectionImpl<>(DataContextFactory.createCompositeDataContext(dataContexts), this, closeablesArray); }
@Override public Table getPreviewTable(final Datastore datastore) { try (DatastoreConnection con = datastore.openConnection()) { return con.getSchemaNavigator().convertToTable(_schemaName, _tableName); } }
private void openConnection(final Datastore datastore) { if (_connection != null && _connection.getDatastore() == datastore) { return; } if (_connection != null) { _connection.close(); _connection = null; } if (datastore != null) { try { _connection = datastore.openConnection(); } catch (final Exception e) { logger.warn("Could not open connection to datastore: {}", datastore); } } }
@Initialize public void init() { datastoreConnection = datastore.openConnection(); resetCachedColumns(); cache.invalidateAll(); compileLookupQuery(); }
/** * Automatically maps all unmapped paths by looking them up in a datastore. * * @param schemaNavigator */ public void autoMap(final Datastore datastore) { setDatastore(datastore); try (DatastoreConnection con = datastore.openConnection()) { final SchemaNavigator schemaNavigator = con.getSchemaNavigator(); for (final Entry<String, Column> entry : _map.entrySet()) { if (entry.getValue() == null) { final String path = entry.getKey(); final Column column = schemaNavigator.convertToColumn(path); entry.setValue(column); } } } }
private Column[] getQueryConditionColumns() { if (queryConditionColumns == null) { if (isCarthesianProductMode()) { queryConditionColumns = new Column[0]; } else { try (DatastoreConnection con = datastore.openConnection()) { queryConditionColumns = con.getSchemaNavigator().convertToColumns(schemaName, tableName, conditionColumns); } } } return queryConditionColumns; }
private JButton createQueryButton(final Datastore datastore) { final JButton queryButton = WidgetFactory.createDefaultButton("Query", IconUtils.MODEL_QUERY); queryButton.setToolTipText("Query datastore"); queryButton.addActionListener(e -> { final String queryString; try (DatastoreConnection connection = datastore.openConnection()) { final List<Table> tables = connection.getSchemaNavigator().getDefaultSchema().getTables(); if (tables.size() > 0) { queryString = "SELECT *\nFROM " + tables.get(0).getQualifiedLabel(); } else { queryString = "SELECT *\nFROM ?"; } } final QueryWindow queryWindow = new QueryWindow(_windowContext, datastore, queryString); queryWindow.open(); }); return queryButton; }
@Override protected RowProcessingQueryOptimizer fetch() { final Datastore datastore = getAnalysisJob().getDatastore(); try (DatastoreConnection con = datastore.openConnection()) { final DataContext dataContext = con.getDataContext(); final Column[] columnArray = _physicalColumns.toArray(new Column[_physicalColumns.size()]); final Query baseQuery = dataContext.query().from(getTable()).select(columnArray).toQuery(); logger.debug("Base query for row processing: {}", baseQuery); // try to optimize return new RowProcessingQueryOptimizerImpl(datastore, getConsumersSorted(), baseQuery); } catch (final RuntimeException e) { logger.error("Failed to build query optimizer! {}", e.getMessage(), e); throw e; } } };
private DataSet getPreviewData(final String filename) { if (!isPreviewDataAvailable()) { logger.info("Not displaying preview table because isPreviewDataAvailable() returned false"); return null; } final D datastore = getPreviewDatastore(filename); try (DatastoreConnection con = datastore.openConnection()) { final DataContext dc = con.getDataContext(); final Table table = getPreviewTable(dc); List<Column> columns = table.getColumns(); if (columns.size() > getPreviewColumns()) { // include max 10 columns columns = columns.stream().limit(getPreviewColumns()).collect(Collectors.toList()); } final Query q = dc.query().from(table).select(columns).toQuery(); q.setMaxRows(7); return dc.executeQuery(q); } }
/** * Gets the output columns of the lookup query * * @param checkNames * whether to check/validate/adjust the names of these columns * @return */ private Column[] getQueryOutputColumns(final boolean checkNames) { if (queryOutputColumns == null) { try (DatastoreConnection con = datastore.openConnection()) { queryOutputColumns = con.getSchemaNavigator().convertToColumns(schemaName, tableName, outputColumns); } } else if (checkNames) { if (!isQueryOutputColumnsUpdated()) { queryOutputColumns = null; return getQueryOutputColumns(false); } } return queryOutputColumns; }
@Override public SchemaIdentifier getDefaultSchema(TenantIdentifier tenant, DatastoreIdentifier datastoreId) throws DatastoreConnectionException { final TenantContext tenantContext = _tenantContextFactory.getContext(tenant); final Datastore datastore = tenantContext.getDatastore(datastoreId); if (datastore == null) { return null; } try (final DatastoreConnection con = datastore.openConnection()) { final Schema schema = con.getDataContext().getDefaultSchema(); return new SchemaIdentifier(datastoreId, schema.getName()); } catch (Exception e) { logger.warn("Failed to open connection to datastore: " + datastoreId.getName(), e); throw new DatastoreConnectionException(e.getMessage()); } }
@Override public void actionPerformed(final ActionEvent event) { final String queryString = _queryTextArea.getText(); logger.debug("Query being parsed: {}", queryString); try (DatastoreConnection con = _datastore.openConnection()) { final DataContext dataContext = con.getDataContext(); final Query q = dataContext.parseQuery(queryString); logger.info("Parsed query: {}", q); final String limitString = _limitTextField.getText(); if (!StringUtils.isNullOrEmpty(limitString)) { final int limit = Integer.parseInt(limitString); q.setMaxRows(limit); } final DataSet dataSet = dataContext.executeQuery(q); _centerPanel.setVisible(true); _table.setModel(new DataSetTableModel(dataSet)); } catch (final MetaModelException e) { WidgetUtils.showErrorMessage("Failed to execute query", e.getMessage(), e); } } });
@Override protected void setValue(String value) { final Datastore datastore = _datastoreRef.get(); if (value == null && datastore != null) { try (DatastoreConnection con = datastore.openConnection()) { value = con.getSchemaNavigator().getDefaultSchema().getName(); } } if (getValue() == value) { return; } final Schema schema; if (value == null) { schema = null; } else if (datastore == null) { schema = new MutableSchema(value); } else { try (DatastoreConnection con = datastore.openConnection()) { schema = con.getSchemaNavigator().getSchemaByName(value); } } _comboBox.setEditable(true); _comboBox.setSelectedItem(schema); _comboBox.setEditable(false); }
@Override public SynonymCatalogConnection openConnection(final DataCleanerConfiguration configuration) { final Datastore datastore = configuration.getDatastoreCatalog().getDatastore(_datastoreName); if (datastore == null) { throw new NoSuchDatastoreException(_datastoreName); } final DatastoreConnection datastoreConnection = datastore.openConnection(); if (_loadIntoMemory) { final SimpleSynonymCatalog simpleSynonymCatalog = loadIntoMemory(datastoreConnection); // no need for the connection anymore datastoreConnection.close(); return simpleSynonymCatalog.openConnection(configuration); } return new DatastoreSynonymCatalogConnection(this, datastoreConnection); }
@Override public DictionaryConnection openConnection(final DataCleanerConfiguration configuration) { final Datastore datastore = configuration.getDatastoreCatalog().getDatastore(_datastoreName); if (datastore == null) { throw new NoSuchDatastoreException(_datastoreName); } final DatastoreConnection datastoreConnection = datastore.openConnection(); if (_loadIntoMemory) { final SimpleDictionary simpleDictionary = loadIntoMemory(datastoreConnection); // no need for the connection anymore datastoreConnection.close(); return simpleDictionary.openConnection(configuration); } return new DatastoreDictionaryConnection(this, datastoreConnection); }
/** * A main method that will display the results of a few example number * analyzers. Useful for tweaking the charts and UI. * * @param args */ public static void main(final String[] args) throws Exception { LookAndFeelManager.get().init(); final Injector injector = Guice.createInjector(new DCModuleImpl(VFSUtils.getFileSystemManager().resolveFile("."), null)); // run a small job final AnalysisJobBuilder ajb = injector.getInstance(AnalysisJobBuilder.class); final Datastore ds = injector.getInstance(DatastoreCatalog.class).getDatastore("orderdb"); final DatastoreConnection con = ds.openConnection(); final Table table = con.getSchemaNavigator().convertToTable("PUBLIC.CUSTOMERS"); ajb.setDatastore(ds); ajb.addSourceColumns(table.getNumberColumns()); ajb.addAnalyzer(NumberAnalyzer.class).addInputColumns(ajb.getSourceColumns()); final ResultWindow resultWindow = injector.getInstance(ResultWindow.class); resultWindow.setVisible(true); resultWindow.startAnalysis(); } }
public void setDatastore(final Datastore datastore) { final String previousValue = getValue(); _datastoreRef.set(datastore); if (datastore == null) { _comboBox.setModel(new DefaultComboBoxModel<>(new Schema[1])); } else { try (DatastoreConnection con = datastore.openConnection()) { Schema[] schemas = con.getSchemaNavigator().getSchemas(); schemas = CollectionUtils.array(new Schema[1], schemas); _comboBox.setModel(new DefaultComboBoxModel<>(schemas)); Schema newValue = null; if (previousValue != null) { newValue = con.getSchemaNavigator().getSchemaByName(previousValue); } if (newValue == null) { newValue = con.getSchemaNavigator().getDefaultSchema(); } _comboBox.setSelectedItem(newValue); } } }
@Override public List<TableIdentifier> getTables(final TenantIdentifier tenant, final SchemaIdentifier schemaId) { final TenantContext tenantContext = _tenantContextFactory.getContext(tenant); final Datastore datastore = tenantContext.getDatastore(schemaId.getDatastore()); if (datastore == null) { return null; } try (final DatastoreConnection con = datastore.openConnection()) { final Schema schema = con.getDataContext().getSchemaByName(schemaId.getName()); final String[] tableNames = schema.getTableNames(); final List<TableIdentifier> tableIdentifiers = CollectionUtils.map(tableNames, new Func<String, TableIdentifier>() { @Override public TableIdentifier eval(String tableName) { return new TableIdentifier(schemaId, tableName); } }); return tableIdentifiers; } }
public Column resolveForeignColumn(final DatastoreCatalog datastoreCatalog) { final Datastore datastore = datastoreCatalog.getDatastore(getForeignDatastoreName()); if (datastore == null) { return null; } try (DatastoreConnection connection = datastore.openConnection()) { final DataContext dataContext = connection.getDataContext(); final Schema schema = dataContext.getSchemaByName(getForeignSchemaName()); if (schema == null) { return null; } final Table table = schema.getTableByName(getForeignTableName()); if (table == null) { return null; } return table.getColumnByName(getForeignColumnName()); } } }