f1 = $f1; $this->f2 = $f2; } } interface T1DatabaseAccess { public function get_one($f2); public function get_all(); public function put($o); public function remove($f1); } class T1DatabaseAccessPgsql implements T1DatabaseAccess { private $server; private $username; private $password; private $database; public function __construct($server, $username, $password, $database) { $this->server = $server; $this->username = $username; $this->password = $password; $this->database = $database; } private function get_connection() { // connect to server and select database $con = pg_connect(sprintf('host=%s port=5432 dbname=%s user=%s password=%s', $this->server, $this->database, $this->username, $this->password)) or die(pg_last_error()); // return connection (note: no globals) return $con; } public function get_one($f2) { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement pg_prepare($con, 'stmt_get_one', 'SELECT f1,f2 FROM t1 WHERE f2=$1') or die(pg_last_error($con)); // execute $rs = pg_execute($con, 'stmt_get_one', array($f2)) or die(pg_last_error($con)); // get first row and field F1 if($row = pg_fetch_array($rs, NULL, PGSQL_ASSOC)) { $res = new T1($row['f1'], $row['f2']); } else { die("$f2 not found"); } // close pg_query('DEALLOCATE stmt_get_one'); pg_close($con); // return value return $res; } public function get_all() { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement pg_prepare($con, 'stmt_get_all', 'SELECT f1,f2 FROM t1') or die(pg_last_error($con)); // execute $rs = pg_execute($con, 'stmt_get_all', array()) or die(pg_last_error($con)); // create array to contain rows $res = array(); // get all rows while($row = pg_fetch_array($rs, NULL, PGSQL_ASSOC)) { $res[] = new T1($row['f1'], $row['f2']); } // close pg_query('DEALLOCATE stmt_get_all'); pg_close($con); // return array with rows return $res; } public function put($o) { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement pg_prepare($con, 'stmt_put', 'INSERT INTO t1(f1,f2) VALUES($1,$2)') or die(pg_last_error($con)); // execute pg_execute($con, 'stmt_put', array($o->f1, $o->f2)) or die(pg_last_error($con)); // close pg_query('DEALLOCATE stmt_put'); pg_close($con); } public function remove($f1) { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement pg_prepare($con, 'stmt_remove', 'DELETE FROM t1 WHERE f1=$1') or die(pg_last_error($con)); // execute pg_execute($con, 'stmt_remove', array($f1)) or die(pg_last_error($con)); // close pg_query('DEALLOCATE stmt_remove'); pg_close($con); } } class T1DatabaseAccessFactory { const PGSQL = 'pgsql'; public static function get_database_access($id, $server, $username, $password, $database) { if($id === T1DatabaseAccessFactory::PGSQL) { return new T1DatabaseAccessPgsql($server, $username, $password, $database); } else { throw new Exception("Unknown T1DatabaseAccess id: $id"); } } } interface T1Presenter { public function display($data); } class T1PresenterHtmlTable implements T1Presenter { public function display($data) { $rows = ""; foreach($data as $row) { $f1 = $row->f1; $f2 = $row->f2; $rows .= sprintf("\r\n$f1\r\n$f2\r\n\r\n"); } return "\r\n\r\n\r\n\r\n\r\n$rows
F1F2
\r\n"; } } class T1PresenterFactory { const HTML_TABLE = 'HTML Table'; public static function get_presenter($id) { if($id === T1PresenterFactory::HTML_TABLE) { return new T1PresenterHtmlTable() ; } else { throw new Exception("Unknown T1Presenter id: $id"); } } } function test() { $data_access = T1DatabaseAccessFactory::get_database_access(T1DatabaseAccessFactory::PGSQL, 'localhost', 'postgres', 'xxxxxx', 'Test'); $presenter = T1PresenterFactory::get_presenter(T1PresenterFactory::HTML_TABLE); $f1 = $data_access->get_one('BB')->f1; echo "$f1
\r\n"; $data = $data_access->get_all(); echo $presenter->display($data); $data_access->put(new T1(999, 'XXX')); $data = $data_access->get_all(); echo $presenter->display($data); $data_access->remove(999); $data = $data_access->get_all(); echo $presenter->display($data); } test(); ?>