logo

A basic ad hoc query example

A basic ad hoc query example

This project source code is available on github at https://github.com/deanhiller/playorm.

Playorm has a command line tool which can query on any table that was from an @NoSQLEntity object. Apart from that, one can also code up and save DboTableMeta objects and the ad-hoc query interface will provide query support into those tables.

e.g., Assuming that you already have NoSqlEntityManager as mgr, you can write below code to create an entity in Playorm using code as below:

DboDatabaseMeta metaDb = mgr.find(DboDatabaseMeta.class, DboDatabaseMeta.META_DB_ROWKEY);
DboTableMeta meta = new DboTableMeta();
meta.setup(null, "MyEntity", false);
metaDb.addMetaClassDbo(meta);

DboColumnIdMeta idMeta = new DboColumnIdMeta();
idMeta.setup(meta, "idField", String.class, true);
mgr.put(idMeta);

DboColumnCommonMeta fieldDbo = new DboColumnCommonMeta();
fieldDbo.setup(meta, "firstname", String.class, true, false);
mgr.put(fieldDbo);
DboColumnCommonMeta fieldDbo2= new DboColumnCommonMeta();
fieldDbo2.setup(meta, "lastname", String.class, true, false);
mgr.put(fieldDbo2);

mgr.put(meta);
mgr.flush();

And then insert some data in the above table like below:

NoSqlTypedSession session = mgr.getTypedSession();
TypedRow typedRow = session.createTypedRow("MyEntity");
typedRow.addColumn("firstname", "harry");
typedRow.addColumn("lastname", "potter");
typedRow.setRowKey("name1");

session.put("MyEntity", typedRow);
session.flush();

and finally, can query on the above entity:

String sql = "select * FROM MyEntity as e WHERE e.firstname = \"harry\"";
Iterable<KeyValue<TypedRow>> rowsIterable = session.createQueryCursor(sql, 50).getPrimaryViewIter();

 << Back to Documentation Index