Search in sources :

Example 1 with JDBCMetadataProcessor

use of org.teiid.translator.jdbc.JDBCMetadataProcessor in project teiid by teiid.

the class MySQLExecutionFactory method getMetadataProcessor.

@Override
public MetadataProcessor<Connection> getMetadataProcessor() {
    return new JDBCMetadataProcessor() {

        @Override
        protected String getRuntimeType(int type, String typeName, int precision) {
            // mysql will otherwise report a 0/null type for geometry
            if ("geometry".equalsIgnoreCase(typeName)) {
                // $NON-NLS-1$
                return TypeFacility.RUNTIME_NAMES.GEOMETRY;
            }
            return super.getRuntimeType(type, typeName, precision);
        }

        @Override
        protected Column addColumn(ResultSet columns, Table table, MetadataFactory metadataFactory, int rsColumns) throws SQLException {
            Column c = super.addColumn(columns, table, metadataFactory, rsColumns);
            if (c.getPrecision() == 0 && "bit".equalsIgnoreCase(c.getNativeType())) {
                // $NON-NLS-1$
                c.setNativeType(TINYINT);
            }
            return c;
        }

        @Override
        protected void getTableStatistics(Connection conn, String catalog, String schema, String name, Table table) throws SQLException {
            PreparedStatement stmt = null;
            ResultSet rs = null;
            try {
                // $NON-NLS-1$
                stmt = conn.prepareStatement("SELECT cardinality FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = ? AND table_name = ?");
                if (catalog != null && schema == null) {
                    // mysql jdbc reports the schema as the catalog
                    stmt.setString(1, catalog);
                } else {
                    stmt.setString(1, schema);
                }
                stmt.setString(2, name);
                rs = stmt.executeQuery();
                if (rs.next()) {
                    int cardinality = rs.getInt(1);
                    if (!rs.wasNull()) {
                        table.setCardinality(cardinality);
                    }
                }
            } finally {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            }
        }

        @Override
        protected boolean isUnsignedTypeName(String name) {
            if (!name.contains("UNSIGNED")) {
                // $NON-NLS-1$
                return false;
            }
            return super.isUnsignedTypeName(name);
        }
    };
}
Also used : Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) JDBCMetadataProcessor(org.teiid.translator.jdbc.JDBCMetadataProcessor) Column(org.teiid.metadata.Column) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 2 with JDBCMetadataProcessor

use of org.teiid.translator.jdbc.JDBCMetadataProcessor in project teiid by teiid.

the class TeiidExecutionFactory method getMetadataProcessor.

@Override
public MetadataProcessor<Connection> getMetadataProcessor() {
    return new JDBCMetadataProcessor() {

        @Override
        protected String getRuntimeType(int type, String typeName, int precision) {
            if ("geometry".equalsIgnoreCase(typeName)) {
                // $NON-NLS-1$
                return TypeFacility.RUNTIME_NAMES.GEOMETRY;
            }
            return super.getRuntimeType(type, typeName, precision);
        }

        @Override
        protected void getGeometryMetadata(Column c, Connection conn, String tableCatalog, String tableSchema, String tableName, String columnName) {
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                if (tableCatalog == null) {
                    tableCatalog = conn.getCatalog();
                }
                // $NON-NLS-1$
                ps = conn.prepareStatement("select coord_dimension, srid, type from sys.geometry_columns where f_table_catalog=? and f_table_schema=? and f_table_name=? and f_geometry_column=?");
                ps.setString(1, tableCatalog);
                ps.setString(2, tableSchema);
                ps.setString(3, tableName);
                ps.setString(4, columnName);
                rs = ps.executeQuery();
                if (rs.next()) {
                    // $NON-NLS-1$
                    c.setProperty(MetadataFactory.SPATIAL_URI + "coord_dimension", rs.getString(1));
                    // $NON-NLS-1$
                    c.setProperty(MetadataFactory.SPATIAL_URI + "srid", rs.getString(2));
                    // $NON-NLS-1$
                    c.setProperty(MetadataFactory.SPATIAL_URI + "type", rs.getString(3));
                }
            } catch (SQLException e) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Could not get geometry metadata for column", tableSchema, tableName, columnName);
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                    }
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                    }
                }
            }
        }
    };
}
Also used : JDBCMetadataProcessor(org.teiid.translator.jdbc.JDBCMetadataProcessor) Column(org.teiid.metadata.Column) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with JDBCMetadataProcessor

use of org.teiid.translator.jdbc.JDBCMetadataProcessor in project teiid by teiid.

the class PhoenixExecutionFactory method createMetadataProcessor.

@Override
protected JDBCMetadataProcessor createMetadataProcessor() {
    JDBCMetadataProcessor processor = new JDBCMetadataProcessor() {

        @Override
        protected boolean getIndexInfoForTable(String catalogName, String schemaName, String tableName, boolean uniqueOnly, boolean approximateIndexes, String tableType) {
            // unique returns an empty result set that is not reusable
            return !uniqueOnly;
        }
    };
    // same issue with foreign keys
    processor.setImportForeignKeys(false);
    return processor;
}
Also used : JDBCMetadataProcessor(org.teiid.translator.jdbc.JDBCMetadataProcessor)

Example 4 with JDBCMetadataProcessor

use of org.teiid.translator.jdbc.JDBCMetadataProcessor in project teiid by teiid.

the class SQLServerExecutionFactory method getMetadataProcessor.

@Override
public MetadataProcessor<Connection> getMetadataProcessor() {
    return new JDBCMetadataProcessor() {

        @Override
        protected Column addColumn(ResultSet columns, Table table, MetadataFactory metadataFactory, int rsColumns) throws SQLException {
            Column c = super.addColumn(columns, table, metadataFactory, rsColumns);
            // The ms jdbc driver does not correctly report the auto incremented column
            if (!c.isAutoIncremented() && c.getNativeType() != null && StringUtil.endsWithIgnoreCase(c.getNativeType(), " identity")) {
                // $NON-NLS-1$
                c.setAutoIncremented(true);
            }
            return c;
        }

        @Override
        protected ResultSet executeSequenceQuery(Connection conn) throws SQLException {
            if (getVersion().compareTo(ELEVEN_0) < 0) {
                return null;
            }
            String query = // $NON-NLS-1$
            "select db_name() as sequence_catalog, SCHEMA_NAME(schema_id) as sequence_schema, name as sequence_name from sys.sequences" + // $NON-NLS-1$
            "where db_name() like ? and SCHEMA_NAME(schema_id) like ? and name like ?";
            PreparedStatement ps = conn.prepareStatement(query);
            // $NON-NLS-1$
            ps.setString(1, getCatalog() == null ? "%" : getCatalog());
            // $NON-NLS-1$
            ps.setString(2, getSchemaPattern() == null ? "%" : getSchemaPattern());
            // $NON-NLS-1$
            ps.setString(3, getSequenceNamePattern() == null ? "%" : getSequenceNamePattern());
            return ps.executeQuery();
        }
    };
}
Also used : Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) JDBCMetadataProcessor(org.teiid.translator.jdbc.JDBCMetadataProcessor) Column(org.teiid.metadata.Column) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 5 with JDBCMetadataProcessor

use of org.teiid.translator.jdbc.JDBCMetadataProcessor in project teiid by teiid.

the class AccessExecutionFactory method getMetadataProcessor.

@Override
public MetadataProcessor<Connection> getMetadataProcessor() {
    JDBCMetadataProcessor processor = new JDBCMetadataProcessor();
    // $NON-NLS-1$
    processor.setExcludeTables(".*[.]MSys.*");
    processor.setImportKeys(false);
    return processor;
}
Also used : JDBCMetadataProcessor(org.teiid.translator.jdbc.JDBCMetadataProcessor)

Aggregations

JDBCMetadataProcessor (org.teiid.translator.jdbc.JDBCMetadataProcessor)7 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)3 Column (org.teiid.metadata.Column)3 MetadataFactory (org.teiid.metadata.MetadataFactory)2 Table (org.teiid.metadata.Table)2 SQLException (java.sql.SQLException)1