Package nexj.core.meta.persistence.sql

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


   {
      if (sIndexName != null)
      {
         for (int nTable = 0; nTable < mapping.getTableCount(); ++nTable)
         {
            Table table = mapping.getTable(nTable);

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

               if (indexNameMatches(index, sIndexName))
               {
                  if (index.isUnique() && index.getIndexColumnCount() != 0)
                  {
View Full Code Here


                     matchOp.setMapping(join);
                  }
                  else // if null then not seen before or no join table created
                  {
                     Source source = matchOp.getAttribute().getSource();
                     Table table = m_adapter.getMatchJoin(
                        ((RelationalPrimitiveMapping)source.getAttributeMapping()).getColumn(),
                        matchOp.getExpression());

                     if (table != null) // if adapter needs a table join
                     {
View Full Code Here

    * table. Then create Full-Text indexes on the new table.
    * @see nexj.core.persistence.sql.SQLSchemaManager#createIndex(nexj.core.meta.persistence.sql.Index)
    */
   protected void createIndex(Index index)
   {
      Table table = index.getTable();

      if (index.getType() != Index.TEXT || table.getType() != Table.MANAGED)
      {
         super.createIndex(index); // let super deal with regular indexes

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

      if (!bColInPK) // Existing Full-Text table to alter, column does not exist yet
      {
         buf.append("alter table ").append(sTable).append(" add column ");
         appendColumnDeclaration(buf, column, false, false, ", "); // take only first column
         m_appender.appendSQL(buf.toString());           // column nullable since rows can exist
         buf.setLength(0);
         dropTextTriggers(table);
         createTextTriggers(table, null, null);
         buf.append("update ").append(sTable).append(" dst inner join ");
         buf.append(table.getFullName(getOwner(), null, true)).append(" src on ");

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

            if (i != 0)
            {
               buf.append("and ");
            }
View Full Code Here

    * also MySQL indexes explicitly belong to a specific table.
    * @see nexj.core.persistence.sql.SQLSchemaManager#dropIndex(nexj.core.meta.persistance.sql.Index)
    */
   protected void dropIndex(Index index)
   {
      Table table = index.getTable();
     
      if (index.getType() >= Index.BTREE && table.getType() == Table.MANAGED)
      {
         StringBuffer buf = new StringBuffer(128);

         if (isConstraint(index))
         {
            buf.append("alter table ");
            buf.append(table.getFullName(getOwner()));
            buf.append(" drop primary key");
         }
         else
         {
            buf.append("drop index ");
            buf.append(getFullIndexName(index, true));
            buf.append(" on ");
            buf.append(table.getFullName(getOwner()));
         }

         m_appender.appendSQL(buf.toString());
      }
      else if (index.getType() == Index.TEXT && table.getType() == Table.MANAGED)
      {
         String sTable = table.getFullName(getOwner(), "$", true);
         StringBuffer buf = new StringBuffer(128);

         buf.append("drop index ");
         buf.append(getFullIndexName(index, true));
         buf.append(" on ");
         buf.append(sTable);
         m_appender.appendSQL(buf.toString());
         buf.setLength(0);

         boolean bHaveMoreIndexes = false;
         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)
         {
            return; // done
         }

         dropTextTriggers(table);
View Full Code Here

    * @see nexj.core.persistence.sql.SQLSchemaManager#renameColumn(nexj.core.meta.persistence.sql.Column, nexj.core.meta.persistence.sql.Column)
    */
   protected void renameColumn(Column newColumn, Column oldColumn)
   {
      StringBuffer buf;
      Table table = oldColumn.getTable();

      if (table.getType() != Table.MANAGED)
      {
         return;
      }

      buf = new StringBuffer(128);
      buf.append("alter table ");
      buf.append(table.getFullName(getOwner()));
      buf.append(" change column ");
      buf.append(oldColumn.getQuotedName());
      buf.append(" ");
      appendColumnDeclaration(buf, newColumn, null, oldColumn.isNullable(), false);

View Full Code Here

   }

   public void testRenameColumnStep() throws SQLException
   {
      RelationalSchema schema = upgrade(m_singleColTableStep, null, null);
      Table table = schema.getTable(m_singleColTableStep.getName());

      assertNotNull(table);
      assertEquals(1, table.getColumnCount());
      assertEquals("id", table.getColumn(0).getName());
      table.setType(Table.MANAGED); // comes in as Table.EXTERNAL from DB

      SQLStatement statement = new SQLStatement();
      SQLScript script = new SQLScript();
      ExecStep populate = new ExecStep();

      statement.setSQL("insert into " + table.getQuotedName() + " values (1)");
      script.addStatement(statement);
      populate.getScriptHolder().addScript(script);
      upgrade(populate, schema, null);

      RenameColumnStep step = new RenameColumnStep();

      step.setTableName(table.getName());
      step.setOldName("id");
      step.setNewName("id1");
      schema = upgrade(step, schema, null);
      table = schema.getTable(m_singleColTableStep.getName());
      assertNotNull(table);
      assertEquals(1, table.getColumnCount());
      assertEquals("id1", table.getColumn(0).getName());

      // ensure no data loss
      Statement stmt = null;
      ResultSet rs = null;

      try
      {
         stmt = m_connection.getConnection().createStatement();
         rs = stmt.executeQuery("select count(*) from " + table.getFullName(m_manager.getOwner()));
         assertNotNull(rs);
         assertTrue(rs.next());
         assertEquals(1, rs.getInt(1));
      }
      finally
View Full Code Here

   }

   public void testRenameIndexStep()
   {
      RelationalSchema schema = upgrade(m_singleIdxColTableStep, null, null);
      Table table = schema.getTable(m_singleIdxColTableStep.getName());

      assertNotNull(table);
      assertEquals(1, table.getIndexCount());
      assertEquals(m_manager.generateIndexName(table.getTableName(), "ind0", null),
                   table.getIndex(0).getName());
      table.setType(Table.MANAGED); // comes in as Table.EXTERNAL from DB

      RenameIndexStep step = new RenameIndexStep();

      step.setOldName(table.getIndex(0).getName());
      step.setNewName("ind1");
      schema = upgrade(step, schema, null);
      table = schema.getTable(m_singleIdxColTableStep.getName());
      assertNotNull(table);
      assertEquals(1, table.getIndexCount());
      assertEquals(m_manager.generateIndexName(table.getTableName(), "ind1", null),
                   table.getIndex(0).getName());
   }
View Full Code Here

   }

   public void testRenameTableStep() throws SQLException
   {
      RelationalSchema schema = upgrade(m_singlePKColTableStep, null, null);
      Table table = schema.getTable(m_singlePKColTableStep.getName());

      assertNotNull(table);
      table.setType(Table.MANAGED); // comes in as Table.EXTERNAL from DB

      SQLStatement statement = new SQLStatement();
      SQLScript script = new SQLScript();
      ExecStep populate = new ExecStep();

      statement.setSQL("insert into " + table.getQuotedName() + " values (1)");
      script.addStatement(statement);
      populate.getScriptHolder().addScript(script);
      upgrade(populate, schema, null);

      RenameTableStep step = new RenameTableStep();
      step.setOldName(table.getName());
      step.setNewName("B");
      schema = upgrade(step, schema, null);

      assertNull(schema.findTable(m_singlePKColTableStep.getName()));

      table = schema.getTable(schema.getPrefix() + "b");

      assertNotNull(table);
      assertEquals(1, table.getColumnCount());

      Column column = table.getColumn(0);

      assertEquals("id", column.getName());
      assertEquals(Primitive.INTEGER, column.getType());

      // ensure no data loss
      Statement stmt = null;
      ResultSet rs = null;

      try
      {
         stmt = m_connection.getConnection().createStatement();
         rs = stmt.executeQuery("select count(*) from " + table.getFullName(m_manager.getOwner()));
         assertNotNull(rs);
         assertTrue(rs.next());
         assertEquals(1, rs.getInt(1));
      }
      finally
View Full Code Here

   public void testSchemaVersion()
   {
      RelationalSchema schema = (RelationalSchema)m_database.getSchema();
      Metadata metadata = schema.getMetadata();
      StringWriter writer = new StringWriter();
      Table origVersionTable = schema.getVersionTable(); // note original
      SQLAppender origAppender = m_manager.getSQLAppender();

      try
      {
         schema.setVersionTable(schema.getTable("Version"));
View Full Code Here

   {
      StringWriter output = new StringWriter();
      RelationalSchema schema = new RelationalSchema();
      TransferObject valueMap = new TransferObject();
      TransferObject defaultsMap = new TransferObject();
      Table tmpTable;

      tmpTable = new Table(schema);
      tmpTable.setName("table1"); // required for addTable() to work
      tmpTable.setLongspaceName("longspace1");
      schema.addTable(tmpTable);
      tmpTable = new Table(schema);
      tmpTable.setName("table2"); // required for addTable() to work
      tmpTable.setLongspaceName("longspace2");
      schema.addTable(tmpTable);
      tmpTable = new Table(schema);
      tmpTable.setName("table3"); // required for addTable() to work
      tmpTable.setLongspaceName("longspace3");
      schema.addTable(tmpTable);
      valueMap.setValue("valid-key", "valid-value");
      valueMap.setValue("missing-key", null);
      valueMap.setValue("default-key", null);
      defaultsMap.setValue("default-key", "default-value");
View Full Code Here

TOP

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

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.