13fde0efcSAndreas Gohr<?php 23fde0efcSAndreas Gohr 33fde0efcSAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 43fde0efcSAndreas Gohr 53fde0efcSAndreas Gohr/** 63fde0efcSAndreas Gohr * Class QueryWhere 73fde0efcSAndreas Gohr * @package dokuwiki\plugin\struct\meta 83fde0efcSAndreas Gohr */ 93fde0efcSAndreas Gohrclass QueryBuilderWhere { 103fde0efcSAndreas Gohr 113fde0efcSAndreas Gohr /** @var QueryBuilderWhere[]|string */ 123fde0efcSAndreas Gohr protected $statement; 133fde0efcSAndreas Gohr /** @var string */ 143fde0efcSAndreas Gohr protected $type = 'AND'; 15af993d55SMichael Grosse /** @var QueryBuilder */ 16af993d55SMichael Grosse protected $QB; 173fde0efcSAndreas Gohr 183fde0efcSAndreas Gohr /** 193fde0efcSAndreas Gohr * Create a new WHERE clause 203fde0efcSAndreas Gohr * 21*95c419beSMichael Grosse * @param QueryBuilder $QB The QueryBuilder to which this where-clause belongs 223fde0efcSAndreas Gohr * @param string $type The type of the statement, either 'AND' or 'OR' 233fde0efcSAndreas Gohr * @param null|string $statement The statement or null if this should hold sub statments 243fde0efcSAndreas Gohr */ 25af993d55SMichael Grosse public function __construct(QueryBuilder $QB, $type = 'AND', $statement = null) { 26af993d55SMichael Grosse $this->QB = $QB; 273fde0efcSAndreas Gohr $this->type = $type; 283fde0efcSAndreas Gohr if($statement === null) { 293fde0efcSAndreas Gohr $this->statement = array(); 303fde0efcSAndreas Gohr } else { 313fde0efcSAndreas Gohr $this->statement = $statement; 323fde0efcSAndreas Gohr } 333fde0efcSAndreas Gohr } 343fde0efcSAndreas Gohr 353fde0efcSAndreas Gohr /** 363fde0efcSAndreas Gohr * Adds another AND clause 373fde0efcSAndreas Gohr * 383fde0efcSAndreas Gohr * @param string $statement 393fde0efcSAndreas Gohr * @return $this 403fde0efcSAndreas Gohr */ 413fde0efcSAndreas Gohr public function whereAnd($statement) { 42d21da21cSAndreas Gohr return $this->where('AND', $statement); 433fde0efcSAndreas Gohr } 443fde0efcSAndreas Gohr 453fde0efcSAndreas Gohr /** 463fde0efcSAndreas Gohr * Adds another OR clause 473fde0efcSAndreas Gohr * 483fde0efcSAndreas Gohr * @param $statement 493fde0efcSAndreas Gohr * @return $this 503fde0efcSAndreas Gohr */ 513fde0efcSAndreas Gohr public function whereOr($statement) { 52d21da21cSAndreas Gohr return $this->where('OR', $statement); 533fde0efcSAndreas Gohr } 543fde0efcSAndreas Gohr 553fde0efcSAndreas Gohr /** 563fde0efcSAndreas Gohr * Add a new AND sub clause on which more statements can be added 573fde0efcSAndreas Gohr * 583fde0efcSAndreas Gohr * @return QueryBuilderWhere 593fde0efcSAndreas Gohr */ 603fde0efcSAndreas Gohr public function whereSubAnd() { 61d21da21cSAndreas Gohr return $this->where('AND', null); 623fde0efcSAndreas Gohr } 633fde0efcSAndreas Gohr 643fde0efcSAndreas Gohr /** 653fde0efcSAndreas Gohr * Add a new OR sub clause on which more statements can be added 663fde0efcSAndreas Gohr * 673fde0efcSAndreas Gohr * @return QueryBuilderWhere 683fde0efcSAndreas Gohr */ 693fde0efcSAndreas Gohr public function whereSubOr() { 70d21da21cSAndreas Gohr return $this->where('OR', null); 713fde0efcSAndreas Gohr } 723fde0efcSAndreas Gohr 733fde0efcSAndreas Gohr /** 743fde0efcSAndreas Gohr * Adds another statement to this sub clause 753fde0efcSAndreas Gohr * 764c13ff97SAndreas Gohr * @param string $op either AND or OR 773fde0efcSAndreas Gohr * @param null|string $statement null creates a new sub clause 783fde0efcSAndreas Gohr * @return $this|QueryBuilderWhere 793fde0efcSAndreas Gohr * @throws StructException when this is not a sub clause 803fde0efcSAndreas Gohr */ 814c13ff97SAndreas Gohr public function where($op = 'AND', $statement = null) { 82d21da21cSAndreas Gohr if(!is_array($this->statement)) { 83d21da21cSAndreas Gohr throw new StructException('This WHERE is not a sub clause and can not have additional clauses'); 84d21da21cSAndreas Gohr } 854c13ff97SAndreas Gohr if($op != 'AND' && $op != 'OR') { 86d21da21cSAndreas Gohr throw new StructException('Bad logical operator'); 87d21da21cSAndreas Gohr } 88af993d55SMichael Grosse $where = new QueryBuilderWhere($this->QB, $op, $statement); 893fde0efcSAndreas Gohr $this->statement[] = $where; 903fde0efcSAndreas Gohr 913fde0efcSAndreas Gohr if($statement) { 923fde0efcSAndreas Gohr return $this; 933fde0efcSAndreas Gohr } else { 943fde0efcSAndreas Gohr return $where; 953fde0efcSAndreas Gohr } 963fde0efcSAndreas Gohr } 973fde0efcSAndreas Gohr 983fde0efcSAndreas Gohr /** 99af993d55SMichael Grosse * @return QueryBuilder 100af993d55SMichael Grosse */ 101af993d55SMichael Grosse public function getQB() { 102af993d55SMichael Grosse return $this->QB; 103af993d55SMichael Grosse } 104af993d55SMichael Grosse 105af993d55SMichael Grosse /** 1063fde0efcSAndreas Gohr * @param bool $first is this the first where statement? Then the type is ignored 1073fde0efcSAndreas Gohr * @return string 1083fde0efcSAndreas Gohr */ 1093fde0efcSAndreas Gohr public function toSQL($first = true) { 1103fde0efcSAndreas Gohr if(!$this->statement) return ''; 1113fde0efcSAndreas Gohr 1123fde0efcSAndreas Gohr $sql = ' '; 1133fde0efcSAndreas Gohr if(!$first) $sql .= $this->type . ' '; 1143fde0efcSAndreas Gohr 1153fde0efcSAndreas Gohr if(is_array($this->statement)) { 1163fde0efcSAndreas Gohr $first = true; 1173fde0efcSAndreas Gohr $sql .= '('; 1183fde0efcSAndreas Gohr foreach($this->statement as $where) { 1193fde0efcSAndreas Gohr $sql .= $where->toSQL($first); 1203fde0efcSAndreas Gohr $first = false; 1213fde0efcSAndreas Gohr } 1223fde0efcSAndreas Gohr $sql .= ' )'; 1233fde0efcSAndreas Gohr } else { 1243fde0efcSAndreas Gohr $sql .= $this->statement; 1253fde0efcSAndreas Gohr } 1263fde0efcSAndreas Gohr 1273fde0efcSAndreas Gohr return $sql; 1283fde0efcSAndreas Gohr } 1293fde0efcSAndreas Gohr} 130