xref: /plugin/struct/meta/QueryBuilderWhere.php (revision af993d55de4e0a953dd341ba1012e1447afbac9b)
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';
15*af993d55SMichael Grosse    /** @var QueryBuilder */
16*af993d55SMichael Grosse    protected $QB;
173fde0efcSAndreas Gohr
183fde0efcSAndreas Gohr    /**
193fde0efcSAndreas Gohr     * Create a new WHERE clause
203fde0efcSAndreas Gohr     *
213fde0efcSAndreas Gohr     * @param string $type The type of the statement, either 'AND' or 'OR'
223fde0efcSAndreas Gohr     * @param null|string $statement The statement or null if this should hold sub statments
233fde0efcSAndreas Gohr     */
24*af993d55SMichael Grosse    public function __construct(QueryBuilder $QB, $type = 'AND', $statement = null) {
25*af993d55SMichael Grosse        $this->QB = $QB;
263fde0efcSAndreas Gohr        $this->type = $type;
273fde0efcSAndreas Gohr        if($statement === null) {
283fde0efcSAndreas Gohr            $this->statement = array();
293fde0efcSAndreas Gohr        } else {
303fde0efcSAndreas Gohr            $this->statement = $statement;
313fde0efcSAndreas Gohr        }
323fde0efcSAndreas Gohr    }
333fde0efcSAndreas Gohr
343fde0efcSAndreas Gohr    /**
353fde0efcSAndreas Gohr     * Adds another AND clause
363fde0efcSAndreas Gohr     *
373fde0efcSAndreas Gohr     * @param string $statement
383fde0efcSAndreas Gohr     * @return $this
393fde0efcSAndreas Gohr     */
403fde0efcSAndreas Gohr    public function whereAnd($statement) {
41d21da21cSAndreas Gohr        return $this->where('AND', $statement);
423fde0efcSAndreas Gohr    }
433fde0efcSAndreas Gohr
443fde0efcSAndreas Gohr    /**
453fde0efcSAndreas Gohr     * Adds another OR clause
463fde0efcSAndreas Gohr     *
473fde0efcSAndreas Gohr     * @param $statement
483fde0efcSAndreas Gohr     * @return $this
493fde0efcSAndreas Gohr     */
503fde0efcSAndreas Gohr    public function whereOr($statement) {
51d21da21cSAndreas Gohr        return $this->where('OR', $statement);
523fde0efcSAndreas Gohr    }
533fde0efcSAndreas Gohr
543fde0efcSAndreas Gohr    /**
553fde0efcSAndreas Gohr     * Add a new AND sub clause on which more statements can be added
563fde0efcSAndreas Gohr     *
573fde0efcSAndreas Gohr     * @return QueryBuilderWhere
583fde0efcSAndreas Gohr     */
593fde0efcSAndreas Gohr    public function whereSubAnd() {
60d21da21cSAndreas Gohr        return $this->where('AND', null);
613fde0efcSAndreas Gohr    }
623fde0efcSAndreas Gohr
633fde0efcSAndreas Gohr    /**
643fde0efcSAndreas Gohr     * Add a new OR sub clause on which more statements can be added
653fde0efcSAndreas Gohr     *
663fde0efcSAndreas Gohr     * @return QueryBuilderWhere
673fde0efcSAndreas Gohr     */
683fde0efcSAndreas Gohr    public function whereSubOr() {
69d21da21cSAndreas Gohr        return $this->where('OR', null);
703fde0efcSAndreas Gohr    }
713fde0efcSAndreas Gohr
723fde0efcSAndreas Gohr    /**
733fde0efcSAndreas Gohr     * Adds another statement to this sub clause
743fde0efcSAndreas Gohr     *
754c13ff97SAndreas Gohr     * @param string $op either AND or OR
763fde0efcSAndreas Gohr     * @param null|string $statement null creates a new sub clause
773fde0efcSAndreas Gohr     * @return $this|QueryBuilderWhere
783fde0efcSAndreas Gohr     * @throws StructException when this is not a sub clause
793fde0efcSAndreas Gohr     */
804c13ff97SAndreas Gohr    public function where($op = 'AND', $statement = null) {
81d21da21cSAndreas Gohr        if(!is_array($this->statement)) {
82d21da21cSAndreas Gohr            throw new StructException('This WHERE is not a sub clause and can not have additional clauses');
83d21da21cSAndreas Gohr        }
844c13ff97SAndreas Gohr        if($op != 'AND' && $op != 'OR') {
85d21da21cSAndreas Gohr            throw new StructException('Bad logical operator');
86d21da21cSAndreas Gohr        }
87*af993d55SMichael Grosse        $where = new QueryBuilderWhere($this->QB, $op, $statement);
883fde0efcSAndreas Gohr        $this->statement[] = $where;
893fde0efcSAndreas Gohr
903fde0efcSAndreas Gohr        if($statement) {
913fde0efcSAndreas Gohr            return $this;
923fde0efcSAndreas Gohr        } else {
933fde0efcSAndreas Gohr            return $where;
943fde0efcSAndreas Gohr        }
953fde0efcSAndreas Gohr    }
963fde0efcSAndreas Gohr
973fde0efcSAndreas Gohr    /**
98*af993d55SMichael Grosse     * @return QueryBuilder
99*af993d55SMichael Grosse     */
100*af993d55SMichael Grosse    public function getQB() {
101*af993d55SMichael Grosse        return $this->QB;
102*af993d55SMichael Grosse    }
103*af993d55SMichael Grosse
104*af993d55SMichael Grosse    /**
1053fde0efcSAndreas Gohr     * @param bool $first is this the first where statement? Then the type is ignored
1063fde0efcSAndreas Gohr     * @return string
1073fde0efcSAndreas Gohr     */
1083fde0efcSAndreas Gohr    public function toSQL($first = true) {
1093fde0efcSAndreas Gohr        if(!$this->statement) return '';
1103fde0efcSAndreas Gohr
1113fde0efcSAndreas Gohr        $sql = ' ';
1123fde0efcSAndreas Gohr        if(!$first) $sql .= $this->type . ' ';
1133fde0efcSAndreas Gohr
1143fde0efcSAndreas Gohr        if(is_array($this->statement)) {
1153fde0efcSAndreas Gohr            $first = true;
1163fde0efcSAndreas Gohr            $sql .= '(';
1173fde0efcSAndreas Gohr            foreach($this->statement as $where) {
1183fde0efcSAndreas Gohr                $sql .= $where->toSQL($first);
1193fde0efcSAndreas Gohr                $first = false;
1203fde0efcSAndreas Gohr            }
1213fde0efcSAndreas Gohr            $sql .= ' )';
1223fde0efcSAndreas Gohr        } else {
1233fde0efcSAndreas Gohr            $sql .= $this->statement;
1243fde0efcSAndreas Gohr        }
1253fde0efcSAndreas Gohr
1263fde0efcSAndreas Gohr        return $sql;
1273fde0efcSAndreas Gohr    }
1283fde0efcSAndreas Gohr}
129