/** * set database * @param string $database */ public function setDB($database = 'PF') { $f3 = \Base::instance(); if ($database === 'CCP') { // CCP DB $dns = Controller\Controller::getEnvironmentData('DB_CCP_DNS'); $name = Controller\Controller::getEnvironmentData('DB_CCP_NAME'); $user = Controller\Controller::getEnvironmentData('DB_CCP_USER'); $password = Controller\Controller::getEnvironmentData('DB_CCP_PASS'); } else { // Pathfinder DB $dns = Controller\Controller::getEnvironmentData('DB_DNS'); $name = Controller\Controller::getEnvironmentData('DB_NAME'); $user = Controller\Controller::getEnvironmentData('DB_USER'); $password = Controller\Controller::getEnvironmentData('DB_PASS'); } // check for DB switch. If current DB equal new DB -> no switch needed if (!$f3->exists('DB') || $name !== $f3->get('DB')->name()) { $db = $this->connect($dns, $name, $user, $password); $f3->set('DB', $db); // set DB timezone to UTC +00:00 (eve server time) $f3->get('DB')->exec('SET @@session.time_zone = "+00:00";'); // disable innoDB schema (relevant vor MySql 5.5) // not necessary for MySql > v.5.6 //$f3->get('DB')->exec('SET GLOBAL innodb_stats_on_metadata = OFF;'); } }
/** * get database * @param string $database * @return mixed|void */ public function getDB($database = 'PF') { $f3 = \Base::instance(); $dbHiveKey = $this->getDbHiveKey($database); if ($f3->exists($dbHiveKey)) { return $f3->get($dbHiveKey); } else { return $this->setDB($database); } }
/** * re-assort the current collection using a sql-like syntax * @param $cond */ public function orderBy($cond) { $cols = \Base::instance()->split($cond); $this->uasort(function ($val1, $val2) use($cols) { foreach ($cols as $col) { $parts = explode(' ', $col, 2); $order = empty($parts[1]) ? 'ASC' : $parts[1]; $col = $parts[0]; list($v1, $v2) = array($val1[$col], $val2[$col]); if ($out = strnatcmp($v1, $v2) * ((strtoupper($order) == 'ASC') * 2 - 1)) { return $out; } } return 0; }); }
/** * connect to a database * @param $dns * @param $name * @param $user * @param $password * @return SQL */ protected function connect($dns, $name, $user, $password) { $db = null; try { $db = new SQL($dns . $name, $user, $password, [\PDO::MYSQL_ATTR_COMPRESS => true, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_TIMEOUT => \Base::instance()->get('REQUIREMENTS.MYSQL.PDO_TIMEOUT')]); } catch (\PDOException $e) { // DB connection error // -> log it self::getLogger()->write($e->getMessage()); } return $db; }
/** * get an intersection from a cached relation-set, based on given keys * @param string $prop * @param array|string $keys * @return array */ public function getSubset($prop, $keys) { if (is_string($keys)) { $keys = \Base::instance()->split($keys); } if (!is_array($keys)) { trigger_error(sprintf(self::E_SubsetKeysValue, gettype($keys))); } if (!$this->hasRelSet($prop) || !($relSet = $this->getRelSet($prop))) { return null; } foreach ($keys as &$key) { if ($key instanceof \MongoId) { $key = (string) $key; } } return array_values(array_intersect_key($relSet, array_flip($keys))); }