xref: /plugin/struct/meta/QueryBuilderWhere.php (revision 3fde0efc81e69bcfc27b24ebed4b3398b3a8084e)
1*3fde0efcSAndreas Gohr<?php
2*3fde0efcSAndreas Gohr
3*3fde0efcSAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
4*3fde0efcSAndreas Gohr
5*3fde0efcSAndreas Gohr/**
6*3fde0efcSAndreas Gohr * Class QueryWhere
7*3fde0efcSAndreas Gohr * @package dokuwiki\plugin\struct\meta
8*3fde0efcSAndreas Gohr */
9*3fde0efcSAndreas Gohrclass QueryBuilderWhere {
10*3fde0efcSAndreas Gohr
11*3fde0efcSAndreas Gohr    /** @var  QueryBuilderWhere[]|string */
12*3fde0efcSAndreas Gohr    protected $statement;
13*3fde0efcSAndreas Gohr    /** @var string */
14*3fde0efcSAndreas Gohr    protected $type = 'AND';
15*3fde0efcSAndreas Gohr
16*3fde0efcSAndreas Gohr    /**
17*3fde0efcSAndreas Gohr     * Create a new WHERE clause
18*3fde0efcSAndreas Gohr     *
19*3fde0efcSAndreas Gohr     * @param string $type The type of the statement, either 'AND' or 'OR'
20*3fde0efcSAndreas Gohr     * @param null|string $statement The statement or null if this should hold sub statments
21*3fde0efcSAndreas Gohr     */
22*3fde0efcSAndreas Gohr    public function __construct($type = 'AND', $statement = null) {
23*3fde0efcSAndreas Gohr        $this->type = $type;
24*3fde0efcSAndreas Gohr        if($statement === null) {
25*3fde0efcSAndreas Gohr            $this->statement = array();
26*3fde0efcSAndreas Gohr        } else {
27*3fde0efcSAndreas Gohr            $this->statement = $statement;
28*3fde0efcSAndreas Gohr        }
29*3fde0efcSAndreas Gohr    }
30*3fde0efcSAndreas Gohr
31*3fde0efcSAndreas Gohr    /**
32*3fde0efcSAndreas Gohr     * Adds another AND clause
33*3fde0efcSAndreas Gohr     *
34*3fde0efcSAndreas Gohr     * @param string $statement
35*3fde0efcSAndreas Gohr     * @return $this
36*3fde0efcSAndreas Gohr     */
37*3fde0efcSAndreas Gohr    public function whereAnd($statement) {
38*3fde0efcSAndreas Gohr        return $this->add('AND', $statement);
39*3fde0efcSAndreas Gohr    }
40*3fde0efcSAndreas Gohr
41*3fde0efcSAndreas Gohr    /**
42*3fde0efcSAndreas Gohr     * Adds another OR clause
43*3fde0efcSAndreas Gohr     *
44*3fde0efcSAndreas Gohr     * @param $statement
45*3fde0efcSAndreas Gohr     * @return $this
46*3fde0efcSAndreas Gohr     */
47*3fde0efcSAndreas Gohr    public function whereOr($statement) {
48*3fde0efcSAndreas Gohr        return $this->add('OR', $statement);
49*3fde0efcSAndreas Gohr    }
50*3fde0efcSAndreas Gohr
51*3fde0efcSAndreas Gohr    /**
52*3fde0efcSAndreas Gohr     * Add a new AND sub clause on which more statements can be added
53*3fde0efcSAndreas Gohr     *
54*3fde0efcSAndreas Gohr     * @return QueryBuilderWhere
55*3fde0efcSAndreas Gohr     */
56*3fde0efcSAndreas Gohr    public function whereSubAnd() {
57*3fde0efcSAndreas Gohr        return $this->add('AND', null);
58*3fde0efcSAndreas Gohr    }
59*3fde0efcSAndreas Gohr
60*3fde0efcSAndreas Gohr    /**
61*3fde0efcSAndreas Gohr     * Add a new OR sub clause on which more statements can be added
62*3fde0efcSAndreas Gohr     *
63*3fde0efcSAndreas Gohr     * @return QueryBuilderWhere
64*3fde0efcSAndreas Gohr     */
65*3fde0efcSAndreas Gohr    public function whereSubOr() {
66*3fde0efcSAndreas Gohr        return $this->add('OR', null);
67*3fde0efcSAndreas Gohr    }
68*3fde0efcSAndreas Gohr
69*3fde0efcSAndreas Gohr    /**
70*3fde0efcSAndreas Gohr     * Adds another statement to this sub clause
71*3fde0efcSAndreas Gohr     *
72*3fde0efcSAndreas Gohr     * @param string $type either AND or OR
73*3fde0efcSAndreas Gohr     * @param null|string $statement null creates a new sub clause
74*3fde0efcSAndreas Gohr     * @return $this|QueryBuilderWhere
75*3fde0efcSAndreas Gohr     * @throws StructException when this is not a sub clause
76*3fde0efcSAndreas Gohr     */
77*3fde0efcSAndreas Gohr    protected function add($type = 'AND', $statement = null) {
78*3fde0efcSAndreas Gohr        if(!is_array($this->statement)) throw new StructException('This WHERE is not a sub clause and can not have additional clauses');
79*3fde0efcSAndreas Gohr        $where = new QueryBuilderWhere($type, $statement);
80*3fde0efcSAndreas Gohr        $this->statement[] = $where;
81*3fde0efcSAndreas Gohr
82*3fde0efcSAndreas Gohr        if($statement) {
83*3fde0efcSAndreas Gohr            return $this;
84*3fde0efcSAndreas Gohr        } else {
85*3fde0efcSAndreas Gohr            return $where;
86*3fde0efcSAndreas Gohr        }
87*3fde0efcSAndreas Gohr    }
88*3fde0efcSAndreas Gohr
89*3fde0efcSAndreas Gohr    /**
90*3fde0efcSAndreas Gohr     * @param bool $first is this the first where statement? Then the type is ignored
91*3fde0efcSAndreas Gohr     * @return string
92*3fde0efcSAndreas Gohr     */
93*3fde0efcSAndreas Gohr    public function toSQL($first = true) {
94*3fde0efcSAndreas Gohr        if(!$this->statement) return '';
95*3fde0efcSAndreas Gohr
96*3fde0efcSAndreas Gohr        $sql = ' ';
97*3fde0efcSAndreas Gohr        if(!$first) $sql .= $this->type . ' ';
98*3fde0efcSAndreas Gohr
99*3fde0efcSAndreas Gohr        if(is_array($this->statement)) {
100*3fde0efcSAndreas Gohr            $first = true;
101*3fde0efcSAndreas Gohr            $sql .= '(';
102*3fde0efcSAndreas Gohr            foreach($this->statement as $where) {
103*3fde0efcSAndreas Gohr                $sql .= $where->toSQL($first);
104*3fde0efcSAndreas Gohr                $first = false;
105*3fde0efcSAndreas Gohr            }
106*3fde0efcSAndreas Gohr            $sql .= ' )';
107*3fde0efcSAndreas Gohr        } else {
108*3fde0efcSAndreas Gohr            $sql .= $this->statement;
109*3fde0efcSAndreas Gohr        }
110*3fde0efcSAndreas Gohr
111*3fde0efcSAndreas Gohr        return $sql;
112*3fde0efcSAndreas Gohr    }
113*3fde0efcSAndreas Gohr}
114