-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdbselect.inc.php
148 lines (129 loc) · 3.39 KB
/
sdbselect.inc.php
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
141
142
143
144
145
146
147
148
<?php
class SDBSelectIterator implements Iterator {
private $position = 0;
private $total_position = 0;
private $next_token = null;
private $result_batch = null;
private $query = "";
private $parser = false;
private $sdb = null;
private $ok = true;
private $err = "";
public function __construct($query, $parser = false) {
if(!class_exists('AmazonSDB')){
require('AWSSDKforPHP/sdk.class.php');
}
$this->position = 0;
$this->total_position = 0;
$this->query = $query;
$this->next_token = null;
$this->parser = $parser;
$this->sdb = new AmazonSDB();
$this->query();
}
public function isOK(){
return $this->ok;
}
public function error_message(){
return $this->err;
}
private function query(){
if ($this->next_token){
$response = $this->sdb->select($this->query, array(
'NextToken' => $this->next_token,
));
}else{
$response = $this->sdb->select($this->query);
}
$this->position = 0;
$this->next_token = isset($response->body->SelectResult->NextToken)
? (string) $response->body->SelectResult->NextToken
: null;
if (!$response->isOK()){
$this->ok = false;
$this->err = "";
if($response->body->Errors){
foreach($response->body->Errors as $error){
$this->err .= $error->Error->Message . " ";
}
}
$this->err = trim($this->err);
}else{
$this->ok = true;
}
$this->result_batch = array();
if($response->body->Item()){
foreach($response->body->Item() as $item){
$this->result_batch[] = $this->parse($item);
}
}
}
private function parse($row){
if($this->parser and function_exists($this->parser)){
$parser = $this->parser;
return $parser($row);
}else{
return $this->generic_sdb_parse($row);
}
}
private function generic_sdb_parse($item){
$return = array(
'primary_key' => (string) $item->Name
);
// Loop through the item's attributes
foreach ($item->Attribute as $attribute){
$column_name = (string) $attribute->Name;
if(preg_match('/^\{.*\}$/',(string) $attribute->Value)){ //Data is plausibly JSON
$parsed_json = json_decode((string) $attribute->Value, true);
if($parsed_json === null){
//Guess it wasn't JSON after all
$return[$column_name] = (string) $attribute->Value;
}else{
$return[$column_name] = $parsed_json;
}
}elseif(preg_match('/^[\-+]?\d+(:?\.\d+)?$/', (string) $attribute->Value)){ //Numeric
$return[$column_name] = floatval((string) $attribute->Value);
}else{ //Treat as dumb string
$return[$column_name] = (string) $attribute->Value;
}
}
return $return;
}
function rewind() {
//var_dump(__METHOD__);
if($this->total_position == 0){
return;
}
$this->position = 0;
$this->total_position = 0;
$this->next_token = null;
$this->query();
}
function current() {
//var_dump(__METHOD__);
return $this->result_batch[$this->position];
}
function key() {
//var_dump(__METHOD__);
return $this->total_position;
}
function next() {
//var_dump(__METHOD__);
$this->position += 1;
$this->total_position += 1;
if(!isset($this->result_batch[$this->position]) && $this->next_token){
$this->query();
}
}
function next_valid() {
return (isset($this->result_batch[$this->position + 1]) or $this->next_token);
}
function valid() {
//var_dump(__METHOD__);
if(!$this->result_batch or !is_array($this->result_batch)){
return false;
}
return isset($this->result_batch[$this->position]);
}
}
?>