VCL
[ class tree: VCL ] [ index: VCL ] [ all elements ]

Source for file dbtables.inc.php

Documentation is available at dbtables.inc.php

  1. <?php
  2. /**
  3. *  This file is part of the VCL for PHP project
  4. *
  5. *  Copyright (c) 2004-2007 qadram software <support@qadram.com>
  6. *
  7. *  Checkout AUTHORS file for more information on the developers
  8. *
  9. *  This library is free software; you can redistribute it and/or
  10. *  modify it under the terms of the GNU Lesser General Public
  11. *  License as published by the Free Software Foundation; either
  12. *  version 2.1 of the License, or (at your option) any later version.
  13. *
  14. *  This library is distributed in the hope that it will be useful,
  15. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. *  Lesser General Public License for more details.
  18. *
  19. *  You should have received a copy of the GNU Lesser General Public
  20. *  License along with this library; if not, write to the Free Software
  21. *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  22. *  USA
  23. *
  24. */
  25.  
  26. use_unit("classes.inc.php");
  27. use_unit("controls.inc.php");
  28. use_unit("db.inc.php");
  29. use_unit("rtl.inc.php");
  30. use_unit('adodb/adodb.inc.php');
  31. use_unit('adodb/adodb-exceptions.inc.php');
  32.  
  33. /**
  34.  * Database class
  35.  *
  36.  * A component that represents a database
  37.  */
  38. class Database extends CustomConnection
  39. {
  40.         public $_connection=null;
  41.         protected $_debug=0;
  42.         protected $_drivername="mysql";
  43.         protected $_databasename="";
  44.         protected $_host="";
  45.         protected $_username="";
  46.         protected $_userpassword="";
  47.         protected $_connected=0;
  48.         protected $_dictionary="";
  49.         protected $_dictionaryproperties=false;
  50.  
  51.         function MetaFields($tablename)
  52.         {
  53.             $fd=$this->_connection->MetaColumns($tablename);
  54.             $result=array();
  55.             reset($fd);
  56.             while(list($key$val)=each($fd))
  57.             {
  58.  
  59.                 $result[$val->name]='';
  60.             }
  61.             return($result);
  62.         }
  63.  
  64.         function BeginTrans()
  65.         {
  66.             $this->_connection->StartTrans();
  67.         }
  68.  
  69.         function CompleteTrans($autocomplete=true)
  70.         {
  71.             $this->_connection->CompleteTrans($autocomplete);
  72.         }
  73.  
  74.         function getOnAfterConnect(return $this->readonafterconnect()}
  75.         function setOnAfterConnect($value$this->writeonafterconnect($value)}
  76.  
  77.         function getOnBeforeConnect(return $this->readonbeforeconnect()}
  78.         function setOnBeforeConnect($value$this->writeonbeforeconnect($value)}
  79.  
  80.         function getOnAfterDisconnect(return $this->readonafterdisconnect()}
  81.         function setOnAfterDisconnect($value$this->writeonafterdisconnect($value)}
  82.  
  83.         function getOnBeforeDisconnect(return $this->readonbeforedisconnect()}
  84.         function setOnBeforeDisconnect($value$this->writeonbeforedisconnect($value)}
  85.  
  86.  
  87.         function Prepare($query)
  88.         {
  89.             $this->_connection->Prepare($query);
  90.         }
  91.  
  92.         function PrepareSP($query)
  93.         {
  94.             $this->_connection->PrepareSP($query);
  95.         }
  96.  
  97.         function DBDate($input)
  98.         {
  99.             return($this->_connection->DBDate($input));
  100.         }
  101.  
  102.         function Param($input)
  103.         {
  104.             return($this->_connection->Param($input));
  105.         }
  106.  
  107.         function QuoteStr($input)
  108.         {
  109.             return($this->_connection->qstr($input));
  110.         }
  111.  
  112.         function __construct($aowner=null)
  113.         {
  114.                 //Calls inherited constructor
  115.                 parent::__construct($aowner);
  116.         }
  117.  
  118.         /**
  119.         * Specifies if the database is connected or not
  120.         * @return boolean 
  121.         */
  122.         function readConnected(return ($this->_connection!=null)}
  123.         function getConnected(return $this->readconnected()}
  124.         function setConnected($value$this->writeconnected($value)}
  125.  
  126.         /**
  127.         * Specifies if the database is in debug mode or not
  128.         * @return boolean 
  129.         */
  130.         function getDebug(return $this->_debug}
  131.         function setDebug($value$this->_debug=$value}
  132.         function defaultDebug(return 0}
  133.  
  134.         /**
  135.         * Specifies the type of database you want to access
  136.         * @return string 
  137.         */
  138.         function getDriverName(return $this->_drivername;   }
  139.         function setDriverName($value$this->_drivername=$value}
  140.  
  141.         /**
  142.         * Specifies the dictionary table you want to use
  143.         * @return string 
  144.         */
  145.         function getDictionary(return $this->_dictionary;   }
  146.         function setDictionary($value$this->_dictionary=$value}
  147.  
  148.         /**
  149.         * Specifies the name of the database you want to access
  150.         * @return string 
  151.         */
  152.         function getDatabaseName(return $this->_databasename;       }
  153.         function setDatabaseName($value$this->_databasename=$value}
  154.  
  155.         /**
  156.         * Specifies the host where your database resides
  157.         * @return string 
  158.         */
  159.         function getHost(return $this->_host;       }
  160.         function setHost($value$this->_host=$value}
  161.  
  162.         /**
  163.         * Specifies the username used to acces the database
  164.         * @return string 
  165.         */
  166.         function getUserName(return $this->_username;       }
  167.         function setUserName($value$this->_username=$value}
  168.  
  169.         /**
  170.         * Specifies the password to access the database
  171.         * @return string 
  172.         */
  173.         function getUserPassword(return $this->_userpassword;       }
  174.         function setUserPassword($value$this->_userpassword=$value}
  175.  
  176.         /**
  177.          * Executes a query
  178.          *
  179.          * @param string $query  Query to execute
  180.          * @return object 
  181.          */
  182.         function execute($query,$params=array())
  183.         {
  184.                 $this->open();
  185.                 $rs=$this->_connection->Execute($query,$params);
  186.                 if ($rs==null)
  187.                 {
  188.                         DatabaseError("Error executing query$query [".$this->_connection->ErrorMsg()."]");
  189.                 }
  190.                 return($rs);
  191.         }
  192.  
  193.         /**
  194.          * Executes a limited query
  195.          *
  196.          * @param string $query SQL sentence to execute
  197.          * @param integer $numrows Numrows to retrieve
  198.          * @param integer $offset Starting row to retrieve
  199.          * @return object 
  200.          */
  201.         function executelimit($query,$numrows,$offset)
  202.         {
  203.                 $this->open();
  204.                 $rs=$this->_connection->SelectLimit($query,$numrows,$offset);
  205.                 if ($rs==null)
  206.                 {
  207.                         DatabaseError("Error executing query$query [".$this->_connection->ErrorMsg()."]");
  208.                 }
  209.                 return($rs);
  210.         }
  211.  
  212.         function DoConnect()
  213.         {
  214.             if (($this->ControlState csDesigning)!=csDesigning)
  215.             {
  216.                 global $ADODB_FETCH_MODE;
  217.                 $ADODB_FETCH_MODE=ADODB_FETCH_ASSOC;
  218.  
  219.                 try
  220.                 {
  221.                     $this->_connection = ADONewConnection($this->DriverName);
  222.  
  223.                     $this->_connection->debug=$this->Debug;
  224.  
  225.                     if (($this->DriverName=='borland_ibase'|| ($this->DriverName=='ibase'))
  226.                     {
  227.                         $result=$this->_connection->PConnect($this->DatabaseName,$this->UserName,$this->UserPassword);
  228.                     }
  229.                     else
  230.                     {
  231.                         $result=$this->_connection->Connect($this->Host,$this->UserName,$this->UserPassword,$this->DatabaseName);
  232.                     }
  233.                 }
  234.                 catch (Exception $e)
  235.                 {
  236.                     DatabaseError("Cannot connect to database server");
  237.                 }
  238.             }
  239.         }
  240.  
  241.         function DoDisconnect()
  242.         {
  243.                 if ($this->_connection!=null)
  244.                 {
  245.                         $this->_connection->Close();
  246.                         $this->_connection=null;
  247.                 }
  248.         }
  249.  
  250.         /**
  251.          * Return properties for a field
  252.          *
  253.          * @param string $table 
  254.          * @param string $field 
  255.          * @return array 
  256.          */
  257.         function readFieldDictionaryProperties($table$field)
  258.         {
  259.                 $table=trim($table);
  260.                 $field=trim($field);
  261.                 $result=false;
  262.                 if ($this->_connection!=null)
  263.                 {
  264.                         if ($this->_dictionary!='')
  265.                         {
  266.                                 global $ADODB_FETCH_MODE;
  267.                                 $ADODB_FETCH_MODE=ADODB_FETCH_ASSOC;
  268.  
  269.                                 $q="select * from $this->_dictionary where dict_tablename='$tableand dict_fieldname='$field'";
  270.                                 $r=$this->execute($q);
  271.                                 $props=array();
  272.                                 while ($r->fetchInto($arow))
  273.                                 {
  274.                                         $row=array();
  275.                                         reset($arow);
  276.                                         while (list($k,$v)=each($arow))
  277.                                         {
  278.                                                 $row[strtolower($k)]=$v;
  279.                                         }
  280.  
  281.                                         $props[$row['dict_property']]=array($row['dict_value1'],$row['dict_value2']);
  282.                                 }
  283.                                 if (!empty($props)) $result=$props;
  284.                         }
  285.                         else
  286.                         {
  287.                                 if ($this->_dictionaryproperties!=false)
  288.                                 {
  289.                                         $result=$this->_dictionaryproperties[$table][$field];
  290.                                 }
  291.                         }
  292.                 }
  293.                 return($result);
  294.         }
  295.  
  296.  
  297.         /**
  298.          * Return indexes for a table
  299.          *
  300.          * @param string $table 
  301.          * @param boolean $primary 
  302.          * @return array 
  303.          */
  304.         function &extractIndexes($table$primary FALSE)
  305.         {
  306.                 return($this->_connection->MetaIndexes($table,$primary));
  307.         }
  308.  
  309.         /**
  310.          * Return an array with dictionary properties
  311.          *
  312.          * @return array 
  313.          */
  314.         function readDictionaryProperties(return $this->_dictionaryproperties;   }
  315.         function writeDictionaryProperties($value$this->_dictionaryproperties=$value}
  316.  
  317.  
  318.         /**
  319.          * Creates the dictionary table on the database
  320.          *
  321.          * @return boolean 
  322.          */
  323.         function createDictionaryTable()
  324.         {
  325.                 $result=false;
  326.                 if ($this->_connection!=null)
  327.                 {
  328.                         if ($this->_dictionary!='')
  329.                         {
  330.                                 if ($this->_drivername=='borland_ibase')
  331.                                 {
  332.                                         $q="CREATE TABLE $this->_dictionary (\n";
  333.                                         $q.="  DICT_ID INTEGER NOT NULL,\n";
  334.                                         $q.="  DICT_TABLENAME VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  335.                                         $q.="  DICT_FIELDNAME VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  336.                                         $q.="  DICT_PROPERTY VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  337.                                         $q.="  DICT_VALUE1 VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  338.                                         $q.="  DICT_VALUE2 VARCHAR(200) CHARACTER SET NONE COLLATE NONE);\n";
  339.                                         $this->execute($q);
  340.  
  341.                                         $q="ALTER TABLE $this->_dictionary ADD PRIMARY KEY (DICT_ID);\n";
  342.                                         $this->execute($q);
  343.  
  344.                                         $result=true;
  345.                                 }
  346.                                 else
  347.                                 {
  348.                                         $q="CREATE TABLE $this->_dictionary (";
  349.                                         $q.="  `dict_id` int(11) unsigned NOT NULL auto_increment,";
  350.                                         $q.="  `dict_tablename` varchar(60) NULL,";
  351.                                         $q.="  `dict_fieldname` varchar(60) NULL,";
  352.                                         $q.="  `dict_property` varchar(60) NULL,";
  353.                                         $q.="  `dict_value1` varchar(60) NULL,";
  354.                                         $q.="  `dict_value2` text NULL,";
  355.                                         $q.="  PRIMARY KEY (`dict_id`)";
  356.                                         $q.=");";
  357.                                         $this->execute($q);
  358.  
  359.                                         $result=true;
  360.                                 }
  361.                         }
  362.                 }
  363.                 return($result);
  364.         }
  365.  
  366.         /*
  367.         private $_debug=0;
  368.  
  369.         function getDebug() { return $this->_debug; }
  370.         function setDebug($value) { $this->_debug=$value; }
  371.         function defaultDebug() { return 0; }
  372.  
  373.         //DriverName property
  374.         function getDriverName() { return $this->_drivername;   }
  375.         function setDriverName($value) { $this->_drivername=$value; }
  376.  
  377.         //Dictionary property
  378.         function getDictionary() { return $this->_dictionary;   }
  379.         function setDictionary($value) { $this->_dictionary=$value; }
  380.  
  381.         //DatabaseName property
  382.         function getDatabaseName() { return $this->_databasename;       }
  383.         function setDatabaseName($value) { $this->_databasename=$value; }
  384.  
  385.         //Host property
  386.         function getHost() { return $this->_host;       }
  387.         function setHost($value) { $this->_host=$value; }
  388.  
  389.         //User property
  390.         function getUser() { return $this->_user;       }
  391.         function setUser($value) { $this->_user=$value; }
  392.  
  393.         //Password property
  394.         function getPassword() { return $this->_password;       }
  395.         function setPassword($value) { $this->_password=$value; }
  396.  
  397.         //Connected property
  398.         function getConnected() { return $this->_connected;     }
  399.         function setConnected($value)
  400.         {
  401.                 if ($value!=$this->_connected)
  402.                 {
  403.                         $this->_connected=$value;
  404.  
  405.                         if (($this->ControlState & csLoading)!=csLoading)
  406.                         {
  407.                                 if ($this->_connected) $this->open();
  408.                                 else $this->close();
  409.                         }
  410.           }
  411.         }
  412.  
  413.         function loaded()
  414.         {
  415.                 parent::loaded();
  416.  
  417.                 if ($this->_connected) $this->open();
  418.                 else $this->close();
  419.         }
  420.         */
  421.  
  422.         /**
  423.          * Opens the database
  424.          *
  425.          */
  426.          /*
  427.         function open()
  428.         {
  429.                 if (($this->ControlState & csDesigning)!=csDesigning)
  430.                 {
  431.                                 global $ADODB_FETCH_MODE;
  432.                                 $ADODB_FETCH_MODE=ADODB_FETCH_ASSOC;
  433.  
  434.                         $this->_connection = ADONewConnection($this->DriverName);
  435.  
  436.                         $this->_connection->debug=$this->Debug;
  437.  
  438.                         if (($this->DriverName=='borland_ibase') || ($this->DriverName=='ibase'))
  439.                         {
  440.                                 $result=$this->_connection->PConnect($this->DatabaseName,$this->User,$this->Password);
  441.                         }
  442.                         else
  443.                         {
  444.                                 $result=$this->_connection->Connect($this->Host,$this->User,$this->Password,$this->DatabaseName);
  445.                         }
  446.                 }
  447.         }
  448.         */
  449.  
  450.         /**
  451.          * Return all the databases using the connection information
  452.          *
  453.          * @return array 
  454.          */
  455.          /*
  456.         function databases()
  457.         {
  458.                 if ($this->_connection==null)
  459.                 {
  460.                         $this->open();
  461.                 }
  462.  
  463.                return($this->_connection->MetaDatabases());
  464.         }
  465.         */
  466.  
  467.         /**
  468.          * Return tables on this database
  469.          *
  470.          * @return array 
  471.          */
  472.         function tables()
  473.         {
  474.                 if ($this->_connection==null)
  475.                 {
  476.                         $this->open();
  477.                 }
  478.                 return($this->_connection->MetaTables());
  479.         }
  480.  
  481.         /**
  482.          * Closes the database
  483.          *
  484.          */
  485.          /*
  486.         function close()
  487.         {
  488.                 if ($this->_connection!=null)
  489.                 {
  490.                         $this->_connection->Close();
  491.                         $this->_connection=null;
  492.                 }
  493.         }
  494.         */
  495.  
  496.         /**
  497.          * Executes a query
  498.          *
  499.          * @param string $query 
  500.          * @return object 
  501.          */
  502.          /*
  503.         function execute($query)
  504.         {
  505.                 $this->open();
  506. //                $this->_connection->debug=true;
  507.                 $rs=$this->_connection->Execute($query);
  508.                 if ($rs==null)
  509.                 {
  510.                         throw new Exception("Query error: $query ".$this->_connection->ErrorMsg());
  511.                 }
  512.                 return($rs);
  513.         }
  514.  
  515.         function dumpContents()
  516.         {
  517.                 //Dump here connection code if any
  518.         }
  519.         */
  520. }
  521.  
  522. /**
  523.  * DBDataSet class
  524.  *
  525.  * Base Class for datasets linked to a database
  526.  */
  527. class DBDataSet extends DataSet
  528. {
  529.     public $_rs=null;
  530.     protected $_database=null;
  531.     protected $_params=array();
  532.  
  533.     /**
  534.     * Specifies the database object to be used to connect to the server
  535.     *
  536.     * @return CustomConnection 
  537.     */
  538.     function readDatabase(return $this->_database}
  539.     function writeDatabase($value$this->_database=$this->fixupProperty($value)}
  540.     function defaultDatabase(return null}
  541.  
  542.     function loaded()
  543.     {
  544.         $this->writeDatabase($this->_database);
  545.         parent::loaded();
  546.     }
  547.  
  548.     /**
  549.     * Field array (name=>value) of the current record
  550.     *
  551.     * @return array 
  552.     */
  553.     function readFields()
  554.     {
  555.         return($this->_rs->fields);
  556.     }
  557.  
  558.     /**
  559.     * Number of fields on the current record
  560.     *
  561.     * @return integer 
  562.     */
  563.     function readFieldCount(return count($this->_rs->_numOfFields)}
  564.  
  565.     /**
  566.     * Number of records in the dataset
  567.     *
  568.     * @return integer 
  569.     */
  570.     function readRecordCount()
  571.     {
  572.         if (assigned($this->_rs))
  573.         {
  574.             return($this->_rs->RecordCount());
  575.         }
  576.         else return(parent::readRecordCount());
  577.     }
  578.  
  579.     function MoveBy($distance)
  580.     {
  581.         parent::MoveBy($distance);
  582.         for($i=0;$i<=$distance-1;$i++)
  583.         {
  584.             if (!$this->_rs->EOF)
  585.             {
  586.                 $this->_rs->MoveNext();
  587.             }
  588.         }
  589.     }
  590.  
  591.     /**
  592.     * If true, the pointer is at the end of the dataset
  593.     *
  594.     * @return boolean 
  595.     */
  596.     function readEOF()
  597.     {
  598.         return($this->_rs->EOF);
  599.     }
  600.  
  601.     function CheckDatabase()
  602.     {
  603.         if (!is_object($this->_database)) DatabaseError(_("No Database assigned or is not an object"));
  604.     }
  605.  
  606.     public $_keyfields=array();
  607.  
  608.     function InternalClose()
  609.     {
  610.         $this->_rs=null;
  611.     }
  612.  
  613.     function InternalOpen()
  614.     {
  615.         if (($this->ControlState csDesigning)!=csDesigning)
  616.         {
  617.             $query=$this->buildQuery();
  618.             if (trim($query)==''DatabaseError(_("Missing query to execute"));
  619.             $this->CheckDatabase();
  620.  
  621.             if ((trim($this->_limitstart)=='-1'&& (trim($this->_limitcount)=='-1'))
  622.             {
  623.                 $this->_rs=$this->Database->Execute($query,$this->_params);
  624.             }
  625.             else
  626.             {
  627.                 $limitstart=trim($this->_limitstart);
  628.                 if ($limitstart==''$limitstart=-1;
  629.  
  630.                 $limitcount=trim($this->_limitcount);
  631.                 if ($limitcount==''$limitcount=-1;
  632.                 $this->_rs=$this->Database->ExecuteLimit($query,$limitcount,$limitstart);
  633.             }
  634.  
  635.             $this->_keyfields=$this->readKeyFields();
  636.             if (!is_array($this->_rs->fields))
  637.             {
  638.                 if ($this->_tablename!='')
  639.                 {
  640.                     $fd=$this->Database->MetaFields($this->_tablename);
  641.                     $this->_rs->fields=$fd;
  642.                 }
  643.             }
  644.             $this->fieldbuffer=$this->_rs->fields;
  645.  
  646.         }
  647.     }
  648.  
  649.     function InternalFirst()
  650.     {
  651.         $this->_rs->MoveFirst();
  652.  
  653.             if (!is_array($this->_rs->Fields))
  654.             {
  655.                 if ($this->_tablename!='')
  656.                 {
  657.                     $fd=$this->Database->MetaFields($this->_tablename);
  658.                     $this->_rs->Fields=$fd;
  659.                 }
  660.             }
  661.     }
  662.  
  663.     function InternalLast()
  664.     {
  665.         $this->_rs->MoveLast();
  666.  
  667.             if (!is_array($this->_rs->Fields))
  668.             {
  669.                 if ($this->_tablename!='')
  670.                 {
  671.                     $fd=$this->Database->MetaFields($this->_tablename);
  672.                     $this->_rs->Fields=$fd;
  673.                 }
  674.             }
  675.     }
  676.  
  677.     /**
  678.     * Overriden to allow get field values as properties
  679.     *
  680.     * @param string $nm 
  681.     * @return mixed 
  682.     */
  683.     function __get($nm)
  684.     {
  685.         if ($this->_rs!=null)
  686.         {
  687.             if ($this->Active)
  688.             {
  689.                 if ((is_array($this->_rs->fields)) && (array_key_exists($nm,$this->_rs->fields)))
  690.                 {
  691.                     if ($this->State==dsBrowse)
  692.                     {
  693.                         return ($this->_rs->fields[$nm]);
  694.                     }
  695.                     else if (array_key_exists($nm,$this->fieldbuffer))
  696.                     {
  697.                         return($this->fieldbuffer[$nm]);
  698.                     }
  699.                     else return('');
  700.                 }
  701.                 else if ((is_array($this->fieldbuffer)) && (array_key_exists($nm,$this->fieldbuffer)))
  702.                 {
  703.                     return($this->fieldbuffer[$nm]);
  704.                 }
  705.                 else return(parent::__get($nm));
  706.  
  707.             }
  708.             else
  709.             {
  710.                 return(parent::__get($nm));
  711.             }
  712.          }
  713.          else
  714.          {
  715.             return(parent::__get($nm));
  716.          }
  717.     }
  718.  
  719.     /**
  720.     * Overriden to allow get field values as properties
  721.     *
  722.     * @param string $nm 
  723.     * @return mixed 
  724.     */
  725.     function __set($nm,$val)
  726.     {
  727.         if ($this->_rs!=null)
  728.         {
  729.             if ($this->Active)
  730.             {
  731.                 if ((is_array(($this->_rs->fields))) && (array_key_exists($nm,$this->_rs->fields)))
  732.                 {
  733.                     $this->fieldbuffer[$nm]=$val;
  734.                     $this->Modified=true;
  735.                     if ($this->State==dsBrowse)
  736.                     {
  737.                         $this->State=dsEdit;
  738.                     }
  739.                 }
  740.                 else parent::__set($nm$val);
  741.             }
  742.             else
  743.             {
  744.                 parent::__set($nm$val);
  745.             }
  746.          }
  747.          else
  748.          {
  749.             parent::__set($nm$val);
  750.          }
  751.     }
  752.  
  753.  
  754. }
  755.  
  756. /**
  757.  * Table class
  758.  *
  759.  * A class to encapsulate a database table
  760.  */
  761. class CustomTable extends DBDataSet
  762. {
  763.         protected $_tablename="";
  764.  
  765.         /**
  766.         * Name of the table you want to access
  767.         *
  768.         * @return string 
  769.         */
  770.         function readTableName(return $this->_tablename}
  771.         function writeTableName($value$this->_tablename=$value}
  772.         function defaultTableName(return ""}
  773.  
  774.         function InternalDelete()
  775.         {
  776.                 $where='';
  777.                 reset($this->_keyfields);
  778.                 while(list($key$fname)=each($this->_keyfields))
  779.                 {
  780.                     $val=$this->fieldbuffer[$fname];
  781.                     if (trim($val)==''continue;
  782.                     if ($where!=''$where.=" and ";
  783.                     $where.=" $fname = ".$this->Database->QuoteStr($val);
  784.                 }
  785.  
  786.             if ($where!='')
  787.             {
  788.                 $query="delete from $this->TableName where $where";
  789.                 $this->Database->Execute($query);
  790.             }
  791.  
  792.         }
  793.  
  794.         /**
  795.          * Get field properties
  796.          *
  797.          * @param string $fieldname 
  798.          * @return mixed 
  799.          */
  800.         function readFieldProperties($fieldname)
  801.         {
  802.                 if ($this->_database!=null)
  803.                 {
  804.                         return($this->_database->readFieldDictionaryProperties($this->_tablename,$fieldname));
  805.                 }
  806.                 else return(false);
  807.         }
  808.  
  809.         function InternalPost()
  810.         {
  811.             if ($this->State == dsEdit)
  812.             {
  813.                 $where='';
  814.                 $buffer=$this->fieldbuffer;
  815.                 reset($this->_keyfields);
  816.                 while(list($key$fname)=each($this->_keyfields))
  817.                 {
  818.                     $val=$this->fieldbuffer[$fname];
  819.                     unset($buffer[$fname]);
  820.                     if (trim($val)==''continue;
  821.                     if ($where!=''$where.=" and ";
  822.                     $where.=" $fname = ".$this->Database->QuoteStr($val);
  823.                 }
  824.  
  825.  
  826.                 try
  827.                 {
  828.                     $updateSQL $this->Database->_connection->AutoExecute($this->TableName$buffer'UPDATE'$where);
  829.                     $this->_rs->fields=array_merge($this->_rs->fields,$this->fieldbuffer);
  830.                 }
  831.                 catch (Exception $e)
  832.                 {
  833.                     $this->_rs->fields=array_merge($this->_rs->fields,$this->fieldbuffer);
  834.                     throw $e;
  835.                 }
  836.  
  837.                 //TODO: Handle errors
  838.             }
  839.             else
  840.             {
  841.                 $where='';
  842.                 if (is_array($this->_keyfields))
  843.                 {
  844.                     reset($this->_keyfields);
  845.                     while(list($key$fname)=each($this->_keyfields))
  846.                     {
  847.                         unset($this->fieldbuffer[$fname]);
  848.                     }
  849.                 }
  850.  
  851.                 $insertSQL $this->Database->_connection->AutoExecute($this->TableName$this->fieldbuffer'INSERT');
  852.  
  853.                 $this->_rs->fields=array_merge($this->_rs->fields,$this->fieldbuffer);
  854.                 //TODO: Handle errors
  855.             }
  856.         }
  857.  
  858.         /**
  859.          * Builds the query to execute
  860.          *
  861.          * @return string 
  862.          */
  863.         function buildQuery()
  864.         {
  865.             if (($this->ControlState csDesigning)!=csDesigning)
  866.             {
  867.                 if (trim($this->_tablename)=='')
  868.                 {
  869.                     if ($this->Active)
  870.                     {
  871.                         DatabaseError(_("Missing TableName property"));
  872.                     }
  873.                 }
  874.                 $result="select * from $this->_tablename";
  875.                 $where="";
  876.                 if ($this->Filter!=""$where.=" $this->Filter ";
  877.                 if ($this->MasterSource!="")
  878.                 {
  879.                     $this->writeMasterSource($this->_mastersource);
  880.                     if (is_object($this->_mastersource))
  881.                     {
  882.                         if (is_array($this->_masterfields))
  883.                         {
  884.                             $ms="";
  885.                             reset($this->_masterfields);
  886.  
  887.                             while(list($key$val)=each($this->_masterfields))
  888.                             {
  889.                                 $thisfield=$key;
  890.                                 $msfield=$val;
  891.  
  892.                                 if ($ms!=""$ms.=" and ";
  893.                                 $ms.=" $thisfield=".$this->Database->QuoteStr($this->_mastersource->DataSet->$msfield)." ";
  894.                             }
  895.  
  896.                             if ($ms!="")
  897.                             {
  898.                                 if ($where!=""$where.=" and ";
  899.                                 $where.=" ($ms";
  900.                             }
  901.                         }
  902.                     }
  903.                 }
  904.  
  905.                 if ($where!=""$result.=" where $where ";
  906.                 return($result);
  907.             }
  908.             else return('');
  909.         }
  910.  
  911.           /**
  912.            * Return an array containg the row values
  913.            *
  914.            * @return array 
  915.            */
  916.   function readAssociativeFieldValues()
  917.   {
  918.         $result=array();
  919.  
  920.         if ($this->Active)
  921.         {
  922.                 return($this->_rs->fields);
  923.         }
  924.  
  925.         return($result);
  926.   }
  927.  
  928.         /**
  929.         * Return an array with Key fields for the table
  930.         *
  931.         * @return array 
  932.         */
  933.         function readKeyFields()
  934.         {
  935.                 //TODO: Check here for another indexes
  936.                 $result="";
  937.                 if ($this->_tablename!='')
  938.                 {
  939.                 $indexes=$this->Database->extractIndexes($this->_tablename,true);
  940.  
  941.                 if (is_array($indexes))
  942.                 {
  943.                     list(,$primary)=each($indexes);
  944.  
  945.                     $result=$primary['columns'];
  946.                     if (is_array($result))
  947.                     {
  948.                         while (list($k,$v)=each($result))
  949.                         {
  950.                                 $result[$k]=trim($v);
  951.                         }
  952.                     }
  953.                 }
  954.                 }
  955.                 return($result);
  956.         }
  957.  
  958.         /**
  959.          * Dump hidden html fields with the key fields of this dataset
  960.          *
  961.          * @param string $basename Prefix to be used to generate hidden key field names
  962.          * @param array $values Array with the values to generate
  963.          */
  964.         function dumpHiddenKeyFields($basename$values=array())
  965.         {
  966.                 $keyfields=$this->readKeyFields();
  967.  
  968.                 if (empty($values))
  969.                 {
  970.                         $values=$this->readAssociativeFieldValues();
  971.                 }
  972.  
  973.                 if (is_array($keyfields))
  974.                 {
  975.                     reset($keyfields);
  976.                     while (list($k,$v)=each($keyfields))
  977.                     {
  978.                             echo "<input type=\"hidden\" name=\"".$basename."[$v]\" value=\"$values[$v]\" />";
  979.                     }
  980.                 }
  981.         }
  982.  
  983.     protected $_orderfield="";
  984.  
  985.         /**
  986.          * Specifies the field to order this table for
  987.          *
  988.          * @return string 
  989.          */
  990.     function readOrderField(return $this->_orderfield}
  991.     function writeOrderField($value$this->_orderfield=$value}
  992.     function defaultOrderField(return ""}
  993.  
  994.     protected $_order="asc";
  995.  
  996.         /**
  997.          * Specifies the ordering of the query, ascendant or descendant
  998.          *
  999.          * @return string 
  1000.          */
  1001.     function readOrder(return $this->_order}
  1002.     function writeOrder($value$this->_order=$value}
  1003.     function defaultOrder(return "asc"}
  1004. }
  1005.  
  1006. /**
  1007.  * Table class
  1008.  *
  1009.  * A class to encapsulate a database table
  1010.  */
  1011. class Table extends CustomTable
  1012. {
  1013.         function getMasterSource(return $this->readmastersource()}
  1014.         function setMasterSource($value$this->writemastersource($value)}
  1015.  
  1016.         function getMasterFields(return $this->readmasterfields()}
  1017.         function setMasterFields($value$this->writemasterfields($value)}
  1018.  
  1019.         function getTableName(return $this->readtablename()}
  1020.         function setTableName($value$this->writetablename($value)}
  1021.  
  1022.         function getActive(return $this->readactive()}
  1023.         function setActive($value$this->writeactive($value)}
  1024.  
  1025.         function getDatabase(return $this->readdatabase()}
  1026.         function setDatabase($value$this->writedatabase($value)}
  1027.  
  1028.         function getFilter(return $this->readfilter()}
  1029.         function setFilter($value$this->writefilter($value)}
  1030.  
  1031.         function getOrderField(return $this->readorderfield()}
  1032.         function setOrderField($value$this->writeorderfield($value)}
  1033.  
  1034.         function getOrder(return $this->readorder()}
  1035.         function setOrder($value$this->writeorder($value)}
  1036.  
  1037.  
  1038.     function getOnBeforeOpen(return $this->readonbeforeopen()}
  1039.     function setOnBeforeOpen($value$this->writeonbeforeopen($value)}
  1040.  
  1041.     function getOnAfterOpen(return $this->readonafteropen()}
  1042.     function setOnAfterOpen($value$this->writeonafteropen($value)}
  1043.  
  1044.     function getOnBeforeClose(return $this->readonbeforeclose()}
  1045.     function setOnBeforeClose($value$this->writeonbeforeclose($value)}
  1046.  
  1047.  
  1048.     function getOnAfterClose(return $this->readonafterclose()}
  1049.     function setOnAfterClose($value$this->writeonafterclose($value)}
  1050.  
  1051.     function getOnBeforeInsert(return $this->readonbeforeinsert()}
  1052.     function setOnBeforeInsert($value$this->writeonbeforeinsert($value)}
  1053.  
  1054.     function getOnAfterInsert(return $this->readonafterinsert()}
  1055.     function setOnAfterInsert($value$this->writeonafterinsert($value)}
  1056.  
  1057.     function getOnBeforeEdit(return $this->readonbeforeedit()}
  1058.     function setOnBeforeEdit($value$this->writeonbeforeedit($value)}
  1059.  
  1060.  
  1061.     function getOnAfterEdit(return $this->readonafteredit()}
  1062.     function setOnAfterEdit($value$this->writeonafteredit($value)}
  1063.  
  1064.     function getOnBeforePost(return $this->readonbeforepost()}
  1065.     function setOnBeforePost($value$this->writeonbeforepost($value)}
  1066.  
  1067.     function getOnAfterPost(return $this->readonafterpost()}
  1068.     function setOnAfterPost($value$this->writeonafterpost($value)}
  1069.  
  1070.     function getOnBeforeCancel(return $this->readonbeforecancel()}
  1071.     function setOnBeforeCancel($value$this->writeonbeforecancel($value)}
  1072.  
  1073.     function getOnAfterCancel(return $this->readonaftercancel()}
  1074.     function setOnAfterCancel($value$this->writeonaftercancel($value)}
  1075.  
  1076.     function getOnBeforeDelete(return $this->readonbeforedelete()}
  1077.     function setOnBeforeDelete($value$this->writeonbeforedelete($value)}
  1078.  
  1079.     function getOnAfterDelete(return $this->readonafterdelete()}
  1080.     function setOnAfterDelete($value$this->writeonafterdelete($value)}
  1081.  
  1082.     function getOnDeleteError(return $this->readondeleteerror()}
  1083.     function setOnDeleteError($value$this->writeondeleteerror($value)}
  1084.  
  1085. }
  1086.  
  1087. /**
  1088.  * Table class
  1089.  *
  1090.  * A class to encapsulate a database table
  1091.  */
  1092. class CustomQuery extends CustomTable
  1093. {
  1094.         protected $_sql=array();
  1095.  
  1096.         /**
  1097.          * Specifies the query property to be executed
  1098.          *
  1099.          * @return string 
  1100.          */
  1101.         function readSQL(return $this->_sql;     }
  1102.         function writeSQL($value)
  1103.         {
  1104.                 $clean=unserialize($value);
  1105.                 if ($clean===false)
  1106.                 {
  1107.                         $this->_sql=$value;
  1108.                 }
  1109.                 else
  1110.                 {
  1111.                         $this->_sql=$clean;
  1112.                 }
  1113.         }
  1114.         function defaultSQL(return "";     }
  1115.  
  1116.         /**
  1117.          * Prepares the query to be executed
  1118.          *
  1119.          */
  1120.         function Prepare()
  1121.         {
  1122.             $this->Database->Prepare($this->buildQuery());
  1123.         }
  1124.  
  1125.         /**
  1126.          * Specifies the parameters to be used in a parametrized query
  1127.          *
  1128.          * @return array 
  1129.          */
  1130.         function readParams(return $this->_params}
  1131.         function writeParams($value$this->_params=$value}
  1132.         function defaultParams(return ""}
  1133.  
  1134.         function buildQuery()
  1135.         {
  1136.             if (($this->ControlState csDesigning)!=csDesigning)
  1137.             {
  1138.                 if (is_array($this->_sql))
  1139.                 {
  1140.                     if (!empty($this->_sql))
  1141.                     {
  1142.                         $use_query_string=true;
  1143.                         $qu=implode(' ',$this->_sql);
  1144.                     }
  1145.                 }
  1146.                 else
  1147.                 {
  1148.                     if ($this->_sql!="")
  1149.                     {
  1150.                         $qu=$this->_sql;
  1151.                     }
  1152.                 }
  1153.  
  1154.                 $order="";
  1155.                 if ($this->_orderfield!=""$order="order by $this->_orderfield $this->_order";
  1156.  
  1157.                 $filter="";
  1158.                 if ($this->_filter!=""$filter=" where $this->_filter ";
  1159.  
  1160.                 $result=$qu." $filter $order ";
  1161.  
  1162.                 return($result);
  1163.             }
  1164.             else return('');
  1165.         }
  1166. }
  1167.  
  1168. /**
  1169.  * Table class
  1170.  *
  1171.  * A class to encapsulate a database table
  1172.  */
  1173. class Query extends CustomQuery
  1174. {
  1175.         function getSQL(return $this->readsql()}
  1176.         function setSQL($value$this->writesql($value)}
  1177.  
  1178.         function getParams(return $this->readparams()}
  1179.         function setParams($value$this->writeparams($value)}
  1180.  
  1181.         function getTableName(return $this->readtablename()}
  1182.         function setTableName($value$this->writetablename($value)}
  1183.  
  1184.         function getActive(return $this->readactive()}
  1185.         function setActive($value$this->writeactive($value)}
  1186.  
  1187.         function getDatabase(return $this->readdatabase()}
  1188.         function setDatabase($value$this->writedatabase($value)}
  1189.  
  1190.         function getFilter(return $this->readfilter()}
  1191.         function setFilter($value$this->writefilter($value)}
  1192.  
  1193.         function getOrderField(return $this->readorderfield()}
  1194.         function setOrderField($value$this->writeorderfield($value)}
  1195.  
  1196.         function getOrder(return $this->readorder()}
  1197.         function setOrder($value$this->writeorder($value)}
  1198. }
  1199.  
  1200. /**
  1201.  * StoredProc Class
  1202.  *
  1203.  * A component to call stored procedures
  1204.  */
  1205. class StoredProc extends CustomQuery
  1206. {
  1207.     protected $_storedprocname="";
  1208.  
  1209.         /**
  1210.          * Name of the stored procedure to execute
  1211.          *
  1212.          * @return string 
  1213.          */
  1214.     function getStoredProcName(return $this->_storedprocname}
  1215.     function setStoredProcName($value$this->_storedprocname=$value}
  1216.     function defaultStoredProcName(return ""}
  1217.  
  1218.         function getActive(return $this->readactive()}
  1219.         function setActive($value$this->writeactive($value)}
  1220.  
  1221.         function getDatabase(return $this->readdatabase()}
  1222.         function setDatabase($value$this->writedatabase($value)}
  1223.  
  1224.         function getFilter(return $this->readfilter()}
  1225.         function setFilter($value$this->writefilter($value)}
  1226.  
  1227.         function getOrderField(return $this->readorderfield()}
  1228.         function setOrderField($value$this->writeorderfield($value)}
  1229.  
  1230.         function getOrder(return $this->readorder()}
  1231.         function setOrder($value$this->writeorder($value)}
  1232.  
  1233.         function getParams(return $this->readparams()}
  1234.         function setParams($value$this->writeparams($value)}
  1235.  
  1236.         function Prepare()
  1237.         {
  1238.             $this->Database->PrepareSP($this->buildQuery());
  1239.         }
  1240.  
  1241.         function buildQuery()
  1242.         {
  1243.             if (($this->ControlState csDesigning)!=csDesigning)
  1244.             {
  1245.                     $pars="";
  1246.  
  1247.                     reset($this->_params);
  1248.                     while(list($key$val)=each($this->_params))
  1249.                     {
  1250.                         if ($pars!=""$pars.=', ';
  1251.                         $pars.="'$val'";
  1252.                     }
  1253.  
  1254.                     if ($pars!=""$pars="($pars)";
  1255.  
  1256.                     $result="select * from $this->_storedprocname$pars";
  1257.  
  1258.  
  1259.                 return($result);
  1260.             }
  1261.             else return('');
  1262.         }
  1263. }
  1264.  
  1265. ?>

Documentation generated on Tue, 27 Mar 2007 13:34:23 +0200 by phpDocumentor 1.3.1