PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/XML/db2xml/Input/Dbresult.php

https://github.com/chregu/fluxcms
PHP | 180 lines | 107 code | 30 blank | 43 comment | 26 complexity | ed6bcec20371eb9cd059fdb2e5ee7c66 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4.0 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Christian Stocker <chregu@phant.ch> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id$
  19. function addTestBefore_Dbresult ($resultset) {
  20. if (is_object($resultset)) {
  21. return is_subclass_of($resultset, 'MDB2_Result');
  22. } else {
  23. return false;
  24. }
  25. }
  26. Class XML_db2xml_Input_Dbresult {
  27. function XML_db2xml_Input_Dbresult (&$parent)
  28. {
  29. $this->parent = &$parent;
  30. }
  31. /**
  32. * Adds an additional pear::db_result resultset to $this->xmldoc
  33. *
  34. * @param Object db_result result from a DB-query
  35. * @see doSql2Xml()
  36. * @access public
  37. */
  38. function add($result)
  39. {
  40. $this->doSql2Xml($result);
  41. }
  42. /**
  43. * For adding db_result-'trees' to $this->xmldoc
  44. * @param Object db_result
  45. * @access private
  46. * @see addResult(),addSql()
  47. */
  48. function doSql2Xml($result)
  49. {
  50. if (MDB2::isError($result)) {
  51. throw new PopoonDBException($result);
  52. //new MDB2_Error($result->code,PEAR_ERROR_DIE);
  53. }
  54. // the method_exists is here, cause tableInfo is only in the cvs at the moment
  55. // BE CAREFUL: if you have fields with the same name in different tables, you will get errors
  56. // later, since MDB2_FETCHMODE_ASSOC doesn't differentiate that stuff.
  57. $this->LastResult = &$result;
  58. $result->db->loadModule('Reverse');
  59. if ( ! ($tableInfo = $result->db->reverse->tableInfo($result, False)))
  60. {
  61. //emulate tableInfo. this can go away, if every db supports tableInfo
  62. $fetchmode = MDB2_FETCHMODE_ASSOC;
  63. $res = $result->FetchRow($fetchmode);
  64. $this->parent->nested = False;
  65. $i = 0;
  66. foreach ($res as $key => $val)
  67. {
  68. $tableInfo[$i]['table']= $this->parent->tagNameResult;
  69. $tableInfo[$i]['name'] = $key;
  70. $resFirstRow[$i] = $val;
  71. $i++;
  72. }
  73. $res = $resFirstRow;
  74. $FirstFetchDone = True;
  75. $fetchmode = MDB2_FETCHMODE_ORDERED;
  76. }
  77. else
  78. {
  79. $FirstFetchDone = False;
  80. $fetchmode = MDB2_FETCHMODE_ORDERED;
  81. }
  82. // initialize db hierarchy...
  83. $parenttable = 'root';
  84. $tableInfo['parent_key']['root'] = 0;
  85. foreach ($tableInfo as $key => $value)
  86. {
  87. if (is_int($key))
  88. {
  89. // if the sql-query had a function the table starts with a # (only in mysql i think....), then give the field the name of the table before...
  90. if (preg_match ('/^#/',$value['table']) || strlen($value['table']) == 0) {
  91. $value['table'] = $tableInfo[($key - 1)]['table'] ;
  92. $tableInfo[$key]['table'] = $value['table'];
  93. }
  94. if (!isset($tableInfo['parent_table']) || !isset($tableInfo['parent_table'][$value['table']]) || is_null($tableInfo['parent_table'][$value['table']]))
  95. {
  96. $tableInfo['parent_key'][$value['table']] = $key;
  97. $tableInfo['parent_table'][$value['table']] = $parenttable;
  98. $parenttable = $value['table'] ;
  99. }
  100. }
  101. //if you need more tableInfo for later use you can write a function addTableInfo..
  102. $this->parent->Format->addTableInfo($key, $value, $tableInfo);
  103. }
  104. // end initialize
  105. // if user made some own tableInfo data, merge them here.
  106. if ($this->parent->user_tableInfo)
  107. {
  108. $tableInfo = $this->parent->array_merge_clobber($tableInfo,$this->parent->user_tableInfo);
  109. }
  110. $parent['root'] = $this->parent->Format->insertNewResult($tableInfo);
  111. //initialize $resold to get rid of warning messages;
  112. $resold[0] = 'ThisValueIsImpossibleForTheFirstFieldInTheFirstRow';
  113. while ($FirstFetchDone == True || $res = $result->FetchRow($fetchmode))
  114. {
  115. //FirstFetchDone is only for emulating tableInfo, as long as not all dbs support tableInfo. can go away later
  116. $FirstFetchDone = False;
  117. foreach ($res as $key => $val)
  118. {
  119. if ($resold[$tableInfo['parent_key'][$tableInfo[$key]['table']]] != $res[$tableInfo['parent_key'][$tableInfo[$key]['table']]] || !$this->parent->nested)
  120. {
  121. if ($tableInfo['parent_key'][$tableInfo[$key]['table']] == $key )
  122. {
  123. if ($this->parent->nested || $key == 0)
  124. {
  125. $parent[$tableInfo[$key]['table']] = $this->parent->Format->insertNewRow($parent[strtolower($tableInfo['parent_table'][$tableInfo[$key]['table']])], $res, $key, $tableInfo);
  126. }
  127. else
  128. {
  129. $parent[$tableInfo[$key]['table']]= $parent[strtolower($tableInfo['parent_table'][$tableInfo[$key]['table']])];
  130. }
  131. //set all children entries to somethin stupid
  132. foreach($tableInfo['parent_table'] as $pkey => $pvalue)
  133. {
  134. if ($pvalue == $tableInfo[$key]['table'])
  135. {
  136. $resold[$tableInfo['parent_key'][$pkey]]= 'ThisIsJustAPlaceHolder';
  137. }
  138. }
  139. }
  140. if ( $parent[$tableInfo[$key]['table']] !== Null)
  141. {
  142. $this->parent->Format->insertNewElement($parent[$tableInfo[$key]['table']], $res, $key, $tableInfo, $subrow);
  143. }
  144. }
  145. }
  146. $resold = $res;
  147. unset ($subrow);
  148. }
  149. }
  150. }