001/* 002 * Copyright (c) 2007-2022 The Cascading Authors. All Rights Reserved. 003 * 004 * Project and contact information: https://cascading.wensel.net/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021package cascading.local.tap.neo4j; 022 023import java.util.Map; 024 025import cascading.util.LogUtil; 026import iot.jcypher.database.util.QParamsUtil; 027import iot.jcypher.query.JcQuery; 028import iot.jcypher.query.writer.CypherWriter; 029import iot.jcypher.query.writer.Format; 030import iot.jcypher.query.writer.QueryParam; 031import iot.jcypher.query.writer.WriterContext; 032import iot.jcypher.util.Util; 033import org.neo4j.driver.v1.StatementResult; 034import org.neo4j.driver.v1.Transaction; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038/** 039 * Base class for Neo4j query generation. 040 */ 041public abstract class Neo4jStatement<T> 042 { 043 private static final Logger LOG = LoggerFactory.getLogger( Neo4jStatement.class ); 044 045 /** 046 * Method enableDebugLogging enables logging of the generated cypher query to the DEBUG log. 047 */ 048 public static void enableDebugLogging() 049 { 050 LogUtil.setLog4jLevel( Neo4jStatement.class.getName(), "debug" ); 051 } 052 053 public abstract JcQuery getStatement( T node ); 054 055 public StatementResult runStatement( Transaction tx, T node ) 056 { 057 JcQuery query = getStatement( node ); 058 059 if( LOG.isDebugEnabled() ) 060 LOG.debug( "cypher: {}", Util.toCypher( query, Format.NONE ) ); 061 062 WriterContext context = new WriterContext(); 063 QueryParam.setExtractParams( query.isExtractParams(), context ); 064 CypherWriter.toCypherExpression( query, context ); 065 066 String cypher = context.buffer.toString(); 067 Map<String, Object> paramsMap = QParamsUtil.createQueryParams( context ); 068 069 return tx.run( cypher, paramsMap ); 070 } 071 }