Added Address_Book_DB class
authorHeiko Schlittermann (IT Trainingshaus) <hs@schlittermann.de>
Wed, 26 Oct 2011 16:01:21 +0200
changeset 14 2a78703d3c2d
parent 13 6216181a7668
child 15 1d0a2e38ffcf
child 17 56308e61381c
Added Address_Book_DB class
.hgignore
class.Address_Book_DB.php
index.php
--- a/.hgignore	Wed Oct 26 16:00:14 2011 +0200
+++ b/.hgignore	Wed Oct 26 16:01:21 2011 +0200
@@ -3,3 +3,8 @@
 var/abook.txt
 .index.php.swp
 var/abook.txt~
+class.Address_Book_DB.php~
+class.Address_Book_File.php~
+templates/results.html~
+test.php~
+var/abook.sqlite
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/class.Address_Book_DB.php	Wed Oct 26 16:01:21 2011 +0200
@@ -0,0 +1,101 @@
+<?
+require "interface.Address_Book.php";
+
+class Address_Book_DB implements Address_Book {
+	const INSERT_ENTRY = 'INSERT INTO data (name, tel, email)
+	 		      VALUES(:name, :tel, :email)';
+	const SELECT_ENTRY = 'SELECT name AS NAME, tel AS TEL, email AS MAIL
+			      FROM data WHERE name LIKE :name
+			      	           OR tel LIKE :tel
+					   OR email LIKE :email';
+		
+
+	private $dbh;
+
+	public function __construct($dsn, $user = null, $pass = null) {
+		$this->dbh = new PDO($dsn, $user, $pass,
+		array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
+
+		$this->dbh->exec("CREATE TABLE IF NOT EXISTS data
+			(name TEXT, tel TEXT, email TEXT)");
+
+		$this->dbh->beginTransaction();
+
+		$sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data");
+		$sth->execute();
+		$r = $sth->fetch();
+
+		print_r($r);
+
+		if ($r['COUNT'] == 0) 
+			$this->insert(array("Hans Hanson", "0815", "hans@hanson.de"));
+		$this->dbh->commit();
+	}
+
+	private function insert($entry) {
+		static $sth = null;
+
+		if ($sth === null)
+			$sth = $this->dbh->prepare(self::INSERT_ENTRY);
+		
+		$sth->execute(array("name" => $entry[0],
+				    "tel"  => $entry[1],
+				    "email" => $entry[2]));
+	}
+			
+	public function get_all_entries() {
+		$entries = array();
+		fseek($this->fh, 0, SEEK_SET);
+		while($line = stream_get_line($this->fh, 0, self::RS)) {
+			if ($line === FALSE) break;
+			$entries[] = explode(self::FS, $line);
+		}
+		return $entries;
+	}
+
+	public function add_entry($entry) {
+		$fields = array('name', 'tel', 'mail');
+		$new = array();
+		foreach($fields as $key) {
+			if (!isset($entry[$key])) return;
+			$new[$key] = $entry[$key];
+			trim($new[$key]);
+			preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]);
+			if (empty($new[$key])) return;
+		}
+
+		flock($this->fh, LOCK_EX);
+		fputs($this->fh, join(self::FS, $new) . self::RS);
+		flock($this->fh, LOCK_UN);
+		header("Location: $_SERVER[PHP_SELF]?action=add");
+		exit;
+	}
+
+	public function search_entries($pattern) {
+		static $sth = null;
+
+		if ($sth === null)
+			$sth = $this->dbh->prepare(self::SELECT_ENTRY);
+
+		// $pattern = "%" . trim(trim($pattern), "%") . "%";
+
+		$pattern = trim($pattern);
+		$pattern = trim($pattern, '%');
+		$pattern = "%$pattern%";
+
+		$sth->execute(array("name" => $pattern,
+				    "tel" => $pattern,
+				    "email" => $pattern));
+		$entries = array();
+		while ($r = $sth->fetch()) {
+			$entries[] = array('name' => $r['NAME'],
+					   'tel' => $r['TEL'],
+					   'mail' => $r['EMAIL']);
+		}
+		return $entries;
+	}
+
+	public function delete_entries($ids) { }
+}
+
+?>
--- a/index.php	Wed Oct 26 16:00:14 2011 +0200
+++ b/index.php	Wed Oct 26 16:01:21 2011 +0200
@@ -15,7 +15,8 @@
 }
 
 try {
-	$abook = new Address_Book_File(FILE);
+	//$abook = new Address_Book_DB("mysql:host=localhost;dbname=abook", "hans", "x");
+	$abook = new Address_Book_DB("sqlite:var/abook.sqlite");
 	switch (@$_REQUEST['action']) {
 		case 'add':	$abook->add_entry($_REQUEST);
 					break;