use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class OCommandExecutorSQLTransactional method execute.
@Override
public Object execute(Map<Object, Object> iArgs) {
final ODatabaseDocument database = getDatabase();
boolean txbegun = database.getTransaction() == null || !database.getTransaction().isActive();
if (txbegun)
database.begin();
try {
final Object result = super.execute(iArgs);
if (txbegun)
database.commit();
return result;
} catch (Exception e) {
if (txbegun)
database.rollback();
throw OException.wrapException(new OCommandExecutionException("Transactional command failed"), e);
}
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class ODocumentValidationTest method testValidationNotValidEmbeddedSet.
@Test
public void testValidationNotValidEmbeddedSet() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
db.create();
try {
OClass embeddedClazz = db.getMetadata().getSchema().createClass("EmbeddedValidation");
embeddedClazz.createProperty("int", OType.INTEGER).setMandatory(true);
embeddedClazz.createProperty("long", OType.LONG).setMandatory(true);
OClass clazz = db.getMetadata().getSchema().createClass("Validation");
clazz.createProperty("int", OType.INTEGER).setMandatory(true);
clazz.createProperty("long", OType.LONG).setMandatory(true);
clazz.createProperty("embeddedSet", OType.EMBEDDEDSET, embeddedClazz).setMandatory(true);
ODocument d = new ODocument(clazz);
d.field("int", 30);
d.field("long", 30);
final Set<ODocument> embeddedSet = new HashSet<ODocument>();
d.field("embeddedSet", embeddedSet);
ODocument embeddedInSet = new ODocument("EmbeddedValidation");
embeddedInSet.field("int", 30);
embeddedInSet.field("long", 30);
embeddedSet.add(embeddedInSet);
ODocument embeddedInSet2 = new ODocument("EmbeddedValidation");
embeddedInSet2.field("int", 30);
embeddedSet.add(embeddedInSet2);
try {
d.validate();
Assert.fail("Validation doesn't throw exception");
} catch (OValidationException e) {
Assert.assertTrue(e.toString().contains("EmbeddedValidation.long"));
}
} finally {
db.drop();
}
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class ODocumentValidationTest method testMaxValidation.
@Test
public void testMaxValidation() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
db.create();
try {
OClass clazz = db.getMetadata().getSchema().createClass("Validation");
clazz.createProperty("int", OType.INTEGER).setMax("11");
clazz.createProperty("long", OType.LONG).setMax("11");
clazz.createProperty("float", OType.FLOAT).setMax("11");
// clazz.createProperty("boolean", OType.BOOLEAN) no meaning
clazz.createProperty("binary", OType.BINARY).setMax("11");
clazz.createProperty("byte", OType.BYTE).setMax("11");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, cal.get(Calendar.HOUR) == 11 ? 0 : 1);
SimpleDateFormat format = ((ODatabaseDocumentTx) db).getStorage().getConfiguration().getDateFormatInstance();
clazz.createProperty("date", OType.DATE).setMax(format.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
format = ((ODatabaseDocumentTx) db).getStorage().getConfiguration().getDateTimeFormatInstance();
clazz.createProperty("datetime", OType.DATETIME).setMax(format.format(cal.getTime()));
clazz.createProperty("decimal", OType.DECIMAL).setMax("11");
clazz.createProperty("double", OType.DOUBLE).setMax("11");
clazz.createProperty("short", OType.SHORT).setMax("11");
clazz.createProperty("string", OType.STRING).setMax("11");
// clazz.createProperty("link", OType.LINK) no meaning
// clazz.createProperty("embedded", OType.EMBEDDED) no meaning
clazz.createProperty("embeddedList", OType.EMBEDDEDLIST).setMax("2");
clazz.createProperty("embeddedSet", OType.EMBEDDEDSET).setMax("2");
clazz.createProperty("embeddedMap", OType.EMBEDDEDMAP).setMax("2");
clazz.createProperty("linkList", OType.LINKLIST).setMax("2");
clazz.createProperty("linkSet", OType.LINKSET).setMax("2");
clazz.createProperty("linkMap", OType.LINKMAP).setMax("2");
ODocument d = new ODocument(clazz);
d.field("int", 11);
d.field("long", 11);
d.field("float", 11);
d.field("binary", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
d.field("byte", 11);
d.field("date", new Date());
d.field("datetime", new Date());
d.field("decimal", 10);
d.field("double", 10);
d.field("short", 10);
d.field("string", "yeah");
d.field("embeddedList", Arrays.asList("a", "b"));
d.field("embeddedSet", new HashSet<String>(Arrays.asList("a", "b")));
HashMap<String, String> cont = new HashMap<String, String>();
cont.put("one", "one");
cont.put("two", "one");
d.field("embeddedMap", cont);
d.field("linkList", Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 34)));
d.field("linkSet", new HashSet<ORecordId>(Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 31))));
HashMap<String, ORecordId> cont1 = new HashMap<String, ORecordId>();
cont1.put("one", new ORecordId(30, 30));
cont1.put("two", new ORecordId(30, 30));
d.field("linkMap", cont1);
d.validate();
checkField(d, "int", 12);
checkField(d, "long", 12);
checkField(d, "float", 20);
checkField(d, "binary", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 });
checkField(d, "byte", 20);
cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
checkField(d, "date", cal.getTime());
checkField(d, "datetime", cal.getTime());
checkField(d, "decimal", 20);
checkField(d, "double", 20);
checkField(d, "short", 20);
checkField(d, "string", "0123456789101112");
checkField(d, "embeddedList", Arrays.asList("a", "b", "d"));
checkField(d, "embeddedSet", new HashSet<String>(Arrays.asList("a", "b", "d")));
HashMap<String, String> con1 = new HashMap<String, String>();
con1.put("one", "one");
con1.put("two", "one");
con1.put("three", "one");
checkField(d, "embeddedMap", con1);
checkField(d, "linkList", Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 33), new ORecordId(40, 31)));
checkField(d, "linkSet", new HashSet<ORecordId>(Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 33), new ORecordId(40, 31))));
HashMap<String, ORecordId> cont3 = new HashMap<String, ORecordId>();
cont3.put("one", new ORecordId(30, 30));
cont3.put("two", new ORecordId(30, 30));
cont3.put("three", new ORecordId(30, 30));
checkField(d, "linkMap", cont3);
} finally {
db.drop();
}
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class ODocumentValidationTest method testValidationNotValidEmbeddedMap.
@Test
public void testValidationNotValidEmbeddedMap() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
db.create();
try {
OClass embeddedClazz = db.getMetadata().getSchema().createClass("EmbeddedValidation");
embeddedClazz.createProperty("int", OType.INTEGER).setMandatory(true);
embeddedClazz.createProperty("long", OType.LONG).setMandatory(true);
OClass clazz = db.getMetadata().getSchema().createClass("Validation");
clazz.createProperty("int", OType.INTEGER).setMandatory(true);
clazz.createProperty("long", OType.LONG).setMandatory(true);
clazz.createProperty("embeddedMap", OType.EMBEDDEDMAP, embeddedClazz).setMandatory(true);
ODocument d = new ODocument(clazz);
d.field("int", 30);
d.field("long", 30);
final Map<String, ODocument> embeddedMap = new HashMap<String, ODocument>();
d.field("embeddedMap", embeddedMap);
ODocument embeddedInMap = new ODocument("EmbeddedValidation");
embeddedInMap.field("int", 30);
embeddedInMap.field("long", 30);
embeddedMap.put("1", embeddedInMap);
ODocument embeddedInMap2 = new ODocument("EmbeddedValidation");
embeddedInMap2.field("int", 30);
embeddedMap.put("2", embeddedInMap2);
try {
d.validate();
Assert.fail("Validation doesn't throw exception");
} catch (OValidationException e) {
Assert.assertTrue(e.toString().contains("EmbeddedValidation.long"));
}
} finally {
db.drop();
}
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class ODocumentValidationTest method testRegExpValidation.
@Test
public void testRegExpValidation() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
db.create();
try {
OClass clazz = db.getMetadata().getSchema().createClass("Validation");
clazz.createProperty("string", OType.STRING).setRegexp("[^Z]*");
ODocument d = new ODocument(clazz);
d.field("string", "yeah");
d.validate();
checkField(d, "string", "yaZah");
} finally {
db.drop();
}
}
Aggregations