Package org.apache.ddlutils.model

Examples of org.apache.ddlutils.model.Table


        if(!disableJPA){
          // Remove from the current schema those tables that are not known by Siena,
            // since they're likely to be JPA classes.
            // Iterate in reverse order since removeTable() changes the list size.
            for (int i = currentDatabase.getTableCount() - 1; i >= 0; i--) {
                Table table = currentDatabase.getTable(i);
                if(database.findTable(table.getName(), false) == null){
                    Logger.debug("Keeping existing table %s", table.getName());
                    currentDatabase.removeTable(i);
                }
            }
        }
       
View Full Code Here


 
  public Table addTable(Class<?> clazz) {
    if(Modifier.isAbstract(clazz.getModifiers())){
      return null;
    }
    Table table = new Table();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    table.setName(info.tableName);
    table.setType("MyISAM");
    database.addTable(table);
   
    Map<String, UniqueIndex> uniques = new HashMap<String, UniqueIndex>();
    Map<String, NonUniqueIndex> indexes = new HashMap<String, NonUniqueIndex>();
   
    /* columns */
    for (Field field : info.allFields) {
      String[] columns = ClassInfo.getColumnNames(field);
      boolean notNull = field.getAnnotation(NotNull.class) != null;
     
      Class<?> type = field.getType();
      if(!ClassInfo.isModel(type) || (ClassInfo.isModel(type) && ClassInfo.isEmbedded(field))) {
        Column column = createColumn(clazz, field, columns[0]);
       
        if(notNull || type.isPrimitive()) {
          column.setRequired(true);
         
          if(type.isPrimitive() && !ClassInfo.isId(field)) { // TODO: add also Boolean, Long, Double,... ?
            if(type == Boolean.TYPE) {
              column.setDefaultValue("false");
            } else {
              column.setDefaultValue("0");
            }
          }
        }
       
        Id id = field.getAnnotation(Id.class);
        if(id != null) {
          column.setPrimaryKey(true);
          column.setRequired(true);
         
          // auto_increments managed ONLY for long
          if(id.value() == Generator.AUTO_INCREMENT
              && (Long.TYPE == type || Long.class.isAssignableFrom(type)))
            column.setAutoIncrement(true);
         
          // adds index on primary key
          /*UniqueIndex i = uniques.get(columns[0]);
          if(i == null) {
            i = new UniqueIndex();
            i.setName(columns[0]);
            uniques.put(columns[0], i);
            table.addIndex(i);
          }
          fillIndex(i, field);*/
        }
       
        table.addColumn(column);
      } else {
        List<Field> keys = ClassInfo.getClassInfo(type).keys;
       
        for (int i = 0; i < columns.length; i++) {
          Field f = keys.get(i);
          Column column = createColumn(clazz, f, columns[i]);

          if(notNull)
            column.setRequired(true);
         
          table.addColumn(column);
        }
      }
    }

    /* indexes */
    for (Field field : info.updateFields) {
      Index index = field.getAnnotation(Index.class);
      if(index != null) {
        String[] names = index.value();
        for (String name : names) {
          NonUniqueIndex i = indexes.get(name);
          if(i == null) {
            i = new NonUniqueIndex();
            i.setName(name);
            indexes.put(name, i);
            table.addIndex(i);
          }
          fillIndex(i, field);
        }
      }
     
      Unique unique = field.getAnnotation(Unique.class);
      if(unique != null) {
        String[] names = unique.value();
        for (String name : names) {
          UniqueIndex i = uniques.get(name);
          if(i == null) {
            i = new UniqueIndex();
            i.setName(name);
            uniques.put(name, i);
            table.addIndex(i);
          }
          fillIndex(i, field);
        }
      }
    }
   
    tables.put(table.getName(), table);
    return table;
  }
View Full Code Here

     * @param tableName The table
     * @return The rows
     */
    protected List getRows(String tableName)
    {
        Table table = getModel().findTable(tableName, getPlatform().isDelimitedIdentifierModeOn());
       
        return getPlatform().fetch(getModel(),
                                   getSelectQueryForAllString(table, null),
                                   new Table[] { table });
    }
View Full Code Here

     * @param orderColumn The column to order the rows by
     * @return The rows
     */
    protected List getRows(String tableName, String orderColumn)
    {
        Table table = getModel().findTable(tableName, getPlatform().isDelimitedIdentifierModeOn());
       
        return getPlatform().fetch(getModel(),
                                   getSelectQueryForAllString(table, orderColumn),
                                   new Table[] { table });
    }
View Full Code Here

        {
            Database model = (Database)getModel().clone();

            for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
            {
                Table table = model.getTable(tableIdx);

                for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
                {
                    Column column     = table.getColumn(columnIdx);
                    int    origType   = column.getTypeCode();
                    int    targetType = getPlatformInfo().getTargetJdbcType(origType);

                    // we adjust the column types if the native type would back-map to a
                    // different jdbc type
                    if (targetType != origType)
                    {
                        column.setTypeCode(targetType);
                        // we should also adapt the default value
                        if (column.getDefaultValue() != null)
                        {
                            DefaultValueHelper helper = getPlatform().getSqlBuilder().getDefaultValueHelper();

                            column.setDefaultValue(helper.convert(column.getDefaultValue(), origType, targetType));
                        }
                    }
                    // we also promote the default size if the column has no size
                    // spec of its own
                    if ((column.getSize() == null) && getPlatformInfo().hasSize(targetType))
                    {
                        Integer defaultSize = getPlatformInfo().getDefaultSize(targetType);

                        if (defaultSize != null)
                        {
                            column.setSize(defaultSize.toString());
                        }
                    }
                    // finally the platform might return a synthetic default value if the column
                    // is a primary key column
                    if (getPlatformInfo().isSyntheticDefaultValueForRequiredReturned() &&
                        (column.getDefaultValue() == null) && column.isRequired() && !column.isAutoIncrement())
                    {
                        switch (column.getTypeCode())
                        {
                            case Types.TINYINT:
                            case Types.SMALLINT:
                            case Types.INTEGER:
                            case Types.BIGINT:
                                column.setDefaultValue("0");
                                break;
                            case Types.REAL:
                            case Types.FLOAT:
                            case Types.DOUBLE:
                                column.setDefaultValue("0.0");
                                break;
                            case Types.BIT:
                                column.setDefaultValue("false");
                                break;
                            default:
                                column.setDefaultValue("");
                                break;
                        }
                    }
                }
                // we also add the default names to foreign keys that are initially unnamed
                for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++)
                {
                    ForeignKey fk = table.getForeignKey(fkIdx);

                    if (fk.getName() == null)
                    {
                        fk.setName(getPlatform().getSqlBuilder().getForeignKeyName(table, fk));
                    }
View Full Code Here

        assertEquals(1,
                     changes.size());

        ColumnOrderChange change      = (ColumnOrderChange)changes.get(0);
        Table             sourceTable = change.getChangedTable();

        assertEquals(3,
                     change.getNewPosition(sourceTable.getColumn(1)));
        assertEquals(-1,
                     change.getNewPosition(sourceTable.getColumn(2)));
        assertEquals(1,
                     change.getNewPosition(sourceTable.getColumn(3)));
    }
View Full Code Here

     */
    public void testToColumnValues()
    {
        Database         database = parseDatabaseFromString(TESTED_MODEL);
        PlatformImplBase platform = new TestPlatform();
        Table            table    = database.getTable(0);
        SqlDynaClass     clz      = SqlDynaClass.newInstance(table);
        DynaBean         db       = new SqlDynaBean(SqlDynaClass.newInstance(table));

        db.set("name", "name");

View Full Code Here

        printIndent();
        print("ADD ");
        writeColumn(change.getChangedTable(), change.getNewColumn());
        printEndOfStatement();

        Table curTable = currentModel.findTable(change.getChangedTable().getName(), getPlatform().isDelimitedIdentifierModeOn());

        if (!change.isAtEnd())
        {
            Column prevColumn = change.getPreviousColumn();

            if (prevColumn != null)
            {
              // we need the corresponding column object from the current table
              prevColumn = curTable.findColumn(prevColumn.getName(), getPlatform().isDelimitedIdentifierModeOn());
            }
            // Even though Firebird can only add columns, we can move them later on
            print("ALTER TABLE ");
            printlnIdentifier(getTableName(change.getChangedTable()));
            printIndent();
            print("ALTER ");
            printIdentifier(getColumnName(change.getNewColumn()));
            print(" POSITION ");
            // column positions start at 1 in Firebird
            print(prevColumn == null ? "1" : String.valueOf(curTable.getColumnIndex(prevColumn) + 2));
            printEndOfStatement();
        }
        if (change.getNewColumn().isAutoIncrement())
        {
            writeAutoIncrementCreateStmts(currentModel, curTable, change.getNewColumn());
View Full Code Here

    /**
     * {@inheritDoc}
     */
    protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
    {
        Table table = super.readTable(metaData, values);

        if (table != null)
        {
          determineAutoIncrementColumns(table);
        }
View Full Code Here

    /**
     * {@inheritDoc}
     */
    public void apply(Database database, boolean caseSensitive)
    {
        Table  table  = database.findTable(getChangedTable().getName(), caseSensitive);
        Column column = table.findColumn(_column.getName(), caseSensitive);

        column.setTypeCode(_newTypeCode);
    }
View Full Code Here

TOP

Related Classes of org.apache.ddlutils.model.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.