| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
@link |
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fRecordSet |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes |
|---|
| 14 |
|
|---|
| 15 |
class fRecordSet implements Iterator |
|---|
| 16 |
{ |
|---|
| 17 |
const build = 'fRecordSet::build'; |
|---|
| 18 |
const buildFromRecords = 'fRecordSet::buildFromRecords'; |
|---|
| 19 |
const buildFromSQL = 'fRecordSet::buildFromSQL'; |
|---|
| 20 |
const configure = 'fRecordSet::configure'; |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
@var |
|---|
| 27 |
|
|---|
| 28 |
static private $method_callbacks = array(); |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
@link |
|---|
| 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 |
@param @link |
|---|
| 72 |
@param |
|---|
| 73 |
@param |
|---|
| 74 |
@param |
|---|
| 75 |
@param |
|---|
| 76 |
@return @link |
|---|
| 77 |
|
|---|
| 78 |
static public function build($class, $where_conditions=array(), $order_bys=array(), $limit=NULL, $page=NULL) |
|---|
| 79 |
{ |
|---|
| 80 |
self::configure($class); |
|---|
| 81 |
|
|---|
| 82 |
$table = fORM::tablize($class); |
|---|
| 83 |
|
|---|
| 84 |
$sql = "SELECT " . $table . ".* FROM :from_clause"; |
|---|
| 85 |
|
|---|
| 86 |
if ($where_conditions) { |
|---|
| 87 |
$sql .= ' WHERE ' . fORMDatabase::createWhereClause($table, $where_conditions); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
$sql .= ' :group_by_clause '; |
|---|
| 91 |
|
|---|
| 92 |
if ($order_bys) { |
|---|
| 93 |
$sql .= 'ORDER BY ' . fORMDatabase::createOrderByClause($table, $order_bys); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
} else { |
|---|
| 97 |
$primary_keys = fORMSchema::getInstance()->getKeys($table, 'primary'); |
|---|
| 98 |
$expressions = array(); |
|---|
| 99 |
foreach ($primary_keys as $primary_key) { |
|---|
| 100 |
$expressions[] = $table . '.' . $primary_key . ' ASC'; |
|---|
| 101 |
} |
|---|
| 102 |
$sql .= 'ORDER BY ' . join(', ', $expressions); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
$sql = fORMDatabase::insertFromAndGroupByClauses($table, $sql); |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
$non_limited_count_sql = NULL; |
|---|
| 109 |
if ($limit !== NULL) { |
|---|
| 110 |
$primary_key_fields = fORMSchema::getInstance()->getKeys($table, 'primary'); |
|---|
| 111 |
$primary_key_fields = fORMDatabase::addTableToValues($table, $primary_key_fields); |
|---|
| 112 |
|
|---|
| 113 |
$non_limited_count_sql = str_replace('SELECT ' . $table . '.*', 'SELECT ' . join(', ', $primary_key_fields), $sql); |
|---|
| 114 |
$non_limited_count_sql = 'SELECT count(*) FROM (' . $non_limited_count_sql . ') AS sq'; |
|---|
| 115 |
|
|---|
| 116 |
$sql .= ' LIMIT ' . $limit; |
|---|
| 117 |
|
|---|
| 118 |
if ($page !== NULL) { |
|---|
| 119 |
|
|---|
| 120 |
if (!is_numeric($page) || $page < 1) { |
|---|
| 121 |
fCore::toss( |
|---|
| 122 |
'fProgrammerException', |
|---|
| 123 |
fGrammar::compose( |
|---|
| 124 |
'The page specified, %s, is not a number or less than one', |
|---|
| 125 |
fCore::dump($page) |
|---|
| 126 |
) |
|---|
| 127 |
); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
$sql .= ' OFFSET ' . (($page-1) * $limit); |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
return new fRecordSet($class, fORMDatabase::getInstance()->translatedQuery($sql), $non_limited_count_sql); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
@link |
|---|
| 140 |
|
|---|
| 141 |
@throws |
|---|
| 142 |
@internal |
|---|
| 143 |
|
|---|
| 144 |
@param |
|---|
| 145 |
@param |
|---|
| 146 |
@return @link |
|---|
| 147 |
|
|---|
| 148 |
static public function buildFromRecords($class, $records) |
|---|
| 149 |
{ |
|---|
| 150 |
$record_set = new fRecordSet($class); |
|---|
| 151 |
$record_set->records = $records; |
|---|
| 152 |
return $record_set; |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
@link |
|---|
| 158 |
|
|---|
| 159 |
@param |
|---|
| 160 |
@param |
|---|
| 161 |
@param |
|---|
| 162 |
@return @link |
|---|
| 163 |
|
|---|
| 164 |
static public function buildFromSQL($class, $sql, $non_limited_count_sql=NULL) |
|---|
| 165 |
{ |
|---|
| 166 |
self::configure($class); |
|---|
| 167 |
|
|---|
| 168 |
$result = fORMDatabase::getInstance()->translatedQuery($sql); |
|---|
| 169 |
return new fRecordSet($class, $result, $non_limited_count_sql); |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
@link @link |
|---|
| 175 |
|
|---|
| 176 |
@param |
|---|
| 177 |
@return |
|---|
| 178 |
|
|---|
| 179 |
static public function configure($class) |
|---|
| 180 |
{ |
|---|
| 181 |
if (!fORM::isConfigured($class)) { |
|---|
| 182 |
new $class(); |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
@param |
|---|
| 198 |
@param |
|---|
| 199 |
@return |
|---|
| 200 |
|
|---|
| 201 |
static public function registerMethodCallback($method, $callback) |
|---|
| 202 |
{ |
|---|
| 203 |
self::$method_callbacks[$method] = $callback; |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
@link |
|---|
| 209 |
|
|---|
| 210 |
@var |
|---|
| 211 |
|
|---|
| 212 |
private $associate = FALSE; |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
@var |
|---|
| 218 |
|
|---|
| 219 |
private $class = NULL; |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
@var |
|---|
| 225 |
|
|---|
| 226 |
private $non_limited_count = NULL; |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
@var |
|---|
| 232 |
|
|---|
| 233 |
private $non_limited_count_sql = NULL; |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
@var |
|---|
| 239 |
|
|---|
| 240 |
private $pointer = 0; |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
@var |
|---|
| 246 |
|
|---|
| 247 |
private $records = array(); |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
@throws |
|---|
| 254 |
|
|---|
| 255 |
@param |
|---|
| 256 |
@param |
|---|
| 257 |
@return |
|---|
| 258 |
|
|---|
| 259 |
public function __call($method_name, $parameters) |
|---|
| 260 |
{ |
|---|
| 261 |
list($action, $element) = fORM::parseMethod($method_name); |
|---|
| 262 |
|
|---|
| 263 |
if (isset(self::$method_callbacks[$method_name])) { |
|---|
| 264 |
return call_user_func_array( |
|---|
| 265 |
self::$method_callbacks[$method_name], |
|---|
| 266 |
array( |
|---|
| 267 |
$this, |
|---|
| 268 |
$this->class, |
|---|
| 269 |
&$this->records, |
|---|
| 270 |
&$this->pointer, |
|---|
| 271 |
&$this->associate |
|---|
| 272 |
) |
|---|
| 273 |
); |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
switch ($action) { |
|---|
| 277 |
case 'build': |
|---|
| 278 |
$element = fGrammar::camelize($element, TRUE); |
|---|
| 279 |
$element = fGrammar::singularize($element); |
|---|
| 280 |
return $this->preloadRecords($element, ($parameters != array()) ? $parameters[0] : NULL); |
|---|
| 281 |
|
|---|
| 282 |
case 'count': |
|---|
| 283 |
$element = fGrammar::camelize($element, TRUE); |
|---|
| 284 |
$element = fGrammar::singularize($element); |
|---|
| 285 |
return $this->preloadCounts($element, ($parameters != array()) ? $parameters[0] : NULL); |
|---|
| 286 |
} |
|---|
| 287 |
|
|---|
| 288 |
fCore::toss('fProgrammerException', 'Unknown method, ' . $method_name . '(), called'); |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
@param |
|---|
| 296 |
@param @link |
|---|
| 297 |
@param |
|---|
| 298 |
@return |
|---|
| 299 |
|
|---|
| 300 |
protected function __construct($class, fResult $result_object=NULL, $non_limited_count_sql=NULL) |
|---|
| 301 |
{ |
|---|
| 302 |
if (!class_exists($class)) { |
|---|
| 303 |
fCore::toss( |
|---|
| 304 |
'fProgrammerException', |
|---|
| 305 |
fGrammar::compose( |
|---|
| 306 |
'The class specified, %s, could not be loaded', |
|---|
| 307 |
fCore::dump($class) |
|---|
| 308 |
) |
|---|
| 309 |
); |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
if (!is_subclass_of($class, 'fActiveRecord')) { |
|---|
| 313 |
fCore::toss( |
|---|
| 314 |
'fProgrammerException', |
|---|
| 315 |
fGrammar::compose( |
|---|
| 316 |
'The class specified, %1$s, does not extend %2$s. All classes used with %3$s must extend %4$s.', |
|---|
| 317 |
fCore::dump($class), |
|---|
| 318 |
'fActiveRecord', |
|---|
| 319 |
'fRecordSet', |
|---|
| 320 |
'fActiveRecord' |
|---|
| 321 |
) |
|---|
| 322 |
); |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
$this->class = $class; |
|---|
| 326 |
$this->non_limited_count_sql = $non_limited_count_sql; |
|---|
| 327 |
|
|---|
| 328 |
while ($result_object && $result_object->valid()) { |
|---|
| 329 |
$this->records[] = new $class($result_object); |
|---|
| 330 |
$result_object->next(); |
|---|
| 331 |
} |
|---|
| 332 |
} |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
@param |
|---|
| 339 |
@return |
|---|
| 340 |
|
|---|
| 341 |
public function __get($method) |
|---|
| 342 |
{ |
|---|
| 343 |
return array($this, $method); |
|---|
| 344 |
} |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
@return |
|---|
| 351 |
|
|---|
| 352 |
public function call($method) |
|---|
| 353 |
{ |
|---|
| 354 |
$output = array(); |
|---|
| 355 |
foreach ($this->records as $record) { |
|---|
| 356 |
$output[] = $record->$method(); |
|---|
| 357 |
} |
|---|
| 358 |
return $output; |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
@param |
|---|
| 366 |
@return |
|---|
| 367 |
|
|---|
| 368 |
private function constructOrderByClause($route=NULL) |
|---|
| 369 |
{ |
|---|
| 370 |
$table = fORM::tablize($this->class); |
|---|
| 371 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 372 |
|
|---|
| 373 |
$pk_columns = fORMSchema::getInstance()->getKeys($table, 'primary'); |
|---|
| 374 |
$first_pk_column = $pk_columns[0]; |
|---|
| 375 |
|
|---|
| 376 |
$sql = ''; |
|---|
| 377 |
|
|---|
| 378 |
$number = 0; |
|---|
| 379 |
foreach ($this->getPrimaryKeys() as $primary_key) { |
|---|
| 380 |
$sql .= 'WHEN '; |
|---|
| 381 |
|
|---|
| 382 |
if (is_array($primary_key)) { |
|---|
| 383 |
$conditions = array(); |
|---|
| 384 |
foreach ($pk_columns as $pk_column) { |
|---|
| 385 |
$conditions[] = $table_with_route . '.' . $pk_column . fORMDatabase::escapeBySchema($table, $pk_column, $primary_key[$pk_column], '='); |
|---|
| 386 |
} |
|---|
| 387 |
$sql .= join(' AND ', $conditions); |
|---|
| 388 |
} else { |
|---|
| 389 |
$sql .= $table_with_route . '.' . $first_pk_column . fORMDatabase::escapeBySchema($table, $first_pk_column, $primary_key, '='); |
|---|
| 390 |
} |
|---|
| 391 |
|
|---|
| 392 |
$sql .= ' THEN ' . $number . ' '; |
|---|
| 393 |
|
|---|
| 394 |
$number++; |
|---|
| 395 |
} |
|---|
| 396 |
|
|---|
| 397 |
return 'CASE ' . $sql . 'END ASC'; |
|---|
| 398 |
} |
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
@param |
|---|
| 405 |
@return |
|---|
| 406 |
|
|---|
| 407 |
private function constructWhereClause($route=NULL) |
|---|
| 408 |
{ |
|---|
| 409 |
$table = fORM::tablize($this->class); |
|---|
| 410 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 411 |
|
|---|
| 412 |
$pk_columns = fORMSchema::getInstance()->getKeys($table, 'primary'); |
|---|
| 413 |
|
|---|
| 414 |
$sql = ''; |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
if (sizeof($pk_columns) > 1) { |
|---|
| 418 |
|
|---|
| 419 |
$conditions = array(); |
|---|
| 420 |
|
|---|
| 421 |
foreach ($this->getPrimaryKeys() as $primary_key) { |
|---|
| 422 |
$sub_conditions = array(); |
|---|
| 423 |
foreach ($pk_columns as $pk_column) { |
|---|
| 424 |
$sub_conditions[] = $table_with_route . '.' . $pk_column . fORMDatabase::escapeBySchema($table, $pk_column, $primary_key[$pk_column], '='); |
|---|
| 425 |
} |
|---|
| 426 |
$conditions[] = join(' AND ', $sub_conditions); |
|---|
| 427 |
} |
|---|
| 428 |
$sql .= '(' . join(') OR (', $conditions) . ')'; |
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
} else { |
|---|
| 432 |
$first_pk_column = $pk_columns[0]; |
|---|
| 433 |
|
|---|
| 434 |
$values = array(); |
|---|
| 435 |
foreach ($this->getPrimaryKeys() as $primary_key) { |
|---|
| 436 |
$values[] = fORMDatabase::escapeBySchema($table, $first_pk_column, $primary_key); |
|---|
| 437 |
} |
|---|
| 438 |
$sql .= $table_with_route . '.' . $first_pk_column . ' IN (' . join(', ', $values) . ')'; |
|---|
| 439 |
} |
|---|
| 440 |
|
|---|
| 441 |
return $sql; |
|---|
| 442 |
} |
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
@return |
|---|
| 449 |
|
|---|
| 450 |
public function count() |
|---|
| 451 |
{ |
|---|
| 452 |
return sizeof($this->records); |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
@return |
|---|
| 460 |
|
|---|
| 461 |
public function countWithoutLimit() |
|---|
| 462 |
{ |
|---|
| 463 |
|
|---|
| 464 |
if ($this->non_limited_count_sql === NULL) { |
|---|
| 465 |
return $this->count(); |
|---|
| 466 |
} |
|---|
| 467 |
|
|---|
| 468 |
if ($this->non_limited_count === NULL) { |
|---|
| 469 |
try { |
|---|
| 470 |
$this->non_limited_count = fORMDatabase::getInstance()->translatedQuery($this->non_limited_count_sql)->fetchScalar(); |
|---|
| 471 |
} catch (fExpectedException $e) { |
|---|
| 472 |
$this->non_limited_count = $this->count(); |
|---|
| 473 |
} |
|---|
| 474 |
} |
|---|
| 475 |
return $this->non_limited_count; |
|---|
| 476 |
} |
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
@throws |
|---|
| 483 |
@internal |
|---|
| 484 |
|
|---|
| 485 |
@return |
|---|
| 486 |
|
|---|
| 487 |
public function current() |
|---|
| 488 |
{ |
|---|
| 489 |
if (!$this->valid()) { |
|---|
| 490 |
fCore::toss( |
|---|
| 491 |
'fProgrammerException', |
|---|
| 492 |
fGrammar::compose('There are no remaining records') |
|---|
| 493 |
); |
|---|
| 494 |
} |
|---|
| 495 |
|
|---|
| 496 |
return $this->records[$this->pointer]; |
|---|
| 497 |
} |
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
@param |
|---|
| 504 |
@return |
|---|
| 505 |
|
|---|
| 506 |
public function filter($callback) |
|---|
| 507 |
{ |
|---|
| 508 |
if (!$this->records) { |
|---|
| 509 |
return clone $this; |
|---|
| 510 |
} |
|---|
| 511 |
|
|---|
| 512 |
$call_filter = FALSE; |
|---|
| 513 |
if (preg_match('#^\{record\}::([a-z0-9_\-]+)$#i', $callback, $matches)) { |
|---|
| 514 |
$call_filter = TRUE; |
|---|
| 515 |
$method = $matches[1]; |
|---|
| 516 |
} |
|---|
| 517 |
|
|---|
| 518 |
$new_records = array(); |
|---|
| 519 |
foreach ($this->records as $record) { |
|---|
| 520 |
if ($call_filter) { |
|---|
| 521 |
$value = $record->$method(); |
|---|
| 522 |
} else { |
|---|
| 523 |
$value = call_user_func($callback, $record); |
|---|
| 524 |
} |
|---|
| 525 |
if ($value) { |
|---|
| 526 |
$new_records[] = $record; |
|---|
| 527 |
} |
|---|
| 528 |
} |
|---|
| 529 |
|
|---|
| 530 |
return self::buildFromRecords($this->class, $new_records); |
|---|
| 531 |
} |
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
@link |
|---|
| 536 |
|
|---|
| 537 |
@internal |
|---|
| 538 |
|
|---|
| 539 |
@return |
|---|
| 540 |
|
|---|
| 541 |
public function flagAssociate() |
|---|
| 542 |
{ |
|---|
| 543 |
$this->associate = TRUE; |
|---|
| 544 |
} |
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
@throws |
|---|
| 551 |
|
|---|
| 552 |
@return |
|---|
| 553 |
|
|---|
| 554 |
public function fetchRecord() |
|---|
| 555 |
{ |
|---|
| 556 |
try { |
|---|
| 557 |
$record = $this->current(); |
|---|
| 558 |
$this->next(); |
|---|
| 559 |
return $record; |
|---|
| 560 |
} catch (fValidationException $e) { |
|---|
| 561 |
throw $e; |
|---|
| 562 |
} catch (fExpectedException $e) { |
|---|
| 563 |
fCore::toss( |
|---|
| 564 |
'fNoRemainingException', |
|---|
| 565 |
fGrammar::compose('There are no remaining records') |
|---|
| 566 |
); |
|---|
| 567 |
} |
|---|
| 568 |
} |
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
@return |
|---|
| 575 |
|
|---|
| 576 |
public function getClass() |
|---|
| 577 |
{ |
|---|
| 578 |
return $this->class; |
|---|
| 579 |
} |
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
@throws |
|---|
| 586 |
|
|---|
| 587 |
@return |
|---|
| 588 |
|
|---|
| 589 |
public function getRecords() |
|---|
| 590 |
{ |
|---|
| 591 |
return $this->records; |
|---|
| 592 |
} |
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
@throws |
|---|
| 599 |
|
|---|
| 600 |
@return |
|---|
| 601 |
|
|---|
| 602 |
public function getPrimaryKeys() |
|---|
| 603 |
{ |
|---|
| 604 |
$table = fORM::tablize($this->class); |
|---|
| 605 |
$pk_columns = fORMSchema::getInstance()->getKeys($table, 'primary'); |
|---|
| 606 |
$first_pk_column = $pk_columns[0]; |
|---|
| 607 |
|
|---|
| 608 |
$primary_keys = array(); |
|---|
| 609 |
|
|---|
| 610 |
foreach ($this->records as $number => $record) { |
|---|
| 611 |
$keys = array(); |
|---|
| 612 |
|
|---|
| 613 |
foreach ($pk_columns as $pk_column) { |
|---|
| 614 |
$method = 'get' . fGrammar::camelize($pk_column, TRUE); |
|---|
| 615 |
$keys[$pk_column] = $record->$method(); |
|---|
| 616 |
} |
|---|
| 617 |
|
|---|
| 618 |
$primary_keys[$number] = (sizeof($pk_columns) == 1) ? $keys[$first_pk_column] : $keys; |
|---|
| 619 |
} |
|---|
| 620 |
|
|---|
| 621 |
return $primary_keys; |
|---|
| 622 |
} |
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 |
@link |
|---|
| 627 |
|
|---|
| 628 |
@internal |
|---|
| 629 |
|
|---|
| 630 |
@return |
|---|
| 631 |
|
|---|
| 632 |
public function isFlaggedForAssociation() |
|---|
| 633 |
{ |
|---|
| 634 |
return $this->associate; |
|---|
| 635 |
} |
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
@internal |
|---|
| 642 |
|
|---|
| 643 |
@return |
|---|
| 644 |
|
|---|
| 645 |
public function key() |
|---|
| 646 |
{ |
|---|
| 647 |
return $this->pointer; |
|---|
| 648 |
} |
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
|
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
@param |
|---|
| 673 |
@param |
|---|
| 674 |
@return |
|---|
| 675 |
|
|---|
| 676 |
public function map($callback) |
|---|
| 677 |
{ |
|---|
| 678 |
$parameters = array_slice(func_get_args(), 1); |
|---|
| 679 |
|
|---|
| 680 |
if (!$this->records) { |
|---|
| 681 |
return array(); |
|---|
| 682 |
} |
|---|
| 683 |
|
|---|
| 684 |
$parameters_array = array(); |
|---|
| 685 |
$found_record = FALSE; |
|---|
| 686 |
$total_records = sizeof($this->records); |
|---|
| 687 |
|
|---|
| 688 |
foreach ($parameters as $parameter) { |
|---|
| 689 |
if (!is_array($parameter)) { |
|---|
| 690 |
if (preg_match('#^\{record\}::([a-z0-9_\-]+)$#i', $parameter, $matches)) { |
|---|
| 691 |
$parameters_array[] = $this->call($matches[1]); |
|---|
| 692 |
$found_record = TRUE; |
|---|
| 693 |
} elseif ($parameter == '{record}') { |
|---|
| 694 |
$parameters_array[] = $this->records; |
|---|
| 695 |
$found_record = TRUE; |
|---|
| 696 |
} else { |
|---|
| 697 |
$parameters_array[] = array_pad(array(), $total_records, $parameter); |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
} elseif (sizeof($parameter) > $total_records) { |
|---|
| 701 |
$parameters_array[] = array_slice($parameter, 0, $total_records); |
|---|
| 702 |
} elseif (sizeof($parameter) < $total_records) { |
|---|
| 703 |
$parameters_array[] = array_pad($parameter, $total_records, NULL); |
|---|
| 704 |
} else { |
|---|
| 705 |
$parameters_array[] = $parameter; |
|---|
| 706 |
} |
|---|
| 707 |
} |
|---|
| 708 |
|
|---|
| 709 |
if (!$found_record) { |
|---|
| 710 |
array_unshift($parameters_array, $this->records); |
|---|
| 711 |
} |
|---|
| 712 |
|
|---|
| 713 |
array_unshift($parameters_array, $callback); |
|---|
| 714 |
|
|---|
| 715 |
return call_user_func_array('array_map', $parameters_array); |
|---|
| 716 |
} |
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
@internal |
|---|
| 723 |
|
|---|
| 724 |
@return |
|---|
| 725 |
|
|---|
| 726 |
public function next() |
|---|
| 727 |
{ |
|---|
| 728 |
$this->pointer++; |
|---|
| 729 |
} |
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
@throws |
|---|
| 736 |
|
|---|
| 737 |
@param |
|---|
| 738 |
@param |
|---|
| 739 |
@return |
|---|
| 740 |
|
|---|
| 741 |
private function preloadCounts($related_class, $route=NULL) |
|---|
| 742 |
{ |
|---|
| 743 |
|
|---|
| 744 |
if (!array_merge($this->getPrimaryKeys())) { |
|---|
| 745 |
return; |
|---|
| 746 |
} |
|---|
| 747 |
|
|---|
| 748 |
$related_table = fORM::tablize($related_class); |
|---|
| 749 |
$table = fORM::tablize($this->class); |
|---|
| 750 |
|
|---|
| 751 |
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many'); |
|---|
| 752 |
$relationship = fORMSchema::getRoute($table, $related_table, $route, '*-to-many'); |
|---|
| 753 |
|
|---|
| 754 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 755 |
|
|---|
| 756 |
|
|---|
| 757 |
$where_sql = $this->constructWhereClause($route); |
|---|
| 758 |
$order_by_sql = $this->constructOrderByClause($route); |
|---|
| 759 |
|
|---|
| 760 |
$related_table_keys = fORMSchema::getInstance()->getKeys($related_table, 'primary'); |
|---|
| 761 |
$related_table_keys = fORMDatabase::addTableToValues($related_table, $related_table_keys); |
|---|
| 762 |
$related_table_keys = join(', ', $related_table_keys); |
|---|
| 763 |
|
|---|
| 764 |
$column = $table_with_route . '.' . $relationship['column']; |
|---|
| 765 |
|
|---|
| 766 |
$new_sql = 'SELECT count(' . $related_table_keys . ') AS __flourish_count, ' . $column . ' AS __flourish_column '; |
|---|
| 767 |
$new_sql .= ' FROM :from_clause '; |
|---|
| 768 |
$new_sql .= ' WHERE ' . $where_sql; |
|---|
| 769 |
$new_sql .= ' GROUP BY ' . $column; |
|---|
| 770 |
$new_sql .= ' ORDER BY ' . $column . ' ASC'; |
|---|
| 771 |
|
|---|
| 772 |
$new_sql = fORMDatabase::insertFromAndGroupByClauses($related_table, $new_sql); |
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
$result = fORMDatabase::getInstance()->translatedQuery($new_sql); |
|---|
| 776 |
|
|---|
| 777 |
$counts = array(); |
|---|
| 778 |
foreach ($result as $row) { |
|---|
| 779 |
$counts[$row['__flourish_column']] = (int) $row['__flourish_count']; |
|---|
| 780 |
} |
|---|
| 781 |
|
|---|
| 782 |
unset($result); |
|---|
| 783 |
|
|---|
| 784 |
$total_records = sizeof($this->records); |
|---|
| 785 |
$get_method = 'get' . fGrammar::camelize($relationship['column'], TRUE); |
|---|
| 786 |
$tally_method = 'tally' . fGrammar::pluralize($related_class); |
|---|
| 787 |
|
|---|
| 788 |
for ($i=0; $i < $total_records; $i++) { |
|---|
| 789 |
$record = $this->records[$i]; |
|---|
| 790 |
$count = (isset($counts[$record->$get_method()])) ? $counts[$record->$get_method()] : 0; |
|---|
| 791 |
$record->$tally_method($count, $route); |
|---|
| 792 |
} |
|---|
| 793 |
} |
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
@throws |
|---|
| 800 |
|
|---|
| 801 |
@param |
|---|
| 802 |
@param |
|---|
| 803 |
@return |
|---|
| 804 |
|
|---|
| 805 |
private function preloadRecords($related_class, $route=NULL) |
|---|
| 806 |
{ |
|---|
| 807 |
|
|---|
| 808 |
if (!array_merge($this->getPrimaryKeys())) { |
|---|
| 809 |
return; |
|---|
| 810 |
} |
|---|
| 811 |
|
|---|
| 812 |
$related_table = fORM::tablize($related_class); |
|---|
| 813 |
$table = fORM::tablize($this->class); |
|---|
| 814 |
|
|---|
| 815 |
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many'); |
|---|
| 816 |
$relationship = fORMSchema::getRoute($table, $related_table, $route, '*-to-many'); |
|---|
| 817 |
|
|---|
| 818 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 819 |
|
|---|
| 820 |
|
|---|
| 821 |
$where_sql = $this->constructWhereClause($route); |
|---|
| 822 |
|
|---|
| 823 |
$order_by_sql = $this->constructOrderByClause($route); |
|---|
| 824 |
if ($related_order_bys = fORMRelated::getOrderBys($this->class, $related_class, $route)) { |
|---|
| 825 |
$order_by_sql .= ', ' . fORMDatabase::createOrderByClause($related_table, $related_order_bys); |
|---|
| 826 |
} |
|---|
| 827 |
|
|---|
| 828 |
$new_sql = 'SELECT ' . $related_table . '.*'; |
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
if (isset($relationship['join_table'])) { |
|---|
| 832 |
$new_sql .= ", " . $table_with_route . '.' . $relationship['column']; |
|---|
| 833 |
} |
|---|
| 834 |
|
|---|
| 835 |
$new_sql .= ' FROM :from_clause '; |
|---|
| 836 |
$new_sql .= ' WHERE ' . $where_sql; |
|---|
| 837 |
$new_sql .= ' :group_by_clause '; |
|---|
| 838 |
$new_sql .= ' ORDER BY ' . $order_by_sql; |
|---|
| 839 |
|
|---|
| 840 |
$new_sql = fORMDatabase::insertFromAndGroupByClauses($related_table, $new_sql); |
|---|
| 841 |
|
|---|
| 842 |
|
|---|
| 843 |
if (strpos($new_sql, 'GROUP BY') !== FALSE) { |
|---|
| 844 |
$new_sql = str_replace(' ORDER BY', ', ' . $table . '.' . $relationship['column'] . ' ORDER BY', $new_sql); |
|---|
| 845 |
} |
|---|
| 846 |
|
|---|
| 847 |
|
|---|
| 848 |
|
|---|
| 849 |
$result = fORMDatabase::getInstance()->translatedQuery($new_sql); |
|---|
| 850 |
|
|---|
| 851 |
$total_records = sizeof($this->records); |
|---|
| 852 |
for ($i=0; $i < $total_records; $i++) { |
|---|
| 853 |
|
|---|
| 854 |
|
|---|
| 855 |
|
|---|
| 856 |
$record = $this->records[$i]; |
|---|
| 857 |
$keys = array(); |
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
|
|---|
| 861 |
if (isset($relationship['join_table'])) { |
|---|
| 862 |
try { |
|---|
| 863 |
$current_row = $result->current(); |
|---|
| 864 |
$keys[$relationship['column']] = $current_row[$relationship['column']]; |
|---|
| 865 |
} catch (fExpectedException $e) { } |
|---|
| 866 |
|
|---|
| 867 |
|
|---|
| 868 |
} else { |
|---|
| 869 |
$method = 'get' . fGrammar::camelize($relationship['related_column'], TRUE); |
|---|
| 870 |
$keys[$relationship['related_column']] = $record->$method(); |
|---|
| 871 |
} |
|---|
| 872 |
|
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
$rows = array(); |
|---|
| 876 |
|
|---|
| 877 |
try { |
|---|
| 878 |
while (!array_diff_assoc($keys, $result->current())) { |
|---|
| 879 |
$row = $result->fetchRow(); |
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 |
if (isset($relationship['join_table'])) { |
|---|
| 883 |
unset($row[$relationship['column']]); |
|---|
| 884 |
} |
|---|
| 885 |
|
|---|
| 886 |
$rows[] = $row; |
|---|
| 887 |
} |
|---|
| 888 |
} catch (fExpectedException $e) { } |
|---|
| 889 |
|
|---|
| 890 |
|
|---|
| 891 |
|
|---|
| 892 |
$method = 'get' . fGrammar::camelize($relationship['column'], TRUE); |
|---|
| 893 |
|
|---|
| 894 |
$sql = "SELECT " . $related_table . ".* FROM :from_clause"; |
|---|
| 895 |
|
|---|
| 896 |
$where_conditions = array( |
|---|
| 897 |
$table_with_route . '.' . $relationship['column'] . '=' => $record->$method() |
|---|
| 898 |
); |
|---|
| 899 |
$sql .= ' WHERE ' . fORMDatabase::createWhereClause($related_table, $where_conditions); |
|---|
| 900 |
|
|---|
| 901 |
$sql .= ' :group_by_clause '; |
|---|
| 902 |
|
|---|
| 903 |
if ($order_bys = fORMRelated::getOrderBys($this->class, $related_class, $route)) { |
|---|
| 904 |
$sql .= ' ORDER BY ' . fORMDatabase::createOrderByClause($related_table, $order_bys); |
|---|
| 905 |
} |
|---|
| 906 |
|
|---|
| 907 |
$sql = fORMDatabase::insertFromAndGroupByClauses($related_table, $sql); |
|---|
| 908 |
|
|---|
| 909 |
|
|---|
| 910 |
|
|---|
| 911 |
$injected_result = new fResult(fORMDatabase::getInstance()->getType(), 'array'); |
|---|
| 912 |
$injected_result->setSQL($sql); |
|---|
| 913 |
$injected_result->setResult($rows); |
|---|
| 914 |
$injected_result->setReturnedRows(sizeof($rows)); |
|---|
| 915 |
$injected_result->setAffectedRows(0); |
|---|
| 916 |
$injected_result->setAutoIncrementedValue(NULL); |
|---|
| 917 |
|
|---|
| 918 |
$set = new fRecordSet($related_class, $injected_result); |
|---|
| 919 |
|
|---|
| 920 |
|
|---|
| 921 |
|
|---|
| 922 |
$method = 'inject' . fGrammar::pluralize($related_class); |
|---|
| 923 |
$record->$method($set, $route); |
|---|
| 924 |
} |
|---|
| 925 |
} |
|---|
| 926 |
|
|---|
| 927 |
|
|---|
| 928 |
|
|---|
| 929 |
|
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 |
|
|---|
| 933 |
|
|---|
| 934 |
|
|---|
| 935 |
|
|---|
| 936 |
@param |
|---|
| 937 |
@param |
|---|
| 938 |
@return |
|---|
| 939 |
|
|---|
| 940 |
public function reduce($callback, $inital_value=NULL) |
|---|
| 941 |
{ |
|---|
| 942 |
if (!$this->records) { |
|---|
| 943 |
return $initial_value; |
|---|
| 944 |
} |
|---|
| 945 |
|
|---|
| 946 |
$values = $this->records; |
|---|
| 947 |
if ($inital_value === NULL) { |
|---|
| 948 |
$result = $values[0]; |
|---|
| 949 |
$values = array_slice($values, 1); |
|---|
| 950 |
} else { |
|---|
| 951 |
$result = $inital_value; |
|---|
| 952 |
} |
|---|
| 953 |
|
|---|
| 954 |
foreach($values as $value) { |
|---|
| 955 |
$result = call_user_func($callback, $result, $value); |
|---|
| 956 |
} |
|---|
| 957 |
|
|---|
| 958 |
return $result; |
|---|
| 959 |
} |
|---|
| 960 |
|
|---|
| 961 |
|
|---|
| 962 |
|
|---|
| 963 |
|
|---|
| 964 |
|
|---|
| 965 |
@internal |
|---|
| 966 |
|
|---|
| 967 |
@return |
|---|
| 968 |
|
|---|
| 969 |
public function rewind() |
|---|
| 970 |
{ |
|---|
| 971 |
$this->pointer = 0; |
|---|
| 972 |
} |
|---|
| 973 |
|
|---|
| 974 |
|
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 |
|
|---|
| 978 |
@link |
|---|
| 979 |
|
|---|
| 980 |
@throws |
|---|
| 981 |
|
|---|
| 982 |
@param |
|---|
| 983 |
@param |
|---|
| 984 |
@return |
|---|
| 985 |
|
|---|
| 986 |
public function sort($method, $direction) |
|---|
| 987 |
{ |
|---|
| 988 |
if (!in_array($direction, array('asc', 'desc'))) { |
|---|
| 989 |
fCore::toss( |
|---|
| 990 |
'fProgrammerException', |
|---|
| 991 |
fGrammar::compose( |
|---|
| 992 |
'The sort direction specified, %1$s, is invalid. Must be one of: %2$s or %3$s.', |
|---|
| 993 |
fCore::dump($direction), |
|---|
| 994 |
'asc', |
|---|
| 995 |
'desc' |
|---|
| 996 |
) |
|---|
| 997 |
); |
|---|
| 998 |
} |
|---|
| 999 |
|
|---|
| 1000 |
|
|---|
| 1001 |
$lambda_params = '$a,$b'; |
|---|
| 1002 |
$lambda_funcs = array( |
|---|
| 1003 |
'asc' => 'return fUTF8::inatcmp($a->' . $method . '(), $b->' . $method . '());', |
|---|
| 1004 |
'desc' => 'return fUTF8::inatcmp($b->' . $method . '(), $a->' . $method . '());' |
|---|
| 1005 |
); |
|---|
| 1006 |
|
|---|
| 1007 |
$this->sortByCallback(create_function($lambda_params, $lambda_funcs[$direction])); |
|---|
| 1008 |
} |
|---|
| 1009 |
|
|---|
| 1010 |
|
|---|
| 1011 |
|
|---|
| 1012 |
@link http://php.net/usort |
|---|
| 1013 |
|
|---|
| 1014 |
@throws |
|---|
| 1015 |
|
|---|
| 1016 |
@param |
|---|
| 1017 |
@return |
|---|
| 1018 |
|
|---|
| 1019 |
public function sortByCallback($callback) |
|---|
| 1020 |
{ |
|---|
| 1021 |
usort($this->records, $callback); |
|---|
| 1022 |
$this->rewind(); |
|---|
| 1023 |
} |
|---|
| 1024 |
|
|---|
| 1025 |
|
|---|
| 1026 |
|
|---|
| 1027 |
@link @link |
|---|
| 1028 |
|
|---|
| 1029 |
@throws |
|---|
| 1030 |
|
|---|
| 1031 |
@return |
|---|
| 1032 |
|
|---|
| 1033 |
public function tossIfEmpty() |
|---|
| 1034 |
{ |
|---|
| 1035 |
if (!$this->count()) { |
|---|
| 1036 |
fCore::toss( |
|---|
| 1037 |
'fEmptySetException', |
|---|
| 1038 |
fGrammar::compose( |
|---|
| 1039 |
'No %s could be found', |
|---|
| 1040 |
fGrammar::pluralize(fORM::getRecordName($this->class)) |
|---|
| 1041 |
) |
|---|
| 1042 |
); |
|---|
| 1043 |
} |
|---|
| 1044 |
} |
|---|
| 1045 |
|
|---|
| 1046 |
|
|---|
| 1047 |
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 |
@internal |
|---|
| 1051 |
|
|---|
| 1052 |
@return |
|---|
| 1053 |
|
|---|
| 1054 |
public function valid() |
|---|
| 1055 |
{ |
|---|
| 1056 |
return $this->pointer < $this->count(); |
|---|
| 1057 |
} |
|---|
| 1058 |
} |
|---|
| 1059 |
|
|---|
| 1060 |
|
|---|
| 1061 |
|
|---|
| 1062 |
|
|---|
| 1063 |
will@flourishlib.com |
|---|
| 1064 |
|
|---|
| 1065 |
|
|---|
| 1066 |
|
|---|
| 1067 |
|
|---|
| 1068 |
|
|---|
| 1069 |
|
|---|
| 1070 |
|
|---|
| 1071 |
|
|---|
| 1072 |
|
|---|
| 1073 |
|
|---|
| 1074 |
|
|---|
| 1075 |
|
|---|
| 1076 |
|
|---|
| 1077 |
|
|---|
| 1078 |
|
|---|
| 1079 |
|
|---|
| 1080 |
|
|---|
| 1081 |
|
|---|
| 1082 |
|
|---|