private void initActiveSheet() {
    if (dataFile == null) return;

    if (sheetIndex > -1) {
      dataSheet = dataFile.getSheetAt(sheetIndex);
      if (dataSheet == null) {
        throw new IndexOutOfBoundsException(
            "Sheet with index "
                + sheetIndex
                + " does not exist in file: "
                + inputFile.getFullPath());
      }
    } else if (sheetName != null) {
      dataSheet = dataFile.getSheet(sheetName);
      if (dataSheet == null) {
        throw new IllegalArgumentException(
            "Sheet with name " + sheetName + " does not exist in file: " + inputFile.getFullPath());
      }
    } else {
      int index = dataFile.getActiveSheetIndex();
      dataSheet = dataFile.getSheetAt(index);
    }
    headerColumns = null;
    int numMergedRegions = dataSheet.getNumMergedRegions();
    mergedRegions = new ArrayList<>(numMergedRegions);
    for (int i = 0; i < numMergedRegions; i++) {
      mergedRegions.add(dataSheet.getMergedRegion(i));
    }
  }
  @Override
  public List<ConnectionProfile> readProfiles(String filename) {
    LogMgr.logInfo(
        "IniProfileStorage.readProfiles()", "Loading connection profiles from " + filename);
    WbProperties props = new WbProperties(1);
    BufferedReader reader = null;
    List<ConnectionProfile> profiles = new ArrayList<>(25);

    WbFile inifile = new WbFile(filename);

    try {
      File fileDir = inifile.getCanonicalFile().getParentFile();
      reader = new BufferedReader(new FileReader(filename));
      props.loadFromReader(reader);
      Set<String> keys = getProfileKeys(props);
      for (String key : keys) {
        ConnectionProfile profile = readProfile(fileDir, key, props);
        if (profile != null) {
          profiles.add(profile);
        }
      }
    } catch (Exception ex) {
      LogMgr.logError(
          "IniProfileStorage.readProfiles()", "Could not read profiles from: " + filename, ex);
    } finally {
      FileUtil.closeQuietely(reader);
    }
    return profiles;
  }
 public LoadWorkspaceFileAction(MainWindow aClient, WbFile file) {
   super();
   client = aClient;
   workspace = file;
   this.setMenuText(workspace.getFileName());
   this.setTooltip(workspace.getFullPath());
   this.setIcon(null);
 }
  @Test
  public void testMapping() {
    TestUtil util = new TestUtil("PkMappingTest");
    WbFile f = new WbFile(util.getBaseDir(), "mapping_test.properties");
    PkMapping map = new PkMapping(f.getFullPath());
    TableIdentifier tbl = new TableIdentifier("PERSON");
    map.addMapping(tbl, "id");

    TableIdentifier tbl2 = new TableIdentifier("person");
    List<String> col = map.getPKColumns(tbl2);
    assertEquals(1, col.size());
    assertEquals("id", col.get(0));
  }
 @Override
 public void executeAction(ActionEvent e) {
   boolean canLoad = client.saveWorkspace(true);
   if (canLoad) {
     this.client.loadWorkspace(workspace.getFullPath(), true);
   }
 }
  @Override
  public void writeFormatFile(DataExporter exporter, RowDataConverter converter) {
    WbFile baseFile = new WbFile(exporter.getFullOutputFilename());
    String dir = baseFile.getParent();

    String tableName = exporter.getTableNameToUse();
    WbFile ctl = new WbFile(dir, "load_" + tableName + ".sql");
    PrintWriter out = null;
    try {
      out = new PrintWriter(new FileWriter(ctl));
      out.print("load data infile '");
      out.print(baseFile.getFullPath());
      out.print("'\n");
      out.print("  into table ");
      out.println(tableName);

      String encoding = exporter.getEncoding();
      if (encoding != null) {
        out.print("  character set " + encoding + "\n");
      }
      String delim =
          StringUtil.escapeText(exporter.getTextDelimiter(), CharacterRange.RANGE_CONTROL);
      out.print("  columns\n");
      out.print("    terminated by '" + delim + "'\n");
      String quote = exporter.getTextQuoteChar();

      if (quote != null) {
        out.print("    ");
        if (!exporter.getQuoteAlways()) {
          out.print("optionally ");
        }
        out.print("enclosed by '" + quote + "'\n");
      }

      if (exporter.getExportHeaders()) {
        out.print("  ignore 1 lines\n");
      }
    } catch (Exception e) {
      LogMgr.logError(
          "PostgresCopyStatementWriter.writeFormatFile()", "Could not write format file", e);
    } finally {
      FileUtil.closeQuietely(out);
    }
  }
 public ExcelReader(File excelFile, int sheetNumber, String name) {
   inputFile = new WbFile(excelFile);
   sheetIndex = sheetNumber > -1 ? sheetNumber : -1;
   if (sheetIndex < 0 && StringUtil.isNonBlank(name)) {
     sheetName = name.trim();
   } else {
     sheetName = null;
   }
   useXLSX = inputFile.getExtension().equalsIgnoreCase("xlsx");
 }
  @Override
  public List<String> getSheets() {
    List<String> names = new ArrayList<>();

    if (dataFile == null) {
      try {
        load();
      } catch (Exception io) {
        LogMgr.logError(
            "ExcelReader.getSheets()", "Could not load Excel file: " + inputFile.getFullPath(), io);
        return names;
      }
    }

    int count = dataFile.getNumberOfSheets();
    for (int i = 0; i < count; i++) {
      names.add(dataFile.getSheetName(i));
    }
    return names;
  }
  @Test
  public void testWriteFormatFile() throws Exception {
    TestUtil util = getTestUtil();
    final WbFile export = new WbFile(util.getBaseDir(), "export.txt");
    DataExporter exporter =
        new DataExporter(null) {

          @Override
          public String getFullOutputFilename() {
            return export.getFullPath();
          }

          @Override
          public boolean getExportHeaders() {
            return true;
          }

          @Override
          public String getTextDelimiter() {
            return "\t";
          }

          @Override
          public String getTextQuoteChar() {
            return "\"";
          }

          @Override
          public String getEncoding() {
            return "UTF-8";
          }

          @Override
          public String getTableNameToUse() {
            return "person";
          }
        };

    ColumnIdentifier id = new ColumnIdentifier("id", Types.INTEGER, true);
    ColumnIdentifier firstname = new ColumnIdentifier("firstname", Types.VARCHAR);
    ColumnIdentifier lastname = new ColumnIdentifier("lastname", Types.VARCHAR);
    final TableIdentifier table = new TableIdentifier("person");

    final ResultInfo info = new ResultInfo(new ColumnIdentifier[] {id, firstname, lastname});
    info.setUpdateTable(table);

    RowDataConverter converter =
        new RowDataConverter() {

          @Override
          public ResultInfo getResultInfo() {
            return info;
          }

          @Override
          public StringBuilder convertRowData(RowData row, long rowIndex) {
            return new StringBuilder();
          }

          @Override
          public StringBuilder getStart() {
            return null;
          }

          @Override
          public StringBuilder getEnd(long totalRows) {
            return null;
          }
        };

    try {
      PostgresCopyStatementWriter writer = new PostgresCopyStatementWriter();
      writer.writeFormatFile(exporter, converter);
      WbFile formatFile = new WbFile(util.getBaseDir(), "import_export.sql");
      assertTrue(formatFile.exists());

      List<String> contents = StringUtil.readLines(formatFile);
      assertNotNull(contents);
      assertEquals(3, contents.size());
      assertEquals("copy person (id, firstname, lastname)", contents.get(0).trim());
      assertEquals("from '" + export.getFullPath() + "'", contents.get(1).trim());
      String expected =
          "with (format csv, header true, quote '\"', delimiter '\t', encoding 'UTF-8', null '');";
      assertEquals(expected, contents.get(2).trim());

    } finally {
      util.emptyBaseDirectory();
    }
  }
  @Test
  public void testWriteFormatFile() throws Exception {
    TestUtil util = getTestUtil();
    final WbFile export = new WbFile(util.getBaseDir(), "export.txt");
    DataExporter exporter =
        new DataExporter(null) {

          @Override
          public String getFullOutputFilename() {
            return export.getFullPath();
          }

          @Override
          public boolean getExportHeaders() {
            return true;
          }

          @Override
          public String getTextDelimiter() {
            return "\t";
          }

          @Override
          public String getTextQuoteChar() {
            return "\"";
          }
        };

    ColumnIdentifier id = new ColumnIdentifier("id", Types.INTEGER, true);
    ColumnIdentifier firstname = new ColumnIdentifier("firstname", Types.VARCHAR);
    ColumnIdentifier lastname = new ColumnIdentifier("lastname", Types.VARCHAR);
    final TableIdentifier table = new TableIdentifier("person");

    final ResultInfo info = new ResultInfo(new ColumnIdentifier[] {id, firstname, lastname});
    info.setUpdateTable(table);

    RowDataConverter converter =
        new RowDataConverter() {

          @Override
          public ResultInfo getResultInfo() {
            return info;
          }

          @Override
          public StringBuilder convertRowData(RowData row, long rowIndex) {
            return new StringBuilder();
          }

          @Override
          public StringBuilder getStart() {
            return null;
          }

          @Override
          public StringBuilder getEnd(long totalRows) {
            return null;
          }
        };

    try {
      SqlServerFormatFileWriter writer = new SqlServerFormatFileWriter();
      writer.writeFormatFile(exporter, converter);
      WbFile formatFile = new WbFile(util.getBaseDir(), "export.fmt");
      assertTrue(formatFile.exists());

      List<String> contents = StringUtil.readLines(formatFile);
      assertEquals("7.0", contents.get(0));
      assertEquals("3", contents.get(1));
      assertEquals("1    SQLCHAR 0  0 \"\\t\"   1    id", contents.get(2).trim());
      assertEquals("2    SQLCHAR 0  0 \"\\t\"   2    firstname", contents.get(3).trim());
      assertEquals("3    SQLCHAR 0  0 \"\\n\"   3    lastname", contents.get(4).trim());
    } finally {
      util.emptyBaseDirectory();
    }
  }
Exemple #11
0
  @Override
  public StatementRunnerResult execute(String aSql) throws SQLException {
    StatementRunnerResult result = new StatementRunnerResult();
    String parm = SqlUtil.stripVerb(aSql);

    cmdLine.parse(parm);

    WbFile inputFile = evaluateFileArgument(cmdLine.getValue(ARG_INPUT));
    WbFile outputFile = evaluateFileArgument(cmdLine.getValue(ARG_OUTPUT));
    WbFile xsltFile = evaluateFileArgument(cmdLine.getValue(ARG_STYLESHEET));

    if (!cmdLine.hasArguments()) {
      result.addErrorMessageByKey("ErrXsltWrongParameter");
      return result;
    }

    if (inputFile == null) {
      result.addErrorMessageByKey("ErrXsltMissingInputFile");
      return result;
    }

    if (!inputFile.exists()) {
      result.addErrorMessageByKey("ErrFileNotFound", cmdLine.getValue(ARG_INPUT));
      return result;
    }

    if (outputFile == null) {
      result.addErrorMessageByKey("ErrXsltMissingOutputFile");
      return result;
    }

    if (xsltFile == null) {
      result.addErrorMessageByKey("ErrXsltMissingStylesheet");
      return result;
    }

    if (!xsltFile.exists()) {
      result.addErrorMessageByKey("ErrFileNotFound", cmdLine.getValue(ARG_STYLESHEET));
      return result;
    }

    Map<String, String> params = getParameters(cmdLine);

    XsltTransformer transformer = new XsltTransformer();

    try {
      transformer.setSaveSystemOutMessages(true);
      transformer.setXsltBaseDir(getXsltBaseDir());

      transformer.transform(inputFile, outputFile, xsltFile, params);

      String msg = transformer.getAllOutputs();
      if (msg.length() != 0) {
        result.addMessage(msg);
        result.addMessage(""); // create newline
      }

      WbFile xsltUsed = new WbFile(transformer.getXsltUsed());
      WbFile userXslt = new WbFile(xsltFile);
      if (xsltUsed != null && !userXslt.equals(xsltUsed)) {
        // If the xslt file has been "automatically" found, inform the user about this
        result.addMessage(ResourceMgr.getFormattedString("MsgXsltUsed", xsltUsed.getFullPath()));
      }
      result.addMessage(ResourceMgr.getFormattedString("MsgXsltSuccessful", outputFile));
      result.setSuccess();
    } catch (Exception e) {
      LogMgr.logError(
          "WbXslt.execute()",
          "Error when transforming '" + inputFile + "' to '" + outputFile + "' using " + xsltFile,
          e);
      String msg = transformer.getAllOutputs(e);
      LogMgr.logError("WbXslt.execute()", msg, null);
      result.addErrorMessage(msg);
    }
    return result;
  }
  private ConnectionProfile readProfile(File baseDir, String key, WbProperties props) {
    key = "." + key;
    String url = props.getProperty(PROP_PREFIX + key + PROP_URL, null);
    String tags = props.getProperty(PROP_PREFIX + key + PROP_TAGS, null);
    String name = props.getProperty(PROP_PREFIX + key + PROP_NAME, null);
    String driverClass = props.getProperty(PROP_PREFIX + key + PROP_DRIVERCLASS, null);
    String driverJar = props.getProperty(PROP_PREFIX + key + PROP_DRIVERJAR, null);
    String driverName = props.getProperty(PROP_PREFIX + key + PROP_DRIVERNAME, null);
    String group = props.getProperty(PROP_PREFIX + key + PROP_GROUP, null);
    String user = props.getProperty(PROP_PREFIX + key + PROP_USERNAME, null);
    String pwd = props.getProperty(PROP_PREFIX + key + PROP_PWD, null);
    String icon = props.getProperty(PROP_PREFIX + key + PROP_ICON, null);
    String wksp = props.getProperty(PROP_PREFIX + key + PROP_WORKSPACE, null);
    String delimiter = props.getProperty(PROP_PREFIX + key + PROP_ALT_DELIMITER, null);
    String macroFile = props.getProperty(PROP_PREFIX + key + PROP_MACROFILE, null);
    String postConnect = props.getProperty(PROP_PREFIX + key + PROP_SCRIPT_CONNECT, null);
    String preDisconnect = props.getProperty(PROP_PREFIX + key + PROP_SCRIPT_DISCONNECT, null);
    String idleScript = props.getProperty(PROP_PREFIX + key + PROP_SCRIPT_IDLE, null);
    String xmlProps = props.getProperty(PROP_PREFIX + key + PROP_CONN_PROPS, null);
    String colorValue = props.getProperty(PROP_PREFIX + key + PROP_INFO_COLOR, null);

    Properties connProps = toProperties(xmlProps);
    Color color = Settings.stringToColor(colorValue);

    boolean autoCommit = props.getBoolProperty(PROP_PREFIX + key + PROP_AUTOCOMMMIT, false);
    boolean storeCache = props.getBoolProperty(PROP_PREFIX + key + PROP_STORECACHE, false);
    boolean storePwd = props.getBoolProperty(PROP_PREFIX + key + PROP_STORE_PWD, true);
    boolean rollback = props.getBoolProperty(PROP_PREFIX + key + PROP_ROLLBACK_DISCONNECT, false);
    boolean seperateConnection =
        props.getBoolProperty(PROP_PREFIX + key + PROP_SEPARATECONNECTION, false);
    boolean ignoreDropError =
        props.getBoolProperty(PROP_PREFIX + key + PROP_IGNOREDROPERRORS, false);
    boolean trimCharData = props.getBoolProperty(PROP_PREFIX + key + PROP_TRIMCHARDATA, false);
    boolean sysDBA = props.getBoolProperty(PROP_PREFIX + key + PROP_ORACLESYSDBA, false);
    boolean detectOpen =
        props.getBoolProperty(PROP_PREFIX + key + PROP_DETECTOPENTRANSACTION, false);
    boolean readonly = props.getBoolProperty(PROP_PREFIX + key + PROP_READONLY, false);
    boolean preventNoWhere =
        props.getBoolProperty(PROP_PREFIX + key + PROP_PREVENT_NO_WHERE, false);
    boolean confirmUpdates = props.getBoolProperty(PROP_PREFIX + key + PROP_CONFIRM_UPDATES, false);
    boolean promptUsername = props.getBoolProperty(PROP_PREFIX + key + PROP_PROMPTUSERNAME, false);
    boolean emptyStringIsNull =
        props.getBoolProperty(PROP_PREFIX + key + PROP_EMPTY_STRING_IS_NULL, false);
    boolean includeNullInInsert =
        props.getBoolProperty(PROP_PREFIX + key + PROP_INCLUDE_NULL_ON_INSERT, true);
    boolean removeComments = props.getBoolProperty(PROP_PREFIX + key + PROP_REMOVE_COMMENTS, false);
    boolean rememberExplorerSchema =
        props.getBoolProperty(PROP_PREFIX + key + PROP_REMEMEMBER_SCHEMA, false);
    boolean hideWarnings = props.getBoolProperty(PROP_PREFIX + key + PROP_HIDE_WARNINGS, false);
    boolean copyProps = props.getBoolProperty(PROP_PREFIX + key + PROP_COPY_PROPS, false);
    int idleTime = props.getIntProperty(PROP_PREFIX + key + PROP_IDLE_TIME, -1);
    int size = props.getIntProperty(PROP_PREFIX + key + PROP_FETCHSIZE, -1);
    int timeOut = props.getIntProperty(PROP_PREFIX + key + PROP_CONNECTION_TIMEOUT, -1);

    Integer fetchSize = null;
    if (size >= 0) {
      fetchSize = Integer.valueOf(size);
    }

    Integer connectionTimeOut = null;
    if (timeOut > 0) {
      connectionTimeOut = Integer.valueOf(timeOut);
    }

    // if a driver jar was explicitely specified, that jar should be used
    // regardless of any registered driver that might be referenced through driverName
    if (StringUtil.isNonEmpty(driverJar)) {
      WbFile drvFile = new WbFile(driverJar);
      if (!drvFile.isAbsolute()) {
        drvFile = new WbFile(baseDir, driverJar);
        LogMgr.logDebug(
            "IniProfileStorage.readProfile()",
            "Using full path: "
                + drvFile.getFullPath()
                + " for driver jar "
                + driverJar
                + " from profile "
                + name);
        driverJar = drvFile.getFullPath();
      } else {
        driverJar = drvFile.getFullPath();
      }
      DbDriver drv = ConnectionMgr.getInstance().registerDriver(driverClass, driverJar);
      driverName = drv.getName();
    }

    ObjectNameFilter schemaFilter = getSchemaFilter(props, key);
    ObjectNameFilter catalogFilter = getCatalogFilter(props, key);

    ConnectionProfile profile = new ConnectionProfile();
    profile.setName(name);
    profile.setUsername(user);
    profile.setUrl(url);
    profile.setInputPassword(pwd);
    profile.setDriverclass(driverClass);
    profile.setDriverName(driverName);
    profile.setGroup(group);
    profile.setTagList(tags);
    profile.setDefaultFetchSize(fetchSize);
    profile.setOracleSysDBA(sysDBA);
    profile.setReadOnly(readonly);
    profile.setWorkspaceFile(wksp);
    profile.setIcon(icon);
    profile.setConnectionTimeout(connectionTimeOut);
    profile.setRollbackBeforeDisconnect(rollback);
    profile.setUseSeparateConnectionPerTab(seperateConnection);
    profile.setAlternateDelimiterString(delimiter);
    profile.setMacroFilename(macroFile);
    profile.setIgnoreDropErrors(ignoreDropError);
    profile.setTrimCharData(trimCharData);
    profile.setDetectOpenTransaction(detectOpen);
    profile.setPreventDMLWithoutWhere(preventNoWhere);
    profile.setConfirmUpdates(confirmUpdates);
    profile.setPromptForUsername(promptUsername);
    profile.setEmptyStringIsNull(emptyStringIsNull);
    profile.setIncludeNullInInsert(includeNullInInsert);
    profile.setRemoveComments(removeComments);
    profile.setStoreExplorerSchema(rememberExplorerSchema);
    profile.setHideWarnings(hideWarnings);
    profile.setStoreCacheLocally(storeCache);
    profile.setAutocommit(autoCommit);
    profile.setPreDisconnectScript(preDisconnect);
    profile.setPostConnectScript(postConnect);
    profile.setIdleScript(idleScript);
    profile.setIdleTime(idleTime);
    profile.setStorePassword(storePwd);
    profile.setCopyExtendedPropsToSystem(copyProps);
    profile.setConnectionProperties(connProps);
    profile.setInfoDisplayColor(color);
    profile.setSchemaFilter(schemaFilter);
    profile.setCatalogFilter(catalogFilter);

    return profile;
  }