Esempio n. 1
0
  @Test
  public void testBuildObjects() throws SQLException {
    Db db = IciqlSuite.openNewDb();
    db.insertAll(Product.getList());

    // test plain statement
    ResultSet rs = db.executeQuery("select * from product where unitsInStock=120");
    List<Product> products = db.buildObjects(Product.class, rs);
    JdbcUtils.closeSilently(rs, true);

    assertEquals(1, products.size());
    assertEquals("Condiments", products.get(0).category);

    // test prepared statement
    rs = db.executeQuery("select * from product where unitsInStock=?", 120);
    products = db.buildObjects(Product.class, rs);
    JdbcUtils.closeSilently(rs, true);

    assertEquals(1, products.size());
    assertEquals("Condiments", products.get(0).category);

    db.close();
  }
Esempio n. 2
0
  /**
   * Reads the DatabaseMetaData for the details of this table including primary keys and indexes.
   *
   * @param metaData the database meta data
   */
  void read(DatabaseMetaData metaData) throws SQLException {
    ResultSet rs = null;

    // primary keys
    try {
      rs = metaData.getPrimaryKeys(null, schema, table);
      while (rs.next()) {
        String c = rs.getString("COLUMN_NAME");
        primaryKeys.add(c);
      }
      closeSilently(rs);

      // indexes
      rs = metaData.getIndexInfo(null, schema, table, false, true);
      indexes = Utils.newHashMap();
      while (rs.next()) {
        IndexInspector info = new IndexInspector(rs);
        if (info.type.equals(IndexType.UNIQUE)) {
          String name = info.name.toLowerCase();
          if (name.startsWith("primary")
              || name.startsWith("sys_idx_sys_pk")
              || name.startsWith("sql")
              || name.endsWith("_pkey")) {
            // skip primary key indexes
            continue;
          }
        }
        if (indexes.containsKey(info.name)) {
          indexes.get(info.name).addColumn(rs);
        } else {
          indexes.put(info.name, info);
        }
      }
      closeSilently(rs);

      // columns
      rs = metaData.getColumns(null, schema, table, null);
      columns = Utils.newHashMap();
      while (rs.next()) {
        ColumnInspector col = new ColumnInspector();
        col.name = rs.getString("COLUMN_NAME");
        col.type = rs.getString("TYPE_NAME");
        col.clazz = ModelUtils.getClassForSqlType(col.type, dateTimeClass);
        col.size = rs.getInt("COLUMN_SIZE");
        col.nullable = rs.getInt("NULLABLE") == DatabaseMetaData.columnNullable;
        try {
          Object autoIncrement = rs.getObject("IS_AUTOINCREMENT");
          if (autoIncrement instanceof Boolean) {
            col.isAutoIncrement = (Boolean) autoIncrement;
          } else if (autoIncrement instanceof String) {
            String val = autoIncrement.toString().toLowerCase();
            col.isAutoIncrement = val.equals("true") | val.equals("yes");
          } else if (autoIncrement instanceof Number) {
            Number n = (Number) autoIncrement;
            col.isAutoIncrement = n.intValue() > 0;
          }
        } catch (SQLException s) {
          //					throw s;
        }
        if (primaryKeys.size() == 1) {
          if (col.name.equalsIgnoreCase(primaryKeys.get(0))) {
            col.isPrimaryKey = true;
          }
        }
        if (!col.isAutoIncrement) {
          col.defaultValue = rs.getString("COLUMN_DEF");
        }
        columns.put(col.name.toLowerCase(), col);
      }
    } finally {
      closeSilently(rs);
    }
  }