#native_company# #native_desc#
#native_cta#

PostgreSQL Abstraction Layer 1.0

By Cem EVK
on January 7, 2001

Version: 1.0

Type: Class

Category: Databases

License: GNU General Public License

Description: Easy way to access database and store fetched results. Database connection and query operation implemented in a separate class to play with multiple query results simultaneously over the same connection.

              <?php 
              /* 
              ##   Simple PostgreSQL Abstraction Layer v1.0 
              ##   by Cem EVK <[email protected]> 
              ##   Sturday, January 06, 2001 
              ## 
              ##   Easy way to access database and store fetched results. 
              ##   Database connection and query operation implemented in a separate 
              ##   class to play with multiple query results simultaneously over 
              ##   the same connection. 
              ## 
              ##   You can implement your own abstraction layer for other DBMSes that 
              ##   PHP supports. 
              ## 
              ##   If you find a way/idea to run this software better, 
              ##   please send it to author. 
              ##   Tanks to my colleagues. 
              ## 
              ##   Usage : 
              ##          $DB = new PGDB (); 
              ##          $sql = new QUERY ($DB); 
              ##          $sql->execute ("select name, surname, email from users where " . 
              ##                "username = '$username'"); 
              ##          if ($sql->getntuples () == 0) { 
              ##                 echo "No records found !"; 
              ##                 $DB->close (); 
              ##                 exit; 
              ##          } 
              ##          $record = $sql->gofirst (); 
              ##          $name = $record['name']; 
              ##          echo "Realname of ($username) : " . $name; 
              ##          $DB->close (); 
              */ 

              $DBNAME       = "database_name"; 
              $DBUSER        = "database_user"; 
              $DBPASSWD  = "database_user_password"; 
              $DBHOST       = "database_host"; 
              $DBPORT       = "database_port"; 

              class Abstract_DB 
              { 

              ##PRIVATE## 

                      var $db_linkid = 0; 

              ##PUBLIC## 

                      function open ($dbname, $user, $pass, $host, $port) 
                      { 
                      } 

                      function close () 
                      { 
                      } 
              } 

              class PGDB extends Abstract_DB 
              { 

                      function PGDB() 
                      { 
                              global $DBNAME; 
                              global $DBUSER; 
                              global $DBPASSWD; 
                              global $DBHOST; 
                              global $DBPORT; 

                              $this->open ($DBNAME, $DBUSER, $DBPASSWD, $DBHOST, $DBPORT); 
                      } 

                      function _PGDB() 
                      { 
                              $this->close(); 
                      } 

                      function open ($dbname, $user, $passwd, $host, $port) 
                      { 
                              $this->db_linkid = pg_connect("dbname=$dbname " . 
                                              "user=$user password=$passwd port=$port"); 
                              return $this->db_linkid; 
                      } 

                      function close () 
                      { 
                              return pg_close ($this->db_linkid); 
                      } 


              } 

              class QUERY 
              { 

              ##PRIVATE## 
                      var $dbobject; 
                      var $ntuples; 
                      var $operationresult; 
                      var $resultset; 
                      var $currentrow = 0; 

              ##PUBLIC## 

                      function QUERY ($pdbobject) 
                      { 
                              if ($pdbobject) 
                                      $this->associate ($pdbobject); 
                      } 

                       function associate ($pdbobject) 
                      { 
                              $this->dbobject = $pdbobject; 
                      } 

                      function execute ($querystring) 
                      { 
                              $this->operationresult = pg_exec ($this->dbobject->db_linkid, 
                                              $querystring); 
                              return $this->operationresult; 
                      } 

                      function getntuples () 
                      { 
                              $this->ntuples = pg_numrows ($this->operationresult); 
                              return $this->ntuples; 
                      } 

                      function getfieldname ($fieldnumber) 
                      { 
                              return pg_fieldname ($this->operationresult, $fieldnumber); 
                      } 

                      function readrow () 
                      { 
                              $this->resultset = pg_fetch_array ($this->operationresult, 
                                              $this->currentrow); 
                              return ($this->currentresultset = $this->resultset); 
                      } 

                      function gofirst () 
                      { 
                              $this->currentrow = 0; 
                              return $this->readrow (); 
                      } 

                      function golast () 
                      { 
                              $this->currentrow = ($this->getntuples ()) - 1; 
                              return $this->readrow (); 
                      } 

                      function gonext () 
                      { 
                              $this->currentrow++; 
                              if ($this->currentrow < $this->getntuples ()) { 
                                      $this->resultset = $this->readrow (); 
                                      return $this->resultset; 
                              } 
                              else 
                                      return "LAST_RECORD_REACHED"; 
                      } 

                      function goprevious () 
                      { 
                              $this->currentrow--; 
                              if ($this->currentrow >= 0) { 
                                      $this->resultset = $this->readrow (); 
                                      return $this->resultset; 
                              } 
                              else 
                                      return "FIRST_RECORD_REACHED"; 
                      } 

                      function beginTransaction () 
                      { 
                              if (!$this->execute ("BEGIN")) 
                                      return false; 
                              return true; 
                      } 

                      function commitTransaction () 
                      { 
                              if (!$this->execute ("COMMIT")) 
                                      return false; 
                              return true; 
                      } 

                      function rollbackTransaction () 
                      { 
                              if (!$this->execute ("ROLLBACK")) 
                                      return false; 
                              return true; 
                      } 

              } 
              ?>