aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--classes/MaleteDb.php58
-rw-r--r--classes/PhpIsisDb.php51
-rw-r--r--classes/SchemaDb.php29
-rw-r--r--index.php18
-rw-r--r--interface.php21
-rw-r--r--isis.php117
-rw-r--r--tests/index.php (renamed from test.php)0
7 files changed, 171 insertions, 123 deletions
diff --git a/classes/MaleteDb.php b/classes/MaleteDb.php
new file mode 100644
index 0000000..9e8684f
--- /dev/null
+++ b/classes/MaleteDb.php
@@ -0,0 +1,58 @@
+<?php
+
+/**
+ * Malete implementation of IsisDb.
+ */
+class MaleteDb implements IsisDb {
+ var $fdt;
+ var $db;
+ var $format;
+
+ public function __construct($schema) {
+ // Save db schema.
+ $this->format = $schema;
+
+ // Setup $fdt used by malete.
+ foreach ($schema['fields'] as $field => $info) {
+ $this->fdt[$field] = $info['name'];
+ }
+
+ // Open a database connection.
+ $this->db = new Isis_Db($this->fdt, $schema['db']['name'], new Isis_Server());
+ if (!$this->db->srv->sock) {
+ return FALSE;
+ }
+ }
+
+ public function read($id) {
+ if (!is_numeric($id)) {
+ return FALSE;
+ }
+ $results = $this->db->read($id);
+ return $this->tag($results);
+ }
+
+ public function rows() {
+ }
+
+ public function default_schema() {
+ return SchemaDb::default_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;
+ }
+}
diff --git a/classes/PhpIsisDb.php b/classes/PhpIsisDb.php
new file mode 100644
index 0000000..4e1aaf0
--- /dev/null
+++ b/classes/PhpIsisDb.php
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * PHP-Isis implementation of IsisDb.
+ */
+class PhpIsisDb implements IsisDb {
+ var $db;
+ var $format;
+
+ public function __construct($schema) {
+ // Save db schema.
+ $this->format = $schema;
+
+ // Setup $fdt used by malete.
+ foreach ($schema['fields'] as $field => $info) {
+ $this->fdt[$field] = $info['name'];
+ }
+
+ // Open the database.
+ $name = $schema['db']['name'];
+ $this->db = isis_open("db/$name/$name");
+ }
+
+ public function read($id) {
+ $results = isis_search('$', $this->db);
+ if (!isis_data_seek($results, $id)) {
+ return FALSE;
+ }
+
+ return $this->tag(isis_fetch_array($results));
+ }
+
+ public function rows() {
+ return isis_last_mfn($this->db);
+ }
+
+ public function default_schema() {
+ return SchemaDb::default_schema();
+ }
+
+ // Tag results of a db query.
+ function tag($results) {
+ foreach ($results as $key => $value) {
+ if ($key != 'mfn') {
+ $name = $this->format['fields'][$key]['name'];
+ $data[$name] = $value;
+ }
+ }
+ return $data;
+ }
+}
diff --git a/classes/SchemaDb.php b/classes/SchemaDb.php
new file mode 100644
index 0000000..ac5a40e
--- /dev/null
+++ b/classes/SchemaDb.php
@@ -0,0 +1,29 @@
+<?php
+
+class SchemaDb {
+ /**
+ * Schema format example.
+ */
+ public function default_schema() {
+ $schema = array(
+ 'db' => array(
+ 'name' => 'dbname',
+ 'charset' => 'charset',
+ ),
+ 'fields' => array(
+ 1 => array(
+ 'name' => 'field_name',
+ 'size' => 1000,
+ 'format' => 'numeric',
+ 'repeat' => TRUE,
+ 'subfields' => array(
+ 'a' => 'test',
+ 'b' => 'test2',
+ ),
+ ),
+ ),
+ );
+
+ return $schema;
+ }
+}
diff --git a/index.php b/index.php
index 423485e..62a58dd 100644
--- a/index.php
+++ b/index.php
@@ -1,20 +1,26 @@
<?php
/**
- * Database procedures.
+ * Isis db migration tool.
*/
-// Import Malete Library
+// Import Malete Library.
require 'contrib/malete/php/Isis.php';
-// Import Spyc
+// Import Spyc.
include('contrib/spyc/spyc.php');
-// Import database classes
-require 'isis.php';
+// Import Isis interface.
+require 'interface.php';
+
+// Autoloader.
+function __autoload($class) {
+ require_once 'classes/' .$class. '.php';
+}
// Test database connection.
$schema = Spyc::YAMLLoad('schemas/anu10.yaml');
-$db = new MaleteDb($schema);
+//$db = new MaleteDb($schema);
+$db = new PhpIsisDb($schema);
if ($db) {
$result = $db->read(4);
echo '<pre>';
diff --git a/interface.php b/interface.php
new file mode 100644
index 0000000..5ba5a55
--- /dev/null
+++ b/interface.php
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Database procedures.
+ */
+
+/**
+ * Generic interface for reading Isis databases.
+ */
+interface IsisDb {
+ // Constructor.
+ public function __construct($schema);
+
+ // 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();
+}
diff --git a/isis.php b/isis.php
deleted file mode 100644
index 4dd0ee2..0000000
--- a/isis.php
+++ /dev/null
@@ -1,117 +0,0 @@
-<?php
-/**
- * Database procedures.
- */
-
-/**
- * Generic interface for reading Isis databases.
- */
-interface IsisDb {
- // Constructor.
- public function __construct($schema);
-
- // Return field data for a given entry.
- public function fields($id = NULL);
-
- // Return subfield data for a given entry.
- 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();
-}
-
-/**
- * Malete implementation of IsisDb.
- */
-class MaleteDb implements IsisDb {
- var $fdt;
- var $db;
- var $format;
-
- public function __construct($schema) {
- // Save db schema.
- $this->format = $schema;
-
- // Setup $fdt used by malete.
- foreach ($schema['fields'] as $field => $info) {
- $this->fdt[$field] = $info['name'];
- }
-
- // Open a database connection.
- $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 subfields($id = NULL) {
- }
-
- public function read($id) {
- if (!is_numeric($id)) {
- return FALSE;
- }
- $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;
- }
-}
-
-/**
- * PHP-Isis implementation of IsisDb.
- */
-class PhpIsis implements IsisDb {
-}
diff --git a/test.php b/tests/index.php
index 195fbbc..195fbbc 100644
--- a/test.php
+++ b/tests/index.php