Skip to content

Commit

Permalink
Multi-dimensional array support in selectCol()
Browse files Browse the repository at this point in the history
git-svn-id: svn://dklab.ru/lib/DbSimple/trunk@201 78bb956b-1e24-0410-b8d0-c528fdc9eae3
  • Loading branch information
dk committed Jul 2, 2007
1 parent 1552685 commit 3631bcb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
24 changes: 18 additions & 6 deletions lib/DbSimple/Generic.php
Expand Up @@ -272,12 +272,8 @@ function selectCol()
$total = false;
$rows = $this->_query($args, $total);
if (!is_array($rows)) return $rows;
$col = array();
foreach ($rows as $k=>$row) {
reset($row);
$col[$k] = current($row);
}
return $col;
$this->_shrinkLastArrayDimensionCallback($rows);
return $rows;
}

/**
Expand Down Expand Up @@ -1023,6 +1019,22 @@ function _transformResultToForest($rows, $idName, $pidName)
}


/**
* Replaces the last array in a multi-dimensional array $V by its first value.
* Used for selectCol(), when we need to transform (N+1)d resulting array
* to Nd array (column).
*/
function _shrinkLastArrayDimensionCallback(&$v)
{
reset($v);
if (!is_array($firstCell = current($v))) {
$v = $firstCell;
} else {
array_walk($v, array(&$this, '_shrinkLastArrayDimensionCallback'));
}
}


/**
* void _logQuery($query, $noTrace=false)
* Must be called on each query.
Expand Down
2 changes: 1 addition & 1 deletion t/DbSimple/Generic/030_select_cell.phpt
@@ -1,5 +1,5 @@
--TEST--
Generic: selectCell(); usage
Generic: selectCell() usage
--FILE--
<?php
require_once dirname(__FILE__) . '/../init.php';
Expand Down
2 changes: 1 addition & 1 deletion t/DbSimple/Generic/040_select_col.phpt
@@ -1,5 +1,5 @@
--TEST--
Generic: selectCol(); usage
Generic: selectCol() usage
--FILE--
<?php
require_once dirname(__FILE__) . '/../init.php';
Expand Down
48 changes: 48 additions & 0 deletions t/DbSimple/Generic/040_select_col_multi.phpt
@@ -0,0 +1,48 @@
--TEST--
Generic: selectCol() usage with multi-dimensional array
--FILE--
<?php
require_once dirname(__FILE__) . '/../init.php';

function main(&$DB)
{
@$DB->query("DROP TABLE test");
$DB->query("CREATE TABLE test(id1 INTEGER, id2 INTEGER, str VARCHAR(1))");
$DB->query("INSERT INTO test VALUES( 1, 10, 'a')");
$DB->query("INSERT INTO test VALUES( 2, 20, 'b')");
$DB->query("INSERT INTO test VALUES( 2, 30, 'c')");
$DB->query("INSERT INTO test VALUES( 4, 40, 'd')");
printr($DB->selectCol("SELECT id1 AS ARRAY_KEY_1, str FROM test"));
printr($DB->selectCol("SELECT id1 AS ARRAY_KEY_1, id2 AS ARRAY_KEY_2, str FROM test"));
}

?>
--EXPECT--
Query: 'DROP TABLE test'
Query: 'CREATE TABLE test(id1 INTEGER, id2 INTEGER, str VARCHAR(1))'
Query: 'INSERT INTO test VALUES( 1, 10, \'a\')'
Query: 'INSERT INTO test VALUES( 2, 20, \'b\')'
Query: 'INSERT INTO test VALUES( 2, 30, \'c\')'
Query: 'INSERT INTO test VALUES( 4, 40, \'d\')'
Query: 'SELECT id1 AS ARRAY_KEY_1, str FROM test'
array (
1 => 'a',
2 => 'c',
4 => 'd',
)
Query: 'SELECT id1 AS ARRAY_KEY_1, id2 AS ARRAY_KEY_2, str FROM test'
array (
1 =>
array (
10 => 'a',
),
2 =>
array (
20 => 'b',
30 => 'c',
),
4 =>
array (
40 => 'd',
),
)

0 comments on commit 3631bcb

Please sign in to comment.