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'; 153fde0efcSAndreas Gohr 163fde0efcSAndreas Gohr /** 173fde0efcSAndreas Gohr * Create a new WHERE clause 183fde0efcSAndreas Gohr * 193fde0efcSAndreas Gohr * @param string $type The type of the statement, either 'AND' or 'OR' 203fde0efcSAndreas Gohr * @param null|string $statement The statement or null if this should hold sub statments 213fde0efcSAndreas Gohr */ 223fde0efcSAndreas Gohr public function __construct($type = 'AND', $statement = null) { 233fde0efcSAndreas Gohr $this->type = $type; 243fde0efcSAndreas Gohr if($statement === null) { 253fde0efcSAndreas Gohr $this->statement = array(); 263fde0efcSAndreas Gohr } else { 273fde0efcSAndreas Gohr $this->statement = $statement; 283fde0efcSAndreas Gohr } 293fde0efcSAndreas Gohr } 303fde0efcSAndreas Gohr 313fde0efcSAndreas Gohr /** 323fde0efcSAndreas Gohr * Adds another AND clause 333fde0efcSAndreas Gohr * 343fde0efcSAndreas Gohr * @param string $statement 353fde0efcSAndreas Gohr * @return $this 363fde0efcSAndreas Gohr */ 373fde0efcSAndreas Gohr public function whereAnd($statement) { 38*d21da21cSAndreas Gohr return $this->where('AND', $statement); 393fde0efcSAndreas Gohr } 403fde0efcSAndreas Gohr 413fde0efcSAndreas Gohr /** 423fde0efcSAndreas Gohr * Adds another OR clause 433fde0efcSAndreas Gohr * 443fde0efcSAndreas Gohr * @param $statement 453fde0efcSAndreas Gohr * @return $this 463fde0efcSAndreas Gohr */ 473fde0efcSAndreas Gohr public function whereOr($statement) { 48*d21da21cSAndreas Gohr return $this->where('OR', $statement); 493fde0efcSAndreas Gohr } 503fde0efcSAndreas Gohr 513fde0efcSAndreas Gohr /** 523fde0efcSAndreas Gohr * Add a new AND sub clause on which more statements can be added 533fde0efcSAndreas Gohr * 543fde0efcSAndreas Gohr * @return QueryBuilderWhere 553fde0efcSAndreas Gohr */ 563fde0efcSAndreas Gohr public function whereSubAnd() { 57*d21da21cSAndreas Gohr return $this->where('AND', null); 583fde0efcSAndreas Gohr } 593fde0efcSAndreas Gohr 603fde0efcSAndreas Gohr /** 613fde0efcSAndreas Gohr * Add a new OR sub clause on which more statements can be added 623fde0efcSAndreas Gohr * 633fde0efcSAndreas Gohr * @return QueryBuilderWhere 643fde0efcSAndreas Gohr */ 653fde0efcSAndreas Gohr public function whereSubOr() { 66*d21da21cSAndreas Gohr return $this->where('OR', null); 673fde0efcSAndreas Gohr } 683fde0efcSAndreas Gohr 693fde0efcSAndreas Gohr /** 703fde0efcSAndreas Gohr * Adds another statement to this sub clause 713fde0efcSAndreas Gohr * 723fde0efcSAndreas Gohr * @param string $type either AND or OR 733fde0efcSAndreas Gohr * @param null|string $statement null creates a new sub clause 743fde0efcSAndreas Gohr * @return $this|QueryBuilderWhere 753fde0efcSAndreas Gohr * @throws StructException when this is not a sub clause 763fde0efcSAndreas Gohr */ 77*d21da21cSAndreas Gohr public function where($type = 'AND', $statement = null) { 78*d21da21cSAndreas Gohr if(!is_array($this->statement)) { 79*d21da21cSAndreas Gohr throw new StructException('This WHERE is not a sub clause and can not have additional clauses'); 80*d21da21cSAndreas Gohr } 81*d21da21cSAndreas Gohr if($type != 'AND' && $type != 'OR') { 82*d21da21cSAndreas Gohr throw new StructException('Bad logical operator'); 83*d21da21cSAndreas Gohr } 843fde0efcSAndreas Gohr $where = new QueryBuilderWhere($type, $statement); 853fde0efcSAndreas Gohr $this->statement[] = $where; 863fde0efcSAndreas Gohr 873fde0efcSAndreas Gohr if($statement) { 883fde0efcSAndreas Gohr return $this; 893fde0efcSAndreas Gohr } else { 903fde0efcSAndreas Gohr return $where; 913fde0efcSAndreas Gohr } 923fde0efcSAndreas Gohr } 933fde0efcSAndreas Gohr 943fde0efcSAndreas Gohr /** 953fde0efcSAndreas Gohr * @param bool $first is this the first where statement? Then the type is ignored 963fde0efcSAndreas Gohr * @return string 973fde0efcSAndreas Gohr */ 983fde0efcSAndreas Gohr public function toSQL($first = true) { 993fde0efcSAndreas Gohr if(!$this->statement) return ''; 1003fde0efcSAndreas Gohr 1013fde0efcSAndreas Gohr $sql = ' '; 1023fde0efcSAndreas Gohr if(!$first) $sql .= $this->type . ' '; 1033fde0efcSAndreas Gohr 1043fde0efcSAndreas Gohr if(is_array($this->statement)) { 1053fde0efcSAndreas Gohr $first = true; 1063fde0efcSAndreas Gohr $sql .= '('; 1073fde0efcSAndreas Gohr foreach($this->statement as $where) { 1083fde0efcSAndreas Gohr $sql .= $where->toSQL($first); 1093fde0efcSAndreas Gohr $first = false; 1103fde0efcSAndreas Gohr } 1113fde0efcSAndreas Gohr $sql .= ' )'; 1123fde0efcSAndreas Gohr } else { 1133fde0efcSAndreas Gohr $sql .= $this->statement; 1143fde0efcSAndreas Gohr } 1153fde0efcSAndreas Gohr 1163fde0efcSAndreas Gohr return $sql; 1173fde0efcSAndreas Gohr } 1183fde0efcSAndreas Gohr} 119