-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquery_row.class.php
More file actions
90 lines (73 loc) · 3.22 KB
/
query_row.class.php
File metadata and controls
90 lines (73 loc) · 3.22 KB
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
<?php
namespace PluSQL;
use Exception;
class QueryRow
{
private $query;
private $table;
private $data;
private $index;
public function __construct($query,$table,$index)
{
$this->query = $query;
$this->index = $index;
$this->table_inspector = TableInspector::forTable($table,$this->query->link());
$this->data = $this->query->rowAtIndex($this->index);
if(!is_array($this->data))
throw new InvalidQueryRowException('You have tried to create a QueryRow object with an empty data set for: '.$table);
}
public function rowData()
{
return $this->query->rowAtIndex($this->index);
}
public function keySignature()
{
$keys = $this->table_inspector->primaryKeys();
$sig = array();
foreach($keys as $key_name)
{
$table_key_name = $this->table_inspector->name().'.'.$key_name;
if(array_key_exists($key_name,$this->data))
$sig[] = $this->data[$key_name];
else if(array_key_exists($table_key_name,$this->data))
$sig[] = $this->data[$table_key_name];
else
throw new InvalidQueryRowException('You can\'t get a key signature for: '.$this->table_inspector->name().' from row: '.implode('::',array_keys($this->data)).' because: '.$key_name.' is not present');
}
return implode('::',$sig);
}
public function __get($name)
{
$table_name = $this->table_inspector->name().'.'.$name;
if(array_key_exists($name,$this->data))
$ret = $this->data[$name];
else if(array_key_exists($table_name,$this->data))
$ret = $this->data[$table_name];
else
{
try
{
$ret = new QueryIterator($this->query,$name,$this->index);
$pairs = array();
foreach($this->table_inspector->primaryKeys() as $name)
{
if(isset($this->data[$name]))
$pairs[$name] = $this->data[$name];
else
{
$table_name = $this->table_inspector->name().'.'.$name;
if(isset($this->data[$table_name]))
$pairs[$table_name] = $this->data[$table_name];
}
}
$ret->constrainKeys($pairs);
}
catch(TableInspectorException $exc)
{
throw new InvalidQueryRowException('You tried to get the attribute: '.$name.' from a query row for the table: '.$this->table_inspector->name().' but this is neither a valid column nor a valid table in the database (so I can\'t return a new iterator either)');
}
}
return $ret;
}
}
class InvalidQueryRowException extends Exception {}