aboutsummaryrefslogtreecommitdiff
path: root/classes/PhpIsisDb.php
blob: 78c9cf0689ecd97c755b7664757eae5d2f5dd92b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

/**
 * PHP-Isis implementation of IsisDb.
 */
class PhpIsisDb implements IsisDb {
  /**
   * @var $db
   *   Database resource.
   */  
  var $db;

  /**
   * @var $format
   *   Database format, derived from $schema.
   */
  var $format;

  /**
   * Constructor.
   *
   * @see IsisDb::__construct()
   */
  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");
  }

  /**
   * Read an entry.
   *
   * @see IsisDb::read()
   */    
  public function read($id) {
    $results = isis_search('$', $this->db);
    if (!isis_data_seek($results, $id)) {
      return FALSE;
    }

    return $this->tag(isis_fetch_array($results));
  }

  /**
   * Return number of rows in the database.
   *
   * @see IsisDb::rows()
   */    
  public function rows() {
    return isis_last_mfn($this->db);
  }

  /**
   * Return a default example schema.
   *
   * @see IsisDb::default_schema()
   */    
  public function default_schema() {
    return SchemaDb::default_schema();
  }

  /**
   * Tag results of a db query.
   *
   * @param $results
   *   Database query results.
   *
   * @return
   *   Tagged database result.
   */
  function tag($results) {
    foreach ($results as $key => $value) {
      if ($key != 'mfn') {
        $name  = $this->format['fields'][$key]['name'];
        $data[$name] = $value;
      }
    }
    return $data;
  }  
}