public void refreshFromDB(String newName) {
    if (_mainObject.getName().equals(newName)) {
      if (_mainObject instanceof ICatalogObject) {
        if (_mainObject instanceof SybaseASABaseUserDefinedType) {
          Schema schema = ((UserDefinedType) _mainObject).getSchema();
          // schema may lost
          if (schema == null) {
            _mainObject = null;
            return;
          }
        }
        ((ICatalogObject) _mainObject).refresh();
        return;
      }
    }

    if (!isModelExist()) {
      _mainObject = null;
      return;
    }

    boolean isFound = false;
    if (_mainObject instanceof SybaseASABaseUserDefinedType) {
      Schema schema = ((SybaseASABaseUserDefinedType) _mainObject).getSchema();
      // schema may lost
      if (schema != null) {
        //                DSEUtil.refreshObjectBySchema(schema, _mainObject);

        EList udds = schema.getUserDefinedTypes();
        for (Iterator iter = udds.iterator(); iter.hasNext(); ) {
          SybaseASABaseUserDefinedType asaUDD = (SybaseASABaseUserDefinedType) iter.next();
          if (asaUDD.getName().equals(_mainObject.getName())) {
            _mainObject = asaUDD;
            isFound = true;
            break;
          }
        }

        if (!isFound) {
          for (Iterator iter = udds.iterator(); iter.hasNext(); ) {
            SybaseASABaseUserDefinedType asaUDD = (SybaseASABaseUserDefinedType) iter.next();
            if (asaUDD.getName().equals(newName)) {
              _mainObject = asaUDD;
              isFound = true;
              break;
            }
          }
        }
      }
    }
    if (!isFound) {
      _mainObject = null;
    }
  }
  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);
  }
Esempio n. 4
0
 /**
  *
  * <!-- begin-user-doc -->
  * <!-- end-user-doc -->
  *
  * @generated
  */
 public Schema getSchema() {
   if (schema != null && schema.eIsProxy()) {
     InternalEObject oldSchema = (InternalEObject) schema;
     schema = (Schema) eResolveProxy(oldSchema);
     if (schema != oldSchema) {
       if (eNotificationRequired())
         eNotify(
             new ENotificationImpl(
                 this, Notification.RESOLVE, SQLTablesPackage.TABLE__SCHEMA, oldSchema, schema));
     }
   }
   return schema;
 }
  public void testGetSchemas() {
    // Legacy code Database contains Schemas directly, no catalog
    Database db = SQLSchemaFactoryImpl.eINSTANCE.createDatabase();
    db.setName("database");
    Schema s1 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s1.setName("s1");
    Schema s2 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s2.setName("s2");
    db.getSchemas().add(s1);
    db.getSchemas().add(s2);

    EList ss = ModelUtil.getSchemas(db, null);
    assertTrue(ss.size() == 2);

    // Both Database.getSchemas and Catalog.getSchemas exist, return all
    db = SQLSchemaFactoryImpl.eINSTANCE.createDatabase();
    db.setName("database");
    s1 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s1.setName("s1");
    s2 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s2.setName("s2");
    db.getSchemas().add(s1);
    db.getSchemas().add(s2);
    Catalog cat1 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat1.setName("cat1");
    db.getCatalogs().add(cat1);
    cat1.getSchemas().add(s1);
    Catalog cat2 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat2.setName("cat2");
    db.getCatalogs().add(cat2);
    cat2.getSchemas().add(s2);

    ss = ModelUtil.getSchemas(db, null);
    assertTrue(ss.size() == 2);

    // Both Database.getSchemas and Catalog.getSchemas exist, but only the latter is valid
    db = SQLSchemaFactoryImpl.eINSTANCE.createDatabase();
    db.setName("database");
    s1 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s1.setName("s1");
    s2 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s2.setName("s2");
    Schema s3 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s3.setName("");
    db.getSchemas().add(s3);
    cat1 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat1.setName("cat1");
    db.getCatalogs().add(cat1);
    cat1.getSchemas().add(s1);
    cat2 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat2.setName("cat2");
    db.getCatalogs().add(cat2);
    cat2.getSchemas().add(s2);

    ss = ModelUtil.getSchemas(db, null);
    assertTrue(ss.size() == 2);

    // No Database.getSchemas, use catalogName null to retrieve all schemas
    db = SQLSchemaFactoryImpl.eINSTANCE.createDatabase();
    db.setName("database");
    s1 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s1.setName("s1");
    s2 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s2.setName("s2");
    cat1 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat1.setName("cat1");
    db.getCatalogs().add(cat1);
    cat1.getSchemas().add(s1);
    cat2 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat2.setName("cat2");
    db.getCatalogs().add(cat2);
    cat2.getSchemas().add(s2);

    ss = ModelUtil.getSchemas(db, null);
    assertTrue(ss.size() == 2);

    // No Database.getSchemas, catalog is a dummy node, use catalogName "" to retrieve all schemas
    db = SQLSchemaFactoryImpl.eINSTANCE.createDatabase();
    db.setName("database");
    s1 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s1.setName("s1");
    s2 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s2.setName("s2");
    cat1 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat1.setName("");
    db.getCatalogs().add(cat1);
    cat1.getSchemas().add(s1);
    cat1.getSchemas().add(s2);

    ss = ModelUtil.getSchemas(db, "");
    assertTrue(ss.size() == 2);

    // No Database.getSchemas, use catalogName to retrieve all schemas for a specific catalog
    db = SQLSchemaFactoryImpl.eINSTANCE.createDatabase();
    db.setName("database");
    s1 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s1.setName("s1");
    s2 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s2.setName("s2");
    cat1 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat1.setName("cat1");
    db.getCatalogs().add(cat1);
    cat1.getSchemas().add(s1);
    cat2 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat2.setName("cat2");
    db.getCatalogs().add(cat2);
    cat2.getSchemas().add(s2);

    ss = ModelUtil.getSchemas(db, "cat1");
    assertTrue(ss.size() == 1);
    assertEquals(((Schema) ss.get(0)).getName(), "s1");

    // No Database.getSchemas, and Database only has a dummy catalog,
    // use catalogName to retrieve all schemas for a specific catalog.
    // This is for backward compatibility
    db = SQLSchemaFactoryImpl.eINSTANCE.createDatabase();
    db.setName("database");
    s1 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s1.setName("s1");
    s2 = SQLSchemaFactoryImpl.eINSTANCE.createSchema();
    s2.setName("s2");
    cat1 = SQLSchemaFactoryImpl.eINSTANCE.createCatalog();
    cat1.setName("");
    db.getCatalogs().add(cat1);
    cat1.getSchemas().add(s1);
    cat1.getSchemas().add(s2);

    ss = ModelUtil.getSchemas(db, "database");
    assertTrue(ss.size() == 2);
  }