9858fa3978072ca219596b1fd28ba29114ee6aa1
[gnucomo.git] / src / phpclasses / db.postgres.php
1 <?php
2   
3   class db {
4   /* This class is an abstraction class for the database used. Initially it was
5    * written for the gnucomo-project to support PostgreSQL, but ultimately 
6    * it must be ready to be used with other database systems as well. The code
7    * will then be used to over come the specific differences between systems
8    */
9
10    //Input variables
11    var $db_host;            //The host-name (or IP-address) to make the connection to
12    var $db_name;            //The name of the database
13    var $db_user;            //The username as known in the database
14    var $db_password;        //The password used for entry
15
16    //Database information
17    var $db_connection;     //The active database connection
18    var $db_result;         //The result-set from a query
19    var $db_row_number;     //The row-number that is currently active
20    var $db_result_row;     //Array with in each element a field of the result
21
22    function db_connect ($connection_string)
23    {
24     /* This function makes the connection to the database. It will look at the selected DBMS. 
25      * It will detected if password or hostname is missing and leave that out of the string.
26      * INPUT: NONE (The database connection string)
27      * OUTPUT: NONE
28      */ 
29     
30         $this->db_connection = pg_pconnect($connection_string);
31
32         if ($this->have_db_connection() == FALSE)
33         {
34            syslog (LOG_INFO, "Failed to make a connection to Postgres");
35            die ("connection to Postgres failed\n");
36         }
37         else
38         {
39            syslog (LOG_INFO, "Connection to Postgres was made correctly");
40         }
41    }
42             
43    function have_db_connection () {
44    /* This simple function verifies if a connection has succesfully been made
45     * Output: Returns TRUE if the connection was successfully (else it will be FALSE)
46     */
47    // PHP-> 4.2.Xcode  if (pg_connection_status($this->db_connection) == PGSQL_CONNECTION_BAD) {
48    if ($this->db_connection == '') {
49        return "FALSE";
50     } else {
51        return "TRUE";
52     }
53    }
54    
55    function query ($inp_sql_query) {
56
57    /* This function executes a query against the active database connection
58     * INPUT:
59     *   - $inp_sql_query: The is the string with the SQL to execute
60     * OUTPUT: Resultstring
61     */
62         $this->db_result =  pg_exec ($this->db_connection, $inp_sql_query);
63         $this->db_row_number = 0;
64
65         return $this->db_result;
66     }
67
68     function fetch_row() {
69     /* This function returns a single row as a result of a query if the last record has been reached
70      * the result will be FALSE otherwise it will be TRUE
71      * INPUT      : NONE (everything is available within the class)
72      * OUTPUT     : Success TRUE/FALSE 
73      * VALUES SET :  
74      */
75      unset ($this->db_result_row);
76      if (pg_numrows($this->db_result) < $this->db_row_number) {
77         $this->db_row_number=-1;
78         return "FALSE";
79      }  else {
80         $this->db_result_row = pg_fetch_row($this->db_result);
81         return "TRUE";
82      }
83     } 
84
85     function fetch_object($result, $row)
86     {
87        return pg_fetch_object($result, $row);
88     }
89
90     function num_rows($db_result = 0) {
91     /* This functions returns the number of rows in a resultset
92      * INPUT     : Postgres result index (default = internal result)..
93      * OUTPUT    : Number of rows (number)
94      */
95
96      if ($db_result == 0)
97      {
98         //  Default: use result from inside the object.
99
100         $db_result = $this->db_result;
101      }
102      return pg_numrows($db_result);
103
104      // 4.2.X return pg_num_rows($this->db_result);
105     }
106
107     function num_fields() {
108     /* This function returns the number of fields in the resultset
109      * INPUT    : NONE
110      * OUTPUT   : Number of fields (number)
111      */
112      return pg_numfields($this->db_result);
113      //4.2.X return pg_num_fields($this->db_result);
114     }
115
116 }
117
118 ?>