コード例 #1
0
	function query($sql, $verbose=false){
		return new TestPostgrResult(array());
		
		$sql = trim($sql);
		if($this->_postgr_link === false){
			$this->_postgr_link = pg_connect("host=" . $this->host . " user="******" password="******" dbname=" . $this->database);
			pg_query($this->_postgr_link, "SET NAMES 'utf8'");
		}
		if($this->_postgr_link === false){
			throw new DatabaseException("could not connect to MySQL");
		};

		if($this->_query_cache->get($sql)){
			if($verbose)echo "found in cache<br>";
			$result = $this->_query_cache->get($sql);
			if(pg_num_rows($result)){
				if($verbose) echo ": seeking to 0";
				pg_result_seek($result, 0);
			}
			$ret = new PostgrResult($this->_postgr_link, $result);
			if($verbose) echo "<br>";
		}else{
			if($verbose) echo "not in cache";
			$this->_query_count++;
			/**
			 * this following line should be run once per connection to postgres
			 *
			 * i'm running it before every query. I can probably optimize this
			 * to run once per connection, but I need to do some thorough testing...
			 *
			 * http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
			 */
			pg_query($this->_postgr_link, "SET NAMES 'utf8'");
			$timer = new Timer();
			$timer->start();
			$result = pg_query($this->_postgr_link, $sql);
			$ret = new PostgrResult($this->_postgr_link, $result);
			$timer->stop();
			$time = $timer->read();
			
			if(is_object($this->logger)){
				$this->logger->log($this, ALogger::$LOW, $sql);
			}
			
			/**
			 * the query is too long! oh noes!
			 */
			if($time > .1){
				/**
				 * save the query to the DB, so I can look at it later
				 */
				if(is_object($this->logger)){
					$this->logger->longQuery($time, $sql);
				}
			}
			
			if(pg_last_error($this->_postgr_link)){
				if($verbose) echo "postgr_error: " . pg_last_error($this->_postgr_link) . "<br>";
				throw new DatabaseException(pg_last_error($this->_postgr_link));
			}
			if(strpos($sql, "SELECT") === 0){
				if($verbose) echo ": select: $sql<br><br>";
				$this->_query_cache->put($sql, $result);
			}else{
				if($verbose) echo ": not select: $sql<br>";
				if($verbose) echo "clearing cache<br>";
				$this->_query_cache->reset();
			}

		}
		return $ret;
	}
コード例 #2
0
 function query($sql, $verbose = false)
 {
     $sql = trim($sql);
     if ($this->_mysqli_link === false) {
         $this->_mysqli_link = mysqli_connect($this->host, $this->user, $this->pass, $this->database);
         mysqli_set_charset($this->_mysqli_link, "utf8");
     }
     if ($this->_mysqli_link === false) {
         throw new Exception("could not connect to MySQL");
     }
     if ($this->_query_cache->get($sql)) {
         if ($verbose) {
             echo "found in cache<br/>";
         }
         $result = $this->_query_cache->get($sql);
         if (mysqli_num_rows($result)) {
             if ($verbose) {
                 echo ": seeking to 0";
             }
             mysqli_data_seek($result, 0);
         }
         $ret = new MySQLResult($this->_mysqli_link, $result);
         if ($verbose) {
             echo "<br/>";
         }
     } else {
         if ($verbose) {
             echo "not in cache";
         }
         $this->_query_count++;
         /**
          * this following line should be run once per connection to mysql
          *
          * i'm running it before every query. I can probably optimize this
          * to run once per connection, but I need to do some thorough testing...
          *
          * http://dev.mysql.com/doc/refman/5.6/en/charset-connection.html
          */
         if (is_object($this->logger)) {
             $this->logger->log($this, ALogger::$LOW, $sql);
         }
         mysqli_set_charset($this->_mysqli_link, "utf8");
         $timer = new Timer();
         $timer->start();
         $result = mysqli_query($this->_mysqli_link, $sql);
         $ret = new MySQLResult($this->_mysqli_link, $result);
         $timer->stop();
         $time = $timer->read();
         /**
          * the query is too long! oh noes!
          */
         if ($time > 0.1) {
             /**
              * save the query to the DB, so I can look at it later
              */
             if (is_object($this->logger)) {
                 $this->logger->longQuery($time, $sql);
             }
         }
         if (mysqli_error($this->_mysqli_link)) {
             if ($verbose) {
                 echo "mysqli_error: " . mysqli_error($this->_mysqli_link) . "<br>";
             }
             throw new Exception(mysqli_error($this->_mysqli_link));
         }
         if (strpos($sql, "SELECT") === 0) {
             if ($verbose) {
                 echo ": select: {$sql}<br><br>";
             }
             $this->_query_cache->put($sql, $result);
         } else {
             if ($verbose) {
                 echo ": not select: {$sql}<br>";
             }
             if ($verbose) {
                 echo "clearing cache<br>";
             }
             $this->_query_cache->reset();
         }
     }
     return $ret;
 }