use of org.teiid.metadata.Column in project teiid by teiid.
the class InfinispanQueryExecution method useModifiedGroups.
static void useModifiedGroups(InfinispanConnection connection, ExecutionContext context, RuntimeMetadata metadata, Command command) throws TranslatorException {
BasicCache<String, String> aliasCache = InfinispanDirectQueryExecution.getAliasCache(connection);
CollectorVisitor.collectGroups(command).forEach(namedTable -> {
try {
Table table = InfinispanDirectQueryExecution.getAliasTable(context, metadata, aliasCache, namedTable.getMetadataObject());
Collection<ColumnReference> columns = CollectorVisitor.collectElements(command);
columns.forEach(reference -> {
if (reference.getTable().getMetadataObject().equals(namedTable.getMetadataObject())) {
Column column = table.getColumnByName(reference.getMetadataObject().getName());
reference.getTable().setMetadataObject(table);
reference.setMetadataObject(column);
}
});
namedTable.setMetadataObject(table);
} catch (TranslatorException e) {
LogManager.logError(LogConstants.CTX_CONNECTOR, e, e.getMessage());
}
});
}
use of org.teiid.metadata.Column in project teiid by teiid.
the class InfinispanUpdateExecution method execute.
@Override
public void execute() throws TranslatorException {
if (useAliasCache) {
if (useAliasCache) {
InfinispanQueryExecution.useModifiedGroups(this.connection, this.executionContext, this.metadata, this.command);
}
}
final InfinispanUpdateVisitor visitor = new InfinispanUpdateVisitor(this.metadata);
visitor.append(this.command);
if (!visitor.exceptions.isEmpty()) {
throw visitor.exceptions.get(0);
}
TeiidTableMarsheller marshaller = null;
try {
Table table = visitor.getParentTable();
Column pkColumn = visitor.getPrimaryKey();
if (pkColumn == null) {
throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25013, table.getName()));
}
final String PK = MarshallerBuilder.getDocumentAttributeName(pkColumn, false, this.metadata);
DocumentFilter docFilter = null;
if (visitor.isNestedOperation() && visitor.getWhereClause() != null) {
Action action = Action.ALWAYSADD;
if (command instanceof Delete) {
action = Action.REMOVE;
}
SQLStringVisitor ssv = new SQLStringVisitor() {
@Override
public void visit(ColumnReference obj) {
String groupName = null;
NamedTable group = obj.getTable();
if (group.getCorrelationName() != null) {
groupName = group.getCorrelationName();
} else {
Table groupID = group.getMetadataObject();
if (groupID.getFullName().equals(visitor.getParentTable().getFullName())) {
groupName = visitor.getParentNamedTable().getCorrelationName();
} else {
groupName = visitor.getQueryNamedTable().getCorrelationName();
}
}
buffer.append(groupName).append(Tokens.DOT).append(getName(obj.getMetadataObject()));
}
@Override
public String getName(AbstractMetadataRecord object) {
return object.getName();
}
};
ssv.append(visitor.getWhereClause());
docFilter = new ComplexDocumentFilter(visitor.getParentNamedTable(), visitor.getQueryNamedTable(), this.metadata, ssv.toString(), action);
}
marshaller = MarshallerBuilder.getMarshaller(table, this.metadata, docFilter);
this.connection.registerMarshaller(marshaller);
// if the message in defined in different cache than the default, switch it out now.
final RemoteCache<Object, Object> cache = InfinispanQueryExecution.getCache(table, connection);
if (visitor.getOperationType() == OperationType.DELETE) {
paginateDeleteResults(cache, visitor.getDeleteQuery(), new Task() {
@Override
public void run(Object row) throws TranslatorException {
if (visitor.isNestedOperation()) {
String childName = ProtobufMetadataProcessor.getMessageName(visitor.getQueryTable());
InfinispanDocument document = (InfinispanDocument) row;
cache.replace(document.getProperties().get(PK), document);
// false below means count that not matched, i.e. deleted count
updateCount = updateCount + document.getUpdateCount(childName, false);
} else {
Object key = ((Object[]) row)[0];
cache.remove(key);
updateCount++;
}
}
}, this.executionContext.getBatchSize());
} else if (visitor.getOperationType() == OperationType.UPDATE) {
paginateUpdateResults(cache, visitor.getUpdateQuery(), new Task() {
@Override
public void run(Object row) throws TranslatorException {
InfinispanDocument previous = (InfinispanDocument) row;
Object key = previous.getProperties().get(PK);
int count = previous.merge(visitor.getInsertPayload());
cache.replace(key, previous);
updateCount = updateCount + count;
}
}, this.executionContext.getBatchSize());
} else if (visitor.getOperationType() == OperationType.INSERT) {
performInsert(visitor, table, cache, false, marshaller);
} else if (visitor.getOperationType() == OperationType.UPSERT) {
performInsert(visitor, table, cache, true, marshaller);
}
} finally {
if (marshaller != null) {
this.connection.unRegisterMarshaller(marshaller);
}
}
}
use of org.teiid.metadata.Column in project teiid by teiid.
the class JDBCUpdateExecution method executeTranslatedCommand.
/**
* @param translatedComm
* @throws TranslatorException
* @since 4.3
*/
private void executeTranslatedCommand(TranslatedCommand translatedComm) throws TranslatorException {
// create statement or PreparedStatement and execute
String sql = translatedComm.getSql();
boolean commitType = false;
boolean succeeded = false;
try {
int updateCount = 0;
Class<?>[] keyColumnDataTypes = null;
String[] keyColumnNames = null;
if (command instanceof Insert && context.getCommandContext().isReturnAutoGeneratedKeys() && executionFactory.supportsGeneratedKeys(context, command)) {
Insert insert = (Insert) command;
NamedTable nt = insert.getTable();
if (nt.getMetadataObject() != null) {
KeyRecord key = nt.getMetadataObject().getPrimaryKey();
if (key != null) {
List<Column> cols = key.getColumns();
keyColumnDataTypes = new Class<?>[cols.size()];
keyColumnNames = new String[cols.size()];
for (int i = 0; i < cols.size(); i++) {
Column c = cols.get(i);
keyColumnDataTypes[i] = c.getJavaType();
// won't work in scenarios where the teiid name is changed or contains a .
keyColumnNames[i] = c.getName();
}
}
}
}
if (!translatedComm.isPrepared()) {
statement = getStatement();
// handle autoGeneratedKeys
if (keyColumnDataTypes != null) {
if (executionFactory.useColumnNamesForGeneratedKeys()) {
updateCount = statement.executeUpdate(sql, keyColumnNames);
} else {
updateCount = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
}
} else {
updateCount = statement.executeUpdate(sql);
}
result = new int[] { updateCount };
addStatementWarnings();
} else {
PreparedStatement pstatement = null;
if (statement != null) {
statement.close();
}
if (keyColumnDataTypes != null) {
if (executionFactory.useColumnNamesForGeneratedKeys()) {
pstatement = connection.prepareStatement(sql, keyColumnNames);
} else {
pstatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
}
} else {
pstatement = getPreparedStatement(sql);
}
statement = pstatement;
Iterator<? extends List<?>> vi = null;
if (command instanceof BulkCommand) {
BulkCommand batchCommand = (BulkCommand) command;
vi = batchCommand.getParameterValues();
}
int k = 0;
int batchStart = 0;
if (vi != null) {
try {
commitType = getAutoCommit(translatedComm);
if (commitType) {
connection.setAutoCommit(false);
}
int maxBatchSize = (command instanceof Insert) ? maxPreparedInsertBatchSize : Integer.MAX_VALUE;
boolean done = false;
outer: while (!done) {
for (int i = 0; i < maxBatchSize; i++) {
if (vi.hasNext()) {
List<?> values = vi.next();
bind(pstatement, translatedComm.getPreparedValues(), values);
k++;
} else {
if (i == 0) {
break outer;
}
done = true;
break;
}
}
int[] results = pstatement.executeBatch();
batchStart = k;
if (result == null) {
result = results;
} else {
int len = result.length;
result = Arrays.copyOf(result, len + results.length);
System.arraycopy(results, 0, result, len, results.length);
}
}
} catch (SQLException e) {
int size = k + 1;
if (result == null) {
result = new int[size];
} else {
result = Arrays.copyOf(result, size);
}
// if there is a BatchUpdateException, there are more update counts to accumulate
if (e instanceof BatchUpdateException) {
BatchUpdateException bue = (BatchUpdateException) e;
int[] batchResults = bue.getUpdateCounts();
for (int j = 0; j < batchResults.length; j++) {
result[batchStart + j] = batchResults[j];
}
size = batchStart + batchResults.length;
} else {
size = batchStart;
}
// resize the result and throw exception
throw new TranslatorBatchException(e, Arrays.copyOf(result, size));
}
} else {
bind(pstatement, translatedComm.getPreparedValues(), null);
updateCount = pstatement.executeUpdate();
result = new int[] { updateCount };
}
addStatementWarnings();
succeeded = true;
}
if (keyColumnDataTypes != null) {
try {
ResultSet keys = statement.getGeneratedKeys();
GeneratedKeys generatedKeys = context.getCommandContext().returnGeneratedKeys(keyColumnNames, keyColumnDataTypes);
// many databases only support returning a single generated value, but we'll still attempt to gather all
outer: while (keys.next()) {
List<Object> vals = new ArrayList<Object>(keyColumnDataTypes.length);
for (int i = 0; i < keyColumnDataTypes.length; i++) {
Object value = this.executionFactory.retrieveValue(keys, i + 1, keyColumnDataTypes[i]);
if (value != null && TypeFacility.getRuntimeType(value.getClass()) != keyColumnDataTypes[i]) {
// TODO we may need to let the engine to the final conversion
LogManager.logDetail(LogConstants.CTX_CONNECTOR, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11023, keyColumnDataTypes[i], keyColumnNames[i], value.getClass()));
continue outer;
}
vals.add(value);
}
generatedKeys.addKey(vals);
}
} catch (SQLException e) {
context.addWarning(e);
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Exception determining generated keys, no keys will be returned");
}
}
} catch (SQLException err) {
throw new JDBCExecutionException(JDBCPlugin.Event.TEIID11013, err, translatedComm);
} finally {
if (commitType) {
restoreAutoCommit(!succeeded, translatedComm);
}
}
}
use of org.teiid.metadata.Column in project teiid by teiid.
the class TestHiveExecutionFactory method testQuoting.
@Test
public void testQuoting() throws Exception {
HiveMetadataProcessor hmp = new HiveMetadataProcessor();
Connection c = Mockito.mock(Connection.class);
MetadataFactory mf = Mockito.mock(MetadataFactory.class);
Table table = new Table();
Mockito.stub(mf.addTable("x")).toReturn(table);
Column col = new Column();
col.setName("y");
Mockito.stub(mf.addColumn("y", "string", table)).toReturn(col);
Statement stmt = Mockito.mock(Statement.class);
Mockito.stub(c.createStatement()).toReturn(stmt);
ResultSet rs = Mockito.mock(ResultSet.class);
Mockito.stub(stmt.executeQuery("SHOW TABLES")).toReturn(rs);
Mockito.stub(rs.next()).toReturn(true).toReturn(false);
Mockito.stub(rs.getString(1)).toReturn("x");
ResultSet rs1 = Mockito.mock(ResultSet.class);
Mockito.stub(stmt.executeQuery("DESCRIBE x")).toReturn(rs1);
Mockito.stub(rs1.next()).toReturn(true).toReturn(false);
Mockito.stub(rs1.getString(1)).toReturn("y");
Mockito.stub(rs1.getString(2)).toReturn("string");
hmp.process(mf, c);
assertEquals("`x`", table.getNameInSource());
assertEquals("`y`", col.getNameInSource());
}
use of org.teiid.metadata.Column in project teiid by teiid.
the class MySQLExecutionFactory method getMetadataProcessor.
@Override
public MetadataProcessor<Connection> getMetadataProcessor() {
return new JDBCMetadataProcessor() {
@Override
protected String getRuntimeType(int type, String typeName, int precision) {
// mysql will otherwise report a 0/null type for geometry
if ("geometry".equalsIgnoreCase(typeName)) {
// $NON-NLS-1$
return TypeFacility.RUNTIME_NAMES.GEOMETRY;
}
return super.getRuntimeType(type, typeName, precision);
}
@Override
protected Column addColumn(ResultSet columns, Table table, MetadataFactory metadataFactory, int rsColumns) throws SQLException {
Column c = super.addColumn(columns, table, metadataFactory, rsColumns);
if (c.getPrecision() == 0 && "bit".equalsIgnoreCase(c.getNativeType())) {
// $NON-NLS-1$
c.setNativeType(TINYINT);
}
return c;
}
@Override
protected void getTableStatistics(Connection conn, String catalog, String schema, String name, Table table) throws SQLException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// $NON-NLS-1$
stmt = conn.prepareStatement("SELECT cardinality FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = ? AND table_name = ?");
if (catalog != null && schema == null) {
// mysql jdbc reports the schema as the catalog
stmt.setString(1, catalog);
} else {
stmt.setString(1, schema);
}
stmt.setString(2, name);
rs = stmt.executeQuery();
if (rs.next()) {
int cardinality = rs.getInt(1);
if (!rs.wasNull()) {
table.setCardinality(cardinality);
}
}
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
}
@Override
protected boolean isUnsignedTypeName(String name) {
if (!name.contains("UNSIGNED")) {
// $NON-NLS-1$
return false;
}
return super.isUnsignedTypeName(name);
}
};
}
Aggregations