diff --git a/db.class.php b/db.class.php index 3081a79..5b8a7b2 100644 --- a/db.class.php +++ b/db.class.php @@ -63,6 +63,7 @@ class DB { public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); } public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); } public static function queryAllLists() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryAllLists'), $args); } + public static function queryFullColumns() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFullColumns'), $args); } public static function queryFirstList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstList'), $args); } public static function queryOneList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneList'), $args); } public static function queryFirstColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstColumn'), $args); } @@ -526,6 +527,7 @@ class MeekroDB { public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); } public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); } + public function queryFullColumns() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'full'); } public function queryRaw() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_buf'); } public function queryRawUnbuf() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_unbuf'); } @@ -536,6 +538,7 @@ class MeekroDB { $is_buffered = true; $row_type = 'assoc'; // assoc, list, raw + $full_names = false; switch ($type) { case 'assoc': @@ -543,6 +546,10 @@ class MeekroDB { case 'list': $row_type = 'list'; break; + case 'full': + $row_type = 'list'; + $full_names = true; + break; case 'raw_buf': $row_type = 'raw'; break; @@ -606,7 +613,15 @@ class MeekroDB { $return = array(); if (!($result instanceof MySQLi_Result)) return $return; + if ($full_names) { + $infos = array(); + foreach ($result->fetch_fields() as $info) { + $infos[] = $info->table . '.' . $info->name; + } + } + while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) { + if ($full_names) $row = array_combine($infos, $row); $return[] = $row; } diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index 5be8df1..61fa88a 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -10,12 +10,18 @@ class BasicTest extends SimpleTest { function test_1_create_table() { DB::query("CREATE TABLE `accounts` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , + `profile_id` INT NOT NULL, `username` VARCHAR( 255 ) NOT NULL , `password` VARCHAR( 255 ) NULL , `age` INT NOT NULL DEFAULT '10', `height` DOUBLE NOT NULL DEFAULT '10.0', `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi' ) ENGINE = InnoDB"); + + DB::query("CREATE TABLE `profile` ( + `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , + `signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting' + ) ENGINE = InnoDB"); } function test_1_5_empty_table() { @@ -123,17 +129,17 @@ class BasicTest extends SimpleTest { $this->assert(count($results) === 3); $columnlist = DB::columnList('accounts'); - $this->assert(count($columnlist) === 6); + $this->assert(count($columnlist) === 7); $this->assert($columnlist[0] === 'id'); - $this->assert($columnlist[4] === 'height'); + $this->assert($columnlist[5] === 'height'); $tablelist = DB::tableList(); - $this->assert(count($tablelist) === 1); + $this->assert(count($tablelist) === 2); $this->assert($tablelist[0] === 'accounts'); $tablelist = null; $tablelist = DB::tableList(DB::$dbName); - $this->assert(count($tablelist) === 1); + $this->assert(count($tablelist) === 2); $this->assert($tablelist[0] === 'accounts'); } @@ -339,6 +345,22 @@ class BasicTest extends SimpleTest { $this->assert($result[0]['password'] === 'dookoo'); } + function test_9_fullcolumns() { + DB::insert('profile', array( + 'id' => 1, + 'signature' => 'u_suck' + )); + DB::query("UPDATE accounts SET profile_id=1 WHERE id=2"); + + $r = DB::queryFullColumns("SELECT accounts.*, profile.* FROM accounts + INNER JOIN profile ON accounts.profile_id=profile.id"); + + $this->assert(count($r) === 1); + $this->assert($r[0]['accounts.id'] === '2'); + $this->assert($r[0]['profile.id'] === '1'); + $this->assert($r[0]['profile.signature'] === 'u_suck'); + } + }