PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/assicurazioniesinistri_mysql/include/Database/QueryBuilder.class.php

http://antilophpe.googlecode.com/
PHP | 308 lines | 229 code | 8 blank | 71 comment | 28 complexity | 27e35ae319604f1c4a0ebb38e691d007 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-3.0
  1. <?php
  2. /**
  3. * File QueryBuilder.class.php
  4. *
  5. * This file contains the definition of the class object
  6. * type QueryBuilder.
  7. * @author Dott. Marco Novo <mnovo@ncfsistemi.com>
  8. * @version 1.0
  9. */
  10. /**
  11. * This class defines fields and methods of QueryBuilder.
  12. * Methods of this class are usefull to
  13. * control Antilope's database query.
  14. */
  15. class QueryBuilder {
  16. public $type='mysql';
  17. public $arrayObject;
  18. /**
  19. * Constructor of the class
  20. */
  21. function __construct() {
  22. }
  23. /**
  24. * Destructor of the class
  25. */
  26. function __destruct() {
  27. }
  28. /**
  29. * Sets the value of field arrayObject
  30. * @param array $arrayObject
  31. */
  32. function setArrayObject($arrayObject){
  33. $this->arrayObject=$arrayObject;
  34. }
  35. /**
  36. * buildInsert method: build an Insert SQL Query
  37. * starting from an array representing the Object
  38. * fixed problem with foreign key ('' is NULL)
  39. * @param string $Object_name
  40. * @param string $Object_tableName
  41. * @param string $Object_foreignKey
  42. * @return $query
  43. */
  44. function buildInsert($Object_name, $Object_tableName, $Object_foreignKey=array()){
  45. $Object_match='';
  46. $Insert_set='';
  47. foreach($this->arrayObject as $key => $value) {
  48. if ($Object_name!=''){
  49. $Object_match=explode('__', $key);
  50. $Object_match=$Object_match[0];
  51. }
  52. if ($Object_match==$Object_name){
  53. $value=str_replace("\'","'",$value);
  54. $value=str_replace("'","''",$value);
  55. // If this key is foreign and value = '' sets the database value to NULL
  56. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  57. $Insert_set.= ' '.str_replace($Object_name.'__','',$key)."=NULL,";
  58. else
  59. $Insert_set.= ' '.str_replace($Object_name.'__','',$key)."='".$value."',";
  60. }
  61. }
  62. $Insert_set=rtrim($Insert_set, ',');
  63. $query="INSERT INTO $Object_tableName
  64. SET $Insert_set";
  65. return $query;
  66. }
  67. /**
  68. * buildUpdate method: build an Update SQL Query
  69. * starting from an array representing the Object
  70. * fixed problem with foreign key ('' is NULL)
  71. * @param string $Object_name
  72. * @param string $Object_tableName
  73. * @param string/array $Object_primaryKey
  74. * @param string/array $Object_foreignKey
  75. * @return $query
  76. */
  77. function buildUpdate($Object_name, $Object_tableName, $Object_primaryKey, $Object_foreignKey=array()){
  78. $Object_match='';
  79. $Update_set='';
  80. $Where_set='';
  81. foreach($this->arrayObject as $key => $value) {
  82. if ($Object_name!=''){
  83. $Object_match=explode('__', $key);
  84. $Object_match=$Object_match[0];
  85. }
  86. if ($Object_match==$Object_name){
  87. $value=str_replace("\'","'",$value);
  88. $value=str_replace("'","''",$value);
  89. $updateKeyMatched=0;
  90. if (is_array($Object_primaryKey)){
  91. foreach($Object_primaryKey as $updateKey) {
  92. if ($updateKey==str_replace($Object_name.'__','',$key)){
  93. $Where_set.= " AND ".$updateKey." = '".$value."'";
  94. $updateKeyMatched=1;
  95. }
  96. if (!$updateKeyMatched){
  97. // If this key is foreign and value = '' sets the database value to NULL
  98. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  99. $Update_set.= ' '.str_replace($Object_name.'__','',$key)."=NULL,";
  100. else
  101. $Update_set.= ' '.str_replace($Object_name.'__','',$key)."='".$value."',";
  102. }
  103. }
  104. }
  105. else{
  106. if ($Object_primaryKey==str_replace($Object_name.'__','',$key))
  107. $Where_set.= " AND ".$Object_primaryKey." = '".$value."'";
  108. else{
  109. // If this key is foreign and value = '' sets the database value to NULL
  110. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  111. $Update_set.= ' '.str_replace($Object_name.'__','',$key)."=NULL,";
  112. else
  113. $Update_set.= ' '.str_replace($Object_name.'__','',$key)."='".$value."',";
  114. }
  115. }
  116. }
  117. }
  118. $Update_set=rtrim($Update_set, ',');
  119. $query="UPDATE $Object_tableName
  120. SET $Update_set
  121. WHERE 1 $Where_set";
  122. return $query;
  123. }
  124. /**
  125. * buildInsertOrUpdate method: build an Update SQL Query
  126. * starting from an array representing the Object
  127. * fixed problem with foreign key ('' is NULL)
  128. * @param string $Object_name
  129. * @param string $Object_tableName
  130. * @param string/array $Object_foreignKey
  131. * @return $query
  132. */
  133. function buildInsertOrUpdate($Object_name, $Object_tableName, $Object_foreignKey=array()){
  134. $Object_match='';
  135. foreach($this->arrayObject as $key => $value) {
  136. if ($Object_name!=''){
  137. $Object_match=explode('__', $key);
  138. $Object_match=$Object_match[0];
  139. }
  140. if ($Object_match==$Object_name){
  141. $value=str_replace("\'","'",$value);
  142. $value=str_replace("'","''",$value);
  143. // If this key is foreign and value = '' sets the database value to NULL
  144. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  145. $Insert_set.= ' '.str_replace($Object_name.'__','',$key)."=NULL,";
  146. else
  147. $Insert_set.= ' '.str_replace($Object_name.'__','',$key)."='".$value."',";
  148. }
  149. }
  150. $Insert_set=rtrim($Insert_set, ',');
  151. $query="INSERT INTO $Object_tableName
  152. SET $Insert_set
  153. ON DUPLICATE KEY UPDATE
  154. $Insert_set";
  155. return $query;
  156. }
  157. /**
  158. * buildDelete method: build a Delete SQL Query
  159. * starting from an array representing the Object
  160. * @param string $Object_name
  161. * @param string $Object_tableName
  162. * @return $query
  163. */
  164. function buildDelete($Object_name, $Object_tableName, $Object_primaryKey){
  165. $Object_match='';
  166. $Where_set='';
  167. foreach($this->arrayObject as $key => $value) {
  168. if ($Object_name!=''){
  169. $Object_match=explode('__', $key);
  170. $Object_match=$Object_match[0];
  171. }
  172. if ($Object_match==$Object_name){
  173. if (is_array($Object_primaryKey)){
  174. foreach($Object_primaryKey as $updateKey) {
  175. if ($updateKey==str_replace($Object_name.'__','',$key))
  176. $Where_set.= " AND ".$updateKey." = '".$value."'";
  177. }
  178. }
  179. else{
  180. if ($Object_primaryKey==str_replace($Object_name.'__','',$key))
  181. $Where_set.= " AND ".$Object_primaryKey." = '".$value."'";
  182. }
  183. }
  184. }
  185. $query="DELETE FROM $Object_tableName
  186. WHERE 1 $Where_set";
  187. return $query;
  188. }
  189. /**
  190. * getEnumOptions method: get all enum options of a
  191. * field and return an array
  192. * @param string $table
  193. * @param string $field
  194. * @param string $db
  195. * @param boolean $sort
  196. * @return $finalResult
  197. */
  198. function getEnumOptions($table, $field, $db, $sort=1) {
  199. $finalResult = array();
  200. if (strlen(trim($table)) < 1)
  201. return false;
  202. $query = "show columns from $table";
  203. $result = mysql_query($query, $db);
  204. while ($row = mysql_fetch_array($result)){
  205. if ($field != $row["Field"])
  206. continue;
  207. if (ereg('enum.(.*).', $row['Type'], $match)) {
  208. $opts = explode("','", $match[1]);
  209. foreach ($opts as $item)
  210. $finalResult[] = str_replace("'","",$item);
  211. }
  212. else
  213. return false;
  214. }
  215. if ($sort)
  216. sort($finalResult);
  217. return $finalResult;
  218. }
  219. /**
  220. * printTable method:outputs any SQL table in an
  221. * HTML tabular format.
  222. * @param array $result
  223. * @return true if there is data in the result set
  224. * false if there is no data in the result set
  225. */
  226. function printTable($result){
  227. if($result) {
  228. if(!$row = mysql_fetch_assoc($result))
  229. return false;
  230. print("<table><tr>");
  231. foreach($row as $key=>$value){
  232. print("<th>$key</th>");
  233. }
  234. print("</tr>");
  235. do{
  236. print("<tr>");
  237. foreach($row as $key=>$value)
  238. print("<td>$value</td>");
  239. print("</tr>");
  240. }
  241. while ($row = mysql_fetch_assoc($result));
  242. print("</tr></table>");
  243. return true;
  244. }
  245. else
  246. return false;
  247. }
  248. /*version 2.1*/
  249. /*author: acoppo@ncfsistemi.com*/
  250. /**
  251. * getDistinctFields method: get all distinct fields from table
  252. * return an array
  253. * @param string $table
  254. * @param string $field
  255. * @param string $db
  256. * @param boolean $sort
  257. * @return $finalResult
  258. */
  259. function getDistinctFields($table, $field, $db, $sort=1) {
  260. $finalResult = array();
  261. if (strlen(trim($table)) < 1)
  262. return false;
  263. $query = "SELECT DISTINCT $field FROM $table";
  264. $result = mysql_query($query, $db);
  265. while ($row = mysql_fetch_array($result)){
  266. $finalResult[] = $row[$field];
  267. }
  268. // else
  269. // return false;
  270. if ($sort)
  271. sort($finalResult);
  272. return $finalResult;
  273. }
  274. /**
  275. * getForeignLabel method: get a generic label for the foreing field
  276. * generally return a string
  277. * @param string $table
  278. * @param string $labelField
  279. * @param int $foreignKey
  280. * @param string $db
  281. * @return $label
  282. */
  283. function getForeignLabel($table, $labelField, $foreignKey, $db) {
  284. $finalResult = array();
  285. if (strlen(trim($table)) < 1)
  286. return false;
  287. $query = "SELECT DISTINCT $labelField FROM $table WHERE id ='$foreignKey'";
  288. $result = mysql_query($query, $db);
  289. $row = mysql_fetch_array($result);
  290. return $row[0];
  291. }
  292. }
  293. ?>