Package nexj.core.meta.persistence.sql

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


      manager.upgrade(schema, null); // must upgrade starting from "2-step"
      AssertUtil.assertContained(tableStmtValid.getSQL(), buf.toString());

      // setup for third upgrade version
      CreateColumnStep createColumnStep = new CreateColumnStep(); // step lacking match for adapter
      Column column = new Column("Column", table);
      ColumnOutline outline = new ColumnOutline(column.getName());

      column.setType(Primitive.INTEGER);
      table.addColumn(column);
      outline.setType(column.getType());
      createColumnStep.setOutline(outline);
      createColumnStep.setTableName(table.getName());
      createColumnStep.getScriptHolder().addScript(stepScript);

      // incompatible step -> compatible step -> incompatible step
View Full Code Here


   public void prepare(Work work)
   {
      Instance instance = work.getInstance();
      PersistenceMapping mapping = instance.getPersistenceMapping();
      Object obj;
      Column column;
      SQLWork sqlWork;

      if (m_objectAttribute != null)
      {
         obj = instance.getValueDirect(m_objectAttribute.getOrdinal());

         if (obj instanceof Instance)
         {
            Instance inst = (Instance)obj;

            if (inst.getOID() != null && (inst.getState() != Instance.DELETED || m_bSerializingDeleted))
            {
               if (instance.isDirty(m_objectAttribute.getOrdinal()))
               {
                  if (m_oidAttribute != null)
                  {
                     column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_oidAttribute)).getColumn();
                     sqlWork = (SQLWork)work;

                     if (sqlWork.getTable() == column.getTable())
                     {
                        sqlWork.setInstanceValue(column, inst.getOID().toBinary());
                     }
                  }

                  if (m_classAttribute != null)
                  {
                     column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_classAttribute)).getColumn();
                     sqlWork = (SQLWork)work;

                     if (sqlWork.getTable() == column.getTable())
                     {
                        sqlWork.setInstanceValue(column, inst.getMetaclass().getName());
                     }
                  }
               }

               if (m_lockingAttribute != null)
               {
                  column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_lockingAttribute)).getColumn();
                  sqlWork = (SQLWork)work;

                  if (sqlWork.getTable() == column.getTable())
                  {
                     Attribute lockingAttribute = inst.getPersistenceMapping().getLockingAttribute();

                     sqlWork.setInstanceValue(column, (lockingAttribute != null) ? inst.getValue(lockingAttribute.getOrdinal()) : null);
                  }
               }
            }
         }
      }

      if (m_variablesAttribute != null)
      {
         obj = instance.getValueDirect(m_variablesAttribute.getOrdinal());

         if (obj instanceof SerializablePropertyMap)
         {
            SerializablePropertyMap map = (SerializablePropertyMap)obj;

            column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_serializedVariablesAttribute)).getColumn();
            sqlWork = (SQLWork)work;

            if (sqlWork.getTable() == column.getTable())
            {
               if (column.getType().equals(Primitive.STRING))
               {
                  sqlWork.setInstanceValue(column, map.serializeValues(instance.getUnitOfWork().getInvocationContext()));
               }
               else
               {
View Full Code Here

    * @param obj The dependent instance.
    */
   protected static void addDependency(UnitOfWork uow, Instance instance, Attribute attribute, Instance obj)
   {
      SQLAdapter adapter = (SQLAdapter)instance.getAdapter();
      Column column = ((RelationalPrimitiveMapping)instance.getPersistenceMapping().getAttributeMapping(attribute)).getColumn();
      SQLWork work = adapter.findWork(uow, instance, column.getTable());

      if (work != null)
      {
         work.setValue(column, null);

View Full Code Here

            if (!isValidColumnName(sColName))
            {
               continue;
            }

            Column column = new Column(sName, table);

            column.setNullable(rs.getInt("NULLABLE") != DatabaseMetaData.columnNoNulls);
            column.setDescription(rs.getString("REMARKS"));

            String sTypeName = rs.getString("TYPE_NAME");
            int nType = rs.getInt("DATA_TYPE");
            int nPrecision = rs.getInt("COLUMN_SIZE");
            int nScale = rs.getInt("DECIMAL_DIGITS");
            byte nAllocation = Column.FIXED;
            Primitive type = null;
           
            switch (nType)
            {
               case Types.BIGINT:
                  type = Primitive.LONG;
                  nPrecision = 0;
                  nScale = 0;
                  break;
              
               case Types.BINARY:
                  type = Primitive.BINARY;
                  nScale = 0;
                  break;
              
               case Types.BIT:
               case Types.BOOLEAN:
                  type = Primitive.BOOLEAN;
                  nPrecision = 0;
                  nScale = 0;
                  break;

               case Types.BLOB:
               case Types.LONGVARBINARY:
                  type = Primitive.BINARY;
                 
                  if (nPrecision <= 0x4000)
                  {
                     nPrecision = Integer.MAX_VALUE;
                  }

                  nScale = 0;
                  nAllocation = (nType == Types.BLOB) ? Column.LOCATOR : Column.VARYING;
                  break;

               case Types.CHAR:
                  sTypeName = sTypeName.toLowerCase(Locale.ENGLISH);

                  if (sTypeName.equals("uniqueidentifier"))
                  {
                     type = Primitive.BINARY;
                     nPrecision = 16;
                     nScale = 0;
                  }
                  else
                  {
                     type = Primitive.STRING;
                     nScale = 0;
                  }

                  break;

               case Types.CLOB:
               case Types.LONGVARCHAR:
                  type = Primitive.STRING;
                    
                  if (nPrecision <= 0x4000)
                  {
                     nPrecision = Integer.MAX_VALUE;
                  }
  
                  nScale = 0;
                  nAllocation = (nType == Types.CLOB) ? Column.LOCATOR : Column.VARYING;
                  break;
              
               case Types.DATE:
               case Types.TIME:
               case Types.TIMESTAMP:
                  type = Primitive.TIMESTAMP;
                  nPrecision = 0;
                  nScale = 0;
                  break;
              
               case Types.DECIMAL:
               case Types.NUMERIC:
                  type = Primitive.DECIMAL;
                 
                  if (nScale == 0 && nPrecision <= 20)
                  {
                     if (nPrecision <= 10)
                     {
                        type = Primitive.INTEGER;

                        if (nPrecision <= 3)
                        {
                           nPrecision = 1;
                        }
                        else if (nPrecision <= 5)
                        {
                           nPrecision = 2;
                        }
                        else
                        {
                           nPrecision = 0;
                        }
                     }
                     else
                     {
                        type = Primitive.LONG;
                        nPrecision = 0;
                     }
                  }

                  break;

               case Types.DOUBLE:
               case Types.FLOAT:
                  type = Primitive.DOUBLE;
                  nPrecision = 0;
                  nScale = 0;
                  break;

               case Types.INTEGER:
                  type = Primitive.INTEGER;
                  nPrecision = 0;
                  nScale = 0;
                  break;

               case Types.SMALLINT:
                  type = Primitive.INTEGER;
                  nPrecision = 2;
                  nScale = 0;
                  break;

               case Types.TINYINT:
                  type = Primitive.INTEGER;
                  nPrecision = 1;
                  nScale = 0;
                  break;

               case Types.REAL:
                  type = Primitive.FLOAT;
                  nPrecision = 0;
                  nScale = 0;
                  break;
              
               case Types.VARBINARY:
                  type = Primitive.BINARY;
                  nScale = 0;
                  nAllocation = Column.VARYING;
                  break;

               case Types.VARCHAR:
                  type = Primitive.STRING;
                  nScale = 0;
                  nAllocation = Column.VARYING;
                  break;

               default:
                  sTypeName = sTypeName.toLowerCase(Locale.ENGLISH);

                  if (sTypeName.equals("nchar"))
                  {
                     type = Primitive.STRING;
                     nPrecision >>= 1;
                     nScale = 0;
                  }
                  else if (sTypeName.equals("nvarchar2") || sTypeName.equals("nvarchar"))
                  {
                     type = Primitive.STRING;
                     nPrecision >>= 1;
                     nScale = 0;
                     nAllocation = Column.VARYING;
                  }
                  else if (sTypeName.equals("binary_double"))
                  {
                     type = Primitive.DOUBLE;
                     nPrecision = 0;
                     nScale = 0;
                  }
                  else if (sTypeName.equals("binary_float"))
                  {
                     type = Primitive.FLOAT;
                     nPrecision = 0;
                     nScale = 0;
                  }
                  else if (sTypeName.startsWith("timestamp"))
                  {
                     type = Primitive.TIMESTAMP;
                     nPrecision = 0;
                     nScale = 0;
                  }
                  else if (sTypeName.equals("nclob") || sTypeName.equals("clob"))
                  {
                     type = Primitive.STRING;
                    
                     if (nPrecision <= 0x4000)
                     {
                        nPrecision = Integer.MAX_VALUE;
                     }
  
                     nScale = 0;
                     nAllocation = Column.LOCATOR;
                  }
                  else if (sTypeName.equals("blob"))
                  {
                     type = Primitive.BINARY;
                       
                     if (nPrecision <= 0x4000)
                     {
                        nPrecision = Integer.MAX_VALUE;
                     }
     
                     nScale = 0;
                     nAllocation = Column.LOCATOR;
                  }
              
                  break;
            }

            if (nPrecision < 0)
            {
               nPrecision = 0;
            }

            if (s_logger.isDebugEnabled())
            {
               s_logger.debug("Read column \"" + table.getName() + "." + column.getName() + "\" " +
                  sTypeName + "(" + rs.getInt("COLUMN_SIZE") + "," + rs.getInt("DECIMAL_DIGITS") +
                  "), SQLType=" + nType + " -> " + ((type == null) ? "ignored: unsupported type" : type.getName() +
                  "(" + nPrecision + "," + nScale + "), allocation=" + nAllocation));
            }

            if (type != null)
            {
               column.setType(type);
               column.setPrecision(nPrecision);
               column.setScale(nScale);
               column.setAllocation(nAllocation);

               try
               {
                  table.addColumn(column);
                  bPortable &= isPortable(column);
               }
               catch (MetadataException e)
               {
                  s_logger.error("Cannot add column \"" + column.getName() + "\"", e);
               }
            }
         }

         rs.close();
         rs = null;

         // Set the case-sensitive columns

         for (Iterator tableItr = tableMap.iterator(); tableItr.hasNext();)
         {
            Table table = (Table)tableItr.next();

            for (int i = 0; i < table.getColumnCount(); ++i)
            {
               Column column = table.getColumn(i);

               if (!caseInsensitiveSet.contains(table, column.getName()))
               {
                  column.setCaseInsensitive(false);
               }
            }
         }

         // Read the indexes
View Full Code Here

            buf.append('(');

            // add the table columns
            for (int i = 0; i < table.getColumnCount(); ++i)
            {
               Column column = table.getColumn(i);

               if (i > 0)
               {
                  buf.append(',');
               }
View Full Code Here

      insertBuf.append('(');
      selectBuf.append(") select ");

      for (int i = 0, nColCount = dstTable.getColumnCount(); i < nColCount; ++i)
      {
         Column dstCol = dstTable.getColumn(i);
         String sDstColName = dstCol.getName();
         String sSrcColName = (columnMap != null && columnMap.contains(sDstColName))
                            ? (String)columnMap.get(sDstColName) : sDstColName;
         Column srcCol = (sSrcColName == null) ? null : srcTable.findColumn(sSrcColName);

         if (srcCol != null) // column requiring data to be preserved
         {
            if (bExecute)
            {
View Full Code Here

   public void apply(RelationalSchemaUpgradeState state)
   {
      RelationalSchema schema = state.getSchema();
      Table table = schema.getTable(m_sTableName);

      m_column = new Column(m_outline.getName(), table);
      m_outline.copyTo(m_column);
      table.addColumn(m_column);

      m_holder.validate(schema, table, state.getAdapters());

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

            if (pointcut.hasAspect(table))
            {
               Column column = new Column(m_outline.getName(), table);
               m_outline.copyTo(column);
               pointcut.addColumn(column);
            }
         }
      }
View Full Code Here

         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());

         step.apply(m_state);
         renameColumn(step.getColumn(), oldColumn);
      }
   }
View Full Code Here

      Table template = current.cloneTemporary();

      // remove all indexes related to modified columns from template table
      for (int i = 0, nCount = target.getColumnCount(); i < nCount; ++i)
      {
         Column targetCol = target.getColumn(i);
         Column templateCol = template.getColumn(targetCol.getName());

         if (!isCompatible(targetCol, templateCol))
         {
            targetCol.copyTo(templateCol);

            for (int k = template.getIndexCount() - 1; k >= 0; --k) // backwards due to removal
            {
               Index index = template.getIndex(k);

               if (index.findIndexColumn(templateCol) != null)
               {
                  template.removeIndex(index);
               }
            }
         }
      }

      dropIndexes(template, current);

      // apply all column alterations
      for (int i = 0, nCount = target.getColumnCount(); i < nCount; ++i)
      {
         Column targetCol = target.getColumn(i);
         Column currentCol = current.getColumn(targetCol.getName());

         if (!isCompatible(targetCol, currentCol))
         {
            alterColumn(targetCol, currentCol);
            targetCol.copyTo(current.getColumn(targetCol.getName())); //column might change in alter
View Full Code Here

      {
         return; // nothing to do
      }

      StringBuffer buf = new StringBuffer(128);
      Column nullableColumn = column;

      if (!bNullability && !column.isNullable())
      {
         nullableColumn = (Column)column.clone();
         nullableColumn.setRequired(false, true);
         nullableColumn.setNullable(true);
      }

      buf.append("alter table ");
      buf.append(table.getFullName(getOwner()));
      buf.append(getAddColumnToken());
View Full Code Here

TOP

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

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.