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());
}
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());
}
}
}
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();
}
}
}
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());
}
}
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());
}
}
Aggregations