Search in sources :

Example 81 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class PreparedStatementTest method testFunction.

@Test
public void testFunction() {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("one", 1);
    params.put("three", 3);
    Iterable<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select max(:one, :three) as maximo")).execute(params);
    boolean found = false;
    for (ODocument doc : result) {
        found = true;
        Assert.assertEquals(doc.field("maximo"), 3);
    }
    Assert.assertTrue(found);
}
Also used : HashMap(java.util.HashMap) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 82 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class PreparedStatementTest method testNamedParamTarget.

@Test
public void testNamedParamTarget() {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("className", "PreparedStatementTest1");
    Iterable<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select from :className")).execute(params);
    Set<String> expected = new HashSet<String>();
    expected.add("foo1");
    expected.add("foo2");
    boolean found = false;
    for (ODocument doc : result) {
        found = true;
        Assert.assertTrue(expected.contains(doc.field("name")));
    }
    Assert.assertTrue(found);
}
Also used : HashMap(java.util.HashMap) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 83 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class PreparedStatementTest method testNamedParamTargetDocument.

@Test
public void testNamedParamTargetDocument() {
    Iterable<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select from PreparedStatementTest1 limit 1")).execute();
    ODocument record = result.iterator().next();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("inputRid", record);
    result = database.command(new OSQLSynchQuery<ODocument>("select from :inputRid")).execute(params);
    boolean found = false;
    for (ODocument doc : result) {
        found = true;
        Assert.assertEquals(doc.getIdentity(), record.getIdentity());
        Assert.assertEquals(doc.field("name"), record.field("name"));
    }
    Assert.assertTrue(found);
}
Also used : HashMap(java.util.HashMap) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 84 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class OSQLEngine method parseRIDTarget.

public Set<OIdentifiable> parseRIDTarget(final ODatabaseDocument database, String iTarget, final OCommandContext iContext, Map<Object, Object> iArgs) {
    final Set<OIdentifiable> ids;
    if (iTarget.startsWith("(")) {
        // SUB-QUERY
        final OSQLSynchQuery<Object> query = new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1));
        query.setContext(iContext);
        final List<OIdentifiable> result = database.query(query, iArgs);
        if (result == null || result.isEmpty())
            ids = Collections.emptySet();
        else {
            ids = new HashSet<OIdentifiable>((int) (result.size() * 1.3));
            for (OIdentifiable aResult : result) ids.add(aResult.getIdentity());
        }
    } else if (iTarget.startsWith("[")) {
        // COLLECTION OF RIDS
        final String[] idsAsStrings = iTarget.substring(1, iTarget.length() - 1).split(",");
        ids = new HashSet<OIdentifiable>((int) (idsAsStrings.length * 1.3));
        for (String idsAsString : idsAsStrings) {
            if (idsAsString.startsWith("$")) {
                Object r = iContext.getVariable(idsAsString);
                if (r instanceof OIdentifiable)
                    ids.add((OIdentifiable) r);
                else
                    OMultiValue.add(ids, r);
            } else
                ids.add(new ORecordId(idsAsString));
        }
    } else {
        // SINGLE RID
        if (iTarget.startsWith("$")) {
            Object r = iContext.getVariable(iTarget);
            if (r instanceof OIdentifiable)
                ids = Collections.<OIdentifiable>singleton((OIdentifiable) r);
            else
                ids = (Set<OIdentifiable>) OMultiValue.add(new HashSet<OIdentifiable>(OMultiValue.getSize(r)), r);
        } else
            ids = Collections.<OIdentifiable>singleton(new ORecordId(iTarget));
    }
    return ids;
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 85 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class OStreamSerializerAnyStreamable method fromStream.

/**
   * Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
   */
public Object fromStream(final byte[] iStream) throws IOException {
    if (iStream == null || iStream.length == 0)
        // NULL VALUE
        return null;
    final int classNameSize = OBinaryProtocol.bytes2int(iStream);
    if (classNameSize <= 0) {
        final String message = "Class signature not found in ANY element: " + Arrays.toString(iStream);
        OLogManager.instance().error(this, message);
        throw new OSerializationException(message);
    }
    final String className = new String(iStream, 4, classNameSize, "UTF-8");
    try {
        final OSerializableStream stream;
        // CHECK FOR ALIASES
        if (className.equalsIgnoreCase("q"))
            // QUERY
            stream = new OSQLSynchQuery<Object>();
        else if (className.equalsIgnoreCase("c"))
            // SQL COMMAND
            stream = new OCommandSQL();
        else if (className.equalsIgnoreCase("s"))
            // SCRIPT COMMAND
            stream = new OCommandScript();
        else
            // CREATE THE OBJECT BY INVOKING THE EMPTY CONSTRUCTOR
            stream = (OSerializableStream) Class.forName(className).newInstance();
        return stream.fromStream(OArrays.copyOfRange(iStream, 4 + classNameSize, iStream.length));
    } catch (Exception e) {
        final String message = "Error on unmarshalling content. Class: " + className;
        OLogManager.instance().error(this, message, e);
        throw OException.wrapException(new OSerializationException(message), e);
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Aggregations

OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)506 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)426 Test (org.testng.annotations.Test)282 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)78 Test (org.junit.Test)60 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)57 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)47 ORID (com.orientechnologies.orient.core.id.ORID)34 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)31 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)22 List (java.util.List)21 HashMap (java.util.HashMap)20 ORecordId (com.orientechnologies.orient.core.id.ORecordId)19 Profile (com.orientechnologies.orient.test.domain.whiz.Profile)19 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)16 Collection (java.util.Collection)15 OrientTest (com.orientechnologies.orient.test.database.base.OrientTest)13 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)13 Set (java.util.Set)12 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)11