Search in sources :

Example 86 with ODatabaseDocument

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

the class OFieldTransformer method executeTransform.

@Override
public Object executeTransform(final Object input) {
    if (input instanceof OIdentifiable) {
        final ORecord rec = ((OIdentifiable) input).getRecord();
        if (rec instanceof ODocument) {
            final ODocument doc = (ODocument) rec;
            if (setOperation) {
                final Object newValue;
                if (expression != null) {
                    if (sqlFilter == null)
                        // ONLY THE FIRST TIME
                        sqlFilter = new OSQLFilter(expression, context, null);
                    newValue = sqlFilter.evaluate(doc, null, context);
                } else
                    newValue = value;
                // SET THE TRANSFORMED FIELD BACK
                doc.field(fieldName, newValue);
                log(OETLProcessor.LOG_LEVELS.DEBUG, "set %s=%s in document=%s", fieldName, newValue, doc);
            } else {
                if (fieldName != null) {
                    final Object prev = doc.removeField(fieldName);
                    log(OETLProcessor.LOG_LEVELS.DEBUG, "removed %s (value=%s) from document=%s", fieldName, prev, doc);
                } else {
                    for (String f : fieldNames) {
                        final Object prev = doc.removeField(f);
                        log(OETLProcessor.LOG_LEVELS.DEBUG, "removed %s (value=%s) from document=%s", f, prev, doc);
                    }
                }
            }
            if (save) {
                log(OETLProcessor.LOG_LEVELS.DEBUG, "saving record %s", doc);
                final ODatabaseDocument db = super.pipeline.getDocumentDatabase();
                db.save(doc);
            }
        }
    }
    return input;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) OSQLFilter(com.orientechnologies.orient.core.sql.filter.OSQLFilter) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 87 with ODatabaseDocument

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

the class OOrientDBLoaderTest method shouldSaveDocumentsWithPredefinedSchema.

@Test
public void shouldSaveDocumentsWithPredefinedSchema() {
    //create class
    ODatabaseDocument db = new ODatabaseDocumentTx(graph.getRawGraph().getURL()).open("admin", "admin");
    db.command(new OCommandSQL("CREATE Class Person")).execute();
    db.command(new OCommandSQL("CREATE property Person.name STRING")).execute();
    db.command(new OCommandSQL("CREATE property Person.surname STRING")).execute();
    db.command(new OCommandSQL("CREATE property Person.married BOOLEAN")).execute();
    db.command(new OCommandSQL("CREATE property Person.birthday DATETIME")).execute();
    db.close();
    //store data
    process("{source: { content: { value: 'name,surname,married,birthday\nJay,Miner,false,1970-01-01 05:30:00' } }, " + "extractor : { csv: {columns:['name:string','surname:string','married:boolean','birthday:datetime'], dateFormat :'yyyy-MM-dd HH:mm:ss'} }, loader: { orientdb: {\n" + "      dbURL: \"memory:OETLBaseTest\", class:'Person',     dbUser: \"admin\",\n" + "      dbPassword: \"admin\",\n" + "      dbAutoCreate: false,\n      tx: false,\n" + "      batchCommit: 1000,\n" + "      wal : false,\n" + "      dbType: \"document\" } } }");
    graph.makeActive();
    db = graph.getRawGraph();
    List<?> res = db.query(new OSQLSynchQuery<ODocument>("SELECT FROM Person"));
    assertThat(res.size()).isEqualTo(1);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test) OETLBaseTest(com.orientechnologies.orient.etl.OETLBaseTest)

Example 88 with ODatabaseDocument

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

the class OCommandExecutorSQLCreateEdge method parse.

@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 tempLower = parseOptionalWord(false);
        String temp = tempLower == null ? null : tempLower.toUpperCase();
        while (temp != null) {
            if (temp.equals("CLUSTER")) {
                clusterName = parserRequiredWord(false);
            } else if (temp.equals(KEYWORD_FROM)) {
                from = parserRequiredWord(false, "Syntax error", " =><,\r\n", true);
            } else if (temp.equals("TO")) {
                to = parserRequiredWord(false, "Syntax error", " =><,\r\n", true);
            } else if (temp.equals(KEYWORD_SET)) {
                fields = new ArrayList<OPair<String, Object>>();
                parseSetFields(clazz, fields);
            } else if (temp.equals(KEYWORD_CONTENT)) {
                parseContent();
            } else if (temp.equals(KEYWORD_BATCH)) {
                temp = parserNextWord(true);
                if (temp != null)
                    batch = Integer.parseInt(temp);
            } else if (className == null && temp.length() > 0) {
                className = tempLower;
                OrientBaseGraph graph = OrientBaseGraph.getActiveGraph();
                if (graph != null && graph.isUseClassForEdgeLabel()) {
                    clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(temp);
                } else {
                    clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass("E");
                }
            }
            temp = parseOptionalWord(true);
            if (parserIsEnded())
                break;
        }
        if (className == null) {
            // ASSIGN DEFAULT CLASS
            className = OrientEdgeType.CLASS_NAME;
            clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(className);
        }
        // GET/CHECK CLASS NAME
        if (clazz == null)
            throw new OCommandSQLParsingException("Class '" + className + "' was not found");
        edgeLabel = className;
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 89 with ODatabaseDocument

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

the class OCommandExecutorSQLCreateVertex method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLCreateVertex parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        queryText = preParse(queryText, iRequest);
        textRequest.setText(queryText);
        final ODatabaseDocument database = getDatabase();
        init((OCommandRequestText) iRequest);
        String className = null;
        parserRequiredKeyword("CREATE");
        parserRequiredKeyword("VERTEX");
        String temp = parseOptionalWord(true);
        while (temp != null) {
            if (temp.equals("CLUSTER")) {
                clusterName = parserRequiredWord(false);
            } else if (temp.equals(KEYWORD_SET)) {
                fields = new ArrayList<OPair<String, Object>>();
                parseSetFields(clazz, fields);
            } else if (temp.equals(KEYWORD_CONTENT)) {
                parseContent();
            } else if (className == null && temp.length() > 0) {
                className = temp;
                if (className == null)
                    // ASSIGN DEFAULT CLASS
                    className = OrientVertexType.CLASS_NAME;
                // GET/CHECK CLASS NAME
                clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(className);
                if (clazz == null)
                    throw new OCommandSQLParsingException("Class '" + className + "' was not found");
            }
            temp = parserOptionalWord(true);
            if (parserIsEnded())
                break;
        }
        if (className == null) {
            // ASSIGN DEFAULT CLASS
            className = OrientVertexType.CLASS_NAME;
            // GET/CHECK CLASS NAME
            clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(className);
            if (clazz == null)
                throw new OCommandSQLParsingException("Class '" + className + "' was not found");
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandSQLParsingException(com.orientechnologies.orient.core.sql.OCommandSQLParsingException) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal)

Example 90 with ODatabaseDocument

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

the class OGraphCommandExecutorSQLFactory method getAnyGraph.

/**
   * @return any graph if available, otherwise a Non Transactional OrientGraph implementation from the current database in thread
   *         local.
   */
public static OrientBaseGraph getAnyGraph(final OModifiableBoolean shouldBeShutDown) {
    final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OrientBaseGraph result = OrientBaseGraph.getActiveGraph();
    if (result != null) {
        final ODatabaseDocument graphDb = result.getRawGraph();
        // CHECK IF THE DATABASE + USER IN TL IS THE SAME IN ORDER TO USE IT
        if (canReuseActiveGraph(graphDb, database)) {
            if (!graphDb.isClosed()) {
                graphDb.activateOnCurrentThread();
                shouldBeShutDown.setValue(false);
                return result;
            }
        }
    }
    // Set it again on ThreadLocal because the getRawGraph() may have set a closed db in the thread-local
    shouldBeShutDown.setValue(true);
    ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) database);
    return (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) database);
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

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