/** Execute the DROP CLASS. */ public Object execute(final Map<Object, Object> iArgs) { if (className == null) { throw new OCommandExecutionException( "Cannot execute the command because it has not been parsed yet"); } final ODatabaseDocument database = getDatabase(); if (ifExists && !database.getMetadata().getSchema().existsClass(className)) { return true; } final OClass cls = database.getMetadata().getSchema().getClass(className); if (cls == null) { return null; } final long records = cls.count(true); if (records > 0 && !unsafe) { // NOT EMPTY, CHECK IF CLASS IS OF VERTEX OR EDGES if (cls.isSubClassOf("V")) { // FOUND VERTEX CLASS throw new OCommandExecutionException( "'DROP CLASS' command cannot drop class '" + className + "' because it contains Vertices. Use 'DELETE VERTEX' command first to avoid broken edges in a database, or apply the 'UNSAFE' keyword to force it"); } else if (cls.isSubClassOf("E")) { // FOUND EDGE CLASS throw new OCommandExecutionException( "'DROP CLASS' command cannot drop class '" + className + "' because it contains Edges. Use 'DELETE EDGE' command first to avoid broken vertices in a database, or apply the 'UNSAFE' keyword to force it"); } } database.getMetadata().getSchema().dropClass(className); if (records > 0 && unsafe) { // NOT EMPTY, CHECK IF CLASS IS OF VERTEX OR EDGES if (cls.isSubClassOf("V")) { // FOUND VERTICES if (unsafe) OLogManager.instance() .warn( this, "Dropped class '%s' containing %d vertices using UNSAFE mode. Database could contain broken edges", className, records); } else if (cls.isSubClassOf("E")) { // FOUND EDGES OLogManager.instance() .warn( this, "Dropped class '%s' containing %d edges using UNSAFE mode. Database could contain broken vertices", className, records); } } return true; }
@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(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(); }
private void dropClassIndexes(final OClass cls) { final ODatabaseDocument database = getDatabase(); final OIndexManager indexManager = database.getMetadata().getIndexManager(); for (final OIndex<?> index : indexManager.getClassIndexes(cls.getName())) indexManager.dropIndex(index.getName()); }
@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(); }
@Override public void create() { final ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get(); if (db.getMetadata().getSchema().existsClass(OScheduledEvent.CLASS_NAME)) return; final OClass f = db.getMetadata().getSchema().createClass(OScheduledEvent.CLASS_NAME); f.createProperty(OScheduledEvent.PROP_NAME, OType.STRING, (OType) null, true) .setMandatory(true) .setNotNull(true); f.createProperty(OScheduledEvent.PROP_RULE, OType.STRING, (OType) null, true) .setMandatory(true) .setNotNull(true); f.createProperty(OScheduledEvent.PROP_ARGUMENTS, OType.EMBEDDEDMAP, (OType) null, true); f.createProperty(OScheduledEvent.PROP_STATUS, OType.STRING, (OType) null, true); f.createProperty( OScheduledEvent.PROP_FUNC, OType.LINK, db.getMetadata().getSchema().getClass(OFunction.CLASS_NAME), true) .setMandatory(true) .setNotNull(true); f.createProperty(OScheduledEvent.PROP_STARTTIME, OType.DATETIME, (OType) null, true); }
@Override public void load() { final ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get(); if (db.getMetadata().getSchema().existsClass(OScheduledEvent.CLASS_NAME)) { final Iterable<ODocument> result = db.browseClass(OScheduledEvent.CLASS_NAME); for (ODocument d : result) { final OScheduledEvent event = new OScheduledEvent(d); if (events.putIfAbsent(event.getName(), event) == null) this.scheduleEvent(event); } } }
@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 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 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 queryCountWithConditions() { OClass indexed = database.getMetadata().getSchema().getOrCreateClass("Indexed"); indexed.createProperty("key", OType.STRING); indexed.createIndex("keyed", OClass.INDEX_TYPE.NOTUNIQUE, "key"); database.<ODocument>newInstance("Indexed").field("key", "one").save(); database.<ODocument>newInstance("Indexed").field("key", "two").save(); List<ODocument> result = database .command( new OSQLSynchQuery<ODocument>( "select count(*) as total from Indexed where key > 'one'")) .execute(); Assert.assertTrue(result.size() == 1); for (ODocument d : result) { Assert.assertNotNull(d.field("total")); Assert.assertTrue(((Number) d.field("total")).longValue() > 0); } }
@SuppressWarnings("unchecked") public OCommandExecutorSQLCreateEdge parse(final OCommandRequest iRequest) { final OCommandRequestText textRequest = (OCommandRequestText) iRequest; String queryText = textRequest.getText(); String originalQuery = queryText; try { // System.out.println("NEW PARSER FROM: " + queryText); queryText = preParse(queryText, iRequest); // System.out.println("NEW PARSER TO: " + queryText); textRequest.setText(queryText); final ODatabaseDocument database = getDatabase(); init((OCommandRequestText) iRequest); parserRequiredKeyword("CREATE"); parserRequiredKeyword("EDGE"); String className = null; String temp = parseOptionalWord(true); while (temp != null) { if (temp.equals("CLUSTER")) { clusterName = parserRequiredWord(false); } else if (temp.equals(KEYWORD_FROM)) { from = parserRequiredWord(false, "Syntax error", " =><,\r\n"); } else if (temp.equals("TO")) { to = parserRequiredWord(false, "Syntax error", " =><,\r\n"); } else if (temp.equals(KEYWORD_SET)) { fields = new LinkedHashMap<String, Object>(); parseSetFields(clazz, fields); } else if (temp.equals(KEYWORD_CONTENT)) { parseContent(); } else if (temp.equals(KEYWORD_RETRY)) { parseRetry(); } else if (className == null && temp.length() > 0) { className = temp; clazz = ((OMetadataInternal) database.getMetadata()) .getImmutableSchemaSnapshot() .getClass(className); } temp = parseOptionalWord(true); if (parserIsEnded()) break; } if (className == null) { // ASSIGN DEFAULT CLASS className = "E"; clazz = ((OMetadataInternal) database.getMetadata()) .getImmutableSchemaSnapshot() .getClass(className); } // GET/CHECK CLASS NAME if (clazz == null) throw new OCommandSQLParsingException("Class '" + className + "' was not found"); } finally { textRequest.setText(originalQuery); } return this; }