php/class.Address_Book_DB.php
changeset 23 2d22262da8e0
parent 18 846026b8422b
equal deleted inserted replaced
20:52fb6408b86a 23:2d22262da8e0
       
     1 <?
       
     2 require_once "interface.Address_Book.php";
       
     3 
       
     4 class Address_Book_DB implements Address_Book {
       
     5 	const INSERT_ENTRY = 'INSERT INTO data (name, tel, mail)
       
     6 	 		      VALUES(:name, :tel, :mail)';
       
     7 	const SELECT_ENTRY = 'SELECT name AS NAME, tel AS TEL, mail AS MAIL
       
     8 			      FROM data WHERE name LIKE :name
       
     9 			      	           OR tel LIKE :tel
       
    10 					   OR mail LIKE :mail';
       
    11 		
       
    12 
       
    13 	private $dbh;
       
    14 
       
    15 	public function __construct($dsn, $user = null, $pass = null) {
       
    16 		$this->dbh = new PDO($dsn, $user, $pass,
       
    17 		array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
       
    18 
       
    19 		$this->dbh->exec("CREATE TABLE IF NOT EXISTS data
       
    20 			(name TEXT, tel TEXT, mail TEXT)");
       
    21 
       
    22 		$this->dbh->beginTransaction();
       
    23 
       
    24 		$sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data");
       
    25 		$sth->execute();
       
    26 		$r = $sth->fetch();
       
    27 
       
    28 		if ($r['COUNT'] == 0) 
       
    29 			$this->insert(array("Hans Hanson", "0815", "hans@hanson.de"));
       
    30 		$this->dbh->commit();
       
    31 	}
       
    32 
       
    33 	private function insert($entry) {
       
    34 		static $sth = null;
       
    35 
       
    36 		if ($sth === null)
       
    37 			$sth = $this->dbh->prepare(self::INSERT_ENTRY);
       
    38 		
       
    39 		$sth->execute(array("name" => $entry[0],
       
    40 				    "tel"  => $entry[1],
       
    41 				    "mail" => $entry[2]));
       
    42 	}
       
    43 			
       
    44 	public function get_all_entries() {
       
    45 		$entries = array();
       
    46 		fseek($this->fh, 0, SEEK_SET);
       
    47 		while($line = stream_get_line($this->fh, 0, self::RS)) {
       
    48 			if ($line === FALSE) break;
       
    49 			$entries[] = explode(self::FS, $line);
       
    50 		}
       
    51 		return $entries;
       
    52 	}
       
    53 
       
    54 	public function add_entry($entry) {
       
    55 		$fields = array('name', 'tel', 'mail');
       
    56 		$new = array();
       
    57 		foreach($fields as $key) {
       
    58 			if (!isset($entry[$key])) return;
       
    59 			$new[$key] = $entry[$key];
       
    60 			trim($new[$key]);
       
    61 			preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]);
       
    62 			if (empty($new[$key])) return;
       
    63 		}
       
    64 
       
    65 		flock($this->fh, LOCK_EX);
       
    66 		fputs($this->fh, join(self::FS, $new) . self::RS);
       
    67 		flock($this->fh, LOCK_UN);
       
    68 		header("Location: $_SERVER[PHP_SELF]?action=add");
       
    69 		exit;
       
    70 	}
       
    71 
       
    72 	public function search_entries($pattern) {
       
    73 		static $sth = null;
       
    74 
       
    75 		if ($sth === null)
       
    76 			$sth = $this->dbh->prepare(self::SELECT_ENTRY);
       
    77 
       
    78 		$pattern = trim($pattern);
       
    79 		if (empty($pattern)) return;
       
    80 
       
    81 		$pattern = trim($pattern, '%');
       
    82 		$pattern = "%$pattern%";
       
    83 
       
    84 		$sth->execute(array("name" => $pattern,
       
    85 				    "tel" => $pattern,
       
    86 				    "mail" => $pattern));
       
    87 		$entries = array();
       
    88 		while ($r = $sth->fetch()) {
       
    89 			$entries[] = array('name' => $r['NAME'],
       
    90 					   'tel' => $r['TEL'],
       
    91 					   'mail' => $r['MAIL']);
       
    92 		}
       
    93 		return $entries;
       
    94 	}
       
    95 
       
    96 	public function delete_entries($ids) { }
       
    97 }
       
    98 
       
    99 ?>