use of org.h2.table.Column in project ignite by apache.
the class DdlStatementsProcessor method toQueryEntity.
/**
* Convert this statement to query entity and do Ignite specific sanity checks on the way.
* @return Query entity mimicking this SQL statement.
*/
private static QueryEntity toQueryEntity(GridSqlCreateTable createTbl) {
QueryEntity res = new QueryEntity();
res.setTableName(createTbl.tableName());
Set<String> notNullFields = null;
HashMap<String, Object> dfltValues = new HashMap<>();
for (Map.Entry<String, GridSqlColumn> e : createTbl.columns().entrySet()) {
GridSqlColumn gridCol = e.getValue();
Column col = gridCol.column();
res.addQueryField(e.getKey(), DataType.getTypeClassName(col.getType()), null);
if (!col.isNullable()) {
if (notNullFields == null)
notNullFields = new HashSet<>();
notNullFields.add(e.getKey());
}
Object dfltVal = gridCol.defaultValue();
if (dfltVal != null)
dfltValues.put(e.getKey(), dfltVal);
}
if (!F.isEmpty(dfltValues))
res.setDefaultFieldValues(dfltValues);
String valTypeName = QueryUtils.createTableValueTypeName(createTbl.schemaName(), createTbl.tableName());
String keyTypeName = QueryUtils.createTableKeyTypeName(valTypeName);
if (!F.isEmpty(createTbl.keyTypeName()))
keyTypeName = createTbl.keyTypeName();
if (!F.isEmpty(createTbl.valueTypeName()))
valTypeName = createTbl.valueTypeName();
assert createTbl.wrapKey() != null;
assert createTbl.wrapValue() != null;
if (!createTbl.wrapKey()) {
GridSqlColumn pkCol = createTbl.columns().get(createTbl.primaryKeyColumns().iterator().next());
keyTypeName = DataType.getTypeClassName(pkCol.column().getType());
res.setKeyFieldName(pkCol.columnName());
} else
res.setKeyFields(createTbl.primaryKeyColumns());
if (!createTbl.wrapValue()) {
GridSqlColumn valCol = null;
for (Map.Entry<String, GridSqlColumn> e : createTbl.columns().entrySet()) {
if (!createTbl.primaryKeyColumns().contains(e.getKey())) {
valCol = e.getValue();
break;
}
}
assert valCol != null;
valTypeName = DataType.getTypeClassName(valCol.column().getType());
res.setValueFieldName(valCol.columnName());
}
res.setValueType(valTypeName);
res.setKeyType(keyTypeName);
if (!F.isEmpty(notNullFields)) {
QueryEntityEx res0 = new QueryEntityEx(res);
res0.setNotNullFields(notNullFields);
res = res0;
}
return res;
}
use of org.h2.table.Column in project ignite by apache.
the class DmlAstUtils method selectForUpdate.
/**
* Generate SQL SELECT based on UPDATE's WHERE, LIMIT, etc.
*
* @param update Update statement.
* @param keysParamIdx Index of new param for the array of keys.
* @return SELECT statement.
*/
public static GridSqlSelect selectForUpdate(GridSqlUpdate update, @Nullable Integer keysParamIdx) {
GridSqlSelect mapQry = new GridSqlSelect();
mapQry.from(update.target());
Set<GridSqlTable> tbls = new HashSet<>();
collectAllGridTablesInTarget(update.target(), tbls);
assert tbls.size() == 1 : "Failed to determine target table for UPDATE";
GridSqlTable tbl = tbls.iterator().next();
GridH2Table gridTbl = tbl.dataTable();
assert gridTbl != null : "Failed to determine target grid table for UPDATE";
Column h2KeyCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.KEY_COL);
Column h2ValCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.VAL_COL);
GridSqlColumn keyCol = new GridSqlColumn(h2KeyCol, tbl, h2KeyCol.getName());
keyCol.resultType(GridSqlType.fromColumn(h2KeyCol));
GridSqlColumn valCol = new GridSqlColumn(h2ValCol, tbl, h2ValCol.getName());
valCol.resultType(GridSqlType.fromColumn(h2ValCol));
mapQry.addColumn(keyCol, true);
mapQry.addColumn(valCol, true);
for (GridSqlColumn c : update.cols()) {
String newColName = Parser.quoteIdentifier("_upd_" + c.columnName());
// We have to use aliases to cover cases when the user
// wants to update _val field directly (if it's a literal)
GridSqlAlias alias = new GridSqlAlias(newColName, elementOrDefault(update.set().get(c.columnName()), c), true);
alias.resultType(c.resultType());
mapQry.addColumn(alias, true);
}
GridSqlElement where = update.where();
if (keysParamIdx != null)
where = injectKeysFilterParam(where, keyCol, keysParamIdx);
mapQry.where(where);
mapQry.limit(update.limit());
return mapQry;
}
use of org.h2.table.Column in project elastic-core-maven by OrdinaryDude.
the class FullTextTrigger method init.
/**
* Initialize the trigger (Trigger interface)
*
* @param conn Database connection
* @param schema Database schema name
* @param trigger Database trigger name
* @param table Database table name
* @param before TRUE if trigger is called before database operation
* @param type Trigger type
* @throws SQLException A SQL error occurred
*/
@Override
public void init(Connection conn, String schema, String trigger, String table, boolean before, int type) throws SQLException {
//
if (!isActive || table.contains("_COPY_")) {
return;
}
//
// Access the Lucene index
//
// We need to get the access just once, either in a trigger or in a function alias
//
getIndexAccess(conn);
//
// Get table and index information
//
tableName = schema + "." + table;
try (Statement stmt = conn.createStatement()) {
//
try (ResultSet rs = stmt.executeQuery("SHOW COLUMNS FROM " + table + " FROM " + schema)) {
int index = 0;
while (rs.next()) {
String columnName = rs.getString("FIELD");
String columnType = rs.getString("TYPE");
columnType = columnType.substring(0, columnType.indexOf('('));
columnNames.add(columnName);
columnTypes.add(columnType);
if (columnName.equals("DB_ID")) {
dbColumn = index;
}
index++;
}
}
if (dbColumn < 0) {
Logger.logErrorMessage("DB_ID column not found for table " + tableName);
return;
}
//
try (ResultSet rs = stmt.executeQuery(String.format("SELECT COLUMNS FROM FTL.INDEXES WHERE SCHEMA = '%s' AND TABLE = '%s'", schema, table))) {
if (rs.next()) {
String[] columns = rs.getString(1).split(",");
for (String column : columns) {
int pos = columnNames.indexOf(column);
if (pos >= 0) {
if (columnTypes.get(pos).equals("VARCHAR")) {
indexColumns.add(pos);
} else {
Logger.logErrorMessage("Indexed column " + column + " in table " + tableName + " is not a string");
}
} else {
Logger.logErrorMessage("Indexed column " + column + " not found in table " + tableName);
}
}
}
}
if (indexColumns.isEmpty()) {
Logger.logErrorMessage("No indexed columns found for table " + tableName);
return;
}
//
// Trigger is enabled
//
isEnabled = true;
indexTriggers.put(tableName, this);
} catch (SQLException exc) {
Logger.logErrorMessage("Unable to get table information", exc);
}
}
use of org.h2.table.Column in project ignite by apache.
the class GridQueryParsingTest method cn.
/**
* Constructs non-nullable column.
*
* @param name Column name.
* @param type Column data type.
* @return {@link GridSqlColumn} with given name and type.
*/
private static GridSqlColumn cn(String name, int type) {
Column col = new Column(name, type);
col.setNullable(false);
return new GridSqlColumn(col, null, name);
}
use of org.h2.table.Column in project ignite by apache.
the class GridReduceQueryExecutor method createMergeTable.
/**
* @param conn Connection.
* @param qry Query.
* @param explain Explain.
* @return Table.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
private GridMergeTable createMergeTable(JdbcConnection conn, GridCacheSqlQuery qry, boolean explain) throws IgniteCheckedException {
try {
Session ses = (Session) conn.getSession();
CreateTableData data = new CreateTableData();
data.tableName = "T___";
data.schema = ses.getDatabase().getSchema(ses.getCurrentSchemaName());
data.create = true;
if (!explain) {
LinkedHashMap<String, ?> colsMap = qry.columns();
assert colsMap != null;
ArrayList<Column> cols = new ArrayList<>(colsMap.size());
for (Map.Entry<String, ?> e : colsMap.entrySet()) {
String alias = e.getKey();
GridSqlType t = (GridSqlType) e.getValue();
assert !F.isEmpty(alias);
Column c = new Column(alias, t.type(), t.precision(), t.scale(), t.displaySize());
cols.add(c);
}
data.columns = cols;
} else
data.columns = planColumns();
boolean sortedIndex = !F.isEmpty(qry.sortColumns());
GridMergeTable tbl = new GridMergeTable(data);
ArrayList<Index> idxs = new ArrayList<>(2);
if (explain) {
idxs.add(new GridMergeIndexUnsorted(ctx, tbl, sortedIndex ? MERGE_INDEX_SORTED : MERGE_INDEX_UNSORTED));
} else if (sortedIndex) {
List<GridSqlSortColumn> sortCols = (List<GridSqlSortColumn>) qry.sortColumns();
GridMergeIndexSorted sortedMergeIdx = new GridMergeIndexSorted(ctx, tbl, MERGE_INDEX_SORTED, GridSqlSortColumn.toIndexColumns(tbl, sortCols));
idxs.add(GridMergeTable.createScanIndex(sortedMergeIdx));
idxs.add(sortedMergeIdx);
} else
idxs.add(new GridMergeIndexUnsorted(ctx, tbl, MERGE_INDEX_UNSORTED));
tbl.indexes(idxs);
return tbl;
} catch (Exception e) {
U.closeQuiet(conn);
throw new IgniteCheckedException(e);
}
}
Aggregations