public DatabaseHelper( IDatabaseDialectService databaseDialectService ) { this.databaseTypeHelper = new DatabaseTypeHelper( databaseDialectService.getDatabaseTypes() ); }
/** * Will resolve a database type based on the prefix of the jdbc URL, which we assume originated from one of these * classes * * @param urlPrefix * @return */ private IDatabaseType resolveDatabaseType( String urlPrefix ) { DatabaseDialectService databaseDialectService = new DatabaseDialectService( false ); List<IDatabaseDialect> databaseDialects = databaseDialectService.getDatabaseDialects(); String nativePre = null; for ( IDatabaseDialect databaseDialect : databaseDialects ) { // // NOTE - The GenericDatabaseDialect and the AccessDatabaseDialect // both return null for the value of getNativeJdbcPre - so - this // requires a null-check. // // MB // nativePre = databaseDialect.getNativeJdbcPre(); if ( ( nativePre != null ) && ( nativePre.startsWith( urlPrefix ) ) ) { return databaseDialect.getDatabaseType(); } } return null; }
/** * This service method overrides it's parent and removes all the database types other than native. */ @Override public List<IDatabaseType> getDatabaseTypes() { synchronized ( lock ) { List<IDatabaseType> databaseTypes = super.getDatabaseTypes(); for ( IDatabaseType type : databaseTypes ) { Iterator<DatabaseAccessType> iter = type.getSupportedAccessTypes().iterator(); while ( iter.hasNext() ) { DatabaseAccessType accessType = iter.next(); if ( accessType != DatabaseAccessType.NATIVE ) { iter.remove(); } } } return databaseTypes; } } }
@Test public void testCreatePoolNoDialectService() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper( dialectService.getDatabaseTypes() ); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setDatabaseType( databaseTypeHelper.getDatabaseTypeByShortName( "GENERIC" ) ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "org.postgresql.Driver" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" ); con.setAttributes( attrs ); try { PooledDatasourceHelper.setupPooledDataSource( con ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
@Override public DatabaseConnection unmarshal( DatabaseConnectionDto dbConnDto ) throws Exception { if ( dbConnDto != null ) { IDatabaseDialectService databaseDialectService = new DatabaseDialectService(); DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper( databaseDialectService.getDatabaseTypes() ); DatabaseConnection dbConn = new DatabaseConnection(); dbConn.setId( dbConnDto.getId() );
public ConnectionService() { connectionService = new ConnectionServiceImpl(); this.dialectService = new DatabaseDialectService( true ); sanitizer = UtilHtmlSanitizer.getInstance(); }
@Test( expected = DriverNotInitializedException.class ) public void testDriverNotInitialized() throws DBDatasourceServiceException { when( dialectService.getDialect( connection ) ).thenReturn( driverLocatorDialect ); when( ( (IDriverLocator) driverLocatorDialect ).initialize( nativeDriverName ) ).thenReturn( false ); PooledDatasourceHelper.convert( connection, () -> dialectService ); }
/** * Get the dialect of the given IDatabaseType * * @param databaseType IDatabaseType object to get the dialect of * @return IDatabaseDialect containing the dialect of databaseType */ @POST @Path( "/getDialectByType" ) @Consumes( { APPLICATION_JSON } ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public IDatabaseDialect getDialectWS( IDatabaseType databaseType ) { return super.getDialect( databaseType ); }
@Override public String testConnection( IDatabaseConnection connection ) { try { connection .setPassword( ConnectionServiceHelper.getConnectionPassword( connection.getName(), connection.getPassword() ) ); return super.testConnection( connection ); } catch ( ConnectionServiceException e ) { return super.testConnection( connection ); } } }
/** * Attempt to load the JDBC Driver class. If it's not available, return false. * * @param classname * validate that this classname exists in the classpath * * @return true if the class exists */ @POST @Path( "/validateJdbcDriverClassExists" ) @Consumes( { APPLICATION_JSON } ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public Boolean validateJdbcDriverClassExistsWS( String classname ) { return super.validateJdbcDriverClassExists( classname ); }
/** * Get a list of the database dialects * * @return IDatabaseDialectList containing the database dialects */ @GET @Path( "/getDatabaseDialects" ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public IDatabaseDialectList getDatabaseDialectsWS() { IDatabaseDialectList value = new DefaultDatabaseDialectList(); value.setDialects( super.getDatabaseDialects() ); return value; } }
@Test public void testCreateDatasourceNoClassName() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper( dialectService.getDatabaseTypes() ); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setDatabaseType( databaseTypeHelper.getDatabaseTypeByShortName( "GENERIC" ) ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" ); con.setAttributes( attrs ); try { PooledDatasourceHelper.convert( con, () -> dialectService ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
@Test public void testCreateDatasourceNoDialect() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "org.postgresql.Driver" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" ); con.setAttributes( attrs ); try { PooledDatasourceHelper.convert( con, () -> dialectService ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
public DatabaseMetaReadHandler() { final ObjectFactory objectFactory = ClassicEngineBoot.getInstance().getObjectFactory(); final IDatabaseDialectService dialectService = objectFactory.get( IDatabaseDialectService.class ); this.databaseTypeHelper = new DatabaseTypeHelper( dialectService.getDatabaseTypes() ); }
@Before public void before() throws DatabaseDialectException { when( dialectService.getDialect( connection ) ).thenReturn( driverLocatorDialect ); when( connection.getDatabaseType() ).thenReturn( databaseType ); when( connection.getDatabaseType().getShortName() ).thenReturn( "SomeDBType" ); when( driverLocatorDialect.getNativeDriver() ).thenReturn( nativeDriverName ); when( driverLocatorDialect.getURLWithExtraOptions( connection ) ).thenReturn( jdbcUrl ); when( plainDialect.getNativeDriver() ).thenReturn( nativeDriverName ); when( plainDialect.getURLWithExtraOptions( connection ) ).thenReturn( jdbcUrl ); }
/** * Get a list of the database types * * @return IDatabaseTypesList containing the database types */ @GET @Path( "/getDatabaseTypes" ) @Facet ( name = "Unsupported" ) @Produces( { APPLICATION_JSON } ) public IDatabaseTypesList getDatabaseTypesWS() { DefaultDatabaseTypesList value = new DefaultDatabaseTypesList(); value.setDbTypes( super.getDatabaseTypes() ); return value; }
/** * Get the dialect of a given IDatabaseConnection * * @param connection IDatabaseConnection object to get the dialect of * * @return IDatabaseDialect of the given connection */ @POST @Path( "/getDialectByConnection" ) @Consumes( { APPLICATION_JSON } ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public IDatabaseDialect getDialect( IDatabaseConnection connection ) { return super.getDialect( connection ); }
@Test public void testCreatePoolNoDialect() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); mp = new MicroPlatform( SOLUTION_PATH ); mp.defineInstance( IDatabaseDialectService.class, dialectService ); mp.start(); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" ); con.setAttributes( attrs ); try { PooledDatasourceHelper.setupPooledDataSource( con ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
@Test public void testDialectWithNoDriverSpecified() throws DBDatasourceServiceException { when( dialectService.getDialect( connection ) ).thenReturn( driverLocatorDialect ); when( driverLocatorDialect.getNativeDriver() ).thenReturn( "" ); try { PooledDatasourceHelper.convert( connection, () -> dialectService ); fail( "Expected exception, driver class not specified in dialect." ); } catch ( Exception e ) { assertThat( e, instanceOf( DBDatasourceServiceException.class ) ); } }
@Test public void testDialectWithoutLocatorAndDriverNotPresent() throws DBDatasourceServiceException { when( dialectService.getDialect( connection ) ).thenReturn( plainDialect ); try { PooledDatasourceHelper.convert( connection, () -> dialectService ); fail( "Expected exception, driver class should not be present." ); } catch ( Exception e ) { assertThat( e, instanceOf( DBDatasourceServiceException.class ) ); assertThat( e.getCause().getMessage(), containsString( nativeDriverName ) ); } }