From 9043466103365009afd0cc52c85576f0a88cda1f Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 28 Apr 2013 18:12:38 +0200 Subject: [PATCH 1/2] MDL-39389 add databasemeta cache to ms/oci drivers --- lib/dml/mssql_native_moodle_database.php | 19 ++++++++++++++----- lib/dml/oci_native_moodle_database.php | 19 ++++++++++++++----- lib/dml/sqlsrv_native_moodle_database.php | 18 +++++++++++++----- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/lib/dml/mssql_native_moodle_database.php b/lib/dml/mssql_native_moodle_database.php index 3191d5f0fd5..e1ea067c5e9 100644 --- a/lib/dml/mssql_native_moodle_database.php +++ b/lib/dml/mssql_native_moodle_database.php @@ -410,11 +410,16 @@ class mssql_native_moodle_database extends moodle_database { * @return array array of database_column_info objects indexed with column names */ public function get_columns($table, $usecache=true) { - if ($usecache and isset($this->columns[$table])) { - return $this->columns[$table]; + + if ($usecache) { + $properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash()); + $cache = cache::make('core', 'databasemeta', $properties); + if ($data = $cache->get($table)) { + return $data; + } } - $this->columns[$table] = array(); + $structure = array(); if (!$this->temptables->is_temptable($table)) { // normal table, get metadata from own schema $sql = "SELECT column_name AS name, @@ -494,11 +499,15 @@ class mssql_native_moodle_database extends moodle_database { // Process binary $info->binary = $info->meta_type == 'B' ? true : false; - $this->columns[$table][$info->name] = new database_column_info($info); + $structure[$info->name] = new database_column_info($info); } $this->free_result($result); - return $this->columns[$table]; + if ($usecache) { + $result = $cache->set($table, $structure); + } + + return $structure; } /** diff --git a/lib/dml/oci_native_moodle_database.php b/lib/dml/oci_native_moodle_database.php index f5cce9dbba0..e13be7fd9cd 100644 --- a/lib/dml/oci_native_moodle_database.php +++ b/lib/dml/oci_native_moodle_database.php @@ -469,15 +469,20 @@ class oci_native_moodle_database extends moodle_database { * @return array array of database_column_info objects indexed with column names */ public function get_columns($table, $usecache=true) { - if ($usecache and isset($this->columns[$table])) { - return $this->columns[$table]; + + if ($usecache) { + $properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash()); + $cache = cache::make('core', 'databasemeta', $properties); + if ($data = $cache->get($table)) { + return $data; + } } if (!$table) { // table not specified, return empty array directly return array(); } - $this->columns[$table] = array(); + $structure = array(); // We give precedence to CHAR_LENGTH for VARCHAR2 columns over WIDTH because the former is always // BYTE based and, for cross-db operations, we want CHAR based results. See MDL-29415 @@ -656,10 +661,14 @@ class oci_native_moodle_database extends moodle_database { $info->meta_type = '?'; } - $this->columns[$table][$info->name] = new database_column_info($info); + $structure[$info->name] = new database_column_info($info); } - return $this->columns[$table]; + if ($usecache) { + $result = $cache->set($table, $structure); + } + + return $structure; } /** diff --git a/lib/dml/sqlsrv_native_moodle_database.php b/lib/dml/sqlsrv_native_moodle_database.php index 09fa92efc37..f5992b46f26 100644 --- a/lib/dml/sqlsrv_native_moodle_database.php +++ b/lib/dml/sqlsrv_native_moodle_database.php @@ -476,11 +476,15 @@ class sqlsrv_native_moodle_database extends moodle_database { * @return array array of database_column_info objects indexed with column names */ public function get_columns($table, $usecache = true) { - if ($usecache and isset($this->columns[$table])) { - return $this->columns[$table]; + if ($usecache) { + $properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash()); + $cache = cache::make('core', 'databasemeta', $properties); + if ($data = $cache->get($table)) { + return $data; + } } - $this->columns[$table] = array (); + $structure = array(); if (!$this->temptables->is_temptable($table)) { // normal table, get metadata from own schema $sql = "SELECT column_name AS name, @@ -560,11 +564,15 @@ class sqlsrv_native_moodle_database extends moodle_database { // Process binary $info->binary = $info->meta_type == 'B' ? true : false; - $this->columns[$table][$info->name] = new database_column_info($info); + $structure[$info->name] = new database_column_info($info); } $this->free_result($result); - return $this->columns[$table]; + if ($usecache) { + $result = $cache->set($table, $structure); + } + + return $structure; } /** From f33d4784fec1b1779f1028740fff423639c0f725 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 28 Apr 2013 18:14:17 +0200 Subject: [PATCH 2/2] MDL-39389 add databasemeta cache to pdo_sqlite Done in a separate commit, coz this driver is not truly supported, but considering it as a reference for people developing drivers... it's better to have it supporting the databasemeta cache. --- lib/dml/sqlite3_pdo_moodle_database.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/dml/sqlite3_pdo_moodle_database.php b/lib/dml/sqlite3_pdo_moodle_database.php index e7410a5d50d..7391954340d 100644 --- a/lib/dml/sqlite3_pdo_moodle_database.php +++ b/lib/dml/sqlite3_pdo_moodle_database.php @@ -197,9 +197,17 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database { * @return array array of database_column_info objects indexed with column names */ public function get_columns($table, $usecache=true) { - if ($usecache and isset($this->columns[$table])) { - return $this->columns[$table]; + + if ($usecache) { + $properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash()); + $cache = cache::make('core', 'databasemeta', $properties); + if ($data = $cache->get($table)) { + return $data; + } } + + $structure = array(); + // get table's CREATE TABLE command (we'll need it for autoincrement fields) $sql = 'SELECT sql FROM sqlite_master WHERE type="table" AND tbl_name="'.$this->prefix.$table.'"'; if ($this->debug) { @@ -211,7 +219,6 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database { } $createsql = $createsql['sql']; - $columns = array(); $sql = 'PRAGMA table_info("'. $this->prefix.$table.'")'; if ($this->debug) { $this->debug_query($sql); @@ -287,11 +294,14 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database { // trim extra quotes from text default values $columninfo['default_value'] = substr($columninfo['default_value'], 1, -1); } - $columns[$columninfo['name']] = new database_column_info($columninfo); + $structure[$columninfo['name']] = new database_column_info($columninfo); } - $this->columns[$table] = $columns; - return $columns; + if ($usecache) { + $result = $cache->set($table, $structure); + } + + return $structure; } /**