Source for file Database.php

Documentation is available at Database.php

  1. <?php
  2. /**
  3.  *    LAIKA FRAMEWORK Release Notes:
  4.  *
  5.  *    @filesource     Database.php
  6.  *
  7.  *    @version        0.1.0b
  8.  *    @package        Laika
  9.  *    @subpackage     core
  10.  *    @category       database
  11.  *    @date           2010-01-20 17:06:20 -0500 (Wed, 20 Jan 2010)
  12.  *
  13.  *    @author         Leonard M. Witzel <leonard_witzel@harvard.edu>
  14.  *    @copyright      Copyright (c) 2010  Harvard University <{@link http://lab.dce.harvard.edu}>
  15.  *
  16.  */
  17. /**
  18.  * Laika_Database class.
  19.  *
  20.  * Main wrapper class to abstract database transactions.
  21.  *
  22.  * @extends Laika_Abstract_Socket_Service
  23.  */
  24.  
  25. //-------------------------------------------------------------------
  26. //    VARIABLES
  27. //-------------------------------------------------------------------
  28.     protected static $instance;
  29.     private   static $database;
  30.     private   static $driver;
  31.     
  32. //-------------------------------------------------------------------
  33. //    METHODS
  34. //-------------------------------------------------------------------    
  35.     
  36.     /**
  37.      * init function.
  38.      * 
  39.      * @access public
  40.      * @static
  41.      * @return void 
  42.      */
  43.     public static function init(){
  44.  
  45.         ifempty(self::$instance) )      
  46.             $index Laika_Registry::get_record(__CLASS__);
  47.             ifisset($index) )                   
  48.                 self::$instance Laika_Registry::get_record(__CLASS__);
  49.             else
  50.                 parent::init()
  51.         return self::$instance;    
  52.     }
  53.     
  54.     /**
  55.      * load_database_driver function.
  56.      * 
  57.      * @access public
  58.      * @param  string $driver 
  59.      * @return void 
  60.      */
  61.     public function load_database_driver($driver){
  62.         $this::$database $driver::connect();
  63.     }
  64.     
  65.     /**
  66.      * connect function.
  67.      * 
  68.      * @access public
  69.      * @param  string $driver 
  70.      * @return void 
  71.      */
  72.     public function connect($driver){
  73.  
  74.         USE_PDO && extension_loaded('pdo'
  75.             $this::$driver NAME_SPACE.'Pdo_Driver' :
  76.             $this::$driver NAME_SPACE.ucfirst($driver).'_Driver';
  77.         try
  78.             $this->load_database_driver($this::$driver);
  79.             Laika_Registry::register(__CLASS__,$this);
  80.         }
  81.         catch(Laika_Exception $e)
  82.             throw new Laika_Exception('DATABASE_NO_CONNECT'830)
  83.         }
  84.     }
  85.     
  86.     /**
  87.      * disconnect function.
  88.      * 
  89.      * @access public
  90.      * @return void 
  91.      */
  92.     public function disconnect(){
  93.         $db $this::$driver;
  94.         $this::$database $db::disconnect();   
  95.     }
  96.  
  97.     /**
  98.      * select function.
  99.      * 
  100.      * @access public
  101.      * @static
  102.      * @param mixed $subject 
  103.      * @param mixed $table 
  104.      * @return void 
  105.      */
  106.     public static function select($subject,$table,$condition){
  107.         $db self::init();
  108.         $driver $db::$database;
  109.         return $driver->select($subject,$table,$condition);
  110.     }       
  111.     
  112.     /**
  113.      * select_column function.
  114.      * 
  115.      * @access public
  116.      * @static
  117.      * @param mixed $subject 
  118.      * @param mixed $table 
  119.      * @return void 
  120.      */
  121.     public static function select_column($subject,$table){
  122.         $db self::init();
  123.         $driver $db::$database;
  124.         return $driver->select_column($subject,$table);
  125.     }
  126.       
  127.     /**
  128.      * select_by function.
  129.      * 
  130.      * @access public
  131.      * @static
  132.      * @param mixed $id 
  133.      * @param mixed $table 
  134.      * @return void 
  135.      */
  136.     public static function select_by($id,$table){
  137.         $db self::init();
  138.         $driver $db::$database;
  139.         return $driver->select_by($id,$table);
  140.     }
  141.     /**
  142.      * select_all function.
  143.      * 
  144.      * @access public
  145.      * @static
  146.      * @param mixed $table 
  147.      * @return void 
  148.      */
  149.     public static function select_all($table){
  150.         $db self::init();
  151.         $driver $db::$database;
  152.         return $driver->select_all($table);
  153.     }       
  154.  
  155.     /**
  156.      * select_all_where function.
  157.      * 
  158.      * @access public
  159.      * @static
  160.      * @param mixed $subject 
  161.      * @param mixed $table 
  162.      * @param mixed $condition 
  163.      * @return void 
  164.      */
  165.     public static function select_all_where($table$condition){
  166.         $db self::init();
  167.         $driver $db::$database;
  168.         return $driver->select_all_where($table$condition);
  169.     }
  170.  
  171.     /**
  172.      * select_where function.
  173.      * 
  174.      * @access public
  175.      * @static
  176.      * @param mixed $column 
  177.      * @param mixed $table 
  178.      * @param mixed $condition 
  179.      * @return void 
  180.      */
  181.     public static function select_where($subject$table$condition){
  182.         $db self::init();
  183.         $driver $db::$database;
  184.         return $driver->select_where($subject$table$condition);
  185.     }
  186.     
  187.     /**
  188.      * query function.
  189.      * 
  190.      * @access public
  191.      * @static
  192.      * @param mixed $statement 
  193.      * @param mixed $return 
  194.      * @return void 
  195.      */
  196.     public static function query($statement,$return){
  197.         $db self::init();
  198.         $driver $db::$database;
  199.         return $driver->query($statement,$return);    
  200.     }
  201.     
  202.     /**
  203.      * insert function.
  204.      * 
  205.      * @access public
  206.      * @static
  207.      * @param mixed $table 
  208.      * @param array $columns 
  209.      * @param array $values 
  210.      * @return void 
  211.      */
  212.     public static function insert($table,$columns,$values){
  213.         $db self::init();
  214.         $driver $db::$database;
  215.         return $driver->insert($table,$columns,$values)
  216.     }
  217.     
  218.     /**
  219.      * update function.
  220.      * 
  221.      * @access public
  222.      * @static
  223.      * @param mixed $table 
  224.      * @param mixed $record 
  225.      * @param mixed $data 
  226.      * @param mixed $condition 
  227.      * @return void 
  228.      */
  229.     public static function update($table$record$data$condition){
  230.         $db self::init();
  231.         $driver $db::$database;
  232.         return $driver->update($table$record$data$condition);     
  233.     }
  234.     
  235.     /**
  236.      * batch_update function.
  237.      * 
  238.      * @access public
  239.      * @static
  240.      * @param mixed $table 
  241.      * @param mixed $records 
  242.      * @param mixed $condition 
  243.      * @return void 
  244.      */
  245.     public static function batch_update($table,$records,$condition){
  246.         $db self::init();
  247.         $driver $db::$database;
  248.         return $driver->batch_update($table$records$condition);         
  249.     }
  250.     
  251.     /**
  252.      * add function.
  253.      * 
  254.      * @access public
  255.      * @static
  256.      * @param mixed $object 
  257.      * @return void 
  258.      */
  259.     public static function add($object){
  260.         $db self::init();
  261.         $driver $db::$database;        
  262.         return $driver->add($object);    
  263.     }
  264.     
  265.     /**
  266.      * delete function.
  267.      * 
  268.      * @access public
  269.      * @param mixed $table 
  270.      * @param mixed $id 
  271.      * @return void 
  272.      */
  273.     public static function delete($table,$id){
  274.         $db self::init();
  275.         $driver $db::$database;
  276.         $driver->delete($table,$id);  
  277.     }
  278.  
  279.     /**
  280.      * show function.
  281.      * 
  282.      * @access public
  283.      * @static
  284.      * @param mixed $table 
  285.      * @return void 
  286.      */
  287.     public static function show($table){
  288.         $db self::init();
  289.         $driver $db::$database;        
  290.         return $driver->show($table);        
  291.     }    
  292.     
  293.     /**
  294.      * reset_auto_increment function.
  295.      * 
  296.      * @access public
  297.      * @param mixed $table 
  298.      * @param mixed $id 
  299.      * @return void 
  300.      */
  301.     public static function reset_auto_increment($table,$id){
  302.         $db self::init();
  303.         $driver $db::$database;
  304.         $driver->reset_auto_increment($table,$id);
  305.     }
  306.     
  307.     /**
  308.      * alter function.
  309.      * 
  310.      * @access public
  311.      * @param mixed $table 
  312.      * @param mixed $condition 
  313.      * @return void 
  314.      */
  315.     public static function alter($table,$condition){
  316.         $db self::init();
  317.         $driver $db::$database;
  318.         $driver->alter($table,$condition);
  319.     }
  320.     
  321.     /**
  322.      * last function.
  323.      * 
  324.      * @access public
  325.      * @param mixed $table 
  326.      * @param mixed $limit 
  327.      * @return void 
  328.      */
  329.     public static function last($table,$limit,$conditions=NULL){
  330.         $db self::init();
  331.         $driver $db::$database;
  332.         return $driver->last($table,$limit,$conditions)
  333.     }
  334.     
  335.     /**
  336.      * first function.
  337.      * 
  338.      * @access public
  339.      * @param mixed $table 
  340.      * @param mixed $limit 
  341.      * @return void 
  342.      */
  343.     public static function first($table,$limit,$conditions=NULL){
  344.         $db self::init();
  345.         $driver $db::$database;
  346.         return $driver->first($table,$limit,$conditions);     
  347.     }
  348.      
  349.     /**
  350.      * count function.
  351.      * 
  352.      * @access public
  353.      * @static
  354.      * @param mixed $table 
  355.      * @return void 
  356.      */
  357.     public static function count(){
  358.         $args  func_get_args();
  359.         $db self::init();
  360.         $driver $db::$database;
  361.         
  362.         if(func_num_args()>1)
  363.             return $driver->count($args[0],$args[1]);
  364.         return $driver->count($args[0]);         
  365.     }
  366.     /**
  367.      * offset function.
  368.      * 
  369.      * @access public
  370.      * @static
  371.      * @param mixed $table 
  372.      * @param mixed $column 
  373.      * @param mixed $offset 
  374.      * @param mixed $limit 
  375.      * @return void 
  376.      */
  377.     public static function offset($table,$column,$limit,$offset){
  378.         $db self::init();
  379.         $driver $db::$database;
  380.         return $driver->offset($table,$column,$limit,$offset);    
  381.     }
  382.  
  383.     /**
  384.      * find_with_offset function.
  385.      * 
  386.      * @access public
  387.      * @static
  388.      * @param mixed $param 
  389.      * @param mixed $value 
  390.      * @param mixed $table 
  391.      * @param mixed $limit 
  392.      * @param mixed $offset 
  393.      * @return void 
  394.      */
  395.     public static function find_with_offset($conditions,$table,$limit,$offset){
  396.         $db self::init();
  397.         $driver $db::$database;
  398.         return $driver->find_with_offset($conditions,$table,$limit,$offset);     
  399.     }
  400.  
  401.  
  402.     /**
  403.      * find_with_offset_order_by function.
  404.      * 
  405.      * @access public
  406.      * @static
  407.      * @param mixed $conditions 
  408.      * @param mixed $table 
  409.      * @param mixed $limit 
  410.      * @param mixed $offset 
  411.      * @param mixed $by 
  412.      * @param mixed $order 
  413.      * @return void 
  414.      */
  415.     public static function find_with_offset_order_by($conditions,$table,$limit,$offset,$by,$order){
  416.         $db self::init();
  417.         $driver $db::$database;
  418.         return $driver->find_with_offset_order_by($conditions,$table,$limit,$offset,$by,$order);        
  419.     }
  420.     
  421.     /**
  422.      * create function.
  423.      * 
  424.      * @access public
  425.      * @static
  426.      * @param mixed $table 
  427.      * @param mixed $fields 
  428.      * @return void 
  429.      */
  430.     public static function create($table,$fields){
  431.         $db self::init();
  432.         $driver $db::$database;
  433.         $driver->create($table,$fields);              
  434.     }
  435.     
  436.     public static function drop(){}   
  437. }

Documentation generated on Sat, 19 May 2012 02:16:58 -0400 by phpDocumentor 1.4.4