Search in sources :

Example 31 with IMetadataConnection

use of org.talend.core.model.metadata.IMetadataConnection in project tdq-studio-se by Talend.

the class MultiColAnalysisCreationTest method getDataManager.

/**
 * DOC scorreia Comment method "getDataManager".
 *
 * @return
 */
public Connection getDataManager() {
    // $NON-NLS-1$
    TypedProperties connectionParams = PropertiesLoader.getProperties(AllDataProfilerCoreTests.class, "db.properties");
    // $NON-NLS-1$
    String driverClassName = connectionParams.getProperty("driver");
    // $NON-NLS-1$
    String dbUrl = connectionParams.getProperty("url");
    // $NON-NLS-1$
    String sqlTypeName = connectionParams.getProperty("sqlTypeName");
    DBConnectionParameter params = new DBConnectionParameter();
    // $NON-NLS-1$
    params.setName("My connection");
    params.setDriverClassName(driverClassName);
    params.setJdbcUrl(dbUrl);
    params.setSqlTypeName(sqlTypeName);
    params.setParameters(connectionParams);
    params.getParameters();
    // create connection
    ConnectionUtils.setTimeout(false);
    MetadataFillFactory instance = MetadataFillFactory.getDBInstance();
    IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(params));
    ReturnCode rc = null;
    try {
        rc = instance.checkConnection(metaConnection);
    } catch (java.lang.RuntimeException e) {
        Assert.fail("connect to " + dbUrl + "failed," + e.getMessage());
    }
    Connection dataProvider = null;
    if (rc.isOk()) {
        dataProvider = instance.fillUIConnParams(metaConnection, null);
        dataProvider.setName(DATA_PROVIDER_NAME);
        // because the DI side code is changed, modify the following code.
        metaConnection.setCurrentConnection(dataProvider);
        try {
            ProjectNodeHelper.fillCatalogAndSchemas(metaConnection, (DatabaseConnection) dataProvider);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    Assert.assertNotNull(dataProvider);
    return dataProvider;
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode) SQLException(java.sql.SQLException) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) TypedProperties(org.talend.utils.properties.TypedProperties) DBConnectionParameter(org.talend.dq.analysis.parameters.DBConnectionParameter) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory)

Example 32 with IMetadataConnection

use of org.talend.core.model.metadata.IMetadataConnection in project tdq-studio-se by Talend.

the class ManagedDriver method registerHiveSQLDriver.

/**
 * register hive jdbc driver by DatabaseConnection
 *
 * @param dbConnection
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws Exception
 */
public synchronized void registerHiveSQLDriver(DatabaseConnection dbConnection) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    if (dbConnection != null || driverClassName != null || driverClassName.length() != 0) {
        String dbType = dbConnection.getDatabaseType();
        if (dbType.equalsIgnoreCase(EDatabaseTypeName.HIVE.getXmlName())) {
            unregisterSQLDriver();
            IMetadataConnection metadataConn = ConvertionHelper.convert(dbConnection);
            ClassLoader classLoader = HiveClassLoaderFactory.getInstance().getClassLoader(metadataConn);
            Class<?> classDriver = Class.forName(driverClassName, true, classLoader);
            jdbcDriver = (Driver) classDriver.newInstance();
        } else if (dbType.equalsIgnoreCase(EDatabaseTypeName.IMPALA.getXmlName())) {
            unregisterSQLDriver();
            IMetadataConnection metadataConn = ConvertionHelper.convert(dbConnection);
            ClassLoader classLoader = AliasAndManaDriverHelper.getInstance().getImpalaClassLoader(metadataConn);
            Class<?> classDriver = Class.forName(driverClassName, true, classLoader);
            jdbcDriver = (Driver) classDriver.newInstance();
        }
    }
    if (jdbcDriver == null) {
        log.error("fail to regist Hive jdbc driver in SQLExplorer");
    }
}
Also used : HotClassLoader(org.talend.core.model.metadata.builder.database.HotClassLoader) ISQLDriver(net.sourceforge.squirrel_sql.fw.sql.ISQLDriver) Driver(java.sql.Driver) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection)

Example 33 with IMetadataConnection

use of org.talend.core.model.metadata.IMetadataConnection in project tdq-studio-se by Talend.

the class User method createNewConnection.

/**
 * Creates a new connection, MOD xqliu 2013-04-03 TDQ-7003
 *
 * @return
 * @throws ExplorerException
 * @throws SQLException
 */
protected synchronized SQLConnection createNewConnection() throws SQLException {
    SQLConnection connection = null;
    // need use DynamicClassLoader to deal with it
    if (databaseConnection != null && EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(databaseConnection.getDatabaseType())) {
        if (GlobalServiceRegister.getDefault().isServiceRegistered(ITDQRepositoryService.class)) {
            ITDQRepositoryService tdqRepService = (ITDQRepositoryService) GlobalServiceRegister.getDefault().getService(ITDQRepositoryService.class);
            if (tdqRepService != null) {
                IMetadataConnection mdConn = ConvertionHelper.convert(databaseConnection);
                Connection hiveConnection = tdqRepService.createHiveConnection(mdConn);
                if (hiveConnection != null) {
                    connection = new SQLConnection(this, hiveConnection, alias.getDriver(), "HiveConnection");
                }
            }
        }
    } else {
        connection = alias.getDriver().getConnection(this);
    }
    return connection;
}
Also used : ITDQRepositoryService(org.talend.core.ITDQRepositoryService) Connection(java.sql.Connection) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection)

Example 34 with IMetadataConnection

use of org.talend.core.model.metadata.IMetadataConnection in project tdq-studio-se by Talend.

the class TdqAnalysisConnectionPool method newConnection.

/**
 * DOC xqliu Comment method "newConnection".
 *
 * @return
 */
private Connection newConnection() {
    Connection conn = null;
    if (isFull()) {
        return conn;
    }
    DataManager datamanager = analysis.getContext().getConnection();
    if (datamanager == null) {
        // $NON-NLS-1$
        log.error(Messages.getString("AnalysisExecutor.DataManagerNull", analysis.getName()));
        return null;
    }
    if (datamanager != null && datamanager.eIsProxy()) {
        datamanager = (DataManager) EObjectHelper.resolveObject(datamanager);
    }
    org.talend.core.model.metadata.builder.connection.Connection dataprovider = SwitchHelpers.CONNECTION_SWITCH.doSwitch(datamanager);
    TypedReturnCode<Connection> trcConn = null;
    IMetadataConnection metadataConnection = ConvertionHelper.convert(dataprovider);
    if (metadataConnection != null && EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(metadataConnection.getDbType())) {
        trcConn = new TypedReturnCode<Connection>(false);
        try {
            HiveConnectionHandler hiveConnHandler = HiveConnectionManager.getInstance().createHandler(metadataConnection);
            Connection hiveConnection = hiveConnHandler.createHiveConnection();
            if (hiveConnection != null) {
                trcConn.setOk(true);
                trcConn.setObject(hiveConnection);
            }
        } catch (ClassNotFoundException e) {
            trcConn.setOk(false);
            log.error(e);
        } catch (InstantiationException e) {
            trcConn.setOk(false);
            log.error(e);
        } catch (IllegalAccessException e) {
            trcConn.setOk(false);
            log.error(e);
        } catch (SQLException e) {
            trcConn.setOk(false);
            log.error(e);
        }
    } else {
        trcConn = JavaSqlFactory.createConnection(dataprovider);
    }
    if (trcConn != null && trcConn.isOk()) {
        conn = trcConn.getObject();
        synchronized (this.synchronizedFlag) {
            this.getPConnections().add(new PooledTdqAnalysisConnection(conn));
        }
    }
    if (conn != null) {
        try {
            if (metadataConnection != null && EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(metadataConnection.getDbType())) {
            // don't set the max connection number if it is hive connection
            } else {
                DatabaseMetaData metaData = conn.getMetaData();
                int currentDriverMaxConnections = new Float(metaData.getMaxConnections() * DEFAULT_CONNECTION_NUMBER_OFFSET).intValue();
                synchronized (this.synchronizedFlag) {
                    this.setDriverMaxConnections(currentDriverMaxConnections);
                }
            }
        } catch (SQLException e) {
            log.debug(e, e);
        }
    }
    return conn;
}
Also used : HiveConnectionHandler(org.talend.metadata.managment.hive.handler.HiveConnectionHandler) SQLException(java.sql.SQLException) Connection(java.sql.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DataManager(orgomg.cwm.foundation.softwaredeployment.DataManager) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 35 with IMetadataConnection

use of org.talend.core.model.metadata.IMetadataConnection in project tdq-studio-se by Talend.

the class ConnectionService method createConnection.

public static TypedReturnCode<Connection> createConnection(DBConnectionParameter parameter) {
    TypedReturnCode<Connection> tReturnCode = new TypedReturnCode<Connection>(false);
    MetadataFillFactory instance = null;
    instance = MetadataFillFactory.getDBInstance();
    IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(parameter));
    ReturnCode rc = instance.createConnection(metaConnection);
    if (rc.isOk()) {
        Connection dbConn = instance.fillUIConnParams(metaConnection, null);
        DatabaseMetaData dbMetadata = null;
        List<String> packageFilter = ConnectionUtils.getPackageFilter(parameter);
        java.sql.Connection sqlConn = null;
        try {
            if (rc instanceof TypedReturnCode) {
                @SuppressWarnings("rawtypes") Object sqlConnObject = ((TypedReturnCode) rc).getObject();
                if (sqlConnObject instanceof java.sql.Connection) {
                    sqlConn = (java.sql.Connection) sqlConnObject;
                    dbMetadata = org.talend.utils.sql.ConnectionUtils.getConnectionMetadata(sqlConn);
                }
            }
            instance.fillCatalogs(dbConn, dbMetadata, packageFilter);
            instance.fillSchemas(dbConn, dbMetadata, packageFilter);
            tReturnCode.setObject(dbConn);
        } catch (SQLException e) {
            log.error(e, e);
        // Need to add a dialog for report the reson of error
        } finally {
            if (sqlConn != null) {
                ConnectionUtils.closeConnection(sqlConn);
            }
        }
    } else {
        tReturnCode.setMessage(rc.getMessage());
        tReturnCode.setOk(false);
    }
    return tReturnCode;
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) SQLException(java.sql.SQLException) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DatabaseMetaData(java.sql.DatabaseMetaData) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory)

Aggregations

IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)38 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)27 SQLException (java.sql.SQLException)18 Connection (org.talend.core.model.metadata.builder.connection.Connection)14 ArrayList (java.util.ArrayList)12 List (java.util.List)7 MetadataFillFactory (org.talend.metadata.managment.model.MetadataFillFactory)7 ReturnCode (org.talend.utils.sugars.ReturnCode)7 Properties (java.util.Properties)6 PersistenceException (org.talend.commons.exception.PersistenceException)6 DatabaseConnectionItem (org.talend.core.model.properties.DatabaseConnectionItem)6 DatabaseMetaData (java.sql.DatabaseMetaData)5 DBConnectionParameter (org.talend.dq.analysis.parameters.DBConnectionParameter)5 Shell (org.eclipse.swt.widgets.Shell)4 ErrorDialogWithDetailAreaAndContinueButton (org.talend.commons.ui.swt.dialogs.ErrorDialogWithDetailAreaAndContinueButton)4 DelimitedFileConnection (org.talend.core.model.metadata.builder.connection.DelimitedFileConnection)4 TypedReturnCode (org.talend.utils.sugars.TypedReturnCode)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3