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