Search in sources :

Example 36 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class DefaultValueTest method testDefaultValueDate.

@Test
public void testDefaultValueDate() {
    OSchema schema = database.getMetadata().getSchema();
    OClass classA = schema.createClass("ClassA");
    OProperty prop = classA.createProperty("date", OType.DATE);
    prop.setDefaultValue(ODateHelper.getDateTimeFormatInstance().format(new Date()));
    OProperty some = classA.createProperty("id", OType.STRING);
    some.setDefaultValue("uuid()");
    System.out.println(prop.getDefaultValue());
    ODocument doc = new ODocument(classA);
    ODocument saved = database.save(doc);
    assertNotNull(saved.field("date"));
    assertTrue(saved.field("date") instanceof Date);
    assertNotNull(saved.field("id"));
    OIdentifiable id = database.command(new OCommandSQL("insert into ClassA content {}")).execute();
    ODocument seved1 = database.load(id.getIdentity());
    assertNotNull(seved1.field("date"));
    assertNotNull(seved1.field("id"));
    assertTrue(seved1.field("date") instanceof Date);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Date(java.util.Date) Test(org.junit.Test)

Example 37 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class DefaultValueTest method testKeepValueSerialization.

@Test
public void testKeepValueSerialization() {
    // create example schema
    OSchema schema = database.getMetadata().getSchema();
    OClass classA = schema.createClass("ClassC");
    OProperty prop = classA.createProperty("name", OType.STRING);
    prop.setDefaultValue("uuid()");
    ODocument doc = new ODocument("ClassC");
    byte[] val = doc.toStream();
    ODocument doc1 = new ODocument();
    doc1.fromStream(val);
    doc1.deserializeFields();
    assertEquals(doc.field("name"), doc1.field("name"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Test(org.junit.Test)

Example 38 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class OrientJdbcDatabaseMetaData method getColumns.

@Override
public ResultSet getColumns(final String catalog, final String schemaPattern, final String tableNamePattern, final String columnNamePattern) throws SQLException {
    database.activateOnCurrentThread();
    final List<ODocument> records = new ArrayList<ODocument>();
    OSchema schema = database.getMetadata().getSchema();
    for (OClass clazz : schema.getClasses()) {
        if (OrientJdbcUtils.like(clazz.getName(), tableNamePattern)) {
            for (OProperty prop : clazz.properties()) {
                if (columnNamePattern == null) {
                    records.add(getPropertyAsDocument(clazz, prop));
                } else {
                    if (OrientJdbcUtils.like(prop.getName(), columnNamePattern)) {
                        records.add(getPropertyAsDocument(clazz, prop));
                    }
                }
            }
        }
    }
    return new OrientJdbcResultSet(new OrientJdbcStatement(connection), records, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 39 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class CompositeIndexWithNullTest method testPointQueryInTx.

public void testPointQueryInTx() {
    final OSchema schema = database.getMetadata().getSchema();
    OClass clazz = schema.createClass("compositeIndexNullPointQueryInTxClass");
    clazz.createProperty("prop1", OType.INTEGER);
    clazz.createProperty("prop2", OType.INTEGER);
    clazz.createProperty("prop3", OType.INTEGER);
    final ODocument metadata = new ODocument();
    metadata.field("ignoreNullValues", false);
    clazz.createIndex("compositeIndexNullPointQueryInTxIndex", OClass.INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, new String[] { "prop1", "prop2", "prop3" });
    database.begin();
    for (int i = 0; i < 20; i++) {
        ODocument document = new ODocument("compositeIndexNullPointQueryInTxClass");
        document.field("prop1", i / 10);
        document.field("prop2", i / 5);
        if (i % 2 == 0)
            document.field("prop3", i);
        document.save();
    }
    database.commit();
    String query = "select from compositeIndexNullPointQueryInTxClass where prop1 = 1 and prop2 = 2";
    List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>(query));
    Assert.assertEquals(result.size(), 5);
    for (int k = 0; k < 5; k++) {
        ODocument document = result.get(k);
        Assert.assertEquals(document.field("prop1"), 1);
        Assert.assertEquals(document.field("prop2"), 2);
    }
    ODocument explain = database.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullPointQueryInTxIndex"));
    query = "select from compositeIndexNullPointQueryInTxClass where prop1 = 1 and prop2 = 2 and prop3 is null";
    result = database.query(new OSQLSynchQuery<ODocument>(query));
    Assert.assertEquals(result.size(), 2);
    for (ODocument document : result) Assert.assertNull(document.field("prop3"));
    explain = database.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullPointQueryInTxIndex"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) Set(java.util.Set) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 40 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class CompositeIndexWithNullTest method testRangeQueryInMiddleTx.

public void testRangeQueryInMiddleTx() {
    if (database.getURL().startsWith("remote:"))
        return;
    final OSchema schema = database.getMetadata().getSchema();
    OClass clazz = schema.createClass("compositeIndexNullRangeQueryInMiddleTxClass");
    clazz.createProperty("prop1", OType.INTEGER);
    clazz.createProperty("prop2", OType.INTEGER);
    clazz.createProperty("prop3", OType.INTEGER);
    final ODocument metadata = new ODocument();
    metadata.field("ignoreNullValues", false);
    clazz.createIndex("compositeIndexNullRangeQueryInMiddleTxIndex", OClass.INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, null, new String[] { "prop1", "prop2", "prop3" });
    database.begin();
    for (int i = 0; i < 20; i++) {
        ODocument document = new ODocument("compositeIndexNullRangeQueryInMiddleTxClass");
        document.field("prop1", i / 10);
        document.field("prop2", i / 5);
        if (i % 2 == 0)
            document.field("prop3", i);
        document.save();
    }
    String query = "select from compositeIndexNullRangeQueryInMiddleTxClass where prop1 = 1 and prop2 > 2";
    List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>(query));
    Assert.assertEquals(result.size(), 5);
    for (int k = 0; k < 5; k++) {
        ODocument document = result.get(k);
        Assert.assertEquals(document.field("prop1"), 1);
        Assert.assertTrue(document.<Integer>field("prop2") > 2);
    }
    ODocument explain = database.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullRangeQueryInMiddleTxIndex"));
    query = "select from compositeIndexNullRangeQueryInMiddleTxClass where prop1 > 0";
    result = database.query(new OSQLSynchQuery<ODocument>(query));
    Assert.assertEquals(result.size(), 10);
    for (int k = 0; k < 10; k++) {
        ODocument document = result.get(k);
        Assert.assertTrue(document.<Integer>field("prop1") > 0);
    }
    database.commit();
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) Set(java.util.Set) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)203 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)165 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)122 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)54 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)46 Test (org.testng.annotations.Test)35 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)31 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)23 Test (org.junit.Test)19 Before (org.junit.Before)16 ORID (com.orientechnologies.orient.core.id.ORID)15 OIndex (com.orientechnologies.orient.core.index.OIndex)15 Collection (java.util.Collection)15 Set (java.util.Set)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)9 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 BeforeClass (org.testng.annotations.BeforeClass)9 OStorage (com.orientechnologies.orient.core.storage.OStorage)6 ArrayList (java.util.ArrayList)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)4