MDL-27541 course reports - allow teachers to view individual reports
[moodle.git] / lib / dml / moodle_temptables.php
blobf425164c6bbafd78c9b355caa87512956b541853
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Generic temptables object store
21 * Provides support to databases lacking some "expected behaviour" related
22 * with some operations with temporary tables like:
24 * - databases not retrieving temp tables from information schema tables (mysql)
25 * - databases using a different name schema for temp tables (like mssql).
27 * Basically it works as a simple store of created temporary tables, providing
28 * some simple getters/setters methods. Each database can extend it for its own
29 * purposes (for example, return correct name, see the mssql implementation)
31 * The unique instance of the object by database connection is shared by the database
32 * and the sql_generator, so both are able to use its facilities, with the final goal
33 * of doing temporary tables support 100% cross-db and transparent within the DB API.
35 * Only drivers needing it will use this store. Neither moodle_database (abstract) or
36 * databases like postgres need this, because they don't lack any temp functionality.
38 * @package core
39 * @subpackage dml
40 * @copyright 2009 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 defined('MOODLE_INTERNAL') || die();
46 class moodle_temptables {
48 protected $mdb; // circular reference, to be able to use DB facilities here if needed
49 protected $prefix; // prefix to be used for all the DB objects
50 protected $temptables; // simple array of moodle, not prefixed 'tablename' => DB, final (prefixed) 'tablename'
52 /**
53 * Creates new moodle_temptables instance
54 * @param object moodle_database instance
56 public function __construct($mdb) {
57 $this->mdb = $mdb;
58 $this->prefix = $mdb->get_prefix();
59 $this->temptables = array();
62 /**
63 * Add one temptable to the store
65 * Given one moodle temptable name (without prefix), add it to the store, with the
66 * key being the original moodle name and the value being the real db temptable name
67 * already prefixed
69 * Override and use this *only* if the database requires modification in the table name.
71 * @param string $tablename name without prefix of the table created as temptable
73 public function add_temptable($tablename) {
74 // TODO: throw exception if exists: if ($this->is_temptable...
75 $this->temptables[$tablename] = $tablename;
78 /**
79 * Delete one temptable from the store
81 * @param string $tablename name without prefix of the dropped temptable
83 public function delete_temptable($tablename) {
84 // TODO: throw exception if not exists: if (!$this->is_temptable....
85 unset($this->temptables[$tablename]);
88 /**
89 * Returns all the tablenames (without prefix) existing in the store
91 * @return array containing all the tablenames in the store (tablename both key and value)
93 public function get_temptables() {
94 return array_keys($this->temptables);
97 /**
98 * Returns if one table, based in the information present in the store, is a temp table
100 * @param string $tablename name without prefix of the table we are asking about
101 * @return bool true if the table is a temp table (based in the store info), false if not
103 public function is_temptable($tablename) {
104 return !empty($this->temptables[$tablename]);
108 * Given one tablename (no prefix), return the name of the corresponding temporary table,
109 * If the table isn't a "registered" temp table, returns null
111 * @param string $tablename name without prefix which corresponding temp tablename needs to know
112 * @return mixed DB name of the temp table or null if it isn't a temp table
114 public function get_correct_name($tablename) {
115 if ($this->is_temptable($tablename)) {
116 return $this->temptables[$tablename];
118 return null;
122 * Dispose the temptables stuff, checking for wrong situations, informing and recovering from them
124 public function dispose() {
125 // We shouldn't have any temp table registered at the end of the script.
126 // So we error_log that and, at the same time, drop all the pending temptables
127 if ($temptables = $this->get_temptables()) {
128 error_log('Potential coding error - existing temptables found when disposing database. Must be dropped!');
129 foreach ($temptables as $temptable) {
130 $this->mdb->get_manager()->drop_temp_table(new xmldb_table($temptable));
133 $this->mdb = null;