Source for file database.cls.php

Documentation is available at database.cls.php

  1. <?php
  2.  
  3.     /***********************************************\
  4.      * N2F Yverdon v0                              *
  5.      * Copyright (c) 2009 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 24 2009-09-12 11:41:30Z 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        Name of extension to add
  53.          * @param array $callbacks    Array of callbacks for extension
  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$options null{
  204.             $result new n2f_database_query($sql$this$options);
  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$options null{
  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->addData('options'$options);
  271.  
  272.             $this->addEvent(N2F_DBEVT_PARAMETER_ADDED);
  273.             $this->addEvent(N2F_DBEVT_QUERY_EXECUTED);
  274.             $this->addEvent(N2F_DBEVT_ROW_RETRIEVED);
  275.             $this->addEvent(N2F_DBEVT_ROWS_RETRIEVED);
  276.             $this->addEvent(N2F_DBEVT_LAST_INC_RETRIEVED);
  277.             $this->addEvent(N2F_DBEVT_NUMROWS_RETRIEVED);
  278.             $this->addEvent(N2F_DBEVT_RESULT_RETRIEVED);
  279.             $this->addEvent(N2F_DBEVT_ADD_PARAMETERtrue);
  280.             $this->addEvent(N2F_DBEVT_EXECUTE_QUERYtrue);
  281.             $this->addEvent(N2F_DBEVT_GET_ROWtrue);
  282.             $this->addEvent(N2F_DBEVT_GET_ROWStrue);
  283.             $this->addEvent(N2F_DBEVT_GET_LAST_INCtrue);
  284.             $this->addEvent(N2F_DBEVT_GET_NUMROWStrue);
  285.             $this->addEvent(N2F_DBEVT_GET_RESULTtrue);
  286.  
  287.             if ($db->extension === null || empty($db->extension)) {
  288.                 if ($n2f->debug->showLevel(N2F_DEBUG_ERROR)) {
  289.                     $n2f->debug->throwError(N2F_ERROR_DB_EXTENSION_EMPTYS('N2F_ERROR_DB_EXTENSION_EMPTY')'system/classes/database.cls.php');
  290.                 }
  291.             else if ($db->isOpen(!== true{
  292.                 if ($n2f->debug->showLevel(N2F_DEBUG_ERROR)) {
  293.                     $n2f->debug->throwError(N2F_ERROR_DB_NOT_LOADEDS('N2F_ERROR_DB_NOT_LOADED')'system/classes/database.cls.php');
  294.                 }
  295.             else {
  296.                 $this->hookEvent(N2F_DBEVT_ADD_PARAMETER$_n2f_db_extensions[$db->extension][N2F_DBEVT_ADD_PARAMETER]);
  297.                 $this->hookEvent(N2F_DBEVT_EXECUTE_QUERY$_n2f_db_extensions[$db->extension][N2F_DBEVT_EXECUTE_QUERY]);
  298.                 $this->hookEvent(N2F_DBEVT_GET_ROW$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_ROW]);
  299.                 $this->hookEvent(N2F_DBEVT_GET_ROWS$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_ROWS]);
  300.                 $this->hookEvent(N2F_DBEVT_GET_LAST_INC$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_LAST_INC]);
  301.                 $this->hookEvent(N2F_DBEVT_GET_NUMROWS$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_NUMROWS]);
  302.                 $this->hookEvent(N2F_DBEVT_GET_RESULT$_n2f_db_extensions[$db->extension][N2F_DBEVT_GET_RESULT]);
  303.             }
  304.  
  305.             if ($n2f->debug->showLevel(N2F_DEBUG_NOTICE)) {
  306.                 $n2f->debug->throwNotice(N2F_NOTICE_DB_QUERY_CREATEDS('N2F_NOTICE_DB_QUERY_CREATED'array($db->extension$sql))'system/classes/database.cls.php');
  307.             }
  308.  
  309.             return($this);
  310.         }
  311.  
  312.         /**
  313.          * Mechanism for storing data in the n2f_database_query object's internal data container.
  314.          *
  315.          * @param mixed $key 
  316.          * @param mixed $data 
  317.          * @return n2f_database 
  318.          */
  319.         public function addData($key$data{
  320.             $this->_internalData[$key$data;
  321.  
  322.             return($this);
  323.         }
  324.  
  325.         /**
  326.          * Retrieves data from the n2f_database_query object's internal data container.
  327.          *
  328.          * @param mixed $key 
  329.          * @return mixed 
  330.          */
  331.         public function getData($key{
  332.             if (isset($this->_internalData[$key])) {
  333.                 return($this->_internalData[$key]);
  334.             }
  335.  
  336.             return(null);
  337.         }
  338.  
  339.         /**
  340.          * Adds a parameter to the query stack.
  341.          *
  342.          * @param string $key 
  343.          * @param mixed $value 
  344.          * @param mixed $type 
  345.          * @return n2f_database_query 
  346.          */
  347.         public function addParam($key$value$type{
  348.             global $n2f;
  349.  
  350.             $result $this->hitEvent(N2F_DBEVT_ADD_PARAMETERarray(&$this$key$value$type));
  351.  
  352.             if ($result !== false && $n2f->cfg->dbg->level >= N2F_DEBUG_NOTICE{
  353.                 $n2f->debug->throwNotice(N2F_NOTICE_DB_PARAMETER_ADDEDS('N2F_NOTICE_DB_PARAMETER_ADDED'array($key))'system/classes/database.cls.php');
  354.             }
  355.  
  356.             return($this);
  357.         }
  358.  
  359.         /**
  360.          * Adds an array of parameters to the query stack.
  361.          *
  362.          * @param array $params 
  363.          * @return n2f_database_query 
  364.          */
  365.         public function addParams(array $params{
  366.             global $n2f;
  367.  
  368.             if (count($params1{
  369.                 if ($n2f->debug->showLevel(N2F_DEBUG_WARN)) {
  370.                     $n2f->debug->throwWarning(N2F_WARN_DB_PARAMETERS_NOT_SUPPLIEDS('N2F_WARN_DB_PARAMETERS_NOT_SUPPLIED'array($this->query))'system/classes/database.cls.php');
  371.                 }
  372.  
  373.                 return($this);
  374.             }
  375.  
  376.             foreach (array_values($paramsas $param{
  377.                 if (count($param!= 3{
  378.                     if ($n2f->debug->showLevel(N2F_DEBUG_WARN)) {
  379.                         $n2f->debug->throwWarning(N2F_WARN_DB_INVALID_PARAMETERS('N2F_WARN_DB_INVALID_PARAMETER'array(debugEcho($param)))'system/classes/database.cls.php');
  380.                     }
  381.  
  382.                     continue;
  383.                 }
  384.  
  385.                 $this->addParam($param[0]$param[1]$param[2]);
  386.             }
  387.  
  388.             return($this);
  389.         }
  390.  
  391.         /**
  392.          * Executes the query.
  393.          *
  394.          * @return n2f_database_query 
  395.          */
  396.         public function execQuery({
  397.             global $n2f;
  398.  
  399.             if (count($this->_errors1{
  400.                 $this->hitEvent(N2F_DBEVT_EXECUTE_QUERYarray(&$this));
  401.                 $this->db->queries += 1;
  402.  
  403.                 if ($n2f->cfg->dbg->level >= N2F_DEBUG_NOTICE{
  404.                     $n2f->debug->throwNotice(N2F_NOTICE_DB_QUERY_EXECUTEDS('N2F_NOTICE_DB_QUERY_EXECUTED')'system/classes/database.cls.php');
  405.                 }
  406.             }
  407.  
  408.             return($this);
  409.         }
  410.  
  411.         /**
  412.          * Adds an error to the n2f_database_query object's error stack.
  413.          *
  414.          * @param string $string 
  415.          * @return n2f_database_query 
  416.          */
  417.         public function addError($string{
  418.             if (empty($string)) {
  419.                 return($this);
  420.             }
  421.  
  422.             $this->_errors[$string;
  423.  
  424.             return($this);
  425.         }
  426.  
  427.         /**
  428.          * Returns true or false based on whether or not an error has occurred.
  429.          *
  430.          * @return boolean 
  431.          */
  432.         public function isError({
  433.             if (count($this->_errors0{
  434.                 return(true);
  435.             }
  436.  
  437.             return(false);
  438.         }
  439.  
  440.         /**
  441.          * Returns the last populated error string.
  442.          *
  443.          * @return string 
  444.          */
  445.         public function fetchError({
  446.             return($this->_errors[count($this->_errors1]);
  447.         }
  448.  
  449.         /**
  450.          * Returns the error stack.
  451.          *
  452.          * @return array 
  453.          */
  454.         public function fetchErrors({
  455.             return($this->_errors);
  456.         }
  457.  
  458.         /**
  459.          * Fetches a single row from the result.
  460.          *
  461.          * @return mixed 
  462.          */
  463.         public function fetchRow({
  464.             return($this->hitEvent(N2F_DBEVT_GET_ROWarray(&$this)));
  465.         }
  466.  
  467.         /**
  468.          * Fetches all rows from the result.
  469.          *
  470.          * @return mixed 
  471.          */
  472.         public function fetchRows({
  473.             return($this->hitEvent(N2F_DBEVT_GET_ROWSarray(&$this)));
  474.         }
  475.  
  476.         /**
  477.          * Fetches a specific field from the result.
  478.          *
  479.          * @param integer $offset 
  480.          * @param string $field_name 
  481.          * @return mixed 
  482.          */
  483.         public function fetchResult($offset$field_name{
  484.             return($this->hitEvent(N2F_DBEVT_GET_RESULTarray(&$this$offset$field_name)));
  485.         }
  486.  
  487.         /**
  488.          * Fetches the last automatically incremented value from the query (if applicable).
  489.          *
  490.          * @return mixed 
  491.          */
  492.         public function fetchInc($params null{
  493.             if ($params !== null{
  494.                 return($this->hitEvent(N2F_DBEVT_GET_LAST_INCarray(&$this$params)));
  495.             }
  496.  
  497.             return($this->hitEvent(N2F_DBEVT_GET_LAST_INCarray(&$this)));
  498.         }
  499.  
  500.         /**
  501.          * Returns the number of rows from the result.
  502.          *
  503.          * @return integer 
  504.          */
  505.         public function numRows({
  506.             return($this->hitEvent(N2F_DBEVT_GET_NUMROWSarray(&$this)));
  507.         }
  508.     }
  509.  
  510. ?>

Documentation generated on Sat, 23 Jan 2010 11:13:34 -0500 by phpDocumentor 1.4.1