use of org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor in project ignite by apache.
the class DdlStatementsProcessor method runDdlStatement.
/**
* Execute DDL statement.
*
* @param sql SQL.
* @param stmt H2 statement to parse and execute.
*/
@SuppressWarnings("unchecked")
public FieldsQueryCursor<List<?>> runDdlStatement(String sql, PreparedStatement stmt) throws IgniteCheckedException {
assert stmt instanceof JdbcPreparedStatement;
IgniteInternalFuture fut = null;
try {
GridSqlStatement stmt0 = new GridSqlQueryParser(false).parse(GridSqlQueryParser.prepared(stmt));
if (stmt0 instanceof GridSqlCreateIndex) {
GridSqlCreateIndex cmd = (GridSqlCreateIndex) stmt0;
GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl == null)
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
assert tbl.rowDescriptor() != null;
QueryIndex newIdx = new QueryIndex();
newIdx.setName(cmd.index().getName());
newIdx.setIndexType(cmd.index().getIndexType());
LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>();
// Let's replace H2's table and property names by those operated by GridQueryProcessor.
GridQueryTypeDescriptor typeDesc = tbl.rowDescriptor().type();
for (Map.Entry<String, Boolean> e : cmd.index().getFields().entrySet()) {
GridQueryProperty prop = typeDesc.property(e.getKey());
if (prop == null)
throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, e.getKey());
flds.put(prop.name(), e.getValue());
}
newIdx.setFields(flds);
fut = ctx.query().dynamicIndexCreate(tbl.cacheName(), cmd.schemaName(), typeDesc.tableName(), newIdx, cmd.ifNotExists());
} else if (stmt0 instanceof GridSqlDropIndex) {
GridSqlDropIndex cmd = (GridSqlDropIndex) stmt0;
GridH2Table tbl = idx.dataTableForIndex(cmd.schemaName(), cmd.indexName());
if (tbl != null) {
fut = ctx.query().dynamicIndexDrop(tbl.cacheName(), cmd.schemaName(), cmd.indexName(), cmd.ifExists());
} else {
if (cmd.ifExists())
fut = new GridFinishedFuture();
else
throw new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, cmd.indexName());
}
} else if (stmt0 instanceof GridSqlCreateTable) {
GridSqlCreateTable cmd = (GridSqlCreateTable) stmt0;
if (!F.eq(QueryUtils.DFLT_SCHEMA, cmd.schemaName()))
throw new SchemaOperationException("CREATE TABLE can only be executed on " + QueryUtils.DFLT_SCHEMA + " schema.");
GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl != null) {
if (!cmd.ifNotExists())
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_EXISTS, cmd.tableName());
} else {
ctx.query().dynamicTableCreate(cmd.schemaName(), toQueryEntity(cmd), cmd.templateName(), cmd.atomicityMode(), cmd.backups(), cmd.ifNotExists());
}
} else if (stmt0 instanceof GridSqlDropTable) {
GridSqlDropTable cmd = (GridSqlDropTable) stmt0;
if (!F.eq(QueryUtils.DFLT_SCHEMA, cmd.schemaName()))
throw new SchemaOperationException("DROP TABLE can only be executed on " + QueryUtils.DFLT_SCHEMA + " schema.");
GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl == null) {
if (!cmd.ifExists())
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
} else
ctx.query().dynamicTableDrop(tbl.cacheName(), cmd.tableName(), cmd.ifExists());
} else
throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (fut != null)
fut.get();
QueryCursorImpl<List<?>> resCur = (QueryCursorImpl<List<?>>) new QueryCursorImpl(Collections.singletonList(Collections.singletonList(0L)), null, false);
resCur.fieldsMeta(UPDATE_RESULT_META);
return resCur;
} catch (SchemaOperationException e) {
throw convert(e);
} catch (IgniteSQLException e) {
throw e;
} catch (Exception e) {
throw new IgniteSQLException("Unexpected DLL operation failure: " + e.getMessage(), e);
}
}
use of org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor in project ignite by apache.
the class JdbcRequestHandler method getColumnsMeta.
/**
* {@link OdbcQueryGetColumnsMetaRequest} command handler.
*
* @param req Get columns metadata request.
* @return Response.
*/
@SuppressWarnings("unchecked")
private JdbcResponse getColumnsMeta(JdbcMetaColumnsRequest req) {
try {
Collection<JdbcColumnMeta> meta = new LinkedHashSet<>();
for (String cacheName : ctx.cache().publicCacheNames()) {
for (GridQueryTypeDescriptor table : ctx.query().types(cacheName)) {
if (!matches(table.schemaName(), req.schemaName()))
continue;
if (!matches(table.tableName(), req.tableName()))
continue;
for (Map.Entry<String, Class<?>> field : table.fields().entrySet()) {
String colName = field.getKey();
if (!matches(colName, req.columnName()))
continue;
JdbcColumnMeta columnMeta;
if (protocolVer.compareTo(VER_2_4_0) >= 0) {
GridQueryProperty prop = table.property(colName);
columnMeta = new JdbcColumnMetaV3(table.schemaName(), table.tableName(), field.getKey(), field.getValue(), !prop.notNull(), prop.defaultValue());
} else if (protocolVer.compareTo(VER_2_3_0) >= 0) {
GridQueryProperty prop = table.property(colName);
columnMeta = new JdbcColumnMetaV2(table.schemaName(), table.tableName(), field.getKey(), field.getValue(), !prop.notNull());
} else
columnMeta = new JdbcColumnMeta(table.schemaName(), table.tableName(), field.getKey(), field.getValue());
if (!meta.contains(columnMeta))
meta.add(columnMeta);
}
}
}
JdbcMetaColumnsResult res;
if (protocolVer.compareTo(VER_2_4_0) >= 0)
res = new JdbcMetaColumnsResultV3(meta);
else if (protocolVer.compareTo(VER_2_3_0) >= 0)
res = new JdbcMetaColumnsResultV2(meta);
else
res = new JdbcMetaColumnsResult(meta);
return new JdbcResponse(res);
} catch (Exception e) {
U.error(log, "Failed to get columns metadata [reqId=" + req.requestId() + ", req=" + req + ']', e);
return exceptionToResult(e);
}
}
use of org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor in project ignite by apache.
the class JdbcRequestHandler method getTablesMeta.
/**
* @param req Get tables metadata request.
* @return Response.
*/
private JdbcResponse getTablesMeta(JdbcMetaTablesRequest req) {
try {
List<JdbcTableMeta> meta = new ArrayList<>();
for (String cacheName : ctx.cache().publicCacheNames()) {
for (GridQueryTypeDescriptor table : ctx.query().types(cacheName)) {
if (!matches(table.schemaName(), req.schemaName()))
continue;
if (!matches(table.tableName(), req.tableName()))
continue;
JdbcTableMeta tableMeta = new JdbcTableMeta(table.schemaName(), table.tableName(), "TABLE");
if (!meta.contains(tableMeta))
meta.add(tableMeta);
}
}
JdbcMetaTablesResult res = new JdbcMetaTablesResult(meta);
return new JdbcResponse(res);
} catch (Exception e) {
U.error(log, "Failed to get tables metadata [reqId=" + req.requestId() + ", req=" + req + ']', e);
return exceptionToResult(e);
}
}
use of org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor in project ignite by apache.
the class OdbcRequestHandler method getColumnsMeta.
/**
* {@link OdbcQueryGetColumnsMetaRequest} command handler.
*
* @param req Get columns metadata request.
* @return Response.
*/
private ClientListenerResponse getColumnsMeta(OdbcQueryGetColumnsMetaRequest req) {
try {
List<OdbcColumnMeta> meta = new ArrayList<>();
String schemaPattern;
String tablePattern;
if (req.tablePattern().contains(".")) {
// Parsing two-part table name.
String[] parts = req.tablePattern().split("\\.");
schemaPattern = parts[0];
tablePattern = parts[1];
} else {
schemaPattern = req.schemaPattern();
tablePattern = req.tablePattern();
}
schemaPattern = OdbcUtils.removeQuotationMarksIfNeeded(schemaPattern);
for (String cacheName : ctx.cache().publicCacheNames()) {
for (GridQueryTypeDescriptor table : ctx.query().types(cacheName)) {
if (!matches(table.schemaName(), schemaPattern) || !matches(table.tableName(), tablePattern))
continue;
for (Map.Entry<String, Class<?>> field : table.fields().entrySet()) {
if (!matches(field.getKey(), req.columnPattern()))
continue;
GridQueryProperty prop = table.property(field.getKey());
OdbcColumnMeta columnMeta = new OdbcColumnMeta(table.schemaName(), table.tableName(), field.getKey(), field.getValue(), prop.precision(), prop.scale(), prop.notNull() ? columnNoNulls : columnNullable);
if (!meta.contains(columnMeta))
meta.add(columnMeta);
}
}
}
OdbcQueryGetColumnsMetaResult res = new OdbcQueryGetColumnsMetaResult(meta);
return new OdbcResponse(res);
} catch (Exception e) {
U.error(log, "Failed to get columns metadata [reqId=" + req.requestId() + ", req=" + req + ']', e);
return exceptionToResult(e);
}
}
use of org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor in project ignite by apache.
the class UpdatePlan method processRowForUpdate.
/**
* Convert a row into value.
*
* @param row Row to process.
* @throws IgniteCheckedException if failed.
* @return Tuple contains: [key, old value, new value]
*/
public T3<Object, Object, Object> processRowForUpdate(List<?> row) throws IgniteCheckedException {
GridH2RowDescriptor rowDesc = tbl.rowDescriptor();
GridQueryTypeDescriptor desc = rowDesc.type();
GridCacheContext cctx = rowDesc.context();
boolean hasNewVal = (valColIdx != -1);
boolean hasProps = !hasNewVal || colNames.length > 1;
Object key = row.get(0);
Object oldVal = row.get(1);
if (cctx.binaryMarshaller() && !(oldVal instanceof BinaryObject))
oldVal = cctx.grid().binary().toBinary(oldVal);
Object newVal;
Map<String, Object> newColVals = new HashMap<>();
for (int i = 0; i < colNames.length; i++) {
if (hasNewVal && i == valColIdx - 2)
continue;
GridQueryProperty prop = tbl.rowDescriptor().type().property(colNames[i]);
assert prop != null : "Unknown property: " + colNames[i];
newColVals.put(colNames[i], DmlUtils.convert(row.get(i + 2), rowDesc, prop.type(), colTypes[i], colNames[i]));
}
newVal = valSupplier.apply(row);
if (newVal == null)
throw new IgniteSQLException("New value for UPDATE must not be null", IgniteQueryErrorCode.NULL_VALUE);
// Skip key and value - that's why we start off with 3rd column
for (int i = 0; i < tbl.getColumns().length - QueryUtils.DEFAULT_COLUMNS_COUNT; i++) {
Column c = tbl.getColumn(i + QueryUtils.DEFAULT_COLUMNS_COUNT);
if (rowDesc.isKeyValueOrVersionColumn(c.getColumnId()))
continue;
GridQueryProperty prop = desc.property(c.getName());
if (prop.key())
// Don't get values of key's columns - we won't use them anyway
continue;
boolean hasNewColVal = newColVals.containsKey(c.getName());
if (!hasNewColVal)
continue;
Object colVal = newColVals.get(c.getName());
// UPDATE currently does not allow to modify key or its fields, so we must be safe to pass null as key.
rowDesc.setColumnValue(null, newVal, colVal, i);
}
if (cctx.binaryMarshaller() && hasProps) {
assert newVal instanceof BinaryObjectBuilder;
newVal = ((BinaryObjectBuilder) newVal).build();
}
desc.validateKeyAndValue(key, newVal);
return new T3<>(key, oldVal, newVal);
}
Aggregations