/**
   * The lint that does the job
   *
   * @param table table
   * @param connection connection
   * @throws SchemaCrawlerException SchemaCrawlerException
   */
  @Override
  protected void lint(final Table table, final Connection connection)
      throws SchemaCrawlerException {

    try (Statement stmt = connection.createStatement()) {
      String sql;
      List<Column> columns = table.getColumns();
      for (Column column : columns) {
        if (LintUtils.isSqlTypeTextBased(
            column.getColumnDataType().getJavaSqlType().getJavaSqlType())) {

          sql = "select " + column.getName() + " from " + table.getName();
          LOGGER.log(Level.INFO, "SQL : {0}", sql);

          ResultSet rs = stmt.executeQuery(sql);
          boolean found = false;
          while (rs.next() && !found) {
            String data = rs.getString(column.getName());

            if (JSonUtils.isJsonContent(data)) {
              LOGGER.log(
                  Level.INFO, "Adding lint as data is JSON but column type is not JSONB or JSON.");
              addLint(table, getDescription(), column.getFullName());
              found = true;
            }
          }
        }
      }

    } catch (SQLException ex) {
      LOGGER.severe(ex.getMessage());
    }
  }
Ejemplo n.º 2
0
  private static Element createForeignKeyColumnElement(
      final Document dom, final ForeignKeyColumnReference fkcm) {
    final Element eForeignKey = dom.createElement("column");
    final Column referencingColumn = fkcm.getForeignKeyColumn();
    final Column referencedColumn = fkcm.getPrimaryKeyColumn();

    eForeignKey.setAttribute("name", referencingColumn.getName());
    eForeignKey.setAttribute("referencedtable", referencedColumn.getParent().getName());
    eForeignKey.setAttribute("referencedcolumn", referencedColumn.getName());

    return eForeignKey;
  }
  private Multimap<String, IncrementingColumn> findIncrementingColumns(final Column[] columns) {
    if (columns == null || columns.length <= 1) {
      return new Multimap<String, IncrementingColumn>();
    }

    final Pattern pattern = Pattern.compile("([^0-9]*)([0-9]+)");

    final Map<String, Integer> incrementingColumnsMap = new HashMap<String, Integer>();
    for (final Column column : columns) {
      final String columnName = Utility.convertForComparison(column.getName());
      incrementingColumnsMap.put(columnName, 1);
      final Matcher matcher = pattern.matcher(columnName);
      if (matcher.matches()) {
        final String columnNameBase = matcher.group(1);
        if (incrementingColumnsMap.containsKey(columnNameBase)) {
          incrementingColumnsMap.put(
              columnNameBase, incrementingColumnsMap.get(columnNameBase) + 1);
        } else {
          incrementingColumnsMap.put(columnNameBase, 1);
        }
      }
    }

    final Set<String> columnNameBases = new HashSet<String>(incrementingColumnsMap.keySet());
    for (final String columnNameBase : columnNameBases) {
      if (incrementingColumnsMap.get(columnNameBase) == 1) {
        incrementingColumnsMap.remove(columnNameBase);
      }
    }

    final Multimap<String, IncrementingColumn> incrementingColumns =
        new Multimap<String, IncrementingColumn>();

    for (final Column column : columns) {
      final String columnName = Utility.convertForComparison(column.getName());
      if (incrementingColumnsMap.containsKey(columnName)) {
        incrementingColumns.add(columnName, new IncrementingColumn(columnName, "0", column));
      }
      final Matcher matcher = pattern.matcher(columnName);
      if (matcher.matches()) {
        final String columnNameBase = matcher.group(1);
        final String columnIncrement = matcher.group(2);
        if (incrementingColumnsMap.containsKey(columnNameBase)) {
          incrementingColumns.add(
              columnNameBase, new IncrementingColumn(columnNameBase, columnIncrement, column));
        }
      }
    }

    return incrementingColumns;
  }
Ejemplo n.º 4
0
 @Override
 public Column getColumn(final String name) {
   if (column.getName().equals(name)) {
     return column;
   } else {
     return null;
   }
 }
 private List<String> findColumnsWithQuotedNames(final Column[] columns) {
   final List<String> columnsWithQuotedNames = new ArrayList<String>();
   for (final Column column : columns) {
     final String columnName = column.getName();
     if (isQuotedName(columnName)) {
       columnsWithQuotedNames.add(columnName);
     }
   }
   return columnsWithQuotedNames;
 }
Ejemplo n.º 6
0
  private Element createColumnElement(final Document dom, final Column column) {
    final Element eColumn = dom.createElement("column");

    final String columnName = getPhysicalName(column.getName());
    eColumn.setAttribute("name", getLogicalName(columnName));
    eColumn.setAttribute("physicalname", columnName);

    setColumnTypeAttributes(eColumn, column, "table");

    String requirement = null;
    if (column.isPartOfPrimaryKey()) {
      requirement = "primarykey";
    } else {
      requirement = column.isNullable() ? "optional" : "mandatory";
    }
    eColumn.setAttribute("requirement", requirement);

    return eColumn;
  }
Ejemplo n.º 7
0
 ColumnPartial(final Column column) {
   this(requireNonNull(column, "No column provided").getParent(), column.getName());
   addAttributes(column.getAttributes());
 }