Framework  3.9
json_query.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
37 require_once realpath(dirname(__FILE__)."/query.inc");
38 
39 class JSONQuery extends AbstractQuery
40 {
41  var $groupBy;
42 
43  public function __construct($class, $constraints = "")
44  {
45  parent::__construct($class, $constraints);
46  $this->groupBy = $groupBy;
47  }
48 
49  function groupBy($groupBy)
50  {
51  $this->groupBy = $groupBy;
52  return $this;
53  }
54 
55  function execute()
56  {
57  $json = $this->executeRaw();
58  if (!$json) return null;
59 
60  $json = json_decode($json);
61 
62  if (is_object($json)) $json = array($json);
63  $items = array();
64  foreach($json as $jsonItem)
65  {
66  $item = new $this->class;
67  $item->filter = $this->filter;
68  $item->fromJSON($jsonItem);
69 
70  $items[] = $item;
71  }
72 
73  return $items;
74  }
75 
76  function executeSingle()
77  {
78  $json = $this->executeRaw();
79  trace(print_r($json, true), 1);
80  if (!$json) return null;
81 
82  if (is_array($json))
83  {
84  throw new FakoliException("Ambiguous Singleton Query - JSON");
85  }
86 
87  $item = new $this->class;
88  $item->filter = $this->filter;
89  $item->fromJSON($json);
90 
91  trace(print_r($item, true), 1);
92 
93  return $item;
94  }
95 
96  function executeRaw()
97  {
98  $json = null;
99 
100  $query = $this->constraints;
101  if (!startsWith($query, "SELECT ")) $query = "SELECT $query";
102 
103  try
104  {
106 
107  $result = $db->prepare($query);
108  $result->execute($this->params);
109 
110  trace($query, 1);
111 
112  $line = $result->fetch();
113  trace(print_r($line, true), 1);
114 
115  if ($line)
116  {
117  $json = $line[0];
118  }
119 
120  unset($result);
121  }
122  catch(PDOException $e)
123  {
124  $err = "JSONQuery failed - " . $e->getMessage();
125  trace($err, 2);
126  throw new DataItemException($err);
127  }
128 
129  return $json;
130  }
131 
132  static function create($class, $constraints = "")
133  {
134  return new JSONQuery($class, $constraints);
135  }
136 }
params($params)
Sets the bound parameters array.
Definition: query.inc:91
static getConnection()
Retrieves a reference to the global database connection.
executeSingle()
Definition: json_query.inc:76
__construct($class, $constraints="")
Definition: json_query.inc:43
groupBy($groupBy)
Definition: json_query.inc:49
static create($class, $constraints="")
Definition: json_query.inc:132
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
startsWith($text, $start)
Tests whether a string starts with a given sub-string.
Definition: functions.inc:1470