Location: PHPKode > scripts > ADODB_SQL > adodbsql/class.adodb_sql.php
<?php
/*
   ADODB_SQL v1.0.0 - PHPLIB's DB_Sql format using ADODB library
   Copyright (C) 2001 RFKsolutions <hide@address.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

if( !defined( "_Class.ADODB_SQL_1_0_0_" ) ):
	define( "_Class.ADODB_SQL_1_0_0_" , 1 );

	class DB_Sql {
        var $classname = "DB_Sql";

		/* public: connection parameters */
		var $Host      = "";
		var $Database  = "";
		var $User      = "";
		var $Password  = "";
		var $ADOconnection = "";

		/* public: error number and description */
		var $Errno     = 0;
		var $Error     = "";
		var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning)

		/* public: resultset */
		var $Record    = array();
		var $Row;

		/* private: internal variables */
		var $Link_ID   = 0;
		var $Query_ID  = 0;
		var $Auto_free = 0;  // Set to 1 for free resultset
		var $Debug     = 0;  // Set to 1 for debugging messages

		/* public: constructor */
		function DB_Sql( $query = "" ) {
			$this->query($query);
		}

		/* public: rdbms selection */
		function driver( $connection = "" ) {
			$this->ADOconnection = $connection;
		}

		/* public: connection management */
 		function connect( $Database = "", $Host = "", $User = "", $Password = "" ) {
			if ( $Host == "" ) $Host = $this->Host;
			if ( $Database == "" ) $Database = $this->Database;
			if ( $User == "" ) $User = $this->User;
			if ( $Password == "" ) $Password = $this->Password;
			if ( 0 == $this->Link_ID ) {
				$this->Link_ID = NewADOConnection( $this->ADOconnection );
				if (!$this->Link_ID) {
					$this->halt("Link-ID == false, pconnect failed");
				}
				if (!$this->Link_ID->Connect( $Host, $User, $Password, $Database )) {
					$this->halt("cannot use database ".$Database);
				}
			}
		}

		/* public: perform a query */
		function query( $Query_String ) {
			if ( $Query_String == "" ) {
				return 0;
			}
			$this->connect();
			if ( $this->Debug ) {
				printf("Debug: query = %s<br>\n", $Query_String);
			}
			$this->Query_ID = $this->Link_ID->Execute( $Query_String );
			$this->Row   = 0;
			$this->Errno = $this->Link_ID->ErrorNo();
			$this->Error = $this->Link_ID->ErrorMsg();
			if (!$this->Query_ID) {
				$this->halt("Invalid SQL: ".$Query_String);
			}
			return $this->Query_ID;
		}

		/* public: walk result set */
		function next_record( ) {
			if (!$this->Query_ID) {
				$this->halt("next_record called with no query pending.");
				return 0;
			}
			$tmparr = $this->Query_ID->GetArray( 1 );
			$this->Record = $tmparr[0];
			$this->Row   += 1;
			$this->Errno = $this->Link_ID->ErrorNo();
			$this->Error = $this->Link_ID->ErrorMsg();
			$stat = is_array($this->Record);
			if (!$stat && $this->Auto_free) {
				$this->free();
			}
			return $stat;
		}

		/* public: position in result set */
		function seek( $pos = 0 ) {
			$status = $this->Query_ID->Move( $pos );
			if ($status) {
				$this->Row = $pos;
			} else {
				$this->halt("seek($pos) failed: resultset has " . $this->num_rows() . " rows");
				$this->Query_ID->Move( $this->num_rows() );
				$this->Row = $this->num_rows();
				return 0;
			}
			return 1;
		}

		/* private: error handling */
		function halt( $msg ) {
			$this->Errno = $this->Link_ID->ErrorNo();
			$this->Error = $this->Link_ID->ErrorMsg();
			if ( $this->Halt_On_Error == "no") return;
			$this->haltmsg( $msg );
			if ( $this->Halt_On_Error != "report") die("Session halted");
		}

		/* private: message error */
		function haltmsg( $msg ) {
			echo "<b>Database error:</b> $msg<br>\n";
			echo "Error (". $this->Errno ."): ". $this->Error ."<br>\n";
		}

		/* public: returns an array of ADODBFieldObject's */
		function metadata ( $table ) {
			return $this->Link_ID->MetaColumns( $table );
		}

		/* public: returns the number of rows affected by a update or delete statement */
		function affected_rows( ) {
			return $this->Link_ID->Affected_Rows();
		}

		/* public: returns the number of rows in the record set */
		function num_rows( ) {
			return $this->Query_ID->RecordCount();
		}

		/* public: returns the number of fields at query statement */
		function num_fields( ) {
			return $this->Query_ID->FieldCount();
		}

		/* public: returns the number of rows in the record set */
		function nf( ) {
			return $this->num_rows();
		}

		/* public: prints the number of rows in the record set */
		function np( ) {
			print $this->num_rows();
		}

		/* public: returns field's content from record */
		function f( $name ) {
			return $this->Record[$name];
		}

		/* public: prints field's content from record */
		function p( $name ) {
			print $this->Record[$name];
		}

		/* public: discard the result query */
		function free( ) {
			$this->Query_ID = 0;
		}

		/* public: returns Link_ID (PHPLIB 7.x)*/
		function link_id( ) {
			return $this->Link_ID;
		}

		/* public: returns Query_ID (PHPLIB 7.x)*/
		function query_id( ) {
			return $this->Query_ID;
		}

		/* public: returns an array of tables for the current database as an array (PHPLIB 7.3) */
		function table_names() {
			// next code--line for MySQL database engine
				$this->query("SHOW TABLES");
			$i = 0;
			while ( !$this->Query_ID->EOF ) {
				$return[$i]["table_name"] = $this->Query_ID->fields[0];
				$return[$i]["tablespace_name"] = $this->Database;
				$return[$i]["database"] = $this->Database;
				$i++;
				$this->Query_ID->MoveNext();
			}
			return $return;
		}
	}
endif;
?>
Return current item: ADODB_SQL