Search in sources :

Example 11 with EISException

use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.

the class MongoPlatform method buildInteractionSpec.

/**
 * Allow the platform to build the interaction spec based on properties defined in the interaction.
 */
@Override
public InteractionSpec buildInteractionSpec(EISInteraction interaction) {
    InteractionSpec spec = interaction.getInteractionSpec();
    if (spec == null) {
        MongoInteractionSpec mongoSpec = new MongoInteractionSpec();
        Object operation = interaction.getProperty(OPERATION);
        if (interaction.isQueryStringCall()) {
            mongoSpec.setCode(((QueryStringCall) interaction).getQueryString());
            operation = MongoOperation.EVAL;
        }
        if (operation == null) {
            throw new EISException("'" + OPERATION + "' property must be set on the query's interation.");
        }
        if (operation instanceof String) {
            operation = MongoOperation.valueOf((String) operation);
        }
        mongoSpec.setOperation((MongoOperation) operation);
        Object collection = interaction.getProperty(COLLECTION);
        if (collection != null) {
            mongoSpec.setCollection((String) collection);
        }
        // Allows setting of read preference as a property.
        Object preference = interaction.getProperty(READ_PREFERENCE);
        if (preference instanceof ReadPreference) {
            mongoSpec.setReadPreference((ReadPreference) preference);
        } else if (preference instanceof String) {
            String constant = (String) preference;
            if (constant.equals("PRIMARY")) {
                mongoSpec.setReadPreference(ReadPreference.primary());
            } else if (constant.equals("SECONDARY")) {
                mongoSpec.setReadPreference(ReadPreference.secondary());
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }
        // Allows setting of write concern as a property.
        Object concern = interaction.getProperty(WRITE_CONCERN);
        if (concern instanceof WriteConcern) {
            mongoSpec.setWriteConcern((WriteConcern) concern);
        } else if (concern instanceof String) {
            String constant = (String) concern;
            if (constant.equals("FSYNC_SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.FSYNC_SAFE);
            } else if (constant.equals("JOURNAL_SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.JOURNAL_SAFE);
            } else if (constant.equals("MAJORITY")) {
                mongoSpec.setWriteConcern(WriteConcern.MAJORITY);
            } else if (constant.equals("NONE")) {
                mongoSpec.setWriteConcern(/* WriteConcern.NONE */
                new WriteConcern("none"));
            } else if (constant.equals("NORMAL")) {
                mongoSpec.setWriteConcern(WriteConcern.NORMAL);
            } else if (constant.equals("REPLICAS_SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.REPLICAS_SAFE);
            } else if (constant.equals("SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.SAFE);
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }
        // Allows setting of options as a property.
        Object options = interaction.getProperty(OPTIONS);
        if (options instanceof Number) {
            mongoSpec.setOptions(((Number) options).intValue());
        } else if (options instanceof String) {
            mongoSpec.setOptions(Integer.parseInt(((String) options)));
        }
        // Allows setting of skip as a property.
        Object skip = interaction.getProperty(SKIP);
        if (skip instanceof Number) {
            mongoSpec.setSkip(((Number) skip).intValue());
        } else if (skip instanceof String) {
            mongoSpec.setSkip(Integer.parseInt(((String) skip)));
        }
        // Allows setting of limit as a property.
        Object limit = interaction.getProperty(LIMIT);
        if (limit instanceof Number) {
            mongoSpec.setLimit(((Number) limit).intValue());
        } else if (skip instanceof String) {
            mongoSpec.setLimit(Integer.parseInt(((String) limit)));
        }
        // Allows setting of batchSize as a property.
        Object batchSize = interaction.getProperty(BATCH_SIZE);
        if (batchSize instanceof Number) {
            mongoSpec.setBatchSize(((Number) batchSize).intValue());
        } else if (skip instanceof String) {
            mongoSpec.setBatchSize(Integer.parseInt(((String) batchSize)));
        }
        spec = mongoSpec;
    }
    return spec;
}
Also used : ReadPreference(com.mongodb.ReadPreference) InteractionSpec(jakarta.resource.cci.InteractionSpec) MongoInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.mongo.MongoInteractionSpec) WriteConcern(com.mongodb.WriteConcern) MongoInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.mongo.MongoInteractionSpec) EISException(org.eclipse.persistence.eis.EISException)

Example 12 with EISException

use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.

the class MongoPlatform method appendExpressionToQueryRow.

/**
 * Append the expression and recursively to the query row.
 */
protected void appendExpressionToQueryRow(Expression expression, AbstractRecord row, DatabaseQuery query) {
    if (expression.isRelationExpression()) {
        RelationExpression relation = (RelationExpression) expression;
        Object left = extractValueFromExpression(relation.getFirstChild(), query);
        Object right = extractValueFromExpression(relation.getSecondChild(), query);
        if (relation.getOperator().getSelector() == ExpressionOperator.Equal) {
            row.put(left, right);
        } else {
            DatabaseRecord nested = new DatabaseRecord();
            if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThan) {
                nested.put("$gt", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.LessThan) {
                nested.put("$lt", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.LessThanEqual) {
                nested.put("$lte", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThanEqual) {
                nested.put("$gte", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.NotEqual) {
                nested.put("$ne", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.In) {
                nested.put("$in", right);
                row.put(left, nested);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.NotIn) {
                nested.put("$nin", right);
                row.put(left, nested);
            } else {
                throw new EISException("Query too complex for Mongo translation, relation [" + expression + "] not supported in query: " + query);
            }
            row.put(left, nested);
        }
    } else if (expression.isLogicalExpression()) {
        LogicalExpression logic = (LogicalExpression) expression;
        DatabaseRecord first = new DatabaseRecord();
        DatabaseRecord second = new DatabaseRecord();
        appendExpressionToQueryRow(logic.getFirstChild(), first, query);
        appendExpressionToQueryRow(logic.getSecondChild(), second, query);
        List<DatabaseRecord> nested = new Vector<>();
        nested.add(first);
        nested.add(second);
        if (logic.getOperator().getSelector() == ExpressionOperator.And) {
            row.put("$and", nested);
        } else if (logic.getOperator().getSelector() == ExpressionOperator.Or) {
            row.put("$or", nested);
        } else {
            throw new EISException("Query too complex for Mongo translation, logic [" + expression + "] not supported in query: " + query);
        }
    } else if (expression.isFunctionExpression()) {
        FunctionExpression function = (FunctionExpression) expression;
        if (function.getOperator().getSelector() == ExpressionOperator.Like) {
            Object left = extractValueFromExpression(function.getChildren().get(0), query);
            Object right = extractValueFromExpression(function.getChildren().get(1), query);
            if (!(right instanceof String)) {
                throw new EISException("Query too complex for Mongo translation, like with [" + right + "] not supported in query: " + query);
            }
            String pattern = (String) right;
            DatabaseRecord nested = new DatabaseRecord();
            if (!this.isLikeRegex) {
                pattern = Helper.convertLikeToRegex(pattern);
            }
            nested.put("$regex", pattern);
            row.put(left, nested);
        } else if (function.getOperator().getSelector() == ExpressionOperator.Not) {
            // Detect situation 'not(a = b)' and change it to '(a != b)'
            Expression expr = function.getChildren().get(0);
            if (expr.isRelationExpression()) {
                RelationExpression relation = (RelationExpression) expr;
                Object left = extractValueFromExpression(relation.getFirstChild(), query);
                Object right = extractValueFromExpression(relation.getSecondChild(), query);
                DatabaseRecord nested = new DatabaseRecord();
                if (expr.getOperator().getSelector() == ExpressionOperator.Equal) {
                    nested.put("$ne", right);
                } else {
                    nested.put("not", right);
                }
                row.put(left, nested);
            } else {
                throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
            }
        } else {
            throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
        }
    } else {
        throw new EISException("Query too complex for Mongo translation, expression [" + expression + "] not supported in query: " + query);
    }
}
Also used : LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) EISException(org.eclipse.persistence.eis.EISException) List(java.util.List)

Aggregations

EISException (org.eclipse.persistence.eis.EISException)12 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)4 InitialContext (javax.naming.InitialContext)3 Expression (org.eclipse.persistence.expressions.Expression)3 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)3 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)3 LogicalExpression (org.eclipse.persistence.internal.expressions.LogicalExpression)3 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)3 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)3 RelationExpression (org.eclipse.persistence.internal.expressions.RelationExpression)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 ReadPreference (com.mongodb.ReadPreference)2 WriteConcern (com.mongodb.WriteConcern)2 InteractionSpec (jakarta.resource.cci.InteractionSpec)2 List (java.util.List)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)2 AQDequeueOption (oracle.AQ.AQDequeueOption)1 Consistency (oracle.kv.Consistency)1 Durability (oracle.kv.Durability)1