From 399db1b647e14947e97a865c09215969f56a7efe Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sat, 27 Apr 2013 18:31:40 +0200 Subject: Add db_prefix configuration option in place of db_table_*/db_sequence_* options Make possible to use db_prefix for schema initialization in Installer (#1489067) Fix updatedb.sh script so it recognizes also table prefix for external DDL files --- installer/rcube_install.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'installer/rcube_install.php') diff --git a/installer/rcube_install.php b/installer/rcube_install.php index 2e1298707..32b6a5dd7 100644 --- a/installer/rcube_install.php +++ b/installer/rcube_install.php @@ -372,7 +372,7 @@ class rcube_install $existing_tables = $DB->list_tables(); foreach ($db_schema as $table => $cols) { - $table = !empty($this->config['db_table_'.$table]) ? $this->config['db_table_'.$table] : $table; + $table = $this->config['db_prefix'] . $table; if (!in_array($table, $existing_tables)) { $errors[] = "Missing table '".$table."'"; } @@ -655,6 +655,7 @@ class rcube_install */ function exec_sql($sql, $DB) { + $sql = $this->fix_table_names($sql, $DB); $buff = ''; foreach (explode("\n", $sql) as $line) { if (preg_match('/^--/', $line) || trim($line) == '') @@ -673,6 +674,35 @@ class rcube_install } + /** + * Parse SQL file and fix table names according to db_prefix + * Note: This need to be a complete database initial file + */ + private function fix_table_names($sql, $DB) + { + if (empty($this->config['db_prefix'])) { + return $sql; + } + + // replace table names + if (preg_match_all('/CREATE TABLE (\[dbo\]\.|IF NOT EXISTS )?[`"\[\]]*([^`"\[\] \r\n]+)/i', $sql, $matches)) { + foreach ($matches[2] as $table) { + $real_table = $this->config['db_prefix'] . $table; + $sql = preg_replace("/([^a-zA-Z0-9_])$table([^a-zA-Z0-9_])/", "\\1$real_table\\2", $sql); + } + } + // replace sequence names + if ($DB->db_provider == 'postgres' && preg_match_all('/CREATE SEQUENCE (IF NOT EXISTS )?"?([^" \n\r]+)/i', $sql, $matches)) { + foreach ($matches[2] as $sequence) { + $real_sequence = $this->config['db_prefix'] . $sequence; + $sql = preg_replace("/([^a-zA-Z0-9_])$sequence([^a-zA-Z0-9_])/", "\\1$real_sequence\\2", $sql); + } + } + + return $sql; + } + + /** * Handler for Roundcube errors */ -- cgit v1.2.3