Permalink
Browse files

Multi-dimensional array support in selectCol()

git-svn-id: svn://dklab.ru/lib/DbSimple/trunk@201 78bb956b-1e24-0410-b8d0-c528fdc9eae3
  • Loading branch information...
1 parent 1552685 commit 3631bcb1f5a56530b30e0daccefbb5fd58475ab1 dk committed Jul 2, 2007
View
@@ -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;
}
/**
@@ -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.
@@ -1,5 +1,5 @@
--TEST--
-Generic: selectCell(); usage
+Generic: selectCell() usage
--FILE--
<?php
require_once dirname(__FILE__) . '/../init.php';
@@ -1,5 +1,5 @@
--TEST--
-Generic: selectCol(); usage
+Generic: selectCol() usage
--FILE--
<?php
require_once dirname(__FILE__) . '/../init.php';
@@ -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.