use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class DataSourceInvalidateHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
DBCExecutionContext context = getActiveExecutionContext(event, false);
if (context != null) {
invalidateDataSource(context.getDataSource());
} else {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor instanceof IDataSourceContainerProviderEx) {
// Try to set the same container.
// It should trigger connection instantiation if for some reason it was lost (SQLEditor specific?)
DBPDataSourceContainer dsContainer = ((IDataSourceContainerProviderEx) editor).getDataSourceContainer();
if (dsContainer != null) {
((IDataSourceContainerProviderEx) editor).setDataSourceContainer(null);
((IDataSourceContainerProviderEx) editor).setDataSourceContainer(dsContainer);
}
}
}
return null;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class DataImporterCSV method runImport.
@Override
public void runImport(@NotNull DBRProgressMonitor monitor, @NotNull DBPDataSource streamDataSource, @NotNull InputStream inputStream, @NotNull IDataTransferConsumer consumer) throws DBException {
IStreamDataImporterSite site = getSite();
StreamEntityMapping entityMapping = site.getSourceObject();
Map<String, Object> properties = site.getProcessorProperties();
HeaderPosition headerPosition = getHeaderPosition(properties);
boolean emptyStringNull = CommonUtils.getBoolean(properties.get(PROP_EMPTY_STRING_NULL), false);
String nullValueMark = CommonUtils.toString(properties.get(PROP_NULL_STRING));
DBCExecutionContext context = streamDataSource.getDefaultInstance().getDefaultContext(monitor, false);
try (DBCSession producerSession = context.openSession(monitor, DBCExecutionPurpose.UTIL, "Transfer stream data")) {
LocalStatement localStatement = new LocalStatement(producerSession, "SELECT * FROM Stream");
StreamTransferResultSet resultSet = new StreamTransferResultSet(producerSession, localStatement, entityMapping);
consumer.fetchStart(producerSession, resultSet, -1, -1);
applyTransformHints(resultSet, consumer, properties, PROP_TIMESTAMP_FORMAT, PROP_TIMESTAMP_ZONE);
try (Reader reader = openStreamReader(inputStream, properties)) {
try (CSVReader csvReader = openCSVReader(reader, properties)) {
int maxRows = site.getSettings().getMaxRows();
int targetAttrSize = entityMapping.getStreamColumns().size();
boolean headerRead = false;
for (int lineNum = 0; ; ) {
if (monitor.isCanceled()) {
break;
}
String[] line = csvReader.readNext();
if (line == null) {
break;
}
if (line.length == 0) {
continue;
}
if (headerPosition != HeaderPosition.none && !headerRead) {
// First line is a header
headerRead = true;
continue;
}
if (maxRows > 0 && lineNum >= maxRows) {
break;
}
if (line.length < targetAttrSize) {
// Stream row may be shorter than header
String[] newLine = new String[targetAttrSize];
System.arraycopy(line, 0, newLine, 0, line.length);
for (int i = line.length; i < targetAttrSize; i++) {
newLine[i] = null;
}
line = newLine;
}
if (emptyStringNull) {
for (int i = 0; i < line.length; i++) {
if ("".equals(line[i])) {
line[i] = null;
}
}
}
if (!CommonUtils.isEmpty(nullValueMark)) {
for (int i = 0; i < line.length; i++) {
if (nullValueMark.equals(line[i])) {
line[i] = null;
}
}
}
resultSet.setStreamRow(line);
consumer.fetchRow(producerSession, resultSet);
lineNum++;
if (lineNum % 1000 == 0) {
monitor.subTask(String.valueOf(lineNum) + " rows processed");
}
}
}
} catch (IOException e) {
throw new DBException("IO error reading CSV", e);
} finally {
try {
consumer.fetchEnd(producerSession, resultSet);
} finally {
consumer.close();
}
}
}
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class PropertySourceAbstract method collectProperties.
public boolean collectProperties() {
lazyValues.clear();
props.clear();
propValues.clear();
final Object editableValue = getEditableValue();
if (editableValue != null) {
IPropertyFilter filter;
if (isEnableFilters()) {
if (editableValue instanceof DBSObject) {
filter = new DataSourcePropertyFilter(((DBSObject) editableValue).getDataSource());
} else if (editableValue instanceof DBPContextProvider) {
DBCExecutionContext context = ((DBPContextProvider) editableValue).getExecutionContext();
filter = context == null ? new DataSourcePropertyFilter() : new DataSourcePropertyFilter(context.getDataSource());
} else {
filter = new DataSourcePropertyFilter();
}
} else {
filter = null;
}
List<ObjectPropertyDescriptor> annoProps = ObjectAttributeDescriptor.extractAnnotations(this, editableValue.getClass(), filter, locale);
for (final ObjectPropertyDescriptor desc : annoProps) {
if (desc.isPropertyVisible(editableValue, editableValue)) {
addProperty(desc);
}
}
if (editableValue instanceof DBPPropertySource) {
DBPPropertySource ownPropSource = (DBPPropertySource) editableValue;
DBPPropertyDescriptor[] ownProperties = ownPropSource.getProperties();
if (!ArrayUtils.isEmpty(ownProperties)) {
for (DBPPropertyDescriptor prop : ownProperties) {
props.add(prop);
propValues.put(prop.getId(), ownPropSource.getPropertyValue(null, prop.getId()));
}
}
}
}
return !props.isEmpty();
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class BaseSQLDialog method createSQLPanel.
protected Composite createSQLPanel(Composite parent) {
Composite panel = UIUtils.createPlaceholder(parent, 1);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
if (isLabelVisible()) {
UIUtils.createControlLabel(panel, SQLEditorMessages.pref_page_sql_format_label_SQLPreview);
}
// new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Composite editorPH = new Composite(panel, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.verticalIndent = 3;
gd.horizontalSpan = 1;
gd.minimumHeight = 100;
gd.minimumWidth = 100;
editorPH.setLayoutData(gd);
editorPH.setLayout(new FillLayout());
sqlViewer = new SQLEditorBase() {
@NotNull
@Override
public SQLDialect getSQLDialect() {
return BaseSQLDialog.this.getSQLDialect();
}
@Override
public DBCExecutionContext getExecutionContext() {
return BaseSQLDialog.this.getExecutionContext();
}
};
updateSQL();
sqlViewer.createPartControl(editorPH);
if (isWordWrap()) {
Object text = sqlViewer.getAdapter(Control.class);
if (text instanceof StyledText) {
((StyledText) text).setWordWrap(true);
}
}
sqlViewer.reloadSyntaxRules();
return panel;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class SQLTableManager method getTableDDL.
public DBEPersistAction[] getTableDDL(DBRProgressMonitor monitor, OBJECT_TYPE table, Map<String, Object> options) throws DBException {
List<DBEPersistAction> actions = new ArrayList<>();
final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
SQLObjectEditor<DBSEntityAttribute, OBJECT_TYPE> tcm = getObjectEditor(editorsRegistry, DBSEntityAttribute.class);
SQLObjectEditor<DBSEntityConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSEntityConstraint.class);
SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
DBCExecutionContext executionContext = DBUtils.getDefaultContext(table, true);
if (CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_ONLY_FOREIGN_KEYS)) {
if (fkm != null) {
// Create only foreign keys
try {
for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
continue;
}
DBEPersistAction[] cmdActions = fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options).getPersistActions(monitor, executionContext, options);
if (cmdActions != null) {
Collections.addAll(actions, cmdActions);
}
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
return actions.toArray(new DBEPersistAction[0]);
}
if (isIncludeDropInDDL()) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop table"));
for (DBEPersistAction delAction : new ObjectDeleteCommand(table, ModelMessages.model_jdbc_delete_object).getPersistActions(monitor, executionContext, options)) {
String script = delAction.getScript();
String delimiter = SQLUtils.getScriptLineDelimiter(SQLUtils.getDialectFromObject(table));
if (!script.endsWith(delimiter)) {
script += delimiter;
}
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), script));
}
}
StructCreateCommand command = makeCreateCommand(table, options);
if (tcm != null) {
// Aggregate nested column, constraint and index commands
for (DBSEntityAttribute column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
if (DBUtils.isHiddenObject(column) || DBUtils.isInheritedObject(column)) {
// Do not include hidden (pseudo?) and inherited columns in DDL
continue;
}
command.aggregateCommand(tcm.makeCreateCommand(column, options));
}
}
if (pkm != null) {
try {
for (DBSEntityConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
if (DBUtils.isHiddenObject(constraint) || DBUtils.isInheritedObject(constraint)) {
continue;
}
command.aggregateCommand(pkm.makeCreateCommand(constraint, options));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (fkm != null && !CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_SKIP_FOREIGN_KEYS)) {
try {
for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
continue;
}
command.aggregateCommand(fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (im != null && table instanceof DBSTable) {
try {
for (DBSTableIndex index : CommonUtils.safeCollection(((DBSTable) table).getIndexes(monitor))) {
if (!isIncludeIndexInDDL(monitor, index)) {
continue;
}
command.aggregateCommand(im.makeCreateCommand(index, options));
}
} catch (DBException e) {
// Ignore indexes
log.debug(e);
}
}
addExtraDDLCommands(monitor, table, options, command);
Collections.addAll(actions, command.getPersistActions(monitor, executionContext, options));
return actions.toArray(new DBEPersistAction[0]);
}
Aggregations