@Override public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception { String[] urlParts = checkSyntax( iRequest.url, 3, "Syntax error: cluster/<database>/<cluster-name>[/<limit>]<br>Limit is optional and is setted to 20 by default. Set expressely to 0 to have no limits."); iRequest.data.commandInfo = "Browse cluster"; iRequest.data.commandDetail = urlParts[2]; ODatabaseDocument db = null; try { db = getProfiledDatabaseInstance(iRequest); if (db.getClusterIdByName(urlParts[2]) > -1) { final int limit = urlParts.length > 3 ? Integer.parseInt(urlParts[3]) : 20; final List<OIdentifiable> response = new ArrayList<OIdentifiable>(); for (ORecord rec : db.browseCluster(urlParts[2])) { if (limit > 0 && response.size() >= limit) break; response.add(rec); } iResponse.writeRecords(response); } else iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, null, null, null, null); } finally { if (db != null) db.close(); } return false; }
public void updateMultipleFields() { database.open("admin", "admin"); List<OClusterPosition> positions = getValidPositions(3); OIdentifiable result = database .command( new OCommandSQL( " INSERT INTO Account SET id= 3232,name= 'my name',map= {\"key\":\"value\"},dir= '',user= #3:" + positions.get(0))) .execute(); Assert.assertNotNull(result); ODocument record = result.getRecord(); Assert.assertEquals(record.field("id"), 3232); Assert.assertEquals(record.field("name"), "my name"); Map<String, String> map = record.field("map"); Assert.assertTrue(map.get("key").equals("value")); Assert.assertEquals(record.field("dir"), ""); Assert.assertEquals(record.field("user", OType.LINK), new ORecordId(3, positions.get(0))); database.close(); }
@Test public void queryProjectionOk() { database.open("admin", "admin"); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( " select nick, followings, followers from Profile where nick is defined and followings is defined and followers is defined")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { String[] colNames = d.fieldNames(); Assert.assertEquals(colNames.length, 3); Assert.assertEquals(colNames[0], "nick"); Assert.assertEquals(colNames[1], "followings"); Assert.assertEquals(colNames[2], "followers"); Assert.assertNull(d.getClassName()); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
public void close() throws SQLException { status = ODatabase.STATUS.CLOSED; if (database != null) { database.activateOnCurrentThread(); database.close(); database = null; } }
@Test @SuppressWarnings("unchecked") public void insertList() { database.open("admin", "admin"); ODocument doc = (ODocument) database .command( new OCommandSQL( "insert into cluster:default (equaledges, name, list) values ('yes', 'square', ['bottom', 'top','left','right'] )")) .execute(); Assert.assertTrue(doc != null); doc = (ODocument) new ODocument(doc.getIdentity()).load(); Assert.assertEquals(doc.field("equaledges"), "yes"); Assert.assertEquals(doc.field("name"), "square"); Assert.assertTrue(doc.field("list") instanceof List); List<Object> entries = ((List<Object>) doc.field("list")); Assert.assertEquals(entries.size(), 4); Assert.assertEquals(entries.get(0), "bottom"); Assert.assertEquals(entries.get(1), "top"); Assert.assertEquals(entries.get(2), "left"); Assert.assertEquals(entries.get(3), "right"); database.delete(doc); doc = (ODocument) database .command( new OCommandSQL( "insert into cluster:default SET equaledges = 'yes', name = 'square', list = ['bottom', 'top','left','right'] ")) .execute(); Assert.assertTrue(doc != null); doc = (ODocument) new ODocument(doc.getIdentity()).load(); Assert.assertEquals(doc.field("equaledges"), "yes"); Assert.assertEquals(doc.field("name"), "square"); Assert.assertTrue(doc.field("list") instanceof List); entries = ((List<Object>) doc.field("list")); Assert.assertEquals(entries.size(), 4); Assert.assertEquals(entries.get(0), "bottom"); Assert.assertEquals(entries.get(1), "top"); Assert.assertEquals(entries.get(2), "left"); Assert.assertEquals(entries.get(3), "right"); database.close(); }
@Override public void deinit() { final long endRecords = database.countClass("Account"); System.out.println( "Total accounts: " + endRecords + ". Expected: " + (beginRecords + data.getCycles())); System.out.println(Orient.instance().getProfiler().dump()); if (database != null) database.close(); super.deinit(); }
@Test public void createProperty() { database.open("admin", "admin"); database.command(new OCommandSQL("create property account.timesheet string")).execute(); Assert.assertEquals( database.getMetadata().getSchema().getClass("account").getProperty("timesheet").getType(), OType.STRING); database.close(); }
@Test public void insertOperator() { database.open("admin", "admin"); int addressId = database.getMetadata().getSchema().getClass("Address").getDefaultClusterId(); List<OClusterPosition> positions = getValidPositions(addressId); ODocument doc = (ODocument) database .command( new OCommandSQL( "insert into Profile (name, surname, salary, location, dummy) values ('Luca','Smith', 109.9, #" + addressId + ":" + positions.get(3) + ", 'hooray')")) .execute(); Assert.assertTrue(doc != null); Assert.assertEquals(doc.field("name"), "Luca"); Assert.assertEquals(doc.field("surname"), "Smith"); Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 109.9f); Assert.assertEquals( doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3))); Assert.assertEquals(doc.field("dummy"), "hooray"); doc = (ODocument) database .command( new OCommandSQL( "insert into Profile SET name = 'Luca', surname = 'Smith', salary = 109.9, location = #" + addressId + ":" + positions.get(3) + ", dummy = 'hooray'")) .execute(); database.delete(doc); Assert.assertTrue(doc != null); Assert.assertEquals(doc.field("name"), "Luca"); Assert.assertEquals(doc.field("surname"), "Smith"); Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 109.9f); Assert.assertEquals( doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3))); Assert.assertEquals(doc.field("dummy"), "hooray"); database.close(); }
@Test @SuppressWarnings("unchecked") public void insertMap() { database.open("admin", "admin"); ODocument doc = (ODocument) database .command( new OCommandSQL( "insert into cluster:default (equaledges, name, properties) values ('no', 'circle', {'round':'eeee', 'blaaa':'zigzag'} )")) .execute(); Assert.assertTrue(doc != null); doc = (ODocument) new ODocument(doc.getIdentity()).load(); Assert.assertEquals(doc.field("equaledges"), "no"); Assert.assertEquals(doc.field("name"), "circle"); Assert.assertTrue(doc.field("properties") instanceof Map); Map<Object, Object> entries = ((Map<Object, Object>) doc.field("properties")); Assert.assertEquals(entries.size(), 2); Assert.assertEquals(entries.get("round"), "eeee"); Assert.assertEquals(entries.get("blaaa"), "zigzag"); database.delete(doc); doc = (ODocument) database .command( new OCommandSQL( "insert into cluster:default SET equaledges = 'no', name = 'circle', properties = {'round':'eeee', 'blaaa':'zigzag'} ")) .execute(); Assert.assertTrue(doc != null); doc = (ODocument) new ODocument(doc.getIdentity()).load(); Assert.assertEquals(doc.field("equaledges"), "no"); Assert.assertEquals(doc.field("name"), "circle"); Assert.assertTrue(doc.field("properties") instanceof Map); entries = ((Map<Object, Object>) doc.field("properties")); Assert.assertEquals(entries.size(), 2); Assert.assertEquals(entries.get("round"), "eeee"); Assert.assertEquals(entries.get("blaaa"), "zigzag"); database.close(); }
@Test(dependsOnMethods = "createLinkedTypeProperty") public void removeProperty() { database.open("admin", "admin"); database.command(new OCommandSQL("drop property account.timesheet")).execute(); database.command(new OCommandSQL("drop property account.tags")).execute(); Assert.assertFalse( database.getMetadata().getSchema().getClass("account").existsProperty("timesheet")); Assert.assertFalse( database.getMetadata().getSchema().getClass("account").existsProperty("tags")); database.close(); }
@Test(expectedExceptions = OCommandSQLParsingException.class) public void queryProjectionFlattenError() { database.open("admin", "admin"); try { database .command( new OSQLSynchQuery<ODocument>( "SELECT FLATTEN( out ), in FROM OGraphVertex WHERE out TRAVERSE(1,1) (@class = 'OGraphEdge')")) .execute(); } finally { database.close(); } }
@Test(dependsOnMethods = "createLinkedClassProperty") public void createLinkedTypeProperty() { database.open("admin", "admin"); database.command(new OCommandSQL("create property account.tags embeddedlist string")).execute(); Assert.assertEquals( database.getMetadata().getSchema().getClass("account").getProperty("tags").getType(), OType.EMBEDDEDLIST); Assert.assertEquals( database.getMetadata().getSchema().getClass("account").getProperty("tags").getLinkedType(), OType.STRING); database.close(); }
@Test public void insertWithNoSpaces() { database.open("admin", "admin"); ODocument doc = (ODocument) database .command( new OCommandSQL( "insert into cluster:default(id, title)values(10, 'NoSQL movement')")) .execute(); Assert.assertTrue(doc != null); database.close(); }
@Test public void insertWithWildcards() { database.open("admin", "admin"); int addressId = database.getMetadata().getSchema().getClass("Address").getDefaultClusterId(); List<OClusterPosition> positions = getValidPositions(addressId); ODocument doc = (ODocument) database .command( new OCommandSQL( "insert into Profile (name, surname, salary, location, dummy) values (?,?,?,?,?)")) .execute( "Marc", "Smith", 120.0, new ORecordId(addressId, positions.get(3)), "hooray"); Assert.assertTrue(doc != null); Assert.assertEquals(doc.field("name"), "Marc"); Assert.assertEquals(doc.field("surname"), "Smith"); Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 120.0f); Assert.assertEquals( doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3))); Assert.assertEquals(doc.field("dummy"), "hooray"); database.delete(doc); doc = (ODocument) database .command( new OCommandSQL( "insert into Profile SET name = ?, surname = ?, salary = ?, location = ?, dummy = ?")) .execute( "Marc", "Smith", 120.0, new ORecordId(addressId, positions.get(3)), "hooray"); Assert.assertTrue(doc != null); Assert.assertEquals(doc.field("name"), "Marc"); Assert.assertEquals(doc.field("surname"), "Smith"); Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 120.0f); Assert.assertEquals( doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3))); Assert.assertEquals(doc.field("dummy"), "hooray"); database.close(); }
@Test public void insertAvoidingSubQuery() { database.open("admin", "admin"); final OSchema schema = database.getMetadata().getSchema(); if (schema.getClass("test") == null) schema.createClass("test"); ODocument doc = (ODocument) database .command(new OCommandSQL("INSERT INTO test(text) VALUES ('(Hello World)')")) .execute(); Assert.assertTrue(doc != null); Assert.assertEquals(doc.field("text"), "(Hello World)"); database.close(); }
@Test public void insertCluster() { database.open("admin", "admin"); ODocument doc = (ODocument) database .command( new OCommandSQL( "insert into Account cluster default (id, title) values (10, 'NoSQL movement')")) .execute(); Assert.assertTrue(doc != null); Assert.assertEquals(doc.getIdentity().getClusterId(), database.getDefaultClusterId()); Assert.assertEquals(doc.getClassName(), "Account"); database.close(); }
/** @return result of execution */ public final V execute() { ODatabaseDocument db = null; ODatabaseDocument oldDb = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined(); if (oldDb != null) ODatabaseRecordThreadLocal.INSTANCE.remove(); // Required to avoid stack of transactions try { db = getSettings() .getDatabasePoolFactory() .get(getDBUrl(), getUsername(), getPassword()) .acquire(); db.activateOnCurrentThread(); return execute(db); } finally { if (db != null) db.close(); if (oldDb != null) ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) oldDb); else ODatabaseRecordThreadLocal.INSTANCE.remove(); } }
@Test public void queryProjectionContentCollection() { database.open("admin", "admin"); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( "SELECT FLATTEN( out ) FROM OGraphVertex WHERE out TRAVERSE(1,1) (@class = 'OGraphEdge')")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertEquals(d.getClassName(), "OGraphEdge"); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
@Test public void queryProjectionJSON() { database.open("admin", "admin"); List<ODocument> result = database .command(new OSQLSynchQuery<ODocument>("select @this.toJson() as json from Profile")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertTrue(d.fieldNames().length <= 1); Assert.assertNotNull(d.field("json")); new ODocument().fromJSON((String) d.field("json")); } database.close(); }
@Test public void insertSubQuery() { database.open("admin", "admin"); final OSchema schema = database.getMetadata().getSchema(); if (schema.getClass("test") == null) schema.createClass("test"); ODocument doc = (ODocument) database .command(new OCommandSQL("INSERT INTO test SET names = (select name from OUser)")) .execute(); Assert.assertTrue(doc != null); Assert.assertNotNull(doc.field("names")); Assert.assertTrue(doc.field("names") instanceof Collection); Assert.assertEquals(((Collection<?>) doc.field("names")).size(), 3); database.close(); }
@Test public void queryProjectionSimpleValues() { database.open("admin", "admin"); List<ODocument> result = database .command(new OSQLSynchQuery<ODocument>("select 10, 'ciao' from Profile LIMIT 1")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertTrue(d.fieldNames().length <= 2); Assert.assertEquals(((Integer) d.field("10")).intValue(), 10l); Assert.assertEquals(d.field("ciao"), "ciao"); Assert.assertNull(d.getClassName()); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
@Test(dependsOnMethods = "createProperty") public void createLinkedClassProperty() { database.open("admin", "admin"); database .command(new OCommandSQL("create property account.knows embeddedmap account")) .execute(); Assert.assertEquals( database.getMetadata().getSchema().getClass("account").getProperty("knows").getType(), OType.EMBEDDEDMAP); Assert.assertEquals( database .getMetadata() .getSchema() .getClass("account") .getProperty("knows") .getLinkedClass(), database.getMetadata().getSchema().getClass("account")); database.close(); }
@Test public void queryProjectionLinkedAndFunction() { database.open("admin", "admin"); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( "select name.toUppercase(), address.city.country.name from Profile")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertTrue(d.fieldNames().length <= 2); if (d.field("name") != null) Assert.assertTrue(d.field("name").equals(((String) d.field("name")).toUpperCase())); Assert.assertNull(d.getClassName()); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
@Test public void queryProjectionStaticValues() { database.open("admin", "admin"); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( "select location.city.country.name, address.city.country.name from Profile where location.city.country.name is not null")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertNotNull(d.field("location")); Assert.assertNull(d.field("address")); Assert.assertNull(d.getClassName()); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
@Test public void queryProjectionAliases() { database.open("admin", "admin"); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( "select name.append('!') as 1, surname as 2 from Profile where name is not null and surname is not null")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertTrue(d.fieldNames().length <= 2); Assert.assertTrue(d.field("1").toString().endsWith("!")); Assert.assertNotNull(d.field("2")); Assert.assertNull(d.getClassName()); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
@Test public void queryProjectionSameFieldTwice() { database.open("admin", "admin"); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( "select name, name.toUppercase() from Profile where name is not null")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertTrue(d.fieldNames().length <= 2); Assert.assertNotNull(d.field("name")); Assert.assertNotNull(d.field("name2")); Assert.assertNull(d.getClassName()); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
@Test public void queryProjectionFunctionsAndFieldOperators() { database.open("admin", "admin"); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( "select max(name.append('.')).prefix('Mr. ') as name from Profile where name is not null")) .execute(); Assert.assertTrue(result.size() != 0); for (ODocument d : result) { Assert.assertTrue(d.fieldNames().length <= 1); Assert.assertTrue(d.field("name").toString().startsWith("Mr. ")); Assert.assertTrue(d.field("name").toString().endsWith(".")); Assert.assertNull(d.getClassName()); Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE); } database.close(); }
@Override public void deinit() { database.close(); super.deinit(); }
@AfterTest public void closeDatabase() { database.close(); }