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
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
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
22 function db_connect ($connection_string)
26 * This function makes the connection to the database. It will look at
28 * It will detected if password or hostname is missing and leave that
30 * INPUT: NONE (The database connection string)
34 $this->db_connection = pg_pconnect($connection_string);
36 if ($this->have_db_connection() == FALSE)
38 syslog (LOG_INFO, "Failed to make a connection to Postgres");
39 die ("connection to Postgres failed\n");
43 syslog (LOG_INFO, "Connection to Postgres was made correctly");
47 function have_db_connection ()
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)
57 // if (pg_connection_status($this->db_connection) == PGSQL_CONNECTION_BAD) {
59 if ($this->db_connection == '')
69 function query ($inp_sql_query)
73 * This function executes a query against the active database connection
75 * - $inp_sql_query: The is the string with the SQL to execute
76 * OUTPUT: Resultstring
79 $this->db_result = pg_exec ($this->db_connection, $inp_sql_query);
80 if ($this->db_result == false)
82 echo "Query error in " . $inp_sql_query . "\n";
84 $this->db_row_number = 0;
86 return $this->db_result;
91 return $this->db_result;
94 function fetch_row($result = 0)
98 * This function returns a single row as a result of a query. If the
99 * last record has been reached the result will be FALSE otherwise
101 * INPUT : Result resource from the query.
102 * OUTPUT : Success TRUE/FALSE
108 // Default: use result from inside the object
110 $result = $this->db_result;
112 unset ($this->db_result_row);
113 if (pg_numrows($result) < $this->db_row_number)
115 $this->db_row_number=-1;
120 $this->db_result_row = pg_fetch_row($result);
125 function fetch_object($result, $row)
127 return pg_fetch_object($result, $row);
130 function num_rows($db_result = 0)
134 * This functions returns the number of rows in a resultset
135 * INPUT : Postgres result index (default = internal result)..
136 * OUTPUT : Number of rows (number)
141 // Default: use result from inside the object.
143 $db_result = $this->db_result;
145 return pg_numrows($db_result);
147 // 4.2.X return pg_num_rows($db_result);
150 function num_fields($result = 0)
152 /* This function returns the number of fields in the resultset
153 * INPUT : Result resource from the query
154 * OUTPUT : Number of fields (number)
159 // Default: use result from inside the object
161 $result = $this->db_result;
163 return pg_numfields($result);
164 //4.2.X return pg_num_fields($result);
167 function Field($result, $tuple, $fieldname)
169 // Return the value of a specific field.
171 return pg_result($result, $tuple, $fieldname);
175 * Create a new notification and return the id of the new record.
176 * Return 0 if the notification can not be created.
179 function new_notification($objectid, $issue, $remark)
183 // Find the id of the issue
185 $qry = "SELECT type_of_issueid, suggested_priority
186 FROM type_of_issue WHERE name='$issue'";
189 if ($this->num_rows() == 1)
191 $issueid = $this->Field($this->Result(), 0, 'type_of_issueid');
192 $insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
193 $insertion .= " statuscode, priority) values ('";
194 $insertion .= $objectid . "', '";
195 $insertion .= $issueid . "', '" . date('Y-m-d H:i:s') . "', 'new', '";
196 $insertion .= $this->Field($this->Result(), 0, "suggested_priority") . "')";
198 $this->query($insertion);
200 $this->query("select currval('notification_notificationid_seq')");
202 if ($this->num_rows() == 1)
204 $notif_id = $this->Field($this->Result(), 0, "currval");
205 $insertion = "insert into action_user (actionid, username, notificationid,";
206 $insertion .= " timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
207 $insertion .= $notif_id . "', '" . date('Y-m-d H:i:s') . "', 'new', '";
208 $insertion .= $remark . "')";
210 $this->query($insertion);
214 echo "Error inserting notification.\n";
219 echo "DATABASE ERROR: Type of issue '$issue' not found.\n";