Search in sources :

Example 1 with QueryEntityEx

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)");
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueryEntityEx(org.apache.ignite.internal.processors.query.QueryEntityEx) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedHashMap(java.util.LinkedHashMap) QueryIndex(org.apache.ignite.cache.QueryIndex) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Example 2 with QueryEntityEx

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Column(org.h2.table.Column) GridSqlAlterTableAddColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlterTableAddColumn) SqlIndexColumn(org.apache.ignite.internal.sql.command.SqlIndexColumn) GridSqlAlterTableDropColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlterTableDropColumn) QueryEntityEx(org.apache.ignite.internal.processors.query.QueryEntityEx) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) QueryEntity(org.apache.ignite.cache.QueryEntity) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 3 with QueryEntityEx

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;
}
Also used : SqlConfiguration(org.apache.ignite.configuration.SqlConfiguration) QueryEntityEx(org.apache.ignite.internal.processors.query.QueryEntityEx) ConnectorConfiguration(org.apache.ignite.configuration.ConnectorConfiguration) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedHashMap(java.util.LinkedHashMap) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) QueryIndex(org.apache.ignite.cache.QueryIndex) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) HashSet(java.util.HashSet)

Example 4 with QueryEntityEx

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;
}
Also used : QueryEntityEx(org.apache.ignite.internal.processors.query.QueryEntityEx) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) GridSqlAlterTableAddColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlterTableAddColumn) SqlIndexColumn(org.apache.ignite.internal.sql.command.SqlIndexColumn) GridSqlAlterTableDropColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlterTableDropColumn) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)4 QueryEntityEx (org.apache.ignite.internal.processors.query.QueryEntityEx)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 QueryEntity (org.apache.ignite.cache.QueryEntity)3 Map (java.util.Map)2 QueryIndex (org.apache.ignite.cache.QueryIndex)2 AffinityKey (org.apache.ignite.cache.affinity.AffinityKey)2 GridSqlAlterTableAddColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlterTableAddColumn)2 GridSqlAlterTableDropColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlterTableDropColumn)2 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)2 SqlIndexColumn (org.apache.ignite.internal.sql.command.SqlIndexColumn)2 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)2 Column (org.h2.table.Column)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ConnectorConfiguration (org.apache.ignite.configuration.ConnectorConfiguration)1