<commit_before>package org.intermine.task;

/*
 * Copyright (C) 2002-2004 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Project;

import org.apache.torque.task.TorqueSQLExec;
import org.apache.torque.task.TorqueSQLTask;

import java.io.File;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileWriter;
import java.io.IOException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

import org.intermine.sql.Database;
import org.intermine.sql.DatabaseFactory;

/**
 * Generates and inserts SQL given database name, schema and temporary directory
 *
 * @author <NAME>
 */
public class BuildDbTask extends Task
{
    protected static final String SEQUENCE_NAME = "serial";
    protected File tempDir;
    protected Database database;
    protected String schemaFile;

    /**
     * Sets the database
     * @param database String used to identify Database (usually dataSourceName)
     */
    public void setDatabase(String database) {
        try {
            this.database = DatabaseFactory.getDatabase(database);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Sets the directory for temporary files including sql output
     * @param tempDir the directory location
     */
    public void setTempdir(File tempDir) {
        this.tempDir = tempDir;
    }

    /**
     * Adds the schemafile to be processed.
     * @param schemafile to be processed
     */
    public void setSchemafile(String schemafile) {
        this.schemaFile = schemafile;
    }

    /**
     * @see Task#execute
     * @throws BuildException
     */
    public void execute() throws BuildException {
        if (tempDir == null) {
            throw new BuildException("tempDir attribute is not set");
        }
        if (database == null) {
            throw new BuildException("database attribute is not set or database is not present");
        }
        if (schemaFile == null) {
            throw new BuildException("schemaFile attribute is not set");
        }
       
        SQL sql = new SQL();
        sql.setControlTemplate("sql/base/Control.vm");
        sql.setOutputDirectory(tempDir);
        sql.setUseClasspath(true);
        //sql.setBasePathToDbProps("sql/base/");
        sql.setSqlDbMap(tempDir + "/sqldb.map");
        sql.setOutputFile("report.sql.generation");
        sql.setTargetDatabase(database.getPlatform().toLowerCase()); // "postgresql"
        InputStream schemaFileInputStream =
            getClass().getClassLoader().getResourceAsStream(schemaFile);
        
        File tempFile;

        try {
            tempFile = File.createTempFile("schema", "xml", tempDir);

            PrintWriter writer = new PrintWriter(new FileWriter(tempFile));
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(schemaFileInputStream));
            
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                } else {
                    writer.println(line);
                }
            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new BuildException("cannot create temporary file for BuildDbTask: " +
                                     e.getMessage());
        }

        sql.setXmlFile(tempFile.getPath());

        sql.execute();

        InsertSQL isql = new InsertSQL();
        isql.setDriver(database.getDriver()); // "org.postgresql.Driver"
        isql.setUrl(database.getURL()); // "jdbc:postgresql://localhost/test"
        isql.setUserid(database.getUser()); // "mark"
        isql.setPassword(database.getPassword()); // ""
        isql.setAutocommit(true);
        TorqueSQLExec.OnError ea = new TorqueSQLExec.OnError();
        ea.setValue("continue"); // "abort", "continue" or "stop"
        isql.setOnerror(ea);
        isql.setSqlDbMap(tempDir + "/sqldb.map");
        isql.setSrcDir(tempDir.toString());
        isql.execute();

        try {<commit_msg>Fix checkstyle<commit_after>package org.intermine.task;

/*
 * Copyright (C) 2002-2004 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Project;

import org.apache.torque.task.TorqueSQLExec;
import org.apache.torque.task.TorqueSQLTask;

import java.io.File;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileWriter;
import java.io.IOException;

import java.sql.Connection;
import java.sql.SQLException;

import org.intermine.sql.Database;
import org.intermine.sql.DatabaseFactory;

/**
 * Generates and inserts SQL given database name, schema and temporary directory
 *
 * @author <NAME>
 */
public class BuildDbTask extends Task
{
    protected static final String SEQUENCE_NAME = "serial";
    protected File tempDir;
    protected Database database;
    protected String schemaFile;

    /**
     * Sets the database
     * @param database String used to identify Database (usually dataSourceName)
     */
    public void setDatabase(String database) {
        try {
            this.database = DatabaseFactory.getDatabase(database);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Sets the directory for temporary files including sql output
     * @param tempDir the directory location
     */
    public void setTempdir(File tempDir) {
        this.tempDir = tempDir;
    }

    /**
     * Adds the schemafile to be processed.
     * @param schemafile to be processed
     */
    public void setSchemafile(String schemafile) {
        this.schemaFile = schemafile;
    }

    /**
     * @see Task#execute
     * @throws BuildException
     */
    public void execute() throws BuildException {
        if (tempDir == null) {
            throw new BuildException("tempDir attribute is not set");
        }
        if (database == null) {
            throw new BuildException("database attribute is not set or database is not present");
        }
        if (schemaFile == null) {
            throw new BuildException("schemaFile attribute is not set");
        }
       
        SQL sql = new SQL();
        sql.setControlTemplate("sql/base/Control.vm");
        sql.setOutputDirectory(tempDir);
        sql.setUseClasspath(true);
        //sql.setBasePathToDbProps("sql/base/");
        sql.setSqlDbMap(tempDir + "/sqldb.map");
        sql.setOutputFile("report.sql.generation");
        sql.setTargetDatabase(database.getPlatform().toLowerCase()); // "postgresql"
        InputStream schemaFileInputStream =
            getClass().getClassLoader().getResourceAsStream(schemaFile);
        
        File tempFile;

        try {
            tempFile = File.createTempFile("schema", "xml", tempDir);

            PrintWriter writer = new PrintWriter(new FileWriter(tempFile));
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(schemaFileInputStream));
            
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                } else {
                    writer.println(line);
                }
            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new BuildException("cannot create temporary file for BuildDbTask: "
                                     + e.getMessage());
        }

        sql.setXmlFile(tempFile.getPath());

        sql.execute();

        InsertSQL isql = new InsertSQL();
        isql.setDriver(database.getDriver()); // "org.postgresql.Driver"
        isql.setUrl(database.getURL()); // "jdbc:postgresql://localhost/test"
        isql.setUserid(database.getUser()); // "mark"
        isql.setPassword(database.getPassword()); // ""
        isql.setAutocommit(true);
        TorqueSQLExec.OnError ea = new TorqueSQLExec.OnError();
        ea.setValue("continue"); // "abort", "continue" or "stop"
        isql.setOnerror(ea);
        isql.setSqlDbMap(tempDir + "/sqldb.map");
        isql.setSrcDir(tempDir.toString());
        isql.execute();

        try {
{'fraction_non_alphanumeric': 0.1072219760744351, 'fraction_numerical': 0.0017722640673460345, 'mean_word_length': 2.339127218934911, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5559279', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2420, 'n_words': 601}