public static boolean isOwnTransaction(WbConnection dbConnection) {
   if (dbConnection == null) return false;
   if (dbConnection.getAutoCommit()) return false;
   return (dbConnection.getProfile().getUseSeparateConnectionPerTab()
       || getAlwaysUseSeparateConnForDbExpWindow());
 }
Exemplo n.º 2
0
  protected void retrieveCurrentTrigger() {
    if (this.dbConnection == null || this.reader == null) return;
    int row = this.triggerList.getSelectedRow();
    if (!WbSwingUtilities.isConnectionIdle(this, this.dbConnection)) return;

    if (row < 0) return;

    final String triggerName =
        this.triggerList.getValueAsString(row, TriggerReader.COLUMN_IDX_TABLE_TRIGGERLIST_TRG_NAME);
    final String tableName =
        this.triggerList.getValueAsString(
            row, TriggerReader.COLUMN_IDX_TABLE_TRIGGERLIST_TRG_TABLE);
    final String comment =
        this.triggerList.getValueAsString(
            row, TriggerReader.COLUMN_IDX_TABLE_TRIGGERLIST_TRG_COMMENT);

    Container parent = this.getParent();
    WbSwingUtilities.showWaitCursor(parent);

    try {
      if (dbConnection.getProfile().getUseSeparateConnectionPerTab()) {
        levelChanger.changeIsolationLevel(dbConnection);
      }
      dbConnection.setBusy(true);

      try {
        TableIdentifier tbl = null;
        if (tableName != null) {
          tbl = new TableIdentifier(tableName, dbConnection);
          if (tbl.getCatalog() == null) tbl.setCatalog(currentCatalog);
          if (tbl.getSchema() == null) tbl.setSchema(currentSchema);
        }

        DropType dropType =
            DbExplorerSettings.getDropTypeToGenerate(TriggerDefinition.TRIGGER_TYPE_NAME);

        String sql =
            reader.getTriggerSource(currentCatalog, currentSchema, triggerName, tbl, comment, true);
        Object obj = triggerList.getUserObject(row);

        boolean isReplace = SqlUtil.isReplaceDDL(sql, dbConnection, dropType);

        if (dropType != DropType.none
            && obj instanceof TriggerDefinition
            && sql != null
            && !isReplace) {
          TriggerDefinition trg = (TriggerDefinition) obj;
          String drop = trg.getDropStatement(dbConnection, dropType == DropType.cascaded);
          if (StringUtil.isNonBlank(drop)) {
            sql = drop + getDelimiterForDrop() + "\n\n" + sql;
          }
        }

        final String sourceSql = sql == null ? "" : sql;
        WbSwingUtilities.invoke(
            () -> {
              source.setText(sourceSql, triggerName, TRG_TYPE_NAME);
            });

      } catch (Throwable ex) {
        LogMgr.logError(
            "TriggerListPanel.retrieveTriggerSource() thread", "Could not read trigger source", ex);
        source.setPlainText(ExceptionUtil.getDisplay(ex));
      }
    } finally {
      WbSwingUtilities.showDefaultCursor(parent);
      levelChanger.restoreIsolationLevel(dbConnection);
      dbConnection.setBusy(false);
    }

    if (this.triggerList.getSelectedRowCount() == 1) {
      EventQueue.invokeLater(
          () -> {
            source.setCaretPosition(0, false);
            if (DbExplorerSettings.getSelectSourcePanelAfterRetrieve()) {
              source.requestFocusInWindow();
            }
          });
    }
  }