aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/DbView.class.php
blob: 45d580e80bac5a64f2c43012fafc5c592ddde0f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO
// +---------------------------------------------------------------------------+
// This file is part of Stud.IP
// DbView.class.php
// Class to provide simple Views and Prepared Statements
// Mainly for MySql, may work with other DBs (not tested)
// Copyright (c) 2002 André Noack <andre.noack@gmx.net>
// +---------------------------------------------------------------------------+
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
// +---------------------------------------------------------------------------+
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// +---------------------------------------------------------------------------+


/**
 * Class to provide simple Views and Prepared Statements
 *
 * Only tested with MySql, needs MySql >= 3.23
 * Uses DB abstraction layer of PHPLib
 *
 * @access   public
 * @author   André Noack <andre.noack@gmx.net>
 * @package  DBTools
 */
class DbView
{
    /**
     * the processed list of queries
     *
     *
     * @access   private
     * @var      array $query_list
     */
    private $query_list = [];
    /**
     * list of parameters
     *
     *
     * @access   public
     * @var      array $params
     */
    public $params = [];

    /**
     * Database Object
     *
     *
     * @access   private
     * @var      object $db
     */
    private $db;
    /**
     * Database Object Type
     *
     * Use your subclass of db_mysql here, or pass existing object to constuctor
     *
     * @access   private
     * @var      string $db_class_name
     * @see      DbView()
     */
    private $db_class_name = "DB_Seminar";
    /**
     * Temp Table Type
     *
     * MyISAM is always safe, HEAP may provide better performance
     *
     * @access   private
     * @var      string $temp_table_type
     */
    private $temp_table_type = "MyISAM";
    /**
     * Primary Key used in Temp Table
     *
     * If none is set in your view, an auto_increment row is used
     *
     * @access   private
     * @var      string $pk
     * @see      get_temp_table()
     */
    private $pk = "";
    /**
     * delete the params array after each query execution
     *
     *
     * @access   public
     * @var      boolean $auto_free_params
     */
    public $auto_free_params = true;
    /**
     * turn on/off debugging
     *
     *
     * @access   public
     * @var      boolean $debug
     */
    public $debug = false;

    static protected $dbviewfiles = [];

    static protected $dbviews = [];

    public static function addView($view)
    {
        $view = mb_strtolower($view);
        if (!isset(self::$dbviewfiles[$view])) {
            self::$dbviewfiles[$view] = 0;
        }
    }

    /**
     * Convenience method that combines addView() and returns an instance.
     *
     * @param String $view Required view (at least this will be present in the
     *                     returned instance)
     * @param mixed  $db classname of db abstraction or existing db object
     *
     * @return DbView Instance of self with at least the required view loaded
     */
    public static function getView($view, $db = '')
    {
        self::addView($view);

        return new self($db);
    }

    /**
     * Constructor
     *
     * Pass nothing to use a new instance of db_class_name, the classname for a new instance, or existing instance
     *
     * @access   public
     *
     * @param    mixed $db classname of used db abstraction or existing db object
     */
    public function __construct($db = "")
    {
        if (is_object($db)) {
            $this->db = $db;
        } else if ($db != "") {
            $this->db            = new $db;
            $this->db_class_name = $db;
        } else {
            $this->db = new $this->db_class_name;
        }
        $this->init_views();
    }

    public function init_views()
    {
        foreach (self::$dbviewfiles as $view => $status) {
            if ($status === 0) {
                include 'lib/dbviews/' . $view . '.view.php';
                self::$dbviews += $_views;
                unset($_views);
                self::$dbviewfiles[$view] = 1;
            }
        }
    }

    public function __get($view)
    {
        if (isset(self::$dbviews[$view])) {
            return self::$dbviews[$view];
        } else {
            return null;
        }
    }

    /**
     * print error message and exit script
     *
     * @access   private
     *
     * @param    string $msg the message to print
     */
    public function halt($msg)
    {
        echo "<hr>$msg<hr>";
        if ($this->debug) {
            echo "<pre>";
            print_r($this);
            echo "</pre>";
        }
        die;
    }

    public function get_query()
    {
        $parsed_query = $this->get_parsed_query(func_get_args());
        $this->db->query($parsed_query);
        return $this->db;
    }

    public function get_parsed_query($query_list)
    {
        $parsed_query     = "";
        $this->query_list = [];
        (is_array($query_list)) ? $this->query_list = $query_list : $this->query_list[] = $query_list;
        if (count($this->query_list) == 1) {
            $spl = explode(":", $this->query_list[0]);
            if ($spl[0] == "view") {
                $this->query_list = $this->get_view(trim($spl[1]));
            }
        }
        $this->parse_query($this->query_list);
        if (is_array($this->query_list)) {
            $parsed_query = $this->query_list[0];
        } else {
            $parsed_query = $this->query_list;
        }

        return $parsed_query;
    }


    public function parse_query(&$query)
    {
        if (is_array($query)) {
            for ($i = (count($query) - 1); $i > 0; --$i) {
                $spl = explode(":", $query[$i]);
                if ($spl[0] == "view") {
                    $query[$i] = $this->get_view(trim($spl[1]), $spl[2]);
                }
                $query[$i]  = $this->parse_query($query[$i]);
                $repl_query = (is_array($query[$i])) ? $query[$i][0] : $query[$i];
                for ($j = 0; $j < $i; ++$j) {
                    $spl = mb_stristr($query[$j], "where");
                    if (!$spl)
                        $spl = mb_stristr($query[$j], "having");
                    if ($spl) {
                        $pos = mb_strpos($spl, "{" . $i . "}");
                        if (!$pos === false)
                            $repl_query = $this->get_temp_values($repl_query);
                    }
                    if (!$spl OR $pos === false) {
                        $pos = mb_strpos($query[$j], "{" . $i . "}");
                        if (!$pos === false)
                            $repl_query = $this->get_temp_table($repl_query);
                    }
                    $query[$j] = str_replace("{" . $i . "}", $repl_query, $query[$j]);
                }
            }
        }

        return $query;
    }


    public function get_temp_table($sub_query)
    {
        $id    = self::get_uniqid();
        $pk    = $this->pk ? "PRIMARY KEY($this->pk)" : "auto_" . $id . " INT NOT NULL AUTO_INCREMENT PRIMARY KEY";
        $query = "CREATE TEMPORARY TABLE temp_$id ($pk) ENGINE=$this->temp_table_type $sub_query";
        $this->db->query($query);

        return " temp_" . $id . " ";
    }


    public function get_temp_values($sub_query)
    {
        $this->db->query($sub_query);
        if (!$this->db->num_rows())
            $this->halt("Sub Query: <b>$sub_query</b> returns nothing!");
        else {
            while ($this->db->next_record()) {
                $result[] = $this->db->Record[0];
            }
            $value_list = $this->get_value_list($result);
        }

        return $value_list;
    }

    public static function get_uniqid()
    {
        mt_srand((double)microtime() * 1000000);

        return md5(uniqid(mt_rand(), 1));
    }

    public function get_value_list($list)
    {
        $value_list = false;
        if (count($list) == 1)
            $value_list = "'$list[0]'";
        else
            $value_list = "'" . join("','", $list) . "'";

        return $value_list;
    }

    public function get_view($name)
    {
        if (self::$dbviews[$name]["pk"])
            $this->pk = self::$dbviews[$name]["pk"];
        if (self::$dbviews[$name]["temp_table_type"])
            $this->temp_table_type = self::$dbviews[$name]["temp_table_type"];
        if (!$query_list = self::$dbviews[$name]["query"])
            $this->halt("View not found: $name");
        (is_array($query_list)) ? $query = $query_list[0] : $query = $query_list;
        $tokens = preg_split("/[\?§\&]/u", $query);
        if (count($tokens) > 1) {
            $types = [];
            $token = 0;
            foreach (preg_split('//u', $query, null, PREG_SPLIT_NO_EMPTY) as $i => $c) {
                switch ($c) {
                    case '?':
                        $types[$token++] = 1;
                        break;
                    case '§':
                        $types[$token++] = 2;
                        break;
                    case '&':
                        $types[$token++] = 3;
                        break;
                }
            }
            if (count($this->params) != count($types))
                $this->halt("Wrong parameter count in view: $name");
            $query = "";
            for ($i = 0; $i < count($this->params); ++$i) {
                $query .= $tokens[$i];
                if (is_null($this->params[$i])) {
                    $query .= 'NULL';
                } else {
                    switch ($types[$i]) {
                        case 1:
                            $query .= "'" . $this->params[$i] . "'";
                            break;
                        case 2:
                            $query .= $this->params[$i];
                            break;
                        case 3:
                            $query .= (is_array($this->params[$i])) ? "'" . join("','", $this->params[$i]) . "'" : "'" . $this->params[$i] . "'";
                            break;
                    }
                }
            }
            $query .= $tokens[$i];
            if ($this->auto_free_params)
                $this->params = [];
        }
        (is_array($query_list)) ? $query_list[0] = $query : $query_list = $query;
        return $query_list;
    }

    public function Get_union()
    {
        $queries     = func_get_args();
        $view        = new DbView();
        $union_table = $view->get_temp_table($view->get_parsed_query($queries[0]));
        if ($queries[1]) {
            for ($i = 1; $i < count($queries); ++$i) {
                $view->db->query("REPLACE INTO $union_table " . $view->get_parsed_query($queries[$i]));
            }
        }

        return $union_table;
    }
}

?>