xref: /plugin/struct/meta/QueryBuilderWhere.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
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 */
9*d6d97f60SAnna Dabrowskaclass QueryBuilderWhere
10*d6d97f60SAnna Dabrowska{
113fde0efcSAndreas Gohr
123fde0efcSAndreas Gohr    /** @var  QueryBuilderWhere[]|string */
133fde0efcSAndreas Gohr    protected $statement;
143fde0efcSAndreas Gohr    /** @var string */
153fde0efcSAndreas Gohr    protected $type = 'AND';
16af993d55SMichael Grosse    /** @var QueryBuilder */
17af993d55SMichael Grosse    protected $QB;
183fde0efcSAndreas Gohr
193fde0efcSAndreas Gohr    /**
203fde0efcSAndreas Gohr     * Create a new WHERE clause
213fde0efcSAndreas Gohr     *
2295c419beSMichael Grosse     * @param QueryBuilder $QB The QueryBuilder to which this where-clause belongs
233fde0efcSAndreas Gohr     * @param string $type The type of the statement, either 'AND' or 'OR'
243fde0efcSAndreas Gohr     * @param null|string $statement The statement or null if this should hold sub statments
253fde0efcSAndreas Gohr     */
26*d6d97f60SAnna Dabrowska    public function __construct(QueryBuilder $QB, $type = 'AND', $statement = null)
27*d6d97f60SAnna Dabrowska    {
28af993d55SMichael Grosse        $this->QB = $QB;
293fde0efcSAndreas Gohr        $this->type = $type;
303fde0efcSAndreas Gohr        if ($statement === null) {
313fde0efcSAndreas Gohr            $this->statement = array();
323fde0efcSAndreas Gohr        } else {
333fde0efcSAndreas Gohr            $this->statement = $statement;
343fde0efcSAndreas Gohr        }
353fde0efcSAndreas Gohr    }
363fde0efcSAndreas Gohr
373fde0efcSAndreas Gohr    /**
383fde0efcSAndreas Gohr     * Adds another AND clause
393fde0efcSAndreas Gohr     *
403fde0efcSAndreas Gohr     * @param string $statement
413fde0efcSAndreas Gohr     * @return $this
423fde0efcSAndreas Gohr     */
43*d6d97f60SAnna Dabrowska    public function whereAnd($statement)
44*d6d97f60SAnna Dabrowska    {
45d21da21cSAndreas Gohr        return $this->where('AND', $statement);
463fde0efcSAndreas Gohr    }
473fde0efcSAndreas Gohr
483fde0efcSAndreas Gohr    /**
493fde0efcSAndreas Gohr     * Adds another OR clause
503fde0efcSAndreas Gohr     *
513fde0efcSAndreas Gohr     * @param $statement
523fde0efcSAndreas Gohr     * @return $this
533fde0efcSAndreas Gohr     */
54*d6d97f60SAnna Dabrowska    public function whereOr($statement)
55*d6d97f60SAnna Dabrowska    {
56d21da21cSAndreas Gohr        return $this->where('OR', $statement);
573fde0efcSAndreas Gohr    }
583fde0efcSAndreas Gohr
593fde0efcSAndreas Gohr    /**
603fde0efcSAndreas Gohr     * Add a new AND sub clause on which more statements can be added
613fde0efcSAndreas Gohr     *
623fde0efcSAndreas Gohr     * @return QueryBuilderWhere
633fde0efcSAndreas Gohr     */
64*d6d97f60SAnna Dabrowska    public function whereSubAnd()
65*d6d97f60SAnna Dabrowska    {
66d21da21cSAndreas Gohr        return $this->where('AND', null);
673fde0efcSAndreas Gohr    }
683fde0efcSAndreas Gohr
693fde0efcSAndreas Gohr    /**
703fde0efcSAndreas Gohr     * Add a new OR sub clause on which more statements can be added
713fde0efcSAndreas Gohr     *
723fde0efcSAndreas Gohr     * @return QueryBuilderWhere
733fde0efcSAndreas Gohr     */
74*d6d97f60SAnna Dabrowska    public function whereSubOr()
75*d6d97f60SAnna Dabrowska    {
76d21da21cSAndreas Gohr        return $this->where('OR', null);
773fde0efcSAndreas Gohr    }
783fde0efcSAndreas Gohr
793fde0efcSAndreas Gohr    /**
803fde0efcSAndreas Gohr     * Adds another statement to this sub clause
813fde0efcSAndreas Gohr     *
824c13ff97SAndreas Gohr     * @param string $op either AND or OR
833fde0efcSAndreas Gohr     * @param null|string $statement null creates a new sub clause
843fde0efcSAndreas Gohr     * @return $this|QueryBuilderWhere
853fde0efcSAndreas Gohr     * @throws StructException when this is not a sub clause
863fde0efcSAndreas Gohr     */
87*d6d97f60SAnna Dabrowska    public function where($op = 'AND', $statement = null)
88*d6d97f60SAnna Dabrowska    {
89d21da21cSAndreas Gohr        if (!is_array($this->statement)) {
90d21da21cSAndreas Gohr            throw new StructException('This WHERE is not a sub clause and can not have additional clauses');
91d21da21cSAndreas Gohr        }
924c13ff97SAndreas Gohr        if ($op != 'AND' && $op != 'OR') {
93d21da21cSAndreas Gohr            throw new StructException('Bad logical operator');
94d21da21cSAndreas Gohr        }
95af993d55SMichael Grosse        $where = new QueryBuilderWhere($this->QB, $op, $statement);
963fde0efcSAndreas Gohr        $this->statement[] = $where;
973fde0efcSAndreas Gohr
983fde0efcSAndreas Gohr        if ($statement) {
993fde0efcSAndreas Gohr            return $this;
1003fde0efcSAndreas Gohr        } else {
1013fde0efcSAndreas Gohr            return $where;
1023fde0efcSAndreas Gohr        }
1033fde0efcSAndreas Gohr    }
1043fde0efcSAndreas Gohr
1053fde0efcSAndreas Gohr    /**
106af993d55SMichael Grosse     * @return QueryBuilder
107af993d55SMichael Grosse     */
108*d6d97f60SAnna Dabrowska    public function getQB()
109*d6d97f60SAnna Dabrowska    {
110af993d55SMichael Grosse        return $this->QB;
111af993d55SMichael Grosse    }
112af993d55SMichael Grosse
113af993d55SMichael Grosse    /**
1143fde0efcSAndreas Gohr     * @param bool $first is this the first where statement? Then the type is ignored
1153fde0efcSAndreas Gohr     * @return string
1163fde0efcSAndreas Gohr     */
117*d6d97f60SAnna Dabrowska    public function toSQL($first = true)
118*d6d97f60SAnna Dabrowska    {
1193fde0efcSAndreas Gohr        if (!$this->statement) return '';
1203fde0efcSAndreas Gohr
1213fde0efcSAndreas Gohr        $sql = ' ';
1223fde0efcSAndreas Gohr        if (!$first) $sql .= $this->type . ' ';
1233fde0efcSAndreas Gohr
1243fde0efcSAndreas Gohr        if (is_array($this->statement)) {
1253fde0efcSAndreas Gohr            $first = true;
1263fde0efcSAndreas Gohr            $sql .= '(';
1273fde0efcSAndreas Gohr            foreach ($this->statement as $where) {
1283fde0efcSAndreas Gohr                $sql .= $where->toSQL($first);
1293fde0efcSAndreas Gohr                $first = false;
1303fde0efcSAndreas Gohr            }
1313fde0efcSAndreas Gohr            $sql .= ' )';
1323fde0efcSAndreas Gohr        } else {
1333fde0efcSAndreas Gohr            $sql .= $this->statement;
1343fde0efcSAndreas Gohr        }
1353fde0efcSAndreas Gohr
1363fde0efcSAndreas Gohr        return $sql;
1373fde0efcSAndreas Gohr    }
1383fde0efcSAndreas Gohr}
139