| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fUnbufferedResult |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes |
|---|
| 14 |
|
|---|
| 15 |
class fUnbufferedResult implements Iterator |
|---|
| 16 |
{ |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
@var |
|---|
| 21 |
|
|---|
| 22 |
private $character_set = NULL; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
@var |
|---|
| 28 |
|
|---|
| 29 |
private $current_row = NULL; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
@var |
|---|
| 35 |
|
|---|
| 36 |
private $extension = NULL; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
@var |
|---|
| 42 |
|
|---|
| 43 |
private $pointer; |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
@var |
|---|
| 49 |
|
|---|
| 50 |
private $result = NULL; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
@var |
|---|
| 56 |
|
|---|
| 57 |
private $sql = ''; |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
@var |
|---|
| 63 |
|
|---|
| 64 |
private $type = NULL; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
@var |
|---|
| 70 |
|
|---|
| 71 |
private $untranslated_sql = NULL; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
@internal |
|---|
| 78 |
|
|---|
| 79 |
@param |
|---|
| 80 |
@param |
|---|
| 81 |
@param |
|---|
| 82 |
@return |
|---|
| 83 |
|
|---|
| 84 |
public function __construct($type, $extension, $character_set=NULL) |
|---|
| 85 |
{ |
|---|
| 86 |
$valid_types = array('mssql', 'mysql', 'postgresql', 'sqlite'); |
|---|
| 87 |
if (!in_array($type, $valid_types)) { |
|---|
| 88 |
fCore::toss( |
|---|
| 89 |
'fProgrammerException', |
|---|
| 90 |
fGrammar::compose( |
|---|
| 91 |
'The database type specified, %1$s, in invalid. Must be one of: %2$s.', |
|---|
| 92 |
fCore::dump($type), |
|---|
| 93 |
join(', ', $valid_types) |
|---|
| 94 |
) |
|---|
| 95 |
); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
$valid_extensions = array('mssql', 'mysql', 'mysqli', 'odbc', 'pdo', 'pgsql', 'sqlite', 'sqlsrv'); |
|---|
| 99 |
if (!in_array($extension, $valid_extensions)) { |
|---|
| 100 |
fCore::toss( |
|---|
| 101 |
'fProgrammerException', |
|---|
| 102 |
fGrammar::compose( |
|---|
| 103 |
'The database extension specified, %1$s, is invalid. Must be one of: %2$s.', |
|---|
| 104 |
fCore::dump($extension), |
|---|
| 105 |
join(', ', $valid_extensions) |
|---|
| 106 |
) |
|---|
| 107 |
); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
$this->type = $type; |
|---|
| 111 |
$this->extension = $extension; |
|---|
| 112 |
$this->character_set = $character_set; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
@internal |
|---|
| 120 |
|
|---|
| 121 |
@return |
|---|
| 122 |
|
|---|
| 123 |
public function __destruct() |
|---|
| 124 |
{ |
|---|
| 125 |
if (!is_resource($this->result) && !is_object($this->result)) { |
|---|
| 126 |
return; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
if ($this->extension == 'mssql') { |
|---|
| 130 |
mssql_free_result($this->result); |
|---|
| 131 |
} elseif ($this->extension == 'mysql') { |
|---|
| 132 |
mysql_free_result($this->result); |
|---|
| 133 |
} elseif ($this->extension == 'mysqli') { |
|---|
| 134 |
mysqli_free_result($this->result); |
|---|
| 135 |
} elseif ($this->extension == 'odbc') { |
|---|
| 136 |
odbc_free_result($this->result); |
|---|
| 137 |
} elseif ($this->extension == 'pgsql') { |
|---|
| 138 |
pg_free_result($this->result); |
|---|
| 139 |
} elseif ($this->extension == 'sqlite') { |
|---|
| 140 |
sqlite_fetch_all($this->result); |
|---|
| 141 |
} elseif ($this->extension == 'sqlsrv') { |
|---|
| 142 |
sqlsrv_free_stmt($this->result); |
|---|
| 143 |
} elseif ($this->extension == 'pdo') { |
|---|
| 144 |
$this->result->closeCursor(); |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
@param |
|---|
| 153 |
@return |
|---|
| 154 |
|
|---|
| 155 |
public function __get($method) |
|---|
| 156 |
{ |
|---|
| 157 |
return array($this, $method); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
@return |
|---|
| 165 |
|
|---|
| 166 |
private function advanceCurrentRow() |
|---|
| 167 |
{ |
|---|
| 168 |
if ($this->extension == 'mssql') { |
|---|
| 169 |
$row = mssql_fetch_assoc($this->result); |
|---|
| 170 |
if (empty($row)) { |
|---|
| 171 |
mssql_fetch_batch($this->result); |
|---|
| 172 |
$row = mssql_fetch_assoc($this->result); |
|---|
| 173 |
} |
|---|
| 174 |
if (!empty($row)) { |
|---|
| 175 |
$row = $this->fixDblibMSSQLDriver($row); |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
if (!empty($row) && $this->untranslated_sql !== NULL && isset($row['__flourish_limit_offset_row_num'])) { |
|---|
| 180 |
unset($row['__flourish_limit_offset_row_num']); |
|---|
| 181 |
} |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
} elseif ($this->extension == 'mysql') { |
|---|
| 185 |
$row = mysql_fetch_assoc($this->result); |
|---|
| 186 |
} elseif ($this->extension == 'mysqli') { |
|---|
| 187 |
$row = mysqli_fetch_assoc($this->result); |
|---|
| 188 |
} elseif ($this->extension == 'odbc') { |
|---|
| 189 |
$row = odbc_fetch_array($this->result); |
|---|
| 190 |
} elseif ($this->extension == 'pgsql') { |
|---|
| 191 |
$row = pg_fetch_assoc($this->result); |
|---|
| 192 |
} elseif ($this->extension == 'sqlite') { |
|---|
| 193 |
$row = sqlite_fetch_array($this->result, SQLITE_ASSOC); |
|---|
| 194 |
} elseif ($this->extension == 'sqlsrv') { |
|---|
| 195 |
$row = sqlsrv_fetch_array($this->result, SQLSRV_FETCH_ASSOC); |
|---|
| 196 |
} elseif ($this->extension == 'pdo') { |
|---|
| 197 |
$row = $this->result->fetch(PDO::FETCH_ASSOC); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
if ($row && $this->type == 'mssql') { |
|---|
| 202 |
if ($this->character_set) { |
|---|
| 203 |
foreach ($row as $key => $value) { |
|---|
| 204 |
if (!is_string($value) || strpos($key, '__flourish_mssqln_') === 0) { |
|---|
| 205 |
continue; |
|---|
| 206 |
} |
|---|
| 207 |
$row[$key] = iconv($this->character_set, 'UTF-8', $value); |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
$row = $this->decodeMSSQLNationalColumns($row); |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
$this->current_row = $row; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
@throws |
|---|
| 221 |
@throws |
|---|
| 222 |
@internal |
|---|
| 223 |
|
|---|
| 224 |
@return |
|---|
| 225 |
|
|---|
| 226 |
public function current() |
|---|
| 227 |
{ |
|---|
| 228 |
|
|---|
| 229 |
if ($this->pointer === NULL) { |
|---|
| 230 |
$this->pointer = 0; |
|---|
| 231 |
$this->advanceCurrentRow(); |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
if(!$this->current_row && $this->pointer == 0) { |
|---|
| 235 |
fCore::toss( |
|---|
| 236 |
'fNoResultsException', |
|---|
| 237 |
fGrammar::compose('The query did not return any rows') |
|---|
| 238 |
); |
|---|
| 239 |
|
|---|
| 240 |
} elseif (!$this->current_row) { |
|---|
| 241 |
fCore::toss( |
|---|
| 242 |
'fNoRemainingException', |
|---|
| 243 |
fGrammar::compose('There are no remaining rows') |
|---|
| 244 |
); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
return $this->current_row; |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
@param |
|---|
| 255 |
@return |
|---|
| 256 |
|
|---|
| 257 |
private function decodeMSSQLNationalColumns($row) |
|---|
| 258 |
{ |
|---|
| 259 |
if (strpos($this->sql, '__flourish_mssqln_') === FALSE) { |
|---|
| 260 |
return $row; |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
$columns = array_keys($row); |
|---|
| 264 |
|
|---|
| 265 |
foreach ($columns as $column) { |
|---|
| 266 |
if (substr($column, 0, 18) != '__flourish_mssqln_') { |
|---|
| 267 |
continue; |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
$real_column = substr($column, 18); |
|---|
| 271 |
|
|---|
| 272 |
$row[$real_column] = iconv('ucs-2le', 'utf-8', $row[$column]); |
|---|
| 273 |
unset($row[$column]); |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
return $row; |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
@throws |
|---|
| 284 |
@throws |
|---|
| 285 |
|
|---|
| 286 |
@return |
|---|
| 287 |
|
|---|
| 288 |
public function fetchRow() |
|---|
| 289 |
{ |
|---|
| 290 |
$row = $this->current(); |
|---|
| 291 |
$this->next(); |
|---|
| 292 |
return $row; |
|---|
| 293 |
} |
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
@param |
|---|
| 300 |
@return |
|---|
| 301 |
|
|---|
| 302 |
private function fixDblibMSSQLDriver($row) |
|---|
| 303 |
{ |
|---|
| 304 |
static $using_dblib = NULL; |
|---|
| 305 |
|
|---|
| 306 |
if ($using_dblib === NULL) { |
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
if (fCore::getOS() != 'windows') { |
|---|
| 310 |
$using_dblib = FALSE; |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
} else { |
|---|
| 314 |
ob_start(); |
|---|
| 315 |
phpinfo(INFO_MODULES); |
|---|
| 316 |
$module_info = ob_get_contents(); |
|---|
| 317 |
ob_end_clean(); |
|---|
| 318 |
|
|---|
| 319 |
$using_dblib = preg_match('#FreeTDS#ims', $module_info, $match); |
|---|
| 320 |
} |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
if (!$using_dblib) { |
|---|
| 324 |
return $row; |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
foreach ($row as $key => $value) { |
|---|
| 328 |
if ($value == ' ') { |
|---|
| 329 |
$row[$key] = ''; |
|---|
| 330 |
fCore::trigger( |
|---|
| 331 |
'notice', |
|---|
| 332 |
fGrammar::compose( |
|---|
| 333 |
'A single space was detected coming out of the database and was converted into an empty string - see %s for more information', |
|---|
| 334 |
'http://bugs.php.net/bug.php?id=26315' |
|---|
| 335 |
) |
|---|
| 336 |
); |
|---|
| 337 |
} |
|---|
| 338 |
if (strlen($key) == 30) { |
|---|
| 339 |
fCore::trigger( |
|---|
| 340 |
'notice', |
|---|
| 341 |
fGrammar::compose( |
|---|
| 342 |
'A column name exactly 30 characters in length was detected coming out of the database - this column name may be truncated, see %s for more information.', |
|---|
| 343 |
'http://bugs.php.net/bug.php?id=23990' |
|---|
| 344 |
) |
|---|
| 345 |
); |
|---|
| 346 |
} |
|---|
| 347 |
if (strlen($value) == 256) { |
|---|
| 348 |
fCore::trigger( |
|---|
| 349 |
'notice', |
|---|
| 350 |
fGrammar::compose( |
|---|
| 351 |
'A value exactly 255 characters in length was detected coming out of the database - this value may be truncated, see %s for more information.', |
|---|
| 352 |
'http://bugs.php.net/bug.php?id=37757' |
|---|
| 353 |
) |
|---|
| 354 |
); |
|---|
| 355 |
} |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
return $row; |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
@internal |
|---|
| 366 |
|
|---|
| 367 |
@return |
|---|
| 368 |
|
|---|
| 369 |
public function getResult() |
|---|
| 370 |
{ |
|---|
| 371 |
return $this->result; |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
@return |
|---|
| 379 |
|
|---|
| 380 |
public function getSQL() |
|---|
| 381 |
{ |
|---|
| 382 |
return $this->sql; |
|---|
| 383 |
} |
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
@return |
|---|
| 390 |
|
|---|
| 391 |
public function getUntranslatedSQL() |
|---|
| 392 |
{ |
|---|
| 393 |
return $this->untranslated_sql; |
|---|
| 394 |
} |
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
@throws |
|---|
| 401 |
@internal |
|---|
| 402 |
|
|---|
| 403 |
@return |
|---|
| 404 |
|
|---|
| 405 |
public function key() |
|---|
| 406 |
{ |
|---|
| 407 |
if ($this->pointer === NULL) { |
|---|
| 408 |
$this->current(); |
|---|
| 409 |
} |
|---|
| 410 |
|
|---|
| 411 |
return $this->pointer; |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
@throws |
|---|
| 419 |
@internal |
|---|
| 420 |
|
|---|
| 421 |
@return |
|---|
| 422 |
|
|---|
| 423 |
public function next() |
|---|
| 424 |
{ |
|---|
| 425 |
if ($this->pointer === NULL) { |
|---|
| 426 |
$this->current(); |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|
| 429 |
$this->advanceCurrentRow(); |
|---|
| 430 |
$this->pointer++; |
|---|
| 431 |
} |
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
@internal |
|---|
| 438 |
|
|---|
| 439 |
@return |
|---|
| 440 |
|
|---|
| 441 |
public function rewind() |
|---|
| 442 |
{ |
|---|
| 443 |
if (!empty($this->pointer)) { |
|---|
| 444 |
fCore::toss( |
|---|
| 445 |
'fProgrammerException', |
|---|
| 446 |
fGrammar::compose( |
|---|
| 447 |
'Unbuffered database results can not be iterated through multiple times' |
|---|
| 448 |
) |
|---|
| 449 |
); |
|---|
| 450 |
} |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
@internal |
|---|
| 458 |
|
|---|
| 459 |
@param |
|---|
| 460 |
@return |
|---|
| 461 |
|
|---|
| 462 |
public function setResult($result) |
|---|
| 463 |
{ |
|---|
| 464 |
$this->result = $result; |
|---|
| 465 |
} |
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
@internal |
|---|
| 472 |
|
|---|
| 473 |
@param |
|---|
| 474 |
@return |
|---|
| 475 |
|
|---|
| 476 |
public function setSQL($sql) |
|---|
| 477 |
{ |
|---|
| 478 |
$this->sql = $sql; |
|---|
| 479 |
} |
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
@internal |
|---|
| 486 |
|
|---|
| 487 |
@param |
|---|
| 488 |
@return |
|---|
| 489 |
|
|---|
| 490 |
public function setUntranslatedSQL($untranslated_sql) |
|---|
| 491 |
{ |
|---|
| 492 |
$this->untranslated_sql = $untranslated_sql; |
|---|
| 493 |
} |
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
@throws |
|---|
| 500 |
|
|---|
| 501 |
@param |
|---|
| 502 |
@return |
|---|
| 503 |
|
|---|
| 504 |
public function tossIfNoResults($message=NULL) |
|---|
| 505 |
{ |
|---|
| 506 |
try { |
|---|
| 507 |
$this->current(); |
|---|
| 508 |
} catch (fNoResultsException $e) { |
|---|
| 509 |
if ($message !== NULL) { |
|---|
| 510 |
$e->getMessage($message); |
|---|
| 511 |
} |
|---|
| 512 |
throw $e; |
|---|
| 513 |
} |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
@return |
|---|
| 521 |
|
|---|
| 522 |
public function valid() |
|---|
| 523 |
{ |
|---|
| 524 |
if ($this->pointer === NULL) { |
|---|
| 525 |
$this->advanceCurrentRow(); |
|---|
| 526 |
$this->pointer = 0; |
|---|
| 527 |
} |
|---|
| 528 |
|
|---|
| 529 |
return !empty($this->current_row); |
|---|
| 530 |
} |
|---|
| 531 |
} |
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
will@flourishlib.com |
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|