├── .gitmodules ├── .travis.yml ├── CodeIgniter ├── CI_MX_phpStorm.php ├── CI_phpStorm.php ├── DB_active_rec.php └── my_models.php ├── CodeIgniter3 ├── CI_phpStorm.php ├── DB_active_rec.php └── my_models.php ├── OpenCart ├── controller.php ├── loader.php └── model.php ├── README.md ├── WordPress └── wpdb_docs.php ├── example.php ├── phpBB3.php ├── plugins ├── CodeIgniter │ └── META-INF │ │ └── plugin.xml ├── OpenCart │ └── META-INF │ │ └── plugin.xml └── WordPress │ └── META-INF │ └── plugin.xml └── screenshots └── CodeIgniter ├── acive_record1.png ├── active_record2.png ├── active_record3.png ├── driver_result.png ├── info-blocks.png ├── mark-plain.png ├── methods.png └── properties.png /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "laravel-helpers"] 2 | path = laravel-helpers 3 | url = https://github.com/danielboendergaard/laravel-helpers.git 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: generic 2 | script: 3 | - cp -r ./CodeIgniter plugins/CodeIgniter/ 4 | - cd plugins/CodeIgniter/ 5 | - zip -r ../../codeigniter-code-completion-helper-$TRAVIS_TAG.jar * 6 | - cd ../../ 7 | - cp -r ./OpenCart plugins/OpenCart/ 8 | - cd plugins/OpenCart/ 9 | - zip -r ../../opencart-code-completion-helper-$TRAVIS_TAG.jar * 10 | - cd ../../ 11 | - cp -r ./WordPress plugins/WordPress/ 12 | - cd plugins/WordPress/ 13 | - zip -r ../../wordpress-code-completion-helper-$TRAVIS_TAG.jar * 14 | - cd ../../ 15 | deploy: 16 | provider: releases 17 | api_key: 18 | secure: QmUSrY2z1c78xGxXnFq68My+r9gohsa+txDW6DlQPhxvIpbXbAF50y8B0xpSYLIMPYFoFJYguVJDm04UMTQcLE8W1ftrWHsSGwF1PaxvgX1yWFXtPRQ84N2HSKUtvPY/2RMd5oaDznHeN8koINfSkRPrcdtwLpJ83MXKn+fEbfuZIneJjdzzJJ3R5yU+nTLL+j85Gqy+g9pY29sKOLV7JHN61TrFsMC/YKDcjuMMHGjPAkodtdFmQYgF8WZgwc1OEED6iZlvBHNaMTamf+BEG4SNQT/4cpV1uwXCxBAkeDwsRfzSShAITNDPtd90JJOId5mDFVKj5n8RC/ZgHMZl/io21oZKHNRxgDrKHYmWuTZNwe+tNsQPmDBXe3munMoFiXusa2lNAddrswjQLhjvFZZGxT+r/FtebaJsu9TULGqpB2n/QycOZx2n/2bR8PFYtbpTC66LGMnBvsNL+EotJm9m531ImodKT7c2mXFz+bIXa3DdgELt4aaCrxaE06zJ3XX+gfDmlLZUZ4E90K3S/F+tdtO1d0amaAlJkOoPrZLleXdvAERrELKzJ56miZfHckZjNIo7U+aqDardlZC8kQ2z0hdrUNnFEmGZJZxKgOH9W3OCy2AjCmhBwxtzrisVqmLn0KtmgOrZxWdsUk+oR4RErYsjEu2oRA8EOjAyd10= 19 | file: 20 | - codeigniter-code-completion-helper-$TRAVIS_TAG.jar 21 | - opencart-code-completion-helper-$TRAVIS_TAG.jar 22 | - wordpress-code-completion-helper-$TRAVIS_TAG.jar 23 | skip_cleanup: true 24 | on: 25 | tags: true -------------------------------------------------------------------------------- /CodeIgniter/CI_MX_phpStorm.php: -------------------------------------------------------------------------------- 1 | Memory consumption can also be displayed. 31 | * @property CI_Calendar $calendar This class enables the creation of calendars 32 | * @property CI_Cart $cart Shopping Cart Class 33 | * @property CI_Config $config This class contains functions that enable config files to be managed 34 | * @property CI_Controller $controller This class object is the super class that every library in.
CodeIgniter will be assigned to. 35 | * @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP. 36 | * @property CI_Encrypt $encrypt Provides two-way keyed encoding using XOR Hashing and Mcrypt 37 | * @property CI_Exceptions $exceptions Exceptions Class 38 | * @property CI_Form_validation $form_validation Form Validation Class 39 | * @property CI_Ftp $ftp FTP Class 40 | * @property CI_Hooks $hooks //dead 41 | * @property CI_Image_lib $image_lib Image Manipulation class 42 | * @property CI_Input $input Pre-processes global input data for security 43 | * @property CI_Lang $lang Language Class 44 | * @property CI_Loader $load Loads views and files 45 | * @property CI_Log $log Logging Class 46 | * @property CI_Model $model CodeIgniter Model Class 47 | * @property CI_Output $output Responsible for sending final output to browser 48 | * @property CI_Pagination $pagination Pagination Class 49 | * @property CI_Parser $parser Parses pseudo-variables contained in the specified template view,
replacing them with the data in the second param 50 | * @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data
in order to help with debugging and optimization. 51 | * @property CI_Router $router Parses URIs and determines routing 52 | * @property CI_Session $session Session Class 53 | * @property CI_Sha1 $sha1 Provides 160 bit hashing using The Secure Hash Algorithm 54 | * @property CI_Table $table HTML table generation
Lets you create tables manually or from database result objects, or arrays. 55 | * @property CI_Trackback $trackback Trackback Sending/Receiving Class 56 | * @property CI_Typography $typography Typography Class 57 | * @property CI_Unit_test $unit_test Simple testing class 58 | * @property CI_Upload $upload File Uploading Class 59 | * @property CI_URI $uri Parses URIs and determines routing 60 | * @property CI_User_agent $user_agent Identifies the platform, browser, robot, or mobile devise of the browsing agent 61 | * @property CI_Validation $validation //dead 62 | * @property CI_Xmlrpc $xmlrpc XML-RPC request handler class 63 | * @property CI_Xmlrpcs $xmlrpcs XML-RPC server class 64 | * @property CI_Zip $zip Zip Compression Class 65 | * @property CI_Javascript $javascript Javascript Class 66 | * @property CI_Jquery $jquery Jquery Class 67 | * @property CI_Utf8 $utf8 Provides support for UTF-8 environments 68 | * @property CI_Security $security Security Class, xss, csrf, etc... 69 | * @property CI_Driver_Library $driver CodeIgniter Driver Library Class 70 | * @property CI_Cache $cache CodeIgniter Caching Class 71 | * @method static CI_Controller get_instance() CodeIgniter CI_Controller instance class 72 | */ 73 | class CI_Controller extends my_models 74 | { 75 | public function __construct() {} //This default return construct as set 76 | } 77 | 78 | /** 79 | * @property CI_DB_active_record $db This is the platform-independent base Active Record implementation class. 80 | * @property CI_DB_forge $dbforge Database Utility Class 81 | * @property CI_Benchmark $benchmark This class enables you to mark points and calculate the time difference between them.
Memory consumption can also be displayed. 82 | * @property CI_Calendar $calendar This class enables the creation of calendars 83 | * @property CI_Cart $cart Shopping Cart Class 84 | * @property CI_Config $config This class contains functions that enable config files to be managed 85 | * @property CI_Controller $controller This class object is the super class that every library in.
CodeIgniter will be assigned to. 86 | * @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP. 87 | * @property CI_Encrypt $encrypt Provides two-way keyed encoding using XOR Hashing and Mcrypt 88 | * @property CI_Exceptions $exceptions Exceptions Class 89 | * @property CI_Form_validation $form_validation Form Validation Class 90 | * @property CI_Ftp $ftp FTP Class 91 | * @property CI_Hooks $hooks //dead 92 | * @property CI_Image_lib $image_lib Image Manipulation class 93 | * @property CI_Input $input Pre-processes global input data for security 94 | * @property CI_Lang $lang Language Class 95 | * @property CI_Loader $load Loads views and files 96 | * @property CI_Log $log Logging Class 97 | * @property CI_Model $model CodeIgniter Model Class 98 | * @property CI_Output $output Responsible for sending final output to browser 99 | * @property CI_Pagination $pagination Pagination Class 100 | * @property CI_Parser $parser Parses pseudo-variables contained in the specified template view,
replacing them with the data in the second param 101 | * @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data
in order to help with debugging and optimization. 102 | * @property CI_Router $router Parses URIs and determines routing 103 | * @property CI_Session $session Session Class 104 | * @property CI_Sha1 $sha1 Provides 160 bit hashing using The Secure Hash Algorithm 105 | * @property CI_Table $table HTML table generation
Lets you create tables manually or from database result objects, or arrays. 106 | * @property CI_Trackback $trackback Trackback Sending/Receiving Class 107 | * @property CI_Typography $typography Typography Class 108 | * @property CI_Unit_test $unit_test Simple testing class 109 | * @property CI_Upload $upload File Uploading Class 110 | * @property CI_URI $uri Parses URIs and determines routing 111 | * @property CI_User_agent $user_agent Identifies the platform, browser, robot, or mobile devise of the browsing agent 112 | * @property CI_Validation $validation //dead 113 | * @property CI_Xmlrpc $xmlrpc XML-RPC request handler class 114 | * @property CI_Xmlrpcs $xmlrpcs XML-RPC server class 115 | * @property CI_Zip $zip Zip Compression Class 116 | * @property CI_Javascript $javascript Javascript Class 117 | * @property CI_Jquery $jquery Jquery Class 118 | * @property CI_Utf8 $utf8 Provides support for UTF-8 environments 119 | * @property CI_Security $security Security Class, xss, csrf, etc... 120 | * @property CI_Driver_Library $driver CodeIgniter Driver Library Class 121 | * @property CI_Cache $cache CodeIgniter Caching Class 122 | */ 123 | class CI_Model extends my_models 124 | { 125 | public function __construct() {} //This default return construct as set 126 | } 127 | -------------------------------------------------------------------------------- /CodeIgniter/DB_active_rec.php: -------------------------------------------------------------------------------- 1 | ar_select[] = $val; 147 | $this->ar_no_escape[] = $escape; 148 | 149 | if ($this->ar_caching === TRUE) 150 | { 151 | $this->ar_cache_select[] = $val; 152 | $this->ar_cache_exists[] = 'select'; 153 | $this->ar_cache_no_escape[] = $escape; 154 | } 155 | } 156 | } 157 | return $this; 158 | } 159 | 160 | // -------------------------------------------------------------------- 161 | 162 | /** 163 | * Select Max 164 | * 165 | * Generates a SELECT MAX(field) portion of a query 166 | * 167 | * @param string $select the field 168 | * @param string $alias an alias 169 | * 170 | * @return CI_DB_active_record|CI_DB_result 171 | */ 172 | public function select_max($select = '', $alias = '') 173 | { 174 | $this->_max_min_avg_sum($select, $alias, 'MAX'); 175 | 176 | return $this; 177 | } 178 | 179 | // -------------------------------------------------------------------- 180 | 181 | /** 182 | * Select Min 183 | * 184 | * Generates a SELECT MIN(field) portion of a query 185 | * 186 | * @param string $select the field 187 | * @param string $alias an alias 188 | * 189 | * @return CI_DB_active_record|CI_DB_result 190 | */ 191 | public function select_min($select = '', $alias = '') 192 | { 193 | $this->_max_min_avg_sum($select, $alias, 'MIN'); 194 | 195 | return $this; 196 | } 197 | 198 | // -------------------------------------------------------------------- 199 | 200 | /** 201 | * Select Average 202 | * 203 | * Generates a SELECT AVG(field) portion of a query 204 | * 205 | * @param string $select the field 206 | * @param string $alias an alias 207 | * 208 | * @return CI_DB_active_record|CI_DB_result 209 | */ 210 | public function select_avg($select = '', $alias = '') 211 | { 212 | $this->_max_min_avg_sum($select, $alias, 'AVG'); 213 | 214 | return $this; 215 | } 216 | 217 | // -------------------------------------------------------------------- 218 | 219 | /** 220 | * Select Sum 221 | * 222 | * Generates a SELECT SUM(field) portion of a query 223 | * 224 | * @param string $select the field 225 | * @param string $alias an alias 226 | * 227 | * @return CI_DB_active_record|CI_DB_result 228 | */ 229 | public function select_sum($select = '', $alias = '') 230 | { 231 | return $this->_max_min_avg_sum($select, $alias, 'SUM'); 232 | } 233 | 234 | // -------------------------------------------------------------------- 235 | 236 | /** 237 | * Processing Function for the four functions above: 238 | * 239 | * select_max() 240 | * select_min() 241 | * select_avg() 242 | * select_sum() 243 | * 244 | * @param string $select the string field 245 | * @param string $alias an string alias 246 | * @param string $type 247 | * 248 | * @return CI_DB_active_record|CI_DB_result 249 | */ 250 | protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') 251 | { 252 | if (!is_string($select) OR $select == '') 253 | { 254 | $this->display_error('db_invalid_query'); 255 | } 256 | 257 | $type = strtoupper($type); 258 | 259 | if (!in_array($type, array( 260 | 'MAX', 261 | 'MIN', 262 | 'AVG', 263 | 'SUM' 264 | )) 265 | ) 266 | { 267 | show_error('Invalid function type: ' . $type); 268 | } 269 | 270 | if ($alias == '') 271 | { 272 | $alias = $this->_create_alias_from_table(trim($select)); 273 | } 274 | 275 | $sql = $type . '(' . $this->_protect_identifiers(trim($select)) . ') AS ' . $alias; 276 | 277 | $this->ar_select[] = $sql; 278 | 279 | if ($this->ar_caching === TRUE) 280 | { 281 | $this->ar_cache_select[] = $sql; 282 | $this->ar_cache_exists[] = 'select'; 283 | } 284 | 285 | return $this; 286 | } 287 | 288 | // -------------------------------------------------------------------- 289 | 290 | /** 291 | * Determines the alias name based on the table 292 | * 293 | * @param string $item 294 | * 295 | * @return string 296 | */ 297 | protected function _create_alias_from_table($item) 298 | { 299 | if (strpos($item, '.') !== FALSE) 300 | { 301 | return end(explode('.', $item)); 302 | } 303 | 304 | return $item; 305 | } 306 | 307 | // -------------------------------------------------------------------- 308 | 309 | /** 310 | * DISTINCT 311 | * 312 | * Sets a flag which tells the query string compiler to add DISTINCT 313 | * 314 | * @param bool $val 315 | * 316 | * @return CI_DB_active_record|CI_DB_result 317 | */ 318 | public function distinct($val = TRUE) 319 | { 320 | $this->ar_distinct = (is_bool($val)) ? $val : TRUE; 321 | return $this; 322 | } 323 | 324 | // -------------------------------------------------------------------- 325 | 326 | /** 327 | * From 328 | * 329 | * Generates the FROM portion of the query 330 | * 331 | * @param mixed $from can be a string or array 332 | * 333 | * @return CI_DB_active_record|CI_DB_result 334 | */ 335 | public function from($from) 336 | { 337 | foreach ((array) $from as $val) 338 | { 339 | if (strpos($val, ',') !== FALSE) 340 | { 341 | foreach (explode(',', $val) as $v) 342 | { 343 | $v = trim($v); 344 | $this->_track_aliases($v); 345 | 346 | $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); 347 | 348 | if ($this->ar_caching === TRUE) 349 | { 350 | $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); 351 | $this->ar_cache_exists[] = 'from'; 352 | } 353 | } 354 | 355 | } 356 | else 357 | { 358 | $val = trim($val); 359 | 360 | // Extract any aliases that might exist. We use this information 361 | // in the _protect_identifiers to know whether to add a table prefix 362 | $this->_track_aliases($val); 363 | 364 | $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE); 365 | 366 | if ($this->ar_caching === TRUE) 367 | { 368 | $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE); 369 | $this->ar_cache_exists[] = 'from'; 370 | } 371 | } 372 | } 373 | 374 | return $this; 375 | } 376 | 377 | // -------------------------------------------------------------------- 378 | 379 | /** 380 | * Join 381 | * 382 | * Generates the JOIN portion of the query 383 | * 384 | * @param string $table 385 | * @param string $cond the join condition 386 | * @param string $type the type of join 387 | * 388 | * @return CI_DB_active_record|CI_DB_result 389 | */ 390 | public function join($table, $cond, $type = '') 391 | { 392 | if ($type != '') 393 | { 394 | $type = strtoupper(trim($type)); 395 | 396 | if (!in_array($type, array( 397 | 'LEFT', 398 | 'RIGHT', 399 | 'OUTER', 400 | 'INNER', 401 | 'LEFT OUTER', 402 | 'RIGHT OUTER' 403 | )) 404 | ) 405 | { 406 | $type = ''; 407 | } 408 | else 409 | { 410 | $type .= ' '; 411 | } 412 | } 413 | 414 | // Extract any aliases that might exist. We use this information 415 | // in the _protect_identifiers to know whether to add a table prefix 416 | $this->_track_aliases($table); 417 | 418 | // Strip apart the condition and protect the identifiers 419 | if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match)) 420 | { 421 | $match[1] = $this->_protect_identifiers($match[1]); 422 | $match[3] = $this->_protect_identifiers($match[3]); 423 | 424 | $cond = $match[1] . $match[2] . $match[3]; 425 | } 426 | 427 | // Assemble the JOIN statement 428 | $join = $type . 'JOIN ' . $this->_protect_identifiers($table, TRUE, NULL, FALSE) . ' ON ' . $cond; 429 | 430 | $this->ar_join[] = $join; 431 | if ($this->ar_caching === TRUE) 432 | { 433 | $this->ar_cache_join[] = $join; 434 | $this->ar_cache_exists[] = 'join'; 435 | } 436 | 437 | return $this; 438 | } 439 | 440 | // -------------------------------------------------------------------- 441 | 442 | /** 443 | * Where 444 | * 445 | * Generates the WHERE portion of the query. Separates 446 | * multiple calls with AND 447 | * 448 | * @param mixed $key 449 | * @param null $value 450 | * @param bool $escape 451 | * 452 | * @return CI_DB_active_record|CI_DB_result 453 | */ 454 | public function where($key, $value = NULL, $escape = TRUE) 455 | { 456 | return $this->_where($key, $value, 'AND ', $escape); 457 | } 458 | 459 | // -------------------------------------------------------------------- 460 | 461 | /** 462 | * OR Where 463 | * 464 | * Generates the WHERE portion of the query. Separates 465 | * multiple calls with OR 466 | * 467 | * @param mixed $key 468 | * @param null $value 469 | * @param bool $escape 470 | * 471 | * @return CI_DB_active_record|CI_DB_result 472 | */ 473 | public function or_where($key, $value = NULL, $escape = TRUE) 474 | { 475 | return $this->_where($key, $value, 'OR ', $escape); 476 | } 477 | 478 | // -------------------------------------------------------------------- 479 | 480 | /** 481 | * Where 482 | * 483 | * Called by where() or or_where() 484 | * 485 | * @param mixed $key 486 | * @param null $value 487 | * @param string $type 488 | * @param null $escape 489 | * 490 | * @return CI_DB_active_record|CI_DB_result 491 | */ 492 | protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) 493 | { 494 | if (!is_array($key)) 495 | { 496 | $key = array($key => $value); 497 | } 498 | 499 | // If the escape value was not set will will base it on the global setting 500 | if (!is_bool($escape)) 501 | { 502 | $escape = $this->_protect_identifiers; 503 | } 504 | 505 | foreach ($key as $k => $v) 506 | { 507 | $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type; 508 | 509 | if (is_null($v) && !$this->_has_operator($k)) 510 | { 511 | // value appears not to have been set, assign the test to IS NULL 512 | $k .= ' IS NULL'; 513 | } 514 | 515 | if (!is_null($v)) 516 | { 517 | if ($escape === TRUE) 518 | { 519 | $k = $this->_protect_identifiers($k, FALSE, $escape); 520 | 521 | $v = ' ' . $this->escape($v); 522 | } 523 | 524 | if (!$this->_has_operator($k)) 525 | { 526 | $k .= ' = '; 527 | } 528 | } 529 | else 530 | { 531 | $k = $this->_protect_identifiers($k, FALSE, $escape); 532 | } 533 | 534 | $this->ar_where[] = $prefix . $k . $v; 535 | 536 | if ($this->ar_caching === TRUE) 537 | { 538 | $this->ar_cache_where[] = $prefix . $k . $v; 539 | $this->ar_cache_exists[] = 'where'; 540 | } 541 | 542 | } 543 | 544 | return $this; 545 | } 546 | 547 | // -------------------------------------------------------------------- 548 | 549 | /** 550 | * Where_in 551 | * 552 | * Generates a WHERE field IN ('item', 'item') SQL query joined with 553 | * AND if appropriate 554 | * 555 | * @param string $key The field to search 556 | * @param array $values The values searched on 557 | * 558 | * @return CI_DB_active_record|CI_DB_result 559 | */ 560 | public function where_in($key = NULL, $values = NULL) 561 | { 562 | return $this->_where_in($key, $values); 563 | } 564 | 565 | // -------------------------------------------------------------------- 566 | 567 | /** 568 | * Where_in_or 569 | * 570 | * Generates a WHERE field IN ('item', 'item') SQL query joined with 571 | * OR if appropriate 572 | * 573 | * @param string $key The field to search 574 | * @param array $values The values searched on 575 | * 576 | * @return CI_DB_active_record|CI_DB_result 577 | */ 578 | public function or_where_in($key = NULL, $values = NULL) 579 | { 580 | return $this->_where_in($key, $values, FALSE, 'OR '); 581 | } 582 | 583 | // -------------------------------------------------------------------- 584 | 585 | /** 586 | * Where_not_in 587 | * 588 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined 589 | * with AND if appropriate 590 | * 591 | * @param string $key The field to search 592 | * @param array $values The values searched on 593 | * 594 | * @return CI_DB_active_record|CI_DB_result 595 | */ 596 | public function where_not_in($key = NULL, $values = NULL) 597 | { 598 | return $this->_where_in($key, $values, TRUE); 599 | } 600 | 601 | // -------------------------------------------------------------------- 602 | 603 | /** 604 | * Where_not_in_or 605 | * 606 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined 607 | * with OR if appropriate 608 | * 609 | * @param string $key The field to search 610 | * @param array $values The values searched on 611 | * 612 | * @return CI_DB_active_record|CI_DB_result 613 | */ 614 | public function or_where_not_in($key = NULL, $values = NULL) 615 | { 616 | return $this->_where_in($key, $values, TRUE, 'OR '); 617 | } 618 | 619 | // -------------------------------------------------------------------- 620 | 621 | /** 622 | * Where_in 623 | * 624 | * Called by where_in, where_in_or, where_not_in, where_not_in_or 625 | * 626 | * @param string $key The field to search 627 | * @param array $values The values searched on 628 | * @param boolean $not If the statement would be IN or NOT IN 629 | * @param string $type 630 | * 631 | * @return CI_DB_active_record|CI_DB_result 632 | */ 633 | protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ') 634 | { 635 | if ($key === NULL OR $values === NULL) 636 | { 637 | return null; 638 | } 639 | 640 | if (!is_array($values)) 641 | { 642 | $values = array($values); 643 | } 644 | 645 | $not = ($not) ? ' NOT' : ''; 646 | 647 | foreach ($values as $value) 648 | { 649 | $this->ar_wherein[] = $this->escape($value); 650 | } 651 | 652 | $prefix = (count($this->ar_where) == 0) ? '' : $type; 653 | 654 | $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") "; 655 | 656 | $this->ar_where[] = $where_in; 657 | if ($this->ar_caching === TRUE) 658 | { 659 | $this->ar_cache_where[] = $where_in; 660 | $this->ar_cache_exists[] = 'where'; 661 | } 662 | 663 | // reset the array for multiple calls 664 | $this->ar_wherein = array(); 665 | return $this; 666 | } 667 | 668 | // -------------------------------------------------------------------- 669 | 670 | /** 671 | * Like 672 | * 673 | * Generates a %LIKE% portion of the query. Separates 674 | * multiple calls with AND 675 | * 676 | * @param string $field 677 | * @param string $match 678 | * @param string $side 679 | * 680 | * @return CI_DB_active_record|CI_DB_result 681 | */ 682 | public function like($field, $match = '', $side = 'both') 683 | { 684 | $this->_like($field, $match, 'AND ', $side); 685 | 686 | return $this; 687 | } 688 | 689 | // -------------------------------------------------------------------- 690 | 691 | /** 692 | * Not Like 693 | * 694 | * Generates a NOT LIKE portion of the query. Separates 695 | * multiple calls with AND 696 | * 697 | * @param string $field 698 | * @param string $match 699 | * @param string $side 700 | * 701 | * @return CI_DB_active_record|CI_DB_result 702 | */ 703 | public function not_like($field, $match = '', $side = 'both') 704 | { 705 | $this->_like($field, $match, 'AND ', $side, 'NOT'); 706 | 707 | return $this; 708 | } 709 | 710 | // -------------------------------------------------------------------- 711 | 712 | /** 713 | * OR Like 714 | * 715 | * Generates a %LIKE% portion of the query. Separates 716 | * multiple calls with OR 717 | * 718 | * @param string $field 719 | * @param string $match 720 | * @param string $side 721 | * 722 | * @return CI_DB_active_record|CI_DB_result 723 | */ 724 | public function or_like($field, $match = '', $side = 'both') 725 | { 726 | return $this->_like($field, $match, 'OR ', $side); 727 | } 728 | 729 | // -------------------------------------------------------------------- 730 | 731 | /** 732 | * OR Not Like 733 | * 734 | * Generates a NOT LIKE portion of the query. Separates 735 | * multiple calls with OR 736 | * 737 | * @param string $field 738 | * @param mixed $match 739 | * @param string $side 740 | * 741 | * @return CI_DB_active_record|CI_DB_result 742 | */ 743 | public function or_not_like($field, $match = '', $side = 'both') 744 | { 745 | return $this->_like($field, $match, 'OR ', $side, 'NOT'); 746 | } 747 | 748 | // -------------------------------------------------------------------- 749 | 750 | /** 751 | * Like 752 | * 753 | * Called by like() or orlike() 754 | * 755 | * @param string $field 756 | * @param string $match 757 | * @param string $type 758 | * @param string $side 759 | * @param string $not 760 | * 761 | * @return CI_DB_active_record|CI_DB_result 762 | */ 763 | protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') 764 | { 765 | if (!is_array($field)) 766 | { 767 | $field = array($field => $match); 768 | } 769 | 770 | foreach ($field as $k => $v) 771 | { 772 | $k = $this->_protect_identifiers($k); 773 | 774 | $prefix = (count($this->ar_like) == 0) ? '' : $type; 775 | 776 | $v = $this->escape_like_str($v); 777 | 778 | if ($side == 'before') 779 | { 780 | $like_statement = $prefix . " $k $not LIKE '%{$v}'"; 781 | } 782 | elseif ($side == 'after') 783 | { 784 | $like_statement = $prefix . " $k $not LIKE '{$v}%'"; 785 | } 786 | else 787 | { 788 | $like_statement = $prefix . " $k $not LIKE '%{$v}%'"; 789 | } 790 | 791 | // some platforms require an escape sequence definition for LIKE wildcards 792 | if ($this->_like_escape_str != '') 793 | { 794 | $like_statement = $like_statement . sprintf($this->_like_escape_str, $this->_like_escape_chr); 795 | } 796 | 797 | $this->ar_like[] = $like_statement; 798 | if ($this->ar_caching === TRUE) 799 | { 800 | $this->ar_cache_like[] = $like_statement; 801 | $this->ar_cache_exists[] = 'like'; 802 | } 803 | 804 | } 805 | return $this; 806 | } 807 | 808 | // -------------------------------------------------------------------- 809 | 810 | /** 811 | * GROUP BY 812 | * 813 | * @param string $by 814 | * 815 | * @return CI_DB_active_record|CI_DB_result 816 | */ 817 | public function group_by($by) 818 | { 819 | if (is_string($by)) 820 | { 821 | $by = explode(',', $by); 822 | } 823 | 824 | foreach ($by as $val) 825 | { 826 | $val = trim($val); 827 | 828 | if ($val != '') 829 | { 830 | $this->ar_groupby[] = $this->_protect_identifiers($val); 831 | 832 | if ($this->ar_caching === TRUE) 833 | { 834 | $this->ar_cache_groupby[] = $this->_protect_identifiers($val); 835 | $this->ar_cache_exists[] = 'groupby'; 836 | } 837 | } 838 | } 839 | return $this; 840 | } 841 | 842 | // -------------------------------------------------------------------- 843 | 844 | /** 845 | * Sets the HAVING value 846 | * 847 | * Separates multiple calls with AND 848 | * 849 | * @param mixed $key 850 | * @param string $value 851 | * @param bool $escape 852 | * 853 | * @return CI_DB_active_record|CI_DB_result 854 | */ 855 | public function having($key, $value = '', $escape = TRUE) 856 | { 857 | $this->_having($key, $value, 'AND ', $escape); 858 | 859 | return $this; 860 | } 861 | 862 | // -------------------------------------------------------------------- 863 | 864 | /** 865 | * Sets the OR HAVING value 866 | * 867 | * Separates multiple calls with OR 868 | * 869 | * @param mixed $key 870 | * @param string $value 871 | * @param bool $escape 872 | * 873 | * @return CI_DB_active_record|CI_DB_result 874 | */ 875 | public function or_having($key, $value = '', $escape = TRUE) 876 | { 877 | $this->_having($key, $value, 'OR ', $escape); 878 | 879 | return $this; 880 | } 881 | 882 | // -------------------------------------------------------------------- 883 | 884 | /** 885 | * Sets the HAVING values 886 | * 887 | * Called by having() or or_having() 888 | * 889 | * @param mixed $key 890 | * @param string $value 891 | * @param string $type 892 | * @param bool $escape 893 | * 894 | * @return CI_DB_active_record|CI_DB_result 895 | */ 896 | protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE) 897 | { 898 | if (!is_array($key)) 899 | { 900 | $key = array($key => $value); 901 | } 902 | 903 | foreach ($key as $k => $v) 904 | { 905 | $prefix = (count($this->ar_having) == 0) ? '' : $type; 906 | 907 | if ($escape === TRUE) 908 | { 909 | $k = $this->_protect_identifiers($k); 910 | } 911 | 912 | if (!$this->_has_operator($k)) 913 | { 914 | $k .= ' = '; 915 | } 916 | 917 | if ($v != '') 918 | { 919 | $v = ' ' . $this->escape($v); 920 | } 921 | 922 | $this->ar_having[] = $prefix . $k . $v; 923 | if ($this->ar_caching === TRUE) 924 | { 925 | $this->ar_cache_having[] = $prefix . $k . $v; 926 | $this->ar_cache_exists[] = 'having'; 927 | } 928 | } 929 | 930 | return $this; 931 | } 932 | 933 | // -------------------------------------------------------------------- 934 | 935 | /** 936 | * Sets the ORDER BY value 937 | * 938 | * @param string $orderby 939 | * @param string $direction: asc or desc 940 | * 941 | * @return CI_DB_active_record|CI_DB_result 942 | */ 943 | public function order_by($orderby, $direction = '') 944 | { 945 | if (strtolower($direction) == 'random') 946 | { 947 | $orderby = ''; // Random results want or don't need a field name 948 | $direction = $this->_random_keyword; 949 | } 950 | elseif (trim($direction) != '') 951 | { 952 | $direction = (in_array(strtoupper(trim($direction)), array( 953 | 'ASC', 954 | 'DESC' 955 | ), TRUE)) ? ' ' . $direction : ' ASC'; 956 | } 957 | 958 | if (strpos($orderby, ',') !== FALSE) 959 | { 960 | $temp = array(); 961 | foreach (explode(',', $orderby) as $part) 962 | { 963 | $part = trim($part); 964 | if (!in_array($part, $this->ar_aliased_tables)) 965 | { 966 | $part = $this->_protect_identifiers(trim($part)); 967 | } 968 | 969 | $temp[] = $part; 970 | } 971 | 972 | $orderby = implode(', ', $temp); 973 | } 974 | else if ($direction != $this->_random_keyword) 975 | { 976 | $orderby = $this->_protect_identifiers($orderby); 977 | } 978 | 979 | $orderby_statement = $orderby . $direction; 980 | 981 | $this->ar_orderby[] = $orderby_statement; 982 | if ($this->ar_caching === TRUE) 983 | { 984 | $this->ar_cache_orderby[] = $orderby_statement; 985 | $this->ar_cache_exists[] = 'orderby'; 986 | } 987 | 988 | return $this; 989 | } 990 | 991 | // -------------------------------------------------------------------- 992 | 993 | /** 994 | * Sets the LIMIT value 995 | * 996 | * @param integer $value the offset value 997 | * @param string $offset 998 | * 999 | * @return CI_DB_active_record|CI_DB_result 1000 | */ 1001 | public function limit($value, $offset = '') 1002 | { 1003 | $this->ar_limit = (int) $value; 1004 | 1005 | if ($offset != '') 1006 | { 1007 | $this->ar_offset = (int) $offset; 1008 | } 1009 | 1010 | return $this; 1011 | } 1012 | 1013 | // -------------------------------------------------------------------- 1014 | 1015 | /** 1016 | * Sets the OFFSET value 1017 | * 1018 | * @param integer $offset the offset value 1019 | * 1020 | * @return CI_DB_active_record|CI_DB_result 1021 | */ 1022 | public function offset($offset) 1023 | { 1024 | $this->ar_offset = $offset; 1025 | return $this; 1026 | } 1027 | 1028 | // -------------------------------------------------------------------- 1029 | 1030 | /** 1031 | * The "set" function. Allows key/value pairs to be set for inserting or updating 1032 | * 1033 | * @param mixed $key 1034 | * @param string $value 1035 | * @param boolean $escape 1036 | * 1037 | * @return CI_DB_active_record|CI_DB_result 1038 | */ 1039 | public function set($key, $value = '', $escape = TRUE) 1040 | { 1041 | $key = $this->_object_to_array($key); 1042 | 1043 | if (!is_array($key)) 1044 | { 1045 | $key = array($key => $value); 1046 | } 1047 | 1048 | foreach ($key as $k => $v) 1049 | { 1050 | if ($escape === FALSE) 1051 | { 1052 | $this->ar_set[$this->_protect_identifiers($k)] = $v; 1053 | } 1054 | else 1055 | { 1056 | $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); 1057 | } 1058 | } 1059 | 1060 | return $this; 1061 | } 1062 | 1063 | // -------------------------------------------------------------------- 1064 | 1065 | /** 1066 | * Get 1067 | * 1068 | * Compiles the select statement based on the other functions called 1069 | * and runs the query 1070 | * 1071 | * @param string $table the table 1072 | * @param string $limit the limit clause 1073 | * @param string $offset the offset clause 1074 | * 1075 | * @return mixed $result 1076 | */ 1077 | public function get($table = '', $limit = null, $offset = null) 1078 | { 1079 | if ($table != '') 1080 | { 1081 | $this->_track_aliases($table); 1082 | $this->from($table); 1083 | } 1084 | 1085 | if (!is_null($limit)) 1086 | { 1087 | $this->limit($limit, $offset); 1088 | } 1089 | 1090 | $sql = $this->_compile_select(); 1091 | 1092 | $result = $this->query($sql); 1093 | $this->_reset_select(); 1094 | 1095 | return $result; 1096 | } 1097 | 1098 | /** 1099 | * "Count All Results" query 1100 | * 1101 | * Generates a platform-specific query string that counts all records 1102 | * returned by an Active Record query. 1103 | * 1104 | * @param string $table 1105 | * 1106 | * @return int 1107 | */ 1108 | public function count_all_results($table = '') 1109 | { 1110 | if ($table != '') 1111 | { 1112 | $this->_track_aliases($table); 1113 | $this->from($table); 1114 | } 1115 | 1116 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows')); 1117 | 1118 | $query = $this->query($sql); 1119 | $this->_reset_select(); 1120 | 1121 | if ($query->num_rows() == 0) 1122 | { 1123 | return 0; 1124 | } 1125 | 1126 | $row = $query->row(); 1127 | return (int) $row->numrows; 1128 | } 1129 | 1130 | // -------------------------------------------------------------------- 1131 | 1132 | /** 1133 | * Get_Where 1134 | * 1135 | * Allows the where clause, limit and offset to be added directly 1136 | * 1137 | * @param string $table 1138 | * @param null $where the string where clause 1139 | * @param null $limit the string limit clause 1140 | * @param null $offset the string offset clause 1141 | * 1142 | * @return CI_DB_active_record|CI_DB_result 1143 | */ 1144 | public function get_where($table = '', $where = null, $limit = null, $offset = null) 1145 | { 1146 | if ($table != '') 1147 | { 1148 | $this->from($table); 1149 | } 1150 | 1151 | if (!is_null($where)) 1152 | { 1153 | $this->where($where); 1154 | } 1155 | 1156 | if (!is_null($limit)) 1157 | { 1158 | $this->limit($limit, $offset); 1159 | } 1160 | 1161 | $sql = $this->_compile_select(); 1162 | 1163 | $result = $this->query($sql); 1164 | $this->_reset_select(); 1165 | return $result; 1166 | } 1167 | 1168 | // -------------------------------------------------------------------- 1169 | 1170 | /** 1171 | * Insert_Batch 1172 | * 1173 | * Compiles batch insert strings and runs the queries 1174 | * 1175 | * @param string $table the table to retrieve the results from 1176 | * @param array $set an associative array of insert values 1177 | * 1178 | * @return CI_DB_active_record|CI_DB_result 1179 | */ 1180 | public function insert_batch($table = '', $set = NULL) 1181 | { 1182 | if (!is_null($set)) 1183 | { 1184 | $this->set_insert_batch($set); 1185 | } 1186 | 1187 | if (count($this->ar_set) == 0) 1188 | { 1189 | if ($this->db_debug) 1190 | { 1191 | //No valid data array. Folds in cases where keys and values did not match up 1192 | return $this->display_error('db_must_use_set'); 1193 | } 1194 | return FALSE; 1195 | } 1196 | 1197 | if ($table == '') 1198 | { 1199 | if (!isset($this->ar_from[0])) 1200 | { 1201 | if ($this->db_debug) 1202 | { 1203 | return $this->display_error('db_must_set_table'); 1204 | } 1205 | return FALSE; 1206 | } 1207 | 1208 | $table = $this->ar_from[0]; 1209 | } 1210 | 1211 | // Batch this baby 1212 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) 1213 | { 1214 | 1215 | $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100)); 1216 | 1217 | //echo $sql; 1218 | 1219 | $this->query($sql); 1220 | } 1221 | 1222 | $this->_reset_write(); 1223 | 1224 | return TRUE; 1225 | } 1226 | 1227 | // -------------------------------------------------------------------- 1228 | 1229 | /** 1230 | * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts 1231 | * 1232 | * @param mixed $key 1233 | * @param string $value 1234 | * @param boolean $escape 1235 | * 1236 | * @return CI_DB_active_record|CI_DB_result 1237 | */ 1238 | public function set_insert_batch($key, $value = '', $escape = TRUE) 1239 | { 1240 | $key = $this->_object_to_array_batch($key); 1241 | 1242 | if (!is_array($key)) 1243 | { 1244 | $key = array($key => $value); 1245 | } 1246 | 1247 | $keys = array_keys(current($key)); 1248 | sort($keys); 1249 | 1250 | foreach ($key as $row) 1251 | { 1252 | if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) 1253 | { 1254 | // batch function above returns an error on an empty array 1255 | $this->ar_set[] = array(); 1256 | return; 1257 | } 1258 | 1259 | ksort($row); // puts $row in the same order as our keys 1260 | 1261 | if ($escape === FALSE) 1262 | { 1263 | $this->ar_set[] = '(' . implode(',', $row) . ')'; 1264 | } 1265 | else 1266 | { 1267 | $clean = array(); 1268 | 1269 | foreach ($row as $value) 1270 | { 1271 | $clean[] = $this->escape($value); 1272 | } 1273 | 1274 | $this->ar_set[] = '(' . implode(',', $clean) . ')'; 1275 | } 1276 | } 1277 | 1278 | foreach ($keys as $k) 1279 | { 1280 | $this->ar_keys[] = $this->_protect_identifiers($k); 1281 | } 1282 | 1283 | return $this; 1284 | } 1285 | 1286 | // -------------------------------------------------------------------- 1287 | 1288 | /** 1289 | * Insert 1290 | * 1291 | * Compiles an insert string and runs the query 1292 | * 1293 | * @param string $table the table to insert data into 1294 | * @param array $set an associative array of insert values 1295 | * 1296 | * @return CI_DB_active_record|CI_DB_result 1297 | */ 1298 | function insert($table = '', $set = NULL) 1299 | { 1300 | if (!is_null($set)) 1301 | { 1302 | $this->set($set); 1303 | } 1304 | 1305 | if (count($this->ar_set) == 0) 1306 | { 1307 | if ($this->db_debug) 1308 | { 1309 | return $this->display_error('db_must_use_set'); 1310 | } 1311 | return FALSE; 1312 | } 1313 | 1314 | if ($table == '') 1315 | { 1316 | if (!isset($this->ar_from[0])) 1317 | { 1318 | if ($this->db_debug) 1319 | { 1320 | return $this->display_error('db_must_set_table'); 1321 | } 1322 | return FALSE; 1323 | } 1324 | 1325 | $table = $this->ar_from[0]; 1326 | } 1327 | 1328 | $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); 1329 | 1330 | $this->_reset_write(); 1331 | return $this->query($sql); 1332 | } 1333 | 1334 | // -------------------------------------------------------------------- 1335 | 1336 | /** 1337 | * Replace 1338 | * 1339 | * Compiles an replace into string and runs the query 1340 | * 1341 | * @param string $table the table to replace data into 1342 | * @param array $set an associative array of insert values 1343 | * 1344 | * @return CI_DB_active_record|CI_DB_result 1345 | */ 1346 | public function replace($table = '', $set = NULL) 1347 | { 1348 | if (!is_null($set)) 1349 | { 1350 | $this->set($set); 1351 | } 1352 | 1353 | if (count($this->ar_set) == 0) 1354 | { 1355 | if ($this->db_debug) 1356 | { 1357 | return $this->display_error('db_must_use_set'); 1358 | } 1359 | return FALSE; 1360 | } 1361 | 1362 | if ($table == '') 1363 | { 1364 | if (!isset($this->ar_from[0])) 1365 | { 1366 | if ($this->db_debug) 1367 | { 1368 | return $this->display_error('db_must_set_table'); 1369 | } 1370 | return FALSE; 1371 | } 1372 | 1373 | $table = $this->ar_from[0]; 1374 | } 1375 | 1376 | $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); 1377 | 1378 | $this->_reset_write(); 1379 | return $this->query($sql); 1380 | } 1381 | 1382 | // -------------------------------------------------------------------- 1383 | 1384 | /** 1385 | * Update 1386 | * 1387 | * Compiles an update string and runs the query 1388 | * 1389 | * @param string $table the table to retrieve the results from 1390 | * @param array $set an associative array of update values 1391 | * @param mixed $where the where clause 1392 | * @param bool $limit 1393 | * 1394 | * @return CI_DB_active_record|CI_DB_result 1395 | */ 1396 | public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) 1397 | { 1398 | // Combine any cached components with the current statements 1399 | $this->_merge_cache(); 1400 | 1401 | if (!is_null($set)) 1402 | { 1403 | $this->set($set); 1404 | } 1405 | 1406 | if (count($this->ar_set) == 0) 1407 | { 1408 | if ($this->db_debug) 1409 | { 1410 | return $this->display_error('db_must_use_set'); 1411 | } 1412 | return FALSE; 1413 | } 1414 | 1415 | if ($table == '') 1416 | { 1417 | if (!isset($this->ar_from[0])) 1418 | { 1419 | if ($this->db_debug) 1420 | { 1421 | return $this->display_error('db_must_set_table'); 1422 | } 1423 | return FALSE; 1424 | } 1425 | 1426 | $table = $this->ar_from[0]; 1427 | } 1428 | 1429 | if ($where != NULL) 1430 | { 1431 | $this->where($where); 1432 | } 1433 | 1434 | if ($limit != NULL) 1435 | { 1436 | $this->limit($limit); 1437 | } 1438 | 1439 | $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); 1440 | 1441 | $this->_reset_write(); 1442 | return $this->query($sql); 1443 | } 1444 | 1445 | 1446 | // -------------------------------------------------------------------- 1447 | 1448 | /** 1449 | * Update_Batch 1450 | * 1451 | * Compiles an update string and runs the query 1452 | * 1453 | * @param string $table the table to retrieve the results from 1454 | * @param array $set an associative array of update values 1455 | * @param string $index the where key 1456 | * 1457 | * @return CI_DB_active_record|CI_DB_result 1458 | */ 1459 | public function update_batch($table = '', $set = NULL, $index = NULL) 1460 | { 1461 | // Combine any cached components with the current statements 1462 | $this->_merge_cache(); 1463 | 1464 | if (is_null($index)) 1465 | { 1466 | if ($this->db_debug) 1467 | { 1468 | return $this->display_error('db_must_use_index'); 1469 | } 1470 | 1471 | return FALSE; 1472 | } 1473 | 1474 | if (!is_null($set)) 1475 | { 1476 | $this->set_update_batch($set, $index); 1477 | } 1478 | 1479 | if (count($this->ar_set) == 0) 1480 | { 1481 | if ($this->db_debug) 1482 | { 1483 | return $this->display_error('db_must_use_set'); 1484 | } 1485 | 1486 | return FALSE; 1487 | } 1488 | 1489 | if ($table == '') 1490 | { 1491 | if (!isset($this->ar_from[0])) 1492 | { 1493 | if ($this->db_debug) 1494 | { 1495 | return $this->display_error('db_must_set_table'); 1496 | } 1497 | return FALSE; 1498 | } 1499 | 1500 | $table = $this->ar_from[0]; 1501 | } 1502 | 1503 | // Batch this baby 1504 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) 1505 | { 1506 | $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where); 1507 | 1508 | $this->query($sql); 1509 | } 1510 | 1511 | $this->_reset_write(); 1512 | } 1513 | 1514 | // -------------------------------------------------------------------- 1515 | 1516 | /** 1517 | * The "set_update_batch" function. Allows key/value pairs to be set for batch updating 1518 | * 1519 | * @param array $key 1520 | * @param string $index 1521 | * @param boolean $escape 1522 | * 1523 | * @return CI_DB_active_record|CI_DB_result 1524 | */ 1525 | public function set_update_batch($key, $index = '', $escape = TRUE) 1526 | { 1527 | $key = $this->_object_to_array_batch($key); 1528 | 1529 | if (!is_array($key)) 1530 | { 1531 | // @todo error 1532 | } 1533 | 1534 | foreach ($key as $k => $v) 1535 | { 1536 | $index_set = FALSE; 1537 | $clean = array(); 1538 | 1539 | foreach ($v as $k2 => $v2) 1540 | { 1541 | if ($k2 == $index) 1542 | { 1543 | $index_set = TRUE; 1544 | } 1545 | else 1546 | { 1547 | $not[] = $k . '-' . $v; 1548 | } 1549 | 1550 | if ($escape === FALSE) 1551 | { 1552 | $clean[$this->_protect_identifiers($k2)] = $v2; 1553 | } 1554 | else 1555 | { 1556 | $clean[$this->_protect_identifiers($k2)] = $this->escape($v2); 1557 | } 1558 | } 1559 | 1560 | if ($index_set == FALSE) 1561 | { 1562 | return $this->display_error('db_batch_missing_index'); 1563 | } 1564 | 1565 | $this->ar_set[] = $clean; 1566 | } 1567 | 1568 | return $this; 1569 | } 1570 | 1571 | // -------------------------------------------------------------------- 1572 | 1573 | /** 1574 | * Empty Table 1575 | * 1576 | * Compiles a delete string and runs "DELETE FROM table" 1577 | * 1578 | * @param string $table the table to empty 1579 | * 1580 | * @return CI_DB_active_record|CI_DB_result 1581 | */ 1582 | public function empty_table($table = '') 1583 | { 1584 | if ($table == '') 1585 | { 1586 | if (!isset($this->ar_from[0])) 1587 | { 1588 | if ($this->db_debug) 1589 | { 1590 | return $this->display_error('db_must_set_table'); 1591 | } 1592 | return FALSE; 1593 | } 1594 | 1595 | $table = $this->ar_from[0]; 1596 | } 1597 | else 1598 | { 1599 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); 1600 | } 1601 | 1602 | $sql = $this->_delete($table); 1603 | 1604 | $this->_reset_write(); 1605 | 1606 | return $this->query($sql); 1607 | } 1608 | 1609 | // -------------------------------------------------------------------- 1610 | 1611 | /** 1612 | * Truncate 1613 | * 1614 | * Compiles a truncate string and runs the query 1615 | * If the database does not support the truncate() command 1616 | * This function maps to "DELETE FROM table" 1617 | * 1618 | * @param string $table the table to truncate 1619 | * 1620 | * @return CI_DB_active_record|CI_DB_result 1621 | */ 1622 | public function truncate($table = '') 1623 | { 1624 | if ($table == '') 1625 | { 1626 | if (!isset($this->ar_from[0])) 1627 | { 1628 | if ($this->db_debug) 1629 | { 1630 | return $this->display_error('db_must_set_table'); 1631 | } 1632 | return FALSE; 1633 | } 1634 | 1635 | $table = $this->ar_from[0]; 1636 | } 1637 | else 1638 | { 1639 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); 1640 | } 1641 | 1642 | $sql = $this->_truncate($table); 1643 | 1644 | $this->_reset_write(); 1645 | 1646 | return $this->query($sql); 1647 | } 1648 | 1649 | // -------------------------------------------------------------------- 1650 | 1651 | /** 1652 | * Delete 1653 | * 1654 | * Compiles a delete string and runs the query 1655 | * 1656 | * @param mixed $table the table(s) to delete from. String or array 1657 | * @param mixed $where the where clause 1658 | * @param mixed $limit the limit clause 1659 | * @param boolean $reset_data 1660 | * 1661 | * @return CI_DB_active_record|CI_DB_result 1662 | */ 1663 | public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) 1664 | { 1665 | // Combine any cached components with the current statements 1666 | $this->_merge_cache(); 1667 | 1668 | if ($table == '') 1669 | { 1670 | if (!isset($this->ar_from[0])) 1671 | { 1672 | if ($this->db_debug) 1673 | { 1674 | return $this->display_error('db_must_set_table'); 1675 | } 1676 | return FALSE; 1677 | } 1678 | 1679 | $table = $this->ar_from[0]; 1680 | } 1681 | elseif (is_array($table)) 1682 | { 1683 | foreach ($table as $single_table) 1684 | { 1685 | $this->delete($single_table, $where, $limit, FALSE); 1686 | } 1687 | 1688 | $this->_reset_write(); 1689 | return; 1690 | } 1691 | else 1692 | { 1693 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); 1694 | } 1695 | 1696 | if ($where != '') 1697 | { 1698 | $this->where($where); 1699 | } 1700 | 1701 | if ($limit != NULL) 1702 | { 1703 | $this->limit($limit); 1704 | } 1705 | 1706 | if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0) 1707 | { 1708 | if ($this->db_debug) 1709 | { 1710 | return $this->display_error('db_del_must_use_where'); 1711 | } 1712 | 1713 | return FALSE; 1714 | } 1715 | 1716 | $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit); 1717 | 1718 | if ($reset_data) 1719 | { 1720 | $this->_reset_write(); 1721 | } 1722 | 1723 | return $this->query($sql); 1724 | } 1725 | 1726 | // -------------------------------------------------------------------- 1727 | 1728 | /** 1729 | * DB Prefix 1730 | * 1731 | * Prepends a database prefix if one exists in configuration 1732 | * 1733 | * @param string $table the table 1734 | * 1735 | * @return string 1736 | */ 1737 | public function dbprefix($table = '') 1738 | { 1739 | if ($table == '') 1740 | { 1741 | $this->display_error('db_table_name_required'); 1742 | } 1743 | 1744 | return $this->dbprefix . $table; 1745 | } 1746 | 1747 | // -------------------------------------------------------------------- 1748 | 1749 | /** 1750 | * Set DB Prefix 1751 | * 1752 | * Set's the DB Prefix to something new without needing to reconnect 1753 | * 1754 | * @param string $prefix the prefix 1755 | * 1756 | * @return string 1757 | */ 1758 | public function set_dbprefix($prefix = '') 1759 | { 1760 | return $this->dbprefix = $prefix; 1761 | } 1762 | 1763 | // -------------------------------------------------------------------- 1764 | 1765 | /** 1766 | * Track Aliases 1767 | * 1768 | * Used to track SQL statements written with aliased tables. 1769 | * 1770 | * @param string $table The table to inspect 1771 | * 1772 | * @return string 1773 | */ 1774 | protected function _track_aliases($table) 1775 | { 1776 | if (is_array($table)) 1777 | { 1778 | foreach ($table as $t) 1779 | { 1780 | $this->_track_aliases($t); 1781 | } 1782 | return; 1783 | } 1784 | 1785 | // Does the string contain a comma? If so, we need to separate 1786 | // the string into discreet statements 1787 | if (strpos($table, ',') !== FALSE) 1788 | { 1789 | return $this->_track_aliases(explode(',', $table)); 1790 | } 1791 | 1792 | // if a table alias is used we can recognize it by a space 1793 | if (strpos($table, " ") !== FALSE) 1794 | { 1795 | // if the alias is written with the AS keyword, remove it 1796 | $table = preg_replace('/ AS /i', ' ', $table); 1797 | 1798 | // Grab the alias 1799 | $table = trim(strrchr($table, " ")); 1800 | 1801 | // Store the alias, if it doesn't already exist 1802 | if (!in_array($table, $this->ar_aliased_tables)) 1803 | { 1804 | $this->ar_aliased_tables[] = $table; 1805 | } 1806 | } 1807 | } 1808 | 1809 | // -------------------------------------------------------------------- 1810 | 1811 | /** 1812 | * Compile the SELECT statement 1813 | * 1814 | * Generates a query string based on which functions were used. 1815 | * Should not be called directly. The get() function calls it. 1816 | * 1817 | * @param bool $select_override 1818 | * 1819 | * @return string 1820 | */ 1821 | protected function _compile_select($select_override = FALSE) 1822 | { 1823 | // Combine any cached components with the current statements 1824 | $this->_merge_cache(); 1825 | 1826 | // ---------------------------------------------------------------- 1827 | 1828 | // Write the "select" portion of the query 1829 | 1830 | if ($select_override !== FALSE) 1831 | { 1832 | $sql = $select_override; 1833 | } 1834 | else 1835 | { 1836 | $sql = (!$this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; 1837 | 1838 | if (count($this->ar_select) == 0) 1839 | { 1840 | $sql .= '*'; 1841 | } 1842 | else 1843 | { 1844 | // Cycle through the "select" portion of the query and prep each column name. 1845 | // The reason we protect identifiers here rather then in the select() function 1846 | // is because until the user calls the from() function we don't know if there are aliases 1847 | foreach ($this->ar_select as $key => $val) 1848 | { 1849 | $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL; 1850 | $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape); 1851 | } 1852 | 1853 | $sql .= implode(', ', $this->ar_select); 1854 | } 1855 | } 1856 | 1857 | // ---------------------------------------------------------------- 1858 | 1859 | // Write the "FROM" portion of the query 1860 | 1861 | if (count($this->ar_from) > 0) 1862 | { 1863 | $sql .= "\nFROM "; 1864 | 1865 | $sql .= $this->_from_tables($this->ar_from); 1866 | } 1867 | 1868 | // ---------------------------------------------------------------- 1869 | 1870 | // Write the "JOIN" portion of the query 1871 | 1872 | if (count($this->ar_join) > 0) 1873 | { 1874 | $sql .= "\n"; 1875 | 1876 | $sql .= implode("\n", $this->ar_join); 1877 | } 1878 | 1879 | // ---------------------------------------------------------------- 1880 | 1881 | // Write the "WHERE" portion of the query 1882 | 1883 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0) 1884 | { 1885 | $sql .= "\nWHERE "; 1886 | } 1887 | 1888 | $sql .= implode("\n", $this->ar_where); 1889 | 1890 | // ---------------------------------------------------------------- 1891 | 1892 | // Write the "LIKE" portion of the query 1893 | 1894 | if (count($this->ar_like) > 0) 1895 | { 1896 | if (count($this->ar_where) > 0) 1897 | { 1898 | $sql .= "\nAND "; 1899 | } 1900 | 1901 | $sql .= implode("\n", $this->ar_like); 1902 | } 1903 | 1904 | // ---------------------------------------------------------------- 1905 | 1906 | // Write the "GROUP BY" portion of the query 1907 | 1908 | if (count($this->ar_groupby) > 0) 1909 | { 1910 | $sql .= "\nGROUP BY "; 1911 | 1912 | $sql .= implode(', ', $this->ar_groupby); 1913 | } 1914 | 1915 | // ---------------------------------------------------------------- 1916 | 1917 | // Write the "HAVING" portion of the query 1918 | 1919 | if (count($this->ar_having) > 0) 1920 | { 1921 | $sql .= "\nHAVING "; 1922 | $sql .= implode("\n", $this->ar_having); 1923 | } 1924 | 1925 | // ---------------------------------------------------------------- 1926 | 1927 | // Write the "ORDER BY" portion of the query 1928 | 1929 | if (count($this->ar_orderby) > 0) 1930 | { 1931 | $sql .= "\nORDER BY "; 1932 | $sql .= implode(', ', $this->ar_orderby); 1933 | 1934 | if ($this->ar_order !== FALSE) 1935 | { 1936 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC'; 1937 | } 1938 | } 1939 | 1940 | // ---------------------------------------------------------------- 1941 | 1942 | // Write the "LIMIT" portion of the query 1943 | 1944 | if (is_numeric($this->ar_limit)) 1945 | { 1946 | $sql .= "\n"; 1947 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset); 1948 | } 1949 | 1950 | return $sql; 1951 | } 1952 | 1953 | // -------------------------------------------------------------------- 1954 | 1955 | /** 1956 | * Object to Array 1957 | * 1958 | * Takes an object as input and converts the class variables to array key/vals 1959 | * 1960 | * @param object $object 1961 | * 1962 | * @return array 1963 | */ 1964 | public function _object_to_array($object) 1965 | { 1966 | if (!is_object($object)) 1967 | { 1968 | return $object; 1969 | } 1970 | 1971 | $array = array(); 1972 | foreach (get_object_vars($object) as $key => $val) 1973 | { 1974 | // There are some built in keys we need to ignore for this conversion 1975 | if (!is_object($val) && !is_array($val) && $key != '_parent_name') 1976 | { 1977 | $array[$key] = $val; 1978 | } 1979 | } 1980 | 1981 | return $array; 1982 | } 1983 | 1984 | // -------------------------------------------------------------------- 1985 | 1986 | /** 1987 | * Object to Array 1988 | * 1989 | * Takes an object as input and converts the class variables to array key/vals 1990 | * 1991 | * @param object $object 1992 | * 1993 | * @return array 1994 | */ 1995 | public function _object_to_array_batch($object) 1996 | { 1997 | if (!is_object($object)) 1998 | { 1999 | return $object; 2000 | } 2001 | 2002 | $array = array(); 2003 | $out = get_object_vars($object); 2004 | $fields = array_keys($out); 2005 | 2006 | foreach ($fields as $val) 2007 | { 2008 | // There are some built in keys we need to ignore for this conversion 2009 | if ($val != '_parent_name') 2010 | { 2011 | 2012 | $i = 0; 2013 | foreach ($out[$val] as $data) 2014 | { 2015 | $array[$i][$val] = $data; 2016 | $i++; 2017 | } 2018 | } 2019 | } 2020 | 2021 | return $array; 2022 | } 2023 | 2024 | // -------------------------------------------------------------------- 2025 | 2026 | /** 2027 | * Start Cache 2028 | * 2029 | * Starts AR caching 2030 | * 2031 | * @return void 2032 | */ 2033 | public function start_cache() 2034 | { 2035 | $this->ar_caching = TRUE; 2036 | } 2037 | 2038 | // -------------------------------------------------------------------- 2039 | 2040 | /** 2041 | * Stop Cache 2042 | * 2043 | * Stops AR caching 2044 | * 2045 | * @return void 2046 | */ 2047 | public function stop_cache() 2048 | { 2049 | $this->ar_caching = FALSE; 2050 | } 2051 | 2052 | // -------------------------------------------------------------------- 2053 | 2054 | /** 2055 | * Flush Cache 2056 | * 2057 | * Empties the AR cache 2058 | * 2059 | * @access public 2060 | * @return void 2061 | */ 2062 | public function flush_cache() 2063 | { 2064 | $this->_reset_run(array( 2065 | 'ar_cache_select' => array(), 2066 | 'ar_cache_from' => array(), 2067 | 'ar_cache_join' => array(), 2068 | 'ar_cache_where' => array(), 2069 | 'ar_cache_like' => array(), 2070 | 'ar_cache_groupby' => array(), 2071 | 'ar_cache_having' => array(), 2072 | 'ar_cache_orderby' => array(), 2073 | 'ar_cache_set' => array(), 2074 | 'ar_cache_exists' => array(), 2075 | 'ar_cache_no_escape' => array() 2076 | )); 2077 | } 2078 | 2079 | // -------------------------------------------------------------------- 2080 | 2081 | /** 2082 | * Merge Cache 2083 | * 2084 | * When called, this function merges any cached AR arrays with 2085 | * locally called ones. 2086 | * 2087 | * @return void 2088 | */ 2089 | protected function _merge_cache() 2090 | { 2091 | if (count($this->ar_cache_exists) == 0) 2092 | { 2093 | return; 2094 | } 2095 | 2096 | foreach ($this->ar_cache_exists as $val) 2097 | { 2098 | $ar_variable = 'ar_' . $val; 2099 | $ar_cache_var = 'ar_cache_' . $val; 2100 | 2101 | if (count($this->$ar_cache_var) == 0) 2102 | { 2103 | continue; 2104 | } 2105 | 2106 | $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable)); 2107 | } 2108 | 2109 | // If we are "protecting identifiers" we need to examine the "from" 2110 | // portion of the query to determine if there are any aliases 2111 | if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0) 2112 | { 2113 | $this->_track_aliases($this->ar_from); 2114 | } 2115 | 2116 | $this->ar_no_escape = $this->ar_cache_no_escape; 2117 | } 2118 | 2119 | // -------------------------------------------------------------------- 2120 | 2121 | /** 2122 | * Resets the active record values. Called by the get() function 2123 | * 2124 | * @param array $ar_reset_items An array of fields to reset 2125 | * 2126 | * @return void 2127 | */ 2128 | protected function _reset_run($ar_reset_items) 2129 | { 2130 | foreach ($ar_reset_items as $item => $default_value) 2131 | { 2132 | if (!in_array($item, $this->ar_store_array)) 2133 | { 2134 | $this->$item = $default_value; 2135 | } 2136 | } 2137 | } 2138 | 2139 | // -------------------------------------------------------------------- 2140 | 2141 | /** 2142 | * Resets the active record values. Called by the get() function 2143 | * 2144 | * @return void 2145 | */ 2146 | protected function _reset_select() 2147 | { 2148 | $ar_reset_items = array( 2149 | 'ar_select' => array(), 2150 | 'ar_from' => array(), 2151 | 'ar_join' => array(), 2152 | 'ar_where' => array(), 2153 | 'ar_like' => array(), 2154 | 'ar_groupby' => array(), 2155 | 'ar_having' => array(), 2156 | 'ar_orderby' => array(), 2157 | 'ar_wherein' => array(), 2158 | 'ar_aliased_tables' => array(), 2159 | 'ar_no_escape' => array(), 2160 | 'ar_distinct' => FALSE, 2161 | 'ar_limit' => FALSE, 2162 | 'ar_offset' => FALSE, 2163 | 'ar_order' => FALSE, 2164 | ); 2165 | 2166 | $this->_reset_run($ar_reset_items); 2167 | } 2168 | 2169 | // -------------------------------------------------------------------- 2170 | 2171 | /** 2172 | * Resets the active record "write" values. 2173 | * 2174 | * Called by the insert() update() insert_batch() update_batch() and delete() functions 2175 | * 2176 | * @return void 2177 | */ 2178 | protected function _reset_write() 2179 | { 2180 | $ar_reset_items = array( 2181 | 'ar_set' => array(), 2182 | 'ar_from' => array(), 2183 | 'ar_where' => array(), 2184 | 'ar_like' => array(), 2185 | 'ar_orderby' => array(), 2186 | 'ar_keys' => array(), 2187 | 'ar_limit' => FALSE, 2188 | 'ar_order' => FALSE 2189 | ); 2190 | 2191 | $this->_reset_run($ar_reset_items); 2192 | } 2193 | } 2194 | 2195 | /* End of file DB_active_rec.php */ 2196 | /* Location: ./system/database/DB_active_rec.php */ -------------------------------------------------------------------------------- /CodeIgniter/my_models.php: -------------------------------------------------------------------------------- 1 | 7 | * $this->site_model->get_records() 8 | * 9 | * Where site_model is the model Class 10 | * 11 | * ---------------------- Models to Load ---------------------- 12 | * 13 | * 14 | * @property membership_model $membership_model 15 | * @property site_model $site_model 16 | * @property data_model $data_model 17 | * @property products_model $products_model 18 | * @property film_model $film_model 19 | * @property film_model1 $film_model1 20 | * 21 | * 22 | * 23 | */ 24 | class my_models 25 | { 26 | } 27 | 28 | // End my_models.php 29 | -------------------------------------------------------------------------------- /CodeIgniter3/CI_phpStorm.php: -------------------------------------------------------------------------------- 1 | Memory consumption can also be displayed. 31 | * @property CI_Calendar $calendar This class enables the creation of calendars 32 | * @property CI_Cart $cart Shopping Cart Class 33 | * @property CI_Config $config This class contains functions that enable config files to be managed 34 | * @property CI_Controller $controller This class object is the super class that every library in.
CodeIgniter will be assigned to. 35 | * @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP. 36 | * @property CI_Encrypt $encrypt Provides two-way keyed encoding using XOR Hashing and Mcrypt 37 | * @property CI_Exceptions $exceptions Exceptions Class 38 | * @property CI_Form_validation $form_validation Form Validation Class 39 | * @property CI_Ftp $ftp FTP Class 40 | * @property CI_Hooks $hooks //dead 41 | * @property CI_Image_lib $image_lib Image Manipulation class 42 | * @property CI_Input $input Pre-processes global input data for security 43 | * @property CI_Lang $lang Language Class 44 | * @property CI_Loader $load Loads views and files 45 | * @property CI_Log $log Logging Class 46 | * @property CI_Model $model CodeIgniter Model Class 47 | * @property CI_Output $output Responsible for sending final output to browser 48 | * @property CI_Pagination $pagination Pagination Class 49 | * @property CI_Parser $parser Parses pseudo-variables contained in the specified template view,
replacing them with the data in the second param 50 | * @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data
in order to help with debugging and optimization. 51 | * @property CI_Router $router Parses URIs and determines routing 52 | * @property CI_Session $session Session Class 53 | * @property CI_Table $table HTML table generation
Lets you create tables manually or from database result objects, or arrays. 54 | * @property CI_Trackback $trackback Trackback Sending/Receiving Class 55 | * @property CI_Typography $typography Typography Class 56 | * @property CI_Unit_test $unit_test Simple testing class 57 | * @property CI_Upload $upload File Uploading Class 58 | * @property CI_URI $uri Parses URIs and determines routing 59 | * @property CI_User_agent $user_agent Identifies the platform, browser, robot, or mobile devise of the browsing agent 60 | * @property CI_Xmlrpc $xmlrpc XML-RPC request handler class 61 | * @property CI_Xmlrpcs $xmlrpcs XML-RPC server class 62 | * @property CI_Zip $zip Zip Compression Class 63 | * @property CI_Javascript $javascript Javascript Class 64 | * @property CI_Jquery $jquery Jquery Class 65 | * @property CI_Utf8 $utf8 Provides support for UTF-8 environments 66 | * @property CI_Security $security Security Class, xss, csrf, etc... 67 | * @property CI_Driver_Library $driver CodeIgniter Driver Library Class 68 | * @property CI_Cache $cache CodeIgniter Caching Class 69 | */ 70 | class CI_Controller extends my_models 71 | { 72 | public function __construct() {} //This default return construct as set 73 | } 74 | 75 | /** 76 | * @property CI_DB_active_record $db This is the platform-independent base Active Record implementation class. 77 | * @property CI_DB_forge $dbforge Database Utility Class 78 | * @property CI_Benchmark $benchmark This class enables you to mark points and calculate the time difference between them.
Memory consumption can also be displayed. 79 | * @property CI_Calendar $calendar This class enables the creation of calendars 80 | * @property CI_Cart $cart Shopping Cart Class 81 | * @property CI_Config $config This class contains functions that enable config files to be managed 82 | * @property CI_Controller $controller This class object is the super class that every library in.
CodeIgniter will be assigned to. 83 | * @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP. 84 | * @property CI_Encrypt $encrypt Provides two-way keyed encoding using XOR Hashing and Mcrypt 85 | * @property CI_Exceptions $exceptions Exceptions Class 86 | * @property CI_Form_validation $form_validation Form Validation Class 87 | * @property CI_Ftp $ftp FTP Class 88 | * @property CI_Hooks $hooks //dead 89 | * @property CI_Image_lib $image_lib Image Manipulation class 90 | * @property CI_Input $input Pre-processes global input data for security 91 | * @property CI_Lang $lang Language Class 92 | * @property CI_Loader $load Loads views and files 93 | * @property CI_Log $log Logging Class 94 | * @property CI_Model $model CodeIgniter Model Class 95 | * @property CI_Output $output Responsible for sending final output to browser 96 | * @property CI_Pagination $pagination Pagination Class 97 | * @property CI_Parser $parser Parses pseudo-variables contained in the specified template view,
replacing them with the data in the second param 98 | * @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data
in order to help with debugging and optimization. 99 | * @property CI_Router $router Parses URIs and determines routing 100 | * @property CI_Session $session Session Class 101 | * @property CI_Table $table HTML table generation
Lets you create tables manually or from database result objects, or arrays. 102 | * @property CI_Trackback $trackback Trackback Sending/Receiving Class 103 | * @property CI_Typography $typography Typography Class 104 | * @property CI_Unit_test $unit_test Simple testing class 105 | * @property CI_Upload $upload File Uploading Class 106 | * @property CI_URI $uri Parses URIs and determines routing 107 | * @property CI_User_agent $user_agent Identifies the platform, browser, robot, or mobile devise of the browsing agent 108 | * @property CI_Xmlrpc $xmlrpc XML-RPC request handler class 109 | * @property CI_Xmlrpcs $xmlrpcs XML-RPC server class 110 | * @property CI_Zip $zip Zip Compression Class 111 | * @property CI_Javascript $javascript Javascript Class 112 | * @property CI_Jquery $jquery Jquery Class 113 | * @property CI_Utf8 $utf8 Provides support for UTF-8 environments 114 | * @property CI_Security $security Security Class, xss, csrf, etc... 115 | * @property CI_Driver_Library $driver CodeIgniter Driver Library Class 116 | * @property CI_Cache $cache CodeIgniter Caching Class 117 | */ 118 | class CI_Model extends my_models 119 | { 120 | public function __construct() {} //This default return construct as set 121 | } 122 | -------------------------------------------------------------------------------- /CodeIgniter3/DB_active_rec.php: -------------------------------------------------------------------------------- 1 | ar_select[] = $val; 147 | $this->ar_no_escape[] = $escape; 148 | 149 | if ($this->ar_caching === TRUE) 150 | { 151 | $this->ar_cache_select[] = $val; 152 | $this->ar_cache_exists[] = 'select'; 153 | $this->ar_cache_no_escape[] = $escape; 154 | } 155 | } 156 | } 157 | return $this; 158 | } 159 | 160 | // -------------------------------------------------------------------- 161 | 162 | /** 163 | * Select Max 164 | * 165 | * Generates a SELECT MAX(field) portion of a query 166 | * 167 | * @param string $select the field 168 | * @param string $alias an alias 169 | * 170 | * @return CI_DB_active_record|CI_DB_result 171 | */ 172 | public function select_max($select = '', $alias = '') 173 | { 174 | $this->_max_min_avg_sum($select, $alias, 'MAX'); 175 | 176 | return $this; 177 | } 178 | 179 | // -------------------------------------------------------------------- 180 | 181 | /** 182 | * Select Min 183 | * 184 | * Generates a SELECT MIN(field) portion of a query 185 | * 186 | * @param string $select the field 187 | * @param string $alias an alias 188 | * 189 | * @return CI_DB_active_record|CI_DB_result 190 | */ 191 | public function select_min($select = '', $alias = '') 192 | { 193 | $this->_max_min_avg_sum($select, $alias, 'MIN'); 194 | 195 | return $this; 196 | } 197 | 198 | // -------------------------------------------------------------------- 199 | 200 | /** 201 | * Select Average 202 | * 203 | * Generates a SELECT AVG(field) portion of a query 204 | * 205 | * @param string $select the field 206 | * @param string $alias an alias 207 | * 208 | * @return CI_DB_active_record|CI_DB_result 209 | */ 210 | public function select_avg($select = '', $alias = '') 211 | { 212 | $this->_max_min_avg_sum($select, $alias, 'AVG'); 213 | 214 | return $this; 215 | } 216 | 217 | // -------------------------------------------------------------------- 218 | 219 | /** 220 | * Select Sum 221 | * 222 | * Generates a SELECT SUM(field) portion of a query 223 | * 224 | * @param string $select the field 225 | * @param string $alias an alias 226 | * 227 | * @return CI_DB_active_record|CI_DB_result 228 | */ 229 | public function select_sum($select = '', $alias = '') 230 | { 231 | return $this->_max_min_avg_sum($select, $alias, 'SUM'); 232 | } 233 | 234 | // -------------------------------------------------------------------- 235 | 236 | /** 237 | * Processing Function for the four functions above: 238 | * 239 | * select_max() 240 | * select_min() 241 | * select_avg() 242 | * select_sum() 243 | * 244 | * @param string $select the string field 245 | * @param string $alias an string alias 246 | * @param string $type 247 | * 248 | * @return CI_DB_active_record|CI_DB_result 249 | */ 250 | protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') 251 | { 252 | if (!is_string($select) OR $select == '') 253 | { 254 | $this->display_error('db_invalid_query'); 255 | } 256 | 257 | $type = strtoupper($type); 258 | 259 | if (!in_array($type, array( 260 | 'MAX', 261 | 'MIN', 262 | 'AVG', 263 | 'SUM' 264 | )) 265 | ) 266 | { 267 | show_error('Invalid function type: ' . $type); 268 | } 269 | 270 | if ($alias == '') 271 | { 272 | $alias = $this->_create_alias_from_table(trim($select)); 273 | } 274 | 275 | $sql = $type . '(' . $this->_protect_identifiers(trim($select)) . ') AS ' . $alias; 276 | 277 | $this->ar_select[] = $sql; 278 | 279 | if ($this->ar_caching === TRUE) 280 | { 281 | $this->ar_cache_select[] = $sql; 282 | $this->ar_cache_exists[] = 'select'; 283 | } 284 | 285 | return $this; 286 | } 287 | 288 | // -------------------------------------------------------------------- 289 | 290 | /** 291 | * Determines the alias name based on the table 292 | * 293 | * @param string $item 294 | * 295 | * @return string 296 | */ 297 | protected function _create_alias_from_table($item) 298 | { 299 | if (strpos($item, '.') !== FALSE) 300 | { 301 | return end(explode('.', $item)); 302 | } 303 | 304 | return $item; 305 | } 306 | 307 | // -------------------------------------------------------------------- 308 | 309 | /** 310 | * DISTINCT 311 | * 312 | * Sets a flag which tells the query string compiler to add DISTINCT 313 | * 314 | * @param bool $val 315 | * 316 | * @return CI_DB_active_record|CI_DB_result 317 | */ 318 | public function distinct($val = TRUE) 319 | { 320 | $this->ar_distinct = (is_bool($val)) ? $val : TRUE; 321 | return $this; 322 | } 323 | 324 | // -------------------------------------------------------------------- 325 | 326 | /** 327 | * From 328 | * 329 | * Generates the FROM portion of the query 330 | * 331 | * @param mixed $from can be a string or array 332 | * 333 | * @return CI_DB_active_record|CI_DB_result 334 | */ 335 | public function from($from) 336 | { 337 | foreach ((array) $from as $val) 338 | { 339 | if (strpos($val, ',') !== FALSE) 340 | { 341 | foreach (explode(',', $val) as $v) 342 | { 343 | $v = trim($v); 344 | $this->_track_aliases($v); 345 | 346 | $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); 347 | 348 | if ($this->ar_caching === TRUE) 349 | { 350 | $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); 351 | $this->ar_cache_exists[] = 'from'; 352 | } 353 | } 354 | 355 | } 356 | else 357 | { 358 | $val = trim($val); 359 | 360 | // Extract any aliases that might exist. We use this information 361 | // in the _protect_identifiers to know whether to add a table prefix 362 | $this->_track_aliases($val); 363 | 364 | $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE); 365 | 366 | if ($this->ar_caching === TRUE) 367 | { 368 | $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE); 369 | $this->ar_cache_exists[] = 'from'; 370 | } 371 | } 372 | } 373 | 374 | return $this; 375 | } 376 | 377 | // -------------------------------------------------------------------- 378 | 379 | /** 380 | * Join 381 | * 382 | * Generates the JOIN portion of the query 383 | * 384 | * @param string $table 385 | * @param string $cond the join condition 386 | * @param string $type the type of join 387 | * 388 | * @return CI_DB_active_record|CI_DB_result 389 | */ 390 | public function join($table, $cond, $type = '') 391 | { 392 | if ($type != '') 393 | { 394 | $type = strtoupper(trim($type)); 395 | 396 | if (!in_array($type, array( 397 | 'LEFT', 398 | 'RIGHT', 399 | 'OUTER', 400 | 'INNER', 401 | 'LEFT OUTER', 402 | 'RIGHT OUTER' 403 | )) 404 | ) 405 | { 406 | $type = ''; 407 | } 408 | else 409 | { 410 | $type .= ' '; 411 | } 412 | } 413 | 414 | // Extract any aliases that might exist. We use this information 415 | // in the _protect_identifiers to know whether to add a table prefix 416 | $this->_track_aliases($table); 417 | 418 | // Strip apart the condition and protect the identifiers 419 | if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match)) 420 | { 421 | $match[1] = $this->_protect_identifiers($match[1]); 422 | $match[3] = $this->_protect_identifiers($match[3]); 423 | 424 | $cond = $match[1] . $match[2] . $match[3]; 425 | } 426 | 427 | // Assemble the JOIN statement 428 | $join = $type . 'JOIN ' . $this->_protect_identifiers($table, TRUE, NULL, FALSE) . ' ON ' . $cond; 429 | 430 | $this->ar_join[] = $join; 431 | if ($this->ar_caching === TRUE) 432 | { 433 | $this->ar_cache_join[] = $join; 434 | $this->ar_cache_exists[] = 'join'; 435 | } 436 | 437 | return $this; 438 | } 439 | 440 | // -------------------------------------------------------------------- 441 | 442 | /** 443 | * Where 444 | * 445 | * Generates the WHERE portion of the query. Separates 446 | * multiple calls with AND 447 | * 448 | * @param mixed $key 449 | * @param null $value 450 | * @param bool $escape 451 | * 452 | * @return CI_DB_active_record|CI_DB_result 453 | */ 454 | public function where($key, $value = NULL, $escape = TRUE) 455 | { 456 | return $this->_where($key, $value, 'AND ', $escape); 457 | } 458 | 459 | // -------------------------------------------------------------------- 460 | 461 | /** 462 | * OR Where 463 | * 464 | * Generates the WHERE portion of the query. Separates 465 | * multiple calls with OR 466 | * 467 | * @param mixed $key 468 | * @param null $value 469 | * @param bool $escape 470 | * 471 | * @return CI_DB_active_record|CI_DB_result 472 | */ 473 | public function or_where($key, $value = NULL, $escape = TRUE) 474 | { 475 | return $this->_where($key, $value, 'OR ', $escape); 476 | } 477 | 478 | // -------------------------------------------------------------------- 479 | 480 | /** 481 | * Where 482 | * 483 | * Called by where() or or_where() 484 | * 485 | * @param mixed $key 486 | * @param null $value 487 | * @param string $type 488 | * @param null $escape 489 | * 490 | * @return CI_DB_active_record|CI_DB_result 491 | */ 492 | protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) 493 | { 494 | if (!is_array($key)) 495 | { 496 | $key = array($key => $value); 497 | } 498 | 499 | // If the escape value was not set will will base it on the global setting 500 | if (!is_bool($escape)) 501 | { 502 | $escape = $this->_protect_identifiers; 503 | } 504 | 505 | foreach ($key as $k => $v) 506 | { 507 | $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type; 508 | 509 | if (is_null($v) && !$this->_has_operator($k)) 510 | { 511 | // value appears not to have been set, assign the test to IS NULL 512 | $k .= ' IS NULL'; 513 | } 514 | 515 | if (!is_null($v)) 516 | { 517 | if ($escape === TRUE) 518 | { 519 | $k = $this->_protect_identifiers($k, FALSE, $escape); 520 | 521 | $v = ' ' . $this->escape($v); 522 | } 523 | 524 | if (!$this->_has_operator($k)) 525 | { 526 | $k .= ' = '; 527 | } 528 | } 529 | else 530 | { 531 | $k = $this->_protect_identifiers($k, FALSE, $escape); 532 | } 533 | 534 | $this->ar_where[] = $prefix . $k . $v; 535 | 536 | if ($this->ar_caching === TRUE) 537 | { 538 | $this->ar_cache_where[] = $prefix . $k . $v; 539 | $this->ar_cache_exists[] = 'where'; 540 | } 541 | 542 | } 543 | 544 | return $this; 545 | } 546 | 547 | // -------------------------------------------------------------------- 548 | 549 | /** 550 | * Where_in 551 | * 552 | * Generates a WHERE field IN ('item', 'item') SQL query joined with 553 | * AND if appropriate 554 | * 555 | * @param string $key The field to search 556 | * @param array $values The values searched on 557 | * 558 | * @return CI_DB_active_record|CI_DB_result 559 | */ 560 | public function where_in($key = NULL, $values = NULL) 561 | { 562 | return $this->_where_in($key, $values); 563 | } 564 | 565 | // -------------------------------------------------------------------- 566 | 567 | /** 568 | * Where_in_or 569 | * 570 | * Generates a WHERE field IN ('item', 'item') SQL query joined with 571 | * OR if appropriate 572 | * 573 | * @param string $key The field to search 574 | * @param array $values The values searched on 575 | * 576 | * @return CI_DB_active_record|CI_DB_result 577 | */ 578 | public function or_where_in($key = NULL, $values = NULL) 579 | { 580 | return $this->_where_in($key, $values, FALSE, 'OR '); 581 | } 582 | 583 | // -------------------------------------------------------------------- 584 | 585 | /** 586 | * Where_not_in 587 | * 588 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined 589 | * with AND if appropriate 590 | * 591 | * @param string $key The field to search 592 | * @param array $values The values searched on 593 | * 594 | * @return CI_DB_active_record|CI_DB_result 595 | */ 596 | public function where_not_in($key = NULL, $values = NULL) 597 | { 598 | return $this->_where_in($key, $values, TRUE); 599 | } 600 | 601 | // -------------------------------------------------------------------- 602 | 603 | /** 604 | * Where_not_in_or 605 | * 606 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined 607 | * with OR if appropriate 608 | * 609 | * @param string $key The field to search 610 | * @param array $values The values searched on 611 | * 612 | * @return CI_DB_active_record|CI_DB_result 613 | */ 614 | public function or_where_not_in($key = NULL, $values = NULL) 615 | { 616 | return $this->_where_in($key, $values, TRUE, 'OR '); 617 | } 618 | 619 | // -------------------------------------------------------------------- 620 | 621 | /** 622 | * Where_in 623 | * 624 | * Called by where_in, where_in_or, where_not_in, where_not_in_or 625 | * 626 | * @param string $key The field to search 627 | * @param array $values The values searched on 628 | * @param boolean $not If the statement would be IN or NOT IN 629 | * @param string $type 630 | * 631 | * @return CI_DB_active_record|CI_DB_result 632 | */ 633 | protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ') 634 | { 635 | if ($key === NULL OR $values === NULL) 636 | { 637 | return null; 638 | } 639 | 640 | if (!is_array($values)) 641 | { 642 | $values = array($values); 643 | } 644 | 645 | $not = ($not) ? ' NOT' : ''; 646 | 647 | foreach ($values as $value) 648 | { 649 | $this->ar_wherein[] = $this->escape($value); 650 | } 651 | 652 | $prefix = (count($this->ar_where) == 0) ? '' : $type; 653 | 654 | $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") "; 655 | 656 | $this->ar_where[] = $where_in; 657 | if ($this->ar_caching === TRUE) 658 | { 659 | $this->ar_cache_where[] = $where_in; 660 | $this->ar_cache_exists[] = 'where'; 661 | } 662 | 663 | // reset the array for multiple calls 664 | $this->ar_wherein = array(); 665 | return $this; 666 | } 667 | 668 | // -------------------------------------------------------------------- 669 | 670 | /** 671 | * Like 672 | * 673 | * Generates a %LIKE% portion of the query. Separates 674 | * multiple calls with AND 675 | * 676 | * @param string $field 677 | * @param string $match 678 | * @param string $side 679 | * 680 | * @return CI_DB_active_record|CI_DB_result 681 | */ 682 | public function like($field, $match = '', $side = 'both') 683 | { 684 | $this->_like($field, $match, 'AND ', $side); 685 | 686 | return $this; 687 | } 688 | 689 | // -------------------------------------------------------------------- 690 | 691 | /** 692 | * Not Like 693 | * 694 | * Generates a NOT LIKE portion of the query. Separates 695 | * multiple calls with AND 696 | * 697 | * @param string $field 698 | * @param string $match 699 | * @param string $side 700 | * 701 | * @return CI_DB_active_record|CI_DB_result 702 | */ 703 | public function not_like($field, $match = '', $side = 'both') 704 | { 705 | $this->_like($field, $match, 'AND ', $side, 'NOT'); 706 | 707 | return $this; 708 | } 709 | 710 | // -------------------------------------------------------------------- 711 | 712 | /** 713 | * OR Like 714 | * 715 | * Generates a %LIKE% portion of the query. Separates 716 | * multiple calls with OR 717 | * 718 | * @param string $field 719 | * @param string $match 720 | * @param string $side 721 | * 722 | * @return CI_DB_active_record|CI_DB_result 723 | */ 724 | public function or_like($field, $match = '', $side = 'both') 725 | { 726 | return $this->_like($field, $match, 'OR ', $side); 727 | } 728 | 729 | // -------------------------------------------------------------------- 730 | 731 | /** 732 | * OR Not Like 733 | * 734 | * Generates a NOT LIKE portion of the query. Separates 735 | * multiple calls with OR 736 | * 737 | * @param string $field 738 | * @param mixed $match 739 | * @param string $side 740 | * 741 | * @return CI_DB_active_record|CI_DB_result 742 | */ 743 | public function or_not_like($field, $match = '', $side = 'both') 744 | { 745 | return $this->_like($field, $match, 'OR ', $side, 'NOT'); 746 | } 747 | 748 | // -------------------------------------------------------------------- 749 | 750 | /** 751 | * Like 752 | * 753 | * Called by like() or orlike() 754 | * 755 | * @param string $field 756 | * @param string $match 757 | * @param string $type 758 | * @param string $side 759 | * @param string $not 760 | * 761 | * @return CI_DB_active_record|CI_DB_result 762 | */ 763 | protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') 764 | { 765 | if (!is_array($field)) 766 | { 767 | $field = array($field => $match); 768 | } 769 | 770 | foreach ($field as $k => $v) 771 | { 772 | $k = $this->_protect_identifiers($k); 773 | 774 | $prefix = (count($this->ar_like) == 0) ? '' : $type; 775 | 776 | $v = $this->escape_like_str($v); 777 | 778 | if ($side == 'before') 779 | { 780 | $like_statement = $prefix . " $k $not LIKE '%{$v}'"; 781 | } 782 | elseif ($side == 'after') 783 | { 784 | $like_statement = $prefix . " $k $not LIKE '{$v}%'"; 785 | } 786 | else 787 | { 788 | $like_statement = $prefix . " $k $not LIKE '%{$v}%'"; 789 | } 790 | 791 | // some platforms require an escape sequence definition for LIKE wildcards 792 | if ($this->_like_escape_str != '') 793 | { 794 | $like_statement = $like_statement . sprintf($this->_like_escape_str, $this->_like_escape_chr); 795 | } 796 | 797 | $this->ar_like[] = $like_statement; 798 | if ($this->ar_caching === TRUE) 799 | { 800 | $this->ar_cache_like[] = $like_statement; 801 | $this->ar_cache_exists[] = 'like'; 802 | } 803 | 804 | } 805 | return $this; 806 | } 807 | 808 | // -------------------------------------------------------------------- 809 | 810 | /** 811 | * GROUP BY 812 | * 813 | * @param string $by 814 | * 815 | * @return CI_DB_active_record|CI_DB_result 816 | */ 817 | public function group_by($by) 818 | { 819 | if (is_string($by)) 820 | { 821 | $by = explode(',', $by); 822 | } 823 | 824 | foreach ($by as $val) 825 | { 826 | $val = trim($val); 827 | 828 | if ($val != '') 829 | { 830 | $this->ar_groupby[] = $this->_protect_identifiers($val); 831 | 832 | if ($this->ar_caching === TRUE) 833 | { 834 | $this->ar_cache_groupby[] = $this->_protect_identifiers($val); 835 | $this->ar_cache_exists[] = 'groupby'; 836 | } 837 | } 838 | } 839 | return $this; 840 | } 841 | 842 | // -------------------------------------------------------------------- 843 | 844 | /** 845 | * Sets the HAVING value 846 | * 847 | * Separates multiple calls with AND 848 | * 849 | * @param mixed $key 850 | * @param string $value 851 | * @param bool $escape 852 | * 853 | * @return CI_DB_active_record|CI_DB_result 854 | */ 855 | public function having($key, $value = '', $escape = TRUE) 856 | { 857 | $this->_having($key, $value, 'AND ', $escape); 858 | 859 | return $this; 860 | } 861 | 862 | // -------------------------------------------------------------------- 863 | 864 | /** 865 | * Sets the OR HAVING value 866 | * 867 | * Separates multiple calls with OR 868 | * 869 | * @param mixed $key 870 | * @param string $value 871 | * @param bool $escape 872 | * 873 | * @return CI_DB_active_record|CI_DB_result 874 | */ 875 | public function or_having($key, $value = '', $escape = TRUE) 876 | { 877 | $this->_having($key, $value, 'OR ', $escape); 878 | 879 | return $this; 880 | } 881 | 882 | // -------------------------------------------------------------------- 883 | 884 | /** 885 | * Sets the HAVING values 886 | * 887 | * Called by having() or or_having() 888 | * 889 | * @param mixed $key 890 | * @param string $value 891 | * @param string $type 892 | * @param bool $escape 893 | * 894 | * @return CI_DB_active_record|CI_DB_result 895 | */ 896 | protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE) 897 | { 898 | if (!is_array($key)) 899 | { 900 | $key = array($key => $value); 901 | } 902 | 903 | foreach ($key as $k => $v) 904 | { 905 | $prefix = (count($this->ar_having) == 0) ? '' : $type; 906 | 907 | if ($escape === TRUE) 908 | { 909 | $k = $this->_protect_identifiers($k); 910 | } 911 | 912 | if (!$this->_has_operator($k)) 913 | { 914 | $k .= ' = '; 915 | } 916 | 917 | if ($v != '') 918 | { 919 | $v = ' ' . $this->escape($v); 920 | } 921 | 922 | $this->ar_having[] = $prefix . $k . $v; 923 | if ($this->ar_caching === TRUE) 924 | { 925 | $this->ar_cache_having[] = $prefix . $k . $v; 926 | $this->ar_cache_exists[] = 'having'; 927 | } 928 | } 929 | 930 | return $this; 931 | } 932 | 933 | // -------------------------------------------------------------------- 934 | 935 | /** 936 | * Sets the ORDER BY value 937 | * 938 | * @param string $orderby 939 | * @param string $direction: asc or desc 940 | * 941 | * @return CI_DB_active_record|CI_DB_result 942 | */ 943 | public function order_by($orderby, $direction = '') 944 | { 945 | if (strtolower($direction) == 'random') 946 | { 947 | $orderby = ''; // Random results want or don't need a field name 948 | $direction = $this->_random_keyword; 949 | } 950 | elseif (trim($direction) != '') 951 | { 952 | $direction = (in_array(strtoupper(trim($direction)), array( 953 | 'ASC', 954 | 'DESC' 955 | ), TRUE)) ? ' ' . $direction : ' ASC'; 956 | } 957 | 958 | if (strpos($orderby, ',') !== FALSE) 959 | { 960 | $temp = array(); 961 | foreach (explode(',', $orderby) as $part) 962 | { 963 | $part = trim($part); 964 | if (!in_array($part, $this->ar_aliased_tables)) 965 | { 966 | $part = $this->_protect_identifiers(trim($part)); 967 | } 968 | 969 | $temp[] = $part; 970 | } 971 | 972 | $orderby = implode(', ', $temp); 973 | } 974 | else if ($direction != $this->_random_keyword) 975 | { 976 | $orderby = $this->_protect_identifiers($orderby); 977 | } 978 | 979 | $orderby_statement = $orderby . $direction; 980 | 981 | $this->ar_orderby[] = $orderby_statement; 982 | if ($this->ar_caching === TRUE) 983 | { 984 | $this->ar_cache_orderby[] = $orderby_statement; 985 | $this->ar_cache_exists[] = 'orderby'; 986 | } 987 | 988 | return $this; 989 | } 990 | 991 | // -------------------------------------------------------------------- 992 | 993 | /** 994 | * Sets the LIMIT value 995 | * 996 | * @param integer $value the offset value 997 | * @param string $offset 998 | * 999 | * @return CI_DB_active_record|CI_DB_result 1000 | */ 1001 | public function limit($value, $offset = '') 1002 | { 1003 | $this->ar_limit = (int) $value; 1004 | 1005 | if ($offset != '') 1006 | { 1007 | $this->ar_offset = (int) $offset; 1008 | } 1009 | 1010 | return $this; 1011 | } 1012 | 1013 | // -------------------------------------------------------------------- 1014 | 1015 | /** 1016 | * Sets the OFFSET value 1017 | * 1018 | * @param integer $offset the offset value 1019 | * 1020 | * @return CI_DB_active_record|CI_DB_result 1021 | */ 1022 | public function offset($offset) 1023 | { 1024 | $this->ar_offset = $offset; 1025 | return $this; 1026 | } 1027 | 1028 | // -------------------------------------------------------------------- 1029 | 1030 | /** 1031 | * The "set" function. Allows key/value pairs to be set for inserting or updating 1032 | * 1033 | * @param mixed $key 1034 | * @param string $value 1035 | * @param boolean $escape 1036 | * 1037 | * @return CI_DB_active_record|CI_DB_result 1038 | */ 1039 | public function set($key, $value = '', $escape = TRUE) 1040 | { 1041 | $key = $this->_object_to_array($key); 1042 | 1043 | if (!is_array($key)) 1044 | { 1045 | $key = array($key => $value); 1046 | } 1047 | 1048 | foreach ($key as $k => $v) 1049 | { 1050 | if ($escape === FALSE) 1051 | { 1052 | $this->ar_set[$this->_protect_identifiers($k)] = $v; 1053 | } 1054 | else 1055 | { 1056 | $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); 1057 | } 1058 | } 1059 | 1060 | return $this; 1061 | } 1062 | 1063 | // -------------------------------------------------------------------- 1064 | 1065 | /** 1066 | * Get 1067 | * 1068 | * Compiles the select statement based on the other functions called 1069 | * and runs the query 1070 | * 1071 | * @param string $table the table 1072 | * @param string $limit the limit clause 1073 | * @param string $offset the offset clause 1074 | * 1075 | * @return mixed $result 1076 | */ 1077 | public function get($table = '', $limit = null, $offset = null) 1078 | { 1079 | if ($table != '') 1080 | { 1081 | $this->_track_aliases($table); 1082 | $this->from($table); 1083 | } 1084 | 1085 | if (!is_null($limit)) 1086 | { 1087 | $this->limit($limit, $offset); 1088 | } 1089 | 1090 | $sql = $this->_compile_select(); 1091 | 1092 | $result = $this->query($sql); 1093 | $this->_reset_select(); 1094 | 1095 | return $result; 1096 | } 1097 | 1098 | /** 1099 | * "Count All Results" query 1100 | * 1101 | * Generates a platform-specific query string that counts all records 1102 | * returned by an Active Record query. 1103 | * 1104 | * @param string $table 1105 | * 1106 | * @return int 1107 | */ 1108 | public function count_all_results($table = '') 1109 | { 1110 | if ($table != '') 1111 | { 1112 | $this->_track_aliases($table); 1113 | $this->from($table); 1114 | } 1115 | 1116 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows')); 1117 | 1118 | $query = $this->query($sql); 1119 | $this->_reset_select(); 1120 | 1121 | if ($query->num_rows() == 0) 1122 | { 1123 | return 0; 1124 | } 1125 | 1126 | $row = $query->row(); 1127 | return (int) $row->numrows; 1128 | } 1129 | 1130 | // -------------------------------------------------------------------- 1131 | 1132 | /** 1133 | * Get_Where 1134 | * 1135 | * Allows the where clause, limit and offset to be added directly 1136 | * 1137 | * @param string $table 1138 | * @param null $where the string where clause 1139 | * @param null $limit the string limit clause 1140 | * @param null $offset the string offset clause 1141 | * 1142 | * @return CI_DB_active_record|CI_DB_result 1143 | */ 1144 | public function get_where($table = '', $where = null, $limit = null, $offset = null) 1145 | { 1146 | if ($table != '') 1147 | { 1148 | $this->from($table); 1149 | } 1150 | 1151 | if (!is_null($where)) 1152 | { 1153 | $this->where($where); 1154 | } 1155 | 1156 | if (!is_null($limit)) 1157 | { 1158 | $this->limit($limit, $offset); 1159 | } 1160 | 1161 | $sql = $this->_compile_select(); 1162 | 1163 | $result = $this->query($sql); 1164 | $this->_reset_select(); 1165 | return $result; 1166 | } 1167 | 1168 | // -------------------------------------------------------------------- 1169 | 1170 | /** 1171 | * Insert_Batch 1172 | * 1173 | * Compiles batch insert strings and runs the queries 1174 | * 1175 | * @param string $table the table to retrieve the results from 1176 | * @param array $set an associative array of insert values 1177 | * 1178 | * @return CI_DB_active_record|CI_DB_result 1179 | */ 1180 | public function insert_batch($table = '', $set = NULL) 1181 | { 1182 | if (!is_null($set)) 1183 | { 1184 | $this->set_insert_batch($set); 1185 | } 1186 | 1187 | if (count($this->ar_set) == 0) 1188 | { 1189 | if ($this->db_debug) 1190 | { 1191 | //No valid data array. Folds in cases where keys and values did not match up 1192 | return $this->display_error('db_must_use_set'); 1193 | } 1194 | return FALSE; 1195 | } 1196 | 1197 | if ($table == '') 1198 | { 1199 | if (!isset($this->ar_from[0])) 1200 | { 1201 | if ($this->db_debug) 1202 | { 1203 | return $this->display_error('db_must_set_table'); 1204 | } 1205 | return FALSE; 1206 | } 1207 | 1208 | $table = $this->ar_from[0]; 1209 | } 1210 | 1211 | // Batch this baby 1212 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) 1213 | { 1214 | 1215 | $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100)); 1216 | 1217 | //echo $sql; 1218 | 1219 | $this->query($sql); 1220 | } 1221 | 1222 | $this->_reset_write(); 1223 | 1224 | return TRUE; 1225 | } 1226 | 1227 | // -------------------------------------------------------------------- 1228 | 1229 | /** 1230 | * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts 1231 | * 1232 | * @param mixed $key 1233 | * @param string $value 1234 | * @param boolean $escape 1235 | * 1236 | * @return CI_DB_active_record|CI_DB_result 1237 | */ 1238 | public function set_insert_batch($key, $value = '', $escape = TRUE) 1239 | { 1240 | $key = $this->_object_to_array_batch($key); 1241 | 1242 | if (!is_array($key)) 1243 | { 1244 | $key = array($key => $value); 1245 | } 1246 | 1247 | $keys = array_keys(current($key)); 1248 | sort($keys); 1249 | 1250 | foreach ($key as $row) 1251 | { 1252 | if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) 1253 | { 1254 | // batch function above returns an error on an empty array 1255 | $this->ar_set[] = array(); 1256 | return; 1257 | } 1258 | 1259 | ksort($row); // puts $row in the same order as our keys 1260 | 1261 | if ($escape === FALSE) 1262 | { 1263 | $this->ar_set[] = '(' . implode(',', $row) . ')'; 1264 | } 1265 | else 1266 | { 1267 | $clean = array(); 1268 | 1269 | foreach ($row as $value) 1270 | { 1271 | $clean[] = $this->escape($value); 1272 | } 1273 | 1274 | $this->ar_set[] = '(' . implode(',', $clean) . ')'; 1275 | } 1276 | } 1277 | 1278 | foreach ($keys as $k) 1279 | { 1280 | $this->ar_keys[] = $this->_protect_identifiers($k); 1281 | } 1282 | 1283 | return $this; 1284 | } 1285 | 1286 | // -------------------------------------------------------------------- 1287 | 1288 | /** 1289 | * Insert 1290 | * 1291 | * Compiles an insert string and runs the query 1292 | * 1293 | * @param string $table the table to insert data into 1294 | * @param array $set an associative array of insert values 1295 | * 1296 | * @return CI_DB_active_record|CI_DB_result 1297 | */ 1298 | function insert($table = '', $set = NULL) 1299 | { 1300 | if (!is_null($set)) 1301 | { 1302 | $this->set($set); 1303 | } 1304 | 1305 | if (count($this->ar_set) == 0) 1306 | { 1307 | if ($this->db_debug) 1308 | { 1309 | return $this->display_error('db_must_use_set'); 1310 | } 1311 | return FALSE; 1312 | } 1313 | 1314 | if ($table == '') 1315 | { 1316 | if (!isset($this->ar_from[0])) 1317 | { 1318 | if ($this->db_debug) 1319 | { 1320 | return $this->display_error('db_must_set_table'); 1321 | } 1322 | return FALSE; 1323 | } 1324 | 1325 | $table = $this->ar_from[0]; 1326 | } 1327 | 1328 | $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); 1329 | 1330 | $this->_reset_write(); 1331 | return $this->query($sql); 1332 | } 1333 | 1334 | // -------------------------------------------------------------------- 1335 | 1336 | /** 1337 | * Replace 1338 | * 1339 | * Compiles an replace into string and runs the query 1340 | * 1341 | * @param string $table the table to replace data into 1342 | * @param array $set an associative array of insert values 1343 | * 1344 | * @return CI_DB_active_record|CI_DB_result 1345 | */ 1346 | public function replace($table = '', $set = NULL) 1347 | { 1348 | if (!is_null($set)) 1349 | { 1350 | $this->set($set); 1351 | } 1352 | 1353 | if (count($this->ar_set) == 0) 1354 | { 1355 | if ($this->db_debug) 1356 | { 1357 | return $this->display_error('db_must_use_set'); 1358 | } 1359 | return FALSE; 1360 | } 1361 | 1362 | if ($table == '') 1363 | { 1364 | if (!isset($this->ar_from[0])) 1365 | { 1366 | if ($this->db_debug) 1367 | { 1368 | return $this->display_error('db_must_set_table'); 1369 | } 1370 | return FALSE; 1371 | } 1372 | 1373 | $table = $this->ar_from[0]; 1374 | } 1375 | 1376 | $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); 1377 | 1378 | $this->_reset_write(); 1379 | return $this->query($sql); 1380 | } 1381 | 1382 | // -------------------------------------------------------------------- 1383 | 1384 | /** 1385 | * Update 1386 | * 1387 | * Compiles an update string and runs the query 1388 | * 1389 | * @param string $table the table to retrieve the results from 1390 | * @param array $set an associative array of update values 1391 | * @param mixed $where the where clause 1392 | * @param bool $limit 1393 | * 1394 | * @return CI_DB_active_record|CI_DB_result 1395 | */ 1396 | public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) 1397 | { 1398 | // Combine any cached components with the current statements 1399 | $this->_merge_cache(); 1400 | 1401 | if (!is_null($set)) 1402 | { 1403 | $this->set($set); 1404 | } 1405 | 1406 | if (count($this->ar_set) == 0) 1407 | { 1408 | if ($this->db_debug) 1409 | { 1410 | return $this->display_error('db_must_use_set'); 1411 | } 1412 | return FALSE; 1413 | } 1414 | 1415 | if ($table == '') 1416 | { 1417 | if (!isset($this->ar_from[0])) 1418 | { 1419 | if ($this->db_debug) 1420 | { 1421 | return $this->display_error('db_must_set_table'); 1422 | } 1423 | return FALSE; 1424 | } 1425 | 1426 | $table = $this->ar_from[0]; 1427 | } 1428 | 1429 | if ($where != NULL) 1430 | { 1431 | $this->where($where); 1432 | } 1433 | 1434 | if ($limit != NULL) 1435 | { 1436 | $this->limit($limit); 1437 | } 1438 | 1439 | $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); 1440 | 1441 | $this->_reset_write(); 1442 | return $this->query($sql); 1443 | } 1444 | 1445 | 1446 | // -------------------------------------------------------------------- 1447 | 1448 | /** 1449 | * Update_Batch 1450 | * 1451 | * Compiles an update string and runs the query 1452 | * 1453 | * @param string $table the table to retrieve the results from 1454 | * @param array $set an associative array of update values 1455 | * @param string $index the where key 1456 | * 1457 | * @return CI_DB_active_record|CI_DB_result 1458 | */ 1459 | public function update_batch($table = '', $set = NULL, $index = NULL) 1460 | { 1461 | // Combine any cached components with the current statements 1462 | $this->_merge_cache(); 1463 | 1464 | if (is_null($index)) 1465 | { 1466 | if ($this->db_debug) 1467 | { 1468 | return $this->display_error('db_must_use_index'); 1469 | } 1470 | 1471 | return FALSE; 1472 | } 1473 | 1474 | if (!is_null($set)) 1475 | { 1476 | $this->set_update_batch($set, $index); 1477 | } 1478 | 1479 | if (count($this->ar_set) == 0) 1480 | { 1481 | if ($this->db_debug) 1482 | { 1483 | return $this->display_error('db_must_use_set'); 1484 | } 1485 | 1486 | return FALSE; 1487 | } 1488 | 1489 | if ($table == '') 1490 | { 1491 | if (!isset($this->ar_from[0])) 1492 | { 1493 | if ($this->db_debug) 1494 | { 1495 | return $this->display_error('db_must_set_table'); 1496 | } 1497 | return FALSE; 1498 | } 1499 | 1500 | $table = $this->ar_from[0]; 1501 | } 1502 | 1503 | // Batch this baby 1504 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) 1505 | { 1506 | $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where); 1507 | 1508 | $this->query($sql); 1509 | } 1510 | 1511 | $this->_reset_write(); 1512 | } 1513 | 1514 | // -------------------------------------------------------------------- 1515 | 1516 | /** 1517 | * The "set_update_batch" function. Allows key/value pairs to be set for batch updating 1518 | * 1519 | * @param array $key 1520 | * @param string $index 1521 | * @param boolean $escape 1522 | * 1523 | * @return CI_DB_active_record|CI_DB_result 1524 | */ 1525 | public function set_update_batch($key, $index = '', $escape = TRUE) 1526 | { 1527 | $key = $this->_object_to_array_batch($key); 1528 | 1529 | if (!is_array($key)) 1530 | { 1531 | // @todo error 1532 | } 1533 | 1534 | foreach ($key as $k => $v) 1535 | { 1536 | $index_set = FALSE; 1537 | $clean = array(); 1538 | 1539 | foreach ($v as $k2 => $v2) 1540 | { 1541 | if ($k2 == $index) 1542 | { 1543 | $index_set = TRUE; 1544 | } 1545 | else 1546 | { 1547 | $not[] = $k . '-' . $v; 1548 | } 1549 | 1550 | if ($escape === FALSE) 1551 | { 1552 | $clean[$this->_protect_identifiers($k2)] = $v2; 1553 | } 1554 | else 1555 | { 1556 | $clean[$this->_protect_identifiers($k2)] = $this->escape($v2); 1557 | } 1558 | } 1559 | 1560 | if ($index_set == FALSE) 1561 | { 1562 | return $this->display_error('db_batch_missing_index'); 1563 | } 1564 | 1565 | $this->ar_set[] = $clean; 1566 | } 1567 | 1568 | return $this; 1569 | } 1570 | 1571 | // -------------------------------------------------------------------- 1572 | 1573 | /** 1574 | * Empty Table 1575 | * 1576 | * Compiles a delete string and runs "DELETE FROM table" 1577 | * 1578 | * @param string $table the table to empty 1579 | * 1580 | * @return CI_DB_active_record|CI_DB_result 1581 | */ 1582 | public function empty_table($table = '') 1583 | { 1584 | if ($table == '') 1585 | { 1586 | if (!isset($this->ar_from[0])) 1587 | { 1588 | if ($this->db_debug) 1589 | { 1590 | return $this->display_error('db_must_set_table'); 1591 | } 1592 | return FALSE; 1593 | } 1594 | 1595 | $table = $this->ar_from[0]; 1596 | } 1597 | else 1598 | { 1599 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); 1600 | } 1601 | 1602 | $sql = $this->_delete($table); 1603 | 1604 | $this->_reset_write(); 1605 | 1606 | return $this->query($sql); 1607 | } 1608 | 1609 | // -------------------------------------------------------------------- 1610 | 1611 | /** 1612 | * Truncate 1613 | * 1614 | * Compiles a truncate string and runs the query 1615 | * If the database does not support the truncate() command 1616 | * This function maps to "DELETE FROM table" 1617 | * 1618 | * @param string $table the table to truncate 1619 | * 1620 | * @return CI_DB_active_record|CI_DB_result 1621 | */ 1622 | public function truncate($table = '') 1623 | { 1624 | if ($table == '') 1625 | { 1626 | if (!isset($this->ar_from[0])) 1627 | { 1628 | if ($this->db_debug) 1629 | { 1630 | return $this->display_error('db_must_set_table'); 1631 | } 1632 | return FALSE; 1633 | } 1634 | 1635 | $table = $this->ar_from[0]; 1636 | } 1637 | else 1638 | { 1639 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); 1640 | } 1641 | 1642 | $sql = $this->_truncate($table); 1643 | 1644 | $this->_reset_write(); 1645 | 1646 | return $this->query($sql); 1647 | } 1648 | 1649 | // -------------------------------------------------------------------- 1650 | 1651 | /** 1652 | * Delete 1653 | * 1654 | * Compiles a delete string and runs the query 1655 | * 1656 | * @param mixed $table the table(s) to delete from. String or array 1657 | * @param mixed $where the where clause 1658 | * @param mixed $limit the limit clause 1659 | * @param boolean $reset_data 1660 | * 1661 | * @return CI_DB_active_record|CI_DB_result 1662 | */ 1663 | public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) 1664 | { 1665 | // Combine any cached components with the current statements 1666 | $this->_merge_cache(); 1667 | 1668 | if ($table == '') 1669 | { 1670 | if (!isset($this->ar_from[0])) 1671 | { 1672 | if ($this->db_debug) 1673 | { 1674 | return $this->display_error('db_must_set_table'); 1675 | } 1676 | return FALSE; 1677 | } 1678 | 1679 | $table = $this->ar_from[0]; 1680 | } 1681 | elseif (is_array($table)) 1682 | { 1683 | foreach ($table as $single_table) 1684 | { 1685 | $this->delete($single_table, $where, $limit, FALSE); 1686 | } 1687 | 1688 | $this->_reset_write(); 1689 | return; 1690 | } 1691 | else 1692 | { 1693 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); 1694 | } 1695 | 1696 | if ($where != '') 1697 | { 1698 | $this->where($where); 1699 | } 1700 | 1701 | if ($limit != NULL) 1702 | { 1703 | $this->limit($limit); 1704 | } 1705 | 1706 | if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0) 1707 | { 1708 | if ($this->db_debug) 1709 | { 1710 | return $this->display_error('db_del_must_use_where'); 1711 | } 1712 | 1713 | return FALSE; 1714 | } 1715 | 1716 | $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit); 1717 | 1718 | if ($reset_data) 1719 | { 1720 | $this->_reset_write(); 1721 | } 1722 | 1723 | return $this->query($sql); 1724 | } 1725 | 1726 | // -------------------------------------------------------------------- 1727 | 1728 | /** 1729 | * DB Prefix 1730 | * 1731 | * Prepends a database prefix if one exists in configuration 1732 | * 1733 | * @param string $table the table 1734 | * 1735 | * @return string 1736 | */ 1737 | public function dbprefix($table = '') 1738 | { 1739 | if ($table == '') 1740 | { 1741 | $this->display_error('db_table_name_required'); 1742 | } 1743 | 1744 | return $this->dbprefix . $table; 1745 | } 1746 | 1747 | // -------------------------------------------------------------------- 1748 | 1749 | /** 1750 | * Set DB Prefix 1751 | * 1752 | * Set's the DB Prefix to something new without needing to reconnect 1753 | * 1754 | * @param string $prefix the prefix 1755 | * 1756 | * @return string 1757 | */ 1758 | public function set_dbprefix($prefix = '') 1759 | { 1760 | return $this->dbprefix = $prefix; 1761 | } 1762 | 1763 | // -------------------------------------------------------------------- 1764 | 1765 | /** 1766 | * Track Aliases 1767 | * 1768 | * Used to track SQL statements written with aliased tables. 1769 | * 1770 | * @param string $table The table to inspect 1771 | * 1772 | * @return string 1773 | */ 1774 | protected function _track_aliases($table) 1775 | { 1776 | if (is_array($table)) 1777 | { 1778 | foreach ($table as $t) 1779 | { 1780 | $this->_track_aliases($t); 1781 | } 1782 | return; 1783 | } 1784 | 1785 | // Does the string contain a comma? If so, we need to separate 1786 | // the string into discreet statements 1787 | if (strpos($table, ',') !== FALSE) 1788 | { 1789 | return $this->_track_aliases(explode(',', $table)); 1790 | } 1791 | 1792 | // if a table alias is used we can recognize it by a space 1793 | if (strpos($table, " ") !== FALSE) 1794 | { 1795 | // if the alias is written with the AS keyword, remove it 1796 | $table = preg_replace('/ AS /i', ' ', $table); 1797 | 1798 | // Grab the alias 1799 | $table = trim(strrchr($table, " ")); 1800 | 1801 | // Store the alias, if it doesn't already exist 1802 | if (!in_array($table, $this->ar_aliased_tables)) 1803 | { 1804 | $this->ar_aliased_tables[] = $table; 1805 | } 1806 | } 1807 | } 1808 | 1809 | // -------------------------------------------------------------------- 1810 | 1811 | /** 1812 | * Compile the SELECT statement 1813 | * 1814 | * Generates a query string based on which functions were used. 1815 | * Should not be called directly. The get() function calls it. 1816 | * 1817 | * @param bool $select_override 1818 | * 1819 | * @return string 1820 | */ 1821 | protected function _compile_select($select_override = FALSE) 1822 | { 1823 | // Combine any cached components with the current statements 1824 | $this->_merge_cache(); 1825 | 1826 | // ---------------------------------------------------------------- 1827 | 1828 | // Write the "select" portion of the query 1829 | 1830 | if ($select_override !== FALSE) 1831 | { 1832 | $sql = $select_override; 1833 | } 1834 | else 1835 | { 1836 | $sql = (!$this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; 1837 | 1838 | if (count($this->ar_select) == 0) 1839 | { 1840 | $sql .= '*'; 1841 | } 1842 | else 1843 | { 1844 | // Cycle through the "select" portion of the query and prep each column name. 1845 | // The reason we protect identifiers here rather then in the select() function 1846 | // is because until the user calls the from() function we don't know if there are aliases 1847 | foreach ($this->ar_select as $key => $val) 1848 | { 1849 | $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL; 1850 | $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape); 1851 | } 1852 | 1853 | $sql .= implode(', ', $this->ar_select); 1854 | } 1855 | } 1856 | 1857 | // ---------------------------------------------------------------- 1858 | 1859 | // Write the "FROM" portion of the query 1860 | 1861 | if (count($this->ar_from) > 0) 1862 | { 1863 | $sql .= "\nFROM "; 1864 | 1865 | $sql .= $this->_from_tables($this->ar_from); 1866 | } 1867 | 1868 | // ---------------------------------------------------------------- 1869 | 1870 | // Write the "JOIN" portion of the query 1871 | 1872 | if (count($this->ar_join) > 0) 1873 | { 1874 | $sql .= "\n"; 1875 | 1876 | $sql .= implode("\n", $this->ar_join); 1877 | } 1878 | 1879 | // ---------------------------------------------------------------- 1880 | 1881 | // Write the "WHERE" portion of the query 1882 | 1883 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0) 1884 | { 1885 | $sql .= "\nWHERE "; 1886 | } 1887 | 1888 | $sql .= implode("\n", $this->ar_where); 1889 | 1890 | // ---------------------------------------------------------------- 1891 | 1892 | // Write the "LIKE" portion of the query 1893 | 1894 | if (count($this->ar_like) > 0) 1895 | { 1896 | if (count($this->ar_where) > 0) 1897 | { 1898 | $sql .= "\nAND "; 1899 | } 1900 | 1901 | $sql .= implode("\n", $this->ar_like); 1902 | } 1903 | 1904 | // ---------------------------------------------------------------- 1905 | 1906 | // Write the "GROUP BY" portion of the query 1907 | 1908 | if (count($this->ar_groupby) > 0) 1909 | { 1910 | $sql .= "\nGROUP BY "; 1911 | 1912 | $sql .= implode(', ', $this->ar_groupby); 1913 | } 1914 | 1915 | // ---------------------------------------------------------------- 1916 | 1917 | // Write the "HAVING" portion of the query 1918 | 1919 | if (count($this->ar_having) > 0) 1920 | { 1921 | $sql .= "\nHAVING "; 1922 | $sql .= implode("\n", $this->ar_having); 1923 | } 1924 | 1925 | // ---------------------------------------------------------------- 1926 | 1927 | // Write the "ORDER BY" portion of the query 1928 | 1929 | if (count($this->ar_orderby) > 0) 1930 | { 1931 | $sql .= "\nORDER BY "; 1932 | $sql .= implode(', ', $this->ar_orderby); 1933 | 1934 | if ($this->ar_order !== FALSE) 1935 | { 1936 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC'; 1937 | } 1938 | } 1939 | 1940 | // ---------------------------------------------------------------- 1941 | 1942 | // Write the "LIMIT" portion of the query 1943 | 1944 | if (is_numeric($this->ar_limit)) 1945 | { 1946 | $sql .= "\n"; 1947 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset); 1948 | } 1949 | 1950 | return $sql; 1951 | } 1952 | 1953 | // -------------------------------------------------------------------- 1954 | 1955 | /** 1956 | * Object to Array 1957 | * 1958 | * Takes an object as input and converts the class variables to array key/vals 1959 | * 1960 | * @param object $object 1961 | * 1962 | * @return array 1963 | */ 1964 | public function _object_to_array($object) 1965 | { 1966 | if (!is_object($object)) 1967 | { 1968 | return $object; 1969 | } 1970 | 1971 | $array = array(); 1972 | foreach (get_object_vars($object) as $key => $val) 1973 | { 1974 | // There are some built in keys we need to ignore for this conversion 1975 | if (!is_object($val) && !is_array($val) && $key != '_parent_name') 1976 | { 1977 | $array[$key] = $val; 1978 | } 1979 | } 1980 | 1981 | return $array; 1982 | } 1983 | 1984 | // -------------------------------------------------------------------- 1985 | 1986 | /** 1987 | * Object to Array 1988 | * 1989 | * Takes an object as input and converts the class variables to array key/vals 1990 | * 1991 | * @param object $object 1992 | * 1993 | * @return array 1994 | */ 1995 | public function _object_to_array_batch($object) 1996 | { 1997 | if (!is_object($object)) 1998 | { 1999 | return $object; 2000 | } 2001 | 2002 | $array = array(); 2003 | $out = get_object_vars($object); 2004 | $fields = array_keys($out); 2005 | 2006 | foreach ($fields as $val) 2007 | { 2008 | // There are some built in keys we need to ignore for this conversion 2009 | if ($val != '_parent_name') 2010 | { 2011 | 2012 | $i = 0; 2013 | foreach ($out[$val] as $data) 2014 | { 2015 | $array[$i][$val] = $data; 2016 | $i++; 2017 | } 2018 | } 2019 | } 2020 | 2021 | return $array; 2022 | } 2023 | 2024 | // -------------------------------------------------------------------- 2025 | 2026 | /** 2027 | * Start Cache 2028 | * 2029 | * Starts AR caching 2030 | * 2031 | * @return void 2032 | */ 2033 | public function start_cache() 2034 | { 2035 | $this->ar_caching = TRUE; 2036 | } 2037 | 2038 | // -------------------------------------------------------------------- 2039 | 2040 | /** 2041 | * Stop Cache 2042 | * 2043 | * Stops AR caching 2044 | * 2045 | * @return void 2046 | */ 2047 | public function stop_cache() 2048 | { 2049 | $this->ar_caching = FALSE; 2050 | } 2051 | 2052 | // -------------------------------------------------------------------- 2053 | 2054 | /** 2055 | * Flush Cache 2056 | * 2057 | * Empties the AR cache 2058 | * 2059 | * @access public 2060 | * @return void 2061 | */ 2062 | public function flush_cache() 2063 | { 2064 | $this->_reset_run(array( 2065 | 'ar_cache_select' => array(), 2066 | 'ar_cache_from' => array(), 2067 | 'ar_cache_join' => array(), 2068 | 'ar_cache_where' => array(), 2069 | 'ar_cache_like' => array(), 2070 | 'ar_cache_groupby' => array(), 2071 | 'ar_cache_having' => array(), 2072 | 'ar_cache_orderby' => array(), 2073 | 'ar_cache_set' => array(), 2074 | 'ar_cache_exists' => array(), 2075 | 'ar_cache_no_escape' => array() 2076 | )); 2077 | } 2078 | 2079 | // -------------------------------------------------------------------- 2080 | 2081 | /** 2082 | * Merge Cache 2083 | * 2084 | * When called, this function merges any cached AR arrays with 2085 | * locally called ones. 2086 | * 2087 | * @return void 2088 | */ 2089 | protected function _merge_cache() 2090 | { 2091 | if (count($this->ar_cache_exists) == 0) 2092 | { 2093 | return; 2094 | } 2095 | 2096 | foreach ($this->ar_cache_exists as $val) 2097 | { 2098 | $ar_variable = 'ar_' . $val; 2099 | $ar_cache_var = 'ar_cache_' . $val; 2100 | 2101 | if (count($this->$ar_cache_var) == 0) 2102 | { 2103 | continue; 2104 | } 2105 | 2106 | $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable)); 2107 | } 2108 | 2109 | // If we are "protecting identifiers" we need to examine the "from" 2110 | // portion of the query to determine if there are any aliases 2111 | if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0) 2112 | { 2113 | $this->_track_aliases($this->ar_from); 2114 | } 2115 | 2116 | $this->ar_no_escape = $this->ar_cache_no_escape; 2117 | } 2118 | 2119 | // -------------------------------------------------------------------- 2120 | 2121 | /** 2122 | * Resets the active record values. Called by the get() function 2123 | * 2124 | * @param array $ar_reset_items An array of fields to reset 2125 | * 2126 | * @return void 2127 | */ 2128 | protected function _reset_run($ar_reset_items) 2129 | { 2130 | foreach ($ar_reset_items as $item => $default_value) 2131 | { 2132 | if (!in_array($item, $this->ar_store_array)) 2133 | { 2134 | $this->$item = $default_value; 2135 | } 2136 | } 2137 | } 2138 | 2139 | // -------------------------------------------------------------------- 2140 | 2141 | /** 2142 | * Resets the active record values. Called by the get() function 2143 | * 2144 | * @return void 2145 | */ 2146 | protected function _reset_select() 2147 | { 2148 | $ar_reset_items = array( 2149 | 'ar_select' => array(), 2150 | 'ar_from' => array(), 2151 | 'ar_join' => array(), 2152 | 'ar_where' => array(), 2153 | 'ar_like' => array(), 2154 | 'ar_groupby' => array(), 2155 | 'ar_having' => array(), 2156 | 'ar_orderby' => array(), 2157 | 'ar_wherein' => array(), 2158 | 'ar_aliased_tables' => array(), 2159 | 'ar_no_escape' => array(), 2160 | 'ar_distinct' => FALSE, 2161 | 'ar_limit' => FALSE, 2162 | 'ar_offset' => FALSE, 2163 | 'ar_order' => FALSE, 2164 | ); 2165 | 2166 | $this->_reset_run($ar_reset_items); 2167 | } 2168 | 2169 | // -------------------------------------------------------------------- 2170 | 2171 | /** 2172 | * Resets the active record "write" values. 2173 | * 2174 | * Called by the insert() update() insert_batch() update_batch() and delete() functions 2175 | * 2176 | * @return void 2177 | */ 2178 | protected function _reset_write() 2179 | { 2180 | $ar_reset_items = array( 2181 | 'ar_set' => array(), 2182 | 'ar_from' => array(), 2183 | 'ar_where' => array(), 2184 | 'ar_like' => array(), 2185 | 'ar_orderby' => array(), 2186 | 'ar_keys' => array(), 2187 | 'ar_limit' => FALSE, 2188 | 'ar_order' => FALSE 2189 | ); 2190 | 2191 | $this->_reset_run($ar_reset_items); 2192 | } 2193 | } 2194 | 2195 | /* End of file DB_active_rec.php */ 2196 | /* Location: ./system/database/DB_active_rec.php */ -------------------------------------------------------------------------------- /CodeIgniter3/my_models.php: -------------------------------------------------------------------------------- 1 | 7 | * $this->site_model->get_records() 8 | * 9 | * Where site_model is the model Class 10 | * 11 | * ---------------------- Models to Load ---------------------- 12 | * 13 | * 14 | * @property membership_model $membership_model 15 | * @property site_model $site_model 16 | * @property data_model $data_model 17 | * @property products_model $products_model 18 | * @property film_model $film_model 19 | * @property film_model1 $film_model1 20 | * 21 | * 22 | * 23 | */ 24 | class my_models 25 | { 26 | } 27 | 28 | // End my_models.php 29 | -------------------------------------------------------------------------------- /OpenCart/controller.php: -------------------------------------------------------------------------------- 1 | 11 | * CodeIgniter CodeIgniter/ 12 | * phpBB3 phpBB3.php Some phpBB3 CC fixes 13 | * WordPress WordPress/ 14 | * OpenCart OpenCart/ Still in early stages, but should show example 15 | * WordPress example.php Some other fix examples 16 | * Laravel submodule laravel-helpers 17 | 18 | 19 | 20 | 21 | ##### Note: plugins work only in PhpStorm 2016.3 EAP 22 | Props to @artspb for pointing me to this and showing me how they are created. 23 | 24 | The plugins in this repo will be available via the phpStorm plugin system as of `2016.3 EAP` 25 | 26 | 27 | Laravel helpers https://github.com/danielboendergaard/laravel-helpers 28 | 29 | CodeIgniter source https://github.com/EllisLab/CodeIgniter 30 | 31 | Thought I would mention I checked NetBeans 7.1 with CI and the CI_phpStorm.php helps NetBeans with code completion as well . 32 | 33 | ## Basic Instructions 34 | Fork and Clone or Clone to somewhere on your dev machine 35 | 36 | ***Get Laravel helpers*** 37 |
38 | git submodule init
39 | git submodule update
40 | 
41 | 42 | ***Loading in phpStorm*** 43 | 44 | Load your actual project, then you have 2 options, set include path or add content root (Your choice) 45 | 46 | **Option 1 =** 47 | File > Settings > Directories > Add Content Root > Select the directory in phpStorm-CC-helpers that is relevant > mark Resource Root 48 | 49 | **Option 2 =** 50 | In the project window right click External Libraries > Configure PHP Include Paths 51 | Then add the path to the phpStorm-CC-helpers that is relevant 52 | 53 | ### CodeIgniter Specific 54 | #### Mark as Plain Text 55 | * /system/core/Controller.php 56 | * /system/core/Model.php 57 | * /system/database/DB_active_rec.php 58 | 59 | You should now have code completion. 60 | 61 | ##### Using the my_models.php 62 | Add your models to the docBlock 63 | 64 | Example 65 | 66 | ```php 67 | @property membership_model $membership_model 68 | ``` 69 | 70 | membership_model is the model class and $membership_model will be the property you use to instance it. 71 | 72 | Example 73 | 74 | ```php 75 | $this->membership_model->get_records(); 76 | ``` 77 | 78 | This will give you code completion for your model methods. 79 | 80 | ### OpenCart Specific 81 | #### Mark as Plain Text 82 | * /system/engine/controller.php 83 | * /system/engine/model.php 84 | * /system/engine/loader.php 85 | 86 | 87 | -------------------------------------------------------------------------------- /WordPress/wpdb_docs.php: -------------------------------------------------------------------------------- 1 | term_id); 7 | 8 | /** 9 | * @property wpdb $ID 10 | * @property wpdb $post_author 11 | * @property wpdb $post_date 12 | * @property wpdb $post_date_gmt 13 | * @property wpdb $post_content 14 | * @property wpdb $post_title 15 | * @property wpdb $post_excerpt 16 | * @property wpdb $post_status 17 | * @property wpdb $comment_status 18 | * @property wpdb $ping_status 19 | * @property wpdb $post_password 20 | * @property wpdb $post_name 21 | * @property wpdb $to_ping 22 | * @property wpdb $pinged 23 | * @property wpdb $post_modified 24 | * @property wpdb $post_modified_gmt 25 | * @property wpdb $post_content_filtered 26 | * @property wpdb $post_parent 27 | * @property wpdb $guid 28 | * @property wpdb $menu_order 29 | * @property wpdb $post_type 30 | * @property wpdb $post_mime_type 31 | * @property wpdb $comment_count 32 | * @property wpdb $filter 33 | * @property wpdb $term_id 34 | * @property wpdb $name 35 | * 36 | */ 37 | class wpdb {} 38 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 2 | me.artpsb.idea.plugin.codeigniter.code.completion.helper 3 | CodeIgniter Code Completion Helper 4 | 1.0 5 | Jeff Behnke 6 | 7 | Mark as Plain Text 10 |
    11 |
  • /system/core/Controller.php
  • 12 |
  • /system/core/Model.php
  • 13 |
  • /system/database/DB_active_rec.php
  • 14 |
15 | ]]>
16 | 17 | 1.0 19 |
    20 |
  • Initial support.
  • 21 |
22 | ]]> 23 |
24 | 25 | 26 | 27 | com.intellij.modules.platform 28 | com.jetbrains.php 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /plugins/OpenCart/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | me.artpsb.idea.plugin.opencart.code.completion.helper 3 | OpenCart Code Completion Helper 4 | 1.0 5 | Jeff Behnke 6 | 7 | Mark as Plain Text 10 |
    11 |
  • /system/engine/controller.php
  • 12 |
  • /system/engine/model.php
  • 13 |
  • /system/engine/loader.php
  • 14 |
15 | 16 | ]]>
17 | 18 | 1.0 20 |
    21 |
  • Initial support.
  • 22 |
23 | ]]> 24 |
25 | 26 | 27 | 28 | com.intellij.modules.platform 29 | com.jetbrains.php 30 | 31 | 32 | 33 | 34 |
-------------------------------------------------------------------------------- /plugins/WordPress/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | me.artpsb.idea.plugin.wordpress.code.completion.helper 3 | WordPress Code Completion Helper 4 | 1.0 5 | Jeff Behnke 6 | 7 | 10 | 11 | 1.0 13 |
    14 |
  • Initial support.
  • 15 |
16 | ]]> 17 |
18 | 19 | 20 | 21 | com.intellij.modules.platform 22 | com.jetbrains.php 23 | 24 | 25 | 26 | 27 |
-------------------------------------------------------------------------------- /screenshots/CodeIgniter/acive_record1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/acive_record1.png -------------------------------------------------------------------------------- /screenshots/CodeIgniter/active_record2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/active_record2.png -------------------------------------------------------------------------------- /screenshots/CodeIgniter/active_record3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/active_record3.png -------------------------------------------------------------------------------- /screenshots/CodeIgniter/driver_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/driver_result.png -------------------------------------------------------------------------------- /screenshots/CodeIgniter/info-blocks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/info-blocks.png -------------------------------------------------------------------------------- /screenshots/CodeIgniter/mark-plain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/mark-plain.png -------------------------------------------------------------------------------- /screenshots/CodeIgniter/methods.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/methods.png -------------------------------------------------------------------------------- /screenshots/CodeIgniter/properties.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/topdown/phpStorm-CC-Helpers/0cf1c94b35f85169e009c3b6cc2d3318c03bcf04/screenshots/CodeIgniter/properties.png --------------------------------------------------------------------------------