Source for file database.cls.php

Documentation is available at database.cls.php

  1. <?php
  2.  
  3.     /***********************************************\
  4.      * N2F Yverdon v0                              *
  5.      * Copyright (c) 2008 Zibings Incorporated     *
  6.      *                                             *
  7.      * You should have received a copy of the      *
  8.      * Microsoft Reciprocal License along with     *
  9.      * this program.  If not, see:                 *
  10.      * <http://opensource.org/licenses/ms-rl.html> *
  11.     \***********************************************/
  12.  
  13.     /*
  14.      * $Id: database.cls.php 166 2008-10-31 06:34:08Z amale $
  15.      */
  16.  
  17.     global $_n2f_db_extensions;
  18.  
  19.     /**
  20.      * Core database class for N2 Framework Yverdon.
  21.      *
  22.      */
  23.     class n2f_database extends n2f_events {
  24.         /**
  25.          * Connection resource for this n2f_database object.
  26.          *
  27.          * @var resource 
  28.          */
  29.         public $conn;
  30.         /**
  31.          * Number of queries performed by this n2f_database object.
  32.          *
  33.          * @var integer 
  34.          */
  35.         public $queries;
  36.         /**
  37.          * The extension currently in use by this n2f_database object.
  38.          *
  39.          * @var string 
  40.          */
  41.         public $extension;
  42.         /**
  43.          * Internal container for object data.
  44.          *
  45.          * @var array 
  46.          */
  47.         protected $_internalData;
  48.  
  49.         /**
  50.          * Static function for adding database extensions to the available list.
  51.          *
  52.          * @param string $name 
  53.          * @param array $callbacks 
  54.          * @return null 
  55.          */
  56.         public static function addExtension($namearray $callbacks{
  57.             if (empty($name)) {
  58.                 return(null);
  59.             }
  60.  
  61.             if (count($callbacks<> 10 ||
  62.                 !isset($callbacks[N2F_DBEVT_OPEN_CONNECTION]|| !isset($callbacks[N2F_DBEVT_CLOSE_CONNECTION]|| !isset($callbacks[N2F_DBEVT_CHECK_CONNECTION]|| !isset($callbacks[N2F_DBEVT_ADD_PARAMETER])
  63.                  || !isset($callbacks[N2F_DBEVT_EXECUTE_QUERY]|| !isset($callbacks[N2F_DBEVT_GET_ROW]|| !isset($callbacks[N2F_DBEVT_GET_ROWS]|| !isset($callbacks[N2F_DBEVT_GET_LAST_INC])
  64.                  || !isset($callbacks[N2F_DBEVT_GET_NUMROWS]|| !isset($callbacks[N2F_DBEVT_GET_RESULT])) {
  65.                 return(null);
  66.             }
  67.  
  68.             global $_n2f_db_extensions;
  69.  
  70.             $_n2f_db_extensions[$namearray(
  71.                 N2F_DBEVT_OPEN_CONNECTION        => $callbacks[N2F_DBEVT_OPEN_CONNECTION],
  72.                 N2F_DBEVT_CLOSE_CONNECTION        => $callbacks[N2F_DBEVT_CLOSE_CONNECTION],
  73.                 N2F_DBEVT_CHECK_CONNECTION        => $callbacks[N2F_DBEVT_CHECK_CONNECTION],
  74.                 N2F_DBEVT_ADD_PARAMETER            => $callbacks[N2F_DBEVT_ADD_PARAMETER],
  75.                 N2F_DBEVT_EXECUTE_QUERY            => $callbacks[N2F_DBEVT_EXECUTE_QUERY],
  76.                 N2F_DBEVT_GET_ROW                => $callbacks[N2F_DBEVT_GET_ROW],
  77.                 N2F_DBEVT_GET_ROWS                => $callbacks[N2F_DBEVT_GET_ROWS],
  78.                 N2F_DBEVT_GET_LAST_INC            => $callbacks[N2F_DBEVT_GET_LAST_INC],
  79.                 N2F_DBEVT_GET_NUMROWS            => $callbacks[N2F_DBEVT_GET_NUMROWS],
  80.                 N2F_DBEVT_GET_RESULT            => $callbacks[N2F_DBEVT_GET_RESULT]
  81.             );
  82.  
  83.             return(null);
  84.         }
  85.  
  86.         /**
  87.          * Initializes a new n2f_database object.
  88.          *
  89.          * @param n2f_cls $n2f 
  90.          * @param string $ext 
  91.          * @return n2f_database 
  92.          */
  93.         public function __construct(n2f_cls &$n2f$ext{
  94.             parent::__construct();
  95.             $this->conn null;
  96.             $this->queries 0;
  97.             $this->_internalData array();
  98.             global $_n2f_db_extensions;
  99.  
  100.             $this->addEvent(N2F_DBEVT_CONNECTION_OPENED);
  101.             $this->addEvent(N2F_DBEVT_CONNECTION_CLOSED);
  102.             $this->addEvent(N2F_DBEVT_QUERY_CREATED);
  103.             $this->addEvent(N2F_DBEVT_OPEN_CONNECTIONtrue);
  104.             $this->addEvent(N2F_DBEVT_CLOSE_CONNECTIONtrue);
  105.             $this->addEvent(N2F_DBEVT_CHECK_CONNECTIONtrue);
  106.  
  107.             if (!isset($_n2f_db_extensions[$ext])) {
  108.                 if ($n2f->debug->showLevel(N2F_DEBUG_ERROR)) {
  109.                     $n2f->debug->throwError(N2F_ERROR_DB_EXTENSION_NOT_LOADEDS('N2F_ERROR_DB_EXTENSION_NOT_LOADED'array($ext))'system/classes/database.cls.php');
  110.                 }
  111.  
  112.                 $this->extension null;
  113.             else {
  114.                 $this->hookEvent(N2F_DBEVT_OPEN_CONNECTION$_n2f_db_extensions[$ext][N2F_DBEVT_OPEN_CONNECTION]);
  115.                 $this->hookEvent(N2F_DBEVT_CLOSE_CONNECTION$_n2f_db_extensions[$ext][N2F_DBEVT_CLOSE_CONNECTION]);
  116.                 $this->hookEvent(N2F_DBEVT_CHECK_CONNECTION$_n2f_db_extensions[$ext][N2F_DBEVT_CHECK_CONNECTION]);
  117.                 $this->extension $ext;
  118.             }
  119.  
  120.             return($this);
  121.         }
  122.  
  123.         /**
  124.          * Mechanism for storing data in the n2f_database object's internal data container.
  125.          *
  126.          * @param mixed $key 
  127.          * @param mixed $data 
  128.          * @return n2f_database 
  129.          */
  130.         public function addData($key$data{
  131.             $this->_internalData[$key$data;
  132.  
  133.             return($this);
  134.         }
  135.  
  136.         /**
  137.          * Retrieves data from the n2f_database object's internal data container.
  138.          *
  139.          * @param mixed $key 
  140.          * @return mixed 
  141.          */
  142.         public function getData($key{
  143.             if (isset($this->_internalData[$key])) {
  144.                 return($this->_internalData[$key]);
  145.             }
  146.  
  147.             return(null);
  148.         }
  149.  
  150.         /**
  151.          * Opens the connection for this n2f_database object.
  152.          *
  153.          * @param array $args 
  154.          * @return n2f_database 
  155.          */
  156.         public function open({
  157.             global $n2f;
  158.  
  159.             $this->hitEvent(N2F_DBEVT_OPEN_CONNECTIONarray($this$n2f->cfg->db));
  160.             $this->hitEvent(N2F_DBEVT_CONNECTION_OPENEDarray($this));
  161.  
  162.             if ($n2f->debug->showLevel(N2F_DEBUG_NOTICE)) {
  163.                 $n2f->debug->throwNotice(N2F_NOTICE_DB_CONNECTION_OPENEDS('N2F_NOTICE_DB_CONNECTION_OPENED'array($this->extension))'system/classes/database.cls.php');
  164.             }
  165.  
  166.             return($this);
  167.         }
  168.  
  169.         /**
  170.          * Closes the connection for this n2f_database object.
  171.          *
  172.          * @return n2f_database 
  173.          */
  174.         public function close({
  175.             global $n2f;
  176.  
  177.             $this->hitEvent(N2F_DBEVT_CLOSE_CONNECTIONarray($this));
  178.             $this->hitEvent(N2F_DBEVT_CONNECTION_CLOSEDarray($this));
  179.             $this->conn null;
  180.  
  181.             if ($n2f->debug->showLevel(N2F_DEBUG_NOTICE)) {
  182.                 $n2f->debug->throwNotice(N2F_NOTICE_DB_CONNECTION_CLOSEDS('N2F_NOTICE_DB_CONNECTION_CLOSED'array($this->extension))'system/classes/database.cls.php');
  183.             }
  184.  
  185.             return($this);
  186.         }
  187.  
  188.         /**
  189.          * Returns true or false based on whether or not the object's connection is active.
  190.          *
  191.          * @return boolean 
  192.          */
  193.         public function isOpen({
  194.             return((bool)$this->hitEvent(N2F_DBEVT_CHECK_CONNECTIONarray($this)));
  195.         }
  196.  
  197.         /**
  198.          * Produces a new n2f_database_query object from the given query.
  199.          *
  200.          * @param string $sql 
  201.          * @return n2f_database_query 
  202.          */
  203.         public function query($sql{
  204.             $result new n2f_database_query($sql$this);
  205.             $this->hitEvent(N2F_DBEVT_QUERY_CREATEDarray($result$this));
  206.  
  207.             return($result);
  208.         }
  209.     }
  210.  
  211.     /**
  212.      * Core database query class for N2 Framework Yverdon.
  213.      *
  214.      */
  215.     class n2f_database_query extends n2f_events {
  216.         /**
  217.          * Internal reference to the global database handler.
  218.          *
  219.          * @var n2f_database 
  220.          */
  221.         public $db;
  222.         /**
  223.          * SQL string used for this query.
  224.          *
  225.          * @var string 
  226.          */
  227.         public $query;
  228.         /**
  229.          * Collection of parameters for the current query.
  230.          *
  231.          * @var array 
  232.          */
  233.         public $params;
  234.         /**
  235.          * Holds the current result set for the query if applicable.
  236.          *
  237.          * @var result 
  238.          */
  239.         public $result;
  240.         /**
  241.          * Latest error returned by the query.
  242.          *
  243.          * @var array 
  244.          */
  245.         private $_errors;
  246.         /**
  247.          * Internal container for object data.
  248.          *
  249.          * @var array 
  250.          */
  251.         protected $_internalData;
  252.  
  253.         /**
  254.          * Initializes a new n2f_database_query object.
  255.          *
  256.          * @param string $sql 
  257.          * @param n2f_database $db 
  258.          * @return n2f_database_query 
  259.          */
  260.         public function __construct($sqln2f_database &$db{
  261.             global $n2f$_n2f_db_extensions;
  262.             parent::__construct();
  263.             $this->db = $db;
  264.             $this->query = $sql;
  265.             $this->errors array();
  266.             $this->params = array();
  267.             $this->result = null;
  268.             $this->_internalData = array();
  269.  
  270.             $this->addEvent(N2F_DBEVT_PARAMETER_ADDED);
  271.             $this->addEvent(N2F_DBEVT_QUERY_EXECUTED);
  272.             $this->addEvent(N2F_DBEVT_ROW_RETRIEVED);
  273.             $this->addEvent(N2F_DBEVT_ROWS_RETRIEVED);
  274.             $this->addEvent(N2F_DBEVT_LAST_INC_RETRIEVED);
  275.             $this->addEvent(N2F_DBEVT_NUMROWS_RETRIEVED);
  276.             $this->addEvent(N2F_DBEVT_RESULT_RETRIEVED);
  277.             $this->addEvent(N2F_DBEVT_ADD_PARAMETERtrue);
  278.             $this->addEvent(N2F_DBEVT_EXECUTE_QUERYtrue);
  279.             $this->addEvent(N2F_DBEVT_GET_ROWtrue);
  280.             $this->addEvent(N2F_DBEVT_GET_ROWStrue);
  281.             $this->addEvent(N2F_DBEVT_GET_LAST_INCtrue);
  282.             $this->addEvent(N2F_DBEVT_GET_NUMROWStrue);
  283.             $this->addEvent(N2F_DBEVT_GET_RESULTtrue);
  284.  
  285.             if ($db->extension === null || empty($db->extension)) {
  286.                 if ($n2f->debug->showLevel(N2F_DEBUG_ERROR)) {
  287.                     $n2f->debug->throwError(N2F_ERROR_DB_EXTENSION_EMPTYS('N2F_ERROR_DB_EXTENSION_EMPTY')'system/classes/database.cls.php');
  288.                 }
  289.             else if ($db->isOpen(!== true{
  290.                 if ($n2f->debug->showLevel(N2F_DEBUG_ERROR)) {
  291.                     $n2f->debug->throwError(N2F_ERROR_DB_NOT_LOADEDS('N2F_ERROR_DB_NOT_LOADED')'system/classes/database.cls.php');
  292.                 }
  293.             else {
  294.                 $this->hookEvent(N2F_DBEVT_ADD_PARAMETER$_n2f_db_extensions[$db->extension][N2F_DBEVT_ADD_PARAMETER]);
  295.                 $this->hookEvent(N2F_DBEVT_EXECUTE_QUERY$_n2f_db_extensions[$db->extension][N2F_DBEVT_EXECUTE_QUERY]);
  296.                 $this->hookEvent(N2F_DBEVT_GET_ROW$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_ROW]);
  297.                 $this->hookEvent(N2F_DBEVT_GET_ROWS$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_ROWS]);
  298.                 $this->hookEvent(N2F_DBEVT_GET_LAST_INC$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_LAST_INC]);
  299.                 $this->hookEvent(N2F_DBEVT_GET_NUMROWS$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_NUMROWS]);
  300.                 $this->hookEvent(N2F_DBEVT_GET_RESULT$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_RESULT]);
  301.             }
  302.  
  303.             if ($n2f->debug->showLevel(N2F_DEBUG_NOTICE)) {
  304.                 $n2f->debug->throwNotice(N2F_NOTICE_DB_QUERY_CREATEDS('N2F_NOTICE_DB_QUERY_CREATED'array($db->extension$sql))'system/classes/database.cls.php');
  305.             }
  306.  
  307.             return($this);
  308.         }
  309.  
  310.         /**
  311.          * Mechanism for storing data in the n2f_database_query object's internal data container.
  312.          *
  313.          * @param mixed $key 
  314.          * @param mixed $data 
  315.          * @return n2f_database 
  316.          */
  317.         public function addData($key$data{
  318.             $this->_internalData[$key$data;
  319.  
  320.             return($this);
  321.         }
  322.  
  323.         /**
  324.          * Retrieves data from the n2f_database_query object's internal data container.
  325.          *
  326.          * @param mixed $key 
  327.          * @return mixed 
  328.          */
  329.         public function getData($key{
  330.             if (isset($this->_internalData[$key])) {
  331.                 return($this->_internalData[$key]);
  332.             }
  333.  
  334.             return(null);
  335.         }
  336.  
  337.         /**
  338.          * Adds a parameter to the query stack.
  339.          *
  340.          * @param string $key 
  341.          * @param mixed $value 
  342.          * @param mixed $type 
  343.          * @return n2f_database_query 
  344.          */
  345.         public function addParam($key$value$type{
  346.             global $n2f;
  347.  
  348.             $result $this->hitEvent(N2F_DBEVT_ADD_PARAMETERarray($this$key$value$type));
  349.  
  350.             if ($result !== false && $n2f->cfg->dbg->level >= N2F_DEBUG_NOTICE{
  351.                 $n2f->debug->throwNotice(N2F_NOTICE_DB_PARAMETER_ADDEDS('N2F_NOTICE_DB_PARAMETER_ADDED'array($key))'system/classes/database.cls.php');
  352.             }
  353.  
  354.             return($this);
  355.         }
  356.  
  357.         /**
  358.          * Executes the query.
  359.          *
  360.          * @return n2f_database_query 
  361.          */
  362.         public function execQuery({
  363.             global $n2f;
  364.  
  365.             if (count($this->_errors1{
  366.                 $this->hitEvent(N2F_DBEVT_EXECUTE_QUERYarray($this));
  367.                 $this->db->queries += 1;
  368.  
  369.                 if ($n2f->cfg->dbg->level >= N2F_DEBUG_NOTICE{
  370.                     $n2f->debug->throwNotice(N2F_NOTICE_DB_QUERY_EXECUTEDS('N2F_NOTICE_DB_QUERY_EXECUTED')'system/classes/database.cls.php');
  371.                 }
  372.             }
  373.  
  374.             return($this);
  375.         }
  376.  
  377.         /**
  378.          * Adds an error to the n2f_database_query object's error stack.
  379.          *
  380.          * @param string $string 
  381.          * @return n2f_database_query 
  382.          */
  383.         public function addError($string{
  384.             if (empty($string)) {
  385.                 return($this);
  386.             }
  387.  
  388.             $this->_errors[$string;
  389.  
  390.             return($this);
  391.         }
  392.  
  393.         /**
  394.          * Returns true or false based on whether or not an error has occurred.
  395.          *
  396.          * @return boolean 
  397.          */
  398.         public function isError({
  399.             if (count($this->_errors0{
  400.                 return(true);
  401.             }
  402.  
  403.             return(false);
  404.         }
  405.  
  406.         /**
  407.          * Returns the last populated error string.
  408.          *
  409.          * @return string 
  410.          */
  411.         public function fetchError({
  412.             return($this->_errors[count($this->_errors1]);
  413.         }
  414.  
  415.         /**
  416.          * Returns the error stack.
  417.          *
  418.          * @return array 
  419.          */
  420.         public function fetchErrors({
  421.             return($this->_errors);
  422.         }
  423.  
  424.         /**
  425.          * Fetches a single row from the result.
  426.          *
  427.          * @return mixed 
  428.          */
  429.         public function fetchRow({
  430.             return($this->hitEvent(N2F_DBEVT_GET_ROWarray($this)));
  431.         }
  432.  
  433.         /**
  434.          * Fetches all rows from the result.
  435.          *
  436.          * @return mixed 
  437.          */
  438.         public function fetchRows({
  439.             return($this->hitEvent(N2F_DBEVT_GET_ROWSarray($this)));
  440.         }
  441.  
  442.         /**
  443.          * Fetches a specific field from the result.
  444.          *
  445.          * @param integer $offset 
  446.          * @param string $field_name 
  447.          * @return mixed 
  448.          */
  449.         public function fetchResult($offset$field_name{
  450.             return($this->hitEvent(N2F_DBEVT_GET_RESULTarray($this$offset$field_name)));
  451.         }
  452.  
  453.         /**
  454.          * Fetches the last automatically incremented value from the query (if applicable).
  455.          *
  456.          * @return mixed 
  457.          */
  458.         public function fetchInc({
  459.             return($this->hitEvent(N2F_DBEVT_GET_LAST_INCarray($this)));
  460.         }
  461.  
  462.         /**
  463.          * Returns the number of rows from the result.
  464.          *
  465.          * @return integer 
  466.          */
  467.         public function numRows({
  468.             return($this->hitEvent(N2F_DBEVT_GET_NUMROWSarray($this)));
  469.         }
  470.     }
  471.  
  472. ?>

Documentation generated on Wed, 05 Nov 2008 13:15:11 -0500 by phpDocumentor 1.4.0