- function affectedRows() { return pg_affected_rows( $this->mConn ); }
-
- # Simple UPDATE wrapper
- # Usually aborts on failure
- # If errors are explicitly ignored, returns success
- function set( $table, $var, $value, $cond, $fname = "Database::set" )
- {
- $sql = "UPDATE \"$table\" SET \"$var\" = '" .
- wfStrencode( $value ) . "' WHERE ($cond)";
- return !!$this->query( $sql, DB_WRITE, $fname );
- }
-
- # Simple SELECT wrapper, returns a single field, input must be encoded
- # Usually aborts on failure
- # If errors are explicitly ignored, returns FALSE on failure
- function get( $table, $var, $cond, $fname = "Database::get" )
- {
- $sql = "SELECT \"$var\" FROM \"$table\" WHERE ($cond)";
- $result = $this->query( $sql, DB_READ, $fname );
-
- $ret = "";
- if ( pg_num_rows( $result ) > 0 ) {
- $s = pg_fetch_object( $result );
- $ret = $s->$var;
- pg_free_result( $result );
- }
- return $ret;
- }
-
- # More complex SELECT wrapper, single row only
- # Aborts or returns FALSE on error
- # Takes an array of selected variables, and a condition map, which is ANDed
- # e.g. getArray( "cur", array( "cur_id" ), array( "cur_namespace" => 0, "cur_title" => "Astronomy" ) )
- # would return an object where $obj->cur_id is the ID of the Astronomy article
- function getArray( $table, $vars, $conds, $fname = "Database::getArray" )
- {
- $vars = implode( ",", $vars );
- $where = Database::makeList( $conds, LIST_AND );
- $sql = "SELECT \"$vars\" FROM \"$table\" WHERE $where LIMIT 1";
- $res = $this->query( $sql, $fname );
- if ( $res === false || !$this->numRows( $res ) ) {
- return false;
- }
- $obj = $this->fetchObject( $res );
- $this->freeResult( $res );
- return $obj;
- }
-
- # Removes most variables from an SQL query and replaces them with X or N for numbers.
- # It's only slightly flawed. Don't use for anything important.
- /* static */ function generalizeSQL( $sql )
- {
- # This does the same as the regexp below would do, but in such a way
- # as to avoid crashing php on some large strings.
- # $sql = preg_replace ( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql);
-
- $sql = str_replace ( "\\\\", "", $sql);
- $sql = str_replace ( "\\'", "", $sql);
- $sql = str_replace ( "\\\"", "", $sql);
- $sql = preg_replace ("/'.*'/s", "'X'", $sql);
- $sql = preg_replace ('/".*"/s', "'X'", $sql);
-
- # All newlines, tabs, etc replaced by single space
- $sql = preg_replace ( "/\s+/", " ", $sql);
-
- # All numbers => N
- $sql = preg_replace ('/-?[0-9]+/s', "N", $sql);
-
- return $sql;
- }
-
- # Determines whether a field exists in a table
- # Usually aborts on failure
- # If errors are explicitly ignored, returns NULL on failure
- function fieldExists( $table, $field, $fname = "Database::fieldExists" )
- {
- $res = $this->query( "DESCRIBE '$table'", DB_READ, $fname );
- if ( !$res ) {
- return NULL;
- }
-
- $found = false;
-
- while ( $row = $this->fetchObject( $res ) ) {
- if ( $row->Field == $field ) {
- $found = true;
- break;
- }
- }
- return $found;