Update PicoDb

This commit is contained in:
Frederic Guillot 2013-05-26 13:07:45 -04:00
parent 8cdb47b003
commit 880b4517d0
2 changed files with 33 additions and 34 deletions

View File

@ -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.');

View File

@ -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;
}