use of org.apache.ignite.internal.processors.query.QueryEntityEx in project ignite by apache.
the class JdbcThinMetadataSelfTest method beforeTestsStarted.
/**
* {@inheritDoc}
*/
@Override
protected void beforeTestsStarted() throws Exception {
super.beforeTestsStarted();
startGridsMultiThreaded(3);
Map<String, Integer> orgPrecision = new HashMap<>();
orgPrecision.put("name", 42);
IgniteCache<String, Organization> orgCache = jcache(grid(0), cacheConfiguration(new QueryEntity(String.class.getName(), Organization.class.getName()).addQueryField("id", Integer.class.getName(), null).addQueryField("name", String.class.getName(), null).setFieldsPrecision(orgPrecision).setIndexes(Arrays.asList(new QueryIndex("id"), new QueryIndex("name", false, "org_name_index")))), "org");
assert orgCache != null;
orgCache.put("o1", new Organization(1, "A"));
orgCache.put("o2", new Organization(2, "B"));
LinkedHashMap<String, Boolean> persFields = new LinkedHashMap<>();
persFields.put("name", true);
persFields.put("age", false);
IgniteCache<AffinityKey, Person> personCache = jcache(grid(0), cacheConfiguration(new QueryEntityEx(new QueryEntity(AffinityKey.class.getName(), Person.class.getName()).addQueryField("name", String.class.getName(), null).addQueryField("age", Integer.class.getName(), null).addQueryField("orgId", Integer.class.getName(), null).setIndexes(Arrays.asList(new QueryIndex("orgId"), new QueryIndex().setFields(persFields)))).setNotNullFields(new HashSet<>(Arrays.asList("age", "name")))), "pers");
assert personCache != null;
personCache.put(new AffinityKey<>("p1", "o1"), new Person("John White", 25, 1));
personCache.put(new AffinityKey<>("p2", "o1"), new Person("Joe Black", 35, 1));
personCache.put(new AffinityKey<>("p3", "o2"), new Person("Mike Green", 40, 2));
jcache(grid(0), defaultCacheConfiguration().setIndexedTypes(Integer.class, Department.class), "dep");
try (Connection conn = DriverManager.getConnection(URL)) {
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE TEST (ID INT, NAME VARCHAR(50) default 'default name', " + "age int default 21, VAL VARCHAR(50), PRIMARY KEY (ID, NAME))");
stmt.execute("CREATE TABLE \"Quoted\" (\"Id\" INT primary key, \"Name\" VARCHAR(50)) WITH WRAP_KEY");
stmt.execute("CREATE INDEX \"MyTestIndex quoted\" on \"Quoted\" (\"Id\" DESC)");
stmt.execute("CREATE INDEX IDX ON TEST (ID ASC)");
stmt.execute("CREATE TABLE TEST_DECIMAL_COLUMN (ID INT primary key, DEC_COL DECIMAL(8, 3))");
stmt.execute("CREATE TABLE TEST_DECIMAL_COLUMN_PRECISION (ID INT primary key, DEC_COL DECIMAL(8))");
stmt.execute("CREATE TABLE TEST_DECIMAL_DATE_COLUMN_META (ID INT primary key, DEC_COL DECIMAL(8), DATE_COL DATE)");
}
}
use of org.apache.ignite.internal.processors.query.QueryEntityEx 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.apache.ignite.internal.processors.query.QueryEntityEx in project ignite by apache.
the class JdbcMetadataSelfTest method getConfiguration.
/**
* {@inheritDoc}
*/
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
LinkedHashMap<String, Boolean> persFields = new LinkedHashMap<>();
persFields.put("name", true);
persFields.put("age", false);
cfg.setCacheConfiguration(cacheConfiguration("pers").setQueryEntities(Arrays.asList(new QueryEntityEx(new QueryEntity(AffinityKey.class.getName(), Person.class.getName()).addQueryField("name", String.class.getName(), null).addQueryField("age", Integer.class.getName(), null).addQueryField("orgId", Integer.class.getName(), null).setIndexes(Arrays.asList(new QueryIndex("orgId"), new QueryIndex().setFields(persFields)))).setNotNullFields(new HashSet<>(Arrays.asList("age", "name"))))), cacheConfiguration("org").setQueryEntities(Arrays.asList(new QueryEntity(AffinityKey.class, Organization.class))), cacheConfiguration("metaTest").setQueryEntities(Arrays.asList(new QueryEntity(AffinityKey.class, MetaTest.class))));
cfg.setConnectorConfiguration(new ConnectorConfiguration());
cfg.setSqlConfiguration(new SqlConfiguration().setSqlSchemas("PREDEFINED_SCHEMAS_1", "PREDEFINED_SCHEMAS_2"));
return cfg;
}
use of org.apache.ignite.internal.processors.query.QueryEntityEx in project ignite by apache.
the class CommandProcessor 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) {
QueryEntityEx res = new QueryEntityEx();
res.setTableName(createTbl.tableName());
Set<String> notNullFields = null;
HashMap<String, Object> dfltValues = new HashMap<>();
Map<String, Integer> precision = new HashMap<>();
Map<String, Integer> scale = new HashMap<>();
for (Map.Entry<String, GridSqlColumn> e : createTbl.columns().entrySet()) {
GridSqlColumn gridCol = e.getValue();
Column col = gridCol.column();
res.addQueryField(e.getKey(), getTypeClassName(gridCol), 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 (col.getType() == Value.DECIMAL) {
if (col.getPrecision() < H2Utils.DECIMAL_DEFAULT_PRECISION)
precision.put(e.getKey(), (int) col.getPrecision());
if (col.getScale() < H2Utils.DECIMAL_DEFAULT_SCALE)
scale.put(e.getKey(), col.getScale());
}
if (col.getType() == Value.STRING || col.getType() == Value.STRING_FIXED || col.getType() == Value.STRING_IGNORECASE || col.getType() == Value.BYTES)
if (col.getPrecision() < H2Utils.STRING_DEFAULT_PRECISION)
precision.put(e.getKey(), (int) col.getPrecision());
}
if (!F.isEmpty(dfltValues))
res.setDefaultFieldValues(dfltValues);
if (!F.isEmpty(precision))
res.setFieldsPrecision(precision);
if (!F.isEmpty(scale))
res.setFieldsScale(scale);
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 = getTypeClassName(pkCol);
res.setKeyFieldName(pkCol.columnName());
} else {
res.setKeyFields(createTbl.primaryKeyColumns());
res.setPreserveKeysOrder(true);
}
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 = getTypeClassName(valCol);
res.setValueFieldName(valCol.columnName());
}
res.setValueType(valTypeName);
res.setKeyType(keyTypeName);
if (!F.isEmpty(notNullFields))
res.setNotNullFields(notNullFields);
// Fill key object with all fields for new tables.
res.fillAbsentPKsWithDefaults(true);
if (Objects.nonNull(createTbl.primaryKeyInlineSize()))
res.setPrimaryKeyInlineSize(createTbl.primaryKeyInlineSize());
if (Objects.nonNull(createTbl.affinityKeyInlineSize()))
res.setAffinityKeyInlineSize(createTbl.affinityKeyInlineSize());
return res;
}
Aggregations