summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/libraries/MY_Database.php16
-rw-r--r--core/tests/Database_Test.php33
2 files changed, 49 insertions, 0 deletions
diff --git a/core/libraries/MY_Database.php b/core/libraries/MY_Database.php
index f5e412a6..7d6c3ece 100644
--- a/core/libraries/MY_Database.php
+++ b/core/libraries/MY_Database.php
@@ -42,4 +42,20 @@ class Database extends Database_Core {
throw new Kohana_Database_Exception('database.missing_open_paren');
}
+
+ /**
+ * Parse the query string and convert any strings of the form `\([a-zA-Z0-9_]*?)\]
+ * table prefix . $1
+ */
+ public function query($sql = '') {
+ if (!empty($sql)) {
+ $sql = $this->add_table_prefixes($sql);
+ }
+ return parent::query($sql);
+ }
+
+ public function add_table_prefixes($sql) {
+ $prefix = $this->config["table_prefix"];
+ return preg_replace("#\[([a-zA-Z0-9_]+)\]#", "{$prefix}$1", $sql);
+ }
} \ No newline at end of file
diff --git a/core/tests/Database_Test.php b/core/tests/Database_Test.php
index 0d4351c9..e9ec5ebb 100644
--- a/core/tests/Database_Test.php
+++ b/core/tests/Database_Test.php
@@ -83,4 +83,37 @@ class Database_Test extends Unit_Test_Case {
"SELECT * WHERE `outer1` = 1 OR ( `inner1` NOT LIKE '%1%')",
$sql);
}
+
+ function prefix_replacement_test() {
+ $db = Database_For_Test::instance();
+ $sql = "UPDATE `[access_caches]` SET `edit_1` = 1 " .
+ "WHERE `item_id` IN " .
+ " (SELECT `id` FROM `[items]` " .
+ " WHERE `left` >= 1 " .
+ " AND `right` <= 6)";
+ $sql = $db->add_table_prefixes($sql);
+
+ $expected = "UPDATE `g3test_access_caches` SET `edit_1` = 1 " .
+ "WHERE `item_id` IN " .
+ " (SELECT `id` FROM `g3test_items` " .
+ " WHERE `left` >= 1 " .
+ " AND `right` <= 6)";
+
+ $this->assert_same($expected, $sql);
+ }
+
+ public function setup() {
+ }
+
+ public function teardown() {
+ }
+
+}
+
+class Database_For_Test extends Database {
+ static function instance() {
+ $db = new Database_For_Test();
+ $db->config["table_prefix"] = "g3test_";
+ return $db;
+ }
}