/**
  * Constructor
  *
  * @param string $host The host of the server
  * @param string $username The connection username
  * @param string $password The connection password
  * @param string $database The database to connect to by default
  * @param int|null $port The port the server is listen to
  * @param array $options PDO options
  * @return \Salvo\Barrage\DataSource\Relational\Driver\Mysql\ConnectionData
  */
 public function __construct($host, $username, $password, $database, $port = null, $options = array())
 {
     $this->host = $host;
     $this->username = $username;
     $this->password = $password;
     $this->database = \Salvo\Barrage\Configuration::getRealDatabaseName($database);
     $this->port = !empty($port) ? $port : self::DEFAULT_PORT;
     $convertedOptions = array();
     if (!empty($options)) {
         foreach ($options as $option => $value) {
             $convertedOptions[constant($option)] = $value;
         }
     }
     $this->options = $convertedOptions;
 }
 public function __construct()
 {
     parent::__construct();
     $this->trueDatabase1 = Configuration::getRealDatabaseName($this->database1);
     $this->trueDatabase2 = Configuration::getRealDatabaseName($this->database2);
 }
 /**
  * @test
  */
 public function getRealDatabaseName()
 {
     $this->assertEquals('TrueTest2', \Salvo\Barrage\Configuration::getRealDatabaseName('TrueTest'));
 }
 /**
  * Generate a sql where condition
  *
  * @param $key
  * @param $options
  * @param $fromAlias
  *
  * @return string
  *
  * @throws \Salvo\Barrage\DataSource\Relational\Exception\RelationalSqlException
  */
 private function getWhereCondition($key, $options, $fromAlias)
 {
     /**
      * Allows us to parse simple sql like where statements when used as values for fields.  Supported conditional types:
      *
      * =
      * !=
      * >
      * >=
      * <
      * <=
      * like
      * notlike
      * in
      * notin
      * between
      */
     if (empty($options) && $options !== null) {
         return '';
     }
     if (is_string($options)) {
         $statementParts = explode(' ', trim($options), 2);
         switch ($statementParts[0]) {
             case '=':
             case '!=':
             case '>':
             case '>=':
             case '<':
             case '<=':
             case 'like':
             case 'notlike':
                 $options = array('condition' => $statementParts[0], 'value' => str_replace("'", '', $statementParts[1]));
                 break;
             case 'in':
             case 'notin':
                 $condition = $statementParts[0] == 'in' ? '=' : '!=';
                 $values = explode(',', $statementParts[1]);
                 $options = array('condition' => $condition, 'value' => $values);
                 break;
             case 'between':
                 $values = explode(' and ', $statementParts[1]);
                 $options = array('condition' => $statementParts[0], 'value' => $values);
                 break;
             default:
                 break;
         }
     }
     if (is_array($options) && !isset($options['value'])) {
         $options = array('value' => $options);
     }
     if (is_array($options) && (!empty($options['value']) || $options['value'] === null)) {
         $condition = !empty($options['condition']) ? $options['condition'] : '=';
         $alias = !empty($options['database']) ? \Salvo\Barrage\Configuration::getRealDatabaseName($options['database']) : $fromAlias;
         $value = $options['value'];
         if (!$this->validateWhereCondition($condition)) {
             throw new RelationalSqlException("Invalid where condition given : {$condition}");
         }
         switch ($condition) {
             case '=':
                 if ($value !== null) {
                     $condition = is_array($value) ? 'IN' : $condition;
                     $value = is_array($value) ? '(' . $this->arrayToCleanStringSql($value) . ')' : " '{$value}'";
                     $condition = $condition . $value;
                 } else {
                     $condition = 'IS NULL';
                 }
                 break;
             case '!=':
                 if ($value !== null) {
                     $condition = is_array($value) ? 'NOT IN' : $condition;
                     $value = is_array($value) ? '(' . $this->arrayToCleanStringSql($value) . ')' : " '{$value}'";
                     $condition = $condition . $value;
                 } else {
                     $condition = 'IS NOT NULL';
                 }
                 break;
             case 'between':
                 if (empty($value[0]) || empty($value[1])) {
                     throw new RelationalSqlException("Between requires the value that is passed is an array with 2 values");
                 }
                 $value1 = $this->cleanQuote($value[0]);
                 $value2 = $this->cleanQuote($value[1]);
                 $condition = strtoupper($condition) . ' ' . $value1 . ' AND ' . $value2;
                 break;
             default:
                 $value = $this->cleanQuote($value);
                 $condition = strtoupper($condition) . ' ' . $value;
                 break;
         }
     } else {
         $alias = $fromAlias;
         /*if(is_array($options))
         		{
         			$value = $this->arrayToCleanStringSql($options);
         			$condition = '= ' . $value;
         		}
         		else */
         if ($options === null) {
             $condition = 'IS NULL';
         } else {
             $value = $this->cleanQuote($options);
             $condition = '= ' . $value;
         }
     }
     //if the dot character (.) is not present, assume it is associated to the main table otherwise, assume the key is fully qualified and escaped properly
     if (strpos($key, '.') === false) {
         return "`{$alias}`.`{$key}` {$condition}";
     } else {
         return $key . ' ' . $condition;
     }
 }
 protected function getDatabaseName($database)
 {
     $database = !empty($database) ? $database : $this->defaultDatabase;
     //return $database;
     return \Salvo\Barrage\Configuration::getRealDatabaseName($database);
 }