Repository: metamodel Updated Branches: refs/heads/master f23b2dfb4 -> 491ab5161
METAMODEL-136: Fixed Fixes #21 Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/491ab516 Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/491ab516 Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/491ab516 Branch: refs/heads/master Commit: 491ab5161b6b9fa7f2357b88ca1749a2cc1c4dd2 Parents: f23b2df Author: Alberto Rodriguez <[email protected]> Authored: Wed Apr 29 14:20:13 2015 +0200 Committer: Kasper Sørensen <[email protected]> Committed: Wed Apr 29 14:20:13 2015 +0200 ---------------------------------------------------------------------- CHANGES.md | 4 ++ .../metamodel/mongodb/MongoDbDataContext.java | 19 ++++++- .../mongodb/MongoDbDataContextTest.java | 56 ++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/491ab516/CHANGES.md ---------------------------------------------------------------------- diff --git a/CHANGES.md b/CHANGES.md index 41f2e71..befe7a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +### Work-in-progress + + * [METAMODEL-136] - Added LIKE operator native support (using conversion to regex) for MongoDB. + ### Apache MetaModel 4.3.3 * [METAMODEL-123] - Added compatibility with ElasticSearch version 1.4.x http://git-wip-us.apache.org/repos/asf/metamodel/blob/491ab516/mongodb/src/main/java/org/apache/metamodel/mongodb/MongoDbDataContext.java ---------------------------------------------------------------------- diff --git a/mongodb/src/main/java/org/apache/metamodel/mongodb/MongoDbDataContext.java b/mongodb/src/main/java/org/apache/metamodel/mongodb/MongoDbDataContext.java index 008dde9..5e89b6d 100644 --- a/mongodb/src/main/java/org/apache/metamodel/mongodb/MongoDbDataContext.java +++ b/mongodb/src/main/java/org/apache/metamodel/mongodb/MongoDbDataContext.java @@ -25,6 +25,7 @@ import java.util.Map.Entry; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; +import java.util.regex.Pattern; import org.apache.metamodel.DataContext; import org.apache.metamodel.MetaModelException; @@ -391,7 +392,12 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U final BasicDBObject existingFilterObject = (BasicDBObject) query.get(columnName); if (existingFilterObject == null) { if (operatorName == null) { - query.put(columnName, operand); + if (item.getOperator().equals(OperatorType.LIKE)) { + query.put(columnName, turnOperandIntoRegExp(operand)); + } + else { + query.put(columnName, operand); + } } else { query.put(columnName, new BasicDBObject(operatorName, operand)); } @@ -409,6 +415,7 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U final String operatorName; switch (item.getOperator()) { case EQUALS_TO: + case LIKE: operatorName = null; break; case LESS_THAN: @@ -435,6 +442,16 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U return operatorName; } + private Pattern turnOperandIntoRegExp(Object operand) { + StringBuilder operandAsRegExp = new StringBuilder(replaceWildCardLikeChars(operand.toString())); + operandAsRegExp.insert(0, "^").append("$"); + return Pattern.compile(operandAsRegExp.toString(), Pattern.CASE_INSENSITIVE); + } + + private String replaceWildCardLikeChars(String operand) { + return operand.replaceAll("%", ".*"); + } + @Override protected DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) { return materializeMainSchemaTableInternal(table, columns, null, 1, maxRows, true); http://git-wip-us.apache.org/repos/asf/metamodel/blob/491ab516/mongodb/src/test/java/org/apache/metamodel/mongodb/MongoDbDataContextTest.java ---------------------------------------------------------------------- diff --git a/mongodb/src/test/java/org/apache/metamodel/mongodb/MongoDbDataContextTest.java b/mongodb/src/test/java/org/apache/metamodel/mongodb/MongoDbDataContextTest.java index b2563d2..1a4a3af 100644 --- a/mongodb/src/test/java/org/apache/metamodel/mongodb/MongoDbDataContextTest.java +++ b/mongodb/src/test/java/org/apache/metamodel/mongodb/MongoDbDataContextTest.java @@ -490,4 +490,60 @@ public class MongoDbDataContextTest extends MongoDbTestCase { dc.refreshSchemas(); assertEquals(0, defaultSchema.getTableCount()); } + + public void testSelectWithLikeOperator() throws Exception { + if (!isConfigured()) { + System.err.println(getInvalidConfigurationMessage()); + return; + } + + DBCollection col = db.createCollection(getCollectionName(), null); + + // delete if already exists + { + col.drop(); + col = db.createCollection(getCollectionName(), null); + } + + final BasicDBObject dbRow = new BasicDBObject(); + dbRow.append("name", new BasicDBObject().append("first", "John").append("last", "Doe")); + dbRow.append("gender", "MALE"); + col.insert(dbRow); + + final BasicDBObject dbRow2 = new BasicDBObject(); + dbRow2.append("name", new BasicDBObject().append("first", "Mary").append("last", "Johnson")); + dbRow2.append("gender", "FEMALE"); + col.insert(dbRow2); + + final BasicDBObject dbRow3 = new BasicDBObject(); + dbRow3.append("name", new BasicDBObject().append("first", "X").append("last", "Unknown")); + dbRow3.append("gender", "UNKNOWN"); + col.insert(dbRow3); + + final MongoDbDataContext dc = new MongoDbDataContext(db, new SimpleTableDef(getCollectionName(), new String[] { + "name.first", "name.last", "gender", "addresses", "addresses[0].city", "addresses[0].country", + "addresses[5].foobar" })); + + final DataSet ds1 = dc.executeQuery("select * from my_collection where gender LIKE '%MALE%'"); + final DataSet ds2 = dc.executeQuery("select * from my_collection where gender LIKE 'MALE%'"); + final DataSet ds3 = dc.executeQuery("select * from my_collection where gender LIKE '%NK%OW%'"); + final DataSet ds4 = dc.executeQuery("select * from my_collection where gender LIKE '%MALE'"); + try { + assertTrue(ds1.next()); + assertTrue(ds1.next()); + assertFalse(ds1.next()); + assertTrue(ds2.next()); + assertFalse(ds2.next()); + assertTrue(ds3.next()); + assertFalse(ds3.next()); + assertTrue(ds4.next()); + assertTrue(ds4.next()); + assertFalse(ds4.next()); + } finally { + ds1.close(); + ds2.close(); + ds3.close(); + ds4.close(); + } + } }
