kenken999/php
0
1<?php2///////////////////////////////////////////////////////////////////////////3// Data from wizard4///////////////////////////////////////////////////////////////////////////5class SQLEntity6{7 protected $connection = null;8 9 function __construct(){}10 function IsAggregationFunctionCall() 11 {12 return false;13 }14 function IsBinary() 15 {16 return false;17 }18 function IsAsterisk() 19 {20 return false;21 }22 function SetQuery(&$query)23 {24 }25 function IsSQLField()26 {27 return false;28 }29 30 /**31 * Set the 'connection' property32 * @param String srcTableName33 */34 protected function setConnection( $srcTableName )35 {36 global $cman;37 $this->connection = $cman->byTable( $srcTableName );38 } 39};40 41class SQLNonParsed extends SQLEntity42{43 var $m_sql;44 function __construct($proto)45 {46 $this->m_sql = isset($proto["m_sql"]) ? $proto["m_sql"] : '';47 }48 49 function toSql($query)50 {51 return $this->m_sql;52 }53 54 function IsAsterisk() 55 {56 $last = substr($this->m_sql,strlen($this->m_sql)-1);57 return ($last=="*");58 }59 60}61 62class SQLField extends SQLEntity63{64 var $m_strName;65 var $m_strTable;66 67 /**68 * The data source table name69 * @type String70 */71 protected $m_srcTableName;72 73 74 function __construct($proto)75 {76 $this->m_strName = isset($proto["m_strName"]) ? $proto["m_strName"] : null;77 $this->m_strTable = isset($proto["m_strTable"]) ? $proto["m_strTable"] : null;78 $this->m_srcTableName = $proto["m_srcTableName"];79 }80 81 function toSql($query)82 {83 if( is_null($this->connection) )84 $this->setConnection( $this->m_srcTableName );85 86 if( $query->cipherer != null )87 {88 return $query->cipherer->GetFieldName(89 ($this->m_strTable != "" && $query->TableCount() > 1 ? $this->connection->addTableWrappers($this->m_strTable)."." : "")90 .$this->connection->addFieldWrappers($this->m_strName) );91 }92 93 $fieldName = $this->connection->addFieldWrappers($this->m_strName);94 if( $this->m_strTable == "" || $query->TableCount() == 1 )95 return $fieldName;96 97 return $this->connection->addTableWrappers($this->m_strTable) . "." . $fieldName;98 }99 100 function GetType()101 {102 $pSet = new ProjectSettings($this->m_strTable);103 return $pSet->getFieldType($this->m_strName);104 }105 106 function IsBinary()107 {108 return IsBinaryType($this->GetType());109 }110 111 function IsSQLField()112 {113 return true;114 }115}116 117class SQLFieldListItem extends SQLEntity118{119 var $m_sql;120 var $m_expr; 121 var $m_alias;122 var $m_isEncrypted = false;123 124 /**125 * The data source table name126 * @type String127 */128 protected $m_srcTableName;129 130 131 function __construct($proto)132 {133 $this->m_expr = isset($proto["m_expr"]) ? $proto["m_expr"] : null;134 $this->m_alias = isset($proto["m_alias"]) ? $proto["m_alias"] : null;135 $this->m_sql = isset($proto["m_sql"]) ? $proto["m_sql"] : null;136 137 $this->m_srcTableName = $proto["m_srcTableName"];138 }139 140 function toSql($query, $addAlias = true)141 {142 $ret = '';143 if($this->m_expr)144 {145 if(is_string($this->m_expr))146 {147 $ret = $this->m_expr;148 }149 else150 {151 if(is_a($this->m_expr, 'SQLQuery'))152 {153 $ret = $this->m_sql;154 }155 else156 {157 $ret = $this->m_expr->toSql($query);158 }159 160 }161 }162 if($this->m_isEncrypted ) 163 {164 // ASP conversion requirement165 if( !$query->cipherer->isEncryptionByPHPEnabled() )166 $ret = $query->cipherer->GetEncryptedFieldName($ret);167 }168 169 if($addAlias)170 {171 if( is_null($this->connection) )172 $this->setConnection( $this->m_srcTableName );173 174 if($this->m_alias != "")175 {176 $ret .= ' as ' . $this->connection->addFieldWrappers($this->m_alias);177 }178 elseif(is_object($this->m_expr))179 {180 if($this->m_expr->IsSQLField() && $query->cipherer != null )181 {182 // ASP conversion requirement183 if( !$query->cipherer->isEncryptionByPHPEnabled() )184 {185 if($query->cipherer->isFieldEncrypted($this->m_expr->m_strName)){186 $ret .= ' as ' . $this->connection->addFieldWrappers($this->m_expr->m_strName); 187 }188 }189 }190 }191 }192 193 return $ret;194 }195 196 function IsAsterisk() 197 {198 if(is_object($this->m_expr))199 return $this->m_expr->IsAsterisk();200 return false;201 }202 function IsAggregationFunctionCall()203 {204 if(is_object($this->m_expr))205 return $this->m_expr->IsAggregationFunctionCall();206 return false;207 }208 function getAlias()209 {210 if(isset($this->m_alias) && !isset($this->m_expr->m_strName))211 {212 return $this->m_alias;213 }214 elseif(isset($this->m_expr->m_strName))215 {216 if(isset($this->m_alias) && $this->m_alias!="")217 {218 return $this->m_alias;219 }220 else221 {222 return $this->m_expr->m_strName;223 }224 }225 }226}227 228class SQLFromListItem extends SQLEntity229{230 var $m_sql;231 var $m_link;232 var $m_table;233 var $m_alias;234 var $m_joinon;235 236 /**237 * The data source table name238 * @type String239 */240 protected $m_srcTableName;241 242 function __construct($proto)243 {244 $this->m_link = isset($proto["m_link"]) ? $proto["m_link"] : null;245 $this->m_table = isset($proto["m_table"]) ? $proto["m_table"] : null;246 $this->m_alias = isset($proto["m_alias"]) ? $proto["m_alias"] : null;247 $this->m_joinon = isset($proto["m_joinon"]) ? $proto["m_joinon"] : null;248 $this->m_sql = isset($proto["m_sql"]) ? $proto["m_sql"] : null;249 250 $this->m_srcTableName = $proto["m_srcTableName"];251 }252 253 function SetQuery(&$query)254 {255 if(is_object($this->m_table))256 $this->m_table->SetQuery($query);257 }258 259 function toSql($query, $first)260 {261 $ret = '';262 $skipAlias = false;263 if(is_a($this->m_table, "SQLTable"))264 {265 $ret .= $this->m_table->toSql($query);266 }267 else268 {269 return $this->m_sql;270 }271 272 if($this->m_alias != '' && !$skipAlias)273 {274 if( is_null($this->connection) )275 $this->setConnection( $this->m_srcTableName );276 277 $ret .= ' ' . $this->connection->addFieldWrappers($this->m_alias);278 }279 280 if($this->m_link == 'SQLL_MAIN')281 {282 return $ret;283 }284 285 switch($this->m_link)286 {287 case 'SQLL_INNERJOIN':288 $ret = ' INNER JOIN ' . $ret;289 break;290 case 'SQLL_NATURALJOIN':291 $ret = ' NATURAL JOIN ' . $ret;292 break;293 case 'SQLL_LEFTJOIN':294 $ret = ' LEFT OUTER JOIN ' . $ret;295 break;296 case 'SQLL_RIGHTJOIN':297 $ret = ' RIGHT OUTER JOIN ' . $ret;298 break;299 case 'SQLL_FULLOUTERJOIN':300 $ret = ' FULL OUTER JOIN ' . $ret;301 break;302 case 'SQLL_CROSSJOIN':303 $ret = (!$first ? ',' : '') . $ret;304 break;305 }306 307 $joinStr = $this->m_joinon->toSql($query);308 if($joinStr != '')309 {310 $ret .= ' ON ' . $joinStr;311 }312 313 return $ret;314 }315 316 function getIdentifier()317 {318 if( $this->m_alias != '' )319 return $this->m_alias;320 321 return $this->m_table;322 } 323}324 325class SQLJoinOn extends SQLEntity326{327 var $m_field1;328 var $m_field2;329 330 function __construct($proto)331 {332 $this->m_field1 = isset($proto["m_field1"]) ? $proto["m_field1"] : null;333 $this->m_field2 = isset($proto["m_field2"]) ? $proto["m_field2"] : null;334 }335}336 337class SQLFunctionCall extends SQLEntity338{339 var $m_functiontype;340 var $m_strFunctionName;341 var $m_arguments;342 function __construct($proto)343 {344 $this->m_functiontype = isset($proto["m_functiontype"]) ? $proto["m_functiontype"] : null;345 $this->m_strFunctionName = isset($proto["m_strFunctionName"]) ? $proto["m_strFunctionName"] : null;346 $this->m_arguments = isset($proto["m_arguments"]) ? $proto["m_arguments"] : null;347 }348 349 function toSql($query)350 {351 $ret = '';352 switch($this->m_functiontype)353 {354 case 'SQLF_AVG':355 $ret .= ' AVG';356 break;357 case 'SQLF_SUM':358 $ret .= ' SUM';359 break;360 case 'SQLF_MIN':361 $ret .= ' MIN';362 break;363 case 'SQLF_MAX':364 $ret .= ' MAX';365 break;366 case 'SQLF_COUNT':367 $ret .= ' COUNT';368 break;369 default:370 $ret .= $this->m_strFunctionName;371 }372 $args = array();373 foreach($this->m_arguments as $a)374 {375 $args []= $a->toSql($query);376 }377 $ret .= '('.implode(',', $args).')';378 return $ret;379 }380 function IsAggregationFunctionCall() 381 {382 switch($this->m_functiontype)383 {384 case 'SQLF_AVG':385 case 'SQLF_SUM':386 case 'SQLF_MIN':387 case 'SQLF_MAX':388 case 'SQLF_COUNT':389 return true;390 }391 if( strtolower( $this->m_strFunctionName ) == "group_concat" )392 return true;393 return false;394 }395}396 397class SQLGroupByItem extends SQLEntity398{399 var $m_column;400 function __construct($proto)401 {402 $this->m_column = isset($proto["m_column"]) ? $proto["m_column"] : null;403 }404 405 function toSql($query)406 {407 return $this->m_column->toSql($query);408 }409}410 411define("LE_AND", 1);412define("LE_OR", 2);413 414define("LE_ISNULL", 1);415define("LE_EQ", 2);416 417define("F_AGG", 1);418define("F_SIMPLE", 2);419 420 421class SQLLogicalExpr extends SQLEntity422{423 var $m_uniontype;424 var $m_column;425 var $m_strCase;426 var $m_havingmode;427 var $m_contained;428 var $m_inBrackets;429 var $m_useAlias;430 var $m_sql;431 432 var $query;433 var $postSql;434 435 function __construct($proto)436 {437 $this->m_sql = isset($proto["m_sql"]) ? $proto["m_sql"] : null;438 $this->m_uniontype = isset($proto["m_uniontype"]) ? $proto["m_uniontype"] : null;439 $this->m_column = isset($proto["m_column"]) ? $proto["m_column"] : null;440 $this->m_strCase = isset($proto["m_strCase"]) ? $proto["m_strCase"] : null;441 $this->m_havingmode = isset($proto["m_havingmode"]) ? $proto["m_havingmode"] : null;442 $this->m_contained = isset($proto["m_contained"]) ? $proto["m_contained"] : null;443 $this->m_inBrackets = isset($proto["m_inBrackets"]) ? $proto["m_inBrackets"] : null;444 $this->m_useAlias = isset($proto["m_useAlias"]) ? $proto["m_useAlias"] : null;445 $this->postSql = array();446 }447 448 function SetQuery(&$query)449 {450 $this->query = &$query;451 for($nCnt = 0; $nCnt < count($this->m_contained); $nCnt ++)452 {453 $this->m_contained[$nCnt]->SetQuery($query);454 }455 }456 457 function toSql($query)458 {459 $ret = '';460 if($this->haveContained())461 {462 // guess glue463 $glue = '';464 if($this->m_uniontype == 'SQLL_AND')465 {466 $glue = ' AND ';467 }468 else if($this->m_uniontype == 'SQLL_OR')469 {470 $glue = ' OR ';471 }472 else473 {474 die ('Unknown union type in SQL Logical Expression');475 }476 477 // get list of contained sql478 $contained = array();479 foreach($this->m_contained as $c)480 {481 $cSql = $c->toSql($query);482 if($cSql != '')483 {484 $contained []= "(".$cSql.")";485 }486 }487 488 // concatenate it489 if(count($contained) > 0)490 {491 $ret = implode($glue, $contained);492 }493 494 // concatenate result with weak typed sql495 if(count($this->postSql) > 0)496 {497 if($ret == '')498 {499 $ret .= '(' . $this->postSql[0] . ')';500 }501 else502 {503 // $ret contains subtree, therefore escape it with brackets504 $ret = '(' . $ret . ')' . $glue . '(' . $this->postSql[0] . ')';505 }506 507 for($nCnt = 1; $nCnt < count($this->postSql); $nCnt++)508 {509 // concatenation of logical expressions of one union type510 $ret .= $glue . '(' . $this->postSql[$nCnt] . ')';511 }512 }513 514 if($this->m_inBrackets)515 {516 $ret = '(' . $ret . ')';517 }518 519 return $ret;520 }521 522 if( $this->m_sql ) {523 return $this->m_sql;524 }525 if(!$this->m_column)526 {527 $ret = $this->m_sql;528 }529 else530 {531 if($this->m_useAlias)532 {533 $ret = $this->m_column->m_alias;534 }535 else536 {537 $ret = $this->m_column->toSql($query);538 }539 }540 541 if($this->m_strCase == 'NOT')542 {543 return ' NOT ' . $ret;544 }545 else546 {547 if($ret != '')548 {549 $ret .= ' ' . $this->m_strCase;550 }551 }552 553 if($this->m_inBrackets)554 {555 $ret = '(' . $ret . ')';556 }557 558 return $ret;559 }560 561 function haveContained()562 {563 return count($this->m_contained) > 0 || count($this->postSql) > 0;564 }565}566 567class SQLOrderByItem extends SQLEntity568{569 var $m_column;570 var $m_bAsc;571 var $m_nColumn;572 function __construct($proto)573 {574 $this->m_column = isset($proto["m_column"]) ? $proto["m_column"] : null;575 $this->m_bAsc = isset($proto["m_bAsc"]) ? $proto["m_bAsc"] : null;576 $this->m_nColumn = isset($proto["m_nColumn"]) ? $proto["m_nColumn"] : null;577 }578 579 function toSql($query)580 {581 $ret = '';582 if(0 == $this->m_nColumn)583 {584 $ret .= $this->m_column->toSql($query);585 }586 else587 {588 $ret .= $this->m_nColumn;589 }590 591 if(!$this->m_bAsc)592 {593 $ret .= ' DESC ';594 }595 596 return $ret;597 }598}599 600class SQLTable extends SQLEntity601{602 var $m_strName;603 var $m_columns;604 var $query;605 606 /**607 * The data source table name608 * @type String609 */610 protected $m_srcTableName; 611 612 613 function __construct($proto)614 {615 $this->m_strName = isset($proto["m_strName"]) ? $proto["m_strName"] : null;616 $this->m_columns = isset($proto["m_columns"]) ? $proto["m_columns"] : null;617 618 $this->m_srcTableName = $proto["m_srcTableName"];619 }620 621 function SetQuery(&$query)622 {623 $this->query = $query;624 }625 626 function toSql($query)627 {628 if( is_null($this->connection) )629 $this->setConnection( $this->m_srcTableName );630 631 return $this->connection->addTableWrappers($this->m_strName);632 }633}634 635class SQLQuery extends SQLEntity636{637 var $m_strHead;638 var $m_strFieldList;639 var $m_strFrom;640 var $m_strWhere;641 var $m_strOrderBy;642 var $m_where;643 var $m_having;644 var $m_fieldlist;645 var $m_fromlist;646 var $m_groupby;647 var $m_orderby;648 var $bHasAsterisks = false;649 var $m_proto = array();650 /**651 * Instance of Cypher class for encoding/decoding fields values and names 652 *653 * @var object654 */655 var $cipherer = null;656 657 public $m_srcTableName;658 659 function __construct($proto)660 {661 $this->m_proto = $proto;662 $this->m_strHead = isset($proto["m_strHead"]) ? $proto["m_strHead"] : null;663 $this->m_strFieldList = isset($proto["m_strFieldList"]) ? $proto["m_strFieldList"] : null;664 $this->m_strFrom = isset($proto["m_strFrom"]) ? $proto["m_strFrom"] : null;665 $this->m_strWhere = isset($proto["m_strWhere"]) ? $proto["m_strWhere"] : null;666 $this->m_strOrderBy = isset($proto["m_strOrderBy"]) ? $proto["m_strOrderBy"] : null;667 $this->m_where = isset($proto["m_where"]) ? $proto["m_where"] : null;668 $this->m_having = isset($proto["m_having"]) ? $proto["m_having"] : null;669 $this->m_fieldlist = isset($proto["m_fieldlist"]) ? $proto["m_fieldlist"] : null;670 $this->m_fromlist = isset($proto["m_fromlist"]) ? $proto["m_fromlist"] : null;671 $this->m_groupby = isset($proto["m_groupby"]) ? $proto["m_groupby"] : null;672 $this->m_orderby = isset($proto["m_orderby"]) ? $proto["m_orderby"] : null;673 $this->cipherer = isset($proto["cipherer"]) ? $proto["cipherer"] : null;674 $this->m_srcTableName = $proto["m_srcTableName"];675 676 for($nCnt = 0; $nCnt < count($this->m_fromlist); $nCnt++)677 {678 $this->m_fromlist[$nCnt]->SetQuery($this);679 }680 $this->m_where->SetQuery($this);681 if(!is_array($this->m_fieldlist))682 return;683 for($i=0;$i<count($this->m_fieldlist);$i++)684 {685 if($this->m_fieldlist[$i]->IsAsterisk())686 {687 $this->bHasAsterisks=true;688 break;689 }690 }691 }692 693 function updateProto() 694 {695 $this->m_proto["m_strHead"] = $this->m_strHead;696 $this->m_proto["m_strFieldList"] = $this->m_strFieldList;697 $this->m_proto["m_strFrom"] = $this->m_strFrom;698 $this->m_proto["m_strWhere"] = $this->m_strWhere;699 $this->m_proto["m_strOrderBy"] = $this->m_strOrderBy;700 $this->m_proto["m_where"] = $this->m_where;701 $this->m_proto["m_having"] = $this->m_having;702 $this->m_proto["m_fieldlist"] = $this->m_fieldlist;703 $this->m_proto["m_fromlist"] = $this->m_fromlist;704 $this->m_proto["m_groupby"] = $this->m_groupby;705 $this->m_proto["m_orderby"] = $this->m_orderby;706 $this->m_proto["cipherer"] = $this->cipherer;707 $this->m_proto["m_srcTableName"] = $this->m_srcTableName;708 }709 710 function CloneObject()711 {712 return new SQLQuery( $this->m_proto );713 }714 715 function FromToSql()716 {717 if( is_null($this->connection) )718 $this->setConnection( $this->m_srcTableName ); 719 720 $sql = "";721 if(count($this->m_fromlist) > 0)722 {723 $sql .= "\r\n";724 $sql .= ' FROM ';725 }726 727 if( $this->connection->dbType == nDATABASE_Access )728 {729 $sqlFromList = '';730 for($nCnt = 0; $nCnt < count($this->m_fromlist); $nCnt ++)731 {732 if($sqlFromList !== '')733 {734 $sqlFromList = '(' . $sqlFromList . ')';735 }736 $sqlFromList .= $this->m_fromlist[$nCnt]->toSql($this, $nCnt == 0);737 }738 $sql .= $sqlFromList;739 }740 else741 { 742 $fromlist = array();743 for($nCnt = 0; $nCnt < count($this->m_fromlist); $nCnt ++)744 {745 $fromlist []= $this->m_fromlist[$nCnt]->toSql($this, $nCnt == 0);746 }747 $sql .= implode("\r\n", $fromlist);748 }749 750 return $sql;751 }752 753 function HavingToSql()754 {755 return $this->m_having->toSql($this);756 }757 758 function OrderByToSql()759 {760 return $this->m_strOrderBy;761 }762 763 function HeadToSql($oneRecordMode = false)764 {765 if( is_null($this->connection) )766 $this->setConnection( $this->m_srcTableName ); 767 768 $sql = '';769 $sql .= $this->m_strHead;770 771 if( $this->connection->dbType == nDATABASE_MSSQLServer || $this->connection->dbType == nDATABASE_Access )772 {773 if($oneRecordMode)774 $sql .= " top 1 ";775 }776 777 // do not add spaces to empty sql string 778 if($sql != '')779 {780 $sql .= "\r\n";781 }782 783 // collect fields784 $fields = array();785 foreach($this->m_fieldlist as $f)786 {787 $fields []= $f->toSql($this);788 }789 790 // allow derived classes to drop out fields from field list791 if(count($fields) > 0)792 {793 $sql .= implode(', ', $fields);794 }795 796 return $sql;797 }798 799 /**800 * Add custom expression to the fields list801 * @param String expression802 * @param ProjectSettings pSet803 * @param String mainTable804 * @param String mainFiled805 * @param String alias806 */807 function AddCustomExpression($expression, $pSet, $mainTable, $mainFiled, $alias = "")808 {809 $index = count($this->m_fieldlist);810 $itemFound = false; 811 foreach($this->m_fieldlist as $key => $listItem)812 {813 if( $listItem->m_expr == $expression && $listItem->m_alias == $alias )814 {815 $index = $key;816 $itemFound = true;817 break;818 }819 }820 821 if( !$itemFound ) 822 $this->m_fieldlist[] = new SQLFieldListItem(array("m_expr" => $expression, "m_alias" => $alias, "m_srcTableName" => $this->m_srcTableName));823 824 $pSet->addCustomExpressionIndex($mainTable, $mainFiled, $index);825 }826 827 function GroupByToSql()828 {829 $sql = '';830 $groupby = array();831 foreach($this->m_groupby as $g)832 {833 $groupby []= $g->toSql($this);834 }835 if(count($groupby) > 0)836 {837 $sql .= ' GROUP BY ';838 $sql .= implode(',', $groupby);839 $sql .= " ";840 }841 return $sql;842 }843 844 function GroupByHavingToSql()845 {846 $sql = "";847 $sqlGroup = $this->GroupByToSql();848 $sqlHaving = $this->HavingToSql();849 if ( strlen($sqlGroup) )850 $sql .= $sqlGroup;851 if ( strlen($sqlHaving) )852 $sql .= " HAVING ". $sqlHaving;853 854 return $sql;855 }856 857 function toSql($strwhere = null, $strorderby = null, $strhaving = null, $oneRecordMode = false, $joinFromPart = null)858 {859 if( is_null($this->connection) )860 $this->setConnection( $this->m_srcTableName ); 861 862 if(is_a($strwhere, 'SQLQuery'))863 {864 // Parent SQL query passed. Ignore.865 $strwhere = null;866 }867 868 $sql = $this->HeadToSql($oneRecordMode);869 870 $sql .= $this->FromToSql();871 872 $sql .= $joinFromPart;873 874 if($strwhere != null)875 {876 if($strwhere !== '')877 {878 $sql .= ' WHERE ' . $strwhere . "\r\n";879 }880 }881 else882 {883 $where = $this->m_where->toSql($this);884 if($where != "")885 {886 $sql .= ' WHERE ' . $where . "\r\n";887 }888 }889 890 $sql .= $this->GroupByToSql();891 892 if($strhaving != null)893 {894 if($strhaving !== '')895 {896 $sql .= ' HAVING ' . $strhaving . "\r\n";897 }898 }899 else900 {901 $having = $this->m_having->toSql($this);902 if($having != "")903 {904 $sql .= ' HAVING ' . $having . "\r\n";905 }906 }907 908 if($strorderby !== null)909 {910 $sql .= $strorderby . "\r\n";911 }912 else913 {914 $orderby = array();915 foreach($this->m_orderby as $g)916 {917 $orderby []= $g->toSql($this);918 }919 if(count($orderby) > 0)920 {921 $sql .= ' ORDER BY ';922 $sql .= implode(',', $orderby);923 $sql .= "\r\n";924 }925 }926 927 if( $this->connection->dbType == nDATABASE_MySQL )928 { 929 if($oneRecordMode)930 $sql.=" limit 0,1";931 }932 933 if( $this->connection->dbType == nDATABASE_PostgreSQL )934 { 935 if($oneRecordMode)936 $sql.=" limit 1";937 }938 939 if( $this->connection->dbType == nDATABASE_DB2 )940 { 941 if($oneRecordMode)942 $sql.=" fetch first 1 rows only";943 }944 945 return $sql;946 }947 948 function TableCount()949 {950 return count($this->m_fromlist);951 }952 953 /**954 * checks if field list item is an aggregation funciton call955 */956 function IsAggrFuncField($idx)957 {958 if($this->HasAsterisks())959 return false;960 if(!isset($this->m_fieldlist[$idx]))961 return false;962 return $this->m_fieldlist[$idx]->IsAggregationFunctionCall();963 }964 965 /**966 * @param Array fieldindices967 */968 function ReplaceFieldsWithDummies( $fieldindices )969 {970 if($this->HasAsterisks())971 return;972 973 foreach($fieldindices as $idx)974 {975 if(!isset($this->m_fieldlist[$idx - 1]))976 return;977 978 $this->m_fieldlist[$idx - 1] = new SQLFieldListItem(array(979 "m_alias" => "runnerdummy" . $idx,980 "m_expr" => "1",981 "m_srcTableName" => $this->m_srcTableName ));982 }983 }984 985 function RemoveAllFieldsExcept($idx)986 {987 if($this->HasAsterisks())988 return;989 $removeindices=array();990 for($i=0;$i<count($this->m_fieldlist);$i++)991 {992 if($i!=$idx-1)993 $removeindices[]=$i+1;994 }995 $this->ReplaceFieldsWithDummies($removeindices);996 } 997 998 function RemoveAllFieldsExceptList( $arr )999 {1000 if($this->HasAsterisks())1001 return;1002 $removeindices=array();1003 for($i=0;$i<count($this->m_fieldlist);$i++)1004 {1005 if( array_search( $i + 1, $arr ) === false )1006 $removeindices[] = $i + 1;1007 }1008 $this->ReplaceFieldsWithDummies($removeindices);1009 } 1010 1011 function WhereToSql()1012 {1013 return $this->m_where->toSql($this);1014 }1015 1016 function & Where()1017 {1018 return $this->m_where;1019 }1020 1021 function & Having()1022 {1023 return $this->m_having;1024 }1025 1026 function Copy()1027 {1028 return unserialize(serialize($this));1029 }1030 1031 function HasGroupBy()1032 {1033 return count($this->m_groupby) > 0;1034 }1035 1036 function HasSubQueryInFromClause()1037 {1038 foreach($this->m_fromlist as $fromList)1039 {1040 if( is_object( $fromList->m_table ) && is_a($fromList->m_table, 'SQLQuery') ) 1041 return true;1042 }1043 return false;1044 }1045 1046 function HasJoinInFromClause()1047 {1048 return count( $this->m_fromlist ) > 1;1049 }1050 1051 function HasTableInFormClause($tName)1052 {1053 foreach($this->m_fromlist as $fromList)1054 {1055 if( $tName == $fromList->getIdentifier() )1056 return true;1057 }1058 return false;1059 }1060 1061 function HasAsterisks()1062 {1063 return $this->bHasAsterisks;1064 }1065 1066 function addWhere($_where)1067 {1068 if(trim($_where) == "")1069 {1070 return; 1071 }1072 1073 $myproto=array();1074 $myproto["m_sql"] = $_where;1075 $myproto["m_uniontype"] = "SQLL_UNKNOWN";1076 1077 $myproto["m_column"]=null;1078 $myproto["m_contained"] = array();1079 $myproto["m_strCase"] = "";1080 $myproto["m_havingmode"] = false;1081 $myproto["m_inBrackets"] = true;1082 $myproto["m_useAlias"] = false;1083 1084 $myobj = new SQLLogicalExpr($myproto);1085 $myobj->query = $this;1086 1087 if(!$this->m_where)1088 {1089 $this->m_where = $myobj;1090 return;1091 }1092 1093 $newproto=array();1094 $newproto["m_uniontype"] = "SQLL_AND";1095 $newproto["m_contained"] = array();1096 $newproto["m_contained"][] = $this->m_where;1097 $newproto["m_contained"][] = $myobj;1098 $newobj = new SQLLogicalExpr($newproto);1099 $newobj->query = $this;1100 $this->m_where = $newobj;1101 1102 }1103 1104 function replaceWhere($_where)1105 { 1106 if(trim($_where) == "")1107 {1108 $myproto = array();1109 $myobj = new SQLLogicalExpr($myproto);1110 $myobj->query = $this;1111 1112 $this->m_where = $myobj;1113 1114 return;1115 }1116 $myproto = array();1117 $myproto["m_sql"] = $_where;1118 $myproto["m_uniontype"] = "SQLL_UNKNOWN";1119 1120 $myproto["m_column"]=null;1121 $myproto["m_contained"] = array();1122 $myproto["m_strCase"] = "";1123 $myproto["m_havingmode"] = false;1124 $myproto["m_inBrackets"] = true;1125 $myproto["m_useAlias"] = false;1126 1127 $myobj = new SQLLogicalExpr($myproto);1128 $myobj->query = $this;1129 1130 $this->m_where = $myobj;1131 }1132 1133 function addField($_field, $_alias)1134 {1135 $myproto=array();1136 $myobj = new SQLNonParsed(array(1137 "m_sql" => $_field1138 ));1139 $myproto["m_expr"]=$myobj;1140 $myproto["m_alias"]=$_alias;1141 $myproto["m_srcTableName"] = $this->m_srcTableName;1142 1143 $myobj = new SQLFieldListItem($myproto);1144 $this->m_fieldlist[] = $myobj;1145 $this->updateProto();1146 }1147 1148 function replaceField($_field, $_newfield, $_newalias = "")1149 {1150 $myproto=array();1151 $myobj = new SQLNonParsed(array(1152 "m_sql" => $_newfield1153 ));1154 $myproto["m_expr"]=$myobj;1155 1156 if(!is_numeric($_field))1157 {1158 foreach($this->m_fieldlist as $key=>$obj)1159 {1160 if($this->m_fieldlist[$key]->getAlias() == $_field)1161 {1162 $_field = $key;1163 break;1164 }1165 }1166 }1167 if(is_numeric($_field))1168 {1169 if(!$_newalias)1170 $_newalias = $this->m_fieldlist[$_field]->getAlias();1171 $myproto["m_alias"]=$_newalias;1172 $myproto["m_srcTableName"] = $this->m_srcTableName;1173 1174 $myobj = new SQLFieldListItem($myproto);1175 $this->m_fieldlist[$_field] = $myobj;1176 }1177 $this->updateProto();1178 }1179 1180 function deleteField($_field)1181 {1182 if(!is_numeric($_field))1183 {1184 foreach($this->m_fieldlist as $key=>$obj)1185 {1186 if($this->m_fieldlist[$key]->getAlias() == $_field)1187 {1188 $_field = $key;1189 break;1190 }1191 }1192 }1193 if(is_numeric($_field))1194 {1195 $fieldlist = $this->m_fieldlist;1196 array_splice($fieldlist, $_field,1);1197 $this->m_fieldlist = $fieldlist;1198 }1199 $this->updateProto();1200 }