class.Address_Book_DB.php
changeset 18 846026b8422b
parent 14 2a78703d3c2d
equal deleted inserted replaced
17:56308e61381c 18:846026b8422b
     1 <?
     1 <?
     2 require "interface.Address_Book.php";
     2 require_once "interface.Address_Book.php";
     3 
     3 
     4 class Address_Book_DB implements Address_Book {
     4 class Address_Book_DB implements Address_Book {
     5 	const INSERT_ENTRY = 'INSERT INTO data (name, tel, email)
     5 	const INSERT_ENTRY = 'INSERT INTO data (name, tel, mail)
     6 	 		      VALUES(:name, :tel, :email)';
     6 	 		      VALUES(:name, :tel, :mail)';
     7 	const SELECT_ENTRY = 'SELECT name AS NAME, tel AS TEL, email AS MAIL
     7 	const SELECT_ENTRY = 'SELECT name AS NAME, tel AS TEL, mail AS MAIL
     8 			      FROM data WHERE name LIKE :name
     8 			      FROM data WHERE name LIKE :name
     9 			      	           OR tel LIKE :tel
     9 			      	           OR tel LIKE :tel
    10 					   OR email LIKE :email';
    10 					   OR mail LIKE :mail';
    11 		
    11 		
    12 
    12 
    13 	private $dbh;
    13 	private $dbh;
    14 
    14 
    15 	public function __construct($dsn, $user = null, $pass = null) {
    15 	public function __construct($dsn, $user = null, $pass = null) {
    16 		$this->dbh = new PDO($dsn, $user, $pass,
    16 		$this->dbh = new PDO($dsn, $user, $pass,
    17 		array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    17 		array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    18 
    18 
    19 		$this->dbh->exec("CREATE TABLE IF NOT EXISTS data
    19 		$this->dbh->exec("CREATE TABLE IF NOT EXISTS data
    20 			(name TEXT, tel TEXT, email TEXT)");
    20 			(name TEXT, tel TEXT, mail TEXT)");
    21 
    21 
    22 		$this->dbh->beginTransaction();
    22 		$this->dbh->beginTransaction();
    23 
    23 
    24 		$sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data");
    24 		$sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data");
    25 		$sth->execute();
    25 		$sth->execute();
    26 		$r = $sth->fetch();
    26 		$r = $sth->fetch();
    27 
       
    28 		print_r($r);
       
    29 
    27 
    30 		if ($r['COUNT'] == 0) 
    28 		if ($r['COUNT'] == 0) 
    31 			$this->insert(array("Hans Hanson", "0815", "hans@hanson.de"));
    29 			$this->insert(array("Hans Hanson", "0815", "hans@hanson.de"));
    32 		$this->dbh->commit();
    30 		$this->dbh->commit();
    33 	}
    31 	}
    38 		if ($sth === null)
    36 		if ($sth === null)
    39 			$sth = $this->dbh->prepare(self::INSERT_ENTRY);
    37 			$sth = $this->dbh->prepare(self::INSERT_ENTRY);
    40 		
    38 		
    41 		$sth->execute(array("name" => $entry[0],
    39 		$sth->execute(array("name" => $entry[0],
    42 				    "tel"  => $entry[1],
    40 				    "tel"  => $entry[1],
    43 				    "email" => $entry[2]));
    41 				    "mail" => $entry[2]));
    44 	}
    42 	}
    45 			
    43 			
    46 	public function get_all_entries() {
    44 	public function get_all_entries() {
    47 		$entries = array();
    45 		$entries = array();
    48 		fseek($this->fh, 0, SEEK_SET);
    46 		fseek($this->fh, 0, SEEK_SET);
    75 		static $sth = null;
    73 		static $sth = null;
    76 
    74 
    77 		if ($sth === null)
    75 		if ($sth === null)
    78 			$sth = $this->dbh->prepare(self::SELECT_ENTRY);
    76 			$sth = $this->dbh->prepare(self::SELECT_ENTRY);
    79 
    77 
    80 		// $pattern = "%" . trim(trim($pattern), "%") . "%";
    78 		$pattern = trim($pattern);
       
    79 		if (empty($pattern)) return;
    81 
    80 
    82 		$pattern = trim($pattern);
       
    83 		$pattern = trim($pattern, '%');
    81 		$pattern = trim($pattern, '%');
    84 		$pattern = "%$pattern%";
    82 		$pattern = "%$pattern%";
    85 
    83 
    86 		$sth->execute(array("name" => $pattern,
    84 		$sth->execute(array("name" => $pattern,
    87 				    "tel" => $pattern,
    85 				    "tel" => $pattern,
    88 				    "email" => $pattern));
    86 				    "mail" => $pattern));
    89 		$entries = array();
    87 		$entries = array();
    90 		while ($r = $sth->fetch()) {
    88 		while ($r = $sth->fetch()) {
    91 			$entries[] = array('name' => $r['NAME'],
    89 			$entries[] = array('name' => $r['NAME'],
    92 					   'tel' => $r['TEL'],
    90 					   'tel' => $r['TEL'],
    93 					   'mail' => $r['EMAIL']);
    91 					   'mail' => $r['MAIL']);
    94 		}
    92 		}
    95 		return $entries;
    93 		return $entries;
    96 	}
    94 	}
    97 
    95 
    98 	public function delete_entries($ids) { }
    96 	public function delete_entries($ids) { }