Search in sources :

Example 6 with Table

use of org.apache.ddlutils.model.Table in project otter by alibaba.

the class DdlUtils method readTable.

private static Table readTable(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
    String tableName = (String) values.get("TABLE_NAME");
    Table table = null;
    if ((tableName != null) && (tableName.length() > 0)) {
        table = new Table();
        table.setName(tableName);
        table.setType((String) values.get("TABLE_TYPE"));
        table.setCatalog((String) values.get("TABLE_CAT"));
        table.setSchema((String) values.get("TABLE_SCHEM"));
        table.setDescription((String) values.get("REMARKS"));
        table.addColumns(readColumns(metaData, tableName));
        Collection<String> primaryKeys = readPrimaryKeyNames(metaData, tableName);
        for (Object key : primaryKeys) {
            Column col = table.findColumn((String) key, true);
            if (col != null) {
                col.setPrimaryKey(true);
            } else {
                throw new NullPointerException(String.format("%s pk %s is null - %s %s", tableName, key, ToStringBuilder.reflectionToString(metaData, ToStringStyle.SIMPLE_STYLE), ToStringBuilder.reflectionToString(values, ToStringStyle.SIMPLE_STYLE)));
            }
        }
    }
    return table;
}
Also used : Table(org.apache.ddlutils.model.Table) Column(org.apache.ddlutils.model.Column)

Example 7 with Table

use of org.apache.ddlutils.model.Table in project siena by mandubian.

the class DdlGenerator method addTable.

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;
}
Also used : Table(org.apache.ddlutils.model.Table) NonUniqueIndex(org.apache.ddlutils.model.NonUniqueIndex) HashMap(java.util.HashMap) NonUniqueIndex(org.apache.ddlutils.model.NonUniqueIndex) Index(siena.Index) UniqueIndex(org.apache.ddlutils.model.UniqueIndex) NotNull(siena.NotNull) Field(java.lang.reflect.Field) IndexColumn(org.apache.ddlutils.model.IndexColumn) Column(org.apache.ddlutils.model.Column) Unique(siena.Unique) Id(siena.Id) NonUniqueIndex(org.apache.ddlutils.model.NonUniqueIndex) UniqueIndex(org.apache.ddlutils.model.UniqueIndex) ClassInfo(siena.ClassInfo)

Example 8 with Table

use of org.apache.ddlutils.model.Table in project otter by alibaba.

the class AbstractDbDialect method initTables.

// ================================ helper method ==========================
private void initTables(final JdbcTemplate jdbcTemplate) {
    // soft引用设置,避免内存爆了
    GenericMapMaker mapMaker = null;
    mapMaker = new MapMaker().softValues().evictionListener(new MapEvictionListener<List<String>, Table>() {

        public void onEviction(List<String> names, Table table) {
            logger.warn("Eviction For Table:" + table);
        }
    });
    this.tables = mapMaker.makeComputingMap(new Function<List<String>, Table>() {

        public Table apply(List<String> names) {
            Assert.isTrue(names.size() == 2);
            try {
                beforeFindTable(jdbcTemplate, names.get(0), names.get(0), names.get(1));
                DdlUtilsFilter filter = getDdlUtilsFilter(jdbcTemplate, names.get(0), names.get(0), names.get(1));
                Table table = DdlUtils.findTable(jdbcTemplate, names.get(0), names.get(0), names.get(1), filter);
                afterFindTable(table, jdbcTemplate, names.get(0), names.get(0), names.get(1));
                if (table == null) {
                    throw new NestableRuntimeException("no found table [" + names.get(0) + "." + names.get(1) + "] , pls check");
                } else {
                    return table;
                }
            } catch (Exception e) {
                throw new NestableRuntimeException("find table [" + names.get(0) + "." + names.get(1) + "] error", e);
            }
        }
    });
}
Also used : Function(com.google.common.base.Function) GenericMapMaker(com.google.common.collect.GenericMapMaker) Table(org.apache.ddlutils.model.Table) NestableRuntimeException(org.apache.commons.lang.exception.NestableRuntimeException) DdlUtilsFilter(com.alibaba.otter.shared.common.utils.meta.DdlUtilsFilter) GenericMapMaker(com.google.common.collect.GenericMapMaker) MapMaker(com.google.common.collect.MapMaker) List(java.util.List) MapEvictionListener(com.google.common.collect.MapEvictionListener) DataAccessException(org.springframework.dao.DataAccessException) NestableRuntimeException(org.apache.commons.lang.exception.NestableRuntimeException) SQLException(java.sql.SQLException)

Example 9 with Table

use of org.apache.ddlutils.model.Table in project otter by alibaba.

the class DataSourceChecker method checkMap.

public String checkMap(String namespace, String name, Long dataSourceId) {
    Connection conn = null;
    Statement stmt = null;
    DataMediaSource source = dataMediaSourceService.findById(dataSourceId);
    DataSource dataSource = null;
    try {
        DbMediaSource dbMediaSource = (DbMediaSource) source;
        dataSource = dataSourceCreator.createDataSource(dbMediaSource);
        // conn = dataSource.getConnection();
        // if (null == conn) {
        // return DATABASE_FAIL;
        // }
        ModeValue namespaceValue = ConfigHelper.parseMode(namespace);
        ModeValue nameValue = ConfigHelper.parseMode(name);
        String tempNamespace = namespaceValue.getSingleValue();
        String tempName = nameValue.getSingleValue();
        try {
            Table table = DdlUtils.findTable(new JdbcTemplate(dataSource), tempNamespace, tempNamespace, tempName);
            if (table == null) {
                return SELECT_FAIL;
            }
        } catch (SQLException se) {
            logger.error("check error!", se);
            return SELECT_FAIL;
        } catch (Exception e) {
            logger.error("check error!", e);
            return SELECT_FAIL;
        }
    // String selectSql = "SELECT * from " + tempNamespace + "." +
    // tempName + " where 1 = 0";
    // String insertSql = "INSERT INTO " + tempNamespace + "." +
    // tempName + " select * from ";
    // insertSql += "( SELECT * from " + tempNamespace + "." + tempName
    // + ") table2 where 1 = 0";
    // String deleteSql = "DELETE from " + tempNamespace + "." +
    // tempName + " where 1 = 0";
    //
    // stmt = conn.createStatement();
    //
    // try {
    // stmt.executeQuery(selectSql);
    // } catch (SQLException se) {
    // return SELECT_FAIL;
    // }
    //
    // try {
    // stmt.execute(insertSql);
    // } catch (SQLException se) {
    // return INSERT_FAIL;
    // }
    //
    // try {
    // stmt.execute(deleteSql);
    // } catch (SQLException se) {
    // return DELETE_FAIL;
    // }
    } finally {
        closeConnection(conn, stmt);
        dataSourceCreator.destroyDataSource(dataSource);
    }
    return TABLE_SUCCESS;
}
Also used : Table(org.apache.ddlutils.model.Table) ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DataMediaSource(com.alibaba.otter.shared.common.model.config.data.DataMediaSource) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 10 with Table

use of org.apache.ddlutils.model.Table in project otter by alibaba.

the class DatabaseExtractor method checkNeedDbForRowMode.

private boolean checkNeedDbForRowMode(Pipeline pipeline, EventData eventData) {
    // 获取数据表信息
    DataMedia dataMedia = ConfigHelper.findDataMedia(pipeline, eventData.getTableId());
    DbDialect dbDialect = dbDialectFactory.getDbDialect(pipeline.getId(), (DbMediaSource) dataMedia.getSource());
    Table table = dbDialect.findTable(eventData.getSchemaName(), eventData.getTableName());
    if (table.getColumnCount() == eventData.getColumns().size() + eventData.getKeys().size()) {
        return false;
    } else {
        return true;
    }
}
Also used : Table(org.apache.ddlutils.model.Table) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) DataMedia(com.alibaba.otter.shared.common.model.config.data.DataMedia)

Aggregations

Table (org.apache.ddlutils.model.Table)19 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)10 Column (org.apache.ddlutils.model.Column)8 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)6 Test (org.testng.annotations.Test)6 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)5 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)5 DataMedia (com.alibaba.otter.shared.common.model.config.data.DataMedia)4 EventColumn (com.alibaba.otter.shared.etl.model.EventColumn)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)3 EventData (com.alibaba.otter.shared.etl.model.EventData)3 EventType (com.alibaba.otter.shared.etl.model.EventType)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 List (java.util.List)3 DataSource (javax.sql.DataSource)3 DataAccessException (org.springframework.dao.DataAccessException)3 PreparedStatementCallback (org.springframework.jdbc.core.PreparedStatementCallback)3