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/config.php | 12 ++++++++++++ installer/rcube_install.php | 32 +++++++++++++++++++++++++++++++- installer/test.php | 8 ++++---- 3 files changed, 47 insertions(+), 5 deletions(-) (limited to 'installer') diff --git a/installer/config.php b/installer/config.php index d6846edc7..b9a051ba0 100644 --- a/installer/config.php +++ b/installer/config.php @@ -301,6 +301,18 @@ echo '
'; ?> + +
db_prefix
+
+ '_db_prefix', 'size' => 20, 'id' => "cfgdbprefix")); +echo $input_prefix->show($RCI->getprop('db_prefix')); + +?> +
Optional prefix that will be added to database object names (tables and sequences).
+
+ 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 */ diff --git a/installer/test.php b/installer/test.php index 340fe267d..fb3e7e937 100644 --- a/installer/test.php +++ b/installer/test.php @@ -24,7 +24,7 @@ else if (!$read_main) { } echo '
'; -if ($read_db && !empty($RCI->config['db_table_users'])) { +if ($read_db && !empty($RCI->config['db_dsnw'])) { $RCI->pass('db.inc.php'); } else if ($read_db) { @@ -171,7 +171,7 @@ else if ($db_working && $_POST['updatedb']) { // test database if ($db_working) { - $db_read = $DB->query("SELECT count(*) FROM {$RCI->config['db_table_users']}"); + $db_read = $DB->query("SELECT count(*) FROM {$RCI->config['db_prefix']}users"); if ($DB->is_error()) { $RCI->fail('DB Schema', "Database not initialized"); echo '

'; @@ -195,11 +195,11 @@ if ($db_working) { if ($db_working) { // write test $insert_id = md5(uniqid()); - $db_write = $DB->query("INSERT INTO {$RCI->config['db_table_session']} (sess_id, created, ip, vars) VALUES (?, ".$DB->now().", '127.0.0.1', 'foo')", $insert_id); + $db_write = $DB->query("INSERT INTO {$RCI->config['db_prefix']}session (sess_id, created, ip, vars) VALUES (?, ".$DB->now().", '127.0.0.1', 'foo')", $insert_id); if ($db_write) { $RCI->pass('DB Write'); - $DB->query("DELETE FROM {$RCI->config['db_table_session']} WHERE sess_id=?", $insert_id); + $DB->query("DELETE FROM {$RCI->config['db_prefix']}session WHERE sess_id=?", $insert_id); } else { $RCI->fail('DB Write', $RCI->get_error()); -- cgit v1.2.3