From dc40765aa4c48b802a135ae22f5349f4a9d275de Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 29 Mar 2010 12:20:10 -0300 Subject: Adding db schemas and adding sample code --- isis.php | 91 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 33 deletions(-) (limited to 'isis.php') diff --git a/isis.php b/isis.php index 66b3b7d..9c8bbc2 100644 --- a/isis.php +++ b/isis.php @@ -3,27 +3,6 @@ * Database procedures. */ -/** - * Schema format example. - */ -$schema = array( - 'db' => array( - 'name' => 'dbname', - ), - 'fields' => array( - 'field_name' => array( - 'id' => 1, - 'size' => 1000, - 'format' => 'numeric', - 'repeat' => TRUE, - 'subfields' => array( - 'a' => 'test', - 'b' => 'test2', - ), - ), - ), -); - /** * Generic interface for reading Isis databases. */ @@ -32,16 +11,19 @@ interface IsisDb { public function __construct($schema); // Return field data for a given entry. - public function fields($id); + public function fields($id = NULL); // Return subfield data for a given entry. - public function subfields($id); + public function subfields($id = NULL); // Read an entry. public function read($id); // Return number of rows in the database. public function rows(); + + // Return a default example schema. + public function default_schema(); } /** @@ -54,33 +36,76 @@ class MaleteDb implements IsisDb { public function __construct($schema) { // Save db schema. - $format = $schema; + $this->format = $schema; // Setup $fdt used by malete. foreach ($schema['fields'] as $field => $info) { - $fdt[$field] = $info['id']; + $this->fdt[$field] = $info['name']; } // Open a database connection. - $db = new Isis_Db($fdt, $schema['db']['name'], new Isis_Server()); + $this->db = new Isis_Db($this->fdt, $schema['db']['name'], new Isis_Server()); + if (!$this->db->srv->sock) { + return FALSE; + } } - public function fields($id == NULL) { + public function fields($id = NULL) { } - public function subfields($id == NULL) { + public function subfields($id = NULL) { } - // TODO: put result into $schema format. public function read($id) { - if (!is_numeric($id) { + if (!is_numeric($id)) { return FALSE; } - return $this->$db->read($id); + $results = $this->db->read($id); + return $this->tag($results); } public function rows() { } -} -?> + /** + * Schema format example. + */ + public function default_schema() { + $schema = array( + 'db' => array( + 'name' => 'dbname', + ), + 'fields' => array( + 1 => array( + 'name' => 'field_name', + 'size' => 1000, + 'format' => 'numeric', + 'repeat' => TRUE, + 'subfields' => array( + 'a' => 'test', + 'b' => 'test2', + ), + ), + ), + ); + + return $schema; + } + + // Tag results of a db query. + function tag($results) { + foreach ($results->val as $key => $value) { + $field = $results->tag[$key]; + $name = $this->format['fields'][$field]['name']; + + // Handles field repetition. + if ($this->format['fields'][$field]['repeat']) { + $data[$name][] = $value; + } + else { + $data[$name] = $value; + } + } + return $data; + } +} -- cgit v1.2.3