Search in sources :

Example 1 with DatabaseMeta

use of org.pentaho.di.core.database.DatabaseMeta in project pentaho-kettle by pentaho.

the class ValueMetaBaseTest method testSetPreparedStatementStringValueLogTruncated.

@Test
public void testSetPreparedStatementStringValueLogTruncated() throws KettleDatabaseException {
    ValueMetaBase valueMetaString = new ValueMetaBase("LOG_FIELD", ValueMetaInterface.TYPE_STRING, LOG_FIELD.length(), 0);
    DatabaseMeta databaseMeta = Mockito.mock(DatabaseMeta.class);
    PreparedStatement preparedStatement = Mockito.mock(PreparedStatement.class);
    Mockito.when(databaseMeta.getMaxTextFieldLength()).thenReturn(MAX_TEXT_FIELD_LEN);
    List<KettleLoggingEvent> events = listener.getEvents();
    assertEquals(0, events.size());
    valueMetaString.setPreparedStatementValue(databaseMeta, preparedStatement, 0, LOG_FIELD);
    // check that truncated string was logged
    assertEquals(1, events.size());
}
Also used : PreparedStatement(java.sql.PreparedStatement) KettleLoggingEvent(org.pentaho.di.core.logging.KettleLoggingEvent) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) NetezzaDatabaseMeta(org.pentaho.di.core.database.NetezzaDatabaseMeta) MySQLDatabaseMeta(org.pentaho.di.core.database.MySQLDatabaseMeta) Vertica5DatabaseMeta(org.pentaho.di.core.database.Vertica5DatabaseMeta) Test(org.junit.Test)

Example 2 with DatabaseMeta

use of org.pentaho.di.core.database.DatabaseMeta in project pentaho-kettle by pentaho.

the class Spoon method refreshDbConnectionsSubtree.

@VisibleForTesting
void refreshDbConnectionsSubtree(TreeItem tiRootName, AbstractMeta meta, GUIResource guiResource) {
    TreeItem tiDbTitle = createTreeItem(tiRootName, STRING_CONNECTIONS, guiResource.getImageFolder());
    DatabasesCollector collector = new DatabasesCollector(meta, rep);
    try {
        try {
            collector.collectDatabases();
        } catch (KettleException e) {
            if (e.getCause() instanceof KettleRepositoryLostException) {
                handleRepositoryLost((KettleRepositoryLostException) e.getCause());
                collector = new DatabasesCollector(meta, null);
                collector.collectDatabases();
            } else {
                throw e;
            }
        }
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "Spoon.ErrorDialog.Title"), BaseMessages.getString(PKG, "Spoon.ErrorDialog.ErrorFetchingFromRepo.DbConnections"), e);
    }
    for (String dbName : collector.getDatabaseNames()) {
        if (!filterMatch(dbName)) {
            continue;
        }
        DatabaseMeta databaseMeta = collector.getMetaFor(dbName);
        TreeItem tiDb = createTreeItem(tiDbTitle, databaseMeta.getDisplayName(), guiResource.getImageConnectionTree());
        if (databaseMeta.isShared()) {
            tiDb.setFont(guiResource.getFontBold());
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) TreeItem(org.eclipse.swt.widgets.TreeItem) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleRepositoryLostException(org.pentaho.di.repository.KettleRepositoryLostException) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with DatabaseMeta

use of org.pentaho.di.core.database.DatabaseMeta in project pentaho-platform by pentaho.

the class SqlMetadataQueryExec method executeQuery.

public IPentahoResultSet executeQuery(Query queryObject) {
    // need to get the correct DatabaseMeta
    SqlPhysicalModel sqlModel = (SqlPhysicalModel) queryObject.getLogicalModel().getPhysicalModel();
    DatabaseMeta databaseMeta = ThinModelConverter.convertToLegacy(sqlModel.getId(), sqlModel.getDatasource());
    // this connection needs closed
    boolean closeConnection = true;
    DatabaseMeta activeDatabaseMeta = getActiveDatabaseMeta(databaseMeta);
    SQLConnection sqlConnection = getConnection(activeDatabaseMeta);
    String sql = null;
    try {
        if ((sqlConnection == null) || !sqlConnection.initialized()) {
            // $NON-NLS-1$
            logger.error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0007_NO_CONNECTION"));
            // TODO: throw an exception up the stack.
            return null;
        }
        // Fix for PDB-1753
        for (Parameter param : queryObject.getParameters()) {
            String pName = param.getName();
            if (parameters.containsKey(pName) && parameters.get(pName) != null && !parameters.get(pName).getClass().isArray()) {
                parameters.put(pName, this.convertParameterValue(param, parameters.get(pName)));
            }
        }
        MappedQuery mappedQuery = null;
        try {
            SqlGenerator sqlGenerator = createSqlGenerator();
            mappedQuery = sqlGenerator.generateSql(queryObject, LocaleHelper.getLocale().toString(), getMetadataDomainRepository(), activeDatabaseMeta, parameters, true);
        } catch (Exception e) {
            throw new RuntimeException(e.getLocalizedMessage(), e);
        }
        Integer timeout = getTimeout();
        if (timeout != null && timeout >= 0) {
            sqlConnection.setQueryTimeout(timeout);
        }
        Integer maxRows = getMaxRows();
        if (maxRows != null && maxRows >= 0) {
            sqlConnection.setMaxRows(maxRows);
        }
        Boolean readOnly = isReadOnly();
        if (readOnly != null && readOnly.booleanValue()) {
            sqlConnection.setReadOnly(true);
        }
        IPentahoResultSet localResultSet = null;
        sql = mappedQuery.getQuery();
        if (logger.isDebugEnabled()) {
            // $NON-NLS-1$
            logger.debug("SQL: " + sql);
        }
        if (getDoQueryLog()) {
            // $NON-NLS-1$
            logger.info("SQL: " + sql);
        }
        // populate prepared sql params
        List<Object> sqlParams = null;
        if (mappedQuery.getParamList() != null) {
            sqlParams = new ArrayList<Object>();
            for (String param : mappedQuery.getParamList()) {
                Object sqlParam = parameters.get(param);
                // lets see if the parameter is a multi valued param
                if (sqlParam instanceof Object[]) {
                    Object[] multivaluedParamValues = (Object[]) sqlParam;
                    for (Object p : multivaluedParamValues) {
                        sqlParams.add(p);
                    }
                    if (multivaluedParamValues.length == 0) {
                        sqlParams.add("");
                    }
                } else {
                    sqlParams.add(sqlParam);
                }
            }
        }
        try {
            if (!isForwardOnly()) {
                if (sqlParams != null) {
                    localResultSet = sqlConnection.prepareAndExecuteQuery(sql, sqlParams);
                } else {
                    localResultSet = sqlConnection.executeQuery(sql);
                }
            } else {
                if (sqlParams != null) {
                    localResultSet = sqlConnection.prepareAndExecuteQuery(sql, sqlParams, SQLConnection.RESULTSET_FORWARDONLY, SQLConnection.CONCUR_READONLY);
                } else {
                    localResultSet = sqlConnection.executeQuery(sql, SQLConnection.RESULTSET_FORWARDONLY, SQLConnection.CONCUR_READONLY);
                }
            }
            IPentahoMetaData metadata = mappedQuery.generateMetadata(localResultSet.getMetaData());
            ((SQLResultSet) localResultSet).setMetaData(metadata);
            closeConnection = false;
        } catch (Exception e) {
            logger.error(Messages.getInstance().getErrorString("SqlMetadataQueryExec.ERROR_0002_ERROR_EXECUTING_QUERY", e.getLocalizedMessage(), // $NON-NLS-1$
            sql));
            // $NON-NLS-1$
            logger.debug("error", e);
            return null;
        }
        return localResultSet;
    } finally {
        if (closeConnection && sqlConnection != null) {
            sqlConnection.close();
        }
    }
}
Also used : MappedQuery(org.pentaho.metadata.query.impl.sql.MappedQuery) SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) GenericDatabaseMeta(org.pentaho.di.core.database.GenericDatabaseMeta) SQLException(java.sql.SQLException) IOException(java.io.IOException) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) SqlGenerator(org.pentaho.metadata.query.impl.sql.SqlGenerator) SQLResultSet(org.pentaho.platform.plugin.services.connections.sql.SQLResultSet) Parameter(org.pentaho.metadata.query.model.Parameter)

Example 4 with DatabaseMeta

use of org.pentaho.di.core.database.DatabaseMeta in project data-access by pentaho.

the class ModelerService method generateDomain.

// TODO: remove this method in favor so specific calls
@Deprecated
public Domain generateDomain(String connectionName, String tableName, String dbType, String query, String datasourceName) throws Exception {
    initKettle();
    try {
        DatabaseMeta database = AgileHelper.getDatabaseMeta();
        IModelerSource source;
        if (tableName != null) {
            source = new TableModelerSource(database, tableName, null, datasourceName);
        } else {
            source = new InlineSqlModelerSource(connectionName, dbType, query, datasourceName);
        }
        return source.generateDomain();
    } catch (Exception e) {
        logger.error(e);
        throw new Exception(e.getLocalizedMessage());
    }
}
Also used : TableModelerSource(org.pentaho.agilebi.modeler.util.TableModelerSource) InlineSqlModelerSource(org.pentaho.platform.dataaccess.datasource.wizard.service.impl.utils.InlineSqlModelerSource) IModelerSource(org.pentaho.agilebi.modeler.IModelerSource) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) KettleException(org.pentaho.di.core.exception.KettleException)

Example 5 with DatabaseMeta

use of org.pentaho.di.core.database.DatabaseMeta in project data-access by pentaho.

the class ModelerService method generateCSVDomain.

/**
 * Use {@link ModelerService#generateCSVDomain(ModelInfo)} instead,
 * as ModelInfo object contains information about csv column names,
 * provided by user, that are not always the same as the names of columns,
 * stored in database. (see BISERVER-13026 for more info)
 */
@Deprecated
public Domain generateCSVDomain(String tableName, String datasourceName) throws Exception {
    initKettle();
    try {
        DatabaseMeta database = AgileHelper.getDatabaseMeta();
        IModelerSource source = new TableModelerSource(database, tableName, null, datasourceName);
        return source.generateDomain();
    } catch (Exception e) {
        logger.error(e);
        throw new Exception(e.getLocalizedMessage());
    }
}
Also used : TableModelerSource(org.pentaho.agilebi.modeler.util.TableModelerSource) IModelerSource(org.pentaho.agilebi.modeler.IModelerSource) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) KettleException(org.pentaho.di.core.exception.KettleException)

Aggregations

DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)522 Test (org.junit.Test)133 KettleException (org.pentaho.di.core.exception.KettleException)131 Database (org.pentaho.di.core.database.Database)88 MessageBox (org.eclipse.swt.widgets.MessageBox)66 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)63 TransMeta (org.pentaho.di.trans.TransMeta)57 StepMeta (org.pentaho.di.trans.step.StepMeta)54 ArrayList (java.util.ArrayList)53 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)48 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)44 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)42 SlaveServer (org.pentaho.di.cluster.SlaveServer)33 IMetaStore (org.pentaho.metastore.api.IMetaStore)30 ObjectId (org.pentaho.di.repository.ObjectId)29 DatabaseExplorerDialog (org.pentaho.di.ui.core.database.dialog.DatabaseExplorerDialog)29 JobMeta (org.pentaho.di.job.JobMeta)26 TransHopMeta (org.pentaho.di.trans.TransHopMeta)26 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)24 PluginRegistry (org.pentaho.di.core.plugins.PluginRegistry)24