use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.
the class DatabaseSQLExecutor method createSqlStatement.
/**
* createSqlStatement: if has limit, add it, else do not use limit
*
* @param connection
* @param analysedElements
* @return
*/
private String createSqlStatement(DataManager connection, List<ModelElement> analysedElements, String where) {
DbmsLanguage dbms = DbmsLanguageFactory.createDbmsLanguage(connection);
TdColumn col = null;
// $NON-NLS-1$
StringBuilder sql = new StringBuilder("SELECT ");
final Iterator<ModelElement> iterator = analysedElements.iterator();
while (iterator.hasNext()) {
ModelElement modelElement = iterator.next();
col = SwitchHelpers.COLUMN_SWITCH.doSwitch(modelElement);
sql.append(dbms.quote(col.getName()));
// append comma if more columns exist
if (iterator.hasNext()) {
sql.append(',');
}
}
sql.append(dbms.from());
sql.append(dbms.getQueryColumnSetWithPrefix(col));
if (where != null && where.length() > 0) {
sql.append(dbms.where());
sql.append(where);
}
String finalQuery = sql.toString();
if (isShowRandomData()) {
finalQuery = dbms.getRandomQuery(finalQuery);
}
if (getLimit() > 0) {
return dbms.getTopNQuery(finalQuery, getLimit());
} else {
return finalQuery;
}
}
use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.
the class ResultSetHelper method getResultSet.
public static ResultSet getResultSet(MetadataTable metadataTable, java.sql.Connection sqlConn, String whereExpression, int maxRows) throws SQLException {
Connection tdDataProvider = ConnectionHelper.getTdDataProvider(metadataTable);
if (sqlConn == null) {
IMetadataConnection metadataBean = ConvertionHelper.convert(tdDataProvider);
TypedReturnCode<java.sql.Connection> createConnection = MetadataConnectionUtils.createConnection(metadataBean, false);
if (!createConnection.isOk()) {
return null;
}
sqlConn = createConnection.getObject();
}
DbmsLanguage dbmsLanguage = DbmsLanguageFactory.createDbmsLanguage(tdDataProvider);
Statement createStatement = null;
if (maxRows != 0) {
// TOPN algorithm, it has row limited,no need the fetch size.
createStatement = dbmsLanguage.createStatement(sqlConn);
} else {
// Resevoir Sample algorithm
createStatement = dbmsLanguage.createStatement(sqlConn, 1000);
}
createStatement.setMaxRows(maxRows);
Expression columnQueryExpression = dbmsLanguage.getTableQueryExpression(metadataTable, whereExpression);
return createStatement.executeQuery(columnQueryExpression.getBody());
}
use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.
the class PreviewColumnAction method run.
@Override
public void run() {
if (modelElements[0] instanceof TdColumn) {
TdColumn[] columns = new TdColumn[modelElements.length];
int i = 0;
for (ModelElement me : modelElements) {
columns[i] = (TdColumn) me;
++i;
}
if (ColumnHelper.isFromSameTable(Arrays.asList(columns))) {
TdColumn oneColumn = columns[0];
Connection dataprovider = ConnectionHelper.getTdDataProvider(oneColumn);
ColumnSet columnSetOwner = ColumnHelper.getColumnOwnerAsColumnSet(oneColumn);
DbmsLanguage language = DbmsLanguageFactory.createDbmsLanguage(dataprovider);
String columnsWithPrefix = language.getQueryColumnsWithPrefix(columns);
String columnSetName = language.getQueryColumnSetWithPrefix(columnSetOwner);
// $NON-NLS-1$ //$NON-NLS-2$
String query = "select " + columnsWithPrefix + " from " + columnSetName;
SqlExplorerUtils.getDefault().runInDQViewer(dataprovider, query, columnSetName);
} else {
MessageDialogWithToggle.openWarning(null, DefaultMessagesImpl.getString("PreviewColumnAction.Warning"), // $NON-NLS-1$ //$NON-NLS-2$
DefaultMessagesImpl.getString("PreviewColumnAction.previewColumns"));
}
}
}
use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.
the class AnalysisExecutorHelperTest method testGetTableName_5.
/**
* try the mysql db type. Test method for
* {@link org.talend.dq.helper.AnalysisExecutorHelper#getTableName(orgomg.cwm.objectmodel.core.ModelElement, org.talend.dq.dbms.DbmsLanguage)}
* .
*/
@Test
public void testGetTableName_5() {
TdColumn tdColumn = RelationalFactory.eINSTANCE.createTdColumn();
TdTable tdTable = RelationalFactory.eINSTANCE.createTdTable();
// $NON-NLS-1$
tdTable.setName("tableName");
tdColumn.setOwner(tdTable);
// $NON-NLS-1$
tdColumn.setName("columnName");
DbmsLanguage dbmsLanguage = DbmsLanguageFactory.createDbmsLanguage(SupportDBUrlType.MYSQLDEFAULTURL.getLanguage(), null);
// mock(Catalog.class);
Package catalog = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createCatalog();
// $NON-NLS-1$
catalog.setName("catalogName");
tdTable.setNamespace(catalog);
// $NON-NLS-1$
assertEquals("`catalogName`.`tableName`", AnalysisExecutorHelper.getTableName(tdColumn, dbmsLanguage));
}
use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.
the class AnalysisExecutorHelperTest method testGetTableName_3.
/**
* try the sybase db type. Test method for
* {@link org.talend.dq.helper.AnalysisExecutorHelper#getTableName(orgomg.cwm.objectmodel.core.ModelElement, org.talend.dq.dbms.DbmsLanguage)}
* .
*/
@Test
public void testGetTableName_3() {
TdColumn tdColumn = RelationalFactory.eINSTANCE.createTdColumn();
TdTable tdTable = RelationalFactory.eINSTANCE.createTdTable();
// $NON-NLS-1$
tdTable.setName("tableName");
tdColumn.setOwner(tdTable);
// $NON-NLS-1$
tdColumn.setName("columnName");
DbmsLanguage dbmsLanguage = DbmsLanguageFactory.createDbmsLanguage(SupportDBUrlType.SYBASEDEFAULTURL.getLanguage(), null);
// mock(Schema.class);
Package schema = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createSchema();
schema.setName("schemaName");
tdTable.setNamespace(schema);
// mock(Catalog.class);
Package catalog = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createCatalog();
// $NON-NLS-1$
catalog.setName("catalogName");
schema.setNamespace(catalog);
// $NON-NLS-1$
assertEquals("catalogName.schemaName.tableName", AnalysisExecutorHelper.getTableName(tdColumn, dbmsLanguage));
}
Aggregations