/**
  * If $name is null then the default connection will be returned.
  *
  * @see Config
  * @param string $name Optional name of a connection
  * @return Connection
  */
 public static function getConnection($name = null)
 {
     $config = Config::instance();
     $name = $name ? $name : $config->getDefaultConnection();
     if (!isset(self::$connections[$name]) || !self::$connections[$name]->connection) {
         self::$connections[$name] = Connection::instance($config->getConnection($name));
     }
     return self::$connections[$name];
 }
 /**
  * If $name is null then the default connection will be returned.
  *
  * @see Config
  * @param string $name Optional name of a connection
  * @return Connection
  */
 public static function get_connection($name = null)
 {
     $config = Config::instance();
     $name = $name ? $name : $config->get_default_connection();
     if (!isset(self::$connections[$name]) || !self::$connections[$name]->connection) {
         self::$connections[$name] = Connection::instance($config->get_connection($name));
         // If we have PHP DebugBar installed then we wrap the connection around it and register it
         if (is_a(self::$connections[$name]->connection, 'PDO') && class_exists('DebugBar\\DebugBar') && self::$debugBarConnections != null) {
             self::$connections[$name]->connection = new TraceablePDO(self::$connections[$name]->connection);
             self::$debugBarConnections->addConnection(self::$connections[$name]->connection, $name . ' #' . (count(self::$debugBarConnections->getConnections()) + 1));
         }
     }
     return self::$connections[$name];
 }
 /**
  * setups db using activerecord
  */
 public static function PRE_init_db()
 {
     # init activerecord configs
     \ActiveRecord\Config::initialize(function ($cfg) {
         # fetching db related configurations
         $dbcfg = \zinux\kernel\application\config::GetConfig("idisqus.db");
         # setting connection string
         $cfg->set_connections(array(\application\dbBootstrap::MODE_TORATAN => "{$dbcfg["type"]}://{$dbcfg["username"]}:{$dbcfg["password"]}@{$dbcfg["host"]}/{$dbcfg["name"]}?charset=utf8"));
         # enable the connection string as to \application\dbBootstrap::MODE_TORATAN
         $cfg->set_default_connection(\application\dbBootstrap::MODE_TORATAN);
     });
     # set default datetime format
     \ActiveRecord\DateTime::$DEFAULT_FORMAT = "iso8601";
     # testing db connection
     \ActiveRecord\Connection::instance();
     # if we reach here we are all OK
 }
Exemple #4
0
 public function testConnectToInvalidDatabaseShouldNotCreateDbFile()
 {
     try {
         if ($GLOBALS['OS'] !== 'WIN') {
             $xcon = Connection::instance("sqlite://" . self::InvalidDb);
             var_dump($xcon);
             $this->assertFalse(true);
         } else {
             //sqlite://windows(c%3A/GitHub/activerecord/Test/Fixtures/test.db)
             $wincon = Connection::instance("sqlite://windows('c%3A/GitHub/activerecord/Test/Fixtures/'" . self::InvalidDb . "')'");
             var_dump($wincon);
             $this->assertFalse(true);
         }
     } catch (ExceptionDatabase $e) {
         //$this->assertFalse(\file_exists(__DIR__."/".self::InvalidDb));
     }
 }
Exemple #5
0
 /**
  * Casts a value to the column's type.
  *
  * @param mixed $value The value to cast
  * @param Connection $connection The Connection this column belongs to
  * @return mixed type-casted value
  */
 public function cast($value, $connection)
 {
     if ($value === null) {
         return null;
     }
     switch ($this->type) {
         case self::STRING:
             return (string) $value;
         case self::INTEGER:
             return (int) $value;
         case self::DECIMAL:
             return (double) $value;
         case self::DATETIME:
         case self::DATE:
             if (!$value) {
                 return null;
             }
             if ($value instanceof DateTime) {
                 return $value;
             }
             return $connection->string_to_datetime($value);
     }
     return $value;
 }
 public function test_encoding()
 {
     $info = ActiveRecord\Connection::parse_connection_url('mysql://*****:*****@127.0.0.1/test?charset=utf8');
     $this->assert_equals('utf8', $info->charset);
 }
Exemple #7
0
 public function testSetCharset()
 {
     $connection_string = Config::instance()->getConnection($this->connection_name);
     $conn = Connection::instance($connection_string . '?charset=utf8');
     $this->assertEquals("SET NAMES 'utf8'", $conn->last_query);
 }
 public function string_to_datetime($string)
 {
     return parent::string_to_datetime(str_replace('.000000', '', $string));
 }
 public function testEncoding()
 {
     $info = Connection::parseConnectionUrl('mysql://*****:*****@127.0.0.1/test?charset=utf8');
     $this->assertEquals('utf8', $info->charset);
 }
Exemple #10
0
 /**
  * @expectedException Activerecord\Exceptions\ExceptionDatabase
  */
 public function testConnectToInvalidDatabase()
 {
     Connection::instance("{$this->conn->protocol}://test:test@127.0.0.1/" . self::InvalidDb);
 }
Exemple #11
0
 /**
  * Casts a value to the column's type.
  *
  * @param mixed $value The value to cast
  * @param Connection $connection The Connection this column belongs to
  * @return mixed type-casted value
  */
 public function cast($value, $connection)
 {
     if ($value === null) {
         return null;
     }
     switch ($this->type) {
         case self::STRING:
             return (string) $value;
         case self::INTEGER:
             return static::castIntegerSafely($value);
         case self::DECIMAL:
             return (double) $value;
         case self::DATETIME:
         case self::DATE:
             if (!$value) {
                 return null;
             }
             if ($value instanceof DateTime) {
                 return $value;
             }
             if ($value instanceof DateTime) {
                 return new DateTime($value->format('Y-m-d H:i:s T'));
             }
             return $connection->stringToDatetime($value);
     }
     return $value;
 }
Exemple #12
0
 public function test_parse_connection_url_with_decode_option()
 {
     $info = ActiveRecord\Connection::parse_connection_url('mysql://h%20az:h%40i@127.0.0.1/test?decode=true');
     $this->assert_equals('h az', $info->user);
     $this->assert_equals('h@i', $info->pass);
 }
Exemple #13
0
 public function testSetCharset()
 {
     $connection_string = Config::instance()->getConnection($this->connection_name);
     $conn = Connection::instance($connection_string . '?charset=utf8');
     $this->assertEquals(';charset=utf8', $conn->dsn_params);
 }
Exemple #14
0
 /**
  * Converts a string like "id_and_name_or_z" into a conditions
  * value like array("id=? AND name=? OR z=?", values, ...).
  *
  * @param Connection $connection
  * @param $name Underscored string
  * @param $values Array of values for the field names. This is used
  *   to determine what kind of bind marker to use: =?, IN(?), IS NULL
  * @param $map A hash of "mapped_column_name" => "real_column_name"
  * @return A conditions array in the form array(sql_string, value1, value2,...)
  */
 public static function createConditionsFromUnderscoredString(Connection $connection, $name, &$values = [], &$map = null)
 {
     if (!$name) {
         return null;
     }
     $parts = \preg_split('/(_and_|_or_)/i', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
     $num_values = \count($values);
     $conditions = [''];
     for ($i = 0, $j = 0, $n = \count($parts); $i < $n; $i += 2, ++$j) {
         if ($i >= 2) {
             $conditions[0] .= \preg_replace(['/_and_/i', '/_or_/i'], [' AND ', ' OR '], $parts[$i - 1]);
         }
         if ($j < $num_values) {
             if (!\is_null($values[$j])) {
                 $bind = \is_array($values[$j]) ? ' IN(?)' : '=?';
                 $conditions[] = $values[$j];
             } else {
                 $bind = ' IS NULL';
             }
         } else {
             $bind = ' IS NULL';
         }
         // map to correct name if $map was supplied
         $name = $map && isset($map[$parts[$i]]) ? $map[$parts[$i]] : $parts[$i];
         $conditions[0] .= $connection->quoteName($name) . $bind;
     }
     return $conditions;
 }