From 18a984c8a4dd625538e2b4cb49c5ef3faf51f232 Mon Sep 17 00:00:00 2001 From: arjen Date: Wed, 3 Sep 2003 07:02:09 +0000 Subject: [PATCH] New PHP methods: db::Result() - Returns the result from the last query. db::Field() - Returns the value of a single field. db::new_notification() - Create a new notification in the database. --- src/phpclasses/db.postgres.php | 232 ++++++++++++++++++++++++++++++----------- 1 file changed, 170 insertions(+), 62 deletions(-) diff --git a/src/phpclasses/db.postgres.php b/src/phpclasses/db.postgres.php index 9858fa3..bbe7234 100644 --- a/src/phpclasses/db.postgres.php +++ b/src/phpclasses/db.postgres.php @@ -8,21 +8,25 @@ */ //Input variables - var $db_host; //The host-name (or IP-address) to make the connection to - var $db_name; //The name of the database - var $db_user; //The username as known in the database - var $db_password; //The password used for entry + var $db_host; //The host-name (or IP-address) to make the connection to + var $db_name; //The name of the database + var $db_user; //The username as known in the database + var $db_password; //The password used for entry //Database information - var $db_connection; //The active database connection - var $db_result; //The result-set from a query - var $db_row_number; //The row-number that is currently active - var $db_result_row; //Array with in each element a field of the result + var $db_connection; //The active database connection + var $db_result; //The result-set from a query + var $db_row_number; //The row-number that is currently active + var $db_result_row; //Array with in each element a field of the result function db_connect ($connection_string) { - /* This function makes the connection to the database. It will look at the selected DBMS. - * It will detected if password or hostname is missing and leave that out of the string. + + /* + * This function makes the connection to the database. It will look at + * the selected DBMS. + * It will detected if password or hostname is missing and leave that + * out of the string. * INPUT: NONE (The database connection string) * OUTPUT: NONE */ @@ -40,46 +44,82 @@ } } - 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 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) { + 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 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); - $this->db_row_number = 0; + $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; + return $this->db_result; } - function fetch_row() { - /* 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 : NONE (everything is available within the class) - * OUTPUT : Success TRUE/FALSE - * VALUES SET : - */ - unset ($this->db_result_row); - if (pg_numrows($this->db_result) < $this->db_row_number) { - $this->db_row_number=-1; - return "FALSE"; - } else { - $this->db_result_row = pg_fetch_row($this->db_result); - return "TRUE"; - } + 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) @@ -87,30 +127,98 @@ 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) - */ + function num_rows($db_result = 0) + { - if ($db_result == 0) - { - // Default: use result from inside the object. + /* + * This functions returns the number of rows in a resultset + * INPUT : Postgres result index (default = internal result).. + * OUTPUT : Number of rows (number) + */ - $db_result = $this->db_result; - } - return pg_numrows($db_result); + 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($this->db_result); + // 4.2.X return pg_num_rows($db_result); } - function num_fields() { - /* This function returns the number of fields in the resultset - * INPUT : NONE - * OUTPUT : Number of fields (number) + 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. */ - return pg_numfields($this->db_result); - //4.2.X return pg_num_fields($this->db_result); + + 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; } } -- 2.11.0