aboutsummaryrefslogtreecommitdiff
path: root/classes/MaleteDb.php
blob: 201ddc0eca6f3a45c2cdaee6d9421853ccaf97af (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

/**
 * Malete implementation of IsisDb.
 */
class MaleteDb implements IsisDb {
  /**
   * @var $fdt
   *   Field description table.
   */
  var $fdt;

  /**
   * @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 a database connection.
    $this->db = new Isis_Db($this->fdt, $schema['db']['name'], new Isis_Server());
    if (!$this->db->srv->sock) {
      return FALSE;
    }
  }

  /**
   * Read an entry.
   *
   * @see IsisDb::read()
   *
   * @todo
   *   Subfields.
   */  
  public function read($id) {
    if (!is_numeric($id)) {
      return FALSE;
    }
    if ($results !== FALSE) {
      $results = $this->db->read($id);
      return $this->tag($results);
    }
    else {
      return FALSE;
    }
  }

  /**
   * Return number of rows in the database.
   *
   * The Malete API doen't implement such feature so we
   * have to emulate it by iterating over all entries
   * until MaleteDb::read() returns FALSE.
   *
   * @see IsisDb::read()
   */  
  public function rows() {
    // The first entry in a malete database has id 1 and
    // not 0, therefore $id's initial value should be 1.
    $id = 1;
    while($this->db->read($id)) {
      $id++; 
    }
    return $id - 1;
  }

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

  /**
   * Check configuration.
   *
   * @see IsisDb::check()
   */  
  public function check($schema, $section = NULL) {
    // Check API availability.
    if (!class_exists('Isis_Db')) {
      throw new Exception('Could not find Isis_Db class. Please check your malete installation.');
      return FALSE;
    }

    // Check schema configuration.
    return SchemaDb::check($schema, $section);
  }

  /**
   * Tag results of a db query.
   *
   * This function converts the keys of query result from field numbers
   * to names and and also puts repetition fields into place as Malete
   * deals with field repetition by using a 'tag' property in the resulting
   * query object. 
   *
   * @param $results
   *   Database query results.
   *
   * @return
   *   Tagged database result.
   */
  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;
  }
}