/** * @param $namespace * @param $name * @param $locale * @return string * @throws DriverException */ private function findContentAndFillCache($namespace, $name, $locale) { try { /** @var array $result */ $result = $this->connection->select('*')->from(self::TABLE)->where(['namespace%s' => $namespace])->fetchAll(); } catch (DriverException $e) { // check table not exist if ($e->getCode() == 1146) { $this->connection->query(' CREATE TABLE [' . self::TABLE . '] ( [id] int(11) NOT NULL AUTO_INCREMENT, [namespace] varchar(255) NOT NULL, [name] varchar(255) NOT NULL, [locale] varchar(255) NOT NULL, [content] text NOT NULL, PRIMARY KEY ([id]), UNIQUE KEY [uniq_record] ([namespace],[name],[locale]) );'); $result = []; } else { throw $e; } } $tmp = []; foreach ($result as $item) { if (!isset($tmp[$item['name']])) { $tmp[$item['name']] = []; } $tmp[$item['namespace']][$item['name']][$item['locale']] = $item['content']; } $this->saveCachedNamespace($namespace, $tmp); return isset($tmp[$namespace][$name][$locale]) ? $tmp[$namespace][$name][$locale] : ''; }
/** * This will detect all columns of the table and divides it to $this->columns array and $this->numbered collection of objects. * @return void|NULL */ public function detectColumns() { $fields = $this->connection->query("SELECT * FROM `{$this->name}` %ofs", 0, "%lmt", 1); if (count($fields) == 0) { throw new \Sakura\SakuraNoRowException("There is no row in table `{$this->name}`."); } $columns = array(); foreach ($fields as $v) { foreach ($v as $key => $value) { $columns[] = $key; } } unset($fields); $numberedSet = $this->detectNumbered($columns); if ($numberedSet == False) { $this->columns = $columns; return NULL; } foreach ($numberedSet as $findName => $numbered) { foreach ($columns as $key => $column) { $numberMaybe = ltrim($column, $findName); if (is_numeric($numberMaybe)) { $number = intval($numberMaybe); if ($number >= $numbered['min'] and $number <= $numbered['max']) { unset($columns[$key]); } } } $this->addNumbered($findName, $findName, $numbered['max'], $numbered['min']); } // reset to the first founded numbered colums $this->activeNumbered = key($this->numbered); $this->columns = $columns; }