public static void loadIdentitySpecifier(
      Connection connection, IdentitySpecifier identitySpecifier, Column column)
      throws SQLException {
    final Table table = column.getTable();
    final Schema schema = table.getSchema();
    final Database database = ModelHelper.getDatabase(schema);
    final DatabaseDefinition databaseDefinition =
        RDBCorePlugin.getDefault().getDatabaseDefinitionRegistry().getDefinition(database);
    final DataModelElementFactory factory = databaseDefinition.getDataModelElementFactory();

    String query =
        "Select INCREMENT,START,MAXVALUE,MINVALUE,CYCLE, CACHE "
            + //$NON-NLS-1$
            " FROM SYSCAT.COLIDENTATTRIBUTES"
            + //$NON-NLS-1$
            " WHERE TABSCHEMA='"
            + LUWUtil.getIdentifier(schema.getName())
            + "'"
            + //$NON-NLS-1$ //$NON-NLS-2$
            " AND TABNAME= '"
            + LUWUtil.getIdentifier(table.getName())
            + "'"
            + //$NON-NLS-1$ //$NON-NLS-2$
            " AND COLNAME ='"
            + LUWUtil.getIdentifier(column.getName())
            + "'"; //$NON-NLS-1$ //$NON-NLS-2$

    Statement s = connection.createStatement();
    ResultSet r = s.executeQuery(query);

    try {
      while (r.next()) {
        DB2IdentitySpecifier identity = (DB2IdentitySpecifier) identitySpecifier;
        identity.setIncrement(r.getBigDecimal("INCREMENT").toBigInteger()); // $NON-NLS-1$
        identity.setStartValue(r.getBigDecimal("START").toBigInteger()); // $NON-NLS-1$
        identity.setMinimum(r.getBigDecimal("MINVALUE").toBigInteger()); // $NON-NLS-1$
        identity.setMaximum(r.getBigDecimal("MAXVALUE").toBigInteger()); // $NON-NLS-1$
        identity.setCache(r.getInt("CACHE")); // $NON-NLS-1$

        if (r.getString("CYCLE").trim().equals("Y")) { // $NON-NLS-1$ //$NON-NLS-2$
          identitySpecifier.setCycleOption(true);
        } else {
          identitySpecifier.setCycleOption(false);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    r.close();
    s.close();
  }
  public synchronized void loadGenerateExrepression() {
    if (this.generateExpressionLoaded) return;
    this.generateExpressionLoaded = true;
    if (!this.isGenerated) return;
    boolean deliver = this.eDeliver();
    this.eSetDeliver(false);
    try {
      final Table table = this.getTable();
      final Schema schema = table.getSchema();
      final Database database = ModelHelper.getDatabase(schema);
      final DatabaseDefinition databaseDefinition =
          RDBCorePlugin.getDefault().getDatabaseDefinitionRegistry().getDefinition(database);
      final DataModelElementFactory factory = databaseDefinition.getDataModelElementFactory();

      Statement s = this.getConnection().createStatement();
      ResultSet r =
          s.executeQuery(
              "SELECT TEXT FROM SYSCAT.COLUMNS" //$NON-NLS-1$
                  + " WHERE TABSCHEMA='"
                  + LUWUtil.getIdentifier(schema.getName()) // $NON-NLS-1$
                  + "' AND TABNAME='"
                  + LUWUtil.getIdentifier(table.getName()) // $NON-NLS-1$
                  + "' AND COLNAME='"
                  + LUWUtil.getIdentifier(this.getName())
                  + "'"); //$NON-NLS-1$ //$NON-NLS-2$

      while (r.next()) {
        String exprValue = r.getString("TEXT"); // $NON-NLS-1$
        if (exprValue != null && exprValue.length() > 0) {
          ValueExpression expr =
              (ValueExpression)
                  factory.create(SQLExpressionsPackage.eINSTANCE.getValueExpressionDefault());
          this.setGenerateExpression(expr);
          int pos = exprValue.indexOf("AS"); // $NON-NLS-1$
          if (pos >= 0) {
            exprValue = exprValue.substring(pos + 2).trim();
          }
          expr.setSQL(exprValue);
        }
      }
      r.close();
      s.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    this.eSetDeliver(deliver);
  }
 /**
  * Creates a result set to be used by the loading logic. The SQL statement is:
  *
  * <pre>
  * select c.name from syscolumns c where id = ? order by c.colid
  * </pre>
  */
 protected ResultSet createResultSet() throws SQLException {
   Table table = this.getTable();
   String query = null;
   String catalog = table.getSchema().getCatalog().getName();
   int tableId = ((ICatalogTable) table).getTableId();
   conn = ((ICatalogObject) table).getConnection();
   query = ASESQLs.QUERY_TABLE_COLUMNS;
   ResultSet rs = null;
   oldCatalog = conn.getCatalog();
   conn.setCatalog(catalog);
   stmt = conn.prepareStatement(query);
   stmt.setInt(1, tableId);
   try {
     rs = stmt.executeQuery();
   } catch (SQLException e) {
     JDBCASEPlugin.getDefault().log(e);
     throw e;
   }
   return rs;
 }
Exemplo n.º 4
0
 /**
  *
  * <!-- begin-user-doc -->
  * <!-- end-user-doc -->
  *
  * @generated
  */
 public Table getSupertable() {
   if (supertable != null && supertable.eIsProxy()) {
     InternalEObject oldSupertable = (InternalEObject) supertable;
     supertable = (Table) eResolveProxy(oldSupertable);
     if (supertable != oldSupertable) {
       if (eNotificationRequired())
         eNotify(
             new ENotificationImpl(
                 this,
                 Notification.RESOLVE,
                 SQLTablesPackage.TABLE__SUPERTABLE,
                 oldSupertable,
                 supertable));
     }
   }
   return supertable;
 }
  public static Collection getDistributeStatistics(Connection connection, Column column) {
    Collection statistics = new ArrayList();
    Collection distStat = new ArrayList();
    Collection typeGroup = new ArrayList();
    try {
      Table table = column.getTable();
      Schema schema = table.getSchema();
      int groupNum = 1;

      String query =
          "SELECT TYPE,SEQNO,COLVALUE,VALCOUNT,DISTCOUNT"
              + " FROM SYSSTAT.COLDIST"
              + " WHERE TABSCHEMA='"
              + LUWUtil.getIdentifier(table.getSchema().getName())
              + "'"
              + " AND TABNAME ='"
              + LUWUtil.getIdentifier(table.getName())
              + "'"
              + " AND COLNAME = '"
              + LUWUtil.getIdentifier(column.getName())
              + "'"
              + " AND COLVALUE <>''"
              + " ORDER BY TYPE"
              + " FOR FETCH ONLY";
      Statement s = connection.createStatement();
      ResultSet r = s.executeQuery(query);
      while (r.next()) {
        CatalogStatistics stats = null;
        distStat = new ArrayList();
        final String type = r.getString("TYPE");
        if (type != null && !type.equals("")) {
          stats =
              new CatalogStatistics(
                  "TYPE",
                  LUWCatalogMessages.STAT_COLDIST_TYPE,
                  LUWCatalogMessages.STAT_COLDIST_TYPE_DES,
                  type,
                  "");
          distStat.add(stats);
        }

        final int seqno = r.getInt("SEQNO");
        if (seqno != -1) {
          stats =
              new CatalogStatistics(
                  "SEQNO",
                  LUWCatalogMessages.STAT_COLDIST_SEQNO,
                  LUWCatalogMessages.STAT_COLDIST_SEQNO_DES,
                  seqno,
                  "");
          distStat.add(stats);
        }

        final String colValue = r.getString("COLVALUE");
        if (colValue != null && !colValue.equals("")) {
          stats =
              new CatalogStatistics(
                  "COLVALUE",
                  LUWCatalogMessages.STAT_COLDIST_COLVALUE,
                  LUWCatalogMessages.STAT_COLDIST_COLVALUE_DES,
                  colValue,
                  "SYSSTAT.COLDIST");
          distStat.add(stats);
        }

        final BigInteger valueCount = r.getBigDecimal("VALCOUNT").toBigInteger();
        ;
        if (valueCount.intValue() != -1) {
          stats =
              new CatalogStatistics(
                  "VALCOUNT",
                  LUWCatalogMessages.STAT_COLDIST_VALCOUNT,
                  LUWCatalogMessages.STAT_COLDIST_VALCOUNT_DES,
                  valueCount,
                  "SYSSTAT.COLDIST");
          distStat.add(stats);
        }

        final BigDecimal distColuntValue = r.getBigDecimal("DISTCOUNT");
        if (distColuntValue != null) {
          final BigInteger distCount = r.getBigDecimal("DISTCOUNT").toBigInteger();
          ;
          if (distCount.intValue() != -1) {
            stats =
                new CatalogStatistics(
                    "DISTCOUNT",
                    LUWCatalogMessages.STAT_COLDIST_DISTCOUNT,
                    LUWCatalogMessages.STAT_COLDIST_DISTCOUNT_DES,
                    distCount,
                    "SYSSTAT.COLDIST");
            distStat.add(stats);
          }
        }

        if (distStat.size() > 0) {
          statistics.add(
              new CatalogStatistics(
                  "Distribution " + seqno,
                  MessageFormat.format(
                      LUWCatalogMessages.STAT_COLDIST, new Object[] {new Integer(groupNum++)}),
                  MessageFormat.format(
                      LUWCatalogMessages.STAT_COLDIST_DES, new Object[] {new Integer(seqno)}),
                  distStat,
                  "SYSSTAT.COLDIST"));
        }
      }
      r.close();
      s.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return statistics;
  }
  public static Collection getColumnStatistics(Connection connection, Column column) {
    Collection statistics = new ArrayList();
    try {
      Table table = column.getTable();
      Schema schema = table.getSchema();

      String query =
          "SELECT COLCARD,HIGH2KEY,LOW2KEY,AVGCOLLEN, NUMNULLS, SUB_COUNT"
              + ",SUB_DELIM_LENGTH "
              + " FROM SYSSTAT.COLUMNS"
              + " WHERE TABSCHEMA='"
              + LUWUtil.getIdentifier(table.getSchema().getName())
              + "'"
              + " AND TABNAME ='"
              + LUWUtil.getIdentifier(table.getName())
              + "'"
              + " AND COLNAME = '"
              + LUWUtil.getIdentifier(column.getName())
              + "'"
              + " FOR FETCH ONLY";
      Statement s = connection.createStatement();
      ResultSet r = s.executeQuery(query);
      while (r.next()) {
        CatalogStatistics stats = null;
        final BigInteger card = r.getBigDecimal("COLCARD").toBigInteger();
        if (card.intValue() != -1) {
          stats =
              new CatalogStatistics(
                  "COLCARD",
                  LUWCatalogMessages.STAT_COL_CARD,
                  LUWCatalogMessages.STAT_COL_CARD_DES,
                  card,
                  "SYSSTAT.COLUMNS");
          statistics.add(stats);
        }

        DataType datatype = column.getDataType();
        boolean isBinaryData = false;
        if (datatype instanceof PredefinedDataType) {
          PredefinedDataType type = (PredefinedDataType) datatype;
          if (type.getPrimitiveType() == PrimitiveType.BINARY_LITERAL
              || type.getPrimitiveType() == PrimitiveType.BINARY_VARYING_LITERAL) {
            isBinaryData = true;
          }
        }

        if (isBinaryData) {
        } else {
          final String high2key = r.getString("HIGH2KEY");
          final String low2key = r.getString("LOW2KEY");
          boolean hasKeyValue =
              (high2key != null
                  && !high2key.trim().equals("")
                  && low2key != null
                  && !low2key.trim().equals("")
                  && !high2key.equals(low2key));

          if (!"".equals(high2key)) {
            stats =
                new CatalogStatistics(
                    "HIGH2KEY",
                    LUWCatalogMessages.STAT_HIGH2KEY,
                    LUWCatalogMessages.STAT_HIGH2KEY_DES,
                    LUWUtil.getSingleQuotedString(high2key),
                    hasKeyValue ? "SYSSTAT.COLUMNS" : "");
            statistics.add(stats);
          }

          if (!"".equals(low2key)) {
            stats =
                new CatalogStatistics(
                    "LOW2KEY",
                    LUWCatalogMessages.STAT_LOW2KEY,
                    LUWCatalogMessages.STAT_LOW2KEY_DES,
                    LUWUtil.getSingleQuotedString(low2key),
                    hasKeyValue ? "SYSSTAT.COLUMNS" : "");
            statistics.add(stats);
          }

          final int avgLen = r.getInt("AVGCOLLEN");
          if (avgLen != -1) {
            stats =
                new CatalogStatistics(
                    "AVGCOLLEN",
                    LUWCatalogMessages.STAT_AVG_COLLEN,
                    LUWCatalogMessages.STAT_AVG_COLLEN_DES,
                    avgLen,
                    "SYSSTAT.COLUMNS");
            statistics.add(stats);
          }
        }
        final BigInteger numNulls = r.getBigDecimal("NUMNULLS").toBigInteger();
        ;
        if (numNulls.intValue() != -1) {
          stats =
              new CatalogStatistics(
                  "NUMNULLS",
                  LUWCatalogMessages.STAT_NUMNULLS,
                  LUWCatalogMessages.STAT_NUMNULLS_DES,
                  numNulls,
                  "SYSSTAT.COLUMNS");
          statistics.add(stats);
        }

        final int subCount = r.getInt("SUB_COUNT");
        if (subCount != -1) {
          stats =
              new CatalogStatistics(
                  "SUB_COUNT",
                  LUWCatalogMessages.STAT_SUB_COUNT,
                  LUWCatalogMessages.STAT_SUB_COUNT_DES,
                  subCount,
                  "SYSSTAT.COLUMNS");
          statistics.add(stats);
        }

        final int subDelimLen = r.getInt("SUB_DELIM_LENGTH");
        if (subDelimLen != -1) {
          stats =
              new CatalogStatistics(
                  "SUB_DELIM_LENGTH",
                  LUWCatalogMessages.STAT_SUB_DELIM_LENGTH,
                  LUWCatalogMessages.STAT_SUB_DELIM_LENGTH_DES,
                  subDelimLen,
                  "SYSSTAT.COLUMNS");
          statistics.add(stats);
        }
      }
      r.close();
      s.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return statistics;
  }