/**
  * INTERNAL: Convert all the class-name-based settings in this InheritancePolicy to actual
  * class-based settings. This method is used when converting a project that has been built with
  * class names to a project with classes. It will also convert referenced classes to the versions
  * of the classes from the classLoader.
  */
 public void convertClassNamesToClasses(ClassLoader classLoader) {
   Vector newParentInterfaces = new Vector();
   for (Iterator iterator = getParentInterfaceNames().iterator(); iterator.hasNext(); ) {
     String interfaceName = (String) iterator.next();
     Class interfaceClass = null;
     try {
       if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
         try {
           interfaceClass =
               (Class)
                   AccessController.doPrivileged(
                       new PrivilegedClassForName(interfaceName, true, classLoader));
         } catch (PrivilegedActionException exception) {
           throw ValidationException.classNotFoundWhileConvertingClassNames(
               interfaceName, exception.getException());
         }
       } else {
         interfaceClass =
             org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(
                 interfaceName, true, classLoader);
       }
     } catch (ClassNotFoundException exc) {
       throw ValidationException.classNotFoundWhileConvertingClassNames(interfaceName, exc);
     }
     newParentInterfaces.add(interfaceClass);
   }
   this.parentInterfaces = newParentInterfaces;
 }
 /** INTERNAL: Return the create table statement. */
 public Writer buildCreationWriter(AbstractSession session, Writer writer)
     throws ValidationException {
   try {
     DatabasePlatform platform = session.getPlatform();
     writer.write("CREATE PACKAGE " + getFullName());
     writer.write(" AS");
     writer.write("\n");
     for (Enumeration statementsEnum = getStatements().elements();
         statementsEnum.hasMoreElements(); ) {
       writer.write((String) statementsEnum.nextElement());
       writer.write(platform.getBatchDelimiterString());
       writer.write("\n");
     }
     for (Enumeration proceduresEnum = getProcedures().elements();
         proceduresEnum.hasMoreElements(); ) {
       writer.write("\n");
       String procedureString =
           ((StoredProcedureDefinition) proceduresEnum.nextElement())
               .buildCreationWriter(session, writer)
               .toString();
       writer.write(procedureString.substring(7, procedureString.length()));
       writer.write("\n");
     }
     writer.write(platform.getBatchEndString());
     writer.write("\n" + session.getPlatform().getStoredProcedureTerminationToken());
   } catch (IOException ioException) {
     throw ValidationException.fileError(ioException);
   }
   return writer;
 }
Ejemplo n.º 3
0
 /**
  * INTERNAL: Return the database platform specific information. This allows TopLink to configure
  * certain advanced features for the database desired. NOTE: this must only be used for relational
  * specific usage and will not work for non-relational datasources.
  */
 public DatabasePlatform getPlatform() {
   try {
     return (DatabasePlatform) getDatasourcePlatform();
   } catch (ClassCastException wrongType) {
     throw ValidationException.notSupportedForDatasource();
   }
 }
 /** INTERNAL: Return the drop type statement. */
 public Writer buildDeletionWriter(AbstractSession session, Writer writer)
     throws ValidationException {
   try {
     writer.write("DROP TYPE " + getFullName());
   } catch (IOException ioException) {
     throw ValidationException.fileError(ioException);
   }
   return writer;
 }
Ejemplo n.º 5
0
 /** PUBLIC: Add the field-value pair to the row. */
 public Object put(Object key, Object value) throws ValidationException {
   if (key instanceof String) {
     return put((String) key, value);
   } else if (key instanceof DatabaseField) {
     return put((DatabaseField) key, value);
   } else {
     throw ValidationException.onlyFieldsAreValidKeysForDatabaseRows();
   }
 }
Ejemplo n.º 6
0
 /**
  * INTERNAL: Set the name of the Platform to be used. Creates a new instance of the specified
  * Class.
  */
 public void setPlatformClassName(String platformClassName) throws ValidationException {
   Class platformClass = null;
   try {
     // First try loading with the Login's class loader
     platformClass = this.getClass().getClassLoader().loadClass(platformClassName);
     Platform platform = null;
     if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
       try {
         platform =
             (Platform)
                 AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(platformClass));
       } catch (PrivilegedActionException exception) {
         throw exception.getException();
       }
     } else {
       platform = (Platform) PrivilegedAccessHelper.newInstanceFromClass(platformClass);
     }
     usePlatform(platform);
   } catch (Exception cne) {
     // next try using ConversionManager
     try {
       platformClass = ConversionManager.loadClass(platformClassName);
       Platform platform = null;
       if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
         try {
           platform =
               (Platform)
                   AccessController.doPrivileged(
                       new PrivilegedNewInstanceFromClass(platformClass));
         } catch (PrivilegedActionException exception) {
           throw ValidationException.platformClassNotFound(
               exception.getException(), platformClassName);
         }
       } else {
         platform = (Platform) PrivilegedAccessHelper.newInstanceFromClass(platformClass);
       }
       usePlatform(platform);
     } catch (Exception cne2) {
       // if still not found, throw exception
       throw ValidationException.platformClassNotFound(cne2, platformClassName);
     }
   }
 }
 /** INTERNAL: Return the SQL to delete the row from the sequence table. */
 public Writer buildDeletionWriter(AbstractSession session, Writer writer)
     throws ValidationException {
   try {
     writer.write("DELETE FROM ");
     writer.write(getSequenceTableName());
     writer.write(" WHERE " + getSequenceNameFieldName());
     writer.write(" = '" + getName() + "'");
   } catch (IOException ioException) {
     throw ValidationException.fileError(ioException);
   }
   return writer;
 }
  /** Append the string containing the SQL insert string for the given table. */
  protected SQLCall buildCallWithoutReturning(AbstractSession session) {
    SQLCall call = new SQLCall();
    call.returnNothing();

    Writer writer = new CharArrayWriter(200);
    try {
      writer.write("INSERT ");
      if (getHintString() != null) {
        writer.write(getHintString());
        writer.write(" ");
      }
      writer.write("INTO ");
      writer.write(getTable().getQualifiedNameDelimited(session.getPlatform()));
      writer.write(" (");

      Vector fieldsForTable = new Vector();
      for (Enumeration fieldsEnum = getModifyRow().keys(); fieldsEnum.hasMoreElements(); ) {
        DatabaseField field = (DatabaseField) fieldsEnum.nextElement();
        if (field.getTable().equals(getTable()) || (!field.hasTableName())) {
          fieldsForTable.addElement(field);
        }
      }

      if (fieldsForTable.isEmpty()) {
        throw QueryException.objectToInsertIsEmpty(getTable());
      }

      for (int i = 0; i < fieldsForTable.size(); i++) {
        writer.write(
            ((DatabaseField) fieldsForTable.elementAt(i)).getNameDelimited(session.getPlatform()));
        if ((i + 1) < fieldsForTable.size()) {
          writer.write(", ");
        }
      }
      writer.write(") VALUES (");

      for (int i = 0; i < fieldsForTable.size(); i++) {
        DatabaseField field = (DatabaseField) fieldsForTable.elementAt(i);
        call.appendModify(writer, field);
        if ((i + 1) < fieldsForTable.size()) {
          writer.write(", ");
        }
      }
      writer.write(")");

      call.setSQLString(writer.toString());
    } catch (IOException exception) {
      throw ValidationException.fileError(exception);
    }
    return call;
  }
 /**
  * INTERNAL: Return the SQL required to insert the sequence row into the sequence table. Assume
  * that the sequence table exists.
  */
 public Writer buildCreationWriter(AbstractSession session, Writer writer)
     throws ValidationException {
   try {
     writer.write("INSERT INTO ");
     writer.write(getSequenceTableName());
     writer.write("(" + getSequenceNameFieldName());
     writer.write(", " + getSequenceCounterFieldName());
     writer.write(") values (");
     writer.write(
         "'" + getName() + "', " + Integer.toString(sequence.getInitialValue() - 1) + ")");
   } catch (IOException ioException) {
     throw ValidationException.fileError(ioException);
   }
   return writer;
 }
 /** INTERNAL: Return the create type statement. */
 public Writer buildCreationWriter(AbstractSession session, Writer writer)
     throws ValidationException {
   try {
     writer.write("CREATE TYPE " + getFullName() + " AS OBJECT (");
     for (Enumeration fieldsEnum = getFields().elements(); fieldsEnum.hasMoreElements(); ) {
       FieldDefinition field = (FieldDefinition) fieldsEnum.nextElement();
       field.appendTypeString(writer, session);
       if (fieldsEnum.hasMoreElements()) {
         writer.write(", ");
       }
     }
     writer.write(")");
   } catch (IOException ioException) {
     throw ValidationException.fileError(ioException);
   }
   return writer;
 }
  /** Append the string containing the SQL insert string for the given table. */
  public DatabaseCall buildCall(AbstractSession session) {
    SQLCall call = new SQLCall();
    call.returnNothing();

    Writer writer = new CharArrayWriter(100);

    Vector mainPrimaryKeys = new Vector();
    mainPrimaryKeys.addAll((Collection) tablesToPrimaryKeyFields.get(table));

    Vector allFields = (Vector) mainPrimaryKeys.clone();
    Iterator itDatabaseFieldsToValues = tables_databaseFieldsToValues.values().iterator();
    while (itDatabaseFieldsToValues.hasNext()) {
      Iterator itDatabaseFields = ((HashMap) itDatabaseFieldsToValues.next()).keySet().iterator();
      while (itDatabaseFields.hasNext()) {
        allFields.addElement(itDatabaseFields.next());
      }
    }

    try {
      // DECLARE
      writer.write("DECLARE\n");

      for (int i = 0; i < allFields.size(); i++) {
        writeDeclareTypeAndVar(
            writer, (DatabaseField) allFields.elementAt(i), session.getPlatform());
      }

      // BEGIN
      writer.write("BEGIN\n");

      //  select t0.emp_id, concat('Even', t0.f_name), t1.salary + 1000 BULK COLLECT into
      // EMPLOYEEE_EMP_ID_VAR, EMPLOYEEE_F_NAME_VAR, SALARY_SALARY_VAR from employee t0, salary t1
      // where t0.l_name like 'updateEmployeeTestUsingTempTable' and t0.f_name in ('0', '2') and
      // t1.salary = 0 and t0.emp_id = t1.emp_id;
      String selectStr = selectCall.getSQLString();
      int index = selectStr.toUpperCase().indexOf(" FROM ");
      String firstPart = selectStr.substring(0, index);
      String secondPart = selectStr.substring(index, selectStr.length());

      writer.write(tab);
      writer.write(firstPart);
      writer.write(" BULK COLLECT INTO ");

      for (int i = 0; i < allFields.size(); i++) {
        writeVar(writer, (DatabaseField) allFields.elementAt(i), session.getPlatform());
        if (i < allFields.size() - 1) {
          writer.write(", ");
        }
      }
      writer.write(secondPart);
      writer.write(";\n");

      call.getParameters().addAll(selectCall.getParameters());
      call.getParameterTypes().addAll(selectCall.getParameterTypes());

      DatabaseField firstMainPrimaryKey = (DatabaseField) mainPrimaryKeys.firstElement();
      writer.write(tab);
      writer.write("IF ");
      writeVar(writer, firstMainPrimaryKey, session.getPlatform());
      writer.write(".COUNT > 0 THEN\n");

      Iterator itEntries = tables_databaseFieldsToValues.entrySet().iterator();
      while (itEntries.hasNext()) {
        writeForAll(writer, firstMainPrimaryKey, session.getPlatform());
        writer.write(trpltab);
        writer.write("UPDATE ");
        Map.Entry entry = (Map.Entry) itEntries.next();
        DatabaseTable t = (DatabaseTable) entry.getKey();
        writer.write(t.getQualifiedNameDelimited(session.getPlatform()));
        writer.write(" SET ");
        HashMap databaseFieldsToValues = (HashMap) entry.getValue();
        int counter = 0;
        Iterator itDatabaseFields = databaseFieldsToValues.keySet().iterator();
        while (itDatabaseFields.hasNext()) {
          counter++;
          DatabaseField field = (DatabaseField) itDatabaseFields.next();
          writer.write(field.getNameDelimited(session.getPlatform()));
          writer.write(" = ");
          writeVar(writer, field, session.getPlatform());
          writer.write("(i)");
          if (counter < databaseFieldsToValues.size()) {
            writer.write(", ");
          }
        }

        writer.write(" WHERE ");

        Vector tablePrimaryKeys = new Vector();
        tablePrimaryKeys.addAll((Collection) tablesToPrimaryKeyFields.get(t));
        for (int i = 0; i < mainPrimaryKeys.size(); i++) {
          DatabaseField tableField = (DatabaseField) tablePrimaryKeys.elementAt(i);
          writer.write(tableField.getNameDelimited(session.getPlatform()));
          writer.write(" = ");
          DatabaseField mainField = (DatabaseField) mainPrimaryKeys.elementAt(i);
          writeVar(writer, mainField, session.getPlatform());
          writer.write("(i)");
          if (i < mainPrimaryKeys.size() - 1) {
            writer.write(" AND ");
          } else {
            writer.write(";\n");
          }
        }
      }

      writer.write(tab);
      writer.write("END IF;\n");

      writer.write(tab);
      DatabaseField outField = new DatabaseField("ROW_COUNT");
      outField.setType(Integer.class);
      call.appendOut(writer, outField);
      writer.write(" := ");
      writeVar(writer, firstMainPrimaryKey, session.getPlatform());
      writer.write(".COUNT;\n");

      writer.write("END;");

      call.setSQLString(writer.toString());

    } catch (IOException exception) {
      throw ValidationException.fileError(exception);
    }

    return call;
  }