Commit 8479651d by Scott

Add key/value options to DbResult, other refactoring

parent 9cbf09d4
......@@ -42,7 +42,7 @@ class DbConnection
if (defined('QA_FINAL_WORDPRESS_INTEGRATE_PATH')) {
// Wordpress allows setting port inside DB_HOST constant, like 127.0.0.1:3306
$hostAndPort = explode(':', $host);
$hostAndPort = explode(':', $this->config['host']);
if (count($hostAndPort) >= 2) {
$this->config['host'] = $hostAndPort[0];
$this->config['port'] = $hostAndPort[1];
......
......@@ -30,35 +30,46 @@ class DbResult
$this->stmt = $stmt;
}
public function fetchNextAssoc()
/**
* Return the first row from the results as an array of [column name] => [column value].
* @param bool $allowempty If false, throw a fatal error if there is no result.
* @return array|null
*/
public function fetchNextAssoc($allowempty = false)
{
return $this->stmt->fetch(PDO::FETCH_ASSOC);
return $this->handleResult($this->stmt->fetch(PDO::FETCH_ASSOC), $allowempty, 'Reading one assoc from invalid result');
}
public function fetchAllAssoc()
/**
* Return the data as an associative array.
* @param string $key Unique field to use as the array key for each row.
* @param string $value Field to use as the value (for key=>value format instead of the default key=>row)
* @return array
*/
public function fetchAllAssoc($key = null, $value = null)
{
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
$data = array();
while (($row = $this->stmt->fetch(PDO::FETCH_ASSOC)) !== false) {
if (isset($key)) {
$data[$row[$key]] = isset($value) ? $row[$value] : $row;
} else {
$data[] = isset($value) ? $row[$value] : $row;
}
}
return $data;
}
/**
* Return a specific cell from the results. Typically used with (single-row, single-column) aggregate queries.
* @param $col 0-indexed column to select from (defaults to first column).
* @param bool $allowempty If false, throw a fatal error if there is no result.
* @return mixed|null
* @return array|null
*/
public function fetchOneValue($col = 0, $allowempty = false)
{
$data = $this->stmt->fetchColumn($col);
if ($data !== false) {
return $data;
}
if (!$allowempty) {
qa_fatal_error('Reading one value from empty results');
}
return null;
return $this->handleResult($this->stmt->fetchColumn($col), $allowempty, 'Reading one value from empty results');
}
/**
......@@ -72,6 +83,26 @@ class DbResult
}
/**
* Return data or optionally fail if no more rows.
* @param array $data
* @param bool $allowempty
* @param string $errorMsg
* @return mixed
*/
private function handleResult($data, $allowempty, $errorMsg)
{
if ($data !== false) {
return $data;
}
if (!$allowempty) {
qa_fatal_error($errorMsg);
}
return null;
}
/**
* Number of rows found (SELECT queries) or rows affected (UPDATE/INSERT/DELETE queries).
* @return int
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment