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

/branches/messigdp/include/Database/save_QueryBuilder.class.php

http://antilophpe.googlecode.com/
PHP | 317 lines | 214 code | 14 blank | 89 comment | 36 complexity | b38a9893fb5447d3b29f1af505fb8137 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. if($Object_name=='Utente') $value=str_replace("\'","'",$value);
  54. else $value=strtoupper(str_replace("\'","'",$value));
  55. $value=str_replace("'","''",$value);
  56. // If this key is foreign and value = '' sets the database value to NULL
  57. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  58. $Insert_set.= ' NULL,';
  59. else{
  60. if(is_numeric($value)) $Insert_set.= ' '.$value.',';
  61. else $Insert_set.= ' \''.$value.'\',';
  62. }
  63. }
  64. }
  65. $Insert_set=rtrim($Insert_set, ',');
  66. if((substr($Object_tableName, -3, 3)!='Log')&&(substr($Object_tableName, -4, 4)!='File')) $altro=', 0';
  67. $query='INSERT INTO "'.$Object_tableName.'"
  68. VALUES ('.last_insert_id($Object_tableName).', '.$Insert_set.$altro.')';
  69. return $query;
  70. }
  71. /**
  72. * buildUpdate method: build an Update SQL Query
  73. * starting from an array representing the Object
  74. * fixed problem with foreign key ('' is NULL)
  75. * @param string $Object_name
  76. * @param string $Object_tableName
  77. * @param string/array $Object_primaryKey
  78. * @param string/array $Object_foreignKey
  79. * @return $query
  80. */
  81. function buildUpdate($Object_name, $Object_tableName, $Object_primaryKey, $Object_foreignKey=array()){
  82. $Object_match='';
  83. $Update_set='';
  84. $Where_set='';
  85. foreach($this->arrayObject as $key => $value) {
  86. if ($Object_name!=''){
  87. $Object_match=explode('__', $key);
  88. $Object_match=$Object_match[0];
  89. }
  90. if ($Object_match==$Object_name){
  91. if($Object_name=='Utente') $value=str_replace("\'","'",$value);
  92. else $value=strtoupper(str_replace("\'","'",$value));
  93. $value=str_replace("'","''",$value);
  94. $updateKeyMatched=0;
  95. if (is_array($Object_primaryKey)){
  96. foreach($Object_primaryKey as $updateKey) {
  97. if ($updateKey==str_replace($Object_name.'__','',$key)){
  98. $Where_set.= " AND ".$updateKey." = '".$value."'";
  99. $updateKeyMatched=1;
  100. }
  101. if (!$updateKeyMatched){
  102. // If this key is foreign and value = '' sets the database value to NULL
  103. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  104. $Update_set.= ' "'.str_replace($Object_name.'__','',$key).'"=NULL,';
  105. else
  106. $Update_set.= ' "'.str_replace($Object_name.'__','',$key).'"=\''.$value.'\',';
  107. }
  108. }
  109. }
  110. else{
  111. if ($Object_primaryKey==str_replace($Object_name.'__','',$key))
  112. $Where_set.= ' AND "'.$Object_primaryKey.'"='.$value;
  113. else{
  114. // If this key is foreign and value = '' sets the database value to NULL
  115. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  116. $Update_set.= ' "'.str_replace($Object_name.'__','',$key).'"=NULL,';
  117. else{
  118. if(is_numeric($value)) $Update_set.= ' "'.str_replace($Object_name.'__','',$key).'"='.$value.',';
  119. else $Update_set.= ' "'.str_replace($Object_name.'__','',$key).'"=\''.$value.'\',';
  120. }
  121. }
  122. }
  123. }
  124. }
  125. $Update_set=rtrim($Update_set, ',');
  126. $query='UPDATE "'.$Object_tableName.'"
  127. SET '.$Update_set.'
  128. WHERE 1=1 '.$Where_set;
  129. return $query;
  130. }
  131. /**
  132. * buildInsertOrUpdate method: build an Update SQL Query
  133. * starting from an array representing the Object
  134. * fixed problem with foreign key ('' is NULL)
  135. * @param string $Object_name
  136. * @param string $Object_tableName
  137. * @param string/array $Object_foreignKey
  138. * @return $query
  139. */
  140. function buildInsertOrUpdate($Object_name, $Object_tableName, $Object_foreignKey=array()){
  141. $Object_match='';
  142. foreach($this->arrayObject as $key => $value) {
  143. if ($Object_name!=''){
  144. $Object_match=explode('__', $key);
  145. $Object_match=$Object_match[0];
  146. }
  147. if ($Object_match==$Object_name){
  148. $value=str_replace("\'","'",$value);
  149. $value=str_replace("'","''",$value);
  150. // If this key is foreign and value = '' sets the database value to NULL
  151. if (array_search(str_replace($Object_name.'__','',$key), $Object_foreignKey)!==false && $value=='')
  152. $Insert_set.= ' '.str_replace($Object_name.'__','',$key)."=NULL,";
  153. else
  154. $Insert_set.= ' '.str_replace($Object_name.'__','',$key)."='".$value."',";
  155. }
  156. }
  157. $Insert_set=rtrim($Insert_set, ',');
  158. $query="INSERT INTO $Object_tableName
  159. SET $Insert_set
  160. ON DUPLICATE KEY UPDATE
  161. $Insert_set";
  162. return $query;
  163. }
  164. /**
  165. * buildDelete method: build a Delete SQL Query
  166. * starting from an array representing the Object
  167. * @param string $Object_name
  168. * @param string $Object_tableName
  169. * @return $query
  170. */
  171. function buildDelete($Object_name, $Object_tableName, $Object_primaryKey){
  172. $Object_match='';
  173. $Where_set='';
  174. foreach($this->arrayObject as $key => $value) {
  175. if ($Object_name!=''){
  176. $Object_match=explode('__', $key);
  177. $Object_match=$Object_match[0];
  178. }
  179. if ($Object_match==$Object_name){
  180. if (is_array($Object_primaryKey)){
  181. foreach($Object_primaryKey as $updateKey) {
  182. if ($updateKey==str_replace($Object_name.'__','',$key))
  183. $Where_set.= " AND ".$updateKey." = '".$value."'";
  184. }
  185. }
  186. else{
  187. if ($Object_primaryKey==str_replace($Object_name.'__','',$key))
  188. $Where_set.= " AND ".$Object_primaryKey." = '".$value."'";
  189. }
  190. }
  191. }
  192. $query="DELETE FROM $Object_tableName
  193. WHERE 1 $Where_set";
  194. return $query;
  195. }
  196. /**
  197. * getEnumOptions method: get all enum options of a
  198. * field and return an array
  199. * @param string $table
  200. * @param string $field
  201. * @param string $db
  202. * @param boolean $sort
  203. * @return $finalResult
  204. */
  205. function getEnumOptions($table, $field, $db, $sort=1) {
  206. $finalResult = array();
  207. if (strlen(trim($table)) < 1)
  208. return false;
  209. $query = "show columns from $table";
  210. $result = oci_parse($resourceLinkID, $query);
  211. while ($row = oci_fetch_array($result)){
  212. if ($field != $row["Field"])
  213. continue;
  214. if (ereg('enum.(.*).', $row['Type'], $match)) {
  215. $opts = explode(',', $match[1]);
  216. foreach ($opts as $item)
  217. $finalResult[] = substr($item, 1, strlen($item)-2);
  218. }
  219. else
  220. return false;
  221. }
  222. if ($sort)
  223. sort($finalResult);
  224. return $finalResult;
  225. }
  226. /**
  227. * printTable method:outputs any SQL table in an
  228. * HTML tabular format.
  229. * @param array $result
  230. * @return true if there is data in the result set
  231. * false if there is no data in the result set
  232. */
  233. function printTable($result){
  234. if($result) {
  235. if(!$row = oci_fetch_array($result))
  236. return false;
  237. print("<table><tr>");
  238. foreach($row as $key=>$value){
  239. print("<th>$key</th>");
  240. }
  241. print("</tr>");
  242. do{
  243. print("<tr>");
  244. foreach($row as $key=>$value)
  245. print("<td>$value</td>");
  246. print("</tr>");
  247. }
  248. while ($row = oci_fetch_array($result));
  249. print("</tr></table>");
  250. return true;
  251. }
  252. else
  253. return false;
  254. }
  255. /*version 2.1*/
  256. /*author: acoppo@ncfsistemi.com*/
  257. /**
  258. * getDistinctFields method: get all distinct fields from table
  259. * return an array
  260. * @param string $table
  261. * @param string $field
  262. * @param string $db
  263. * @param boolean $sort
  264. * @return $finalResult
  265. */
  266. function getDistinctFields($table, $field, $db, $sort=1) {
  267. $finalResult = array();
  268. if (strlen(trim($table)) < 1)
  269. return false;
  270. $query = "SELECT DISTINCT $field FROM $table";
  271. $result = mysql_query($query, $db);
  272. while ($row = oci_fetch_array($result)){
  273. $finalResult[] = $row[$field];
  274. }
  275. // else
  276. // return false;
  277. if ($sort)
  278. sort($finalResult);
  279. return $finalResult;
  280. }
  281. /**
  282. * getForeignLabel method: get a generic label for the foreing field
  283. * generally return a string
  284. * @param string $table
  285. * @param string $labelField
  286. * @param int $foreignKey
  287. * @param string $db
  288. * @return $label
  289. */
  290. function getForeignLabel($table, $labelField, $foreignKey, $db) {
  291. $finalResult = array();
  292. if (strlen(trim($table)) < 1)
  293. return false;
  294. $query = "SELECT DISTINCT $labelField FROM $table WHERE id ='$foreignKey'";
  295. $result = mysql_query($query, $db);
  296. $row = oci_fetch_array($result);
  297. return $row[0];
  298. }
  299. }
  300. ?>