<commit_before>package org.deepdive.helpers

import org.deepdive.Logging
import org.deepdive.settings._
import org.deepdive.datastore.JdbcDataStore
import java.io._
import scala.sys.process._
import scala.util.{Try, Success, Failure}

/** Some helper functions that are used in many places 
 */
object Helpers extends Logging {
  
  // Constants
  val Psql = "psql"
  val Mysql = "mysql"
    
  val PsqlDriver = "org.postgresql.Driver"
  val MysqlDriver = "com.mysql.jdbc.Driver"
  
  /**
   * Get the dbtype from DbSettings
   * 
   * @returns 
   *     Helpers.Psql 
   *     or
   *     Helpsers.Mysql
   */
  def getDbType(dbSettings: DbSettings) : String = {
    // Branch by database driver type
    dbSettings.driver match {
      case PsqlDriver => Psql
      case MysqlDriver => Mysql
    }    
  }
  
  /**
   * Return a string of psql / mysql options, including user / password / host 
   * / port / database.
   * Do not support password for psql for now.
   * 
   * TODO: using passwords via command line is poor practice. postgres support 
   * specifying password through a PGPASSFILE:
   *   http://www.postgresql.org/docs/current/static/libpq-pgpass.html
   */
  def getOptionString(dbSettings: DbSettings) : String = {
    val dbtype = getDbType(dbSettings)
    val dbname = dbSettings.dbname
    val dbuser = dbSettings.user
    val dbport = dbSettings.port
    val dbhost = dbSettings.host
    val dbpassword = dbSettings.password
      // TODO do not use password for now
    val dbnameStr = dbname match {
      case null => ""
      case _ => dbtype match {
        case Psql => s" -d ${dbname} "
        case Mysql => s" ${dbname} " // can also use -D but mysqlimport does not support -D
      }
    }

    val dbuserStr = dbuser match {
      case null => ""
      case _ => dbtype match {
        case Psql => s" -U ${dbuser} "
        case Mysql => dbpassword match { // see if password is empty
          case null => s" -u ${dbuser} "
          case "" => s" -u ${dbuser} "
          case _ => s" -u ${dbuser} -p=${dbpassword}"
        }
      }
    }
    val dbportStr = dbport match {
      case null => ""
      case _ => dbtype match {
        case Psql => s" -p ${dbport} "
        case Mysql => s" -P ${dbport} "
      }
    }
    val dbhostStr = dbhost match {
      case null => ""
      case _ => s" -h ${dbhost} "
    }
    // Return a concatinated options string<commit_msg>Fix mysql command line password option<commit_after>package org.deepdive.helpers

import org.deepdive.Logging
import org.deepdive.settings._
import org.deepdive.datastore.JdbcDataStore
import java.io._
import scala.sys.process._
import scala.util.{Try, Success, Failure}

/** Some helper functions that are used in many places 
 */
object Helpers extends Logging {
  
  // Constants
  val Psql = "psql"
  val Mysql = "mysql"
    
  val PsqlDriver = "org.postgresql.Driver"
  val MysqlDriver = "com.mysql.jdbc.Driver"
  
  /**
   * Get the dbtype from DbSettings
   * 
   * @returns 
   *     Helpers.Psql 
   *     or
   *     Helpsers.Mysql
   */
  def getDbType(dbSettings: DbSettings) : String = {
    // Branch by database driver type
    dbSettings.driver match {
      case PsqlDriver => Psql
      case MysqlDriver => Mysql
    }    
  }
  
  /**
   * Return a string of psql / mysql options, including user / password / host 
   * / port / database.
   * Do not support password for psql for now.
   * 
   * TODO: using passwords via command line is poor practice. postgres support 
   * specifying password through a PGPASSFILE:
   *   http://www.postgresql.org/docs/current/static/libpq-pgpass.html
   */
  def getOptionString(dbSettings: DbSettings) : String = {
    val dbtype = getDbType(dbSettings)
    val dbname = dbSettings.dbname
    val dbuser = dbSettings.user
    val dbport = dbSettings.port
    val dbhost = dbSettings.host
    val dbpassword = dbSettings.password
      // TODO do not use password for now
    val dbnameStr = dbname match {
      case null => ""
      case _ => dbtype match {
        case Psql => s" -d ${dbname} "
        case Mysql => s" ${dbname} " // can also use -D but mysqlimport does not support -D
      }
    }

    val dbuserStr = dbuser match {
      case null => ""
      case _ => dbtype match {
        case Psql => s" -U ${dbuser} "
        case Mysql => dbpassword match { // see if password is empty
          case null => s" -u ${dbuser} "
          case "" => s" -u ${dbuser} "
          case _ => s" -u ${dbuser} -p${dbpassword}"
        }
      }
    }
    val dbportStr = dbport match {
      case null => ""
      case _ => dbtype match {
        case Psql => s" -p ${dbport} "
        case Mysql => s" -P ${dbport} "
      }
    }
    val dbhostStr = dbhost match {
      case null => ""
      case _ => s" -h ${dbhost} "
    }
    // Return a concatinated options string
{'fraction_non_alphanumeric': 0.10861500412201154, 'fraction_numerical': 0.0, 'mean_word_length': 2.1512987012987015, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5556999', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1537, 'n_words': 472}