<commit_before>package org.leberrigaud.maven.plugins.database

import groovy.sql.Sql
import org.codehaus.gmaven.mojo.GroovyMojo
import org.apache.maven.plugin.MojoFailureException

/**
 *
 */
abstract class AbstractDatabaseMojo extends GroovyMojo
{
    static final DB = [
            'mysql': new MySql(),
            'postgres': new Postgres(),
            'oracle': new Oracle()
    ]

    /**
     * The type of database to connect to
     * @parameter default-value="mysql"
     */
    String database

    /**
     * The host the database server lives on
     * @parameter expression="${db.host}" default-value="localhost"
     */
    String host;

    /**
     * the port on which to access the database via JDBC.
     * It has no default value set here, if unassigned it will default to the default port for each DB.
     * @parameter expression="${db.port}"
     */
    String port;

    /**
     * The name of the database to create
     * @parameter
     */
    String name

    /**
     * The username of the user to associate to the database
     * @parameter
     */
    String username

    /**
     * The password of the user to associate to the database
     * @parameter
     */
    String password

    /**
     * @parameter
     */
    String rootUsername

    /**
     * @parameter
     */
    String rootPassword

    /**
     * @parameter expression="${db.skip}" default-value="false"
     */
    boolean skip

    void drop()
    {
        final def db = db()
        final Sql sql = newSql(db)

        executeSql(sql, db.dropDb(name), true)
        executeSql(sql, db.dropUser(username), true)
    }

    void create()
    {
        final def db = db()
        final Sql sql = newSql(db)
        executeSql(sql, db.createUser(username, password))
        executeSql(sql, db.createDb(name))
        executeSql(sql, db.grantPrivileges(name, username))
    }

    def db()
    {
        def db = DB[database]
        if (!db)
        {
            throw new MojoFailureException("Could not find database '$database'")
        }
        return db
    }

    Sql newSql(def db)
    {
        final Properties props = new Properties();
        props['user'] = rootUsername
        props['password'] = <PASSWORD> ? <PASSWORD> : ""
        props['internal_logon'] = 'sysdba' // for Oracle
        return Sql.newInstance(db.url(host, port), props, db.driver)
    }

    private void executeSql(Sql runner, String sql)
    {
        if (sql) executeSql runner, sql, false
    }

    private void executeSql(Sql runner, String sql, boolean ignoreException)
    {
        log.info sql
        try
        {
            runner.execute(sql)
        }
        catch (Exception e)
        {
            if (!ignoreException) throw e
            else log.info("Error running '$sql': $e.message")<commit_msg>Adding some useful logging when debugging database access.<commit_after>package org.leberrigaud.maven.plugins.database

import groovy.sql.Sql
import org.codehaus.gmaven.mojo.GroovyMojo
import org.apache.maven.plugin.MojoFailureException

/**
 *
 */
abstract class AbstractDatabaseMojo extends GroovyMojo
{
    static final DB = [
            'mysql': new MySql(),
            'postgres': new Postgres(),
            'oracle': new Oracle()
    ]

    /**
     * The type of database to connect to
     * @parameter default-value="mysql"
     */
    String database

    /**
     * The host the database server lives on
     * @parameter expression="${db.host}" default-value="localhost"
     */
    String host;

    /**
     * the port on which to access the database via JDBC.
     * It has no default value set here, if unassigned it will default to the default port for each DB.
     * @parameter expression="${db.port}"
     */
    String port;

    /**
     * The name of the database to create
     * @parameter
     */
    String name

    /**
     * The username of the user to associate to the database
     * @parameter
     */
    String username

    /**
     * The password of the user to associate to the database
     * @parameter
     */
    String password

    /**
     * @parameter
     */
    String rootUsername

    /**
     * @parameter
     */
    String rootPassword

    /**
     * @parameter expression="${db.skip}" default-value="false"
     */
    boolean skip

    /**
     * Whether or not to show passwords when running the plugin in DEBUG mode.
     * @parameter expression="${db.showPasswords}" default-value="false"
     */
    boolean showPasswords

    void drop()
    {
        final def db = db()
        final Sql sql = newSql(db)

        executeSql(sql, db.dropDb(name), true)
        executeSql(sql, db.dropUser(username), true)
    }

    void create()
    {
        final def db = db()
        final Sql sql = newSql(db)
        executeSql(sql, db.createUser(username, password))
        executeSql(sql, db.createDb(name))
        executeSql(sql, db.grantPrivileges(name, username))
    }

    def db()
    {
        def db = DB[database]
        if (!db)
        {
            throw new MojoFailureException("Could not find database '$database'")
        }
        return db
    }

    Sql newSql(def db)
    {
        final Properties props = new Properties();
        props['user'] = rootUsername
        props['password'] = <PASSWORD> ? <PASSWORD> : ""
        props['internal_logon'] = 'sysdba' // for Oracle

        final url = db.url(host, port)

        if (log.debugEnabled)
        {
            log.debug "Accessing database at '$url' with username '$rootUsername' ${showPasswords ? "and password $rootPassword":''}"
        }
        return Sql.newInstance(url, props, db.driver)
    }

    private void executeSql(Sql runner, String sql)
    {
        if (sql) executeSql runner, sql, false
    }

    private void executeSql(Sql runner, String sql, boolean ignoreException)
    {
        log.info sql
        try
        {
            runner.execute(sql)
        }
        catch (Exception e)
        {
            if (!ignoreException) throw e
            else log.info("Error running '$sql': $e.message")
{'fraction_non_alphanumeric': 0.10851693521867807, 'fraction_numerical': 0.0, 'mean_word_length': 2.0098960910440375, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 7, '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': '3232120', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1684, 'n_words': 513}