/**
   * @param config
   * @param properties
   */
  public ConnectionURLBuilder(Config config, Properties properties) {
    List<String> opts = new ArrayList<String>();

    for (String key : config.getDbSpecificOptions().keySet()) {
      opts.add((key.startsWith("-") ? "" : "-") + key);
      opts.add(config.getDbSpecificOptions().get(key));
    }
    opts.addAll(config.getRemainingParameters());

    DbSpecificConfig dbConfig = new DbSpecificConfig(config.getDbType());
    options = dbConfig.getOptions();
    connectionURL = buildUrl(opts, properties, config);

    List<String> remaining = config.getRemainingParameters();

    for (DbSpecificOption option : options) {
      int idx = remaining.indexOf("-" + option.getName());
      if (idx >= 0) {
        remaining.remove(idx); // -paramKey
        remaining.remove(idx); // paramValue
      }
    }

    logger.config("connectionURL: " + connectionURL);
  }
  /**
   * Run the LogicalTable(Database,String,String,String,String) constructor test.
   *
   * @throws Exception
   * @generatedBy CodePro at 6/7/15 4:20 PM
   */
  @Test
  public void testLogicalTable_29() throws Exception {
    Database db =
        new Database(
            Config.getInstance(),
            (Connection) null,
            (DatabaseMetaData) null,
            "An��t-1.0.txt",
            "An��t-1.0.txt",
            "An��t-1.0.txt",
            new SchemaMeta("0123456789", "0123456789", "0123456789"));
    String catalog = "";
    String schema = "0123456789";
    String name = "";
    String comments = "0123456789";

    LogicalTable result = new LogicalTable(db, catalog, schema, name, comments);

    // add additional test code here
    // An unexpected exception was thrown in user code while executing this test:
    //    net.sourceforge.schemaspy.model.InvalidConfigurationException: Specified meta file
    // "0123456789" does not exist
    //       at net.sourceforge.schemaspy.model.xml.SchemaMeta.<init>(SchemaMeta.java:74)
    assertNotNull(result);
  }
  /**
   * Returns the number of rows contained in this table, or -1 if unable to determine the number of
   * rows.
   *
   * @return
   */
  public int getNumRows() {
    if (numRows == null) {
      numRows = Config.getInstance().isNumRowsEnabled() ? fetchNumRows() : -1;
    }

    return numRows;
  }
示例#4
0
  public void connectForeignKeys(
      Map<String, Table> tables,
      Database db,
      Properties properties,
      Pattern excludeIndirectColumns,
      Pattern excludeColumns)
      throws SQLException {
    ResultSet rs = null;

    try {
      rs = db.getMetaData().getImportedKeys(null, getSchema(), getName());

      while (rs.next()) {
        addForeignKey(
            rs.getString("FK_NAME"),
            rs.getString("FKCOLUMN_NAME"),
            rs.getString("PKTABLE_SCHEM"),
            rs.getString("PKTABLE_NAME"),
            rs.getString("PKCOLUMN_NAME"),
            tables,
            db,
            properties,
            excludeIndirectColumns,
            excludeColumns);
      }
    } finally {
      if (rs != null) rs.close();
    }

    // if we're one of multiples then also find all of the 'remote' tables in other
    // schemas that point to our primary keys (not necessary in the normal case
    // as we infer this from the opposite direction)
    if (getSchema() != null && Config.getInstance().isOneOfMultipleSchemas()) {
      try {
        rs = db.getMetaData().getExportedKeys(null, getSchema(), getName());

        while (rs.next()) {
          String otherSchema = rs.getString("FKTABLE_SCHEM");
          if (!getSchema().equals(otherSchema))
            db.addRemoteTable(
                otherSchema,
                rs.getString("FKTABLE_NAME"),
                getSchema(),
                properties,
                excludeIndirectColumns,
                excludeColumns);
        }
      } finally {
        if (rs != null) rs.close();
      }
    }
  }
示例#5
0
 public Table(
     Database db,
     String schema,
     String name,
     String comments,
     Properties properties,
     Pattern excludeIndirectColumns,
     Pattern excludeColumns)
     throws SQLException {
   this.schema = schema;
   this.name = name;
   setComments(comments);
   initColumns(db, excludeIndirectColumns, excludeColumns);
   initIndexes(db, properties);
   initPrimaryKeys(db.getMetaData(), properties);
   numRows = Config.getInstance().isNumRowsEnabled() ? fetchNumRows(db, properties) : -1;
 }
  private String getParam(List<String> args, DbSpecificOption option, Config config) {
    String param = null;
    int paramIndex = args.indexOf("-" + option.getName());

    if (paramIndex < 0) {
      if (config != null)
        param = config.getParam(option.getName()); // not in args...might be one of
      // the common db params
      if (param == null)
        throw new Config.MissingRequiredParameterException(
            option.getName(), option.getDescription(), true);
    } else {
      args.remove(paramIndex);
      param = args.get(paramIndex).toString();
      args.remove(paramIndex);
    }

    return param;
  }
  /** @param args */
  public static void main(final String[] args) {
    LogUtils.initializeLogging();

    if (args.length != 1) {
      LOGGER.fatal("You must specify the output directory");
      return;
    }

    final String dbname = "generate_schema";
    Connection connection = null;
    try {
      // generate example database
      final DataSource datasource = Utilities.createMemoryDataSource(dbname);
      connection = datasource.getConnection();

      final String baseDir = "fll/resources/challenge-descriptors/";
      final String challengeName = "example-database.xml";
      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      final URL challengeUrl = classLoader.getResource(baseDir + challengeName);

      final InputStream stream = challengeUrl.openStream();
      final Reader reader = new InputStreamReader(stream, Utilities.DEFAULT_CHARSET);
      final Document document = ChallengeParser.parse(reader);

      GenerateDB.generateDB(document, connection);

      SchemaAnalyzer analyzer = new SchemaAnalyzer();
      final Config config = new HsqlMemConfig();
      config.setAdsEnabled(false);
      config.setDb(dbname);
      config.setHighQuality(true);
      config.setSchema("PUBLIC");
      config.setUser("SA");
      config.setOutputDir(args[0]);
      analyzer.analyze(config);

    } catch (final SQLException e) {
      LOGGER.fatal("Error talking to the database", e);
    } catch (final Exception e) {
      LOGGER.fatal("Error creating the diagram", e);
    } finally {
      // clean up database
      SQLFunctions.close(connection);
    }
  }
  /**
   * @param rs ResultSet from {@link DatabaseMetaData#getImportedKeys(String, String, String)}
   *     rs.getString("FK_NAME"); rs.getString("FKCOLUMN_NAME"); rs.getString("PKTABLE_SCHEM");
   *     rs.getString("PKTABLE_NAME"); rs.getString("PKCOLUMN_NAME");
   * @param tables Map
   * @param db
   * @throws SQLException
   */
  protected void addForeignKey(
      String fkName,
      String fkColName,
      String pkTableSchema,
      String pkTableName,
      String pkColName,
      int updateRule,
      int deleteRule,
      Map<String, Table> tables,
      Pattern excludeIndirectColumns,
      Pattern excludeColumns)
      throws SQLException {
    if (fkName == null) return;

    ForeignKeyConstraint foreignKey = foreignKeys.get(fkName);

    if (foreignKey == null) {
      foreignKey = new ForeignKeyConstraint(this, fkName, updateRule, deleteRule);

      foreignKeys.put(fkName, foreignKey);
    }

    TableColumn childColumn = getColumn(fkColName);
    if (childColumn != null) {
      foreignKey.addChildColumn(childColumn);

      Table parentTable = tables.get(pkTableName);
      String parentSchema = pkTableSchema;
      String baseSchema = Config.getInstance().getSchema();

      // if named table doesn't exist in this schema
      // or exists here but really referencing same named table in another schema
      if (parentTable == null
          || (baseSchema != null && parentSchema != null && !baseSchema.equals(parentSchema))) {
        parentTable =
            db.addRemoteTable(
                parentSchema,
                pkTableName,
                baseSchema,
                properties,
                excludeIndirectColumns,
                excludeColumns);
      }

      if (parentTable != null) {
        TableColumn parentColumn = parentTable.getColumn(pkColName);
        if (parentColumn != null) {
          foreignKey.addParentColumn(parentColumn);

          childColumn.addParent(parentColumn, foreignKey);
          parentColumn.addChild(childColumn, foreignKey);
        } else {
          logger.warning(
              "Couldn't add FK '"
                  + foreignKey.getName()
                  + "' to table '"
                  + this
                  + "' - Column '"
                  + pkColName
                  + "' doesn't exist in table '"
                  + parentTable
                  + "'");
        }
      } else {
        logger.warning(
            "Couldn't add FK '"
                + foreignKey.getName()
                + "' to table '"
                + this
                + "' - Unknown Referenced Table '"
                + pkTableName
                + "'");
      }
    } else {
      logger.warning(
          "Couldn't add FK '"
              + foreignKey.getName()
              + "' to table '"
              + this
              + "' - Column '"
              + fkColName
              + "' doesn't exist");
    }
  }
示例#9
0
  /**
   * @param rs ResultSet from {@link DatabaseMetaData#getImportedKeys(String, String, String)}
   *     rs.getString("FK_NAME"); rs.getString("FKCOLUMN_NAME"); rs.getString("PKTABLE_SCHEM");
   *     rs.getString("PKTABLE_NAME"); rs.getString("PKCOLUMN_NAME");
   * @param tables Map
   * @param db
   * @throws SQLException
   */
  protected void addForeignKey(
      String fkName,
      String fkColName,
      String pkTableSchema,
      String pkTableName,
      String pkColName,
      Map<String, Table> tables,
      Database db,
      Properties properties,
      Pattern excludeIndirectColumns,
      Pattern excludeColumns)
      throws SQLException {
    if (fkName == null) return;

    ForeignKeyConstraint foreignKey = getForeignKey(fkName);

    if (foreignKey == null) {
      foreignKey = new ForeignKeyConstraint(this, fkName);

      foreignKeys.put(foreignKey.getName(), foreignKey);
    }

    TableColumn childColumn = getColumn(fkColName);
    if (childColumn != null) {
      foreignKey.addChildColumn(childColumn);

      Table parentTable = tables.get(pkTableName);
      if (parentTable == null) {
        String otherSchema = pkTableSchema;
        if (otherSchema != null
            && !otherSchema.equals(getSchema())
            && Config.getInstance().isOneOfMultipleSchemas()) {
          parentTable =
              db.addRemoteTable(
                  otherSchema,
                  pkTableName,
                  getSchema(),
                  properties,
                  excludeIndirectColumns,
                  excludeColumns);
        }
      }

      if (parentTable != null) {
        TableColumn parentColumn = parentTable.getColumn(pkColName);
        if (parentColumn != null) {
          foreignKey.addParentColumn(parentColumn);

          childColumn.addParent(parentColumn, foreignKey);
          parentColumn.addChild(childColumn, foreignKey);
        } else {
          System.err.println(
              "Couldn't add FK '"
                  + foreignKey.getName()
                  + "' to table '"
                  + this
                  + "' - Column '"
                  + pkColName
                  + "' doesn't exist in table '"
                  + parentTable
                  + "'");
        }
      } else {
        System.err.println(
            "Couldn't add FK '"
                + foreignKey.getName()
                + "' to table '"
                + this
                + "' - Unknown Referenced Table '"
                + pkTableName
                + "'");
      }
    } else {
      System.err.println(
          "Couldn't add FK '"
              + foreignKey.getName()
              + "' to table '"
              + this
              + "' - Column '"
              + fkColName
              + "' doesn't exist");
    }
  }