xref: /plugin/struct/meta/QueryBuilder.php (revision 83cb959bf0a16f7bcdfa57ee63b4273225ed5f7d)
18c551fd7SAndreas Gohr<?php
28c551fd7SAndreas Gohr
38c551fd7SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
48c551fd7SAndreas Gohr
58c551fd7SAndreas Gohr/**
68c551fd7SAndreas Gohr * Class QueryBuilder
78c551fd7SAndreas Gohr * @package dokuwiki\plugin\struct\meta
88c551fd7SAndreas Gohr */
98c551fd7SAndreas Gohrclass QueryBuilder {
108c551fd7SAndreas Gohr
118c551fd7SAndreas Gohr    /** @var array placeholder -> values */
128c551fd7SAndreas Gohr    protected $values = array();
138c551fd7SAndreas Gohr    /** @var array (alias -> statement */
148c551fd7SAndreas Gohr    protected $select = array();
158c551fd7SAndreas Gohr    /** @var array (alias -> statement) */
168c551fd7SAndreas Gohr    protected $from = array();
178c551fd7SAndreas Gohr    /** @var QueryBuilderWhere */
188c551fd7SAndreas Gohr    protected $where;
19*83cb959bSAndreas Gohr    /** @var  string[] */
20*83cb959bSAndreas Gohr    protected $orderby;
21*83cb959bSAndreas Gohr    /** @var  string[] */
22*83cb959bSAndreas Gohr    protected $groupby;
238c551fd7SAndreas Gohr
248c551fd7SAndreas Gohr    /**
258c551fd7SAndreas Gohr     * QueryBuilder constructor.
268c551fd7SAndreas Gohr     */
278c551fd7SAndreas Gohr    public function __construct() {
288c551fd7SAndreas Gohr        $this->where = new QueryBuilderWhere();
298c551fd7SAndreas Gohr    }
308c551fd7SAndreas Gohr
318c551fd7SAndreas Gohr    /**
328c551fd7SAndreas Gohr     * Adds a column to select
338c551fd7SAndreas Gohr     *
348c551fd7SAndreas Gohr     * If the alias already exists, the current statement for that alias will be overwritten.
358c551fd7SAndreas Gohr     *
368c551fd7SAndreas Gohr     * @param string $tablealias The table to select from
378c551fd7SAndreas Gohr     * @param string $column The column to select
388c551fd7SAndreas Gohr     * @param string $alias Under whichname to slect the column. blank for column name
398c551fd7SAndreas Gohr     */
408c551fd7SAndreas Gohr    public function addSelectColumn($tablealias, $column, $alias = '') {
418c551fd7SAndreas Gohr        if($alias === '') $alias = $column;
428c551fd7SAndreas Gohr        if(!isset($this->from[$tablealias])) throw new StructException('Table Alias does not exist');
438c551fd7SAndreas Gohr        $this->select[$alias] = "$tablealias.$column AS $alias";
448c551fd7SAndreas Gohr    }
458c551fd7SAndreas Gohr
468c551fd7SAndreas Gohr    /**
478c551fd7SAndreas Gohr     * Add a new select statement (the column part of it)
488c551fd7SAndreas Gohr     *
498c551fd7SAndreas Gohr     * Basically the same as @see addSelectColumn but accepts any statement. This is useful to
508c551fd7SAndreas Gohr     * select things like fixed strings or more complex function calls, but the correctness will not
518c551fd7SAndreas Gohr     * be checked.
528c551fd7SAndreas Gohr     *
538c551fd7SAndreas Gohr     * If the alias already exists, the current statement for that alias will be overwritten.
548c551fd7SAndreas Gohr     *
558c551fd7SAndreas Gohr     * @param string $statement
568c551fd7SAndreas Gohr     * @param string $alias
578c551fd7SAndreas Gohr     */
588c551fd7SAndreas Gohr    public function addSelectStatement($statement, $alias) {
598c551fd7SAndreas Gohr        $this->select[$alias] = "$statement AS $alias";
608c551fd7SAndreas Gohr    }
618c551fd7SAndreas Gohr
628c551fd7SAndreas Gohr    /**
638c551fd7SAndreas Gohr     * Adds the the table to the FROM statement part
648c551fd7SAndreas Gohr     *
658c551fd7SAndreas Gohr     * @param string $table the table to add
668c551fd7SAndreas Gohr     * @param string $alias alias for the table, blank for table name
678c551fd7SAndreas Gohr     */
688c551fd7SAndreas Gohr    public function addTable($table, $alias = '') {
698c551fd7SAndreas Gohr        if($alias === '') $alias = $table;
708c551fd7SAndreas Gohr        if(isset($this->from[$alias])) throw new StructException('Table Alias exists');
718c551fd7SAndreas Gohr        $this->from[$alias] = "$table AS $alias";
728c551fd7SAndreas Gohr    }
738c551fd7SAndreas Gohr
748c551fd7SAndreas Gohr    /**
758c551fd7SAndreas Gohr     * Adds a LEFT JOIN clause to the FROM statement part, sorted at the correct spot
768c551fd7SAndreas Gohr     *
778c551fd7SAndreas Gohr     * @param string $leftalias the alias of the left table you're joining on, has to exist already
788c551fd7SAndreas Gohr     * @param string $righttable the right table to be joined
798c551fd7SAndreas Gohr     * @param string $rightalias an alias for the right table, blank for table name
808c551fd7SAndreas Gohr     * @param string $onclause the ON clause condition the join is based on
818c551fd7SAndreas Gohr     */
828c551fd7SAndreas Gohr    public function addLeftJoin($leftalias, $righttable, $rightalias, $onclause) {
838c551fd7SAndreas Gohr        if($rightalias === '') $rightalias = $righttable;
848c551fd7SAndreas Gohr        if(!isset($this->from[$leftalias])) throw new StructException('Table Alias does not exist');
858c551fd7SAndreas Gohr        if(isset($this->from[$rightalias])) throw new StructException('Table Alias already exists');
868c551fd7SAndreas Gohr
878c551fd7SAndreas Gohr        $pos = array_search($leftalias, array_keys($this->from));
888c551fd7SAndreas Gohr        $statement = "LEFT OUTER JOIN $righttable AS $rightalias ON $onclause";
898c551fd7SAndreas Gohr        $this->from = $this->array_insert($this->from, array($rightalias => $statement), $pos + 1);
908c551fd7SAndreas Gohr    }
918c551fd7SAndreas Gohr
928c551fd7SAndreas Gohr    /**
938c551fd7SAndreas Gohr     * Returns the current WHERE filters and allows to set new ones
948c551fd7SAndreas Gohr     *
958c551fd7SAndreas Gohr     * @return QueryBuilderWhere
968c551fd7SAndreas Gohr     */
978c551fd7SAndreas Gohr    public function getFilters() {
988c551fd7SAndreas Gohr        return $this->where;
998c551fd7SAndreas Gohr    }
1008c551fd7SAndreas Gohr
1018c551fd7SAndreas Gohr    /**
102*83cb959bSAndreas Gohr     * Add an ORDER BY clause
103*83cb959bSAndreas Gohr     *
104*83cb959bSAndreas Gohr     * @param string $sort a single sorting condition
105*83cb959bSAndreas Gohr     */
106*83cb959bSAndreas Gohr    public function addOrderBy($sort) {
107*83cb959bSAndreas Gohr        $this->orderby[] = $sort;
108*83cb959bSAndreas Gohr    }
109*83cb959bSAndreas Gohr
110*83cb959bSAndreas Gohr    /**
111*83cb959bSAndreas Gohr     * Add an GROUP BY clause
112*83cb959bSAndreas Gohr     *
113*83cb959bSAndreas Gohr     * @param string $group a single grouping clause
114*83cb959bSAndreas Gohr     */
115*83cb959bSAndreas Gohr    public function addGroupBy($group) {
116*83cb959bSAndreas Gohr        $this->groupby[] = $group;
117*83cb959bSAndreas Gohr    }
118*83cb959bSAndreas Gohr
119*83cb959bSAndreas Gohr    /**
1208c551fd7SAndreas Gohr     * Adds a value to the statement
1218c551fd7SAndreas Gohr     *
122df30dbf7SAndreas Gohr     * This function returns the name of the placeholder you have to use in your statement, so whenever
123df30dbf7SAndreas Gohr     * you need to use a user value in a statement, call this first, then add the statement through the
124df30dbf7SAndreas Gohr     * other functions using the returned placeholder.
1258c551fd7SAndreas Gohr     *
1268c551fd7SAndreas Gohr     * @param mixed $value
1278c551fd7SAndreas Gohr     * @return string
1288c551fd7SAndreas Gohr     */
1298c551fd7SAndreas Gohr    public function addValue($value) {
1308c551fd7SAndreas Gohr        static $count = 0;
1318c551fd7SAndreas Gohr        $count++;
1328c551fd7SAndreas Gohr
1338c551fd7SAndreas Gohr        $placeholder = ":!!val$count!!:"; // sqlite plugin does not support named parameters, yet so we have simulate it
1348c551fd7SAndreas Gohr        $this->values[$placeholder] = $value;
1358c551fd7SAndreas Gohr        return $placeholder;
1368c551fd7SAndreas Gohr    }
1378c551fd7SAndreas Gohr
1388c551fd7SAndreas Gohr    /**
1398c551fd7SAndreas Gohr     * Creates a new table alias that has not been used before
1408c551fd7SAndreas Gohr     *
1418c551fd7SAndreas Gohr     * @return string
1428c551fd7SAndreas Gohr     */
1438c551fd7SAndreas Gohr    public function generateTableAlias() {
1448c551fd7SAndreas Gohr        static $count = 0;
1458c551fd7SAndreas Gohr        $count++;
1468c551fd7SAndreas Gohr        return "T$count";
1478c551fd7SAndreas Gohr    }
1488c551fd7SAndreas Gohr
1498c551fd7SAndreas Gohr    /**
1508c551fd7SAndreas Gohr     * Returns the complete SQL statement and the values to apply
1518c551fd7SAndreas Gohr     *
152*83cb959bSAndreas Gohr     * @return array ($sql, $vals)
1538c551fd7SAndreas Gohr     */
1548c551fd7SAndreas Gohr    public function getSQL() {
1558c551fd7SAndreas Gohr        $sql =
1568c551fd7SAndreas Gohr            ' SELECT ' . join(",\n", $this->select) . "\n" .
1578c551fd7SAndreas Gohr            '   FROM ' . join(",\n", $this->from) . "\n" .
1588c551fd7SAndreas Gohr            '  WHERE ' . $this->where->toSQL() . "\n";
1598c551fd7SAndreas Gohr
160*83cb959bSAndreas Gohr        if($this->orderby) {
161*83cb959bSAndreas Gohr            $sql .=
162*83cb959bSAndreas Gohr                'ORDER BY' . join(",\n", $this->orderby) . "\n";
163*83cb959bSAndreas Gohr        }
164*83cb959bSAndreas Gohr
165*83cb959bSAndreas Gohr        if($this->groupby) {
166*83cb959bSAndreas Gohr            $sql .=
167*83cb959bSAndreas Gohr                'GROUP BY' . join(",\n", $this->groupby) . "\n";
168*83cb959bSAndreas Gohr        }
169*83cb959bSAndreas Gohr
1708c551fd7SAndreas Gohr        return $this->fixPlaceholders($sql);
1718c551fd7SAndreas Gohr    }
1728c551fd7SAndreas Gohr
1738c551fd7SAndreas Gohr    /**
1748c551fd7SAndreas Gohr     * Replaces the named placeholders with ? placeholders
1758c551fd7SAndreas Gohr     *
1768c551fd7SAndreas Gohr     * Until the sqlite plugin can use named placeholder properly
1778c551fd7SAndreas Gohr     *
1788c551fd7SAndreas Gohr     * @param string $sql
1798c551fd7SAndreas Gohr     * @return array
1808c551fd7SAndreas Gohr     */
1818c551fd7SAndreas Gohr    protected function fixPlaceholders($sql) {
1828c551fd7SAndreas Gohr        $vals = array();
1838c551fd7SAndreas Gohr
1848c551fd7SAndreas Gohr        while(preg_match('/(:!!val\d+!!:)/', $sql, $m)) {
1858c551fd7SAndreas Gohr            $pl = $m[1];
1862b5df26eSAndreas Gohr
1872b5df26eSAndreas Gohr            if(!isset($this->values[$pl])) throw new StructException('Placeholder not found');
1882b5df26eSAndreas Gohr
1892b5df26eSAndreas Gohr            $sql = preg_replace("/$pl/", '?', $sql, 1);
1908c551fd7SAndreas Gohr            $vals[] = $this->values[$pl];
1918c551fd7SAndreas Gohr        }
1928c551fd7SAndreas Gohr
1938c551fd7SAndreas Gohr        return array($sql, $vals);
1948c551fd7SAndreas Gohr    }
1958c551fd7SAndreas Gohr
1968c551fd7SAndreas Gohr    /**
1978c551fd7SAndreas Gohr     * Insert an array into another array at a given position in an associative array
1988c551fd7SAndreas Gohr     *
1998c551fd7SAndreas Gohr     * @param array $array The initial array
2008c551fd7SAndreas Gohr     * @param array $pairs The array to insert
2018c551fd7SAndreas Gohr     * @param string $key_pos The position at which to insert
2028c551fd7SAndreas Gohr     * @link https://gist.github.com/scribu/588429 simplified
2038c551fd7SAndreas Gohr     * @return array
2048c551fd7SAndreas Gohr     */
2058c551fd7SAndreas Gohr    protected function array_insert($array, $pairs, $key_pos) {
2068c551fd7SAndreas Gohr        $result = array_slice($array, 0, $key_pos);
2078c551fd7SAndreas Gohr        $result = array_merge($result, $pairs);
2088c551fd7SAndreas Gohr        $result = array_merge($result, array_slice($array, $key_pos));
2098c551fd7SAndreas Gohr        return $result;
2108c551fd7SAndreas Gohr    }
2118c551fd7SAndreas Gohr}
2128c551fd7SAndreas Gohr
213