コード例 #1
1
  /**
   * Test importing an unsupported field e.g. datetime, the program should continue and assign null
   * value to the field.
   *
   * @throws Exception
   */
  @Test
  public void testImportUnsupportedField() throws Exception {
    ConnectionProperties p = getConnectionProperties();
    Connection cn = DatabaseConnection.getConnection(p);
    try {
      List<Field> verifiedFields = new Vector<Field>();
      String[] fields = "ListingId, Title".split(",");
      String tableName = "Listings";
      DatabaseConnection.verifyTable(p, tableName, fields, verifiedFields);

      Collection<JsonNode> importNodes = new LinkedList<JsonNode>();
      JsonNodeFactory f = JsonNodeFactory.instance;

      int listingId = 1559350;

      ObjectNode n;
      n = new ObjectNode(f);
      n.put("ListingId", listingId);
      importNodes.add(n);

      JsonArrayImporter importer = new JsonArrayImporter(p);
      importer.doImport(tableName, verifiedFields, importNodes);

      Statement st = cn.createStatement();
      ResultSet rs = st.executeQuery("SELECT ListingId, Title FROM Listings");
      assertTrue("Expected result set to contain a record", rs.next());
      assertEquals(listingId, rs.getInt("ListingId"));
      assertEquals(null, rs.getString("Title"));
    } finally {
      cn.close();
    }
  }
コード例 #2
0
  @Override
  public void setConnection(final DatabaseConnection conn) {
    LogFactory.getLogger()
        .debug("Connected to " + conn.getConnectionUserName() + "@" + conn.getURL());
    this.connection = conn;
    try {
      boolean autoCommit = conn.getAutoCommit();
      if (autoCommit == getAutoCommitMode()) {
        // Don't adjust the auto-commit mode if it's already what the database wants it to be.
        LogFactory.getLogger()
            .debug("Not adjusting the auto commit mode; it is already " + autoCommit);
      } else {
        // Store the previous auto-commit mode, because the connection needs to be restored to it
        // when this
        // AbstractDatabase type is closed. This is important for systems which use connection
        // pools.
        previousAutoCommit = autoCommit;

        LogFactory.getLogger()
            .debug("Setting auto commit to " + getAutoCommitMode() + " from " + autoCommit);
        connection.setAutoCommit(getAutoCommitMode());
      }
    } catch (DatabaseException e) {
      LogFactory.getLogger()
          .warning("Cannot set auto commit to " + getAutoCommitMode() + " on connection");
    }

    this.connection.attached(this);
  }
コード例 #3
0
 /**
  * Default implementation, just look for "local" IPs. If the database returns a null URL we return
  * false since we don't know it's safe to run the update.
  *
  * @throws liquibase.exception.DatabaseException
  */
 @Override
 public boolean isSafeToRunUpdate() throws DatabaseException {
   DatabaseConnection connection = getConnection();
   if (connection == null) {
     return true;
   }
   String url = connection.getURL();
   if (url == null) {
     return false;
   }
   return (url.contains("localhost")) || (url.contains("127.0.0.1"));
 }
コード例 #4
0
  @Override
  public void close() throws DatabaseException {
    DatabaseConnection connection = getConnection();
    if (connection != null) {
      if (previousAutoCommit != null) {
        try {
          connection.setAutoCommit(previousAutoCommit);
        } catch (DatabaseException e) {
          LogFactory.getLogger()
              .warning("Failed to restore the auto commit to " + previousAutoCommit);

          throw e;
        }
      }
      connection.close();
    }
    ExecutorService.getInstance().clearExecutor(this);
  }
コード例 #5
0
 @Override
 public int getDatabaseMinorVersion() throws DatabaseException {
   if (connection == null) {
     return -1;
   }
   try {
     return connection.getDatabaseMinorVersion();
   } catch (DatabaseException e) {
     throw new DatabaseException(e);
   }
 }
コード例 #6
0
 static void addNewLanguage() {
   if (dbIsOpen()) {
     System.out.println("New language name: ");
     String newLanguage = scanner.next();
     try {
       DatabaseConnection.updateListOfLanguages(flags.getConnection(), newLanguage);
     } catch (SQLException ex) {
       printSQLException(ex);
     }
   }
 }
コード例 #7
0
  @Override
  public String getDatabaseProductVersion() throws DatabaseException {
    if (connection == null) {
      return null;
    }

    try {
      return connection.getDatabaseProductVersion();
    } catch (DatabaseException e) {
      throw new DatabaseException(e);
    }
  }
コード例 #8
0
  /** Returns the name of the database product according to the underlying database. */
  @Override
  public String getDatabaseProductName() {
    if (connection == null) {
      return getDefaultDatabaseProductName();
    }

    try {
      return connection.getDatabaseProductName();
    } catch (DatabaseException e) {
      throw new RuntimeException("Cannot get database name");
    }
  }
コード例 #9
0
 static void showLanguages() {
   if (dbIsOpen()) {
     try {
       ArrayList<String> listOfLanguages =
           DatabaseConnection.getListOfLanguages(flags.getConnection());
       for (String language : listOfLanguages) {
         System.out.println(language);
       }
     } catch (SQLException ex) {
       printSQLException(ex);
     }
   }
 }
コード例 #10
0
  /**
   * Test importing string, int, and decimal values work successfully
   *
   * @throws Exception
   */
  @Test
  public void testImportSimple() throws Exception {
    ConnectionProperties p = getConnectionProperties();
    Connection cn = DatabaseConnection.getConnection(p);
    try {
      List<Field> verifiedFields = new Vector<Field>();
      String[] fields = "ListingId, Title, StartPrice".split(",");
      String tableName = "Listings";
      DatabaseConnection.verifyTable(p, tableName, fields, verifiedFields);

      Collection<JsonNode> importNodes = new LinkedList<JsonNode>();
      JsonNodeFactory f = JsonNodeFactory.instance;

      int listingId = 1559350;
      String title = "Product Pro1-Beta-0066";
      BigDecimal startPrice = BigDecimal.valueOf(20.64);

      ObjectNode n;
      n = new ObjectNode(f);
      n.put("ListingId", listingId);
      n.put("Title", title);
      n.put("StartPrice", startPrice);
      importNodes.add(n);

      JsonArrayImporter importer = new JsonArrayImporter(p);
      importer.doImport(tableName, verifiedFields, importNodes);

      Statement st = cn.createStatement();
      ResultSet rs = st.executeQuery("SELECT ListingId, Title, StartPrice FROM Listings");
      assertTrue("Expected result set to contain a record", rs.next());
      assertEquals(listingId, rs.getInt("ListingId"));
      assertEquals(title, rs.getString("Title"));
      assertEquals(startPrice, rs.getBigDecimal("StartPrice"));
      assertFalse("Expect result set to only contain one record", rs.next());
    } finally {
      cn.close();
    }
  }
コード例 #11
0
  static void programRun() {
    System.out.println("Give a command: ");
    String command = scanner.next().toLowerCase();
    if (command.equals("")) {

    } else if (command.equals("exit")) {
      insideExitCommand = true;
      while (insideExitCommand) {
        System.out.println("Are you sure you want to quit? Y/N");
        command = scanner.next().toLowerCase();
        if (command.startsWith("y")) {
          System.out.println("Goodbye.");
          programIsOn = false;
          insideExitCommand = false;
        } else if (command.startsWith("n")) {
          insideExitCommand = false;
        } else {
          printCommandUnknown();
        }
      }
    } else if (command.equals("help")) {
      helpCommand();
    } else if (command.equals("opendb")) {
      openDB();
      // to be continued
    } else if (command.equals("start") || command.equals("analyze")) {
      insideStart = true;
      while (insideStart) {
        Operation operation = new Operation(scanner, flags, command);
        operation.main();
        insideStart = false;
      }
    } else if (command.equals("truncate")) {
      if (dbIsOpen()) {
        try {
          DatabaseConnection.truncateTables(flags.getConnection());
        } catch (SQLException ex) {
          printSQLException(ex);
        }
      }
    } else if (command.equals("addlang")) {
      addNewLanguage();
    } else if (command.equals("showlangs")) {
      showLanguages();
    } else if (command.equals("automatic")) {
      flags.setApproveFormsAutomatically(!flags.areFormsAutomaticallyApproved());
    } else {
      printCommandUnknown();
    }
  }
コード例 #12
0
  @Override
  public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    AbstractJdbcDatabase that = (AbstractJdbcDatabase) o;

    if (connection == null) {
      if (that.connection == null) {
        return this == that;
      } else {
        return false;
      }
    } else {
      return connection.equals(that.connection);
    }
  }
コード例 #13
0
 private void truncateTable() throws SQLException, ClassNotFoundException {
   ConnectionProperties p = getConnectionProperties();
   Connection cn = DatabaseConnection.getConnection(p);
   Statement st = cn.createStatement();
   st.execute("TRUNCATE TABLE Listings");
 }
コード例 #14
0
 protected String getConnectionCatalogName() throws DatabaseException {
   return connection.getCatalog();
 }
コード例 #15
0
 @Override
 public int hashCode() {
   return (connection != null ? connection.hashCode() : super.hashCode());
 }