Search in sources :

Example 41 with ODatabaseDocument

use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateIndex method execute.

/**
   * Execute the CREATE INDEX.
   */
@SuppressWarnings("rawtypes")
public Object execute(final Map<Object, Object> iArgs) {
    if (indexName == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocument database = getDatabase();
    final OIndex<?> idx;
    List<OCollate> collatesList = null;
    if (collates != null) {
        collatesList = new ArrayList<OCollate>();
        for (String collate : collates) {
            if (collate != null) {
                final OCollate col = OSQLEngine.getCollate(collate);
                collatesList.add(col);
            } else
                collatesList.add(null);
        }
    }
    if (fields == null || fields.length == 0) {
        OIndexFactory factory = OIndexes.getFactory(indexType.toString(), null);
        if (keyTypes != null)
            idx = database.getMetadata().getIndexManager().createIndex(indexName, indexType.toString(), new OSimpleKeyIndexDefinition(keyTypes, collatesList, factory.getLastVersion()), null, null, metadataDoc, engine);
        else if (serializerKeyId != 0) {
            idx = database.getMetadata().getIndexManager().createIndex(indexName, indexType.toString(), new ORuntimeKeyIndexDefinition(serializerKeyId, factory.getLastVersion()), null, null, metadataDoc, engine);
        } else {
            OLogManager.instance().warn(this, "Key type is not provided for '%s' index. Untyped indexes are deprecated and considered unstable." + " Please specify a key type.", indexName);
            idx = database.getMetadata().getIndexManager().createIndex(indexName, indexType.toString(), null, null, null, metadataDoc, engine);
        }
    } else {
        if ((keyTypes == null || keyTypes.length == 0) && collates == null) {
            idx = oClass.createIndex(indexName, indexType.toString(), null, metadataDoc, engine, fields);
        } else {
            final List<OType> fieldTypeList;
            if (keyTypes == null) {
                for (final String fieldName : fields) {
                    if (!fieldName.equals("@rid") && !oClass.existsProperty(fieldName))
                        throw new OIndexException("Index with name : '" + indexName + "' cannot be created on class : '" + oClass.getName() + "' because field: '" + fieldName + "' is absent in class definition.");
                }
                fieldTypeList = ((OClassImpl) oClass).extractFieldTypes(fields);
            } else
                fieldTypeList = Arrays.asList(keyTypes);
            final OIndexDefinition idxDef = OIndexDefinitionFactory.createIndexDefinition(oClass, Arrays.asList(fields), fieldTypeList, collatesList, indexType.toString(), null);
            idx = database.getMetadata().getIndexManager().createIndex(indexName, indexType.name(), idxDef, oClass.getPolymorphicClusterIds(), null, metadataDoc, engine);
        }
    }
    if (idx != null)
        return idx.getSize();
    return null;
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OCollate(com.orientechnologies.orient.core.collate.OCollate)

Example 42 with ODatabaseDocument

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);
    }
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OException(com.orientechnologies.common.exception.OException)

Example 43 with ODatabaseDocument

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();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

Example 44 with ODatabaseDocument

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();
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.testng.annotations.Test)

Example 45 with ODatabaseDocument

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();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

Aggregations

ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)187 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)81 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)68 Test (org.testng.annotations.Test)53 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)47 ORecordId (com.orientechnologies.orient.core.id.ORecordId)23 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)17 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)16 ORecord (com.orientechnologies.orient.core.record.ORecord)14 Test (org.junit.Test)14 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)12 ORID (com.orientechnologies.orient.core.id.ORID)11 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)10 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)9 OValidationException (com.orientechnologies.orient.core.exception.OValidationException)9 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)9 ArrayList (java.util.ArrayList)9 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)8 HashSet (java.util.HashSet)8