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

Source for file interbase.inc.php

Documentation is available at interbase.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.  
  30. /**
  31.  * IBDatabase class
  32.  *
  33.  * A database component for InterBase
  34.  */
  35. class IBDatabase extends CustomConnection
  36. {
  37.         public $_connection=null;
  38.         protected $_debug=0;
  39.         protected $_databasename="";
  40.         protected $_host="";
  41.         protected $_username="";
  42.         protected $_userpassword="";
  43.         protected $_connected="0";
  44.         protected $_dictionary="";
  45.         protected $_dictionaryproperties=false;
  46.  
  47.         function MetaFields($tablename)
  48.         {
  49.             $metaColumnsSQL "select a.rdb\$field_name from rdb\$relation_fields a, rdb\$fields b where a.rdb\$field_source = b.rdb\$field_name and a.rdb\$relation_name = '%s' order by a.rdb\$field_position asc";
  50.  
  51.             $rs $this->Execute(sprintf($metaColumnsSQL,strtoupper($tablename)));
  52.             $result=array();
  53.             while ($row=ibase_fetch_row($rs))
  54.             {
  55.                 $result[$row[0]]='';
  56.             }
  57.             return($result);
  58.         }
  59.  
  60.         function BeginTrans($args=array())
  61.         {
  62.             ibase_trans($args,$this->_connection);
  63.         }
  64.  
  65.         function CompleteTrans($autocomplete=true)
  66.         {
  67.             if ($autocomplete)
  68.             {
  69.                 ibase_commit($this->_connection);
  70.             }
  71.             else ibase_rollback($this->_connection);
  72.         }
  73.  
  74.         function readConnected(if ($this->_connection!=nullreturn("1")else return("0")}
  75.  
  76.         function getConnected(return $this->readconnected()}
  77.         function setConnected($value$this->writeconnected($value)}
  78.  
  79.  
  80.         function getDebug(return $this->_debug}
  81.         function setDebug($value$this->_debug=$value}
  82.         function defaultDebug(return 0}
  83.  
  84.         //Dictionary property
  85.         
  86.         function getDictionary(return $this->_dictionary;   }
  87.         function setDictionary($value$this->_dictionary=$value}
  88.  
  89.         //DatabaseName property
  90.         
  91.         function getDatabaseName(return $this->_databasename;       }
  92.         function setDatabaseName($value$this->_databasename=$value}
  93.  
  94.         //Host property
  95.         
  96.         function getHost(return $this->_host;       }
  97.         function setHost($value$this->_host=$value}
  98.  
  99.         //UserName property
  100.         
  101.         function getUserName(return $this->_username;       }
  102.         function setUserName($value$this->_username=$value}
  103.  
  104.         //Password property
  105.         
  106.         function getUserPassword(return $this->_userpassword;       }
  107.         function setUserPassword($value$this->_userpassword=$value}
  108.  
  109.         function getOnAfterConnect(return $this->readonafterconnect()}
  110.         function setOnAfterConnect($value$this->writeonafterconnect($value)}
  111.  
  112.         function getOnBeforeConnect(return $this->readonbeforeconnect()}
  113.         function setOnBeforeConnect($value$this->writeonbeforeconnect($value)}
  114.  
  115.         function getOnAfterDisconnect(return $this->readonafterdisconnect()}
  116.         function setOnAfterDisconnect($value$this->writeonafterdisconnect($value)}
  117.  
  118.         function getOnBeforeDisconnect(return $this->readonbeforedisconnect()}
  119.         function setOnBeforeDisconnect($value$this->writeonbeforedisconnect($value)}
  120.  
  121.         protected $_dialect=3;
  122.  
  123.         /**
  124.         * Specifies the dialect to be used when connecting to the server
  125.         * @return string 
  126.         */
  127.         function getDialect(return $this->_dialect}
  128.         function setDialect($value$this->_dialect=$value}
  129.         function defaultDialect(return 3}
  130.  
  131.         function Prepare($query)
  132.         {
  133.             ibase_prepare($this->_connection$query);
  134.         }
  135.  
  136.         function PrepareSP($query)
  137.         {
  138.             ibase_prepare($this->_connection$query);
  139.         }
  140.  
  141.         function DBDate($input)
  142.         {
  143.             return(date('%Y-%m-%d',strtotime($input)));
  144.         }
  145.  
  146.         function Param($input)
  147.         {
  148.             return($this->QuoteStr($input));
  149.         }
  150.  
  151.         function QuoteStr($input)
  152.         {
  153.             return("'$input'");
  154.         }
  155.  
  156.         function __construct($aowner=null)
  157.         {
  158.                 //Calls inherited constructor
  159.                 parent::__construct($aowner);
  160.         }
  161.  
  162.  
  163.         /**
  164.          * Executes a query
  165.          *
  166.          * @param string $query 
  167.          * @return object 
  168.          */
  169.         function execute($query,$params=array())
  170.         {
  171.                 $this->open();
  172.                 $rs @ibase_query ($this->_connection$query);
  173.                 if ($rs===false)
  174.                 {
  175.                         DatabaseError("Error executing query$query [".ibase_errmsg()."]");
  176.                 }
  177.  
  178.                 return($rs);
  179.         }
  180.  
  181.         /**
  182.          * Executes a limited query
  183.          *
  184.          * @param string $query 
  185.          * @return object 
  186.          */
  187.         function executelimit($query,$numrows,$offset)
  188.         {
  189.                 $this->open();
  190.                 $sql=$query;
  191.  
  192.                 if ($numrows 0)
  193.                 {
  194.                     if ($offset <= 0$str " ROWS $numrows ";
  195.                     else
  196.                     {
  197.                         $a $offset+1;
  198.                         $b $offset+$numrows;
  199.                         $str " ROWS $a TO $b";
  200.                     }
  201.                 }
  202.                 else
  203.                 {
  204.                     // ok, skip
  205.                     $a $offset 1;
  206.                     $str " ROWS $a TO 999999999"// 999 million
  207.                 }
  208.  
  209.                 $sql .= $str;
  210.  
  211.                 return($this->execute($sql));
  212.         }
  213.  
  214.         function DoConnect()
  215.         {
  216.             if (($this->ControlState csDesigning)!=csDesigning)
  217.             {
  218.                 $this->_connection = ibase_pconnect ($this->DatabaseName$this->UserName$this->UserPassword,'ISO8859_1''100'$this->Dialect);
  219.  
  220.                 if (!$this->_connection)
  221.                 {
  222.                     DatabaseError("Cannot connect to database server");
  223.                 }
  224.             }
  225.         }
  226.  
  227.         function DoDisconnect()
  228.         {
  229.                 if ($this->_connection!=null)
  230.                 {
  231.                         ibase_close ($this->_connection);
  232.                         $this->_connection=null;
  233.                 }
  234.         }
  235.  
  236.         /**
  237.          * Return properties for a field
  238.          *
  239.          * @param string $table 
  240.          * @param string $field 
  241.          * @return array 
  242.          */
  243.         function readFieldDictionaryProperties($table$field)
  244.         {
  245.                 $table=trim($table);
  246.                 $field=trim($field);
  247.                 if ($this->_connected)
  248.                 {
  249.                         if ($this->_dictionary!='')
  250.                         {
  251.                                 $q="select * from $this->_dictionary where dict_tablename='$tableand dict_fieldname='$field'";
  252.                                 $r=$this->execute($q);
  253.                                 $props=array();
  254.                                 while ($arow=ibase_fetch_assoc($r))
  255.                                 {
  256.                                         $row=array();
  257.                                         reset($arow);
  258.                                         while (list($k,$v)=each($arow))
  259.                                         {
  260.                                                 $row[strtolower($k)]=$v;
  261.                                         }
  262.  
  263.                                         $props[$row['dict_property']]=array($row['dict_value1'],$row['dict_value2']);
  264.                                 }
  265.                                 if (!empty($props)) $result=$props;
  266.                         }
  267.                         else
  268.                         {
  269.                                 if ($this->_dictionaryproperties!=false)
  270.                                 {
  271.                                         $result=$this->_dictionaryproperties[$table][$field];
  272.                                 }
  273.                         }
  274.                 }
  275.                 return($result);
  276.         }
  277.  
  278.  
  279.         /**
  280.          * Return indexes for a table
  281.          *
  282.          * @param string $table 
  283.          * @param boolean $primary 
  284.          * @return array 
  285.          */
  286.         function &extractIndexes($table$primary FALSE)
  287.         {
  288.             $table strtoupper($table);
  289.             $sql "SELECT * FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = '".$table."'";
  290.  
  291.             if (!$primary)
  292.             {
  293.                 $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$%'";
  294.             }
  295.             else
  296.             {
  297.                 $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$FOREIGN%'";
  298.             }
  299.  
  300.             $rs=$this->execute($sql);
  301.  
  302.             $indexes array();
  303.             while ($row ibase_fetch_row($rs))
  304.             {
  305.                 $index $row[0];
  306.                 if (!isset($indexes[$index]))
  307.                 {
  308.                     if (is_null($row[3]))
  309.                     {
  310.                         $row[30;
  311.                     }
  312.  
  313.                     $indexes[$indexarray(
  314.                              'unique' => ($row[3== 1),
  315.                              'columns' => array()
  316.                      );
  317.                 }
  318.  
  319.                 $sql "SELECT * FROM RDB\$INDEX_SEGMENTS WHERE RDB\$INDEX_NAME = '".$index."' ORDER BY RDB\$FIELD_POSITION ASC";
  320.                 $rs1 $this->execute($sql);
  321.  
  322.                 while ($row1 ibase_fetch_row($rs1))
  323.                 {
  324.                     $indexes[$index]['columns'][$row1[2]] $row1[1];
  325.                 }
  326.             }
  327.             return $indexes;
  328.         }
  329.  
  330.         function readDictionaryProperties(return $this->_dictionaryproperties;   }
  331.         function writeDictionaryProperties($value$this->_dictionaryproperties=$value}
  332.  
  333.  
  334.         /**
  335.          * Creates the dictionary table on the database
  336.          *
  337.          * @return boolean 
  338.          */
  339.         function createDictionaryTable()
  340.         {
  341.                 $result=false;
  342.                 if ($this->_connected)
  343.                 {
  344.                         if ($this->_dictionary!='')
  345.                         {
  346.                          $q="CREATE TABLE $this->_dictionary (\n";
  347.                          $q.="  DICT_ID INTEGER NOT NULL,\n";
  348.                          $q.="  DICT_TABLENAME VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  349.                          $q.="  DICT_FIELDNAME VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  350.                          $q.="  DICT_PROPERTY VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  351.                          $q.="  DICT_VALUE1 VARCHAR(60) CHARACTER SET NONE COLLATE NONE,\n";
  352.                          $q.="  DICT_VALUE2 VARCHAR(200) CHARACTER SET NONE COLLATE NONE);\n";
  353.                          $this->execute($q);
  354.  
  355.                          $q="ALTER TABLE $this->_dictionary ADD PRIMARY KEY (DICT_ID);\n";
  356.                          $this->execute($q);
  357.                         }
  358.                 }
  359.                 return($result);
  360.         }
  361.  
  362.         /**
  363.          * Return all the databases using the connection information
  364.          *
  365.          * @return array 
  366.          */
  367.         function databases()
  368.         {
  369.             return false;
  370.         }
  371.  
  372.         /**
  373.          * Return tables on this database
  374.          *
  375.          * @return array 
  376.          */
  377.         function tables($ttype=false,$showSchema=false,$mask=false)
  378.         {
  379.                 $metaTablesSQL "select rdb\$relation_name from rdb\$relations where rdb\$relation_name not like 'RDB\$%'";
  380.  
  381.                 $false false;
  382.                 if ($mask)
  383.                 {
  384.                         return $false;
  385.                 }
  386.  
  387.                 if ($metaTablesSQL)
  388.                 {
  389.                         $rs $this->execute($metaTablesSQL);
  390.                         if ($rs === falsereturn $false;
  391.  
  392.                         $rr=array();
  393.                         while ($arr ibase_fetch_row($rs))
  394.                         {
  395.                             $rr[]=$arr[0];
  396.                         }
  397.                         return $rr;
  398.                 }
  399.                 return $false;
  400.         }
  401. }
  402.  
  403. /**
  404.  * IBDataSet class
  405.  *
  406.  * Base Class for datasets linked to a database
  407.  */
  408. class IBDataSet extends DataSet
  409. {
  410.     public $_rs=null;
  411.  
  412.     protected $_eof=false;
  413.     public $_buffer=array();
  414.  
  415.     protected $_database=null;
  416.     protected $_params=array();
  417.  
  418.     function readDatabase(return $this->_database}
  419.     function writeDatabase($value$this->_database=$this->fixupProperty($value)}
  420.     function defaultDatabase(return null}
  421.  
  422.     function loaded()
  423.     {
  424.         $this->writeDatabase($this->_database);
  425.         parent::loaded();
  426.     }
  427.  
  428.     function readFields(return($this->_buffer)}
  429.     function readFieldCount(return count($this->_buffer)}
  430.  
  431.     function readRecordCount()
  432.     {
  433.         if (assigned($this->_rs))
  434.         {
  435.             return(-1);
  436.         }
  437.         else return(parent::readRecordCount());
  438.     }
  439.  
  440.     function MoveBy($distance)
  441.     {
  442.         parent::MoveBy($distance);
  443.  
  444.         for($i=0;$i<=$distance-1;$i++)
  445.         {
  446.             $this->_eof=!($buff=ibase_fetch_assoc($this->_rs));
  447.         }
  448.         if (!$this->_eof$this->_buffer=$buff;
  449.     }
  450.  
  451.     function readEOF()
  452.     {
  453.         return($this->_eof);
  454.     }
  455.  
  456.     function CheckDatabase()
  457.     {
  458.         if (!is_object($this->_database)) DatabaseError(_("No Database assigned or is not an object"));
  459.     }
  460.  
  461.     public $_keyfields=array();
  462.  
  463.     function InternalOpen($lquery="")
  464.     {
  465.         if (($this->ControlState csDesigning)!=csDesigning)
  466.         {
  467.             if ($lquery!=""$query=$lquery;
  468.             else $query=$this->buildQuery();
  469.             if (trim($query)==''DatabaseError(_("Missing query to execute"));
  470.             $this->CheckDatabase();
  471.             $this->_eof=false;
  472.  
  473.             if ((trim($this->_limitstart)=='-1'&& (trim($this->_limitcount)=='-1'))
  474.             {
  475.                 $this->_rs=$this->Database->Execute($query,$this->_params);
  476.                 $this->_buffer=array();
  477.                 $this->MoveBy(1);
  478.             }
  479.             else
  480.             {
  481.                 $limitstart=trim($this->_limitstart);
  482.                 if ($limitstart==''$limitstart=-1;
  483.  
  484.                 $limitcount=trim($this->_limitcount);
  485.                 if ($limitcount==''$limitcount=-1;
  486.  
  487.                 $this->_rs=$this->Database->ExecuteLimit($query,$limitcount,$limitstart);
  488.                 $this->_buffer=array();
  489.                 $this->MoveBy(1);
  490.             }
  491.  
  492.             if ((!is_array($this->_buffer)) || (count($this->_buffer)==0))
  493.             {
  494.                 if ($this->_tablename!='')
  495.                 {
  496.                     $fd=$this->Database->MetaFields($this->_tablename);
  497.                     $this->_buffer=$fd;
  498.                 }
  499.             }
  500.             $this->_keyfields=$this->readKeyFields();
  501.             $this->fieldbuffer=$this->_buffer;
  502.         }
  503.     }
  504.  
  505.     function InternalFirst()
  506.     {
  507.         $this->InternalClose();
  508.         $this->InternalOpen($this->_lastquery);
  509.     }
  510.  
  511.     /**
  512.     * Overriden to allow get field values as properties
  513.     *
  514.     * @param string $nm 
  515.     * @return mixed 
  516.     */
  517.     function __get($nm)
  518.     {
  519.         if ($this->_rs!=null)
  520.         {
  521.             if ($this->Active)
  522.             {
  523.                 if ((is_array($this->_buffer)) && (array_key_exists($nm,$this->_buffer)))
  524.                 {
  525.                     if ($this->State==dsBrowse)
  526.                     {
  527.                         return ($this->_buffer[$nm]);
  528.                     }
  529.                     else if (array_key_exists($nm,$this->fieldbuffer))
  530.                     {
  531.                         return($this->fieldbuffer[$nm]);
  532.                     }
  533.                     else return('');
  534.                 }
  535.                 else if ((is_array($this->fieldbuffer)) && (array_key_exists($nm,$this->fieldbuffer)))
  536.                 {
  537.                     return($this->fieldbuffer[$nm]);
  538.                 }
  539.                 else return(parent::__get($nm));
  540.  
  541.             }
  542.             else
  543.             {
  544.                 return(parent::__get($nm));
  545.             }
  546.          }
  547.          else
  548.          {
  549.             return(parent::__get($nm));
  550.          }
  551.     }
  552.  
  553.     /**
  554.     * Overriden to allow get field values as properties
  555.     *
  556.     * @param string $nm 
  557.     * @return mixed 
  558.     */
  559.     function __set($nm,$val)
  560.     {
  561.         if ($this->_rs!=null)
  562.         {
  563.             if ($this->Active)
  564.             {
  565.                 if ((is_array(($this->_buffer))) && (array_key_exists($nm,$this->_buffer)))
  566.                 {
  567.                     $this->fieldbuffer[$nm]=$val;
  568.                     $this->Modified=true;
  569.                     if ($this->State==dsBrowse)
  570.                     {
  571.                         $this->State=dsEdit;
  572.                     }
  573.                 }
  574.                 else parent::__set($nm$val);
  575.             }
  576.             else
  577.             {
  578.                 parent::__set($nm$val);
  579.             }
  580.          }
  581.          else
  582.          {
  583.             parent::__set($nm$val);
  584.          }
  585.     }
  586. }
  587.  
  588. /**
  589.  * CustomIBTable class
  590.  *
  591.  * A class to encapsulate a database table
  592.  */
  593. class CustomIBTable extends IBDataSet
  594. {
  595.         protected $_tablename="";
  596.         protected $_lastquery="";
  597.  
  598.         function readTableName(return $this->_tablename}
  599.         function writeTableName($value$this->_tablename=$value}
  600.         function defaultTableName(return ""}
  601.  
  602.         function InternalDelete()
  603.         {
  604.                 $where='';
  605.                 reset($this->_keyfields);
  606.                 while(list($key$fname)=each($this->_keyfields))
  607.                 {
  608.                     $val=$this->fieldbuffer[$fname];
  609.                     if (trim($val)==''continue;
  610.                     if ($where!=''$where.=" and ";
  611.                     $where.=" $fname = ".$this->Database->QuoteStr($val);
  612.                 }
  613.  
  614.             if ($where!='')
  615.             {
  616.                 $query="delete from $this->TableName where $where";
  617.                 $this->Database->Execute($query);
  618.             }
  619.  
  620.         }
  621.  
  622.         /**
  623.          * Get field properties
  624.          *
  625.          * @param string $fieldname 
  626.          * @return mixed 
  627.          */
  628.         function readFieldProperties($fieldname)
  629.         {
  630.                 if ($this->_database!=null)
  631.                 {
  632.                         return($this->_database->readFieldDictionaryProperties($this->_tablename,$fieldname));
  633.                 }
  634.                 else return(false);
  635.         }
  636.  
  637.         function InternalPost()
  638.         {
  639.             if ($this->State == dsEdit)
  640.             {
  641.                 $where='';
  642.                 $buffer=$this->fieldbuffer;
  643.                 reset($this->_keyfields);
  644.                 while(list($key$fname)=each($this->_keyfields))
  645.                 {
  646.                     $val=$this->fieldbuffer[$fname];
  647.                     unset($buffer[$fname]);
  648.                     if (trim($val)==''continue;
  649.                     if ($where!=''$where.=" and ";
  650.                     $where.=" $fname = ".$this->Database->QuoteStr($val);
  651.                 }
  652.  
  653.                 $set="";
  654.                 reset($buffer);
  655.                 while(list($key$fname)=each($buffer))
  656.                 {
  657.                     if ($set!=""$set.=", ";
  658.                     $set.=" $key = '$fname";
  659.                 }
  660.  
  661.  
  662.                 try
  663.                 {
  664.                     $updateSQL="update $this->TableName set $set where $where";
  665.                     $this->Database->execute($updateSQL);
  666.                     $this->_buffer=array_merge($this->_buffer,$this->fieldbuffer);
  667.                 }
  668.                 catch (Exception $e)
  669.                 {
  670.                     $this->_buffer=array_merge($this->_buffer,$this->fieldbuffer);
  671.                     throw $e;
  672.                 }
  673.  
  674.                 //TODO: Handle errors
  675.             }
  676.             else
  677.             {
  678.                 $fields='';
  679.                 $values='';
  680.                 if (is_array($this->_keyfields))
  681.                 {
  682.                     reset($this->_keyfields);
  683.                     while(list($key$fname)=each($this->_keyfields))
  684.                     {
  685.                         unset($this->fieldbuffer[$fname]);
  686.                     }
  687.                 }
  688.  
  689.                 reset($this->fieldbuffer);
  690.                 while(list($key$val)=each($this->fieldbuffer))
  691.                 {
  692.                         if ($fields!=''$fields.=',';
  693.                         $fields.='$key';
  694.  
  695.                         if ($values!=''$values.=',';
  696.                         $values.=$this->Database->QuoteStr($val);
  697.                 }
  698.  
  699.                 try
  700.                 {
  701.                     $insertSQL="insert into $this->TableName($fieldsvalues ($values)";
  702.                     $this->Database->execute($insertSQL);
  703.                     $this->_buffer=array_merge($this->_buffer,$this->fieldbuffer);
  704.                 }
  705.                 catch (Exception $e)
  706.                 {
  707.                     $this->_buffer=array_merge($this->_buffer,$this->fieldbuffer);
  708.                     throw $e;
  709.                 }
  710.             }
  711.         }
  712.  
  713.  
  714.         function buildQuery()
  715.         {
  716.             if (($this->ControlState csDesigning)!=csDesigning)
  717.             {
  718.                 if (trim($this->_tablename)=='')
  719.                 {
  720.                     if ($this->Active)
  721.                     {
  722.                         DatabaseError(_("Missing TableName property"));
  723.                     }
  724.                 }
  725.                 $qu="select * from $this->_tablename";
  726.  
  727.                 $order="";
  728.                 if ($this->_orderfield!=""$order="order by $this->_orderfield $this->_order";
  729.  
  730.                 $where="";
  731.                 if ($this->Filter!=""$where.=" $this->Filter ";
  732.  
  733.                 if ($this->MasterSource!="")
  734.                 {
  735.                     $this->writeMasterSource($this->_mastersource);
  736.                     if (is_object($this->_mastersource))
  737.                     {
  738.                         if (is_array($this->_masterfields))
  739.                         {
  740.                             $this->_mastersource->DataSet->open();
  741.  
  742.                             $ms="";
  743.                             reset($this->_masterfields);
  744.  
  745.                             while(list($key$val)=each($this->_masterfields))
  746.                             {
  747.                                 $thisfield=$key;
  748.                                 $msfield=$val;
  749.  
  750.                                 if ($ms!=""$ms.=" and ";
  751.                                 $ms.=" $thisfield=".$this->Database->QuoteStr($this->_mastersource->DataSet->$msfield)." ";
  752.                             }
  753.  
  754.                             if ($ms!="")
  755.                             {
  756.                                 if ($where!=""$where.=" and ";
  757.                                 $where.=" ($ms";
  758.                             }
  759.                         }
  760.                     }
  761.                 }
  762.  
  763.                 if ($where!=""$where=" where $where ";
  764.  
  765.                 $result=$qu." $where $order ";
  766.  
  767.                 $this->_lastquery=$result;
  768.                 return($result);
  769.             }
  770.             else return('');
  771.         }
  772.  
  773.           /**
  774.            * Return an array containg the row values
  775.            *
  776.            * @return array 
  777.            */
  778.   function readAssociativeFieldValues()
  779.   {
  780.         $result=array();
  781.  
  782.         if ($this->Active)
  783.         {
  784.                 return($this->_buffer);
  785.         }
  786.  
  787.         return($result);
  788.   }
  789.  
  790.         /**
  791.         * Return an array with Key fields for the table
  792.         *
  793.         * @return array 
  794.         */
  795.         function readKeyFields()
  796.         {
  797.                 //TODO: Check here for another indexes
  798.                 $result="";
  799.                 if ($this->_tablename!='')
  800.                 {
  801.                 $indexes=$this->Database->extractIndexes($this->_tablename,true);
  802.  
  803.                 if (is_array($indexes))
  804.                 {
  805.                     list(,$primary)=each($indexes);
  806.  
  807.                     $result=$primary['columns'];
  808.                     if (is_array($result))
  809.                     {
  810.                         while (list($k,$v)=each($result))
  811.                         {
  812.                                 $result[$k]=trim($v);
  813.                         }
  814.                     }
  815.                 }
  816.                 }
  817.                 return($result);
  818.         }
  819.  
  820.         function dumpHiddenKeyFields($basename$values=array())
  821.         {
  822.                 $keyfields=$this->readKeyFields();
  823.  
  824.                 if (empty($values))
  825.                 {
  826.                         $values=$this->readAssociativeFieldValues();
  827.                 }
  828.  
  829.                 if (is_array($keyfields))
  830.                 {
  831.                     reset($keyfields);
  832.                     while (list($k,$v)=each($keyfields))
  833.                     {
  834.                             echo "<input type=\"hidden\" name=\"".$basename."[$v]\" value=\"$values[$v]\" />";
  835.                     }
  836.                 }
  837.         }
  838.  
  839.     protected $_orderfield="";
  840.  
  841.     /**
  842.     * Specifies the field by which the dataset will be ordered
  843.     *
  844.     * @return string 
  845.     */
  846.     function readOrderField(return $this->_orderfield}
  847.     function writeOrderField($value$this->_orderfield=$value}
  848.     function defaultOrderField(return ""}
  849.  
  850.     protected $_order="asc";
  851.  
  852.     /**
  853.     * Specifies the order (ascending/descending) to order the dataset
  854.     *
  855.     * @return string 
  856.     */
  857.     function readOrder(return $this->_order}
  858.     function writeOrder($value$this->_order=$value}
  859.     function defaultOrder(return "asc"}
  860. }
  861. //**************************************************************
  862. //TODO: Abstract more this class into Dataset (i.e. active, open, etc)
  863.  
  864. /**
  865.  * IBTable class
  866.  *
  867.  * A class to encapsulate an interbase table
  868.  */
  869. class IBTable extends CustomIBTable
  870. {
  871.         function getMasterSource(return $this->readmastersource()}
  872.         function setMasterSource($value$this->writemastersource($value)}
  873.  
  874.         function getMasterFields(return $this->readmasterfields()}
  875.         function setMasterFields($value$this->writemasterfields($value)}
  876.         
  877.         function getTableName(return $this->readtablename()}
  878.         function setTableName($value$this->writetablename($value)}
  879.  
  880.         function getActive(return $this->readactive()}
  881.         function setActive($value$this->writeactive($value)}
  882.  
  883.         function getDatabase(return $this->readdatabase()}
  884.         function setDatabase($value$this->writedatabase($value)}
  885.  
  886.         function getFilter(return $this->readfilter()}
  887.         function setFilter($value$this->writefilter($value)}
  888.  
  889.         function getOrderField(return $this->readorderfield()}
  890.         function setOrderField($value$this->writeorderfield($value)}
  891.  
  892.         function getOrder(return $this->readorder()}
  893.         function setOrder($value$this->writeorder($value)}
  894.  
  895.  
  896.     function getOnBeforeOpen(return $this->readonbeforeopen()}
  897.     function setOnBeforeOpen($value$this->writeonbeforeopen($value)}
  898.  
  899.     function getOnAfterOpen(return $this->readonafteropen()}
  900.     function setOnAfterOpen($value$this->writeonafteropen($value)}
  901.  
  902.     function getOnBeforeClose(return $this->readonbeforeclose()}
  903.     function setOnBeforeClose($value$this->writeonbeforeclose($value)}
  904.  
  905.  
  906.     function getOnAfterClose(return $this->readonafterclose()}
  907.     function setOnAfterClose($value$this->writeonafterclose($value)}
  908.  
  909.     function getOnBeforeInsert(return $this->readonbeforeinsert()}
  910.     function setOnBeforeInsert($value$this->writeonbeforeinsert($value)}
  911.  
  912.     function getOnAfterInsert(return $this->readonafterinsert()}
  913.     function setOnAfterInsert($value$this->writeonafterinsert($value)}
  914.  
  915.     function getOnBeforeEdit(return $this->readonbeforeedit()}
  916.     function setOnBeforeEdit($value$this->writeonbeforeedit($value)}
  917.  
  918.  
  919.     function getOnAfterEdit(return $this->readonafteredit()}
  920.     function setOnAfterEdit($value$this->writeonafteredit($value)}
  921.  
  922.     function getOnBeforePost(return $this->readonbeforepost()}
  923.     function setOnBeforePost($value$this->writeonbeforepost($value)}
  924.  
  925.     function getOnAfterPost(return $this->readonafterpost()}
  926.     function setOnAfterPost($value$this->writeonafterpost($value)}
  927.  
  928.     function getOnBeforeCancel(return $this->readonbeforecancel()}
  929.     function setOnBeforeCancel($value$this->writeonbeforecancel($value)}
  930.  
  931.     function getOnAfterCancel(return $this->readonaftercancel()}
  932.     function setOnAfterCancel($value$this->writeonaftercancel($value)}
  933.  
  934.     function getOnBeforeDelete(return $this->readonbeforedelete()}
  935.     function setOnBeforeDelete($value$this->writeonbeforedelete($value)}
  936.  
  937.     function getOnAfterDelete(return $this->readonafterdelete()}
  938.     function setOnAfterDelete($value$this->writeonafterdelete($value)}
  939.  
  940.     function getOnDeleteError(return $this->readondeleteerror()}
  941.     function setOnDeleteError($value$this->writeondeleteerror($value)}
  942. }
  943.  
  944. class CustomIBQuery extends CustomIBTable
  945. {
  946.         protected $_sql=array();
  947.  
  948.         /**
  949.         * An array that specifies the query to be executed
  950.         *
  951.         * @return array 
  952.         */
  953.         function readSQL(return $this->_sql;     }
  954.         function writeSQL($value)
  955.         {
  956.                 $clean=unserialize($value);
  957.                 if ($clean===false)
  958.                 {
  959.                         $this->_sql=$value;
  960.                 }
  961.                 else
  962.                 {
  963.                         $this->_sql=$clean;
  964.                 }
  965.         }
  966.         function defaultSQL(return "";     }
  967.  
  968.         function Prepare()
  969.         {
  970.             $this->Database->Prepare($this->buildQuery());
  971.         }
  972.  
  973.         /**
  974.         * Parameters to be used on parametrized queries
  975.         *
  976.         * @return array 
  977.         */
  978.         function readParams(return $this->_params}
  979.         function writeParams($value$this->_params=$value}
  980.         function defaultParams(return ""}
  981.  
  982.         function buildQuery()
  983.         {
  984.             if (($this->ControlState csDesigning)!=csDesigning)
  985.             {
  986.                 if (is_array($this->_sql))
  987.                 {
  988.                     if (!empty($this->_sql))
  989.                     {
  990.                         $use_query_string=true;
  991.                         $qu=implode(' ',$this->_sql);
  992.                     }
  993.                 }
  994.                 else
  995.                 {
  996.                     if ($this->_sql!="")
  997.                     {
  998.                         $qu=$this->_sql;
  999.                     }
  1000.                 }
  1001.  
  1002.                 $order="";
  1003.                 if ($this->_orderfield!=""$order="order by $this->_orderfield $this->_order";
  1004.  
  1005.                 $filter="";
  1006.                 if ($this->_filter!=""$filter=" where $this->_filter ";
  1007.  
  1008.                 $result=$qu." $filter $order ";
  1009.  
  1010.                 return($result);
  1011.             }
  1012.             else return('');
  1013.         }
  1014. }
  1015.  
  1016. /**
  1017.  * Query class
  1018.  *
  1019.  * A class to encapsulate a database table
  1020.  */
  1021. class IBQuery extends CustomIBQuery
  1022. {
  1023.         function getSQL(return $this->readsql()}
  1024.         function setSQL($value$this->writesql($value)}
  1025.  
  1026.         function getParams(return $this->readparams()}
  1027.         function setParams($value$this->writeparams($value)}
  1028.  
  1029.         function getTableName(return $this->readtablename()}
  1030.         function setTableName($value$this->writetablename($value)}
  1031.  
  1032.         function getActive(return $this->readactive()}
  1033.         function setActive($value$this->writeactive($value)}
  1034.  
  1035.         function getDatabase(return $this->readdatabase()}
  1036.         function setDatabase($value$this->writedatabase($value)}
  1037.  
  1038.         function getFilter(return $this->readfilter()}
  1039.         function setFilter($value$this->writefilter($value)}
  1040.  
  1041.         function getOrderField(return $this->readorderfield()}
  1042.         function setOrderField($value$this->writeorderfield($value)}
  1043.  
  1044.         function getOrder(return $this->readorder()}
  1045.         function setOrder($value$this->writeorder($value)}
  1046. }
  1047.  
  1048. /**
  1049.  * IBStoredProc Class
  1050.  *
  1051.  * A component to call stored procedures
  1052.  */
  1053. class IBStoredProc extends CustomIBQuery
  1054. {
  1055.     protected $_storedprocname="";
  1056.  
  1057.     function getStoredProcName(return $this->_storedprocname}
  1058.     function setStoredProcName($value$this->_storedprocname=$value}
  1059.     function defaultStoredProcName(return ""}
  1060.  
  1061.         function getActive(return $this->readactive()}
  1062.         function setActive($value$this->writeactive($value)}
  1063.  
  1064.         function getDatabase(return $this->readdatabase()}
  1065.         function setDatabase($value$this->writedatabase($value)}
  1066.  
  1067.         function getFilter(return $this->readfilter()}
  1068.         function setFilter($value$this->writefilter($value)}
  1069.  
  1070.         function getOrderField(return $this->readorderfield()}
  1071.         function setOrderField($value$this->writeorderfield($value)}
  1072.  
  1073.         function getOrder(return $this->readorder()}
  1074.         function setOrder($value$this->writeorder($value)}
  1075.  
  1076.         function getParams(return $this->readparams()}
  1077.         function setParams($value$this->writeparams($value)}
  1078.  
  1079.         function Prepare()
  1080.         {
  1081.             $this->Database->PrepareSP($this->buildQuery());
  1082.         }
  1083.  
  1084.         function buildQuery()
  1085.         {
  1086.             if (($this->ControlState csDesigning)!=csDesigning)
  1087.             {
  1088.                     $pars="";
  1089.  
  1090.                     reset($this->_params);
  1091.                     while(list($key$val)=each($this->_params))
  1092.                     {
  1093.                         if ($pars!=""$pars.=', ';
  1094.                         $pars.="'$val'";
  1095.                     }
  1096.  
  1097.                     if ($pars!=""$pars="($pars)";
  1098.  
  1099.                     $result="select * from $this->_storedprocname$pars";
  1100.  
  1101.  
  1102.                 return($result);
  1103.             }
  1104.             else return('');
  1105.         }
  1106. }
  1107.  
  1108. ?>

Documentation generated on Tue, 27 Mar 2007 13:35:12 +0200 by phpDocumentor 1.3.1