Commit 2be9dacf authored by Alexander Butenko's avatar Alexander Butenko

new query option MYSQLI_NESTJOIN to join fields by joined table name

parent dedbb0b5
...@@ -120,6 +120,12 @@ class MysqliDb ...@@ -120,6 +120,12 @@ class MysqliDb
*/ */
public $returnType = 'Array'; public $returnType = 'Array';
/**
* Should join() results be nested by table
* @var boolean
*/
protected $_nestJoin = false;
private $_tableName = '';
/** /**
* Variables for query execution tracing * Variables for query execution tracing
* *
...@@ -220,6 +226,8 @@ class MysqliDb ...@@ -220,6 +226,8 @@ class MysqliDb
$this->_query = null; $this->_query = null;
$this->_queryOptions = array(); $this->_queryOptions = array();
$this->returnType = 'Array'; $this->returnType = 'Array';
$this->_nestJoin = false;
$this->_tableName = '';
} }
/** /**
...@@ -332,7 +340,7 @@ class MysqliDb ...@@ -332,7 +340,7 @@ class MysqliDb
public function setQueryOption ($options) { public function setQueryOption ($options) {
$allowedOptions = Array ('ALL','DISTINCT','DISTINCTROW','HIGH_PRIORITY','STRAIGHT_JOIN','SQL_SMALL_RESULT', $allowedOptions = Array ('ALL','DISTINCT','DISTINCTROW','HIGH_PRIORITY','STRAIGHT_JOIN','SQL_SMALL_RESULT',
'SQL_BIG_RESULT','SQL_BUFFER_RESULT','SQL_CACHE','SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS', 'SQL_BIG_RESULT','SQL_BUFFER_RESULT','SQL_CACHE','SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS',
'LOW_PRIORITY','IGNORE','QUICK'); 'LOW_PRIORITY','IGNORE','QUICK', 'MYSQLI_NESTJOIN');
if (!is_array ($options)) if (!is_array ($options))
$options = Array ($options); $options = Array ($options);
...@@ -341,7 +349,10 @@ class MysqliDb ...@@ -341,7 +349,10 @@ class MysqliDb
if (!in_array ($option, $allowedOptions)) if (!in_array ($option, $allowedOptions))
die ('Wrong query option: '.$option); die ('Wrong query option: '.$option);
$this->_queryOptions[] = $option; if ($option == 'MYSQLI_NESTJOIN')
$this->_nestJoin = true;
else
$this->_queryOptions[] = $option;
} }
return $this; return $this;
...@@ -372,8 +383,9 @@ class MysqliDb ...@@ -372,8 +383,9 @@ class MysqliDb
$columns = '*'; $columns = '*';
$column = is_array($columns) ? implode(', ', $columns) : $columns; $column = is_array($columns) ? implode(', ', $columns) : $columns;
$this->_tableName = self::$prefix . $tableName;
$this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' . $this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' .
$column . " FROM " .self::$prefix . $tableName; $column . " FROM " . $this->_tableName;
$stmt = $this->_buildQuery($numRows); $stmt = $this->_buildQuery($numRows);
if ($this->isSubQuery) if ($this->isSubQuery)
...@@ -827,8 +839,14 @@ class MysqliDb ...@@ -827,8 +839,14 @@ class MysqliDb
if ($field->type == $mysqlLongType) if ($field->type == $mysqlLongType)
$shouldStoreResult = true; $shouldStoreResult = true;
$row[$field->name] = null; if ($this->_nestJoin && $field->table != $this->_tableName) {
$parameters[] = & $row[$field->name]; $field->table = substr ($field->table, strlen (self::$prefix));
$row[$field->table][$field->name] = null;
$parameters[] = & $row[$field->table][$field->name];
} else {
$row[$field->name] = null;
$parameters[] = & $row[$field->name];
}
} }
// avoid out of memory bug in php 5.2 and 5.3. Mysqli allocates lot of memory for long* // avoid out of memory bug in php 5.2 and 5.3. Mysqli allocates lot of memory for long*
...@@ -844,8 +862,14 @@ class MysqliDb ...@@ -844,8 +862,14 @@ class MysqliDb
while ($stmt->fetch()) { while ($stmt->fetch()) {
if ($this->returnType == 'Object') { if ($this->returnType == 'Object') {
$x = new stdClass (); $x = new stdClass ();
foreach ($row as $key => $val) foreach ($row as $key => $val) {
$x->$key = $val; if (is_array ($val)) {
$x->$key = new stdClass ();
foreach ($val as $k => $v)
$x->$key->$k = $v;
} else
$x->$key = $val;
}
} else { } else {
$x = array(); $x = array();
foreach ($row as $key => $val) foreach ($row as $key => $val)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment