miniflux-legacy/vendor/fguillot/picodb/lib/PicoDb/Hashtable.php

112 lines
2.2 KiB
PHP
Raw Normal View History

2015-01-28 02:13:16 +01:00
<?php
namespace PicoDb;
use PDO;
2015-08-15 03:33:39 +02:00
/**
* Hashtable (key/value)
*
* @author Frederic Guillot
* @author Mathias Kresin
*/
2015-01-28 02:13:16 +01:00
class Hashtable extends Table
{
2015-08-15 03:33:39 +02:00
/**
* Column for the key
*
* @access private
* @var string
*/
private $keyColumn = 'key';
/**
* Column for the value
*
* @access private
* @var string
*/
private $valueColumn = 'value';
2015-01-28 02:13:16 +01:00
/**
* Set the key column
*
* @access public
2015-08-15 03:33:39 +02:00
* @param string $column
* @return Table
2015-01-28 02:13:16 +01:00
*/
public function columnKey($column)
{
2015-08-15 03:33:39 +02:00
$this->keyColumn = $column;
2015-01-28 02:13:16 +01:00
return $this;
}
/**
* Set the value column
*
* @access public
2015-08-15 03:33:39 +02:00
* @param string $column
* @return Table
2015-01-28 02:13:16 +01:00
*/
public function columnValue($column)
{
2015-08-15 03:33:39 +02:00
$this->valueColumn = $column;
2015-01-28 02:13:16 +01:00
return $this;
}
/**
* Insert or update
*
* @access public
2015-08-15 03:33:39 +02:00
* @param array $hashmap
2015-01-28 02:13:16 +01:00
* @return boolean
*/
2015-08-15 03:33:39 +02:00
public function put(array $hashmap)
2015-01-28 02:13:16 +01:00
{
2015-08-15 03:33:39 +02:00
return $this->db->getDriver()->upsert($this->getName(), $this->keyColumn, $this->valueColumn, $hashmap);
2015-01-28 02:13:16 +01:00
}
/**
* Hashmap result [ [column1 => column2], [], ...]
*
* @access public
* @return array
*/
public function get()
{
$hashmap = array();
// setup where condition
if (func_num_args() > 0) {
2015-08-15 03:33:39 +02:00
$this->in($this->keyColumn, func_get_args());
2015-01-28 02:13:16 +01:00
}
// setup to select columns in case that there are more than two
2015-08-15 03:33:39 +02:00
$this->columns($this->keyColumn, $this->valueColumn);
2015-01-28 02:13:16 +01:00
2015-08-15 03:33:39 +02:00
$rq = $this->db->execute($this->buildSelectQuery(), $this->condition->getValues());
2015-01-28 02:13:16 +01:00
$rows = $rq->fetchAll(PDO::FETCH_NUM);
foreach ($rows as $row) {
$hashmap[$row[0]] = $row[1];
}
return $hashmap;
}
/**
* Shortcut method to get a hashmap result
*
* @access public
* @param string $key Key column
* @param string $value Value column
* @return array
*/
public function getAll($key, $value)
{
2015-08-15 03:33:39 +02:00
$this->keyColumn = $key;
$this->valueColumn = $value;
2015-01-28 02:13:16 +01:00
return $this->get();
}
}