Código PHP:
class SQLQuery{
var $query; // (string) Última consulta creada
var $table; // (string) Tabla usada
var $values; // (array) Cada elemento es un nuevo arreglo con llaves que son los nombres de los campos. Cada nuevo arreglo contiene los elementos: (string) "value" y (bool) "is_sql_function"
var $where; // (string) clausula "WHERE"
var $limit; // (string) clausula "LIMIT"
var $selection; // (string) Selección de la clausula SELECT: "SELECT (selection) FROM table;"
function SQLQuery(){
$this->query = "";
$this->table = "";
$this->values = array();
$this->where = "";
$this->limit = "";
$this->selection = "*";
}
function createSelect($use_limit_clause = true){
if($this->selection=="" || $this->table==""){
if($use_limit_clause){
if($this->selection==""){
$this->error('No se ha podido ejecutar la consulta "SELECT". El parámetro <strong>"selección"</strong> está vacío.');
}
if($this->table==""){
$this->error('No se ha podido ejecutar la consulta "SELECT". El parámetro <strong>"tabla"</strong> está vacío.');
}
}
return false;
}
$this->query = "SELECT " . $this->selection . " FROM " . $this->table . $this->returnWhere();
$this->query .= ($use_limit_clause) ? $this->returnLimit() : "";
return $this->query;
}
function createInsert(){
if($this->table=="" || count($this->values)==0){
if($this->table=="") {
$this->error('No se ha podido ejecutar la consulta "INSERT". El parámetro <strong>"tabla"</strong> está vacío.');
}
if (count($this->values) == 0) {
$this->error('No se ha podido ejecutar la consulta "INSERT". El parámetro <strong>"valores (VALUES)"</strong> está vacío.');
}
return false;
}
$values = $fields = array();
foreach($this->values as $fieldName => $fieldSettings){
$fields[] = $fieldName;
if ($fieldSettings['value']===NULL){
$values[] = "NULL";
}else if($fieldSettings['is_sql_function']){
$values[] = $fieldSettings['value'];
}else{
$values[] = "'" . $fieldSettings['value'] . "'";
}
}
$values = " (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $values) . ")";
$this->query = "INSERT INTO " . $this->table . $values;
return $this->query;
}
function createUpdate(){
if ($this->table == "" || count($this->values) == 0 || $this->where == ""){
if ($this->table == ""){
$this->error('No se ha podido ejecutar la consulta "UPDATE". El parámetro <strong>"tabla"</strong> está vacío.');
}
if (count($this->values) == 0){
$this->error('No se ha podido ejecutar la consulta "UPDATE". El parámetro <strong>"valores (VALUES)"</strong> está vacío.');
}
if ($this->where == "") {
$this->error('Safety procedure: La consulta "UPDATE" no ha sido ejecutada porque la clausula <strong>"WHERE"</strong> está vacía.');
}
return false;
}
$values = $fields = array();
foreach($this->values as $fieldName => $fieldSettings){
if($fieldSettings['value'] === NULL){
$values[] = $fieldName . " = NULL";
}else if($fieldSettings['is_sql_function']){
$values[] = $fieldName . " = " . $fieldSettings['value'];
}else{
$values[] = $fieldName . " = '" . $fieldSettings['value'] . "'";
}
}
$values = " SET " . implode(', ', $values);
$this->query = "UPDATE " . $this->table . $values . $this->returnWhere() . $this->returnLimit();
return $this->query;
}
function createDelete(){
if($this->table == "" || $this->where == ""){
if ($this->table == ""){
$this->error('No se ha podido ejecutar la consulta "DELETE". El parámetro <strong>"tabla"</strong> está vacío.');
}
if ($this->where == ""){
$this->error('Safety procedure: "DELETE" query was not created because <strong>"where"</strong> clause was empty.');
}
return false;
}
$this->query = "DELETE FROM " . $this->table . $this->returnWhere() . $this->returnLimit();
return $this->query;
}
function createTruncate(){
if ($this->table == ""){
$this->error('No se ha podido ejecutar la consulta "TRUNCATE". El parámetro <strong>"tabla"</strong> está vacío.');
return false;
}
$this->query = "TRUNCATE TABLE " . $this->table;
return $this->query;
}
function returnWhere(){
$where = ($this->where != "") ? (" WHERE (" . $this->where . ")") : "";
return $where;
}
function returnLimit(){
$limit = ($this->limit != "") ? (" LIMIT " . $this->limit) : "";
return $limit;
}
function error($message = ""){
echo '<br /><span style="padding: 1px 7px 1px 7px; background-color: #ffd7d7; font-family: verdana; color: #000000; font-size: 13px;"><span style="color: #ff0000; font-weight: bold;">Error!</span> ' . $message . '</span><br />';
}
}