<commit_before>package io.ehdev.testify.dbbuilder

import groovy.sql.Sql
import groovy.util.logging.Slf4j

@Slf4j
/**
 * This class is thread safe, if and only if the test creates a new object every time.
 */
class DBTestCase {

    final Sql connection;
    def testCaseKeyResults
    def currentTestName

    DBTestCase(Sql connection){
        this.connection = connection
        this.testCaseKeyResults = new TestCaseContainer()
    }

    def make(name, closure) {
        if(!name.trim()) {
            log.debug("Setting up test case named $name")
        }
        closure.delegate = this
        closure()
        currentTestName = name
    }

    def make(closure) {
        make("DEFAULT_TEST", closure)
    }

    def methodMissing(String tableName, args){
        Integer rowResult = insertIntoDatabase(tableName, args)
        testCaseKeyResults.addNewPrimaryKey(currentTestName, tableName, rowResult)
        //updatePrimaryKeyList(tableName, rowResult)
        return rowResult
    }

    public Object insertIntoDatabase(String name, args) {
        Map fields = getFieldMap(args)
        String insertStatement = createInsertString(fields, name)
        log.debug("Writing to table $name with parameters {}", fields)
        def rowResult = executeTransactionToDatabase(fields, insertStatement)
        rowResult
    }

    public Object executeTransactionToDatabase(Map fields, String insertStatement) {
        try {
            def insertValue = connection.executeInsert(fields, insertStatement)
            if(null == insertValue[0] || null == insertValue[0][0]){
                return null
            } else {
                return insertValue[0][0]
            }
        } catch (ex) {
            throw new InvalidDatabaseOperationException(ex)
        }
    }

    private GString createInsertString(fields, String name) {
        String values, params

        (values, params) = getValueString(fields)

        "insert into $name ($values) values ($params)";
    }

    private static def getFieldMap(args) {
        args[0]<commit_msg>Pulling out a method to remove multiple returns<commit_after>package io.ehdev.testify.dbbuilder

import groovy.sql.Sql
import groovy.util.logging.Slf4j

@Slf4j
/**
 * This class is thread safe, if and only if the test creates a new object every time.
 */
class DBTestCase {

    final Sql connection;
    def testCaseKeyResults
    def currentTestName

    DBTestCase(Sql connection){
        this.connection = connection
        this.testCaseKeyResults = new TestCaseContainer()
    }

    def make(name, closure) {
        if(!name.trim()) {
            log.debug("Setting up test case named $name")
        }
        closure.delegate = this
        closure()
        currentTestName = name
    }

    def make(closure) {
        make("DEFAULT_TEST", closure)
    }

    def methodMissing(String tableName, args){
        Integer rowResult = insertIntoDatabase(tableName, args)
        testCaseKeyResults.addNewPrimaryKey(currentTestName, tableName, rowResult)
        //updatePrimaryKeyList(tableName, rowResult)
        return rowResult
    }

    private Object insertIntoDatabase(String name, args) {
        Map fields = getFieldMap(args)
        String insertStatement = createInsertString(fields, name)
        log.debug("Writing to table $name with parameters {}", fields)
        def rowResult = executeTransactionToDatabase(fields, insertStatement)
        rowResult
    }

    private Integer executeTransactionToDatabase(Map fields, String insertStatement) {
        try {
            return doInsertReturnPrimaryKey(fields, insertStatement)
        } catch (ex) {
            throw new InvalidDatabaseOperationException(ex)
        }
    }

    private Object doInsertReturnPrimaryKey(Map fields, String insertStatement) {
        def insertValue = connection.executeInsert(fields, insertStatement)
        if (null == insertValue[0] || null == insertValue[0][0]) {
            return null
        } else {
            return insertValue[0][0]
        }
    }

    private GString createInsertString(fields, String name) {
        String values, params

        (values, params) = getValueString(fields)

        "insert into $name ($values) values ($params)";
    }

    private static def getFieldMap(args) {
        args[0]
{'fraction_non_alphanumeric': 0.08354606637270828, 'fraction_numerical': 0.0037131585054537017, 'mean_word_length': 2.609715242881072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7485838', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1114, 'n_words': 306}