2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Generic temptables object store
20 * Provides support to databases lacking some "expected behaviour" related
21 * with some operations with temporary tables like:
23 * - databases not retrieving temp tables from information schema tables (mysql)
24 * - databases using a different name schema for temp tables (like mssql).
25 * - databases that don't collect planner stats for temp tables (like PgSQL).
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.
36 * @copyright 2009 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 defined('MOODLE_INTERNAL') ||
die();
42 class moodle_temptables
{
44 /** @var circular reference, to be able to use DB facilities here if needed */
46 /** @var prefix to be used for all the DB objects */
48 /** @var simple array of moodle, not prefixed 'tablename' => DB, final (prefixed) 'tablename' */
49 protected $temptables;
52 * Creates new moodle_temptables instance
53 * @param moodle_database $mdb An instance of moodle_database.
55 public function __construct($mdb) {
57 $this->prefix
= $mdb->get_prefix();
58 $this->temptables
= array();
62 * Add one temptable to the store
64 * Given one moodle temptable name (without prefix), add it to the store, with the
65 * key being the original moodle name and the value being the real db temptable name
68 * Override and use this *only* if the database requires modification in the table name.
70 * @param string $tablename name without prefix of the table created as temptable
72 public function add_temptable($tablename) {
73 // TODO: throw exception if exists: if ($this->is_temptable...
74 $this->temptables
[$tablename] = $tablename;
78 * Delete one temptable from the store
80 * @param string $tablename name without prefix of the dropped temptable
82 public function delete_temptable($tablename) {
83 // TODO: throw exception if not exists: if (!$this->is_temptable....
84 unset($this->temptables
[$tablename]);
88 * Returns all the tablenames (without prefix) existing in the store
90 * @return array containing all the tablenames in the store (tablename both key and value)
92 public function get_temptables() {
93 return $this->temptables
;
97 * Returns if one table, based in the information present in the store, is a temp table
99 * @param string $tablename name without prefix of the table we are asking about
100 * @return bool true if the table is a temp table (based in the store info), false if not
102 public function is_temptable($tablename) {
103 return !empty($this->temptables
[$tablename]);
107 * Given one tablename (no prefix), return the name of the corresponding temporary table,
108 * If the table isn't a "registered" temp table, returns null
110 * @param string $tablename name without prefix which corresponding temp tablename needs to know
111 * @return mixed DB name of the temp table or null if it isn't a temp table
113 public function get_correct_name($tablename) {
114 if ($this->is_temptable($tablename)) {
115 return $this->temptables
[$tablename];
121 * Analyze the data in temporary tables to force statistics collection after bulk data loads.
122 * The database class detects all temporary tables and will automatically analyze all created tables
126 public function update_stats() {
127 // By default most databases do automatic on temporary tables, PgSQL does not.
128 // As a result, update_stats call immediately return for non-interesting database types.
132 * Dispose the temptables stuff, checking for wrong situations, informing and recovering from them
134 public function dispose() {
135 // We shouldn't have any temp table registered at the end of the script.
136 // So we error_log that and, at the same time, drop all the pending temptables
137 if ($temptables = $this->get_temptables()) {
138 error_log('Potential coding error - existing temptables found when disposing database. Must be dropped!');
139 foreach ($temptables as $temptable) {
140 $this->mdb
->get_manager()->drop_table(new xmldb_table($temptable));