We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

MartinY on 07/19/07


Tagged

mysql sql class php


Versions (?)


SQL_mysql


Published in: PHP 


Clase para generar SQL's complejas

  1. /** inicio clase: SQL_mysql
  2.  * clase para la generacion de sentencias SQL complejas
  3.  **/
  4. class SQL_mysql {
  5.  
  6. /** variables para la sentencia SQL**/
  7. var $sql_tipo = '';
  8. var $sql_datos;
  9. var $sql_from;
  10. var $sql_where = ' WHERE ';
  11. var $sql_group;
  12. var $sql_order;
  13.  
  14. /** variables de la clase **/
  15. var $bracket = 0;
  16. var $whereBracket = 0;
  17. var $sunion = '';
  18. var $condicion = array();
  19. var $from = array();
  20. var $cfrom = 0;
  21.  
  22.  
  23. /** constructor: SQL_mysql()
  24. * genera la sentencia SQL del tipo pasado
  25. * por defecto SELECT
  26. **/
  27. function SQL_mysql ($tipo='SELECT') {
  28. switch ($tipo) {
  29. case 'SELECT': $this->sql_tipo = 'SELECT '; break;
  30. //case 'DELETE': $this->sql_tipo = 'DELETE '; break;
  31. //case 'INSERT': $this->sql_tipo = 'INSERT '; break;
  32. //case 'UPDATE': $this->sql_tipo = 'UPDATE '; break;
  33. default: trigger_error('Tipo de sentencia SQL no aceptada por el constructor de la clase SQL_mysql.');
  34. }
  35. }
  36.  
  37. function addData ($dato) {
  38. //$this->searchTables($dato);
  39. if (is_array($dato))
  40. foreach ($dato as $key => $value)
  41. if (empty($this->sql_datos)) $this->sql_datos .= $key . ' AS ' . $value;
  42. else $this->sql_datos .= ', ' . $key . ' AS ' . $value;
  43. else
  44. if (empty($this->sql_datos)) $this->sql_datos .= $dato;
  45. else $this->sql_datos .= ', ' . $dato;
  46. }
  47.  
  48. function addFrom ($dato, $extra=false) {
  49. if (!$extra) {
  50. if (is_array($dato)) {
  51. foreach ($dato as $value)
  52. if (!array_key_exists($value, $this->from))
  53. $this->from[$value] = 'T' . $this->cfrom++;
  54. } else {
  55. if (!array_key_exists($dato, $this->from))
  56. $this->from[$dato] = 'T' . $this->cfrom++;
  57. }
  58. } else {
  59. if (is_array($dato)) {
  60. foreach ($dato as $key => $value)
  61. if (empty($this->sql_from)) $this->sql_from = $key . ' ' . $value;
  62. else $this->sql_from .= ', ' . $key . ' ' . $value;
  63. }
  64. else {
  65. if (empty($this->sql_from)) $this->sql_from = $dato;
  66. else $this->sql_from .= $dato;
  67. }
  68. }
  69. }
  70.  
  71. function completeFrom () {
  72. if (!empty($this->sql_from) && !empty($this->from))
  73. trigger_error('Parte FROM construida incorrectamente [clase SQL_mysql].');
  74. foreach ($this->from as $key => $value) {
  75. if (empty($this->sql_from)) $this->sql_from = $key;// . ' ' . $value;
  76. else $this->sql_from .= ', ' . $key;// . ' ' . $value;
  77. }
  78. if (empty($this->sql_from)) return false;
  79. return true;
  80. }
  81.  
  82. function searchTables ($datos) {
  83. $tablas = array();
  84. if (is_array($datos)) {
  85. // sin hacer
  86. echo 'sin hacer searchtables para arrays';
  87. } else {
  88. $par = true;
  89. $datos = explode('.',$datos);
  90. if (is_array($datos) && !empty($datos))
  91. foreach ($datos as $value) {
  92. if ($par) {
  93. $value = explode(' ',$value);
  94. $value = explode('(',$value[count($value)-1]);
  95. $tablas[] = $value[count($value)-1];
  96. $par = false;
  97. } else {
  98. $par = true;
  99. }
  100. }
  101. }
  102. if (!empty($tablas)) $this->addFrom($tablas,true);
  103. }
  104.  
  105. function addWhere ($dato,$union) {
  106. //$this->searchTables($dato);
  107. if (is_array($dato)) {
  108. // no funciona de momento
  109. foreach ($dato as $key => $value)
  110. $this->sql_where .= $this-sunion.' '.$key.' = '.$value.' ';
  111. $this->sunion = $union;
  112. }
  113. else {
  114. $this->sql_where .= $this->sunion.' ';
  115. while ($this->bracket > $this->whereBracket) {
  116. $this->whereBracket++;
  117. if (!empty($this->condicion[$this->whereBracket]))
  118. $this->sql_where .= $this->condicion[$this->whereBracket].' ';
  119. $this->sql_where .= '( ';
  120. }
  121. $this->sql_where .= $dato.' ';
  122. $this->sunion = $union;
  123. }
  124. }
  125.  
  126. function addWherePost ($clave, $valor, $union, $nexo='=') {
  127. //$this->searchTables($clave);
  128. if (isset($_POST[$valor]) && !empty($_POST[$valor]) && is_array($_POST[$valor])) {
  129. $this->sql_where .= $this->sunion . ' ';
  130. while ($this->bracket > $this->whereBracket) {
  131. $this->whereBracket++;
  132. if (!empty($this->condicion[$this->whereBracket]))
  133. $this->sql_where .= $this->condicion[$this->whereBracket].' ';
  134. $this->sql_where .= '( ';
  135. }
  136. $primera = true;
  137. foreach ($_POST[$valor] as $value) {
  138. if ($primera)
  139. $primera = false;
  140. else
  141. $this->sql_where .= $this->sunion . ' ';
  142. $this->sql_where .= $clave . ' ' . $nexo . ' \'' . $value . '\' ';
  143. $this->sunion = $union;
  144. }
  145. } else {
  146. if (isset($_POST[$valor]) && !empty($_POST[$valor])) {
  147. $this->sql_where .= $this->sunion.' ';
  148. while ($this->bracket > $this->whereBracket) {
  149. $this->whereBracket++;
  150. if (!empty($this->condicion[$this->whereBracket]))
  151. $this->sql_where .= $this->condicion[$this->whereBracket] . ' ';
  152. $this->sql_where .= '( ';
  153. }
  154. $this->sql_where .= $clave . ' ' . $nexo . ' \'' . $_POST[$valor] . '\' ';
  155. $this->sunion = $union;
  156. }
  157. }
  158. }
  159.  
  160. // break
  161.  
  162. /** metodo: openBracket()
  163. * abre parentesis en la sentencia SQL
  164. **/
  165. function openBracket ($dato = '', $union = 'AND') {
  166. //$this->searchTables($dato);
  167. $this->bracket++;
  168. if (!empty($dato)) $this->condicion[$this->bracket] = $dato . ' ' . $union;
  169. }
  170.  
  171. /** metodo: closeBracket()
  172. * cierra parentesis en la sentencia SQL
  173. * lanza un error al intentar quitar mas parentesis de los que existan
  174. **/
  175. function closeBracket () {
  176. if (!empty($this->condicion[$this->bracket])) $this->condicion[$this->bracket] = '';
  177. if ($this->whereBracket >= $this->bracket) {
  178. $this->sql_where .= ') ';
  179. $this->whereBracket--;
  180. }
  181. if ($this->bracket > 0) $this->bracket--;
  182. else trigger_error('No se puede quitar parentesis inexistentes [clase SQL_mysql].');
  183. }
  184.  
  185. /** metodo: addGroup()
  186. * añade una agrupacion a la sentencia SQL
  187. **/
  188. function addGroup ($dato) {
  189. //$this->searchTables($dato);
  190. if (empty($this->sql_group)) $this->sql_group = $dato;
  191. else $this->sql_group .= ', ' . $dato;
  192. }
  193.  
  194. /** metodo: addOrder()
  195. * añade una ordenacion a la sentencia SQL
  196. **/
  197. function addOrder ($dato) {
  198. //$this->searchTables($dato);
  199. if (empty($this->sql_order)) $this->sql_order = $dato;
  200. else $this->sql_order .= ', ' . $dato;
  201. }
  202.  
  203. /** metodo: getSQL()
  204. * devuelve la sentencia SQL construida
  205. * en caso de error devuelve false
  206. **/
  207. function getSQL ($mostrar=false) {
  208. if ($this->whereBracket != 0) {
  209. trigger_error('Falta por cerrar un parentesis [clase SQL_mysql].');
  210. return false;
  211. }
  212. if (!$this->completeFrom()) {
  213. trigger_error('Sin datos en el FROM [clase SQL_mysql].');
  214. return false;
  215. }
  216. if (!empty($this->sql_tipo)) {
  217. $cadena_sql = $this->sql_tipo;
  218. if (empty($this->sql_datos)) $cadena_sql .= ' * ';
  219. else $cadena_sql .= $this->sql_datos;
  220. if (!empty($this->sql_from)) $cadena_sql .= ' FROM ' . $this->sql_from;
  221. if (strcmp($this->sql_where, ' WHERE ') !== 0) $cadena_sql .= $this->sql_where;
  222. if (!empty($this->sql_group)) $cadena_sql .= ' GROUP BY ' . $this->sql_group;
  223. if (!empty($this->sql_order)) $cadena_sql .= ' ORDER BY ' . $this->sql_order;
  224. if ($mostrar) echo $cadena_sql;
  225. return $cadena_sql;
  226. }
  227. return false;
  228. }
  229.  
  230. }
  231. /** fin clase: SQL_mysql **/

Report this snippet 

You need to login to post a comment.