Overview

Packages

  • Onion::Controllers
  • Onion::Core
  • Onion::UI
  • Onion::Utils

Classes

  • Authenticator
  • Controller
  • Database
  • DatabaseResult
  • Log
  • Model
  • Onion
  • Request
  • Response
  • User
  • Overview
  • Package
  • Class
  • Tree
   1: <?php
   2: 
   3: /**
   4:  * Onion Framework - Práca s databázou MySQL
   5:  *
   6:  * Copyright (c) 2011 Jano Gašpar (http://webstranky.net)
   7:  *
   8:  * @author    Jano Gašpar
   9:  * @copyright Copyright (c) 2011 Jano Gašpar
  10:  * @package   Onion::Core
  11:  **/
  12: 
  13: class Database
  14: {
  15:     /**
  16:      * @var string meno tabuľky
  17:      */
  18:     public $table;
  19: 
  20: 
  21:     /**
  22:      * @var string prefix tabuliek
  23:      */
  24:     private $prefix;
  25: 
  26: 
  27:     /**
  28:      * @var array tabuľky na spojenie
  29:      */
  30:     private $joins = array();
  31: 
  32: 
  33:     /**
  34:      * @var array vzťahy medzi tabuľkami v databáze
  35:      */
  36:     private static $relations = array();
  37: 
  38: 
  39:     /**
  40:      * @var array vzťahy aktuálnej tabuľky s ostatnými tabuľkami v databáze
  41:      */
  42:     private $current_table_relations = array();
  43: 
  44: 
  45:     /**
  46:      * @var array stĺpce tabuliek v databáze
  47:      */
  48:     private static $tables_columns = array();
  49: 
  50: 
  51:     /**
  52:      * @var array stĺpce aktuálnej tabuľky
  53:      */
  54:     private $current_table_columns = array();
  55: 
  56: 
  57:     /**
  58:      * @var string aktuálny príkaz
  59:      */
  60:     public $command = 'SELECT';
  61: 
  62: 
  63:     /**
  64:      * @var array úložisko podmienok výberu
  65:      */
  66:     public $where = array();
  67: 
  68: 
  69:     /**
  70:      * @var array úložisko radenia výberu
  71:      */
  72:     public $order = array();
  73: 
  74: 
  75:     /**
  76:      * @var array limit počtu položiek výberu
  77:      */
  78:     public $limit = array();
  79: 
  80: 
  81:     /**
  82:      * @var array register pripojení k databázam
  83:      */
  84:     private static $connections = array();
  85: 
  86: 
  87:     /**
  88:      * @var string meno aktívneho pripojenia
  89:      */
  90:     private $active_connection = 'default';
  91: 
  92: 
  93:     /**
  94:      * @var string|array zoznam stĺpcov
  95:      */
  96:     public $columns = '*';
  97: 
  98: 
  99:     /**
 100:      * @var bool príznak DISTINCT výberu
 101:      */
 102:     public $distinct = FALSE;
 103: 
 104: 
 105:     /**
 106:      * @var bool príznak aktualizácie v prípade existencie riadku
 107:      */
 108:     public $update_on_duplicate = FALSE;
 109: 
 110: 
 111:     /**
 112:      * @var array úložisko dát záznamu v tvare asociatívneho poľa
 113:      */
 114:     public $data = array();
 115: 
 116: 
 117:     /**
 118:      * @var array úložisko dát záznamu v tvare poľa s hodnotami, kľúče sú uložené v $columns
 119:      */
 120:     public $values = array();
 121: 
 122: 
 123:     /**
 124:      * @var string polia podľa ktorých sa majú výsledky zoskupiť
 125:      */
 126:     public $group_by;
 127: 
 128: 
 129:     /**
 130:      * Konštruktor
 131:      *
 132:      * @param  string názov tabuľky s ktorou sa má pracovať
 133:      * @param  string názov spojenia k databázovemu serveru
 134:      * @return void
 135:      */
 136:     public function __construct($table = NULL, $connection_name = 'default')
 137:     {
 138:         if (isset(self::$connections[$connection_name]) === FALSE) {
 139:             trigger_error('Database connection <strong>"' . $connection_name . '"</strong> is not exist!', E_USER_ERROR);
 140:         }
 141: 
 142:         $this->active_connection = $connection_name;
 143:         $this->prefix = self::$connections[$connection_name]['prefix'];
 144: 
 145:         if ($table !== NULL) {
 146:             $this->table($table);
 147:         }
 148:     }
 149: 
 150: 
 151:     /**
 152:      * Volanie metód v statickom kontexte
 153:      *
 154:      * @param  string meno metódy
 155:      * @param  array  parametre pre volanie metódy
 156:      * @return mixed
 157:      */
 158:     public static function __callstatic($name, $arguments)
 159:     {
 160:         $db = new self;
 161: 
 162:         if (isset($arguments[0]) === FALSE) {
 163:             $arguments[0] = NULL;
 164:         }
 165: 
 166:         switch ($name) {
 167:             case 'query':
 168:                 return $db->_query($arguments[0]);
 169:                 break;
 170: 
 171:             case 'insert':
 172:             case 'update':
 173:             case 'save':
 174:             case 'delete':
 175:             case 'count':
 176:                 $db->table($arguments[0]);
 177:                 array_shift($arguments);
 178:                 return call_user_func_array(array($db, '_' . $name), $arguments);
 179:                 break;
 180:         }
 181:     }
 182: 
 183: 
 184:     /**
 185:      * Volanie metód v kontexte objektu
 186:      *
 187:      * @param  string meno metódy
 188:      * @param  array  parametre pre volanie metódy
 189:      * @return mixed
 190:      */
 191:     public function __call($name, $arguments)
 192:     {
 193:         if ($name === 'query') {
 194:             if (isset($arguments[0]) === FALSE) {
 195:                 $arguments[0] = NULL;
 196:             }
 197: 
 198:             return $this->_query($arguments[0]);
 199: 
 200:         }
 201: 
 202:         return call_user_func_array(array($this, '_' . $name), $arguments);
 203:     }
 204: 
 205: 
 206:     /**
 207:      * Metóda na vrátenie hodnoty chránenej vlastnosti
 208:      *
 209:      * @param  string meno vlastnosti
 210:      * @return mixed  hodnota vlastnosti
 211:      */
 212:     public function &__get($property)
 213:     {
 214:         return $this->$property;
 215:     }
 216: 
 217: 
 218:     /**
 219:      * Pridanie nového pripojenia k databázovému serveru
 220:      *
 221:      * @param  array  asociatívne pole s parametrami pripojenia, požadované
 222:      *                kľúče: 'host', 'user', 'password', 'database', 'prefix'
 223:      * @param  string meno pripojenia
 224:      * @return void
 225:      */
 226:     public static function connection($options, $connection_name = 'default')
 227:     {
 228:         if (isset(self::$connections[$connection_name]) === TRUE) {
 229:             trigger_error('Database connection <strong>"' . $connection_name . '"</strong> is already exist!', E_USER_ERROR);
 230:         }
 231: 
 232:         $options['link'] = NULL;
 233:         if (isset($options['prefix']) === FALSE) {
 234:             $options['prefix'] = '';
 235:         }
 236: 
 237:         self::$connections[$connection_name] = $options;
 238:     }
 239: 
 240: 
 241:     /**
 242:      * Pripojenie k databázovému serveru podľa $this->active_connection
 243:      *
 244:      * @return void
 245:      */
 246:     private function connect()
 247:     {
 248:         $connection = &self::$connections[$this->active_connection];
 249: 
 250:         if (empty($connection['link']) === FALSE) {
 251:             return;
 252:         }
 253: 
 254:         $connection['link'] = mysql_connect($connection['host'], $connection['user'], $connection['password']);
 255: 
 256:         if (mysql_set_charset('utf8') === FALSE) {
 257:             trigger_error('Could not set connection charset!', E_USER_ERROR);
 258:         }
 259: 
 260:         if ($connection['link'] === FALSE) {
 261:             trigger_error('Could not connect to database server!', E_USER_ERROR);
 262:         }
 263: 
 264:         if (mysql_select_db($connection['database']) === FALSE) {
 265:             trigger_error('Could not select database!', E_USER_ERROR);
 266:         }
 267:     }
 268: 
 269: 
 270:     /**
 271:      * Nastavenie mena tabuľky s ktorou sa bude pracovať a nastavenie jej väzieb
 272:      * s inými tabuľkami a jej stĺpcov a zresetovanie ostatných parametrov objektu
 273:      *
 274:      * @param  string meno tabuľky
 275:      * @return object Database pre fluent interface
 276:      */
 277:     public function table($table)
 278:     {
 279:         $this->table = $table;
 280:         if (self::$relations === NULL) {
 281:             self::$relations = $this->get_relations();
 282:         }
 283: 
 284:         if (isset(self::$relations[$table]) === TRUE) {
 285:             $this->current_table_relations = self::$relations[$table];
 286: 
 287:         } else {
 288:             $this->current_table_relations = array();
 289:         }
 290: 
 291:         if (self::$tables_columns === NULL) {
 292:             self::$tables_columns = $this->get_tables_columns();
 293:         }
 294: 
 295:         if (isset(self::$tables_columns[$table]) === TRUE) {
 296:             $this->current_table_columns = self::$tables_columns[$table];
 297: 
 298:         } else {
 299:             $this->current_table_columns = array();
 300:         }
 301: 
 302:         $this->reset();
 303: 
 304:         return $this;
 305:     }
 306: 
 307: 
 308:     /**
 309:      * Vykonanie dopytu do databázového servera
 310:      *
 311:      * Metóda je volaná cez preťažovanie
 312:      * v statickom kontexte (Database::query()), alebo v kontexte objektu Database
 313:      * (Database->query())
 314:      *
 315:      * @var    string|null SQL príkaz, ak je NULL, SQL sa vytvorí
 316:      *                     sa z nastavených vlastností objektu
 317:      * @return mixed       COUNT = int, SELECT = object DatabaseResult,
 318:      *                     INSERT = int ID vloženého záznamu, ostatné bool
 319:      */
 320:     private function _query($sql = NULL)
 321:     {
 322:         $this->connect();
 323: 
 324:         $reset = TRUE;
 325:         if ($sql === FALSE) {
 326:             $reset = FALSE;
 327:             $sql = NULL;
 328:         }
 329: 
 330:         if ($sql === NULL) {
 331:             $sql = $this->build_sql();
 332: 
 333:         } else {
 334:             $sql = trim($sql);
 335:             //list($command, $count) = preg_split('/( )|(\()/', $sql, 3);
 336:             list($command, $count) = preg_split('/(\s+)|(\()/', $sql, 3);
 337: 
 338:             $count = strtoupper($count);
 339:             if ($count === 'COUNT') {
 340:                 $this->command = 'COUNT';
 341: 
 342:             } else {
 343:                 $this->command = strtoupper($command);
 344:             }
 345:         }
 346: 
 347:         if (DEBUG === TRUE) {
 348:             Log::debug($sql);
 349:         }
 350: 
 351:         $result = mysql_query($sql);
 352: 
 353:         if ($result === FALSE) {
 354:             trigger_error(mysql_error() . '<br /><code>' . $sql . '</code>', E_USER_ERROR);
 355:         }
 356: 
 357:         if ($reset === TRUE) {
 358:             $this->reset();
 359:         }
 360: 
 361:         if ($this->command === 'COUNT') {
 362:             if (empty($this->group_by) === TRUE) {
 363:                 $count = mysql_fetch_array($result);
 364:                 return reset($count);
 365:             }
 366: 
 367:             return new DatabaseResult($result);
 368:         }
 369: 
 370:         if ($this->command === 'SELECT') {
 371:             return new DatabaseResult($result);
 372:         }
 373: 
 374:         if ($this->command === 'INSERT') {
 375:             return mysql_insert_id();
 376:         }
 377: 
 378:         return $result;
 379:     }
 380: 
 381: 
 382:     /**
 383:      * Zobrazí SQL aktuálneho dopytu
 384:      */
 385:     public function show_sql()
 386:     {
 387:         $sql = $this->build_sql();
 388:         echo '<pre>';
 389:         print_r($sql);
 390:         echo '</pre>';
 391:         exit;
 392:     }
 393: 
 394: 
 395:     /**
 396:      * Výber záznamov
 397:      *
 398:      * @param  array|string  stĺpce na výber, v prípade pomenovania systémom
 399:      *                       tabuľka.stĺpec sa automaticky vytvorí JOIN
 400:      * @return object        DatabaseResult
 401:      */
 402:     public function get_rows($columns = '*', $where = NULL)
 403:     {
 404:         $this->command = 'SELECT';
 405:         $this->columns = $columns;
 406: 
 407:         if ($where !== NULL) {
 408:             if (is_numeric($where) === TRUE
 409:                 OR Arrays::is_numeric($where) === TRUE) {
 410: 
 411:                 $where = array(array('id', $where));
 412:             }
 413: 
 414:             $this->where = $where;
 415:         }
 416: 
 417:         return $this->_query();
 418:     }
 419: 
 420: 
 421:     /**
 422:      * Výber jedného záznamu
 423:      *
 424:      * @param  array|string  stĺpce na výber, v prípade pomenovania systémom
 425:      *                       tabuľka.stĺpec sa automaticky vytvorí JOIN
 426:      * @return array         dáta záznamu
 427:      */
 428:     public function get_row($columns = '*', $where = NULL)
 429:     {
 430:         $this->limit(1);
 431:         $result = $this->get_rows($columns, $where);
 432: 
 433:         if ($result === FALSE) {
 434:             return FALSE;
 435:         }
 436: 
 437:         return $result->fetch();
 438:     }
 439: 
 440: 
 441:     /**
 442:      * Uloženie záznamu
 443:      *
 444:      * Metóda je volaná cez preťažovanie
 445:      * v statickom kontexte (Database::save()), alebo v kontexte objektu Database
 446:      * (Database->save())
 447:      *
 448:      * @param  array dáta záznamu na uloženie
 449:      * @param  bool  príznak určujúci či sa má zaznam v prípade duplikácie
 450:      *               aktualizovať miesto pokusu o vytvorenie nového záznamu
 451:      * @return int   id záznamu
 452:      */
 453:     public function _save($data, $update_on_duplicate = TRUE)
 454:     {
 455:         if (empty($data['id']) === FALSE) {
 456:             $this->command = 'UPDATE';
 457:             $this->where = array(array('id', $data['id']));
 458: 
 459:         } else {
 460:             $this->command = 'INSERT';
 461:             $this->update_on_duplicate = $update_on_duplicate;
 462:         }
 463: 
 464:         if (empty($this->current_table_columns) === FALSE) {
 465:             $this->data = array();
 466:             foreach ($this->current_table_columns as $column => $type) {
 467:                 if (array_key_exists($column, $data) === TRUE) {
 468:                     $this->data[$column] = $data[$column];
 469:                 }
 470:             }
 471: 
 472:         } else {
 473:             $this->data = $data;
 474:         }
 475: 
 476:         $result = $this->_query();
 477: 
 478:         if ($result === FALSE) {
 479:             return FALSE;
 480:         }
 481: 
 482:         if (isset($data['id']) === TRUE) {
 483:             return $data['id'];
 484: 
 485:         } else {
 486:             return mysql_insert_id();
 487:         }
 488:     }
 489: 
 490: 
 491:     /**
 492:      * Vloženie nového záznamu
 493:      *
 494:      * Metóda je volaná cez preťažovanie
 495:      * v statickom kontexte (Database::insert()), alebo v kontexte objektu Database
 496:      * (Database->insert())
 497:      *
 498:      * @param  array dáta záznamu na uloženie
 499:      * @return int   id záznamu
 500:      */
 501:     private function _insert($data)
 502:     {
 503:         $this->command = 'INSERT';
 504:         return $this->save($data, FALSE);
 505:     }
 506: 
 507: 
 508:     /**
 509:      * Aktualizácia záznamu
 510:      *
 511:      * Metóda je volaná cez preťažovanie
 512:      * v statickom kontexte (Database::update()), alebo v kontexte objektu Database
 513:      * (Database->update())
 514:      *
 515:      * Záznamy sa určuje cez where(), alebo cez parameter pri volaní
 516:      *
 517:      * @param  array     dáta záznamu na aktualizáciu
 518:      * @param  int|array ID záznamu na aktualizáciu, alebo pole s ID záznamov
 519:      *                   alebo podmienkami
 520:      * @return bool
 521:      */
 522:     private function _update($data, $where = NULL)
 523:     {
 524:         $this->command = 'UPDATE';
 525: 
 526:         if ($where !== NULL) {
 527:             if (is_numeric($where) === TRUE
 528:                 OR Arrays::is_numeric($where) === TRUE) {
 529: 
 530:                 $where = array(array('id', $where));
 531:             }
 532: 
 533:             $this->where = $where;
 534:         }
 535: 
 536:         if (empty($this->where) === TRUE) {
 537:             return FALSE;
 538:         }
 539: 
 540:         $this->data = $data;
 541: 
 542:         return $this->_query();
 543:     }
 544: 
 545: 
 546:     /**
 547:      * Zmazanie záznamu
 548:      *
 549:      * Metóda je volaná cez preťažovanie
 550:      * v statickom kontexte (Database::delete()), alebo v kontexte objektu Database
 551:      * (Database->delete())
 552:      *
 553:      * Záznamy sa určuje cez where(), alebo cez parameter pri volaní
 554:      *
 555:      * @param  int|array ID záznamu na aktualizáciu, alebo pole s ID záznamov
 556:      *                   alebo podmienkami
 557:      * @return bool
 558:      */
 559:     private function _delete($where = NULL)
 560:     {
 561:         $this->command = 'DELETE';
 562: 
 563:         if ($where !== NULL) {
 564:             if (is_numeric($where) === TRUE
 565:                 OR Arrays::is_numeric($where) === TRUE) {
 566: 
 567:                 $where = array(array('id', $where));
 568:             }
 569: 
 570:             $this->where = $where;
 571:         }
 572: 
 573:         if (empty($this->where) === TRUE) {
 574:             return FALSE;
 575:         }
 576: 
 577:         return $this->_query();
 578:     }
 579: 
 580: 
 581:     /**
 582:      * Zistenie počtu záznamov
 583:      *
 584:      * Metóda je volaná cez preťažovanie
 585:      * v statickom kontexte (Database::count()), alebo v kontexte objektu Database
 586:      * (Database->count())
 587:      *
 588:      * Záznamy sa určujú cez where(), alebo cez parameter pri volaní
 589:      *
 590:      * @param  int|array ID záznamu na aktualizáciu, alebo pole s ID záznamov alebo podmienkami
 591:      * @return int       počet položiek
 592:      */
 593:     private function _count($where = NULL)
 594:     {
 595:         $this->command = 'COUNT';
 596: 
 597:         if ($where !== NULL) {
 598:             if (is_numeric($where) === TRUE
 599:                 OR Arrays::is_numeric($where) === TRUE) {
 600: 
 601:                 $where = array(array('id', $where));
 602:             }
 603: 
 604:             $this->where = $where;
 605:         }
 606: 
 607:         return $this->_query(FALSE);
 608:     }
 609: 
 610: 
 611:     /**
 612:      * Vytvorenie SQL z vlastností objektu Database
 613:      *
 614:      * @return string SQL
 615:      */
 616:     private function build_sql()
 617:     {
 618:         switch ($this->command) {
 619:             case 'SELECT':
 620:                 return $this->build_select();
 621:                 exit;
 622: 
 623:             case 'COUNT':
 624:                 return $this->build_count();
 625:                 exit;
 626: 
 627:             case 'INSERT':
 628:                 return $this->build_insert();
 629:                 exit;
 630: 
 631:             case 'UPDATE':
 632:                 return $this->build_update();
 633:                 exit;
 634: 
 635:             case 'DELETE':
 636:                 return $this->build_delete();
 637:                 exit;
 638:         }
 639:     }
 640: 
 641: 
 642:     /**
 643:      * Vytvorenie SQL z vlastností objektu Database pre typ príkazu SELECT
 644:      *
 645:      * @return string SQL
 646:      */
 647:     private function build_select()
 648:     {
 649:         $where = '';
 650:         if (empty($this->where) === FALSE) {
 651:             $where = ' WHERE ' . $this->build_where($this->where);
 652:         }
 653: 
 654:         $order = '';
 655:         if (empty($this->order) === FALSE) {
 656:             $order = ' ORDER BY ' . $this->build_order($this->order);
 657:         }
 658: 
 659:         $sql = 'SELECT ';
 660: 
 661:         if ($this->distinct === TRUE) {
 662:             $sql .= 'DISTINCT ';
 663:         }
 664: 
 665:         $sql .= $this->build_columns($this->columns);
 666: 
 667:         $sql .= ' FROM ';
 668:         $sql .= '`' . $this->prefix . $this->table . '` ';
 669: 
 670:         if (empty($this->joins) === FALSE) {
 671:             $sql .= $this->build_joins($this->joins);
 672:         }
 673: 
 674:         $sql .= $where;
 675: 
 676:         if (empty($this->group_by) === FALSE) {
 677:             $sql .= $this->build_group_by($this->group_by);
 678:         }
 679: 
 680:         $sql .= $order;
 681: 
 682:         if (empty($this->limit) === FALSE) {
 683:             $sql .= ' LIMIT ' . implode(',', $this->limit);;
 684:         }
 685: 
 686:         return $sql;
 687:     }
 688: 
 689: 
 690:     /**
 691:      * Vytvorenie SQL z vlastností objektu Database pre typ príkazu COUNT
 692:      *
 693:      * @return string SQL
 694:      */
 695:     private function build_count()
 696:     {
 697:         $where = '';
 698:         if (empty($this->where) === FALSE) {
 699:             $where = ' WHERE ' . $this->build_where($this->where);
 700:         }
 701: 
 702:         $distinct = '*';
 703:         if ($this->distinct === TRUE) {
 704:             $distinct = 'DISTINCT `' . $this->table . '`.`id`';
 705:         }
 706:         $sql = 'SELECT COUNT(' . $distinct . ') FROM ';
 707:         $sql .= '`' . $this->prefix . $this->table . '` ';
 708: 
 709:         if (empty($this->joins) === FALSE) {
 710:             $sql .= $this->build_joins($this->joins);
 711:         }
 712: 
 713:         $sql .= $where;
 714: 
 715:         if (empty($this->group_by) === FALSE) {
 716:             $sql .= $this->build_group_by($this->group_by);
 717:         }
 718: 
 719:         return $sql;
 720:     }
 721: 
 722: 
 723:     /**
 724:      * Vytvorenie SQL z vlastností objektu Database pre typ príkazu INSERT
 725:      *
 726:      * @return string SQL
 727:      */
 728:     private function build_insert()
 729:     {
 730:         $data = $this->data;
 731: 
 732:         if (empty($this->columns) === FALSE
 733:             AND empty($this->values) === FALSE) {
 734: 
 735:             $data_2 = array_combine($this->columns, $this->values);
 736:             $data = array_merge($data, $data_2);
 737:         }
 738: 
 739:         $sql = 'INSERT INTO ';
 740:         $sql .= '`' . $this->prefix . $this->table . '` ';
 741: 
 742:         $sql .= $this->build_values($data);
 743: 
 744:         if ($this->update_on_duplicate === TRUE) {
 745:             $sql .= ' ON DUPLICATE KEY UPDATE ';
 746:             $sql .= 'id=LAST_INSERT_ID(id), ';
 747: 
 748:             if (isset($data['id']) === TRUE) {
 749:                 unset($data['id']);
 750:             }
 751: 
 752:             $sql .= $this->build_key_value($data);
 753:         }
 754: 
 755:         return $sql;
 756:     }
 757: 
 758: 
 759:     /**
 760:      * Vytvorenie SQL z vlastností objektu Database pre typ príkazu UPDATE
 761:      *
 762:      * @return string SQL
 763:      */
 764:     private function build_update()
 765:     {
 766:         if (empty($this->where) === TRUE) {
 767:             return FALSE;
 768:         }
 769: 
 770:         $data = $this->data;
 771: 
 772:         if (empty($this->columns) === FALSE
 773:             AND empty($this->values) === FALSE) {
 774: 
 775:             $data_2 = array_combine($this->columns, $this->values);
 776:             $data = array_merge($data, $data_2);
 777:         }
 778: 
 779:         $sql = 'UPDATE ';
 780:         $sql .= '`' . $this->prefix . $this->table . '` SET ';
 781: 
 782:         $sql .= $this->build_key_value($data);
 783: 
 784:         $sql .= ' WHERE ';
 785:         $sql .= $this->build_where($this->where);
 786: 
 787:         return $sql;
 788:     }
 789: 
 790: 
 791:     /**
 792:      * Vytvorenie SQL z vlastností objektu Database pre typ príkazu DELETE
 793:      *
 794:      * @return string SQL
 795:      */
 796:     private function build_delete()
 797:     {
 798:         if (empty($this->where) === TRUE) {
 799:             return FALSE;
 800:         }
 801: 
 802:         $sql = 'DELETE FROM ';
 803:         $sql .= '`' . $this->prefix . $this->table . '` ';
 804: 
 805:         $sql .= ' WHERE ';
 806:         $sql .= $this->build_where($this->where);
 807: 
 808:         return $sql;
 809:     }
 810: 
 811: 
 812:     /**
 813:      * Vytvorenie časti SQL so zoznamom stĺpcov na výpis
 814:      *
 815:      * @return string
 816:      */
 817:     public function build_columns($columns)
 818:     {
 819:         if (empty($columns) === TRUE
 820:             OR $columns === '*') {
 821: 
 822:             return '*';
 823:         }
 824: 
 825:         $columns_out = '';
 826: 
 827:         foreach ($columns as $key => $column) {
 828:             $column_tmp = preg_split('/(\.)|( as )/i', $column, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 829: 
 830:             $c = count($column_tmp);
 831: 
 832:             $column = array();
 833:             if ($c === 1) {
 834:                 $column[0] = $this->table;
 835:                 $column[1] = $column_tmp[0];
 836:                 $column[2] = $column_tmp[0];
 837: 
 838:             } elseif ($c === 3) {
 839:                 if ($column_tmp[1] === '.') {
 840:                     // table.column > table.column as column
 841:                     $column[0] = $column_tmp[0];
 842:                     $column[1] = $column_tmp[2];
 843:                     $column[2] = $column_tmp[2];
 844: 
 845:                 } else {
 846:                     // column as alias > table.column as alias
 847:                     $column[0] = $this->table;
 848:                     $column[1] = $column_tmp[0];
 849:                     $column[2] = $column_tmp[2];
 850:                 }
 851: 
 852:             } elseif ($c === 5) {
 853:                 $column[0] = $column_tmp[0];
 854:                 $column[1] = $column_tmp[2];
 855:                 $column[2] = $column_tmp[4];
 856:             }
 857: 
 858:             if (isset($this->current_table_relations[$column[0]]) === TRUE) {
 859:                 $column[2] = $column_tmp[0] . '_' . $column_tmp[2];
 860: 
 861:                 $join_table = $this->current_table_relations[$column[0]];
 862:                 $as = ' AS ' . $this->prefix . $column[0];
 863: 
 864:                 $on = $this->table . '.' . $column[0] . ' = ' . $column[0] . '.id';
 865: 
 866:             } else {
 867:                 $join_table = $column[0];
 868:                 $as = '';
 869: 
 870:                 $join_column = FALSE;
 871:                 if (isset(self::$relations[$column[0]]) === TRUE) {
 872:                     $join_column = array_search($this->table, self::$relations[$column[0]]);
 873:                 }
 874: 
 875:                 if ($join_column === FALSE) {
 876:                     $join_column = $column[1];
 877:                 }
 878: 
 879:                 $on = $this->table . '.id = ' . $join_table . '.' . $join_column;
 880:             }
 881: 
 882:             if ($join_table !== $this->table
 883:                 AND isset($this->joins[$join_table . $as]) == FALSE) {
 884: 
 885:                 $this->joins[$join_table . $as] = array($join_table . $as, $on, NULL);
 886:             }
 887: 
 888:             $columns_out .= '`';
 889:             $columns_out .= $this->prefix . $column[0];
 890:             $columns_out .= '`.`';
 891:             $columns_out .= $column[1];
 892:             $columns_out .= '` AS `';
 893:             $columns_out .= $column[2];
 894:             $columns_out .= '`, ';
 895:         }
 896: 
 897:         return trim($columns_out, ', ');
 898:     }
 899: 
 900: 
 901:     /**
 902:      * Zostavenie JOINov SQL dopytu
 903:      *
 904:      * @param  array  JOINy
 905:      * @return string JOIN časť SQL
 906:      */
 907:     public function build_joins($joins)
 908:     {
 909:         $joins_out = '';
 910: 
 911:         foreach ($this->joins as $join) {
 912:             $type = 'LEFT';
 913:             if ($join[2] !== NULL) {
 914:                 $type = $join[2];
 915:             }
 916: 
 917:             $table  = '`' . $this->prefix;
 918:             $table .= str_ireplace(' AS ', '` AS `', $join[0]);
 919:             $table .= '`';
 920: 
 921:             $relation  = '`';
 922:             $relation .= $this->prefix;
 923:             $relation .= str_ireplace(array('.', ' = '), array('`.`', '` = `' . $this->prefix), $join[1]);
 924:             $relation .= '`';
 925: 
 926:             $joins_out .= $type . ' JOIN ' . $table;
 927:             $joins_out .= ' ON ' . $relation . ' ';
 928:         }
 929: 
 930:         return $joins_out;
 931:     }
 932: 
 933: 
 934:     /**
 935:      * Zostavenie WHERE časti SQL dopytu
 936:      *
 937:      * @param  array  podmienky výberu
 938:      * @return string WHERE časť SQL
 939:      */
 940:     public function build_where($conditions)
 941:     {
 942:         $where = '';
 943: 
 944:         $conditions_count = count($conditions);
 945:         $conditions_count--;
 946: 
 947:         foreach ($conditions as $key => $condition) {
 948:             if (is_array($condition[0]) === TRUE) {
 949:                 $where .= '(';
 950:                 $where .= $this->build_where($condition);
 951:                 $where .= ')';
 952: 
 953:             } else {
 954:                 $column = explode('.', $condition[0]);
 955: 
 956:                 if (count($column) === 1) {
 957:                     $column = '`' . $column[0] . '`';
 958: 
 959:                 } else {
 960:                     if (isset($this->current_table_relations[$column[0]]) === TRUE) {
 961:                         $join_table = $this->current_table_relations[$column[0]];
 962:                         $as = ' AS ' . $this->prefix . $column[0];
 963: 
 964:                         $on = $this->table . '.' . $column[0] . ' = ' . $column[0] . '.id';
 965: 
 966:                     } else {
 967:                         $join_table = $column[0];
 968:                         $as = '';
 969: 
 970:                         $join_column = FALSE;
 971:                         if (isset(self::$relations[$column[0]]) === TRUE) {
 972:                             $join_column = array_search($this->table, self::$relations[$column[0]]);
 973:                         }
 974: 
 975:                         if ($join_column === FALSE) {
 976:                             $join_column = $column[1];
 977:                         }
 978: 
 979:                         $on = $this->table . '.id = ' . $join_table . '.' . $join_column;
 980:                     }
 981: 
 982:                     if ($join_table !== $this->table
 983:                         AND isset($this->joins[$join_table . $as]) == FALSE) {
 984: 
 985:                         $this->joins[$join_table . $as] = array($join_table . $as, $on, NULL);
 986:                     }
 987: 
 988:                     $column = '`' . $this->prefix . $column[0] . '`.`' . $column[1] . '`';
 989:                 }
 990: 
 991:                 $value = $condition[1];
 992: 
 993:                 $operator = '=';
 994:                 if (isset($condition[2]) === TRUE) {
 995:                     $operator = $condition[2];
 996:                 }
 997: 
 998:                 $operator = strtoupper($operator);
 999: 
1000:                 if (is_array($value) === TRUE) {
1001:                     if ($operator === '=') {
1002:                         $operator = 'IN';
1003: 
1004:                     } elseif ($operator === '<>'
1005:                         OR $operator === '!=') {
1006: 
1007:                         $operator = 'NOT IN';
1008:                     }
1009: 
1010:                     $where .= $column . ' ' . $operator . ' (';
1011: 
1012:                     foreach ($value as $val) {
1013:                         if ($val === NULL) {
1014:                             $where .= 'NULL,';
1015: 
1016:                         } else {
1017:                             $where .= '\'' . mysql_real_escape_string($val) . '\',';
1018:                         }
1019:                     }
1020: 
1021:                     $where = substr($where, 0, -1);
1022:                     $where .= ')';
1023: 
1024:                 } else {
1025: 
1026:                     if ($value === NULL) {
1027:                         if ($operator === '=') {
1028:                             $operator = 'IS';
1029: 
1030:                         } elseif ($operator === '!=' OR $operator === '<>') {
1031:                             $operator = 'IS NOT';
1032:                         }
1033: 
1034:                         $value = 'NULL';
1035: 
1036:                     } else {
1037:                         $value = '\'' . mysql_real_escape_string($value) . '\'';
1038:                     }
1039: 
1040:                     $where .= $column . ' ' . $operator  . ' ' .$value;
1041:                 }
1042: 
1043:                 $where .= ' ';
1044:             }
1045: 
1046:             $bool = 'AND';
1047:             if (isset($conditions[$key + 1][3]) === TRUE) {
1048:                 $bool = $conditions[$key + 1][3];
1049:             }
1050: 
1051:             if ($conditions_count !== $key) {
1052:                 $where .= ' ' . $bool . ' ';
1053:             }
1054:         }
1055: 
1056:         return $where;
1057:     }
1058: 
1059: 
1060:     /**
1061:      * Zostavenie GROUP BY časti SQL dopytu
1062:      *
1063:      * @param  string stĺpce
1064:      * @return string GROUP BY časť SQL
1065:      */
1066:     public function build_group_by($group_by)
1067:     {
1068:         $group_by = str_replace('.', '`.`', $group_by);
1069:         return ' GROUP BY `' . $group_by . '`';
1070:     }
1071: 
1072: 
1073:     /**
1074:      * Zostavenie ORDER časti SQL dopytu
1075:      *
1076:      * @param  array  podmienky radenia výberu
1077:      * @return string ORDER časť SQL
1078:      */
1079:     public function build_order($orders)
1080:     {
1081:         $order_out = '';
1082: 
1083:         foreach ($orders as $order) {
1084:             $column = explode('.', $order[0]);
1085: 
1086:             if (count($column) === 1) {
1087:                 $column = '`' . $column[0] . '`';
1088: 
1089:             } else {
1090:                 if (isset($this->current_table_relations[$column[0]]) === TRUE) {
1091:                     $join_table = $this->current_table_relations[$column[0]];
1092:                     $as = ' AS ' . $this->prefix . $column[0];
1093: 
1094:                     $on = $this->table . '.' . $column[0] . ' = ' . $column[0] . '.id';
1095: 
1096:                 } else {
1097:                     $join_table = $column[0];
1098:                     $as = '';
1099: 
1100:                     $join_column = FALSE;
1101:                     if (isset(self::$relations[$column[0]]) === TRUE) {
1102:                         $join_column = array_search($this->table, self::$relations[$column[0]]);
1103:                     }
1104: 
1105:                     if ($join_column === FALSE) {
1106:                         $join_column = $column[1];
1107:                     }
1108: 
1109:                     $on = $this->table . '.id = ' . $join_table . '.' . $join_column;
1110:                 }
1111: 
1112:                 if ($join_table !== $this->table
1113:                     AND isset($this->joins[$join_table . $as]) == FALSE) {
1114: 
1115:                     $this->joins[$join_table . $as] = array($join_table . $as, $on, NULL);
1116:                 }
1117: 
1118:                 $column = '`' . $this->prefix . $column[0] . '`.`' . $column[1] . '`';
1119:             }
1120: 
1121:             $direction = 'ASC';
1122:             if (isset($order[1]) === TRUE) {
1123:                 $direction = $order[1];
1124:             }
1125: 
1126:             $order_out .= $column . ' ' . $direction . ' ,';
1127:         }
1128: 
1129:         return substr($order_out, 0, -1);
1130:     }
1131: 
1132: 
1133:     /**
1134:      * Zostavenie VALUES časti SQL dopytu
1135:      * (col1, col2) VALUES (val1, val2), ...
1136:      *
1137:      * @param  array  dáta záznamu
1138:      * @return string VALUES časť SQL
1139:      */
1140:     public function build_values($data)
1141:     {
1142:         if (isset($data[0]) === FALSE
1143:             OR is_array($data[0]) === FALSE) {
1144: 
1145:             $data = array($data);
1146:         }
1147: 
1148:         $keys = array_keys($data[0]);
1149: 
1150:         $values_out  = '(`';
1151:         $values_out .= implode('`,`', $keys);
1152:         $values_out .= '`) VALUES ';
1153: 
1154:         foreach ($data as $values) {
1155:             $values_out .= $this->build_values_list($values) . ',';
1156:         }
1157: 
1158:         return substr($values_out, 0, -1);
1159:     }
1160: 
1161: 
1162:     /**
1163:      * Zostavenie VALUES časti SQL dopytu vo formáte stĺpec=hodnota
1164:      * col1 = val1, col2 = val2
1165:      *
1166:      * @param  array  dáta záznamu
1167:      * @return string VALUES časť SQL
1168:      */
1169:     public function build_key_value($data)
1170:     {
1171:         $values_out = '';
1172: 
1173:         foreach ($data as $key => $value) {
1174:             $values_out .= '`' . $key . '`=';
1175:             if ($value === NULL) {
1176:                 $values_out .= 'NULL,';
1177: 
1178:             } else {
1179:                 $values_out .= '\'' . mysql_real_escape_string($value) . '\',';
1180:             }
1181:         }
1182: 
1183:         return substr($values_out, 0, -1);
1184:     }
1185: 
1186: 
1187:     /**
1188:      * Zostavenie VALUES časti SQL dopytu vo formáte (hodnota, hodnota, hodnota ...)
1189:      *
1190:      * @param  array  dáta záznamu
1191:      * @return string VALUES časť SQL
1192:      */
1193:     public function build_values_list($data)
1194:     {
1195:         $values_out = '(';
1196: 
1197:         foreach ($data as $value) {
1198:             if ($value === NULL) {
1199:                 $values_out .= 'NULL,';
1200: 
1201:             } else {
1202:                 $values_out .= '\'' . mysql_real_escape_string($value) . '\',';
1203:             }
1204:         }
1205: 
1206:         $values_out = substr($values_out, 0, -1);
1207:         $values_out .= ')';
1208: 
1209:         return $values_out;
1210:     }
1211: 
1212: 
1213:     /**
1214:      * Reset parametrov dopytov
1215:      *
1216:      * @return void
1217:      */
1218:     public function reset()
1219:     {
1220:         $this->data = array();
1221:         $this->values = array();
1222:         $this->fields = '*';
1223:         $this->joins = array();
1224:         $this->where = array();
1225:         $this->order = array();
1226:         $this->limit = array();
1227:         $this->distinct = FALSE;
1228:     }
1229: 
1230: 
1231:     /**
1232:      * Nastavenie príkazu aktuálneho dopytu
1233:      *
1234:      * @param  string príkaz
1235:      * @return object Database pre fluent interface
1236:      */
1237:     public function command($command)
1238:     {
1239:         $this->command = strtoupper($command);
1240:         return $this;
1241:     }
1242: 
1243: 
1244:     /**
1245:      * Nastavenie stĺpcov s korými sa bude pracovať
1246:      *
1247:      * @param  string meno stĺpca, počet parametrov je "neobmedzený"
1248:      * @return object Database pre fluent interface
1249:      */
1250:     public function columns()
1251:     {
1252:         $this->columns = func_get_args();
1253:     }
1254: 
1255: 
1256:     /**
1257:      * Pridávanie prepojených tabuliek
1258:      *
1259:      * @param  array  podmienky
1260:      * @return object Database pre fluent interface
1261:      */
1262:     public function join($table, $on, $alias = NULL, $type = 'LEFT')
1263:     {
1264:         if ($alias !== NULL) {
1265:             $table = $table . ' AS ' . $alias;
1266: 
1267:         } else {
1268:             $alias = $table;
1269:         }
1270: 
1271:         $this->joins[$alias] = array($table, $on, $type);
1272:         return $this;
1273:     }
1274: 
1275: 
1276:     /**
1277:      * Pridávanie podmienok dopytu
1278:      *
1279:      * @param  array  podmienky
1280:      * @return object Database pre fluent interface
1281:      */
1282:     public function where()
1283:     {
1284:         $arguments = func_get_args();
1285: 
1286:         $this->where[] = $arguments;
1287: 
1288:         return $this;
1289:     }
1290: 
1291: 
1292:     /**
1293:      * Nastavenie stĺpcov podľa ktorých sa budú výsledky dopytu zoskupovať
1294:      *
1295:      * @param  string stĺpce
1296:      * @return object Database pre fluent interface
1297:      */
1298:     public function group_by($group_by)
1299:     {
1300:         $this->group_by = $group_by;
1301: 
1302:         return $this;
1303:     }
1304: 
1305: 
1306:     /**
1307:      * Pridávanie podmienok radenia výberu
1308:      *
1309:      * @param  string stĺpec podľa ktorého radiť
1310:      * @param  string smer radenia
1311:      * @return object Database pre fluent interface
1312:      */
1313:     public function order($column, $direction = 'ASC')
1314:     {
1315:         if (is_array($column) === TRUE) {
1316:             $this->order = array($column);
1317: 
1318:         } else {
1319:             $this->order[] = array($column, $direction);
1320:         }
1321: 
1322:         return $this;
1323:     }
1324: 
1325: 
1326:     /**
1327:      * Nastavenie limitu počtu položiek výberu
1328:      *
1329:      * @param int štart, ak nie je zadaný aj druhý parameter je tento parameter počet záznamov
1330:      * @param int počet záznamov (nepovinné)
1331:      */
1332:     public function limit()
1333:     {
1334:         $limit = func_get_args();
1335: 
1336:         if (is_array($limit[0]) === TRUE) {
1337:             $this->limit = $limit[0];
1338: 
1339:         } else {
1340:             $this->limit = $limit;
1341:         }
1342: 
1343:         return $this;
1344:     }
1345: 
1346: 
1347:     /**
1348:      * Získanie vzťahov medzi tabuľkami
1349:      *
1350:      * @return array vzťahy medzi tabuľkami v databáze
1351:      */
1352:     public function get_relations()
1353:     {
1354:         $query = 'SELECT TABLE_NAME as `table`, COLUMN_NAME as `column`, REFERENCED_TABLE_NAME as `ref_table`
1355:             FROM information_schema.KEY_COLUMN_USAGE
1356:             WHERE TABLE_SCHEMA = DATABASE()
1357:             AND REFERENCED_TABLE_SCHEMA = DATABASE();';
1358: 
1359:         try {
1360:             $result = $this->_query($query);
1361: 
1362:         } catch (Exception $e) {
1363:             trigger_error('Could not get tables relations!', E_USER_ERROR);
1364:         }
1365: 
1366:         $prefix_len = strlen($this->prefix);
1367: 
1368:         $relations = array();
1369:         foreach ($result->fetch_all() as $relation) {
1370:             $table = substr($relation['table'], $prefix_len);
1371:             $ref_table = substr($relation['ref_table'], $prefix_len);
1372:             $column = $relation['column'];
1373: 
1374:             $relations[$table][$column] = $ref_table;
1375:         }
1376: 
1377:         return $relations;
1378:     }
1379: 
1380: 
1381:     /**
1382:      * Získanie stĺpcov tabuliek v databáze
1383:      *
1384:      * @return array stĺpce tabuliek v databáze
1385:      */
1386:     public function get_tables_columns()
1387:     {
1388:         $query = 'SELECT `TABLE_NAME` AS `table`, `COLUMN_NAME` AS `column`, `DATA_TYPE` AS `type`
1389:             FROM `information_schema`.`COLUMNS`
1390:             WHERE `TABLE_SCHEMA` = DATABASE()';
1391: 
1392:         try {
1393:             $result = $this->_query($query);
1394: 
1395:         } catch (Exception $e) {
1396:             trigger_error('Could not get tables columns!', E_USER_ERROR);
1397:         }
1398: 
1399:         $prefix_len = strlen($this->prefix);
1400: 
1401:         $tables = array();
1402:         foreach ($result->fetch_all() as $row) {
1403:             $table = substr($row['table'], $prefix_len);
1404:             $column = $row['column'];
1405:             $type = $row['type'];
1406: 
1407:             $tables[$table][$column] = $type;
1408:         }
1409: 
1410:         return $tables;
1411:     }
1412: }
1413: 
1414: 
1415: /**
1416:  * Onion Framework - Iterátor výsledkov dopytu do databázy
1417:  *
1418:  * Copyright (c) 2011 Jano Gašpar (http://webstranky.net)
1419:  *
1420:  * @author    Jano Gašpar
1421:  * @copyright Copyright (c) 2011 Jano Gašpar
1422:  * @package   Onion::Core
1423:  **/
1424: 
1425: class DatabaseResult implements Iterator, Countable
1426: {
1427:     /**
1428:      * @var resource
1429:      */
1430:     private $result;
1431: 
1432: 
1433:     /**
1434:      * @var int aktuálny riadok
1435:      */
1436:     private $row;
1437: 
1438: 
1439:     /**
1440:     * @var int ukzaovateľ na aktuálny riadok pre Iterator
1441:     */
1442:     private $pointer;
1443: 
1444: 
1445:     /**
1446:      * Konštruktor
1447:      *
1448:      * @param resource výsledok dopytu do databázy
1449:      * @return void
1450:      */
1451:     public function __construct($result)
1452:     {
1453:         $this->result = $result;
1454:     }
1455: 
1456: 
1457:     /**
1458:      * Získaznie aktuálneho riadku z výsledkov
1459:      */
1460:     public function fetch()
1461:     {
1462:         $this->pointer++;
1463: 
1464:         return mysql_fetch_assoc($this->result, MYSQL_ASSOC);
1465:     }
1466: 
1467: 
1468:     /**
1469:      * Získanie hodnoty stĺpca z aktuálneho riadku
1470:      *
1471:      * @param  string meno stlpca
1472:      * @return mixed  hodnota stĺpca
1473:      */
1474:     public function fetch_single($column = NULL)
1475:     {
1476:         $row = mysql_fetch_assoc($this->result, MYSQL_ASSOC);
1477: 
1478:         if (is_array($row) === FALSE) {
1479:             return FALSE;
1480:         }
1481: 
1482:         $this->pointer++;
1483: 
1484:         if ($column === NULL) {
1485:             return reset($row);
1486:         }
1487: 
1488:         return $row[$column];
1489:     }
1490: 
1491: 
1492:     /**
1493:      * Získanie celého výsledku dopytu
1494:      *
1495:      * @return array výsledok dopytu
1496:      */
1497:     public function fetch_all()
1498:     {
1499:         $this->rewind(TRUE);
1500: 
1501:         $rows = array();
1502: 
1503:         while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1504:             $rows[] = $row;
1505:         }
1506: 
1507:         return $rows;
1508:     }
1509: 
1510: 
1511:     /**
1512:      * Získanie celého výsledku dopytu do associatívneho poľa, kde kľúčom bude
1513:      * hodnota zvoleného stĺpca
1514:      *
1515:      * @return array výsledok dopytu
1516:      */
1517:     public function fetch_assoc($key)
1518:     {
1519:         $this->rewind(TRUE);
1520: 
1521:         $rows = array();
1522: 
1523:         while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1524:             $rows[$row[$key]] = $row;
1525:         }
1526: 
1527:         return $rows;
1528:     }
1529: 
1530: 
1531:     /**
1532:      * Získanie hoddnôt zvoleného stĺpca z celého výsledku dopytu
1533:      * do associatívneho poľa, kde kľúčom bude hodnota zvoleného stĺpca v prvom
1534:      * parametry a hodnota je hodnota zvoleného stĺpca v druhom parametry
1535:      *
1536:      * @param  string meno stĺpca, ktorého hodnta bude použitá ako kľúč
1537:      * @param  string meno stĺpca, ktorého hodnta bude použitá ako hodnota
1538:      * @return array  výsledok dopytu
1539:      */
1540:     public function fetch_pairs($key, $value)
1541:     {
1542:         $this->rewind(TRUE);
1543: 
1544:         $rows = array();
1545: 
1546:         while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1547:             $rows[$row[$key]] = $row[$value];
1548:         }
1549: 
1550:         return $rows;
1551:     }
1552: 
1553: 
1554:     /**
1555:      * Získanie hodnôt zvoleného stĺpca z celého dopytu
1556:      *
1557:      * @param  string meno stĺpca, ktorého hodnoty sa majú získať
1558:      * @return array  hodnoty zvoleného stĺpca
1559:      */
1560:     public function fetch_values($key)
1561:     {
1562:         $this->rewind(TRUE);
1563: 
1564:         $values = array();
1565: 
1566:         while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1567:             $values[] = $row[$key];
1568:         }
1569: 
1570:         return $values;
1571:     }
1572: 
1573: 
1574:     /**
1575:      * Pretočenie Iteratoru na začiatok
1576:      *
1577:      * @param  bool príznak či sa má iba pretočiť na začiatok,
1578:      *              alebo sa má aj vrátiť obsah prvého riadku
1579:      * @return void
1580:      */
1581:     public function rewind($only_seek = FALSE)
1582:     {
1583:         $this->pointer = 0;
1584:         if (mysql_num_rows($this->result) > 0) {
1585:             mysql_data_seek($this->result, 0);
1586:         }
1587: 
1588:         if ($only_seek === TRUE) {
1589:             return;
1590:         }
1591: 
1592:         $this->row = mysql_fetch_assoc($this->result);
1593:     }
1594: 
1595: 
1596:     /**
1597:      * Získanie kľúča aktuálneho prvku iterátora
1598:      *
1599:      * @return mixed
1600:      */
1601:     public function key()
1602:     {
1603:         return $this->pointer;
1604:     }
1605: 
1606: 
1607:     /**
1608:      * Získanie aktuálneho prvku Iteratora
1609:      *
1610:      * @return mixed
1611:      */
1612:     public function current()
1613:     {
1614:         return $this->row;
1615:     }
1616: 
1617: 
1618:     /**
1619:      * Posunutie Iteratora na nasledujúci prvok
1620:      *
1621:      * @return void
1622:      */
1623:     public function next()
1624:     {
1625:         $this->row = mysql_fetch_assoc($this->result);
1626:         $this->pointer++;
1627:     }
1628: 
1629: 
1630:     /**
1631:      * Kontrola či existuje prvok po volaní rewind() alebo next()
1632:      *
1633:      * @return bool
1634:      */
1635:     public function valid()
1636:     {
1637:         return empty($this->row) === FALSE;
1638:     }
1639: 
1640: 
1641:     /**
1642:      * Získanie počtu riadkow, pre Countable
1643:      *
1644:      * @return int
1645:      */
1646:     public function count()
1647:     {
1648:         return mysql_num_rows($this->result);
1649:     }
1650: }
1651: 
Onion API documentation generated by ApiGen.
Generated using the TokenReflection library.