Framework  3.9
JSONQuery Class Reference
+ Inheritance diagram for JSONQuery:
+ Collaboration diagram for JSONQuery:

Public Member Functions

 __construct ($class, $constraints="")
 
 groupBy ($groupBy)
 
 execute ()
 
 executeSingle ()
 
 executeRaw ()
 
- Public Member Functions inherited from AbstractQuery
 constraints ($constraints)
 Sets the constraint clause for the Query. More...
 
 filter ($filter)
 Sets a filter to constrain the fields retrieved when the query is executed. More...
 
 params ($params)
 Sets the bound parameters array. More...
 
 bind ()
 Binds placeholders to parameter values. More...
 

Static Public Member Functions

static create ($class, $constraints="")
 

Public Attributes

 $groupBy
 
- Public Attributes inherited from AbstractQuery
 $class
 
 $constraints
 
 $filter
 
 $params
 

Detailed Description

Definition at line 39 of file json_query.inc.

Constructor & Destructor Documentation

◆ __construct()

JSONQuery::__construct (   $class,
  $constraints = "" 
)

Reimplemented from AbstractQuery.

Definition at line 43 of file json_query.inc.

44  {
45  parent::__construct($class, $constraints);
46  $this->groupBy = $groupBy;
47  }
groupBy($groupBy)
Definition: json_query.inc:49

Member Function Documentation

◆ create()

static JSONQuery::create (   $class,
  $constraints = "" 
)
static

Definition at line 132 of file json_query.inc.

133  {
134  return new JSONQuery($class, $constraints);
135  }

◆ execute()

JSONQuery::execute ( )

Reimplemented from AbstractQuery.

Definition at line 55 of file json_query.inc.

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  }

◆ executeRaw()

JSONQuery::executeRaw ( )

Definition at line 96 of file json_query.inc.

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  }
params($params)
Sets the bound parameters array.
Definition: query.inc:91
static getConnection()
Retrieves a reference to the global database connection.
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

◆ executeSingle()

JSONQuery::executeSingle ( )

Definition at line 76 of file json_query.inc.

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  }

◆ groupBy()

JSONQuery::groupBy (   $groupBy)

Definition at line 49 of file json_query.inc.

50  {
51  $this->groupBy = $groupBy;
52  return $this;
53  }

Member Data Documentation

◆ $groupBy

JSONQuery::$groupBy

Definition at line 41 of file json_query.inc.


The documentation for this class was generated from the following file: