Ejemplo n.º 1
0
  @Override
  public List<Table> getTables(List<String> includes, List<String> excludes) {
    String filter = prepareIncludeAndExcludeClause(includes, excludes);
    List<Table> tables = new ArrayList<Table>();
    String query =
        "SELECT TABLE_CATALOG, TABLE_NAME, TABLE_SCHEMA FROM information_schema.tables WHERE TABLE_TYPE=?"
            + filter;
    String tableType = "BASE TABLE";

    try {
      PreparedStatement statement = getConnection().prepareStatement(query);
      statement.setString(1, tableType);
      ResultSet result = statement.executeQuery();

      while (result.next()) {
        String catalog = result.getString("TABLE_CATALOG");
        String schema = result.getString("TABLE_SCHEMA");
        String name = result.getString("TABLE_NAME");

        Table table = new Table(catalog, schema, name);
        table.setFields(getFieldsForTable(table)); // important :)
        tables.add(table);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return tables;
  }
Ejemplo n.º 2
0
  @Override
  public PreparedStatement buildPreparedInsertStatement(Table table) throws Exception {
    List<Field> fields = table.getFields();
    StringBuffer query = new StringBuffer("INSERT INTO ");
    StringBuffer values = new StringBuffer("VALUES(");

    query.append(buildTableName(table));
    query.append(" (");

    for (int i = 0, l = fields.size(); i < l; i++) {
      Field field = fields.get(i);

      query.append("[");
      query.append(field.getName());
      query.append("]");

      values.append("?");

      if (i < l - 1) {
        query.append(",");
        values.append(",");
      }
    }

    query.append(") ");
    values.append(")");
    query.append(values);

    return createPreparedStatement(query.toString());
  }
Ejemplo n.º 3
0
  /**
   * Delivers a list with {@link Field}'s for the given {@link Table}.
   *
   * @param table
   * @return
   */
  private List<Field> getFieldsForTable(Table table) {
    List<Field> fields = new ArrayList<Field>();

    try {
      DatabaseMetaData meta = getConnection().getMetaData();
      ResultSet columns = meta.getColumns(null, null, table.getName(), null);

      while (columns.next()) {
        Field field = new Field(columns.getString("COLUMN_NAME"), columns.getInt("DATA_TYPE"));
        fields.add(field);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return fields;
  }
Ejemplo n.º 4
0
 @Override
 public String buildTableName(Table table) {
   return "[" + table.getSchema() + "].[" + table.getName() + "]";
 }