|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00030 class Zend_Service_WindowsAzure_Storage_TableEntityQuery 00031 { 00037 protected $_from = ''; 00038 00044 protected $_where = array(); 00045 00051 protected $_orderBy = array(); 00052 00058 protected $_top = null; 00059 00065 protected $_partitionKey = null; 00066 00072 protected $_rowKey = null; 00073 00079 public function select() 00080 { 00081 return $this; 00082 } 00083 00090 public function from($name) 00091 { 00092 $this->_from = $name; 00093 return $this; 00094 } 00095 00102 public function wherePartitionKey($value = null) 00103 { 00104 $this->_partitionKey = $value; 00105 return $this; 00106 } 00107 00114 public function whereRowKey($value = null) 00115 { 00116 $this->_rowKey = $value; 00117 return $this; 00118 } 00119 00128 public function where($condition, $value = null, $cond = '') 00129 { 00130 $condition = $this->_replaceOperators($condition); 00131 00132 if (!is_null($value)) { 00133 $condition = $this->_quoteInto($condition, $value); 00134 } 00135 00136 if (count($this->_where) == 0) { 00137 $cond = ''; 00138 } else if ($cond !== '') { 00139 $cond = ' ' . strtolower(trim($cond)) . ' '; 00140 } 00141 00142 $this->_where[] = $cond . $condition; 00143 return $this; 00144 } 00145 00153 public function andWhere($condition, $value = null) 00154 { 00155 return $this->where($condition, $value, 'and'); 00156 } 00157 00165 public function orWhere($condition, $value = null) 00166 { 00167 return $this->where($condition, $value, 'or'); 00168 } 00169 00177 public function orderBy($column, $direction = 'asc') 00178 { 00179 $this->_orderBy[] = $column . ' ' . $direction; 00180 return $this; 00181 } 00182 00189 public function top($top = null) 00190 { 00191 $this->_top = (int)$top; 00192 return $this; 00193 } 00194 00201 public function assembleQueryString($urlEncode = false) 00202 { 00203 $query = array(); 00204 if (count($this->_where) != 0) { 00205 $filter = implode('', $this->_where); 00206 $query[] = '$filter=' . ($urlEncode ? urlencode($filter) : $filter); 00207 } 00208 00209 if (count($this->_orderBy) != 0) { 00210 $orderBy = implode(',', $this->_orderBy); 00211 $query[] = '$orderby=' . ($urlEncode ? urlencode($orderBy) : $orderBy); 00212 } 00213 00214 if (!is_null($this->_top)) { 00215 $query[] = '$top=' . $this->_top; 00216 } 00217 00218 if (count($query) != 0) { 00219 return '?' . implode('&', $query); 00220 } 00221 00222 return ''; 00223 } 00224 00231 public function assembleFrom($includeParentheses = true) 00232 { 00233 $identifier = ''; 00234 if ($includeParentheses) { 00235 $identifier .= '('; 00236 00237 if (!is_null($this->_partitionKey)) { 00238 $identifier .= 'PartitionKey=\'' . $this->_partitionKey . '\''; 00239 } 00240 00241 if (!is_null($this->_partitionKey) && !is_null($this->_rowKey)) { 00242 $identifier .= ', '; 00243 } 00244 00245 if (!is_null($this->_rowKey)) { 00246 $identifier .= 'RowKey=\'' . $this->_rowKey . '\''; 00247 } 00248 00249 $identifier .= ')'; 00250 } 00251 return $this->_from . $identifier; 00252 } 00253 00259 public function assembleQuery() 00260 { 00261 $assembledQuery = $this->assembleFrom(); 00262 00263 $queryString = $this->assembleQueryString(); 00264 if ($queryString !== '') { 00265 $assembledQuery .= $queryString; 00266 } 00267 00268 return $assembledQuery; 00269 } 00270 00278 protected function _quoteInto($text, $value = null) 00279 { 00280 if (!is_array($value)) { 00281 $text = str_replace('?', '\'' . addslashes($value) . '\'', $text); 00282 } else { 00283 $i = 0; 00284 while(strpos($text, '?') !== false) { 00285 if (is_numeric($value[$i])) { 00286 $text = substr_replace($text, $value[$i++], strpos($text, '?'), 1); 00287 } else { 00288 $text = substr_replace($text, '\'' . addslashes($value[$i++]) . '\'', strpos($text, '?'), 1); 00289 } 00290 } 00291 } 00292 return $text; 00293 } 00294 00301 protected function _replaceOperators($text) 00302 { 00303 $text = str_replace('==', 'eq', $text); 00304 $text = str_replace('>', 'gt', $text); 00305 $text = str_replace('<', 'lt', $text); 00306 $text = str_replace('>=', 'ge', $text); 00307 $text = str_replace('<=', 'le', $text); 00308 $text = str_replace('!=', 'ne', $text); 00309 00310 $text = str_replace('&&', 'and', $text); 00311 $text = str_replace('||', 'or', $text); 00312 $text = str_replace('!', 'not', $text); 00313 00314 return $text; 00315 } 00316 00322 public function __toString() 00323 { 00324 return $this->assembleQuery(); 00325 } 00326 }