My favorites | Sign in
Project Logo
             
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/**
* File: Amazon SDB
* Amazon SimpleDB Service (http://aws.amazon.com/simpledb)
*
* Version:
* 2008.12.18
*
* Copyright:
* 2006-2009 LifeNexus Digital, Inc., and contributors.
*
* License:
* Simplified BSD License - http://opensource.org/licenses/bsd-license.php
*
* See Also:
* Tarzan - http://tarzan-aws.com
* Amazon SDB - http://aws.amazon.com/simpledb
*/


/*%******************************************************************************************%*/
// CONSTANTS

/**
* Constant: SDB_DEFAULT_URL
* Specify the default queue URL.
*/
define('SDB_DEFAULT_URL', 'sdb.amazonaws.com');


/*%******************************************************************************************%*/
// EXCEPTIONS

/**
* Exception: SDB_Exception
* Default SDB Exception.
*/
class SDB_Exception extends Exception {}


/*%******************************************************************************************%*/
// MAIN CLASS

/**
* Class: AmazonSDB
* Container for all Amazon SimpleDB-related methods. Inherits additional methods from TarzanCore.
*
* Extends:
* TarzanCore
*
* Example Usage:
* (start code)
* require_once('tarzan.class.php');
*
* // Instantiate a new AmazonSDB object using the settings from the config.inc.php file.
* $s3 = new AmazonSDB();
*
* // Instantiate a new AmazonSDB object using these specific settings.
* $s3 = new AmazonSDB($key, $secret_key);
* (end)
*/
class AmazonSDB extends TarzanCore
{
/*%******************************************************************************************%*/
// CONSTRUCTOR

/**
* Method: __construct()
* The constructor
*
* Access:
* public
*
* Parameters:
* key - _string_ (Optional) Your Amazon API Key. If blank, it will look for the <AWS_KEY> constant.
* secret_key - _string_ (Optional) Your Amazon API Secret Key. If blank, it will look for the <AWS_SECRET_KEY> constant.
*
* Returns:
* _boolean_ false if no valid values are set, otherwise true.
*
* See Also:
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/__construct.phps
*/
public function __construct($key = null, $secret_key = null)
{
$this->api_version = '2007-11-07';
$this->hostname = SDB_DEFAULT_URL;

if (!$key && !defined('AWS_KEY'))
{
throw new SDB_Exception('No account key was passed into the constructor, nor was it set in the AWS_KEY constant.');
}

if (!$secret_key && !defined('AWS_SECRET_KEY'))
{
throw new SDB_Exception('No account secret was passed into the constructor, nor was it set in the AWS_SECRET_KEY constant.');
}

return parent::__construct($key, $secret_key);
}


/*%******************************************************************************************%*/
// DOMAIN

/**
* Method: create_domain()
* Creates a new domain. The domain name must be unique among the domains associated with the Access Key ID provided in the request. The CreateDomain operation might take 10 or more seconds to complete.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to use for storing data.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_CreateDomain.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/create_domain.phps
* Related - <create_domain()>, <list_domains()>, <delete_domain()>, <domain_metadata()>
*/
public function create_domain($domain_name, $returnCurlHandle = null)
{
$opt = array();
$opt['DomainName'] = $domain_name;
$opt['returnCurlHandle'] = $returnCurlHandle;

return $this->authenticate('CreateDomain', $opt, $this->hostname);
}

/**
* Method: list_domains()
* Lists all domains associated with the Access Key ID. It returns domain names up to the limit set by MaxNumberOfDomains. A NextToken is returned if there are more than MaxNumberOfDomains domains. Calling ListDomains successive times with the NextToken returns up to MaxNumberOfDomains more domain names each time.
*
* Access:
* public
*
* Parameters:
* opt - _array_ (Required) Associative array of parameters which can have the following keys:
*
* Keys for the $opt parameter:
* MaxNumberOfDomains - _integer_ (Optional) The maximum number of domain names you want returned. The range is 1 to 100.
* NextToken - _string_ (Optional) String that tells Amazon SimpleDB where to start the next list of domain names.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_ListDomains.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/list_domains.phps
* Related - <create_domain()>, <list_domains()>, <delete_domain()>, <domain_metadata()>
*/
public function list_domains($opt = null)
{
if (!$opt) $opt = array();

return $this->authenticate('ListDomains', $opt, $this->hostname);
}

/**
* Method: delete_domain()
* Deletes a domain. Any items (and their attributes) in the domain are deleted as well. The DeleteDomain operation might take 10 or more seconds to complete. Running DeleteDomain on a domain that does not exist or running the function multiple times using the same domain name will not result in an error response.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to delete.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_DeleteDomain.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/delete_domain.phps
* Related - <create_domain()>, <list_domains()>, <delete_domain()>, <domain_metadata()>
*/
public function delete_domain($domain_name, $returnCurlHandle = null)
{
$opt = array();
$opt['DomainName'] = $domain_name;
$opt['returnCurlHandle'] = $returnCurlHandle;

return $this->authenticate('DeleteDomain', $opt, $this->hostname);
}

/**
* Method: domain_metadata()
* Returns information about the domain, including when the domain was created, the number of items and attributes, and the size of attribute names and values.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to use for retrieving metadata.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_DomainMetadata.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/domain_metadata.phps
* Related - <create_domain()>, <list_domains()>, <delete_domain()>, <domain_metadata()>
*/
public function domain_metadata($domain_name, $returnCurlHandle = null)
{
$opt = array();
$opt['DomainName'] = $domain_name;
$opt['returnCurlHandle'] = $returnCurlHandle;

return $this->authenticate('DomainMetadata', $opt, $this->hostname);
}


/*%******************************************************************************************%*/
// ATTRIBUTES

/**
* Method: put_attributes()
* Creates or replaces attributes in an item. You specify new attributes using a combination of the Attribute.X.Name and Attribute.X.Value parameters.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to use for storing data.
* item_name - _string_ (Required) The name of the base item which will contain the series of keypairs.
* keypairs - _array_ (Required) Associative array of parameters which are treated as key-value and key-multivalue pairs (i.e. a key can have one or more values; think tags).
* replace - _boolean|array_ (Optional) Whether to replace a key-value pair if a matching key already exists. Supports either a boolean (which affects ALL key-value pairs) or an indexed array of key names (which affects only the keys specified). Defaults to boolean false.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_PutAttributes.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/put_attributes.phps
* Related - <get_attributes()>, <delete_attributes()>
*/
public function put_attributes($domain_name, $item_name, $keypairs, $replace = null, $returnCurlHandle = null)
{
$opt = array();
$opt['DomainName'] = $domain_name;
$opt['ItemName'] = $item_name;
$opt['returnCurlHandle'] = $returnCurlHandle;
$rstore = array();

// Start looping through the keypairs.
$count = 0;
foreach ($keypairs as $k => $v)
{
// Is one of the values an array?
if (is_array($v))
{
// Loop through each of them so that all values are passed as individual attributes.
foreach ($v as $va)
{
$opt['Attribute.' . (string) $count . '.Name'] = $k;
$opt['Attribute.' . (string) $count . '.Value'] = $va;

// Do we want to do replacement?
if ($replace)
{
// Do we have an associative array of key names?
if (is_array($replace))
{
// Store this key-index pair for later.
$rstore[] = array(
'count' => $count,
'key' => $k
);
}
// Or just a since REPLACE ALL?
else
{
$opt['Attribute.' . (string) $count . '.Replace'] = 'true';
}
}

// Increment
$count++;
}
}
else
{
$opt['Attribute.' . (string) $count . '.Name'] = $k;
$opt['Attribute.' . (string) $count . '.Value'] = $v;

// Do we want to do replacement?
if ($replace)
{
// Do we have an associative array of key names?
if (is_array($replace))
{
// Store this key-index pair for later.
$rstore[] = array(
'count' => $count,
'key' => $k
);
}
// Or just a since REPLACE ALL?
else
{
$opt['Attribute.' . (string) $count . '.Replace'] = 'true';
}
}
}

// Increment
$count++;
}

// Go through all of the saved key-index pairs we saved earlier.
foreach ($rstore as $k => $store)
{
// Did we want to replace one of these keypairs?
if (in_array($store['key'], $replace))
{
// Replace!
$opt['Attribute.' . (string) $store['count'] . '.Replace'] = 'true';
}
}

return $this->authenticate('PutAttributes', $opt, $this->hostname);
}

/**
* Method: get_attributes()
* Returns all of the attributes associated with the item. Optionally, the attributes returned can be limited to one or more specified attribute name parameters. If the item does not exist on the replica that was accessed for this operation, an empty set is returned. The system does not return an error as it cannot guarantee the item does not exist on other replicas.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to use for storing data.
* item_name - _string_ (Required) The name of the base item which will contain the series of keypairs.
* keys - _string|array_ (Optional) The name of the key (attribute) in the key-value pair that you want to return. Supports a string value (for single keys) or an indexed array (for multiple keys).
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_GetAttributes.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/get_attributes.phps
* Related - <put_attributes()>, <delete_attributes()>
*/
public function get_attributes($domain_name, $item_name, $keys = null, $returnCurlHandle = null)
{
$opt = array();
$opt['DomainName'] = $domain_name;
$opt['ItemName'] = $item_name;
$opt['returnCurlHandle'] = $returnCurlHandle;

if ($keys)
{
if (is_array($keys))
{
$count = 0;
foreach ($keys as $key)
{
$opt['AttributeName.' . (string) $count] = $key;
$count++;
}
}
else
{
$opt['AttributeName'] = $keys;
}
}

return $this->authenticate('GetAttributes', $opt, $this->hostname);
}

/**
* Method: delete_attributes()
* Deletes one or more attributes associated with the item. If all attributes of an item are deleted, the item is deleted. If you specify DeleteAttributes without attributes or values, all the attributes for the item are deleted. DeleteAttributes is an idempotent operation; running it multiple times on the same item or attribute does not result in an error response.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to use for storing data.
* item_name - _string_ (Required) The name of the base item which will contain the series of keypairs.
* keys - _string|array_ (Optional) The name of the key (attribute) in the key-value pair that you want to delete. Supports a string value (for single keys), an indexed array (for multiple keys), or an associative array containing one or more key-value pairs (for deleting specific values).
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_DeleteAttributes.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/delete_attributes.phps
* Related - <put_attributes()>, <get_attributes()>
*/
public function delete_attributes($domain_name, $item_name, $keys = null, $returnCurlHandle = null)
{
$opt = array();
$opt['DomainName'] = $domain_name;
$opt['ItemName'] = $item_name;
$opt['returnCurlHandle'] = $returnCurlHandle;

// Do we have a key?
if ($keys)
{
// An array?
if (is_array($keys))
{
// Indexed array of Attribute Names?
if ($this->util->ready($keys[0]))
{
for ($x = 0, $i = count($keys); $x < $i; $x++)
{
$opt['Attribute.' . (string) $x . '.Name'] = $keys[$x];
}
}

// Associative array of Name/Value pairs.
else
{
$count = 0;
foreach ($keys as $k => $v)
{
$opt['Attribute.' . (string) $count . '.Name'] = $k;
$opt['Attribute.' . (string) $count . '.Value'] = $v;
$count++;
}
}
}

// Single string Attribute Name.
else
{
$opt['Attribute.Name'] = $keys;
}
}

return $this->authenticate('DeleteAttributes', $opt, $this->hostname);
}


/*%******************************************************************************************%*/
// QUERY

/**
* Method: query()
* Returns a set of ItemNames that match the query expression. Query operations that run longer than 5 seconds will likely time-out and return a time-out error response. A Query with no QueryExpression matches all items in the domain.
*
* The Query operation returns a list of ItemNames that match the query expression. The maximum number that can be returned by one query is determined by MaxNumberOfItems which can be set to number between 1 and 250, inclusive. The default value for MaxNumberOfItems is 100. If more than MaxNumberOfItems items match the query expression, a NextToken is also returned.
*
* Submitting the query again with the NextToken will return the next set of items. To obtain all items matching the query expression, repeat until no NextToken is returned.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to use for storing data.
* opt - _array_ (Optional) Associative array of parameters which can have the following keys:
* expression - _string_ (Optional) The SimpleDB query expression to use.
* follow - _boolean_ (Optional) Whether to take the next step and fetch the items that are returned. This enables very similar functionality to <query_with_attributes()>, except that the response is a bit different and it can return a larger data set. Defaults to false.
*
* Keys for the $opt parameter:
* MaxNumberOfDomains - _integer_ (Optional) The maximum number of domain names you want returned. The range is 1 to 100.
* NextToken - _string_ (Optional) String that tells Amazon SimpleDB where to start the next list of domain names.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_Query.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/query.phps
* Related - <query()>, <query_with_attributes()>, <select()>
*/
public function query($domain_name, $opt = null, $expression = null, $follow = null)
{
if (!$opt) $opt = array();

$opt['DomainName'] = $domain_name;
$opt['QueryExpression'] = $expression;

$query = $this->authenticate('Query', $opt, $this->hostname);

// If $follow is requested, and there's at least one response to follow...
if ($follow && isset($query->body->QueryResult->ItemName))
{
$handles = array();

foreach ($query->body->QueryResult->ItemName as $item)
{
$handles[] = $this->get_attributes($domain_name, $item, null, true);
}

$request = new $this->request_class(null);
return $request->sendMultiRequest($handles);
}

return $query;
}

/**
* Method: query_with_attributes()
* The QueryWithAttributes operation returns a set of Attributes for ItemNames that match the query expression. QueryWithAttributes operations that run longer than 5 seconds will likely time-out and return a time-out error response. A QueryWithAttributes with no QueryExpression matches all items in the domain.
*
* The total size of the response cannot exceed 1 MB in total size. Amazon SimpleDB automatically adjusts the number of items returned per page to enforce this limit. For example, even if you ask to retrieve 250 items, but each individual item is 100 kB in size, the system returns 10 items and an appropriate NextToken to get the next page of results.
*
* Access:
* public
*
* Parameters:
* domain_name - _string_ (Required) The domain name to use for storing data.
* opt - _array_ (Optional) Associative array of parameters which can have the following keys:
* expression - _string_ (Optional) The SimpleDB query expression to use.
*
* Keys for the $opt parameter:
* AttributeName - _string_ (Optional) The name of the attribute to return. To return multiple attributes, you can specify this request parameter multiple times.
* MaxNumberOfDomains - _integer_ (Optional) The maximum number of domain names you want returned. The range is 1 to 100.
* NextToken - _string_ (Optional) String that tells Amazon SimpleDB where to start the next list of domain names.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_QueryWithAttributes.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/query_with_attributes.phps
* Related - <query()>, <query_with_attributes()>, <select()>
*/
public function query_with_attributes($domain_name, $opt = null, $expression = null)
{
if (!$opt) $opt = array();

$opt['DomainName'] = $domain_name;
$opt['QueryExpression'] = $expression;

$query = $this->authenticate('QueryWithAttributes', $opt, $this->hostname);

return $query;
}

/**
* Method: select()
* The Select operation returns a set of Attributes for ItemNames that match the query expression. Select is similar to the standard SQL SELECT statement.
*
* The total size of the response cannot exceed 1 MB in total size. Amazon SimpleDB automatically adjusts the number of items returned per page to enforce this limit. For example, even if you ask to retrieve 250 items, but each individual item is 100 kB in size, the system returns 10 items and an appropriate next token so you can get the next page of results.
*
* Access:
* public
*
* Parameters:
* expression - _string_ (Optional) The SimpleDB select expression to use.
* opt - _array_ (Optional) Associative array of parameters which can have the following keys:
*
* Keys for the $opt parameter:
* NextToken - _string_ (Optional) String that tells Amazon SimpleDB where to start the next list of domain names.
* returnCurlHandle - _boolean_ (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests.
*
* Returns:
* <TarzanHTTPResponse> object
*
* See Also:
* AWS Method - http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_Select.html
* Example Usage - http://tarzan-aws.com/docs/examples/sdb/select.phps
* Related - <query()>, <query_with_attributes()>, <select()>
*/
public function select($expression, $opt = null)
{
if (!$opt) $opt = array();
$opt['SelectExpression'] = $expression;

$query = $this->authenticate('Select', $opt, $this->hostname);

return $query;
}
}
?>
Show details Hide details

Change log

r307 by r...@tarzan-aws.com on Yesterday (25 hours ago)   Diff
Re-forked the trunk from the latest branch
build.
Go to: 
Sign in to write a code review

Older revisions

r301 by r...@tarzan-aws.com on Apr 29, 2009   Diff
Fixes  issue #129 .
r299 by r...@tarzan-aws.com on Apr 29, 2009   Diff
(a) Minor cleanup of classes that
extend TarzanCore::__construct() to
properly return the value that the
constructor returns. (b) Fixes  issue
#134 .
r283 by r...@tarzan-aws.com on Feb 28, 2009   Diff
Updated the copyright date to include
2009. Fixed  issue #110  and applied to
the branch and the trunk.
All revisions of this file

File info

Size: 22844 bytes, 582 lines

File properties

svn:eol-style
native
Hosted by Google Code