* @package Mammut\DB\Adapter\PDO */ trait PDOFetchable { /** * The database statement * * @var \PDOStatement */ protected $stmt; protected $convert = array(); protected $info = array(); protected function initStmt(iDialect $dialect) { if($dialect instanceof \Mammut\DB\Sql\Dialect\Mysql) { for($i = 0, $end = $this->stmt->columnCount(); $i < $end; $i++) { $cinf = $this->stmt->getColumnMeta($i); $this->info[$i] = $cinf; $this->info[$cinf['name']] = &$this->info[$i]; $type = $cinf["native_type"]; switch($type) { case 'DATE': $this->convert[$i] = ['mysql','date']; $this->convert[$cinf['name']] = ['mysql','date']; break; case 'DATETIME': $this->convert[$i] = ['mysql','datetime']; $this->convert[$cinf['name']] = ['mysql','datetime']; break; } } } if($dialect instanceof \Mammut\DB\Sql\Dialect\Postgresql) { for($i = 0, $end = $this->stmt->columnCount(); $i < $end; $i++) { $cinf = $this->stmt->getColumnMeta($i); $this->info[$i] = $cinf; $this->info[$cinf['name']] = &$this->info[$i]; $type = $cinf["native_type"]; switch($type) { case 'date': $this->convert[$i] = ['pgsql','date']; $this->convert[$cinf['name']] = ['pgsql','date']; break; case 'timestamp': $this->convert[$i] = ['pgsql','datetime']; $this->convert[$cinf['name']] = ['pgsql','datetime']; break; } } } } protected function _getColumns() { $cols = array(); foreach($this->info as $cinf) { $cols[] = $cinf['name']; } return $cols; } protected function _getColumnInfo($column) { $cinf = false; if(is_int($column) || preg_match('/^[0-9]+$/', $column)) $cinf = $this->info[(int) $column]; else $cinf = $this->info[$column]; $name = $cinf['name']; $type = NULL; $size = -1; $param = array(); $charset = NULL; $collate = NULL; switch($cinf['pdo_type']) { case \PDO::PARAM_BOOL: $type = ColumnInfo::TYPE_BOOLEAN; break; case \PDO::PARAM_INT: $type = ColumnInfo::TYPE_INT; break; } return new ColumnInfo($name, $type, $size, $param, $charset, $collate); } protected function _getColumnCount() { return $this->stmt->columnCount(); } protected function _getRowNumber() { throw new \BadMethodCallException('not implemented'); } protected function convert($dbtype, $dattype, $value) { switch($dbtype) { case 'mysql': switch($dattype) { case 'date': return new \Mammut\Date\Date($value); case 'datetime': return new \DateTime($value); } break; case 'pgsql': switch($dattype) { case 'date': return new \Mammut\Date\Date($value); case 'datetime': return new \DateTime($value); } break; } return $value; } protected function _fetchRow() { $result = $this->stmt->fetch(\PDO::FETCH_NUM); foreach($this->convert as $key=>$conv) { if(isset($result[$key])) $result[$key] = $this->convert($conv[0], $conv[1], $result[$key]); } return $result; } protected function _fetchArray() { $result = $this->stmt->fetch(\PDO::FETCH_ASSOC); foreach($this->convert as $key=>$conv) { if(isset($result[$key])) $result[$key] = $this->convert($conv[0], $conv[1], $result[$key]); } return $result; } protected function _fetchObject($class = false, $param = array()) { if($class) { if(class_exists($class)) $result = $this->stmt->fetchObject($class, $param); else throw new \BadMethodCallException('class ' . $class . ' dosen\'t exist'); } else $result = $this->stmt->fetchObject(); foreach($this->convert as $key=>$conv) { if(isset($result->{$key})) $result->{$key} = $this->convert($conv[0], $conv[1], $result->{$key}); } return $result; } protected function _getRowCount() { return $this->stmt->rowCount(); } }