Package nexj.core.meta.persistence.sql

Examples of nexj.core.meta.persistence.sql.RelationalSchema


   /**
    * @see nexj.core.meta.persistence.sql.upgrade.RelationalSchemaUpgradeStep#apply(nexj.core.meta.persistence.sql.upgrade.RelationalSchemaUpgradeState)
    */
   public void apply(RelationalSchemaUpgradeState state)
   {
      final RelationalSchema schema = state.getSchema();
      final Table table = schema.getTable(m_sTableName);

      m_index = new Index(m_outline.getName(), m_outline.getType(), table);
      m_outline.copyTo(m_index);
      table.addIndex(m_index);

      new RelationalSchemaAspectManager(schema)
      {
         protected Iterator getTableAspectIterator()
         {
            return schema.getTableIterator();
         }

         protected Iterator getTablePointcutIterator()
         {
            return EmptyIterator.getInstance();
         }

         protected Iterator getIndexAspectIterator()
         {
            return schema.getIndexIterator();
         }

         protected Iterator getIndexPointcutIterator()
         {
            return Collections.singletonList(m_index).iterator();
         }
      }.applyAspects(2);

      if (table.isAspect())
      {
         if (!m_index.isAspect())
         {
            for (Iterator itr = schema.getTableIterator(); itr.hasNext();)
            {
               Table pointcut = (Table)itr.next();

               if (pointcut.hasAspect(table))
               {
                  Index index = new Index(m_index.getName(pointcut), m_outline.getType(), table);

                  m_outline.copyTo(index);
                  pointcut.addIndex(index);

                  if (m_outline.getRelatedTableName() != null)
                  {
                     schema.getTable(m_outline.getRelatedTableName()).addRelatedKey(index);
                  }
               }
            }
         }
      }
      else
      {
         if (m_outline.getRelatedTableName() != null)
         {
            schema.getTable(m_outline.getRelatedTableName()).addRelatedKey(m_index);
         }
      }
   }
View Full Code Here


   public void undo(RelationalSchemaUpgradeState state)
   {
      if (!state.removeIndex(m_outline.getName()) &&
         !state.containsTable(m_sTableName))
      {
         RelationalSchema schema = state.getSchema();
         Index index = schema.getIndex(m_outline.getName());

         schema.removeIndex(index);

         if (!index.isAspect() && index.getTable().isAspect())
         {
            for (Iterator itr = schema.getTableIterator(); itr.hasNext();)
            {
               Table pointcut = (Table)itr.next();

               if (pointcut.hasAspect(index.getTable()))
               {
                  String sName = index.getName(pointcut);

                  if (!state.removeIndex(sName))
                  {
                     schema.removeIndex(pointcut.getIndex(sName));
                  }
               }
            }
         }
      }
View Full Code Here

    * Appends SQL for applying a table aspect to the SQL appender and upgrades the metadata.
    * @param step The apply table aspect step.
    */
   protected void applyTableAspect(ApplyTableAspectStep step)
   {
      RelationalSchema schema = m_state.getSchema();
      List/*<Table>*/ tableList = new ArrayList/*<Table>*/();

      for (Iterator itr = step.getPointcutIterator(schema.getTableIterator());
           itr.hasNext();
           tableList.add(((Table)itr.next()).cloneTemporary()));

      step.apply(m_state);

      SQLScriptHolder holder = step.getScriptHolder();

      for (int i = 0, nCount = tableList.size(); i < nCount; ++i)
      {
         Table current = (Table)tableList.get(i);
         Table target = schema.getTable(current.getName());

         createColumns(target, current, false); // create nullable columns
         execSQL(holder, current); // execute any defined scripts
         alterColumns(target, current); // set null constraint
         createIndexes(target, current); // create missing indexes
View Full Code Here

   /**
    * @see nexj.core.meta.persistence.sql.upgrade.RelationalSchemaUpgradeStep#apply(nexj.core.meta.persistence.sql.upgrade.RelationalSchemaUpgradeState)
    */
   public void apply(RelationalSchemaUpgradeState state)
   {
      final RelationalSchema schema = state.getSchema();

      m_table = new Table(schema);

      m_table.setType(m_nType);
      m_table.setName(m_sName);
      m_table.setQuotedName(m_sAlias);
      m_table.setTablespaceName(m_sTablespaceName);
      m_table.setIndexspaceName(m_sIndexspaceName);
      m_table.setLongspaceName(m_sLongspaceName);

      schema.addTable(m_table);

      for (Lookup.Iterator itr = m_columnMap.valueIterator(); itr.hasNext();)
      {
         ColumnOutline outline = (ColumnOutline)itr.next();
         Column column = new Column(outline.getName(), m_table);

         outline.copyTo(column);
         m_table.addColumn(column);
      }

      m_table.setViewScript(m_viewScript);
      m_table.setViewAutoUpdated(m_bViewAutoUpdated);
      addAspects(schema, m_table);

      RelationalSchemaAspectManager aspectManager = new RelationalSchemaAspectManager(schema)
      {
         protected Iterator getTableAspectIterator()
         {
            return schema.getTableIterator();
         }

         protected Iterator getTablePointcutIterator()
         {
            return Collections.singletonList(m_table).iterator();
         }

         protected Iterator getIndexAspectIterator()
         {
            return schema.getIndexIterator();
         }

         protected Iterator getIndexPointcutIterator()
         {
            return m_table.getIndexIterator();
         }
      };

      aspectManager.applyAspects(0);

      for (Lookup.Iterator itr = m_indexMap.valueIterator(); itr.hasNext();)
      {
         IndexOutline outline = (IndexOutline)itr.next();
         Index index = new Index(outline.getName(), outline.getType(), m_table);

         outline.copyTo(index);
         m_table.addIndex(index);
      }

      aspectManager.applyAspects(1);

      for (Lookup.Iterator itr = m_indexMap.valueIterator(); itr.hasNext();)
      {
         IndexOutline outline = (IndexOutline)itr.next();
         Index index = m_table.getIndex(outline.getName());

         if (outline.getRelatedTableName() != null)
         {
            schema.getTable(outline.getRelatedTableName()).addRelatedKey(index);
         }
      }

      aspectManager.applyAspects(2);

      if (m_hintList != null)
      {
         for (int i = 0, nCount = m_hintList.size(); i < nCount; ++i)
         {
            m_table.addHint(m_hintList.get(i).toString());
         }
      }

      if (m_sPrimaryKeyName != null)
      {
         m_table.setPrimaryKey(schema.getIndex(m_sPrimaryKeyName));
      }

      m_table.computePrimaryKeyParts();
      m_table.validate(state.getSchema().getMetadata(), null);
   }
View Full Code Here

    * Appends SQL for removing a table aspect to the SQL appender and upgrades the metadata.
    * @param step The remove table aspect step.
    */
   protected void removeTableAspect(RemoveTableAspectStep step)
   {
      RelationalSchema schema = m_state.getSchema();
      List/*<Table>*/ tableList = new ArrayList/*<Table>*/();

      for (Iterator itr = step.getPointcutIterator(schema.getTableIterator());
           itr.hasNext();
           tableList.add(((Table)itr.next()).cloneTemporary()));

      step.apply(m_state);

      for (int i = 0, nCount = tableList.size(); i < nCount; ++i)
      {
         Table current = (Table)tableList.get(i);
         Table target = schema.getTable(current.getName());

         dropIndexes(target, current); // drop extra indexes
         dropColumns(target, current); // drop extra columns
         createIndexes(target, current); // recreate any indexes that had the dropped column
      }
View Full Code Here

   /**
    * @see nexj.core.meta.persistence.sql.upgrade.RelationalSchemaUpgradeStep#undo(nexj.core.meta.persistence.sql.upgrade.RelationalSchemaUpgradeState)
    */
   public void undo(RelationalSchemaUpgradeState state)
   {
      final RelationalSchema schema = state.getSchema();

      if (!state.removeTable(m_sName))
      {
         schema.removeTable(schema.getTable(m_sName));
      }

      for (Iterator itr = m_indexMap.iterator(); itr.hasNext();)
      {
         state.removeIndex((String)itr.next());
      }

      // Apply all table-aspects to the table to get full list of indexes coming from aspects
      final Table pointcut = new Table(new RelationalSchema());
      AspectManager aspectManager = new AspectManager()
      {
         // override to avoid logging
         public void applyAspects(int nPass) throws MetadataException
         {
            if (nPass == 0)
            {
               addAspects();
            }

            for (int i = 0, n = pointcut.getAspectCount(); i < n; ++i)
            {
               Aspect aspect = pointcut.getAspect(i);

               if (m_aspectNameSet.contains(aspect.getName()))
               {
                  aspect.applyTo(pointcut, nPass);
               }
            }
         }

         protected Iterator getAspectIterator()
         {
            return schema.getTableIterator();
         }

         protected Iterator getPointcutIterator()
         {
            return Collections.singletonList(pointcut).iterator();
View Full Code Here

    * Appends SQL for creating a column to the SQL appender and upgrades the metadata.
    * @param step The create column step.
    */
   protected void createColumn(CreateColumnStep step)
   {
      RelationalSchema schema = m_state.getSchema();
      Table table = schema.getTable(step.getTableName()); // table in the column is fully updated
      SQLScriptHolder holder = step.getScriptHolder();

      if (table.isAspect())
      {
         int nEnd = 0;
         Table[] tableArray = getSortedTables(schema);

         for (int i = 0, nCount = tableArray.length; i < nCount; ++i)
         {
            if (tableArray[i].hasAspect(table))
            {
               tableArray[nEnd++] = tableArray[i].cloneTemporary();
            }
         }

         step.apply(m_state);

         for (int i = 0; i < nEnd; ++i)
         {
            Table current = tableArray[i];
            Table target = schema.getTable(current.getName());

            createColumns(target, current, false); // create nullable columns
            execSQL(holder, current); // execute any defined scripts
            alterColumns(target, current); // set null constraint
         }
      }
      else
      {
         Table current = table.cloneTemporary();

         step.apply(m_state);
         table = schema.getTable(step.getTableName()); // refresh table from schema, required!
         createColumns(table, current, false); // create nullable columns
         execSQL(holder, current); // execute any defined scripts
         alterColumns(table, current);
      }
   }
View Full Code Here

    * Appends SQL for altering a column to the SQL appender and upgrades the metadata.
    * @param step The alter column step.
    */
   protected void alterColumn(AlterColumnStep step)
   {
      RelationalSchema schema = m_state.getSchema();
      Table table = schema.getTable(step.getTableName());

      if (table.isAspect())
      {
         int nEnd = 0;
         Table[] tableArray = getSortedTables(schema);

         for (int i = 0, nCount = tableArray.length; i < nCount; ++i)
         {
            if (tableArray[i].hasAspect(table))
            {
               tableArray[nEnd++] = tableArray[i].cloneTemporary();
            }
         }

         step.apply(m_state);

         for (int i = 0; i < nEnd; ++i)
         {
            Table current = tableArray[i];

            alterColumns(schema.getTable(current.getName()), current);
         }
      }
      else
      {
         Table current = table.cloneTemporary();
View Full Code Here

    * Appends SQL for renaming a column to the SQL appender and upgrades the metadata.
    * @param step The rename column step.
    */
   protected void renameColumn(RenameColumnStep step)
   {
      RelationalSchema schema = m_state.getSchema();
      Table table = schema.getTable(step.getTableName());

      if (table.isAspect())
      {
         List oldColumnList = new ArrayList();
         Table[] tableArray = getSortedTables(schema);

         for (int i = 0, nCount = tableArray.length; i < nCount; ++i)
         {
            Table pointcut = tableArray[i];

            if (pointcut.hasAspect(table))
            {
               oldColumnList.add(pointcut.cloneTemporary().getColumn(step.getOldName()));
            }
         }

         step.apply(m_state);

         for (int i = 0, n = oldColumnList.size(); i < n; ++i)
         {
            Column oldColumn = (Column)oldColumnList.get(i);

            renameColumn(schema.getTable(oldColumn.getTable().getName()).getColumn(step.getNewName()), oldColumn);
         }
      }
      else
      {
         Column oldColumn = table.cloneTemporary().getColumn(step.getOldName());
View Full Code Here

    * Appends SQL for dropping a column to the SQL appender and upgrades the metadata.
    * @param step The drop column step.
    */
   protected void dropColumn(DropColumnStep step)
   {
      RelationalSchema schema = m_state.getSchema();
      Table table = schema.getTable(step.getTableName());

      if (table.isAspect())
      {
         int nEnd = 0;
         Table[] tableArray = getSortedTables(schema);

         for (int i = 0, nCount = tableArray.length; i < nCount; ++i)
         {
            if (tableArray[i].hasAspect(table))
            {
               tableArray[nEnd++] = tableArray[i].cloneTemporary();
            }
         }

         step.apply(m_state);

         for (int i = 0; i < nEnd; ++i)
         {
            Table current = tableArray[i];

            dropColumns(schema.getTable(current.getName()), current);
         }
      }
      else
      {
         Table current = table.cloneTemporary();
View Full Code Here

TOP

Related Classes of nexj.core.meta.persistence.sql.RelationalSchema

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.