New function to free the database result set.
[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
25     /*
26      * This function makes the connection to the database. It will look at
27      * the selected DBMS. 
28      * It will detected if password or hostname is missing and leave that
29      * out of the string.
30      * INPUT: NONE (The database connection string)
31      * OUTPUT: NONE
32      */ 
33     
34         $this->db_connection = pg_pconnect($connection_string);
35
36         if ($this->have_db_connection() == FALSE)
37         {
38            syslog (LOG_INFO, "Failed to make a connection to Postgres");
39            die ("connection to Postgres failed\n");
40         }
41         else
42         {
43            syslog (LOG_INFO, "Connection to Postgres was made correctly");
44         }
45    }
46             
47    function have_db_connection ()
48    {
49
50       /*
51        * This simple function verifies if a connection has succesfully been made
52        * Output: Returns TRUE if the connection was successfully
53        *         (else it will be FALSE)
54        */
55
56       // PHP-> 4.2.Xcode:
57       //  if (pg_connection_status($this->db_connection) == PGSQL_CONNECTION_BAD) {
58
59       if ($this->db_connection == '')
60       {
61          return "FALSE";
62       }
63       else
64       {
65          return "TRUE";
66       }
67    }
68    
69    function query ($inp_sql_query)
70    {
71
72        /*
73         * This function executes a query against the active database connection
74         * INPUT:
75         *   - $inp_sql_query: The is the string with the SQL to execute
76         * OUTPUT: Resultstring
77         */
78
79        $this->db_result =  pg_exec ($this->db_connection, $inp_sql_query);
80        if ($this->db_result == false)
81        {
82           echo "Query error in " . $inp_sql_query . "\n";
83        }
84        $this->db_row_number = 0;
85
86        return $this->db_result;
87     }
88
89     function Result()
90     {
91        return $this->db_result;
92     }
93
94     function Free($result)
95     {
96        pg_free_result($result);
97     }
98
99     function fetch_row($result = 0)
100     {
101
102        /*
103         * This function returns a single row as a result of a query. If the
104         * last record has been reached the result will be FALSE otherwise
105         * it will be TRUE
106         * INPUT      : Result resource from the query.
107         * OUTPUT     : Success TRUE/FALSE 
108         * VALUES SET :  
109         */
110
111         if ($result == 0)
112         {
113            //  Default: use result from inside the object
114
115            $result = $this->db_result;
116         }
117         unset ($this->db_result_row);
118         if (pg_numrows($result) < $this->db_row_number)
119         {
120            $this->db_row_number=-1;
121            return "FALSE";
122         }
123         else
124         {
125            $this->db_result_row = pg_fetch_row($result);
126            return "TRUE";
127         }
128     } 
129
130     function fetch_object($result, $row)
131     {
132        return pg_fetch_object($result, $row);
133     }
134
135     function num_rows($db_result = 0)
136     {
137
138        /*
139         * This functions returns the number of rows in a resultset
140         * INPUT     : Postgres result index (default = internal result)..
141         * OUTPUT    : Number of rows (number)
142         */
143
144         if ($db_result == 0)
145         {
146            //  Default: use result from inside the object.
147
148            $db_result = $this->db_result;
149         }
150         return pg_numrows($db_result);
151
152         // 4.2.X return pg_num_rows($db_result);
153     }
154
155     function num_fields($result = 0)
156     {
157        /* This function returns the number of fields in the resultset
158         * INPUT    : Result resource from the query
159         * OUTPUT   : Number of fields (number)
160         */
161
162         if ($result == 0)
163         {
164            //  Default: use result from inside the object
165
166            $result = $this->db_result;
167         }
168         return pg_numfields($result);
169         //4.2.X return pg_num_fields($result);
170     }
171
172     function Field($result, $tuple, $fieldname)
173     {
174        //   Return the value of a specific field.
175
176        return pg_result($result, $tuple, $fieldname);
177     }
178
179     /*
180      *   Create a new notification and return the id of the new record.
181      *   Return 0 if the notification can not be created.
182      */
183
184     function new_notification($objectid, $issue, $remark)
185     {
186        $notif_id = 0;
187
188        //  Find the id of the issue
189
190        $qry = "SELECT type_of_issueid, suggested_priority
191                FROM type_of_issue WHERE name='$issue'";
192
193        $this->query($qry);
194        if ($this->num_rows() == 1)
195        {
196           $issueid = $this->Field($this->Result(), 0, 'type_of_issueid');
197           $insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
198           $insertion .= "   statuscode, priority) values ('";
199           $insertion .= $objectid . "', '";
200           $insertion .= $issueid . "', '" . date('Y-m-d H:i:s') . "', 'new', '";
201           $insertion .= $this->Field($this->Result(), 0, "suggested_priority") . "')";
202
203           $this->query($insertion);
204
205           $this->query("select currval('notification_notificationid_seq')");
206
207           if ($this->num_rows() == 1)
208           {
209              $notif_id = $this->Field($this->Result(), 0, "currval");
210              $insertion = "insert into action_user (actionid, username, notificationid,";
211              $insertion .= "    timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
212              $insertion .= $notif_id . "', '" . date('Y-m-d H:i:s') . "', 'new', '";
213              $insertion .= $remark . "')";
214
215              $this->query($insertion);
216           }
217           else
218           {
219              echo "Error inserting notification.\n";
220           }
221        }
222        else
223        {
224           echo "DATABASE ERROR: Type of issue '$issue' not found.\n";
225        }
226        return $notif_id;
227     }
228
229 }
230
231 ?>