root/website/lib/db/DBManager.class.php @ 2165

Revision 2165, 3.9 kB (checked in by kop-labs, 6 years ago)

Echo

Line 
1<?
2/******************************************************************************
3* DBManager - Abstract class to manage databases                                                            *
4* To do inheritance in this class, override the class vars (in constructor),    *
5* and the 'open' method                                                                                                             *
6******************************************************************************/
7class DBManager{
8   
9    #'True' database's value
10    var $_TRUE = true;
11   
12    #'False' databse's value
13    var $_FALSE = false;
14   
15    #connection's link resource
16    var $connection = null;
17
18    #last query done
19    var $lastQuery = null;
20
21    #result's link resource
22    var $result = null;
23       
24    #References to execute queries: the funcion, and params
25    #query
26    var $queryF = array('func'=>"", 'params'=>array());
27    #rows affected
28    var $rowsAF = array('func'=>"", 'params'=>array());
29    #close connection
30    var $closeF = array('func'=>"", 'params'=>array());
31    #fetch object
32    var $fetchObjectF = array('func'=>"", 'params'=>array());
33   
34    #get last insert id
35    var $getLastInsertF = array('func'=>"", 'params'=>array());
36   
37    #open the database connection
38    function open($host, $db, $user, $password){}
39   
40    #closes the database connection
41    function close(){
42        $this->execCommand($this->closeF);
43    }
44   
45    #executes a generic query
46    function executeGenericQuery($query=null){
47        if($query !== null){
48            $this->lastQuery = $query;
49        }
50        echo "\"".$this->lastQuery . "\"";
51        $this->result = $this->execCommand($this->queryF);
52    }
53   
54    function setQuery($query){
55        $this->lastQuery = $query;
56    }
57   
58    #executes a 'select' statement
59    function executeSelect($table, $fields="*", $where=null){
60        $this->lastQuery = "SELECT " . $fields . " FROM " . $table .
61            " WHERE " . $this->checkTrue($where);
62        $this->executeGenericQuery();
63    }
64   
65    #executes an 'update' statement
66    function executeUpdate($table, $fields, $where=null){
67        $this->lastQuery = "UPDATE " . $table . " SET " . $fields .
68            " WHERE " . $this->checkTrue($where);
69        $this->executeGenericQuery();
70    }
71   
72    #executes an 'insert' statement
73    function executeInsert($table, $values, $fields=null){
74        $query = "INSERT INTO ".$table;
75        $query .= ($fields === null) ? "" : " (" . $fields . ") ";
76        $query .= "VALUES (" . $values . ") ";
77       
78        $this->lastQuery = $query;
79        $this->executeGenericQuery();
80    }
81   
82    #executes a 'delete' statement
83    function executeDelete($table, $where=null){
84        $this->lastQuery = "DELETE FROM " . $table . " WHERE " .
85            $this->checkTrue($where);
86        $this->executeGenericQuery();
87    }
88   
89    #checks if $var is null (for a database)
90    function checkTrue($var, $check=null){
91        return ($var === $check) ? $this->_TRUE : $var;
92    }
93   
94    #execute a command
95    function execCommand($functionArr){
96        if(count($functionArr['params']) > 0){
97            $ret = call_user_func_array($functionArr['func'],
98                                    $functionArr['params']);
99        }else{
100            $ret = call_user_func($functionArr['func']);
101        }
102        return $ret;
103    }
104   
105    #get the number of affected rows
106    function getAffectedRows(){
107        return $this->execCommand($this->rowsAF);
108    }
109   
110    #fetch an object
111    function get(){
112        return $this->execCommand($this->fetchObjectF);
113    }
114   
115    #get the last insertion ID
116    function getLastInsertID(){
117        return $this->execCommand($this->lastInsertF);
118    }
119   
120    #fetch all rows in the database as an array of objects
121    function getArray(){
122        $ret = array();
123        while($obj = $this->get()){
124            $ret[] = $obj;
125        }
126        return $ret;
127    }
128   
129}
130?>
Note: See TracBrowser for help on using the browser.