Published in: PHP
Clase para generar SQL's complejas
/** inicio clase: SQL_mysql * clase para la generacion de sentencias SQL complejas **/ class SQL_mysql { /** variables para la sentencia SQL**/ var $sql_tipo = ''; var $sql_datos; var $sql_from; var $sql_where = ' WHERE '; var $sql_group; var $sql_order; /** variables de la clase **/ var $bracket = 0; var $whereBracket = 0; var $sunion = ''; var $cfrom = 0; /** constructor: SQL_mysql() * genera la sentencia SQL del tipo pasado * por defecto SELECT **/ function SQL_mysql ($tipo='SELECT') { switch ($tipo) { case 'SELECT': $this->sql_tipo = 'SELECT '; break; //case 'DELETE': $this->sql_tipo = 'DELETE '; break; //case 'INSERT': $this->sql_tipo = 'INSERT '; break; //case 'UPDATE': $this->sql_tipo = 'UPDATE '; break; default: trigger_error('Tipo de sentencia SQL no aceptada por el constructor de la clase SQL_mysql.'); } } function addData ($dato) { //$this->searchTables($dato); foreach ($dato as $key => $value) else $this->sql_datos .= ', ' . $key . ' AS ' . $value; else else $this->sql_datos .= ', ' . $dato; } function addFrom ($dato, $extra=false) { if (!$extra) { foreach ($dato as $value) $this->from[$value] = 'T' . $this->cfrom++; } else { $this->from[$dato] = 'T' . $this->cfrom++; } } else { foreach ($dato as $key => $value) else $this->sql_from .= ', ' . $key . ' ' . $value; } else { else $this->sql_from .= $dato; } } } function completeFrom () { foreach ($this->from as $key => $value) { else $this->sql_from .= ', ' . $key;// . ' ' . $value; } return true; } function searchTables ($datos) { // sin hacer echo 'sin hacer searchtables para arrays'; } else { $par = true; foreach ($datos as $value) { if ($par) { $par = false; } else { $par = true; } } } } function addWhere ($dato,$union) { //$this->searchTables($dato); // no funciona de momento foreach ($dato as $key => $value) $this->sql_where .= $this-sunion.' '.$key.' = '.$value.' '; $this->sunion = $union; } else { $this->sql_where .= $this->sunion.' '; while ($this->bracket > $this->whereBracket) { $this->whereBracket++; $this->sql_where .= $this->condicion[$this->whereBracket].' '; $this->sql_where .= '( '; } $this->sql_where .= $dato.' '; $this->sunion = $union; } } function addWherePost ($clave, $valor, $union, $nexo='=') { //$this->searchTables($clave); $this->sql_where .= $this->sunion . ' '; while ($this->bracket > $this->whereBracket) { $this->whereBracket++; $this->sql_where .= $this->condicion[$this->whereBracket].' '; $this->sql_where .= '( '; } $primera = true; foreach ($_POST[$valor] as $value) { if ($primera) $primera = false; else $this->sql_where .= $this->sunion . ' '; $this->sql_where .= $clave . ' ' . $nexo . ' \'' . $value . '\' '; $this->sunion = $union; } } else { $this->sql_where .= $this->sunion.' '; while ($this->bracket > $this->whereBracket) { $this->whereBracket++; $this->sql_where .= $this->condicion[$this->whereBracket] . ' '; $this->sql_where .= '( '; } $this->sql_where .= $clave . ' ' . $nexo . ' \'' . $_POST[$valor] . '\' '; $this->sunion = $union; } } } // break /** metodo: openBracket() * abre parentesis en la sentencia SQL **/ function openBracket ($dato = '', $union = 'AND') { //$this->searchTables($dato); $this->bracket++; } /** metodo: closeBracket() * cierra parentesis en la sentencia SQL * lanza un error al intentar quitar mas parentesis de los que existan **/ function closeBracket () { if ($this->whereBracket >= $this->bracket) { $this->sql_where .= ') '; $this->whereBracket--; } if ($this->bracket > 0) $this->bracket--; } /** metodo: addGroup() * añade una agrupacion a la sentencia SQL **/ function addGroup ($dato) { //$this->searchTables($dato); else $this->sql_group .= ', ' . $dato; } /** metodo: addOrder() * añade una ordenacion a la sentencia SQL **/ function addOrder ($dato) { //$this->searchTables($dato); else $this->sql_order .= ', ' . $dato; } /** metodo: getSQL() * devuelve la sentencia SQL construida * en caso de error devuelve false **/ function getSQL ($mostrar=false) { if ($this->whereBracket != 0) { return false; } if (!$this->completeFrom()) { return false; } $cadena_sql = $this->sql_tipo; else $cadena_sql .= $this->sql_datos; return $cadena_sql; } return false; } } /** fin clase: SQL_mysql **/
You need to login to post a comment.
