Spaces:
Sleeping
Sleeping
File size: 30,587 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 | <?php
/**
* class.database_mysql.php
*
* @package gulliver.system
*
* ProcessMaker Open Source Edition
* Copyright (C) 2004 - 2011 Colosa Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
* Coral Gables, FL, 33134, USA, or email info@colosa.com.
*
*/
/**
*
* @package gulliver.system
*
*/
class database extends database_base
{
public $iFetchType = MYSQL_ASSOC;
/**
* class database constructor
*
* @param $sType adapter type
* @param $sServer server
* @param $sUser db user
* @param $sPass db user password
* @param $sDataBase Database name
*/
public function __construct ($sType = DB_ADAPTER, $sServer = DB_HOST, $sUser = DB_USER, $sPass = DB_PASS, $sDataBase = DB_NAME)
{
$this->sType = $sType;
$this->sServer = $sServer;
$this->sUser = $sUser;
$this->sPass = $sPass;
$this->sDataBase = $sDataBase;
$this->oConnection = @mysql_connect( $sServer, $sUser, $sPass ) || null;
$this->sQuoteCharacter = '`';
$this->nullString = 'null';
}
/**
* generate the sql sentence to create a table
*
* @param $sTable table name
* @param $aColumns array of columns
* @return $sSql the sql sentence
*/
public function generateCreateTableSQL ($sTable, $aColumns)
{
$sKeys = '';
$sSQL = 'CREATE TABLE IF NOT EXISTS ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . '(';
foreach ($aColumns as $sColumnName => $aParameters) {
if ($sColumnName != 'INDEXES') {
if ($sColumnName != '' && isset( $aParameters['Type'] ) && $aParameters['Type'] != '') {
$sSQL .= $this->sQuoteCharacter . $sColumnName . $this->sQuoteCharacter . ' ' . $aParameters['Type'];
if (isset( $aParameters['Null'] ) && $aParameters['Null'] == 'YES') {
$sSQL .= ' NULL';
} else {
$sSQL .= ' NOT NULL';
}
if (isset( $aParameters['AutoIncrement'] ) && $aParameters['AutoIncrement']) {
$sSQL .= ' AUTO_INCREMENT PRIMARY KEY';
}
if (isset( $aParameters['Key'] ) && $aParameters['Key'] == 'PRI') {
$sKeys .= $this->sQuoteCharacter . $sColumnName . $this->sQuoteCharacter . ',';
}
if (isset( $aParameters['Default'] )) {
$sSQL .= " DEFAULT '" . trim($aParameters['Default']) . "'";
}
$sSQL .= ',';
}
}
}
$sSQL = substr( $sSQL, 0, - 1 );
if ($sKeys != '') {
$sSQL .= ',PRIMARY KEY(' . substr( $sKeys, 0, - 1 ) . ')';
}
$sSQL .= ')ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci' . $this->sEndLine;
return $sSQL;
}
/**
* generate a drop table sentence
*
* @param $sTable table name
* @return sql sentence string
*/
public function generateDropTableSQL ($sTable)
{
return 'DROP TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . $this->sEndLine;
}
/**
* generate rename table sentence
*
* @param $sTableOld old table name
* @return $sSql sql sentence
*/
public function generateRenameTableSQL ($sTableOld)
{
$sSQL = 'ALTER TABLE ' . $sTableOld . ' RENAME TO RBAC_' . $sTableOld;
return $sSQL;
}
/**
* generate drop column sentence
*
* @param $sTable table name
* @param $sColumn column name
* @return $sSql sql sentence
*/
public function generateDropColumnSQL ($sTable, $sColumn)
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP COLUMN ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . $this->sEndLine;
return $sSQL;
}
/**
* This method has to refactor
* @param $sTable
* @param $sColumn
* @param $aParameters
* @return string
*/
public function generateCheckAddColumnSQL($sTable, $sColumn, $aParameters)
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP PRIMARY KEY ';
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* This method has to refactor
* @param $sTable
* @param $sColumn
* @param $aParameters
* @return string
*/
public function deleteAllIndexesIntable($sTable, $sColumn, $aParameters)
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP INDEX indexLoginLog ';
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* This method is used exclusively to verify if it was made changes in the DB to solve the HOR-1787 issue, later
* a generic method which covers all the possible similar problems found in the HOR-1787 issue will be generated.
* @param $sTable
* @param $sColumn
* @param $aParameters
* @return bool
*/
public function checkPatchHor1787($sTable, $sColumn, $aParameters)
{
if (isset($aParameters['AutoIncrement']) && $aParameters['AutoIncrement'] && $sTable == 'LOGIN_LOG') {
return true;
}
return false;
}
/**
* generate an add column sentence
*
* @param $sTable table name
* @param $sColumn column name
* @param $aParameters parameters of field like typo or if it can be null
* @return $sSql sql sentence
*/
public function generateAddColumnSQL ($sTable, $sColumn, $aParameters)
{
if (isset( $aParameters['Type'] ) && isset( $aParameters['Null'] )) {
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' ADD COLUMN ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . ' ' . $aParameters['Type'];
if ($aParameters['Null'] == 'YES') {
$sSQL .= ' NULL';
} else {
$sSQL .= ' NOT NULL';
}
}
if (isset( $aParameters['AutoIncrement'] ) && $aParameters['AutoIncrement']) {
$sSQL .= ' AUTO_INCREMENT';
}
if (isset( $aParameters['PrimaryKey'] ) && $aParameters['PrimaryKey']) {
$sSQL .= ' PRIMARY KEY';
}
if (isset( $aParameters['Unique'] ) && $aParameters['Unique']) {
$sSQL .= ' UNIQUE';
}
//we need to check the property AI
if (isset( $aParameters['AI'] )) {
if ($aParameters['AI'] == 1) {
$sSQL .= ' AUTO_INCREMENT';
} else {
if ($aParameters['Default'] != '') {
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
}
}
} else {
if (isset( $aParameters['Default'] )) {
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
}
}
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* generate a change column sentence
*
* @param $sTable table name
* @param $sColumn column name
* @param $aParameters parameters of field like typo or if it can be null
* @param $sColumnNewName column new name
* @return $sSql sql sentence
*/
public function generateChangeColumnSQL ($sTable, $sColumn, $aParameters, $sColumnNewName = '')
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' CHANGE COLUMN ' . $this->sQuoteCharacter . ($sColumnNewName != '' ? $sColumnNewName : $sColumn) . $this->sQuoteCharacter . ' ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter;
if (isset( $aParameters['Type'] )) {
$sSQL .= ' ' . $aParameters['Type'];
}
if (isset( $aParameters['Null'] )) {
if ($aParameters['Null'] == 'YES') {
$sSQL .= ' NULL';
} else {
$sSQL .= ' NOT NULL';
}
}
//if (isset($aParameters['AI'])) {
// if ($aParameters['AI'] == 1) {
// $sSQL .= ' AUTO_INCREMENT';
// }
// else {
// if (isset($aParameters['Default'])) {
// if ($aParameters['Default'] != '') {
// $sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
// }
// }
// }
//}
//else {
if (isset( $aParameters['Default'] )) {
if (trim( $aParameters['Default'] ) == '' && $aParameters['Type'] == 'datetime') {
//do nothing
} else {
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
}
//}
}
if (! isset( $aParameters['Default'] ) && isset( $aParameters['Null'] ) && $aParameters['Null'] == 'YES') {
$sSQL .= " DEFAULT NULL ";
}
//}
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* Generate and get the primary key in a sentence
*
* @param $sTable table name
* @return $sSql sql sentence
*/
public function generateGetPrimaryKeysSQL ($sTable)
{
try {
if ($sTable == '') {
throw new Exception( 'The table name cannot be empty!' );
}
return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' WHERE Seq_in_index = 1' . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to drop the primary key
*
* @param $sTable table name
* @return sql sentence
*/
public function generateDropPrimaryKeysSQL ($sTable)
{
try {
if ($sTable == '') {
throw new Exception( 'The table name cannot be empty!' );
}
return 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP PRIMARY KEY' . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to add multiple primary keys
*
* @param $sTable table name
* @param $aPrimaryKeys array of primary keys
* @return sql sentence
*/
public function generateAddPrimaryKeysSQL ($sTable, $aPrimaryKeys)
{
try {
if ($sTable == '') {
throw new Exception( 'The table name cannot be empty!' );
}
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' ADD PRIMARY KEY (';
foreach ($aPrimaryKeys as $sKey) {
$sSQL .= $this->sQuoteCharacter . $sKey . $this->sQuoteCharacter . ',';
}
$sSQL = substr( $sSQL, 0, - 1 ) . ')' . $this->sEndLine;
return $sSQL;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to drop an index
*
* @param $sTable table name
* @param $sIndexName index name
* @return sql sentence
*/
public function generateDropKeySQL ($sTable, $sIndexName)
{
try {
if ($sTable == '') {
throw new Exception( 'The table name cannot be empty!' );
}
if ($sIndexName == '') {
throw new Exception( 'The column name cannot be empty!' );
}
return 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP INDEX ' . $this->sQuoteCharacter . $sIndexName . $this->sQuoteCharacter . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to add indexes or primary keys
*
* @param $sTable table name
* @param $indexName index name
* @param $aKeys array of keys
* @return sql sentence
*/
public function generateAddKeysSQL ($sTable, $indexName, $aKeys)
{
try {
$indexType = 'INDEX';
if ($indexName == 'primaryKey' || $indexName == 'PRIMARY') {
$indexType = 'PRIMARY';
$indexName = 'KEY';
}
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' ADD ' . $indexType . ' ' . $indexName . ' (';
foreach ($aKeys as $sKey) {
$sSQL .= $this->sQuoteCharacter . $sKey . $this->sQuoteCharacter . ', ';
}
$sSQL = substr( $sSQL, 0, - 2 );
$sSQL .= ')' . $this->sEndLine;
return $sSQL;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to show the tables
*
* @return sql sentence
*/
public function generateShowTablesSQL ()
{
return 'SHOW TABLES' . $this->sEndLine;
}
/**
* generate a sentence to show the tables with a like sentence
*
* @return sql sentence
*/
public function generateShowTablesLikeSQL ($sTable)
{
return "SHOW TABLES LIKE '" . $sTable . "'" . $this->sEndLine;
}
/**
* generate a sentence to show the tables with a like sentence
*
* @param $sTable table name
* @return sql sentence
*/
public function generateDescTableSQL ($sTable)
{
try {
if ($sTable == '') {
throw new Exception( 'The table name cannot be empty!' );
}
return 'DESC ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to show some table indexes
*
* @param $sTable table name
* @return sql sentence
*/
public function generateTableIndexSQL ($sTable)
{
return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . " " . $this->sEndLine;
//return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . " WHERE Key_name <> 'PRIMARY'" . $this->sEndLine;
}
/**
* execute a sentence to check if there is connection
*
* @return void
*/
public function isConnected ()
{
if (! $this->oConnection) {
return false;
}
return $this->executeQuery( 'USE ' . $this->sDataBase );
}
/**
* generate a sentence to show the tables with a like sentence
*
* @param $sQuery sql query string
* @return void
*/
public function logQuery ($sQuery)
{
try {
$found = false;
if (substr( $sQuery, 0, 6 ) == 'SELECT') {
$found = true;
}
if (substr( $sQuery, 0, 4 ) == 'SHOW') {
$found = true;
}
if (substr( $sQuery, 0, 4 ) == 'DESC') {
$found = true;
}
if (substr( $sQuery, 0, 4 ) == 'USE ') {
$found = true;
}
if (! $found) {
$logDir = PATH_DATA . 'log';
if (! file_exists( $logDir )) {
if (! mkdir( $logDir )) {
return;
}
}
$logFile = "$logDir/query.log";
$fp = fopen( $logFile, 'a+' );
if ($fp !== false) {
fwrite( $fp, date( "Y-m-d H:i:s" ) . " " . $this->sDataBase . " " . $sQuery . "\n" );
fclose( $fp );
}
}
} catch (Exception $oException) {
}
}
/**
* execute a sql query
*
* @param $sQuery table name
* @return void
*/
public function executeQuery ($sQuery)
{
$this->logQuery( $sQuery );
try {
if ($this->oConnection) {
@mysql_select_db( $this->sDataBase );
return @mysql_query( $sQuery );
} else {
throw new Exception( 'invalid connection to database ' . $this->sDataBase );
}
} catch (Exception $oException) {
$this->logQuery( $oException->getMessage() );
throw $oException;
}
}
/**
* count the rows of a dataset
*
* @param $oDataset
* @return the number of rows
*/
public function countResults ($oDataset)
{
return @mysql_num_rows( $oDataset );
}
/**
* count an array of the registry from a dataset
*
* @param $oDataset
* @return the registry
*/
public function getRegistry ($oDataset)
{
return @mysql_fetch_array( $oDataset, $this->iFetchType );
}
/**
* close the current connection
*
* @return void
*/
public function close ()
{
@mysql_close( $this->oConnection );
}
public function generateInsertSQL ($table, $data)
{
$fields = array ();
$values = array ();
foreach ($data as $field) {
$fields[] = $field['field'];
if (! is_null( $field['value'] )) {
switch ($field['type']) {
case 'text':
case 'date':
$values[] = "'" . mysql_real_escape_string( $field['value'] ) . "'";
break;
case 'int':
default:
$values[] = mysql_real_escape_string( $field['value'] );
break;
}
} else {
$values[] = $this->nullString;
}
}
$fields = array_map( array ($this,'putQuotes'
), $fields );
$sql = sprintf( "INSERT INTO %s (%s) VALUES (%s)", $this->putQuotes( $table ), implode( ', ', $fields ), implode( ', ', $values ) );
return $sql;
}
public function generateUpdateSQL ($table, $keys, $data)
{
$fields = array ();
$where = array ();
foreach ($data as $field) {
if (! is_null( $field['value'] )) {
switch ($field['type']) {
case 'text':
case 'date':
$fields[] = $this->putQuotes( $field['field'] ) . " = '" . mysql_real_escape_string( $field['value'] ) . "'";
break;
case 'int':
default:
$fields[] = $this->putQuotes( $field['field'] ) . " = " . mysql_real_escape_string( $field['value'] );
break;
}
} else {
$values[] = $this->nullString;
}
if (in_array( $field['field'], $keys )) {
$where[] = $fields[count( $fields ) - 1];
}
}
$sql = sprintf( "UPDATE %s SET %s WHERE %s", $this->putQuotes( $table ), implode( ', ', $fields ), implode( ', ', $where ) );
return $sql;
}
public function generateDeleteSQL ($table, $keys, $data)
{
$fields = array ();
$where = array ();
foreach ($data as $field) {
if (in_array( $field['field'], $keys )) {
if (! is_null( $field['value'] )) {
switch ($field['type']) {
case 'text':
case 'date':
$where[] = $this->putQuotes( $field['field'] ) . " = '" . mysql_real_escape_string( $field['value'] ) . "'";
break;
case 'int':
default:
$where[] = $this->putQuotes( $field['field'] ) . " = " . mysql_real_escape_string( $field['value'] );
break;
}
} else {
$values[] = $this->nullString;
}
}
}
$sql = sprintf( "DELETE FROM %s WHERE %s", $this->putQuotes( $table ), implode( ', ', $where ) );
return $sql;
}
public function generateSelectSQL ($table, $keys, $data)
{
$fields = array ();
$where = array ();
foreach ($data as $field) {
if (in_array( $field['field'], $keys )) {
if (! is_null( $field['value'] )) {
switch ($field['type']) {
case 'text':
case 'date':
$where[] = $this->putQuotes( $field['field'] ) . " = '" . mysql_real_escape_string( $field['value'] ) . "'";
break;
case 'int':
default:
$where[] = $this->putQuotes( $field['field'] ) . " = " . mysql_real_escape_string( $field['value'] );
break;
}
} else {
$values[] = $this->nullString;
}
}
}
$sql = sprintf( "SELECT * FROM %s WHERE %s", $this->putQuotes( $table ), implode( ', ', $where ) );
return $sql;
}
private function putQuotes ($element)
{
return $this->sQuoteCharacter . $element . $this->sQuoteCharacter;
}
/*=================================================================================================*/
/**
* concatString
* Generates a string equivalent to the chosen database.
*
* author Hector Cortez <hector@gmail.com>
* date 2010-08-04
*
* @return string $sConcat
*/
public function concatString ()
{
$nums = func_num_args();
$vars = func_get_args();
$sConcat = " CONCAT(";
for ($i = 0; $i < $nums; $i ++) {
if (isset( $vars[$i] )) {
$sConcat .= $vars[$i];
if (($i + 1) < $nums) {
$sConcat .= ", ";
}
}
}
$sConcat .= ")";
return $sConcat;
}
/*
* query functions for class class.case.php
*
*/
/**
* concatString
* Generates a string equivalent to the case when
*
* author Hector Cortez <hector@gmail.com>
* date 2010-08-04
*
* @return string $sCompare
*/
public function getCaseWhen ($compareValue, $trueResult, $falseResult)
{
$sCompare = "IF(" . $compareValue . ", " . $trueResult . ", " . $falseResult . ") ";
return $sCompare;
}
/**
* Generates a string equivalent to create table ObjectPermission
*
* class.case.php
* function verifyTable()
*
* @return string $sql
*/
public function createTableObjectPermission ()
{
$sql = "CREATE TABLE IF NOT EXISTS `OBJECT_PERMISSION` (
`OP_UID` varchar(32) NOT NULL,
`PRO_UID` varchar(32) NOT NULL,
`TAS_UID` varchar(32) NOT NULL,
`USR_UID` varchar(32) NOT NULL,
`OP_USER_RELATION` int(1) NOT NULL default '1',
`OP_TASK_SOURCE` varchar(32) NOT NULL,
`OP_PARTICIPATE` int(1) NOT NULL default '1',
`OP_OBJ_TYPE` varchar(15) NOT NULL default 'ANY',
`OP_OBJ_UID` varchar(32) NOT NULL,
`OP_ACTION` varchar(10) NOT NULL default 'VIEW',
KEY `PRO_UID` (`PRO_UID`,`TAS_UID`,`USR_UID`,`OP_TASK_SOURCE`,`OP_OBJ_UID`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;";
return $sql;
}
/*
* query functions for class class.report.php
*
*/
/**
* Generates a string query
*
* class.report.php
* function generatedReport4()
*
* @return string $sql
*/
public function getSelectReport4 ()
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$sql = "SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
MIN(AD.DEL_DURATION) AS MIN,
MAX(AD.DEL_DURATION) AS MAX,
SUM(AD.DEL_DURATION) AS TOTALDUR,
AVG(AD.DEL_DURATION) AS PROMEDIO
FROM APPLICATION AS A
LEFT JOIN APP_DELEGATION AS AD ON(A.APP_UID = AD.APP_UID AND AD.DEL_INDEX=1)
LEFT JOIN USERS AS U ON(U.USR_UID = A.APP_INIT_USER)
WHERE A.APP_UID<>''
GROUP BY " . $sqlGroupBy;
return $sql;
}
/**
* Generates a string query
*
* class.report.php
* function generatedReport4_filter()
*
* @return string $sql
*/
public function getSelectReport4Filter ($var)
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$sql = " SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
MIN(AD.DEL_DURATION) AS MIN,
MAX(AD.DEL_DURATION) AS MAX,
SUM(AD.DEL_DURATION) AS TOTALDUR,
AVG(AD.DEL_DURATION) AS PROMEDIO
FROM APPLICATION AS A
LEFT JOIN APP_DELEGATION AS AD ON(A.APP_UID = AD.APP_UID AND AD.DEL_INDEX=1)
LEFT JOIN USERS AS U ON(U.USR_UID = A.APP_INIT_USER)
" . $var . "
GROUP BY " . $sqlGroupBy;
return $sql;
}
/**
* Generates a string query
*
* class.report.php
* function generatedReport5()
*
* @return string $sql
*/
public function getSelectReport5 ()
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$sql = " SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
MIN(AD.DEL_DURATION) AS MIN,
MAX(AD.DEL_DURATION) AS MAX,
SUM(AD.DEL_DURATION) AS TOTALDUR,
AVG(AD.DEL_DURATION) AS PROMEDIO
FROM APP_DELEGATION AS AD
LEFT JOIN PROCESS AS P ON (P.PRO_UID = AD.PRO_UID)
LEFT JOIN USERS AS U ON(U.USR_UID = AD.USR_UID)
WHERE AD.APP_UID<>'' AND AD.DEL_FINISH_DATE IS NULL
GROUP BY " . $sqlGroupBy;
return $sql;
}
/**
* Generates a string query
*
* class.report.php
* function generatedReport5_filter()
*
* @return string $sql
*/
public function getSelectReport5Filter ($var)
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$sql = "SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
MIN(AD.DEL_DURATION) AS MIN,
MAX(AD.DEL_DURATION) AS MAX,
SUM(AD.DEL_DURATION) AS TOTALDUR,
AVG(AD.DEL_DURATION) AS PROMEDIO
FROM APP_DELEGATION AS AD
LEFT JOIN PROCESS AS P ON (P.PRO_UID = AD.PRO_UID)
LEFT JOIN USERS AS U ON(U.USR_UID = AD.USR_UID)
" . $var . "
GROUP BY " . $sqlGroupBy;
return $sql;
}
/*
* query functions for class class.net.php
*
*/
public function getServerVersion ($driver, $dbIP, $dbPort, $dbUser, $dbPasswd, $dbSourcename)
{
if ($link = @mysql_connect( $dbIP, $dbUser, $dbPasswd )) {
$v = @mysql_get_server_info();
} else {
throw new Exception( @mysql_error( $link ) );
}
return (isset( $v )) ? $v : 'none';
}
/*
* query functions for class class.net.php, class.reportTables.php
*
*/
public function getDropTable ($sTableName)
{
$sql = 'DROP TABLE IF EXISTS `' . $sTableName . '`';
return $sql;
}
public function getTableDescription ($sTableName)
{
$sql = "DESC " . $sTableName;
return $sql;
}
public function getFieldNull ()
{
$fieldName = "Null";
return $fieldName;
}
public function getValidate ($validate)
{
$oValidate = $validate;
return $oValidate;
}
/**
* Determines whether a table exists
* It is part of class.reportTables.php
*/
public function reportTableExist ()
{
$filter = new InputFilter();
$DB_NAME = $filter->validateInput(DB_NAME);
$bExists = true;
$oConnection = mysql_connect( DB_HOST, DB_USER, DB_PASS );
mysql_select_db( $DB_NAME );
$oDataset = mysql_query( 'SELECT COUNT(*) FROM REPORT_TABLE' ) || ($bExists = false);
return $bExists;
}
/**
* It is part of class.pagedTable.php
*/
public function getLimitRenderTable ($nCurrentPage, $nRowsPerPage)
{
$sql = ' LIMIT ' . (($nCurrentPage - 1) * $nRowsPerPage) . ', ' . $nRowsPerPage;
return $sql;
}
/**
* Determining the existence of a table
*/
public function tableExists ($tableName, $database)
{
@mysql_select_db( $database );
$tables = array ();
$tablesResult = mysql_query( "SHOW TABLES FROM $database;" );
while ($row = @mysql_fetch_row( $tablesResult )) {
$tables[] = $row[0];
}
if (in_array( $tableName, $tables )) {
return true;
}
return false;
}
/*
* Determining the existence of a table (Depricated)
*/
// function tableExists ($table, $db) {
// $tables = mysql_list_tables ($db);
// while (list ($temp) = @mysql_fetch_array ($tables)) {
// if ($temp == $table) {
// return TRUE;
// }
// }
// return FALSE;
// }
}
|