diff --git a/vendor/PicoDb/Database.php b/vendor/PicoDb/Database.php index ffc37bb..c0094e9 100644 --- a/vendor/PicoDb/Database.php +++ b/vendor/PicoDb/Database.php @@ -21,14 +21,6 @@ class Database require_once __DIR__.'/Drivers/Sqlite.php'; $this->pdo = new Sqlite($settings['filename']); break; -/* - case 'mysql': - $this->pdo = new \PDO( - 'mysql:host='.$settings['hostname'].';dbname='.$settings['dbname'], - $settings['username'], - $settings['password'] - ); - break;*/ default: throw new \LogicException('This database driver is not supported.'); diff --git a/vendor/PicoDb/Table.php b/vendor/PicoDb/Table.php index f05532b..fc498bf 100644 --- a/vendor/PicoDb/Table.php +++ b/vendor/PicoDb/Table.php @@ -127,23 +127,8 @@ class Table public function findAll() { - $sql = sprintf( - 'SELECT %s FROM %s %s %s %s %s %s', - empty($this->columns) ? '*' : implode(', ', $this->columns), - $this->db->escapeIdentifier($this->table_name), - implode(' ', $this->joins), - $this->conditions(), - $this->sql_order, - $this->sql_limit, - $this->sql_offset - ); - - $rq = $this->db->execute($sql, $this->values); - - if (false === $rq) { - - return false; - } + $rq = $this->db->execute($this->buildSelectQuery(), $this->values); + if (false === $rq) return false; return $rq->fetchAll(\PDO::FETCH_ASSOC); } @@ -158,23 +143,45 @@ class Table } + public function findOneColumn($column) + { + $this->limit(1); + $this->columns = array($column); + + $rq = $this->db->execute($this->buildSelectQuery(), $this->values); + if (false === $rq) return false; + + return $rq->fetchColumn(); + } + + + public function buildSelectQuery() + { + return sprintf( + 'SELECT %s FROM %s %s %s %s %s %s', + empty($this->columns) ? '*' : implode(', ', $this->columns), + $this->db->escapeIdentifier($this->table_name), + implode(' ', $this->joins), + $this->conditions(), + $this->sql_order, + $this->sql_limit, + $this->sql_offset + ); + } + + public function count() { $sql = sprintf( - 'SELECT COUNT(*) AS count FROM %s'.$this->conditions().$this->sql_order.$this->sql_limit.$this->sql_offset, + 'SELECT COUNT(*) FROM %s'.$this->conditions().$this->sql_order.$this->sql_limit.$this->sql_offset, $this->db->escapeIdentifier($this->table_name) ); $rq = $this->db->execute($sql, $this->values); + if (false === $rq) return false; - if (false === $rq) { - - return false; - } - - $result = $rq->fetch(\PDO::FETCH_ASSOC); - - return isset($result['count']) ? (int) $result['count'] : 0; + $result = $rq->fetchColumn(); + return $result ? (int) $result : 0; }