Search in sources :

Example 6 with DatabaseConnection

use of org.pentaho.database.model.DatabaseConnection in project pentaho-platform by pentaho.

the class PooledDatasourceHelperTest method testCreateDatasourceNoClassName.

@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);
    }
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) DatabaseTypeHelper(org.pentaho.database.util.DatabaseTypeHelper) HashMap(java.util.HashMap) DatabaseDialectService(org.pentaho.database.service.DatabaseDialectService) IDatabaseDialectService(org.pentaho.database.service.IDatabaseDialectService) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 7 with DatabaseConnection

use of org.pentaho.database.model.DatabaseConnection in project pentaho-platform by pentaho.

the class PooledDatasourceHelperTest method testCreateDatasourceNoDialect.

@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);
    }
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) HashMap(java.util.HashMap) DatabaseDialectService(org.pentaho.database.service.DatabaseDialectService) IDatabaseDialectService(org.pentaho.database.service.IDatabaseDialectService) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with DatabaseConnection

use of org.pentaho.database.model.DatabaseConnection in project pentaho-platform by pentaho.

the class DatabaseHelper method databaseMetaToDatabaseConnection.

public IDatabaseConnection databaseMetaToDatabaseConnection(final DatabaseMeta databaseMeta) {
    IDatabaseConnection databaseConnection = new DatabaseConnection();
    databaseConnection.setDatabaseType(databaseTypeHelper.getDatabaseTypeByShortName(databaseMeta.getDatabaseTypeDesc()));
    databaseConnection.setName(databaseMeta.getName());
    if (databaseMeta.getObjectId() != null) {
        databaseConnection.setId(databaseMeta.getObjectId().getId());
    }
    String accessType = databaseMeta.getAccessTypeDesc();
    // This is a special case with some PDI connections
    if (accessType != null && accessType.contains("Native")) {
        accessType = DatabaseAccessType.NATIVE.getName();
    } else if (accessType != null && accessType.equals(", ")) {
        accessType = DatabaseAccessType.JNDI.getName();
    }
    databaseConnection.setAccessType(accessType != null ? DatabaseAccessType.getAccessTypeByName(accessType) : null);
    databaseConnection.setHostname(databaseMeta.getHostname());
    databaseConnection.setDatabaseName(databaseMeta.getDatabaseName());
    databaseConnection.setDatabasePort(databaseMeta.getDatabasePortNumberString());
    databaseConnection.setUsername(databaseMeta.getUsername());
    databaseConnection.setPassword(databaseMeta.getPassword());
    databaseConnection.setInformixServername(databaseMeta.getServername());
    databaseConnection.setDataTablespace(databaseMeta.getDataTablespace());
    databaseConnection.setIndexTablespace(databaseMeta.getIndexTablespace());
    databaseConnection.setConnectSql(databaseMeta.getConnectSQL());
    databaseConnection.setInitialPoolSize(databaseMeta.getInitialPoolSize());
    databaseConnection.setMaximumPoolSize(databaseMeta.getMaximumPoolSize());
    databaseConnection.setUsingConnectionPool(databaseMeta.isUsingConnectionPool());
    databaseConnection.setForcingIdentifiersToLowerCase(databaseMeta.isForcingIdentifiersToLowerCase());
    databaseConnection.setForcingIdentifiersToUpperCase(databaseMeta.isForcingIdentifiersToUpperCase());
    databaseConnection.setQuoteAllFields(databaseMeta.isQuoteAllFields());
    databaseConnection.setUsingDoubleDecimalAsSchemaTableSeparator(databaseMeta.isUsingDoubleDecimalAsSchemaTableSeparator());
    Map<String, String> attributeMap = new HashMap<String, String>();
    for (Entry<Object, Object> entry : databaseMeta.getAttributes().entrySet()) {
        attributeMap.put((String) entry.getKey(), (String) entry.getValue());
    }
    databaseConnection.setAttributes(attributeMap);
    Map<String, String> connectionPoolingMap = new HashMap<String, String>();
    for (Entry<Object, Object> entry : databaseMeta.getConnectionPoolingProperties().entrySet()) {
        connectionPoolingMap.put((String) entry.getKey(), (String) entry.getValue());
    }
    databaseConnection.setConnectionPoolingProperties(connectionPoolingMap);
    databaseConnection.setExtraOptions(databaseMeta.getExtraOptions());
    return databaseConnection;
}
Also used : HashMap(java.util.HashMap) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection)

Example 9 with DatabaseConnection

use of org.pentaho.database.model.DatabaseConnection in project pentaho-platform by pentaho.

the class DatabaseHelper method dataNodeToDatabaseConnection.

public IDatabaseConnection dataNodeToDatabaseConnection(final Serializable id, final String name, final DataNode rootNode) {
    IDatabaseConnection databaseConnection = new DatabaseConnection();
    String databaseType = getString(rootNode, PROP_TYPE);
    databaseConnection.setDatabaseType(databaseType != null ? databaseTypeHelper.getDatabaseTypeByShortName(databaseType) : null);
    databaseConnection.setName(name);
    if (id != null) {
        databaseConnection.setId(id.toString());
    }
    String accessType = getString(rootNode, PROP_CONTYPE);
    // This is a special case with some PDI connections
    if (accessType != null && accessType.contains("Native")) {
        accessType = DatabaseAccessType.NATIVE.getName();
    } else if (accessType != null && accessType.equals(", ")) {
        accessType = DatabaseAccessType.JNDI.getName();
    }
    databaseConnection.setAccessType(accessType != null ? DatabaseAccessType.getAccessTypeByName(accessType) : null);
    databaseConnection.setHostname(getString(rootNode, PROP_HOST_NAME));
    databaseConnection.setDatabaseName(getString(rootNode, PROP_DATABASE_NAME));
    databaseConnection.setDatabasePort(getString(rootNode, PROP_PORT));
    databaseConnection.setUsername(getString(rootNode, PROP_USERNAME));
    databaseConnection.setPassword(decryptPassword(getString(rootNode, PROP_PASSWORD)));
    databaseConnection.setInformixServername(getString(rootNode, PROP_SERVERNAME));
    databaseConnection.setDataTablespace(getString(rootNode, PROP_DATA_TBS));
    databaseConnection.setIndexTablespace(getString(rootNode, PROP_INDEX_TBS));
    databaseConnection.setConnectSql(getString(rootNode, PROP_CONNECT_SQL));
    databaseConnection.setInitialPoolSize(getInt(rootNode, PROP_INITIAL_POOL_SIZE));
    databaseConnection.setMaximumPoolSize(getInt(rootNode, PROP_MAX_POOL_SIZE));
    databaseConnection.setUsingConnectionPool(getBoolean(rootNode, PROP_IS_POOLING));
    databaseConnection.setForcingIdentifiersToLowerCase(getBoolean(rootNode, PROP_IS_FORCING_TO_LOWER));
    databaseConnection.setForcingIdentifiersToUpperCase(getBoolean(rootNode, PROP_IS_FORCING_TO_UPPER));
    databaseConnection.setQuoteAllFields(getBoolean(rootNode, PROP_IS_QUOTE_FIELDS));
    databaseConnection.setUsingDoubleDecimalAsSchemaTableSeparator(getBoolean(rootNode, PROP_IS_DECIMAL_SEPERATOR));
    // Also, load all the properties we can find...
    DataNode attrNode = rootNode.getNode(NODE_ATTRIBUTES);
    if (attrNode != null) {
        for (DataProperty property : attrNode.getProperties()) {
            String code = property.getName();
            String attribute = property.getString();
            databaseConnection.getAttributes().put(code, // $NON-NLS-1$
            (attribute == null || attribute.length() == 0) ? "" : attribute);
        }
    }
    // Also, load any pooling params
    attrNode = rootNode.getNode(NODE_POOLING_PROPS);
    if (attrNode != null) {
        for (DataProperty property : attrNode.getProperties()) {
            String code = property.getName();
            String attribute = property.getString();
            databaseConnection.getConnectionPoolingProperties().put(code, // $NON-NLS-1$
            (attribute == null || attribute.length() == 0) ? "" : attribute);
        }
    }
    // Load extra options
    attrNode = rootNode.getNode(NODE_EXTRA_OPTIONS);
    if (attrNode != null) {
        for (DataProperty property : attrNode.getProperties()) {
            String code = property.getName();
            String attribute = property.getString();
            databaseConnection.getExtraOptions().put(code, // $NON-NLS-1$
            (attribute == null || attribute.length() == 0) ? "" : attribute);
        }
    }
    attrNode = rootNode.getNode(NODE_EXTRA_OPTIONS_ORDER);
    if (attrNode != null) {
        for (DataProperty property : attrNode.getProperties()) {
            String code = property.getName();
            String attribute = property.getString();
            databaseConnection.getExtraOptionsOrder().put(code, // $NON
            (attribute == null || attribute.length() == 0) ? "" : attribute);
        }
    }
    return databaseConnection;
}
Also used : DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DataProperty(org.pentaho.platform.api.repository2.unified.data.node.DataProperty) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection)

Example 10 with DatabaseConnection

use of org.pentaho.database.model.DatabaseConnection in project data-access by pentaho.

the class LegacyDatasourceConverter method unmarshal.

/**
 * Convert textual data back into an object.
 *
 * @param reader  The stream to read the text from.
 * @param context
 * @return The resulting object.
 */
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    MultiTableDatasourceDTO resultDTO = new MultiTableDatasourceDTO();
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        String nodeName = reader.getNodeName();
        if (nodeName.equalsIgnoreCase("datasourceName")) {
            String value = reader.getValue();
            resultDTO.setDatasourceName(value);
        } else if (nodeName.equalsIgnoreCase("selectedConnection")) {
            String connectionClass = reader.getAttribute("class");
            if (connectionClass != null) {
                DatabaseConnection databaseConnection = new DatabaseConnection();
                if (connectionClass.equals("org.pentaho.platform.dataaccess.datasource.beans.Connection")) {
                    while (reader.hasMoreChildren()) {
                        reader.moveDown();
                        nodeName = reader.getNodeName();
                        if (reader.getNodeName().equalsIgnoreCase("name")) {
                            String databaseName = reader.getValue();
                            databaseConnection.setName(databaseName);
                            databaseConnection.setId(databaseName);
                        } else if (reader.getNodeName().equalsIgnoreCase("username")) {
                            databaseConnection.setUsername(reader.getValue());
                        } else if (reader.getNodeName().equalsIgnoreCase("password")) {
                            databaseConnection.setPassword(reader.getValue());
                        } else if (reader.getNodeName().equalsIgnoreCase("url")) {
                            ParsedJdbcUrl parsedJdbcUrl = new ParsedJdbcUrl(reader.getValue());
                            databaseConnection.setHostname(parsedJdbcUrl.getHostname());
                            databaseConnection.setDatabasePort(parsedJdbcUrl.getPort());
                            databaseConnection.setDatabaseName(parsedJdbcUrl.getDatabaseName());
                            databaseConnection.setDatabaseType(resolveDatabaseType(parsedJdbcUrl.getJdbcPrefix()));
                        }
                        reader.moveUp();
                    }
                    resultDTO.setSelectedConnection(databaseConnection);
                } else {
                    // instantiate the class specified
                    try {
                        Class databaseConnectionClass = Class.forName(connectionClass);
                        IDatabaseConnection databaseConnectionInstance = (IDatabaseConnection) context.convertAnother(resultDTO, databaseConnectionClass);
                        resultDTO.setSelectedConnection(databaseConnectionInstance);
                    } catch (ClassNotFoundException e) {
                        // not going to work anyway, set empty connection for now
                        resultDTO.setSelectedConnection(new DatabaseConnection());
                    }
                }
            }
        } else if (nodeName.equalsIgnoreCase("schemaModel")) {
            SchemaModel schemaModel = (SchemaModel) context.convertAnother(resultDTO, SchemaModel.class);
            if (schemaModel != null) {
                resultDTO.setSchemaModel(schemaModel);
            }
        } else if (nodeName.equalsIgnoreCase("selectedTables")) {
            List<String> selectedTables = (List<String>) context.convertAnother(resultDTO, ArrayList.class);
            if (selectedTables != null) {
                resultDTO.setSelectedTables(selectedTables);
            }
        } else if (nodeName.equalsIgnoreCase("doOlap")) {
            resultDTO.setDoOlap(Boolean.valueOf(reader.getValue()));
        }
        reader.moveUp();
    }
    return resultDTO;
}
Also used : MultiTableDatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.service.impl.MultiTableDatasourceDTO) SchemaModel(org.pentaho.agilebi.modeler.models.SchemaModel) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) ArrayList(java.util.ArrayList) List(java.util.List) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection)

Aggregations

DatabaseConnection (org.pentaho.database.model.DatabaseConnection)56 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)48 Test (org.junit.Test)36 HashMap (java.util.HashMap)10 IDatabaseDialectService (org.pentaho.database.service.IDatabaseDialectService)7 DatabaseType (org.pentaho.database.model.DatabaseType)6 DatabaseDialectService (org.pentaho.database.service.DatabaseDialectService)6 DatabaseTypeHelper (org.pentaho.database.util.DatabaseTypeHelper)6 ArrayList (java.util.ArrayList)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 IDatabaseType (org.pentaho.database.model.IDatabaseType)5 DBDatasourceServiceException (org.pentaho.platform.api.data.DBDatasourceServiceException)5 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)5 Response (javax.ws.rs.core.Response)4 List (java.util.List)3 LinkedList (java.util.LinkedList)2 Mockito.anyString (org.mockito.Mockito.anyString)2 DatabaseAccessType (org.pentaho.database.model.DatabaseAccessType)2 PartitionDatabaseMeta (org.pentaho.database.model.PartitionDatabaseMeta)2 Category (org.pentaho.metadata.model.Category)2