<commit_before>package org.neo4j.graphql

import graphql.language.*
import org.neo4j.graphdb.GraphDatabaseService

abstract class CypherGenerator {
    companion object {
        val DEFAULT_CYPHER_VERSION = "3.1"

        fun instance(db: GraphDatabaseService) : CypherGenerator {
            try {
                val first = db.execute("CYPHER 3.1 RETURN true as version").columnAs<Boolean>("version").next()
                if (first) return Cypher31Generator()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return Cypher30Generator()
        }
    }

    abstract fun compiled() : String

    fun generateQueryForField(field: Field): String {
        val name = field.name
        val variable = name
        val md = metaData(name)
        val orderBys = mutableListOf<String>()
        return "MATCH (`$variable`:`$name`) \n" +
                where(field, variable, md, orderBys) +
                optionalMatches(md, variable, field.selectionSet.selections, orderBys) +
                // TODO order within each
                orderBy(orderBys) +
                "\nRETURN " + projection(field, variable, md)

    }

    protected fun orderBy(orderBys:List<String>) = if (orderBys.isEmpty()) "" else orderBys.joinToString(",", "\nWITH * ORDER BY ")

    open protected fun optionalMatches(metaData: MetaData, variable: String, selections: Iterable<Selection>, orderBys: MutableList<String>) =
            selections.map {
                when (it) {<commit_msg>Use kernel.Version to determine Neo4j version<commit_after>package org.neo4j.graphql

import graphql.language.*
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.kernel.internal.Version

abstract class CypherGenerator {
    companion object {
        val VERSION = Version.getNeo4jVersion()
        val DEFAULT_CYPHER_VERSION = "3.1"

        fun instance(db: GraphDatabaseService) : CypherGenerator {
            try {
                // val first = db.execute("CYPHER 3.1 RETURN true as version").columnAs<Boolean>("version").next()
                if (VERSION.startsWith("3.1")) return Cypher31Generator()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return Cypher30Generator()
        }
    }

    abstract fun compiled() : String

    fun generateQueryForField(field: Field): String {
        val name = field.name
        val variable = name
        val md = metaData(name)
        val orderBys = mutableListOf<String>()
        return "MATCH (`$variable`:`$name`) \n" +
                where(field, variable, md, orderBys) +
                optionalMatches(md, variable, field.selectionSet.selections, orderBys) +
                // TODO order within each
                orderBy(orderBys) +
                "\nRETURN " + projection(field, variable, md)

    }

    protected fun orderBy(orderBys:List<String>) = if (orderBys.isEmpty()) "" else orderBys.joinToString(",", "\nWITH * ORDER BY ")

    open protected fun optionalMatches(metaData: MetaData, variable: String, selections: Iterable<Selection>, orderBys: MutableList<String>) =
            selections.map {
                when (it) {
{'fraction_non_alphanumeric': 0.1055764411027569, 'fraction_numerical': 0.007832080200501253, 'mean_word_length': 2.343455497382199, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5559486', 'n_tokens_mistral': 909, 'n_tokens_neox': 863, 'n_words': 214}