Package nexj.core.meta.persistence.sql

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


         return false; // nIndexType not one of Index.* constants, hence invalid/absent
      }

      for (int i = 0, nCount = table.getIndexCount(); i < nCount; ++i)
      {
         Index index = table.getIndex(i);

         if (index.getType() == nIndexType && sub.findIndex(index.getName()) == null)
         {
            return true;
         }
      }

View Full Code Here


      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);
         }
View Full Code Here

    * @see nexj.core.persistence.sql.SQLAdapter#getMatchJoin(nexj.core.meta.persistence.sql.Column, nexj.core.scripting.Pair)
    */
   public Table getMatchJoin(Column column, Pair expression)
   {
      Table table = new Table(null);
      Index primary = new Index(table);
      Table srcTable = column.getTable();
      Index srcPK = srcTable.getPrimaryKey(); // it better exist or there's no way to join

      table.setType(Table.EXTERNAL); // has to be set EXTERNAL or setQuotedName() fails
      table.setQuotedName(srcTable.getFullName(null, "$", true));
      table.setPrimaryKey(primary);

      // reuse PrimaryKey columns since they are not modified
      for (int i = 0, nCount = srcPK.getIndexColumnCount(); i < nCount; ++i)
      {
         primary.addIndexColumn(new IndexColumn(srcPK.getIndexColumn(i).getColumn(), true));
      }

      return table;
   }
View Full Code Here

         }
      }

      for (Iterator indexItr = m_schema.getIndexIterator(); indexItr.hasNext();)
      {
         Index index = (Index)indexItr.next();

         if (m_final.findIndex(index.getName()) == null)
         {
            eh = addException(eh, new MetadataException("err.meta.upgrade.sql.extraIndex",
               new Object[]{index.getName(), index.getTable().getName(), m_final.getDataSource().getName()}));
         }
      }

      for (Iterator indexItrFinal = m_final.getIndexIterator(); indexItrFinal.hasNext();)
      {
         Index indexFinal = (Index)indexItrFinal.next();
         Index index = m_schema.findIndex(indexFinal.getName());

         if (index == null)
         {
            if (indexFinal.getType() >= Index.BTREE)
            {
               eh = addException(eh, new MetadataException("err.meta.upgrade.sql.missingIndex",
                  new Object[]{indexFinal.getName(), indexFinal.getTable().getName(), m_final.getDataSource().getName()}));
            }
         }
         else
         {
            if (!indexFinal.getTable().getName().equals(index.getTable().getName()))
            {
               eh = addException(eh, new MetadataException("err.meta.upgrade.sql.indexTableMismatch",
                  new Object[]{indexFinal.getTable().getName(), index.getTable().getName(),
                     indexFinal.getName(), m_final.getDataSource().getName()}));
            }

            if (indexFinal.getType() != index.getType())
            {
               eh = addException(eh, new MetadataException("err.meta.upgrade.sql.indexTypeMismatch",
                  new Object[]{indexFinal.getTypeString(), index.getTypeString(), indexFinal.getName(),
                     indexFinal.getTable().getName(), m_final.getDataSource().getName()}));
            }

            if (indexFinal.isUnique() != index.isUnique())
            {
               eh = addException(eh, new MetadataException("err.meta.upgrade.sql.indexUniquenessMismatch",
                  new Object[]{Boolean.valueOf(indexFinal.isUnique()), Boolean.valueOf(index.isUnique()),
                     indexFinal.getName(), indexFinal.getTable().getName(), m_final.getDataSource().getName()}));
            }

            if (indexFinal.getFill() != index.getFill())
            {
               eh = addException(eh, new MetadataException("err.meta.upgrade.sql.indexFillMismatch",
                  new Object[]{Primitive.createInteger(indexFinal.getFill()), Primitive.createInteger(index.getFill()),
                     indexFinal.getName(), indexFinal.getTable().getName(), m_final.getDataSource().getName()}));
            }

            String sRelatedTableFinal = (indexFinal.getRelatedTable() == null) ? "" : indexFinal.getRelatedTable().getName();
            String sRelatedTable = (index.getRelatedTable() == null) ? "" : index.getRelatedTable().getName();

            if (!sRelatedTableFinal.equals(sRelatedTable))
            {
               eh = addException(eh, new MetadataException("err.meta.upgrade.sql.indexRelatedTableMismatch",
                  new Object[]{sRelatedTableFinal, sRelatedTable, indexFinal.getName(),
                     indexFinal.getTable().getName(), m_final.getDataSource().getName()}));
            }

            for (int i = 0, n = index.getAspectCount(); i < n; ++i)
            {
               Aspect aspect = m_final.findIndex(index.getAspect(i).getName());

               if (aspect != null && !indexFinal.hasAspect(aspect))
               {
                  eh = addException(eh, new MetadataException("err.meta.upgrade.sql.extraIndexAspect",
                     new Object[]{aspect.getName(), indexFinal.getName(), indexFinal.getTable().getName(),
                        m_final.getDataSource().getName()}));
               }
            }

            for (int i = 0, n = indexFinal.getAspectCount(); i < n; ++i)
            {
               Aspect aspect = m_schema.findIndex(indexFinal.getAspect(i).getName());

               if (aspect != null && !index.hasAspect(aspect))
               {
                  eh = addException(eh, new MetadataException("err.meta.upgrade.sql.missingIndexAspect",
                     new Object[]{aspect.getName(), indexFinal.getName(), indexFinal.getTable().getName(),
                        m_final.getDataSource().getName()}));
               }
            }

            if (index.getType() != Index.VIRTUAL && index.getType() != Index.QUERY)
            {
               for (int i = 0, n = index.getIndexColumnCount(); i < n; ++i)
               {
                  IndexColumn indexColumn = index.getIndexColumn(i);
  
                  if (indexFinal.findIndexColumn(indexColumn.getColumn().getName()) == null)
                  {
                     eh = addException(eh, new MetadataException("err.meta.upgrade.sql.extraIndexColumn",
                        new Object[]{indexColumn.getColumn().getName(), index.getName(),
                           index.getTable().getName(), m_final.getDataSource().getName()}));
                  }
               }

               for (int i = 0, n = indexFinal.getIndexColumnCount(); i < n; ++i)
               {
                  IndexColumn indexColumnFinal = indexFinal.getIndexColumn(i);
                  IndexColumn indexColumn = index.findIndexColumn(indexColumnFinal.getColumn().getName());

                  if (indexColumn == null)
                  {
                     eh = addException(eh, new MetadataException("err.meta.upgrade.sql.missingIndexColumn",
                        new Object[]{indexColumnFinal.getColumn().getName(), indexFinal.getName(),
View Full Code Here

         public boolean visit(final Query query)
         {
            boolean bHomogeneous = (query.getRoot() == m_query);
            Query primarySource = query, fieldSource = null;
            SQLJoin primaryJoin, fieldJoin = null;
            Index primaryKey, fieldKey = null;

            if (query.isRoot() && bHomogeneous)
            {
               primaryJoin = addTable(query, ((RelationalMapping)query.getPersistenceMapping()).getPrimaryTable());
               primaryJoin.isInner = true;
               primaryJoin.isEnabled = true;
               primaryKey = primaryJoin.table.getPrimaryKey();

               if (query.isJoin())
               {
                  Index destinationKey = (Index)query.getKey(true);
                  int nDKColumnCount = destinationKey.getIndexColumnCount();
                  Field[] dkFieldArray = new Field[nDKColumnCount];
                  SQLJoin join = addTable(query, destinationKey.getTable());

                  join.isInner = true;
                  join.isEnabled = true;

                  for (int i = 0; i < nDKColumnCount; ++i)
                  {
                     Column column = destinationKey.getIndexColumn(i).getColumn();

                     addOutputField(dkFieldArray[i] = new Field(query, join, column,
                        column.getValueType(), m_adapter.getConverter(column.getValueType(), column),
                        m_adapter.getBind(column), true));
                  }

                  query.setChildItem(dkFieldArray);
               }
            }
            else
            {
               Index sourceKey = (Index)query.getKey(false);
               SQLJoin parent;

               if (sourceKey.isObjectKey())
               {
                  parent = (SQLJoin)query.getParent().getMapping();
                  sourceKey = parent.table.getPrimaryKey();
               }
               else
               {
                  parent = addTable(query.getParent(), sourceKey.getTable());
               }

               parent.isEnabled = true;

               if (bHomogeneous)
               {
                  Index destinationKey = (Index)query.getKey(true);
                  SQLJoin join = addTable(query, destinationKey.getTable());

                  join.parent = parent;
                  join.sourceKey = sourceKey;
                  join.destinationKey = destinationKey;

                  // Decide whether we can use the left joined table
                  // source key instead of the right joined table primary key
                  // to reduce the number of indirections in the SQL query
                  // TODO: Traverse the parent list up for more potential savings

                  if (query.getWhere() == null &&
                     (destinationKey.isObjectKey() &&
                        (!query.isOutput() ||
                           (!sourceKey.isObjectKey() || query.isRequired()) &&
                           query.getFieldCount() == 0 &&
                           query.getAssocCount(Query.ASSOC_QUERY) == 0) ||
                     destinationKey.isObjectKeyPart() &&
                        !query.isOutput() &&
                        query.getFieldCount() == 0 &&
                        query.getAssocCount(Query.ASSOC_QUERY) == 0))
                  {
                     primarySource = query.getParent();
                     primaryJoin = parent;
                     primaryKey = sourceKey;
                  }
                  else
                  {
                     primaryJoin = join;
                     primaryKey = join.table.getPrimaryKey();

                     if (destinationKey.isObjectKeyPart() &&
                        (!sourceKey.isObjectKey() || query.isRequired()))
                     {
                        fieldSource = query.getParent();
                        fieldJoin = parent;
                        fieldKey = sourceKey;
View Full Code Here

         m_nGenMode = GEN_ORDERBY;
         buf.append(" order by ");

         OrderByPrefix prefix = m_adapter.getOrderByPrefix(query);
         int nPrefixColumnCount = 0;
         Index index = null;
         Object mapping = null;
         boolean bSortDirectionReversed = false;

         if (prefix != null)
         {
            nPrefixColumnCount = prefix.getColumnCount();
            index = prefix.getIndex();
            mapping = prefix.getMapping();
            bSortDirectionReversed = prefix.isSortDirectionReversed();
            nCount += nPrefixColumnCount;
         }

         initOffsetArray(nCount);

         for (int i = 0; i < nCount; ++i)
         {
            if (i > 0)
            {
               buf.append(", ");
            }

            Operator op;
            boolean bAscending;

            if (i >= nPrefixColumnCount)
            {
               op = query.getOrderByOperator(i - nPrefixColumnCount);
               bAscending = query.isOrderByAscending(i - nPrefixColumnCount);
            }
            else
            {
               IndexColumn indexColumn = index.getIndexColumn(i);
               Column column = indexColumn.getColumn();
               Field field = new Field(query, mapping, column, column.getValueType(),
                  m_adapter.getConverter(column.getValueType(), column),
                  m_adapter.getBind(column), false);
View Full Code Here

    * @param table The table to create the Full-Text counterpart for.
    */
   protected void createTextTable(Table table)
   {
      StringBuffer columns = new StringBuffer(64);//used for populating Full-Text table(pk,a,b,...)
      Index primaryKey = table.getPrimaryKey();
      String sTable = table.getFullName(getOwner(), "$", true);
      StringBuffer buf = new StringBuffer(128);

      buf.append("create table ").append(sTable).append('(');

      for (int i = 0, nCount = primaryKey.getIndexColumnCount(); i < nCount; ++i) // add PK cols
      {
         Column col = table.getColumn(i);

         appendColumnDeclaration(buf, col, true, true, ", ");
         columns.append(col.getQuotedName());
View Full Code Here

    */
   protected void createTextTriggers(Table table, Index includeIndex, Index excludeIndex)
   {
      String sSrcTable = table.getFullName(getOwner(), null, true);
      String sFtsTable = table.getFullName(getOwner(), "$", true);
      Index primaryKey = table.getPrimaryKey();

      if (primaryKey == excludeIndex)
      {
         return; // invalid trigger action
      }

      StringBuffer changedColumns = new StringBuffer(64); // used for trigger SQL (new.pk,new.a,)
      StringBuffer listingColumns = new StringBuffer(64); // used for trigger SQL (pk,a,)
      StringBuffer matchedColumns = new StringBuffer(64); // used for trigger SQL (pk=old.pk and)
      StringBuffer updatedColumns = new StringBuffer(64); // used for trigger SQL (a=new.a,)

      // determine column listings transfered to Full-Text table
      for (int i = 0, nCount = table.getIndexCount(); i < nCount; ++i)
      {
         Index idx = table.getIndex(i);

         if (idx.getType() == Index.TEXT && idx != excludeIndex &&
             (includeIndex == null || idx == includeIndex))
         {
            Column col = idx.getIndexColumn(0).getColumn();

            if (primaryKey.findIndexColumn(col) == null) // add PK columns later
            {
               changedColumns.append("new.").append(col.getQuotedName());
               listingColumns.append(col.getQuotedName());
View Full Code Here

         return;
      }

      String sTable = table.getFullName(getOwner(), "$", true);
      Column column = index.getIndexColumn(0).getColumn();
      Index primaryKey = table.getPrimaryKey();
      boolean bColInPK = primaryKey.findIndexColumn(column) != null;
      boolean bHaveMoreIndexes = false;
      StringBuffer buf = new StringBuffer(128);

      // determine if this is the first Full-Text index to be created (i.e. Full-Text table needed)
      for (int i = 0, nCount = table.getIndexCount(); i < nCount && !bHaveMoreIndexes; ++i)
      {
         Index idx = table.getIndex(i);

         bHaveMoreIndexes = idx != index && idx.getType() == Index.TEXT;
      }

      if (!bHaveMoreIndexes) // Need to create a new Full-Text table
      {
         createTextTable(table);
View Full Code Here

         Column column = index.getIndexColumn(0).getColumn(); // take only first column

         // see if if there are any more FullText indexes
         for (int i = 0, nCount = table.getIndexCount(); i < nCount && !bHaveMoreIndexes; ++i)
         {
            Index idx = table.getIndex(i);

            bHaveMoreIndexes = idx != index && idx.getType() == Index.TEXT; // ignore self
         }

         // the Full-Text column is part of PrimaryKey, so it is still needed for other Full-Text
         if (bHaveMoreIndexes &&                                                       // indexes
             table.getPrimaryKey().findIndexColumn(column) != null)
View Full Code Here

TOP

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

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.