/table.php

https://github.com/sjorz/property-exporter · PHP · 352 lines · 222 code · 38 blank · 92 comment · 22 complexity · e34e27991faa2b7a6bb905653bf1bee6 MD5 · raw file

  1. <?php
  2. //**********************************************************************
  3. //
  4. // Table class. Represents a data table and can do simple oprations like
  5. // showing in HTML table, searching a partiucular row/fiels, etc.
  6. // A table object is created by obtaining a result set from the ODBC
  7. // connection. The result set is a snapshot; a refresh function is
  8. // available to re-read the results set.
  9. //
  10. // History:
  11. // -------
  12. // 1.0 14Aug10 GB Original
  13. //
  14. //**********************************************************************
  15. require_once "odbc-connection.php";
  16. require_once "log.php";
  17. class Table
  18. {
  19. private $tableName;
  20. private $resultSet;
  21. private $dbConnector;
  22. private $columns;
  23. private $columnHeaders;
  24. private $hiddenColumns;
  25. private $references;
  26. private $sortColumn;
  27. private $theme;
  28. private $dataSource;
  29. private $rowCount;
  30. public function __construct ($n, $c = "", $s = "")
  31. {
  32. if (isset($this))
  33. {
  34. $ds = "Rent_Staging";
  35. $user = "rent_staging";
  36. $pwd = "GreenFish123";
  37. $this->dataSource = $ds;
  38. $this->tableName = $n;
  39. if ($c == "")
  40. $this->columns = NULL;
  41. else
  42. $this->columns = $c;
  43. $this->sortColumn = $s;
  44. $this->dbConnector = new OdbcConnection ($ds, $user, $pwd);
  45. $this->columnHeaders = $c;
  46. $this->hiddenColumns = array ();
  47. $this->references = array ();
  48. $this->rowCount = 0;
  49. }
  50. }
  51. public function __destruct ()
  52. {
  53. }
  54. public function __toString ()
  55. {
  56. return 'Table: [' . $this->tableName . ']';
  57. }
  58. private function error ($msg)
  59. {
  60. Logger::logError ($msg);
  61. exit ($this->__toString() . ': ' . $msg);
  62. }
  63. //**********************************************************************
  64. //
  65. // Set column headers. If omitted, the column names will be used
  66. //
  67. //**********************************************************************
  68. public function setColumnHeaders ($h)
  69. {
  70. $this->columnHeaders = $h;
  71. }
  72. //**********************************************************************
  73. //
  74. // Set hidden column. Hidden columns won't be printed.
  75. //
  76. //**********************************************************************
  77. public function setHiddenColumns ($c)
  78. {
  79. $this->hiddenColumns = $c;
  80. }
  81. //**********************************************************************
  82. //
  83. // Set hidden column. Ref columns are shown as links to the page
  84. // defined by $url, with the value of the column as an argument.
  85. //
  86. //**********************************************************************
  87. public function setReference ($cName, $url)
  88. {
  89. $this->references [$cName] = $url;
  90. }
  91. //**********************************************************************
  92. //
  93. // Execute an SQL
  94. //
  95. //**********************************************************************
  96. public function executeSQL ($sql)
  97. {
  98. Logger::logDebug ($sql);
  99. if (!$this->resultSet = $this->dbConnector->execute ($sql))
  100. {
  101. Logger::logError ("Error in SQL");
  102. $this->error ($sql);
  103. return false;
  104. }
  105. return true;
  106. }
  107. //**********************************************************************
  108. //
  109. // Return the number of rows in the result set
  110. //
  111. //**********************************************************************
  112. public function getRowCount()
  113. {
  114. return $this->rowCount;
  115. }
  116. //**********************************************************************
  117. //
  118. // Read a snapshot of the entire table. Only read specified columns.
  119. //
  120. // NOTE: odbc_num_rows returns -1 for mnost drivers, so it's no use
  121. // for use to count the rows returned by a query.
  122. //
  123. //**********************************************************************
  124. public function readAll ()
  125. {
  126. $sql = "SELECT ";
  127. if (is_null($this->columns))
  128. {
  129. $sql .= "* ";
  130. }
  131. else
  132. {
  133. $i = 0;
  134. foreach ($this->columns as $column)
  135. {
  136. if ($i > 0)
  137. $sql.= ',';
  138. $sql .= $column;
  139. $i++;
  140. }
  141. }
  142. $sql .= ' from ' . $this->tableName;
  143. if (strlen ($this->sortColumn) > 0)
  144. $sql .= ' order by ' . $this->sortColumn;
  145. return $this->executeSQL ($sql);
  146. }
  147. public function readGroupBy ()
  148. {
  149. $sql = "SELECT ";
  150. $i = 0;
  151. foreach ($this->columns as $column)
  152. {
  153. if ($i > 0)
  154. $sql.= ',';
  155. $sql .= $column;
  156. $i++;
  157. }
  158. $sql .= ' from ' . $this->tableName;
  159. if (strlen ($this->sortColumn) > 0)
  160. $sql .= ' group by ' . $this->sortColumn;
  161. return $this->executeSQL ($sql);
  162. }
  163. //**********************************************************************
  164. //
  165. // Read a snapshot of the table with a where clause
  166. //
  167. //**********************************************************************
  168. public function read ($where)
  169. {
  170. $sql = "SELECT ";
  171. if (is_null ($this->columns))
  172. {
  173. $sql .= "* ";
  174. }
  175. else
  176. {
  177. $i = 0;
  178. foreach ($this->columns as $column)
  179. {
  180. if ($i > 0)
  181. $sql.= ',';
  182. $sql .= $column;
  183. $i++;
  184. }
  185. }
  186. $sql .= ' from ' . $this->tableName;
  187. $sql .= ' where ' . $where;
  188. if (strlen ($this->sortColumn) > 0)
  189. $sql .= ' order by ' . $this->sortColumn;
  190. Logger::logDebug ($sql);
  191. if (!$this->resultSet = $this->dbConnector->execute ($sql))
  192. {
  193. $this->error ($sql);
  194. return false;
  195. }
  196. return true;
  197. }
  198. //**********************************************************************
  199. //
  200. // Get a particular column value from an earlier query.
  201. //
  202. //**********************************************************************
  203. public function getValue ($col)
  204. {
  205. odbc_fetch_row ($this->resultSet);
  206. return odbc_result ($this->resultSet, $col);
  207. }
  208. //**********************************************************************
  209. //
  210. // Helper - Test whether column is hidden
  211. //
  212. //**********************************************************************
  213. private function hideColumn ($i)
  214. {
  215. if (count ($this->hiddenColumns) > $i)
  216. return $this->hiddenColumns [$i];
  217. return false;
  218. }
  219. //**********************************************************************
  220. //
  221. // As text
  222. //
  223. //**********************************************************************
  224. public function doPrint ()
  225. {
  226. $n = 0;
  227. while (odbc_fetch_row ($this->resultSet))
  228. {
  229. $i = 0;
  230. foreach ($this->columns as $column)
  231. {
  232. if (!$this->hideColumn ($i++))
  233. {
  234. $val = odbc_result ($this->resultSet, $column);
  235. printf ("%s=>%s", $column, $val);
  236. if ($i > 0)
  237. echo "|";
  238. }
  239. }
  240. echo "\n";
  241. $n++;
  242. }
  243. $this->rowCount = $n;
  244. }
  245. //**********************************************************************
  246. //
  247. // Return the results as an array of rows, where each row is
  248. // an associative arrey of column name and value pairs.
  249. //
  250. //**********************************************************************
  251. public function asArray ()
  252. {
  253. $res = array();
  254. $n = 0; // Only for debugging
  255. while ($r = odbc_fetch_array ($this->resultSet))
  256. {
  257. $res [] = $r;
  258. $n++;
  259. }
  260. Logger::logDebug ('Returned [' . $n . '] rows');
  261. return $res;
  262. }
  263. //**********************************************************************
  264. //
  265. // Return the results as an array (key is col name), separated by a
  266. // column delimiter, $colDelim. If more than one row is read, each row
  267. // is appended in the string, separated by the record delimiter $rowDelim
  268. //
  269. //**********************************************************************
  270. public function toValueString ($rowDelim, $colDelim)
  271. {
  272. $res = "";
  273. $nRows = 0;
  274. while (odbc_fetch_row ($this->resultSet))
  275. {
  276. if (strlen ($res) > 0)
  277. $res .= $rowDelim;
  278. for ($i = 0; $i < odbc_num_fields ($this->resultSet); $i++)
  279. {
  280. $res .= trim (odbc_result ($this->resultSet, $i + 1)) .
  281. $colDelim;
  282. }
  283. $nRows++;
  284. }
  285. Logger::logDebug ('Returned [' . $nRows . '] rows');
  286. return $res;
  287. }
  288. //**********************************************************************
  289. //
  290. // Return the results as an array of rows, where each row is and
  291. // indexed array of values.
  292. //
  293. //**********************************************************************
  294. public function asIndexedArray ()
  295. {
  296. $i = 0;
  297. while (odbc_fetch_into ($this->resultSet, $r))
  298. {
  299. $a [$i++] = $r;
  300. }
  301. Logger::logDebug ('Returned [' . $i . '] rows');
  302. return $a;
  303. }
  304. //**********************************************************************
  305. //
  306. // Call a stored procedure; delegate this to the connection class.
  307. // See connection.php for details
  308. //
  309. //**********************************************************************
  310. public function callSP ($n, $a)
  311. {
  312. return $this->dbConnector->callSP ($n, $a);
  313. }
  314. }