PHP Classes

File: class.QueryInAction.php

Recommend this page to a friend!
  Classes of Koeksal T.   PHP Query In Action   class.QueryInAction.php   Download  
File: class.QueryInAction.php
Role: Class source
Content type: text/plain
Description: Class QueryInAction
Class: PHP Query In Action
MySQL fluent query builder and executor
Author: By
Last change:
Date: 10 years ago
Size: 11,244 bytes
 

Contents

Class file image Download
<?php /** * QueryInAction * generate and execute MySQL-Query * Exemple 1: * $query = new QueryInAction(); * * $result = $query->select("*") * ->from("table") * ->where("ID","=",12) * ->whereAnd("group","=","news") * ->execute(); * * @author Koeksal Toprak <r1zabey@googlemail.com> * @name QueryInAction * @category MySQL * @version 1.3 */ class QueryInAction { /** * * @var boolean Check, if an another instance of QueryInAction exist */ private static $instance = NULL; /** * * @var String Adresse vom MySQL-Server */ private $mysqli_host; /** * * @var String MySQL-Benutzer mit Zugriffberechtigung für die zu bearbeitenden Datenbank */ private $mysqli_user; /** * * @var String Passwort für den angegebenen Benutzer */ private $mysqli_pass; /** * * @var String Die zu bearbeitende Datenbank */ private $mysqli_database; /** * * @var Object(mysqli) Schnittstelle zum Datenbank */ private $conn; /** * * @var String Abfrage wird zusammengestellt */ private $Query; /** * * @var Boolean Wurde ein SELECT ausgewählt? */ private $select = FALSE; /** * * @var String Was wurde Selektiert? */ private $selected = ''; /** * * @var Boolean Wurde ein INSERT ausgewählt? */ private $insert = FALSE; /** * * @var Boolean Wurde ein UPDATE ausgewählt? */ private $update = FALSE; /** * * @var Boolean Wurde ein DELETE ausgewählt? */ private $delete = FALSE; /** * Initialisierung der Klasse */ private function __construct() { $this->Query = ""; } /** * Klassenvariablen */ public function __destruct() { unset($this->mysqli_host); unset($this->mysqli_user); unset($this->mysqli_pass); unset($this->mysqli_database); unset($this->conn); unset($this->Query); unset($this->select); unset($this->selected); unset($this->insert); unset($this->update); unset($this->delete); } /** * * @param String $host host from MySQL-Server * @param String $user MySQL username * @param String $pass MySQL password * @param String $database MySQL database * @return boolean */ public static function config($host, $user, $pass, $database) { $result = FALSE; if(self::$instance == NULL) { self::$instance = new QueryInAction(); // sind die Parameter gesetzt? if(isset($host, $user, $pass, $database)) { // sind die Parameter mit inhalt gefüllt? if(!empty($host) && !empty($user) && !empty($database)) { // sind die Parameter vom Typ String? if(is_string($host) && is_string($user) && is_string($pass) && is_string($database)) { self::$instance->mysqli_host = $host; //$this->mysqli_host = $host; self::$instance->mysqli_user = $user; self::$instance->mysqli_pass = $pass; self::$instance->mysqli_database = $database; if(self::$instance->connect()) { self::$instance->conn->set_charset("UTF8"); $result = self::$instance; } else { echo "Fehler bei der Verbindung: " . mysqli_connect_error(); } } else { die("Ein oder mehrere Parameter ist/sind nicht vom Typ STRING<br>"); } } else { die("Ein oder mehrere Parameter ist/sind leer<br>"); } } else { die("Ein oder mehrere Parameter ist/sind nicht gesetzt<br>"); } } // if(self::$instance == NULL) return $result; } /** * Stellt die Verbindung zum MySQL-Server auf. * * @return boolean bei erfolgreicher Verbindung TRUE, ansonsten FALSE */ private function connect() { $result = FALSE; $this->conn = new mysqli($this->mysqli_host, $this->mysqli_user, $this->mysqli_pass, $this->mysqli_database); if($this->conn) { $result = TRUE; } return $result; } /** * gibt den aktuellen vom Benutzer zusammengebauten MySQL-Query zurück * * @return string */ private function getQuery() { return $this->Query; } /** * Query leeren * * @access private */ private function clearQuery() { $this->Query = ''; } /** * Erweitert den Query um SELECT * * @access public * @param string $what gibt an, was selektiert werden soll * @return Object gibt die klasse selbst zurück. */ public function select($what) { $this->select = TRUE; $this->Query .= "SELECT " . $what . " "; $this->selected = $what; return $this; } /** * Erweitert den Query um INSERT * * @param string $into gibt an, wohin etwas eingefügt wird * @return Object gibt die klasse selbst zurück. */ public function insert($into) { $this->insert = TRUE; $this->Query .= "INSERT INTO " . $into . " "; return $this; } /** * * @param string $from * @param string $condition * @return \hadim\mysql\QueryInAction */ public function innerJoin($from,$condition) { $this->Query .= "INNER JOIN " . $from . " ON " . $condition . " "; return $this; } /** * * @param string $definitions * @return Object */ public function values($definitions) { $this->Query .= "VALUES ". $definitions ." "; return $this; } /** * * @param string $table * @param string $definitions * @return Object */ public function update ($table,$definitions) { $this->update = TRUE; $this->Query .= "UPDATE " . $table . " SET " . $definitions . " "; return $this; } /** * * @param string $from * @return Object */ public function delete($from) { $this->delete = TRUE; $this->Query .= "DELETE FROM " . $from . " "; return $this; } /** * * @param string $from * @return Object */ public function from($from) { $this->Query .= "FROM " . $from . " "; return $this; } /** * * @param string $attribute * @param string $condition * @param string $value * @return Object */ public function where($attribute, $condition, $value) { $this->Query .= "WHERE " . $attribute . " " . $condition . " '" . $value . "' "; return $this; } /** * * @param string $attribute * @param string $condition * @param string $value * @return Object */ public function whereAnd($attribute, $condition, $value) { $this->Query .= "AND " . $attribute . " " . $condition . " '" . $value . "' "; return $this; } /** * * @param string $attribute * @param string $order * @return Object */ public function order($attribute, $order) { $this->Query .= "ORDER BY " . $attribute . " " . $order . " "; return $this; } /** * * @param int $amount * @return Object */ public function limit($amount) { $this->Query .= "LIMIT " . $amount . " "; return $this; } private function executeSelect($resultmode) { switch (strtolower($resultmode)) { case "assoc": $resultat = $this->conn->query($this->getQuery()); $ergebnis = Array(); if($resultat){ while ($row = $resultat->fetch_assoc()) { array_push($ergebnis, $row); } } break; case "array": $resultat = $this->conn->query($this->getQuery()); $ergebnis = Array(); if($resultat){ while ($row = $resultat->fetch_array()) { array_push($ergebnis, $row); } } break; case "field": $resultat = $this->conn->query($this->getQuery()); $ergebnis = Array(); if($resultat){ while ($row = $resultat->fetch_field()) { array_push($ergebnis, $row); } } break; case "fields": $resultat = $this->conn->query($this->getQuery()); $ergebnis = Array(); if($resultat){ while ($row = $resultat->fetch_array()) { array_push($ergebnis, $row); } } break; case "object": $resultat = $this->conn->query($this->getQuery()); $ergebnis = Array(); if($resultat){ while ($row = $resultat->fetch_object()) { array_push($ergebnis, $row); } } break; case "row": $resultat = $this->conn->query($this->getQuery()); $ergebnis = Array(); if($resultat){ while ($row = $resultat->fetch_row()) { array_push($ergebnis, $row); } } break; default : $ergebnis = FALSE; } return $ergebnis; } public function execute($resultmode = "assoc") { $ergebnis = FALSE; //diese geben NUR ein boolean zurück if($this->insert || $this->update || $this->delete) $ergebnis = $this->conn->query($this->getQuery()); //gibt boolean oder Inhalt zurück if($this->select) { $ergebnis = $this->executeSelect($resultmode); } //Datenbank verbindung trennen $this->conn->close(); //ausgeführtes query zurücksetzen $this->clearQuery(); return $ergebnis; } } ?>