openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f7cbc2b6fa99c59e0051c975d7d6f6aa65321147
parent f1c5e4f2fdafd2c824b10effb916a556c1c9a5d5
Author: Jan Dankert <devnull@localhost>
Date:   Sat, 21 Jul 2018 00:00:21 +0200

Datenbank-Modul weiter aufgeräumt und alten Kram entfernt. Das erzeugte Prepared-Statement wird nun im Statement gespeichert, da wo es hingehört.

Diffstat:
modules/cms-core/Dispatcher.class.php | 4+---
modules/database/Database.class.php | 58++++++++++++++++++++++++++--------------------------------
modules/database/Statement.class.php | 43+++++++++++++++++++------------------------
modules/database/driver/PDODriver.class.php | 103+++++++++++++++++++++++++++----------------------------------------------------
4 files changed, 81 insertions(+), 127 deletions(-)

diff --git a/modules/cms-core/Dispatcher.class.php b/modules/cms-core/Dispatcher.class.php @@ -345,9 +345,7 @@ class Dispatcher if (is_object($db)) { - $ok = $db->connect(); - if (!$ok) - throw new DomainException('Database is not available: ' . $db->error); + $db->connect(); // throws exception if error. Session::setDatabase($db); } diff --git a/modules/database/Database.class.php b/modules/database/Database.class.php @@ -64,7 +64,6 @@ class Database /** * Client. - * Enth�lt ein Objekt der Klasse db_<type>. * * @var PDODriver */ @@ -108,35 +107,14 @@ class Database */ public function connect() { - // Ausfuehren des Systemkommandos vor Verbindungsaufbau - if ( !empty($this->conf['cmd'])) - { - $ausgabe = array(); - $rc = false; + // Ausfuehren des Systemkommandos vor Verbindungsaufbau + if (!empty($this->conf['cmd'])) + $this->executeSystemCommand( $this->conf['cmd'] ); - Logger::debug("Database command executing: ".$this->conf['cmd']); - exec( $this->conf['cmd'],$ausgabe,$rc ); - - foreach( $ausgabe as $zeile ) - Logger::debug("Database command output: ".$zeile); - - if ( $rc != 0 ) - { - throw new RuntimeException( 'Command failed: '.implode("",$ausgabe) ); - } - } - - $type = $this->conf['type']; - $classname = 'database\\driver\\'.strtoupper($type).'Driver'; - if ( ! class_exists($classname) ) - { - $this->available = false; - throw new RuntimeException( "Database type '$type' is not available, class '$classname' was not found"); - } // Client instanziieren - $this->client = new $classname; - + $this->client = new PDODriver(); + // Verbindung aufbauen $this->client->connect( $this->conf ); // SQL nach Verbindungsaufbau ausfuehren. @@ -144,9 +122,9 @@ class Database { $cmd = $this->conf['connection_sql']; - $sql = $this->sql($cmd); + $stmt = $this->sql($cmd); - $ok = $sql->execute(); + $ok = $stmt->execute(); if ( ! $ok ) { @@ -165,10 +143,9 @@ class Database } - Logger::debug('database connection established'); + Logger::debug('Database connection established'); $this->available = true; - return true; } /** @@ -221,7 +198,24 @@ class Database { return new Statement( $sql,$this->client,$this->conf); } - + + + private function executeSystemCommand( $cmd ) + { + $ausgabe = array(); + $rc = false; + + Logger::debug("Database command executing: " . $this->conf['cmd']); + exec($cmd, $ausgabe, $rc); + + foreach ($ausgabe as $zeile) + Logger::debug("Database command output: " . $zeile); + + if ($rc != 0) { + throw new RuntimeException('Command failed: ' . implode("", $ausgabe)); + } + } + } diff --git a/modules/database/Statement.class.php b/modules/database/Statement.class.php @@ -17,6 +17,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. namespace database; +use database\driver\PDODriver; use Exception; use RuntimeException; @@ -38,9 +39,8 @@ class Statement /** * Client. - * Enth�lt ein Objekt der Klasse db_<type>. * - * @var Object + * @var PDODriver */ var $client; @@ -52,6 +52,12 @@ class Statement var $conf; /** + * Statement + * @var \PDOStatement + */ + var $stmt; + + /** * Statement constructor. * @param $sql string Sql * @param $client Database @@ -67,12 +73,9 @@ class Statement $sql = str_replace('}}',$conf['suffix'],$sql); $this->sql = new Sql( $sql ); - - // Vorbereitete Datenbankabfrage ("Prepared Statement") - $this->client->clear(); - + // Statement an die Datenbank schicken - $this->client->prepare( $this->sql->query,$this->sql->param ); + $this->stmt = $this->client->prepare( $this->sql->query,$this->sql->param ); } @@ -96,7 +99,7 @@ class Statement public function execute( ) { // Ausfuehren... - $result = $this->client->query($this->sql); + $result = $this->client->query($this->stmt, $this->sql); if ( $result === FALSE ) { @@ -118,8 +121,7 @@ class Statement $none = ''; $result = $this->query(); - $row = $this->client->fetchRow( $result,0 ); - $this->client->freeResult($result); + $row = $this->client->fetchRow( $this->stmt, $result,0 ); if ( ! is_array($row) ) return $none; @@ -139,8 +141,7 @@ class Statement { $result = $this->query(); - $row = $this->client->fetchRow( $result,0 ); - $this->client->freeResult($result); + $row = $this->client->fetchRow( $this->stmt, $result,0 ); if ( ! is_array($row) ) $row = array(); @@ -160,7 +161,7 @@ class Statement $i = 0; $col = array(); - while( $row = $this->client->fetchRow( $result,$i++ ) ) + while( $row = $this->client->fetchRow( $this->stmt, $result,$i++ ) ) { if ( empty($row) ) break; @@ -169,8 +170,6 @@ class Statement $col[] = $row[ $keys[0] ]; } - $this->client->freeResult($result); - return $col; } @@ -189,7 +188,7 @@ class Statement $i = 0; - while( $row = $this->client->fetchRow( $result,$i++ ) ) + while( $row = $this->client->fetchRow( $this->stmt, $result,$i++ ) ) { if ( empty($row) ) break; @@ -211,8 +210,6 @@ class Statement } } - $this->client->freeResult( $result ); - return $results; } @@ -229,13 +226,11 @@ class Statement $results = array(); $i = 0; - while( $row = $this->client->fetchRow( $result,$i++ ) ) + while( $row = $this->client->fetchRow( $this->stmt, $result,$i++ ) ) { $results[] = $row; } - $this->client->freeResult( $result ); - return $results; } @@ -266,7 +261,7 @@ class Statement */ function setInt( $name,$value ) { - $this->client->bind( $name, (int)$value ); + $this->client->bind( $this->stmt, $name, (int)$value ); } @@ -279,7 +274,7 @@ class Statement */ function setString( $name,$value ) { - $this->client->bind( $name, (string)$value ); + $this->client->bind( $this->stmt, $name, (string)$value ); } @@ -309,7 +304,7 @@ class Statement */ function setNull( $name ) { - $this->client->bind( $name, null ); + $this->client->bind( $this->stmt, $name, null ); } diff --git a/modules/database/driver/PDODriver.class.php b/modules/database/driver/PDODriver.class.php @@ -20,6 +20,7 @@ // namespace database\driver; +use database\Sql; use \Logger; use \PDO; use \PDOException; @@ -40,19 +41,8 @@ class PDODriver * * @var PDO */ - var $connection; + private $connection; - /** - * Datenbank-Fehler. - * - * @var String - */ - var $error; - - var $lowercase = false; - - var $params; - /** * @var PDOStatement @@ -125,7 +115,7 @@ class PDODriver * * @return bool */ - function disconnect() + public function disconnect() { // There is no disconnection-function. // So the GC will call the finalize-method of the connection object. @@ -136,59 +126,39 @@ class PDODriver /** - * @param $query + * @param $stmt PDOStatement + * @param $query Sql * @return PDOStatement */ - function query($query) + public function query($stmt,$query) { - $erg = $this->stmt->execute(); + $erg = $stmt->execute(); if ( $erg === false ) { - throw new RuntimeException( 'Could not execute prepared statement "'.$query->query.'": '.implode('/',$this->stmt->errorInfo()) ); + throw new RuntimeException( 'Could not execute prepared statement "'.$query->query.'": '.implode('/',$stmt->errorInfo()) ); } - return $this->stmt; + return $stmt; } - function fetchRow( $result, $rownum ) + /** + * @param $stmt PDOStatement + * @param $result + * @param $rownum + * @return mixed Row + */ + public function fetchRow( $stmt, $result, $rownum ) { - $row = $this->stmt->fetch( PDO::FETCH_ASSOC ); - -/* - * + $row = $stmt->fetch( PDO::FETCH_ASSOC ); - - if(intval(@$row['id'])==1) - { - echo "Hallo:"; - -// echo "\n";print_r($row)."\n"; - var_dump($row); - echo " ist... ".gettype($row); - - } - - */ - - - if ( is_array($row) && $this->lowercase ) - $row = array_change_key_case($row); - return $row; } - function freeResult($result) - { - return true; - } - - - function prepare( $query,$param) + public function prepare( $query,$param) { - $this->params = $param; $offset = 0; foreach( $param as $name=>$pos) { @@ -199,16 +169,23 @@ class PDODriver $offset = $offset + strlen($name); } - $this->stmt = $this->connection->prepare($query); + $stmt = $this->connection->prepare($query); - if ( $this->stmt === false ) + if ( $stmt === false ) throw new RuntimeException("Could not prepare statement:\n$query\nCause: ".implode(' / ',$this->connection->errorInfo()) ); - + + return $stmt; } - - - function bind( $param,$value ) + + /** + * Binding a parameter value. + * + * @param $stmt PDOStatement + * @param $param + * @param $value + */ + public function bind( $stmt,$param,$value ) { $name = ':'.$param; @@ -221,7 +198,7 @@ class PDODriver else throw new RuntimeException( 'Unknown type' ); - $this->stmt->bindValue($name,$value,$type); + $stmt->bindValue($name,$value,$type); } @@ -229,7 +206,7 @@ class PDODriver /** * Startet eine Transaktion. */ - function start() + public function start() { $this->connection->beginTransaction(); } @@ -239,7 +216,7 @@ class PDODriver /** * Beendet eine Transaktion. */ - function commit() + public function commit() { $this->connection->commit(); } @@ -248,7 +225,7 @@ class PDODriver /** * Bricht eine Transaktion ab. */ - function rollback() + public function rollback() { try { @@ -260,16 +237,6 @@ class PDODriver } } - - - /** - * Setzt die letzte Abfrage zurueck. - */ - function clear() - { - $this->params = array(); - } - /** * Why this? See http://e-mats.org/2008/07/fatal-error-exception-thrown-without-a-stack-frame-in-unknown-on-line-0/