PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/datasource_classes/RetrieveFXSQLData.class.php

http://github.com/yodarunamok/fxphp
PHP | 204 lines | 185 code | 11 blank | 8 comment | 25 complexity | 0057ed33ec174092e8d89ae7afef0c4a MD5 | raw file
  1. <?php
  2. require_once('RetrieveFXData.class.php');
  3. #### Part of FX.php #####################################################
  4. # #
  5. # License: Artistic License (included with release) #
  6. # Web Site: www.iviking.org #
  7. # #
  8. #########################################################################
  9. // Do not use this class directly -- it is designed to be appropriately extended
  10. class RetrieveFXSQLData extends RetrieveFXData {
  11. var $whereClause;
  12. var $retrieveMetadata = true;
  13. function BuildSQLSorts () {
  14. $currentOrderBy = '';
  15. if (count($this->FX->sortParams) > 0) {
  16. $counter = 0;
  17. $currentOrderBy .= ' ORDER BY ';
  18. foreach ($this->FX->sortParams as $key1 => $value1) {
  19. $field = '';
  20. $sortOrder = '';
  21. foreach ($value1 as $key2 => $value2) {
  22. $$key2 = $value2;
  23. }
  24. if ($counter > 0) {
  25. $currentOrderBy .= ', ';
  26. }
  27. $currentOrderBy .= "{$field}";
  28. if (substr_count(strtolower($sortOrder), 'desc') > 0) {
  29. $currentOrderBy .= ' DESC';
  30. }
  31. else $currentOrderBy .= ' ASC';
  32. ++$counter;
  33. }
  34. return $currentOrderBy;
  35. }
  36. return '';
  37. }
  38. function BuildSQLQuery ($action, $limitClause='') {
  39. $currentLOP = 'AND';
  40. $logicalOperators = array();
  41. $LOPCount = 0;
  42. $currentSearch = '';
  43. $currentQuery = '';
  44. $counter = 0;
  45. $this->whereClause = '';
  46. $name = '';
  47. $value = '';
  48. $op = ''; // prevent IDE complaint. (msyk, Feb 1, 2012)
  49. switch ($action) {
  50. case '-find':
  51. foreach ($this->FX->dataParams as $key1 => $value1) {
  52. foreach ($value1 as $key2 => $value2) {
  53. $$key2 = $value2;
  54. }
  55. switch ($name) {
  56. case '-lop':
  57. $LOPCount = array_push($logicalOperators, $currentLOP);
  58. $currentLOP = $value;
  59. $currentSearch .= "(";
  60. break;
  61. case '-lop_end':
  62. $currentLOP = array_pop($logicalOperators);
  63. --$LOPCount;
  64. $currentSearch .= ")";
  65. break;
  66. case '-recid':
  67. if ($counter > 0) {
  68. $currentSearch .= " {$currentLOP} ";
  69. }
  70. $currentSearch .= $this->FX->primaryKeyField . " = '" . $value . "'";
  71. ++$counter;
  72. break;
  73. case '-script':
  74. case '-script.prefind':
  75. case '-script.presort':
  76. return new FX_Error("The '-script' parameter is not currently supported for SQL.");
  77. break;
  78. default:
  79. if ($op == "") {
  80. $op = $this->FX->defaultOperator;
  81. }
  82. if ($counter > 0) {
  83. $currentSearch .= " {$currentLOP} ";
  84. }
  85. switch ($op) {
  86. case 'eq':
  87. $currentSearch .= $name . " = '" . $value . "'";
  88. break;
  89. case 'neq':
  90. $currentSearch .= $name . " != '" . $value . "'";
  91. break;
  92. case 'cn':
  93. $currentSearch .= $name . " LIKE '%" . $value . "%'";
  94. break;
  95. case 'bw':
  96. $currentSearch .= $name . " LIKE '" . $value . "%'";
  97. break;
  98. case 'ew':
  99. $currentSearch .= $name . " LIKE '%" . $value . "'";
  100. break;
  101. case 'gt':
  102. $currentSearch .= $name . " > '" . $value . "'";
  103. break;
  104. case 'gte':
  105. $currentSearch .= $name . " >= '" . $value . "'";
  106. break;
  107. case 'lt':
  108. $currentSearch .= $name . " < '" . $value . "'";
  109. break;
  110. case 'lte':
  111. $currentSearch .= $name . " <= '" . $value . "'";
  112. break;
  113. default: // default is a 'begins with' search for historical reasons (default in FM)
  114. $currentSearch .= $name . " LIKE '" . $value . "%'";
  115. break;
  116. }
  117. ++$counter;
  118. break;
  119. }
  120. }
  121. while ($LOPCount > 0) {
  122. --$LOPCount;
  123. $currentSearch .= ")";
  124. }
  125. $this->whereClause = ' WHERE ' . $currentSearch; // set the $this->whereClause variable here, to distinguish this from a "finall" request
  126. case '-findall': //
  127. if ($this->FX->selectColsSet) {
  128. $currentQuery = "SELECT {$this->FX->selectColumns} FROM {$this->FX->layout}{$this->whereClause}" . $this->BuildSQLSorts() . $limitClause;
  129. } else {
  130. $currentQuery = "SELECT * FROM {$this->FX->layout}{$this->whereClause}" . $this->BuildSQLSorts() . $limitClause;
  131. }
  132. break;
  133. case '-delete':
  134. foreach ($this->FX->dataParams as $key1 => $value1) {
  135. foreach ($value1 as $key2 => $value2) {
  136. $$key2 = $value2;
  137. }
  138. if ($name == '-recid') {
  139. $currentQuery = "DELETE FROM {$this->FX->layout} WHERE {$this->FX->primaryKeyField} = '{$value}'";
  140. }
  141. }
  142. break;
  143. case '-edit':
  144. $this->whereClause = ' WHERE 1 = 0'; // if someone wants to update all records, they need to specify such
  145. $currentQuery = "UPDATE {$this->FX->layout} SET ";
  146. foreach ($this->FX->dataParams as $key1 => $value1) {
  147. foreach ($value1 as $key2 => $value2) {
  148. $$key2 = $value2;
  149. }
  150. if ($name == '-recid') {
  151. $this->whereClause = " WHERE {$this->FX->primaryKeyField} = '{$value}'";
  152. } else {
  153. if ($counter > 0) {
  154. $currentQuery .= ", ";
  155. }
  156. $currentQuery .= "{$name} = '{$value}'";
  157. ++$counter;
  158. }
  159. }
  160. $currentQuery .= $this->whereClause;
  161. break;
  162. case '-new':
  163. $tempColList = '(';
  164. $tempValueList = '(';
  165. foreach ($this->FX->dataParams as $key1 => $value1) {
  166. $name = '';
  167. $value = '';
  168. foreach ($value1 as $key2 => $value2) {
  169. $$key2 = $value2;
  170. }
  171. if ($name == '-recid') {
  172. $currentQuery = "DELETE FROM {$this->FX->layout} WHERE {$this->FX->primaryKeyField} = '{$value}'";
  173. }
  174. if ($counter > 0) {
  175. $tempColList .= ", ";
  176. $tempValueList .= ", ";
  177. }
  178. $tempColList .= $name;
  179. $tempValueList .= "'{$value}'";
  180. ++$counter;
  181. }
  182. $tempColList .= ')';
  183. $tempValueList .= ')';
  184. $currentQuery = "INSERT INTO {$this->FX->layout} {$tempColList} VALUES {$tempValueList}";
  185. break;
  186. }
  187. return $currentQuery;
  188. }
  189. function cleanUp() {
  190. // Clean up SQL queries here
  191. }
  192. }
  193. ?>