| Web Commons | ||||
|
||||
| Coverage | ||||||||||||
| Classes | Functions / Methods | Lines | ||||||||||
| Total |
|
100.00% | 3 / 3 |
|
100.00% | 6 / 6 |
|
100.00% | 14 / 14 | |||
| DataSource |
|
100.00% | 1 / 1 |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | |||
| public function __construct($dsn, $username=Null, $passwd=Null, $driver_options=array()) |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
| AbstractDAO |
|
100.00% | 1 / 1 |
|
100.00% | 5 / 5 |
|
100.00% | 11 / 11 | |||
| public function setDataSource(DataSource $ds) |
|
100.00% | 1 / 1 |
|
100.00% | 2 / 2 | ||||||
| protected function execute($statement, $args=array()) |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
| protected function insertAndReturnId($statement, $args, $serial=Null) |
|
100.00% | 1 / 1 |
|
100.00% | 2 / 2 | ||||||
| protected function select($statement, $args=array()) |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
| protected function hydrate($row) |
|
100.00% | 1 / 1 |
|
100.00% | 1 / 1 | ||||||
| NoMoreRowException |
|
100.00% | 1 / 1 |
|
|
100.00% | 0 / 0 | |||||
1 : <?php 2 : /** 3 : * This file groups classes pertaining to data persistence. 4 : * 5 : * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org> 6 : * @license http://www.opensource.org/licenses/mit-license.php 7 : * @version 0.1dev 8 : * 9 : * @todo Make usage of "query" and "statement" more clear. 10 : * @todo Write a DAO that maps to entities. 11 : */ 12 : 13 : /** 14 : * Offers connectivity to databases. 15 : * @since 0.1 16 : */ 17 : class DataSource extends PDO { 18 : /** 19 : * Refer to {@link http://www.php.net/manual/pdo.construct.php PDO} for 20 : * details about this constructor. 21 : * @param string $dsn 22 : * @param string $username 23 : * @param string $passwd 24 : * @param array $driver_options 25 : */ 26 : public function __construct($dsn, $username=Null, $passwd=Null, 27 : $driver_options=array()) { 28 3 : parent::__construct($dsn, $username, $passwd, $driver_options); 29 3 : $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 30 3 : } 31 : } 32 : 33 : /** 34 : * Serves as a basis for a PDO DAO. 35 : * @since 0.1 36 : */ 37 : abstract class AbstractDAO { 38 : /** 39 : * The PDO connection 40 : * @var PDO 41 : */ 42 : private $ds; 43 : 44 : public function setDataSource(DataSource $ds) { 45 3 : $this->ds = $ds; 46 3 : } 47 : 48 : /** 49 : * Prepares and executes a SQL $statement within the connection 50 : * 51 : * @param string $statement 52 : * @param mixed[] $args 53 : * @return PDOStatement 54 : * @since 0.1 55 : */ 56 : protected function execute($statement, $args=array()) { 57 3 : $ps = $this->ds->prepare($statement); 58 3 : $ps->execute($args); 59 : 60 3 : return $ps; 61 : } 62 : 63 : /** 64 : * Executes an insert statement and returns the last insert id. 65 : * 66 : * @todo Make it more transparent for PostgreSQL: it might guess serial name 67 : * 68 : * @param string $statement 69 : * @param arrray $args 70 : * @return int the last insert id for the data source 71 : * @since 0.1 72 : */ 73 : protected function insertAndReturnId($statement, $args, $serial=Null) { 74 1 : $this->execute($statement, $args); 75 1 : return $this->ds->lastInsertId($serial); 76 : } 77 : 78 : /** 79 : * Executes a statement and maps the resulting rows with 80 : * {@link AbstractDAO::hydrate()}. 81 : * 82 : * @param string $statement 83 : * @param array $args 84 : * @return array 85 : * @since 0.1 86 : */ 87 : protected function select($statement, $args=array()) { 88 1 : $statement = $this->execute($statement, $args); 89 1 : $cb = array($this, 'hydrate'); 90 1 : return array_map($cb, $statement->fetchAll(PDO::FETCH_ASSOC)); 91 : } 92 : 93 : /** 94 : * Interprets row data. 95 : * This default implementation returns the row itself. 96 : * Override this method to add custom behaviour. 97 : * 98 : * @param array $row an associative array containing row values. 99 : * @return Entity 100 : * @since 0.1 101 : */ 102 : protected function hydrate($row) { 103 1 : return $row; 104 : } 105 : } 106 : 107 : /** 108 : * Exception thrown when there is no more row to build 109 : * @since 0.1 110 : */ 111 : class NoMoreRowException extends Exception {} |
| Generated by PHPUnit 3.4.5 and Xdebug 2.1.0 at Fri Jul 16 0:48:38 CEST 2010. |