db_connection = pg_pconnect($connection_string); if ($this->have_db_connection() == FALSE) { syslog (LOG_INFO, "Failed to make a connection to Postgres"); die ("connection to Postgres failed\n"); } else { syslog (LOG_INFO, "Connection to Postgres was made correctly"); } } function have_db_connection () { /* * This simple function verifies if a connection has succesfully been made * Output: Returns TRUE if the connection was successfully * (else it will be FALSE) */ // PHP-> 4.2.Xcode: // if (pg_connection_status($this->db_connection) == PGSQL_CONNECTION_BAD) { if ($this->db_connection == '') { return "FALSE"; } else { return "TRUE"; } } function query ($inp_sql_query) { /* * This function executes a query against the active database connection * INPUT: * - $inp_sql_query: The is the string with the SQL to execute * OUTPUT: Resultstring */ $this->db_result = pg_exec ($this->db_connection, $inp_sql_query); if ($this->db_result == false) { echo "Query error in " . $inp_sql_query . "\n"; } $this->db_row_number = 0; return $this->db_result; } function Result() { return $this->db_result; } function fetch_row($result = 0) { /* * This function returns a single row as a result of a query. If the * last record has been reached the result will be FALSE otherwise * it will be TRUE * INPUT : Result resource from the query. * OUTPUT : Success TRUE/FALSE * VALUES SET : */ if ($result == 0) { // Default: use result from inside the object $result = $this->db_result; } unset ($this->db_result_row); if (pg_numrows($result) < $this->db_row_number) { $this->db_row_number=-1; return "FALSE"; } else { $this->db_result_row = pg_fetch_row($result); return "TRUE"; } } function fetch_object($result, $row) { return pg_fetch_object($result, $row); } function num_rows($db_result = 0) { /* * This functions returns the number of rows in a resultset * INPUT : Postgres result index (default = internal result).. * OUTPUT : Number of rows (number) */ if ($db_result == 0) { // Default: use result from inside the object. $db_result = $this->db_result; } return pg_numrows($db_result); // 4.2.X return pg_num_rows($db_result); } function num_fields($result = 0) { /* This function returns the number of fields in the resultset * INPUT : Result resource from the query * OUTPUT : Number of fields (number) */ if ($result == 0) { // Default: use result from inside the object $result = $this->db_result; } return pg_numfields($result); //4.2.X return pg_num_fields($result); } function Field($result, $tuple, $fieldname) { // Return the value of a specific field. return pg_result($result, $tuple, $fieldname); } /* * Create a new notification and return the id of the new record. * Return 0 if the notification can not be created. */ function new_notification($objectid, $issue, $remark) { $notif_id = 0; // Find the id of the issue $qry = "SELECT type_of_issueid, suggested_priority FROM type_of_issue WHERE name='$issue'"; $this->query($qry); if ($this->num_rows() == 1) { $issueid = $this->Field($this->Result(), 0, 'type_of_issueid'); $insertion = "insert into notification (objectid, type_of_issueid, timestamp, "; $insertion .= " statuscode, priority) values ('"; $insertion .= $objectid . "', '"; $insertion .= $issueid . "', '" . date('Y-m-d H:i:s') . "', 'new', '"; $insertion .= $this->Field($this->Result(), 0, "suggested_priority") . "')"; $this->query($insertion); $this->query("select currval('notification_notificationid_seq')"); if ($this->num_rows() == 1) { $notif_id = $this->Field($this->Result(), 0, "currval"); $insertion = "insert into action_user (actionid, username, notificationid,"; $insertion .= " timestamp, statuscode, remarks) values ('1', 'gnucomo', '"; $insertion .= $notif_id . "', '" . date('Y-m-d H:i:s') . "', 'new', '"; $insertion .= $remark . "')"; $this->query($insertion); } else { echo "Error inserting notification.\n"; } } else { echo "DATABASE ERROR: Type of issue '$issue' not found.\n"; } return $notif_id; } } ?>