php interface Aspire_Dbo_Statement function __construct initialQuery i

  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
<?php
interface Aspire_Dbo_Statement
{
function __construct($initialQuery, $initialParameters = null);
function exec($parameters = null);
function fetch($fetchType = Aspire_Dbo::FETCH_ASSOC);
function fetchAll($fetchType = Aspire_Dbo::FETCH_ASSOC);
function fetchEntity($sender);
function fetchAllEntitys($sender);
}
class Aspire_Dbo_Mysql_Statement implements Aspire_Dbo_Statement
{
private $initialQuery = null;
private $query = null;
private $queryResult = null;
function __construct($initialQuery, $initialParameters = null)
{
$this->initialQuery = $initialQuery;
if ($initialParameters !== null) {
$this->query = $this->prepareQuery($initialParameters);
}
}
private function prepareQuery($parameters)
{
$resultingQuery = '';
$query = $this->initialQuery;
$qlen = strlen($query);
$lastBlock = 0;
$ptr = 0;
$lastParam = 0;
while ($ptr < $qlen) {
if ($query[$ptr] == ':') {
$ptr++;
$pstart = $ptr;
while($ptr < $qlen && ctype_alnum($query[$ptr])) {
$ptr++;
}
$paramId = substr($query, $pstart, $ptr - $pstart);
if (isset($parameters[$paramId])) {
$pstrLen = strlen($paramId);
$paramValue = $this->parseParameter($parameters[$paramId]);
$resultingQuery .= substr($query, $lastBlock, $ptr - $lastBlock - $pstrLen - 1);
$resultingQuery .= $paramValue;
$lastBlock = $ptr;
} else {
Aspire_Log::log('Undefined parameter ' . $paramId, Aspire_Log::Error);
return false;
}
} else if ($query[$ptr] == '?') {
if ($lastParam <= count($parameters)) {
$paramValue = $this->parseParameter($parameters[$lastParam]);
$resultingQuery .= substr($query, $lastBlock, $ptr - $lastBlock);
$resultingQuery .= $paramValue;
$lastBlock = $ptr + 1;
$lastParam++;
} else {
Aspire_Log::log('Undefined parameter #' . $lastParam, Aspire_Log::Error);
return false;
}
}
$ptr++;
}
return $resultingQuery;
}
private function parseParameter($param)
{
$paramValue = '';
switch ($param[1]) {
case Aspire_Dbo::PARAM_KEY:
$paramValue = '`' . mysql_real_escape_string($param[0]) . '`';
break;
case Aspire_Dbo::PARAM_STR:
$paramValue = '\'' . mysql_real_escape_string($param[0]) . '\'';
break;
case Aspire_Dbo::PARAM_INT:
$paramValue = intval($param[0]);
break;
default:
$paramValue = $param[0];
break;
}
return $paramValue;
}
/**
* Executes query and returns affected rows
*/
function exec($parameters = null)
{
if ($parameters !== null) {
$this->query = $this->prepareQuery($parameters);
}
$this->queryResult = @mysql_query($this->query);
if ($this->queryResult !== false) {
return true;
} else {
Aspire_Log::log('Query failed: ' . $this->query . '; ' . mysql_error(), Aspire_Log::Error);
return false;
}
}
private function getFetchFunction($fetchType)
{
switch ($fetchType)
{
case Aspire_Dbo::FETCH_ARRAY:
return 'mysql_fetch_array';
break;
case Aspire_Dbo::FETCH_OBJECT:
return 'mysql_fetch_object';
break;
default:
return 'mysql_fetch_assoc';
break;
}
}
function fetchAll($fetchType = Aspire_Dbo::FETCH_ASSOC)
{
if (!$this->queryResult) {
return false;
}
$results = array();
$fetchFunc = $this->getFetchFunction($fetchType);
while ($r = $fetchFunc($this->queryResult)) {
$results[] = $r;
}
return $results;
}
function fetch($fetchType = Aspire_Dbo::FETCH_ASSOC)
{
if (!$this->queryResult) {
return false;
}
$fetchFunc = $this->getFetchFunction($fetchType);
return $fetchFunc($this->queryResult);
}
function fetchEntity($sender)
{
if (!$this->queryResult) {
return false;
}
$entityClass = Aspire_Model::getEntityClass($sender);
return new $entityClass($this->fetch(Aspire_Dbo::FETCH_ASSOC));
}
function fetchAllEntitys($sender)
{
if (!$this->queryResult) {
return false;
}
$entityClass = Aspire_Model::getEntityClass($sender);
$results = array();
while ($r = mysql_fetch_assoc($this->queryResult)) {
$results[] = new $entityClass($r);
}
return $results;
}
}