Source for file class.DB.php

Documentation is available at class.DB.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * Ermöglicht Datenbankzugriff
  6.  *
  7.  * @author Kay Koch, <kay.koch@gmx.de>
  8.  * @package src
  9.  * @subpackage foundation_db
  10.  */
  11.  
  12. if (version_compare(PHP_VERSION'5')) {
  13.     die('This file was generated for PHP 5');
  14. }
  15.  
  16. /* user defined includes */
  17. // section -64--88-122--2-f41e9:1009a89d5b2:-7fb5-includes begin
  18. // section -64--88-122--2-f41e9:1009a89d5b2:-7fb5-includes end
  19.  
  20. /* user defined constants */
  21. // section -64--88-122--2-f41e9:1009a89d5b2:-7fb5-constants begin
  22. // section -64--88-122--2-f41e9:1009a89d5b2:-7fb5-constants end
  23.  
  24. /**
  25.  * Ermöglicht Datenbankzugriff
  26.  *
  27.  * @access public
  28.  * @author Kay Koch, <kay.koch@gmx.de>
  29.  * @package src
  30.  * @subpackage foundation_db
  31.  */
  32. {
  33.     // --- ATTRIBUTES ---
  34.     
  35.         
  36.     /**
  37.      * Instance der Klasse
  38.      *
  39.      * @access private
  40.      * @var MAIL 
  41.      */
  42.     private static $oInstance null;
  43.     
  44.     /**
  45.      * link_id
  46.      *
  47.      * @access private
  48.      * @var int 
  49.      */
  50.     private $link_id 0;
  51.  
  52.     /**
  53.      * query_id
  54.      *
  55.      * @access private
  56.      * @var int 
  57.      */
  58.     private $query_id 0;
  59.  
  60.     /**
  61.      * Servername
  62.      *
  63.      * @access private
  64.      * @var string 
  65.      */
  66.     private $server '';
  67.  
  68.     /**
  69.      * Benutzername
  70.      *
  71.      * @access private
  72.      * @var string 
  73.      */
  74.     private $user '';
  75.  
  76.     /**
  77.      * Password
  78.      *
  79.      * @access private
  80.      * @var string 
  81.      */
  82.     private $password '';
  83.  
  84.     /**
  85.      * Datenbankname
  86.      *
  87.      * @access private
  88.      * @var string 
  89.      */
  90.     private $database '';
  91.  
  92.     // --- OPERATIONS ---
  93.  
  94.     
  95.     /**
  96.      * verbindet zur Datenbank
  97.      *
  98.      * @access public
  99.      * @author Kay Koch, <kay.koch@gmx.de>
  100.      * @return void 
  101.      */
  102.     public function connect(
  103.     {
  104.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f97 begin
  105.         try {
  106.         $a=1;
  107.            $this->link_id mysql_connect($this->server$this->user$this->password);
  108.             if (!$this->link_id)
  109.                 throw new Exception ("Link-ID == false, connect failed");
  110.             if ($this->database != ""$this->select_db($this->database);
  111.         
  112.         catch (Exception $e{
  113.             $e->errNr mysql_errno();
  114.             src_tools_ERROR::setDBError($e__METHOD__);
  115.         
  116.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f97 end
  117.     }
  118.  
  119.     /**
  120.      * ändert die Datenbank
  121.      *
  122.      * @access public
  123.      * @author Kay Koch, <kay.koch@gmx.de>
  124.      * @param void 
  125.      * @return void 
  126.      */
  127.     public function select_db($database)
  128.     {
  129.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f91 begin
  130.         try {
  131.             if ($database != ""$this->database $database;
  132.             if (!@mysql_select_db($this->database$this->link_id))
  133.                 throw new Exception ("cannot use database " $this->database);
  134.         
  135.         catch (Exception $e{
  136.             $e->errNr mysql_errno($this->link_id);
  137.             src_tools_ERROR::setDBError($e__METHOD__);
  138.         
  139.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f91 end
  140.     }
  141.  
  142.     /**
  143.      * liefert einen link zu einer SQL-Eingabe
  144.      *
  145.      * @access public
  146.      * @author Kay Koch, <kay.koch@gmx.de>
  147.      * @param string 
  148.      * @return recourceID 
  149.      */
  150.     public function query($query_string)
  151.     {
  152.         $returnValue null;
  153.  
  154.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f8e begin
  155.         // $query_string= mysql_real_escape_string($query_string);
  156.         try {
  157.             $this->query_id mysql_query($query_string$this->link_id);
  158.             if (!$this->query_id)
  159.                 throw new Exception ("Invalid SQL: " $query_string);
  160.             $returnValue $this->query_id;
  161.         
  162.         catch (Exception $e{          
  163.             $e->errNr mysql_errno($this->link_id);
  164.             src_tools_ERROR::setDBError($e__METHOD__);
  165.         
  166.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f8e end
  167.  
  168.         return $returnValue;
  169.     }
  170.  
  171.     /**
  172.      * liefert Array aus link_id
  173.      *
  174.      * @access public
  175.      * @author Kay Koch, <kay.koch@gmx.de>
  176.      * @param recourceID 
  177.      * @param string 
  178.      * @return src_foundation_db_DB 
  179.      */
  180.     public function fetch_array($query_id$type MYSQL_BOTH)
  181.     {
  182.         $returnValue null;
  183.  
  184.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f8c begin
  185.         if ($query_id != -1$this->query_id $query_id;
  186.         $returnValue mysql_fetch_array($this->query_id$type)
  187.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f8c end
  188.  
  189.         return $returnValue;
  190.     }
  191.  
  192.     /**
  193.      * setzt Speicherplatz frei
  194.      *
  195.      * @access public
  196.      * @author Kay Koch, <kay.koch@gmx.de>
  197.      * @param recourceID 
  198.      * @return int 
  199.      */
  200.     public function free_result($query_id)
  201.     {
  202.         $returnValue = (int) 0;
  203.  
  204.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f8a begin
  205.         if ($query_id != -1$this->query_id $query_id;
  206.         $returnValue @mysql_free_result($this->query_id)
  207.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f8a end
  208.  
  209.         return (int) $returnValue;
  210.     }
  211.  
  212.     /**
  213.      * liefert Array für eine SQL-Anfrage mit ener Zeile
  214.      *
  215.      * @access public
  216.      * @author Kay Koch, <kay.koch@gmx.de>
  217.      * @param string 
  218.      * @return array 
  219.      */
  220.     public function query_first($query_string)
  221.     {
  222.         $returnValue array();
  223.  
  224.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f82 begin
  225.         $this->query($query_string);
  226.         $returnValue $this->fetch_array($this->query_id);
  227.         $this->free_result($this->query_id)
  228.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f82 end
  229.  
  230.         return (array) $returnValue;
  231.     }
  232.  
  233.     /**
  234.      * liefert neue auto_inkrement
  235.      *
  236.      * @access public
  237.      * @author Kay Koch, <kay.koch@gmx.de>
  238.      * @return int 
  239.      */
  240.     public function insert_id()
  241.     {
  242.         $returnValue = (int) 0;
  243.  
  244.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f79 begin
  245.         $returnValue mysql_insert_id($this->link_id)
  246.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f79 end
  247.  
  248.         return (int) $returnValue;
  249.     }
  250.  
  251.     /**
  252.      * Konstruktor
  253.      *
  254.      * stellt Verbindung zur Datenbank her
  255.      *
  256.      * @access public
  257.      * @author Kay Koch, <kay.koch@gmx.de>
  258.      * @param array 
  259.      * @return void 
  260.      */
  261.     public function __construct($arrAccess)
  262.     {
  263.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f6c begin
  264.         $this->server $arrAccess[0];
  265.         $this->user $arrAccess[1];
  266.         $this->password $arrAccess[2];
  267.         $this->database $arrAccess[3];
  268.         $this->connect()
  269.         // section -64--88-122--2-f41e9:1009a89d5b2:-7f6c end
  270.     }
  271.  
  272.     /**
  273.      * Destruktor
  274.      *
  275.      * löscht die Verbindung am Ende
  276.      *
  277.      * @access public
  278.      * @author Kay Koch, <kay.koch@gmx.de>
  279.      * @param array 
  280.      * @return void 
  281.      */    
  282.     function __destruct({
  283.       # mysql_close($this->link_id);
  284.        }
  285.  
  286.     /**
  287.      * liefert Feldnamen
  288.      *
  289.      * @access public
  290.      * @author Kay Koch, <kay.koch@gmx.de>
  291.      * @param string 
  292.      * @return array 
  293.      */
  294.     public function getFieldNames($tableName)
  295.     {
  296.         $returnValue array();
  297.  
  298.         // section -64--88-122--2-56db4adb:103a2d2e802:-7fd3 begin
  299.         $result $this->query("SHOW COLUMNS FROM $tableName");
  300.         while ($row $this->fetch_array($resultMYSQL_ASSOC)) {
  301.             $returnValue[$row['Field'];
  302.         
  303.         // section -64--88-122--2-56db4adb:103a2d2e802:-7fd3 end
  304.  
  305.         return (array) $returnValue;
  306.     }
  307.  
  308.     /**
  309.      * optimiert Datenbank
  310.      *
  311.      * @access public
  312.      * @author Kay Koch, <kay.koch@gmx.de>
  313.      * @return void 
  314.      */
  315.     public function optimizeDB()
  316.     {
  317.         // section -64--88-122--2-56db4adb:103a2d2e802:-7fc7 begin
  318.         $sql "OPTIMIZE TABLE ";
  319.         $results $this->query("SHOW tables");
  320.         while ($row $this->fetch_array($resultsMYSQL_NUM)) {
  321.             $sql .= $row[0",";
  322.         
  323.         $this->query(substr($sql0-1))
  324.         // section -64--88-122--2-56db4adb:103a2d2e802:-7fc7 end
  325.     }
  326.  
  327.     /**
  328.      * liefert die Anzahl der Resultate
  329.      *
  330.      * @access public
  331.      * @author Kay Koch, <kay.koch@gmx.de>
  332.      * @param void 
  333.      * @return int 
  334.      */
  335.     public function num_rows($query_id)
  336.     {
  337.         $returnValue = (int) 0;
  338.  
  339.         // section -64--88-0-1--1e6707e3:1048f065750:-7ff3 begin
  340.         if ($query_id != -1$this->query_id $query_id;
  341.         $returnValue mysql_num_rows($this->query_id)
  342.         // section -64--88-0-1--1e6707e3:1048f065750:-7ff3 end
  343.  
  344.         return (int) $returnValue;
  345.     }
  346.     
  347. /* end of class src_foundation_db_DB */
  348.  
  349. ?>

Documentation generated on Sat, 24 Mar 2007 09:59:05 +0100 by phpDocumentor 1.3.1