use of org.pentaho.di.core.SQLStatement in project pentaho-kettle by pentaho.
the class MySQLBulkLoaderMeta method getSQLStatements.
public SQLStatement getSQLStatements(TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, Repository repository, IMetaStore metaStore) throws KettleStepException {
// default: nothing to do!
SQLStatement retval = new SQLStatement(stepMeta.getName(), databaseMeta, null);
if (databaseMeta != null) {
if (prev != null && prev.size() > 0) {
// Copy the row
RowMetaInterface tableFields = new RowMeta();
// Now change the field names
for (int i = 0; i < fieldTable.length; i++) {
ValueMetaInterface v = prev.searchValueMeta(fieldStream[i]);
if (v != null) {
ValueMetaInterface tableField = v.clone();
tableField.setName(fieldTable[i]);
tableFields.addValueMeta(tableField);
} else {
throw new KettleStepException("Unable to find field [" + fieldStream[i] + "] in the input rows");
}
}
if (!Utils.isEmpty(tableName)) {
Database db = new Database(loggingObject, databaseMeta);
db.shareVariablesWith(transMeta);
try {
db.connect();
String schemaTable = databaseMeta.getQuotedSchemaTableCombination(transMeta.environmentSubstitute(schemaName), transMeta.environmentSubstitute(tableName));
String cr_table = db.getDDL(schemaTable, tableFields, null, false, null, true);
String sql = cr_table;
if (sql.length() == 0) {
retval.setSQL(null);
} else {
retval.setSQL(sql);
}
} catch (KettleException e) {
retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.ErrorOccurred") + e.getMessage());
}
} else {
retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection"));
}
} else {
retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.NotReceivingAnyFields"));
}
} else {
retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.NoConnectionDefined"));
}
return retval;
}
use of org.pentaho.di.core.SQLStatement in project pentaho-kettle by pentaho.
the class IngresVectorwiseLoaderMeta method getSQLStatements.
@Override
public SQLStatement getSQLStatements(TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, Repository repository, IMetaStore metaStore) {
// default:
SQLStatement retval = new SQLStatement(stepMeta.getName(), databaseMeta, null);
if (databaseMeta != null) {
if (prev != null && prev.size() > 0) {
if (!Utils.isEmpty(tablename)) {
Database db = new Database(loggingObject, databaseMeta);
db.shareVariablesWith(transMeta);
try {
db.connect();
String schemaTable = databaseMeta.getQuotedSchemaTableCombination(null, tablename);
String cr_table = db.getDDL(schemaTable, prev);
// Squeeze in the VECTORWISE col store clause...
// TODO: move this to the database dialog and make it user
// configurable.
//
String VW_CLAUSE = "WITH STRUCTURE=VECTORWISE";
if (cr_table.toUpperCase().contains("CREATE TABLE")) {
int scIndex = cr_table.indexOf(';');
if (scIndex < 0) {
cr_table += VW_CLAUSE;
} else {
cr_table = cr_table.substring(0, scIndex) + VW_CLAUSE + cr_table.substring(scIndex);
}
}
// Empty string means: nothing to do: set it to null...
if (cr_table == null || cr_table.length() == 0) {
cr_table = null;
}
retval.setSQL(cr_table);
} catch (KettleDatabaseException dbe) {
retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.ErrorConnecting", dbe.getMessage()));
} finally {
db.disconnect();
}
} else {
retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.NoTable"));
}
} else {
retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.NoInput"));
}
} else {
retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.NoConnection"));
}
return retval;
}
use of org.pentaho.di.core.SQLStatement in project pentaho-kettle by pentaho.
the class TransMeta method getSQLStatementsString.
/**
* Get the SQL statements (needed to run this transformation) as a single String.
*
* @return the SQL statements needed to run this transformation
* @throws KettleStepException
* if any errors occur during SQL statement generation
*/
public String getSQLStatementsString() throws KettleStepException {
String sql = "";
List<SQLStatement> stats = getSQLStatements();
for (int i = 0; i < stats.size(); i++) {
SQLStatement stat = stats.get(i);
if (!stat.hasError() && stat.hasSQL()) {
sql += stat.getSQL();
}
}
return sql;
}
use of org.pentaho.di.core.SQLStatement in project pentaho-kettle by pentaho.
the class UpdateMetaTest method testUseDefaultSchemaName.
@Test
public void testUseDefaultSchemaName() throws Exception {
String schemaName = "";
String tableName = "tableName";
String schemaTable = "default.tableName";
DatabaseMeta databaseMeta = spy(new DatabaseMeta(databaseXML));
doReturn("someValue").when(databaseMeta).getFieldDefinition(any(ValueMetaInterface.class), anyString(), anyString(), anyBoolean());
doReturn(schemaTable).when(databaseMeta).getQuotedSchemaTableCombination(schemaName, tableName);
ValueMetaInterface valueMeta = mock(ValueMetaInterface.class);
when(valueMeta.clone()).thenReturn(mock(ValueMetaInterface.class));
RowMetaInterface rowMetaInterface = mock(RowMetaInterface.class);
when(rowMetaInterface.size()).thenReturn(1);
when(rowMetaInterface.searchValueMeta(anyString())).thenReturn(valueMeta);
UpdateMeta updateMeta = new UpdateMeta();
updateMeta.setDatabaseMeta(databaseMeta);
updateMeta.setTableName(tableName);
updateMeta.setSchemaName(schemaName);
updateMeta.setKeyLookup(new String[] { "KeyLookup1", "KeyLookup2" });
updateMeta.setKeyStream(new String[] { "KeyStream1", "KeyStream2" });
updateMeta.setUpdateLookup(new String[] { "updateLookup1", "updateLookup2" });
updateMeta.setUpdateStream(new String[] { "UpdateStream1", "UpdateStream2" });
SQLStatement sqlStatement = updateMeta.getSQLStatements(new TransMeta(), mock(StepMeta.class), rowMetaInterface, mock(Repository.class), mock(IMetaStore.class));
String sql = sqlStatement.getSQL();
assertTrue(StringUtils.countMatches(sql, schemaTable) == 2);
}
use of org.pentaho.di.core.SQLStatement in project pentaho-kettle by pentaho.
the class SQLStatementsDialog method getSQL.
private String getSQL() {
StringBuilder sql = new StringBuilder();
int[] idx = wFields.table.getSelectionIndices();
// None selected: don't waste users time: select them all!
if (idx.length == 0) {
idx = new int[stats.size()];
for (int i = 0; i < stats.size(); i++) {
idx[i] = i;
}
}
for (int i = 0; i < idx.length; i++) {
SQLStatement stat = stats.get(idx[i]);
DatabaseMeta di = stat.getDatabase();
if (i > 0) {
sql.append("-------------------------------------------------------------------------------------------").append(Const.CR);
}
sql.append(BaseMessages.getString(PKG, "SQLStatementDialog.Log.Step", stat.getStepname()));
sql.append(BaseMessages.getString(PKG, "SQLStatementDialog.Log.Connection", (di != null ? di.getName() : BaseMessages.getString(PKG, "SQLStatementDialog.Log.Undefined"))));
if (stat.hasSQL()) {
sql.append("-- SQL : ");
sql.append(stat.getSQL()).append(Const.CR);
}
if (stat.hasError()) {
sql.append(BaseMessages.getString(PKG, "SQLStatementDialog.Log.Error", stat.getError()));
}
}
return sql.toString();
}
Aggregations