create DB::queryFullColumns() which will supply full column names in the format table.column

This commit is contained in:
Sergey Tsalkov
2013-02-22 22:11:27 -08:00
parent e03ac71b45
commit 027e1529ba
2 changed files with 41 additions and 4 deletions

View File

@@ -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 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 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 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 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 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); } 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 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 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 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'); } 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; $is_buffered = true;
$row_type = 'assoc'; // assoc, list, raw $row_type = 'assoc'; // assoc, list, raw
$full_names = false;
switch ($type) { switch ($type) {
case 'assoc': case 'assoc':
@@ -543,6 +546,10 @@ class MeekroDB {
case 'list': case 'list':
$row_type = 'list'; $row_type = 'list';
break; break;
case 'full':
$row_type = 'list';
$full_names = true;
break;
case 'raw_buf': case 'raw_buf':
$row_type = 'raw'; $row_type = 'raw';
break; break;
@@ -606,7 +613,15 @@ class MeekroDB {
$return = array(); $return = array();
if (!($result instanceof MySQLi_Result)) return $return; 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())) { while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) {
if ($full_names) $row = array_combine($infos, $row);
$return[] = $row; $return[] = $row;
} }

View File

@@ -10,12 +10,18 @@ class BasicTest extends SimpleTest {
function test_1_create_table() { function test_1_create_table() {
DB::query("CREATE TABLE `accounts` ( DB::query("CREATE TABLE `accounts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`profile_id` INT NOT NULL,
`username` VARCHAR( 255 ) NOT NULL , `username` VARCHAR( 255 ) NOT NULL ,
`password` VARCHAR( 255 ) NULL , `password` VARCHAR( 255 ) NULL ,
`age` INT NOT NULL DEFAULT '10', `age` INT NOT NULL DEFAULT '10',
`height` DOUBLE NOT NULL DEFAULT '10.0', `height` DOUBLE NOT NULL DEFAULT '10.0',
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi' `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
) ENGINE = InnoDB"); ) 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() { function test_1_5_empty_table() {
@@ -123,17 +129,17 @@ class BasicTest extends SimpleTest {
$this->assert(count($results) === 3); $this->assert(count($results) === 3);
$columnlist = DB::columnList('accounts'); $columnlist = DB::columnList('accounts');
$this->assert(count($columnlist) === 6); $this->assert(count($columnlist) === 7);
$this->assert($columnlist[0] === 'id'); $this->assert($columnlist[0] === 'id');
$this->assert($columnlist[4] === 'height'); $this->assert($columnlist[5] === 'height');
$tablelist = DB::tableList(); $tablelist = DB::tableList();
$this->assert(count($tablelist) === 1); $this->assert(count($tablelist) === 2);
$this->assert($tablelist[0] === 'accounts'); $this->assert($tablelist[0] === 'accounts');
$tablelist = null; $tablelist = null;
$tablelist = DB::tableList(DB::$dbName); $tablelist = DB::tableList(DB::$dbName);
$this->assert(count($tablelist) === 1); $this->assert(count($tablelist) === 2);
$this->assert($tablelist[0] === 'accounts'); $this->assert($tablelist[0] === 'accounts');
} }
@@ -339,6 +345,22 @@ class BasicTest extends SimpleTest {
$this->assert($result[0]['password'] === 'dookoo'); $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');
}
} }