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

Source for file Driver_skeleton.php

Documentation is available at Driver_skeleton.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: YOUR NAME <YOUR EMAIL>                                       |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Driver_skeleton.php,v 1.6 2004/04/09 18:24:56 lsmith Exp $
  46. //
  47.  
  48. // This is just a skeleton MDB2 driver.
  49. // There may be methods missing as this skeleton is based on the methods
  50. // implemented by the MySQL and PostGreSQL drivers in MDB2.
  51. // Some methods may not have to be implemented in the driver, because the
  52. // implementation in common.php is compatible with the given RDBMS.
  53. // In each of the listed methods I have added comments that tell you where
  54. // to look for a "reference" implementation.
  55. // Some of these methods have been expanded or changed slightly in MDB2.
  56. // Looking in the relevant MDB2 Wrapper should give you some pointers, some
  57. // other difference you will only discover by looking at one of the existing
  58. // MDB2 driver or the common implementation in common.php.
  59. // One thing that will definately have to be modified in all "reference"
  60. // implementations of Metabase methods is the error handling.
  61. // Anyways don't worry if you are having problems: Lukas Smith is here to help!
  62.  
  63. /**
  64.  * MDB2 XXX driver
  65.  *
  66.  * @package MDB2
  67.  * @category Database
  68.  * @author  YOUR NAME <YOUR EMAIL>
  69.  */
  70. class MDB2_xxx extends MDB2_Driver_Common
  71. {
  72. // Most of the class variables are taken from the corresponding Metabase driver.
  73. // Few are taken from the corresponding PEAR DB driver.
  74. // Some are MDB2 specific.
  75.  
  76.     var $escape_quotes = "\\";
  77.  
  78.     // }}}
  79.     // {{{ constructor
  80.  
  81.     /**
  82.     * Constructor
  83.     */
  84.     function MDB2_xxx()
  85.     {
  86.         $this->MDB2_Driver_Common();
  87.         $this->phptype 'xxx';
  88.         $this->dbsyntax 'xxx';
  89.  
  90.         // most of the following codes needs to be taken from the corresponding Metabase driver setup() methods
  91.  
  92.         // also please remember to "register" all driver specific options here like so
  93.         // $this->options['option_name'] = 'non null default value';
  94.     }
  95.  
  96.     // }}}
  97.     // {{{ errorInfo()
  98.  
  99.     /**
  100.      * This method is used to collect information about an error
  101.      *
  102.      * @param integer $error 
  103.      * @return array 
  104.      * @access public
  105.      */
  106.     function errorInfo($error = null)
  107.     {
  108.         // take this method from the corresponding PEAR DB driver: xxxRaiseError(), errorCode() and errorNative()
  109.         // the error code maps from corresponding PEAR DB driver constructor
  110.     }
  111.  
  112.     // }}}
  113.     // {{{ autoCommit()
  114.  
  115.     /**
  116.      * Define whether database changes done on the database be automatically
  117.      * committed. This function may also implicitly start or end a transaction.
  118.      *
  119.      * @param boolean $auto_commit    flag that indicates whether the database
  120.      *                                 changes should be committed right after
  121.      *                                 executing every query statement. If this
  122.      *                                 argument is 0 a transaction implicitly
  123.      *                                 started. Otherwise, if a transaction is
  124.      *                                 in progress it is ended by committing any
  125.      *                                 database changes that were pending.
  126.      *
  127.      * @access public
  128.      *
  129.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  130.      */
  131.     function autoCommit($auto_commit)
  132.     {
  133.         // take this from the corresponding Metabase driver: AutoCommitTransactions()
  134.         // the MetabaseShutdownTransactions function is handled by the PEAR desctructor
  135.     }
  136.  
  137.     // }}}
  138.     // {{{ commit()
  139.  
  140.     /**
  141.      * Commit the database changes done during a transaction that is in
  142.      * progress. This function may only be called when auto-committing is
  143.      * disabled, otherwise it will fail. Therefore, a new transaction is
  144.      * implicitly started after committing the pending changes.
  145.      *
  146.      * @access public
  147.      *
  148.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  149.      */
  150.     function commit()
  151.     {
  152.         // take this from the corresponding Metabase driver: CommitTransaction()
  153.     }
  154.  
  155.     // }}}
  156.     // {{{ rollback()
  157.  
  158.     /**
  159.      * Cancel any database changes done during a transaction that is in
  160.      * progress. This function may only be called when auto-committing is
  161.      * disabled, otherwise it will fail. Therefore, a new transaction is
  162.      * implicitly started after canceling the pending changes.
  163.      *
  164.      * @access public
  165.      *
  166.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  167.      */
  168.     function rollback()
  169.     {
  170.         // take this from the corresponding Metabase driver: RollbackTransaction()
  171.     }
  172.  
  173.     // }}}
  174.     // {{{ connect()
  175.  
  176.     /**
  177.      * Connect to the database
  178.      *
  179.      * @return true on success, MDB2 Error Object on failure
  180.      ***/
  181.     function connect()
  182.     {
  183.         // take this from the corresponding Metabase driver: Connect() and Setup()
  184.         if (PEAR::isError(PEAR::loadExtension($this->phptype))) {
  185.             return PEAR::raiseError(nullMDB2_ERROR_NOT_FOUND,
  186.                 nullnull'extension '.$this->phptype.' is not compiled into PHP',
  187.                 'MDB2_Error'true);
  188.         }
  189.     }
  190.  
  191.     // }}}
  192.     // {{{ _close()
  193.     /**
  194.      * all the RDBMS specific things needed close a DB connection
  195.      *
  196.      * @access private
  197.      *
  198.      */
  199.     function _close()
  200.     {
  201.         // take this from the corresponding Metabase driver: Close()
  202.     }
  203.  
  204.     // }}}
  205.     // {{{ query()
  206.  
  207.     /**
  208.      * Send a query to the database and return any results
  209.      *
  210.      * @access public
  211.      *
  212.      * @param string  $query  the SQL query
  213.      * @param array   $types  array that contains the types of the columns in
  214.      *                         the result set
  215.      *
  216.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  217.      */
  218.     function &query($query$types = null)
  219.     {
  220.         // take this from the corresponding Metabase driver: Query()
  221.     }
  222.  
  223.     // }}}
  224.     // {{{ subSelect()
  225.  
  226.     /**
  227.      * simple subselect emulation for Mysql
  228.      *
  229.      * @access public
  230.      *
  231.      * @param string $query the SQL query for the subselect that may only
  232.      *                       return a column
  233.      * @param string $quote determines if the data needs to be quoted before
  234.      *                       being returned
  235.      *
  236.      * @return string the query
  237.      */
  238.     function subSelect($query$quote = false)
  239.     {
  240.         // This is a new method that only needs to be added if the RDBMS does
  241.         // not support sub-selects. See the MySQL driver for an example
  242.     }
  243.  
  244.     // }}}
  245.     // {{{ replace()
  246.  
  247.     /**
  248.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  249.      * query, except that if there is already a row in the table with the same
  250.      * key field values, the REPLACE query just updates its values instead of
  251.      * inserting a new row.
  252.      *
  253.      * The REPLACE type of query does not make part of the SQL standards. Since
  254.      * practically only MySQL implements it natively, this type of query is
  255.      * emulated through this method for other DBMS using standard types of
  256.      * queries inside a transaction to assure the atomicity of the operation.
  257.      *
  258.      * @access public
  259.      *
  260.      * @param string $table name of the table on which the REPLACE query will
  261.      *   be executed.
  262.      * @param array $fields associative array that describes the fields and the
  263.      *   values that will be inserted or updated in the specified table. The
  264.      *   indexes of the array are the names of all the fields of the table. The
  265.      *   values of the array are also associative arrays that describe the
  266.      *   values and other properties of the table fields.
  267.      *
  268.      *   Here follows a list of field properties that need to be specified:
  269.      *
  270.      *     Value:
  271.      *           Value to be assigned to the specified field. This value may be
  272.      *           of specified in database independent type format as this
  273.      *           function can perform the necessary datatype conversions.
  274.      *
  275.      *     Default:
  276.      *           this property is required unless the Null property
  277.      *           is set to 1.
  278.      *
  279.      *     Type
  280.      *           Name of the type of the field. Currently, all types Metabase
  281.      *           are supported except for clob and blob.
  282.      *
  283.      *     Default: text
  284.      *
  285.      *     Null
  286.      *           Boolean property that indicates that the value for this field
  287.      *           should be set to null.
  288.      *
  289.      *           The default value for fields missing in INSERT queries may be
  290.      *           specified the definition of a table. Often, the default value
  291.      *           is already null, but since the REPLACE may be emulated using
  292.      *           an UPDATE query, make sure that all fields of the table are
  293.      *           listed in this function argument array.
  294.      *
  295.      *     Default: 0
  296.      *
  297.      *     Key
  298.      *           Boolean property that indicates that this field should be
  299.      *           handled as a primary key or at least as part of the compound
  300.      *           unique index of the table that will determine the row that will
  301.      *           updated if it exists or inserted a new row otherwise.
  302.      *
  303.      *           This function will fail if no key field is specified or if the
  304.      *           value of a key field is set to null because fields that are
  305.      *           part of unique index they may not be null.
  306.      *
  307.      *     Default: 0
  308.      *
  309.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  310.      */
  311.     function replace($table$fields)
  312.     {
  313.         // take this from the corresponding Metabase driver: Replace()
  314.     }
  315.  
  316.     // }}}
  317.     // {{{ nextId()
  318.  
  319.     /**
  320.      * returns the next free id of a sequence
  321.      *
  322.      * @param string  $seq_name name of the sequence
  323.      * @param boolean $ondemand when true the seqence is
  324.      *                           automatic created, if it
  325.      *                           not exists
  326.      *
  327.      * @return mixed MDB2 Error Object or id
  328.      * @access public
  329.      */
  330.     function nextID($seq_name$ondemand = false)
  331.     {
  332.         // take this from the corresponding PEAR DB driver: nextId()
  333.     }
  334.  
  335.  
  336.     // }}}
  337.     // {{{ currId()
  338.  
  339.     /**
  340.      * returns the current id of a sequence
  341.      *
  342.      * @param string  $seq_name name of the sequence
  343.      * @return mixed MDB2 Error Object or id
  344.      * @access public
  345.      */
  346.     function currID($seq_name)
  347.     {
  348.         // take this from the corresponding Metabase driver: GetSequenceCurrentValue()
  349.     }
  350. }
  351.  
  352. class MDB2_Result_xxx extends MDB2_Result_Common
  353. {
  354.     // }}}
  355.     // {{{ constructor
  356.  
  357.     /**
  358.      * Constructor
  359.      */
  360.     function MDB2_Result_xxx(&$mdb&$result)
  361.     {
  362.         parent::MDB2_Result_Common($mdb$result);
  363.     }
  364.  
  365.     // }}}
  366.     // {{{ fetch()
  367.  
  368.     /**
  369.     * fetch value from a result set
  370.     *
  371.     * @param int    $rownum    number of the row where the data can be found
  372.     * @param int    $field    field number where the data can be found
  373.     * @return mixed string on success, a MDB2 error on failure
  374.     * @access public
  375.     */
  376.     function fetch($rownum = 0$field = 0)
  377.     {
  378.         // take this from the corresponding Metabase driver: FetchResult()
  379.     }
  380.  
  381.     // }}}
  382.     // {{{ fetchRow()
  383.  
  384.     /**
  385.      * Fetch a row and insert the data into an existing array.
  386.      *
  387.      * @param int       $fetchmode  how the array data should be indexed
  388.      * @return int data array on success, a MDB2 error on failure
  389.      * @access public
  390.      */
  391.     function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT)
  392.     {
  393.         // take this from the corresponding Metabase driver: FetchResultArray()
  394.         // possibly you also need to take code from Metabases FetchRow() method
  395.         // note Metabases FetchRow() method should not be confused with MDB2's fetchRow()
  396.     }
  397.  
  398.     // }}}
  399.     // {{{ getColumnNames()
  400.  
  401.     /**
  402.      * Retrieve the names of columns returned by the DBMS in a query result.
  403.      *
  404.      * @return mixed                an associative array variable
  405.      *                               that will hold the names of columns. The
  406.      *                               indexes of the array are the column names
  407.      *                               mapped to lower case and the values are the
  408.      *                               respective numbers of the columns starting
  409.      *                               from 0. Some DBMS may not return any
  410.      *                               columns when the result set does not
  411.      *                               contain any rows.
  412.      *
  413.      *                               a MDB2 error on failure
  414.      * @access public
  415.      */
  416.     function getColumnNames()
  417.     {
  418.         // take this from the corresponding Metabase driver: GetColumnNames()
  419.     }
  420.  
  421.     // }}}
  422.     // {{{ numCols()
  423.  
  424.     /**
  425.      * Count the number of columns returned by the DBMS in a query result.
  426.      *
  427.      * @access public
  428.      * @return mixed integer value with the number of columns, a MDB2 error
  429.      *                        on failure
  430.      */
  431.     function numCols()
  432.     {
  433.         // take this from the corresponding Metabase driver: NumberOfColumns()
  434.     }
  435.  
  436.     // }}}
  437.     // {{{ resultIsNull()
  438.  
  439.     /**
  440.      * Determine whether the value of a query result located in given row and
  441.      *    field is a null.
  442.      *
  443.      * @param int $rownum number of the row where the data can be found
  444.      * @param int $field field number where the data can be found
  445.      * @return mixed true or false on success, a MDB2 error on failure
  446.      * @access public
  447.      */
  448.     function resultIsNull($rownum$field)
  449.     {
  450.         // take this from the corresponding Metabase driver: ResultIsNull()
  451.     }
  452.  
  453.     // }}}
  454.     // {{{ nextResult()
  455.  
  456.     /**
  457.      * Move the internal mysql result pointer to the next available result
  458.      * Currently not supported
  459.      *
  460.      * @return true if a result is available otherwise return false
  461.      * @access public
  462.      */
  463.     function nextResult()
  464.     {
  465.         // take this from the corresponding PEAR DB driver: nextResult()
  466.     }
  467.  
  468.     // }}}
  469.     // {{{ free()
  470.  
  471.     /**
  472.      * Free the internal resources associated with result.
  473.      *
  474.      * @return boolean true on success, false if result is invalid
  475.      * @access public
  476.      */
  477.     function free()
  478.     {
  479.         // take this from the corresponding Metabase driver: FreeResult()
  480.     }
  481. }
  482.  
  483. class MDB2_BufferedResult_xxx extends MDB2_Result_xxx
  484. {
  485.     // }}}
  486.     // {{{ constructor
  487.  
  488.     /**
  489.      * Constructor
  490.      */
  491.     function MDB2_BufferedResult_xxx(&$mdb&$result)
  492.     {
  493.         parent::MDB2_Result_xxx($mdb$result);
  494.     }
  495.  
  496.     // }}}
  497.     // {{{ seek()
  498.  
  499.     /**
  500.     * seek to a specific row in a result set
  501.     *
  502.     * @param int    $rownum    number of the row where the data can be found
  503.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  504.     * @access public
  505.     */
  506.     function seek($rownum = 0)
  507.     {
  508.         // take this from the corresponding Metabase driver: FetchResultArray()
  509.         // possibly you also need to take code from Metabases FetchRow() method
  510.         // note Metabases FetchRow() method should not be confused with MDB2's fetchRow()
  511.     }
  512.  
  513.     // }}}
  514.     // {{{ valid()
  515.  
  516.     /**
  517.     * check if the end of the result set has been reached
  518.     *
  519.     * @return mixed true or false on sucess, a MDB2 error on failure
  520.     * @access public
  521.     */
  522.     function valid()
  523.     {
  524.         $numrows $this->numRows();
  525.         if (MDB2::isError($numrows)) {
  526.             return $numrows;
  527.         }
  528.         return $this->rownum $numrows - 1;
  529.     }
  530.  
  531.     // }}}
  532.     // {{{ numRows()
  533.  
  534.     /**
  535.     * returns the number of rows in a result object
  536.     *
  537.     * @return mixed MDB2 Error Object or the number of rows
  538.     * @access public
  539.     */
  540.     function numRows()
  541.     {
  542.         // take this from the corresponding Metabase driver: NumberOfRows()
  543.     }
  544. }
  545.  
  546. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.