/**
  * Mock method
  *
  * @param string $sourceType destination type
  * @param string $destType   source type
  *
  * @throws RedBean_Exception_SQL
  */
 public function addConstraintForTypes($sourceType, $destType)
 {
     $exception = new RedBean_Exception_SQL();
     $exception->setSQLState($this->sqlState);
     throw $exception;
 }
Example #2
0
 /**
  * Runs a query. Internal function, available for subclasses. This method
  * runs the actual SQL query and binds a list of parameters to the query.
  * slots. The result of the query will be stored in the protected property
  * $rs (always array). The number of rows affected (result of rowcount, if supported by database)
  * is stored in protected property $affected_rows. If the debug flag is set
  * this function will send debugging output to screen buffer.
  * 
  * @throws RedBean_Exception_SQL 
  * 
  * @param string $sql     the SQL string to be send to database server
  * @param array  $aValues the values that need to get bound to the query slots
  */
 protected function runQuery($sql, $aValues)
 {
     $this->connect();
     if ($this->debug && $this->logger) {
         $this->logger->log($sql, $aValues);
     }
     try {
         if (strpos('pgsql', $this->dsn) === 0) {
             $s = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true));
         } else {
             $s = $this->pdo->prepare($sql);
         }
         $this->bindParams($s, $aValues);
         $s->execute();
         $this->affected_rows = $s->rowCount();
         if ($s->columnCount()) {
             $this->rs = $s->fetchAll();
             if ($this->debug && $this->logger) {
                 $this->logger->log('resultset: ' . count($this->rs) . ' rows');
             }
         } else {
             $this->rs = array();
         }
     } catch (PDOException $e) {
         //Unfortunately the code field is supposed to be int by default (php)
         //So we need a property to convey the SQL State code.
         $x = new RedBean_Exception_SQL($e->getMessage(), 0);
         $x->setSQLState($e->getCode());
         throw $x;
     }
 }
Example #3
0
 /**
  * (non-PHPdoc)
  * @see RedBean/RedBean_Driver#Execute()
  */
 public function Execute($sql, $aValues = array())
 {
     $this->exc = 0;
     if ($this->debug) {
         echo "<HR>" . $sql . print_r($aValues, 1);
     }
     try {
         $s = $this->pdo->prepare($sql);
         $s->execute($aValues);
         $this->affected_rows = $s->rowCount();
         return $this->affected_rows;
     } catch (PDOException $e) {
         //Unfortunately the code field is supposed to be int by default (php)
         //So we need a property to convey the SQL State code
         $x = new RedBean_Exception_SQL($e->getMessage(), 0, $e);
         $x->setSQLState($e->getCode());
         throw $x;
     }
     //
 }
Example #4
0
 /**
  * This method runs the actual SQL query and binds a list of parameters to the query.
  * slots. The result of the query will be stored in the protected property
  * $rs (always array). The number of rows affected (result of rowcount, if supported by database)
  * is stored in protected property $affectedRows. If the debug flag is set
  * this function will send debugging output to screen buffer.
  *
  * @param string $sql      the SQL string to be send to database server
  * @param array  $bindings the values that need to get bound to the query slots
  *
  * @return void
  *
  * @throws RedBean_Exception_SQL
  */
 protected function runQuery($sql, $bindings, $options = array())
 {
     $this->connect();
     if ($this->debug && $this->logger) {
         $this->logger->log($sql, $bindings);
     }
     try {
         if (strpos('pgsql', $this->dsn) === 0) {
             $statement = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => TRUE));
         } else {
             $statement = $this->pdo->prepare($sql);
         }
         $this->bindParams($statement, $bindings);
         $statement->execute();
         $this->affectedRows = $statement->rowCount();
         if ($statement->columnCount()) {
             $fetchStyle = isset($options['fetchStyle']) ? $options['fetchStyle'] : NULL;
             $this->resultArray = $statement->fetchAll($fetchStyle);
             if ($this->debug && $this->logger) {
                 $this->logger->log('resultset: ' . count($this->resultArray) . ' rows');
             }
         } else {
             $this->resultArray = array();
         }
     } catch (PDOException $e) {
         //Unfortunately the code field is supposed to be int by default (php)
         //So we need a property to convey the SQL State code.
         $err = $e->getMessage();
         if ($this->debug && $this->logger) {
             $this->logger->log('An error occurred: ' . $err);
         }
         $exception = new RedBean_Exception_SQL($err, 0);
         $exception->setSQLState($e->getCode());
         throw $exception;
     }
 }
Example #5
0
 protected function runQuery($sql, $aValues)
 {
     $this->connect();
     if ($this->debug) {
         echo "<HR>" . $sql . print_r($aValues, 1);
     }
     try {
         if (strpos("pgsql", $this->dsn) === 0) {
             $s = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true));
         } else {
             $s = $this->pdo->prepare($sql);
         }
         $this->bindParams($s, $aValues);
         $s->execute();
         $this->affected_rows = $s->rowCount();
         if ($s->columnCount()) {
             $this->rs = $s->fetchAll();
             if ($this->debug) {
                 echo '<br><b style="color:green">resultset: ' . count($this->rs) . ' rows</b>';
             }
         } else {
             $this->rs = array();
         }
     } catch (PDOException $e) {
         $x = new RedBean_Exception_SQL($e->getMessage(), 0);
         $x->setSQLState($e->getCode());
         throw $x;
     }
 }
Example #6
0
 public function Execute($sql, $aValues = array())
 {
     $this->connect();
     $this->exc = 0;
     if ($this->debug) {
         echo "<HR>" . $sql . print_r($aValues, 1);
     }
     try {
         if (strpos("pgsql", $this->dsn) === 0) {
             $s = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true));
         } else {
             $s = $this->pdo->prepare($sql);
         }
         $this->bindParams($s, $aValues);
         $s->execute();
         $this->affected_rows = $s->rowCount();
         return $this->affected_rows;
     } catch (PDOException $e) {
         if (version_compare(PHP_VERSION, '5.3.0', '<')) {
             $x = new RedBean_Exception_SQL($e->getMessage(), 0);
         } else {
             $x = new RedBean_Exception_SQL($e->getMessage() . " SQL:" . $sql, 0, $e);
         }
         $x->setSQLState($e->getCode());
         throw $x;
     }
 }
Example #7
0
	/**
	 * Executes SQL code and allows key-value binding.
	 * This function allows you to provide an array with values to bind
	 * to query parameters. For instance you can bind values to question
	 * marks in the query. Each value in the array corresponds to the
	 * question mark in the query that matches the position of the value in the
	 * array. You can also bind values using explicit keys, for instance
	 * array(":key"=>123) will bind the integer 123 to the key :key in the
	 * SQL. This method has no return value.
	 *
	 * @param string $sql	  SQL Code to execute
	 * @param array  $aValues Values to bind to SQL query
	 *
	 * @return void
	 */
	public function Execute( $sql, $aValues=array() ) {
		$this->connect();
		$this->exc = 0;
		if ($this->debug) {
			echo "<HR>" . $sql.print_r($aValues,1);
		}
		try {
			if (strpos("pgsql",$this->dsn)===0) {
				$s = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true));
			}
			else {
				$s = $this->pdo->prepare($sql);
			}
			
			if ($this->flagUseStringOnlyBinding) {
				$s->execute($aValues);
			}
			else {
			
				foreach($aValues as $key=>&$value) {
					if (is_integer($key)) {
						if (ctype_digit(strval($value)) && $value < 2147483648) { $s->bindParam($key+1,$value,PDO::PARAM_INT); }
						else $s->bindParam($key+1,$value,PDO::PARAM_STR);
					}
					else {
						if (ctype_digit(strval($value)) &&  $value < 2147483648) $s->bindParam($key,$value,PDO::PARAM_INT);
						else $s->bindParam($key,$value,PDO::PARAM_STR);
					}
				}
				$s->execute();
			}
			$this->affected_rows=$s->rowCount();
			return $this->affected_rows;
		}
		catch(PDOException $e) {
			
			
			if (version_compare(PHP_VERSION, '5.3.0', '<')) {
				$x = new RedBean_Exception_SQL( $e->getMessage(), 0);
			}
			else {
				$x = new RedBean_Exception_SQL( $e->getMessage(), 0, $e );
			}
			$x->setSQLState( $e->getCode() );
			throw $x;
		}
	}
 public function get($sql, $values = array())
 {
     $exception = new RedBean_Exception_SQL('Just a trouble maker');
     $exception->setSQLState($this->sqlState);
     throw $exception;
 }
Example #9
0
File: rb.php Project: u007/FlexiPHP
 /**
  * (non-PHPdoc)
  * @see RedBean/RedBean_Driver#Execute()
  */
 public function Execute($sql, $aValues = array())
 {
     //FlexiLogger::error(__METHOD__, "SQL: " . $sql);
     $this->exc = 0;
     if ($this->debug) {
         echo "<HR>" . $sql . print_r($aValues, 1);
     }
     try {
         if (strpos("pgsql", $this->dsn) === 0) {
             $s = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true));
         } else {
             $s = $this->pdo->prepare($sql);
         }
         $s->execute($aValues);
         $this->affected_rows = $s->rowCount();
         return $this->affected_rows;
     } catch (PDOException $e) {
         //Unfortunately the code field is supposed to be int by default (php)
         //So we need a property to convey the SQL State code.
         FlexiLogger::error(__METHOD__, "SQL: " . $sql . ", error: " . $e->getMessage());
         if (version_compare(PHP_VERSION, '5.3.0', '<')) {
             $x = new RedBean_Exception_SQL($e->getMessage(), 0);
         } else {
             $x = new RedBean_Exception_SQL($e->getMessage(), 0, $e);
         }
         $x->setSQLState($e->getCode());
         throw $x;
     }
     //
 }
Example #10
0
 /**
  * Tests FUSE and lists, FUSE enforces no more than
  * 3 sugar cubes in coffee.
  * 
  * @return void
  */
 public function testCoffeeWithSugarAndFUSE()
 {
     $coffee = R::dispense('coffee');
     $coffee->size = 'XL';
     $coffee->ownSugar = R::dispense('sugar', 5);
     $id = R::store($coffee);
     $coffee = R::load('coffee', $id);
     asrt(count($coffee->ownSugar), 3);
     $coffee->ownSugar = R::dispense('sugar', 2);
     $id = R::store($coffee);
     $coffee = R::load('coffee', $id);
     asrt(count($coffee->ownSugar), 2);
     $cocoa = R::dispense('cocoa');
     $cocoa->name = 'Fair Cocoa';
     list($taste1, $taste2) = R::dispense('taste', 2);
     $taste1->name = 'sweet';
     $taste2->name = 'bitter';
     $cocoa->ownTaste = array($taste1, $taste2);
     R::store($cocoa);
     $cocoa->name = 'Koko';
     R::store($cocoa);
     if (method_exists(R::$adapter->getDatabase(), 'getPDO')) {
         $pdo = R::$adapter->getDatabase()->getPDO();
         $driver = new RedBean_Driver_PDO($pdo);
         pass();
         asrt($pdo->getAttribute(PDO::ATTR_ERRMODE), PDO::ERRMODE_EXCEPTION);
         asrt($pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE), PDO::FETCH_ASSOC);
         asrt(strval($driver->GetCell('select 123')), '123');
     }
     $a = new RedBean_Exception_SQL();
     $a->setSqlState('test');
     $b = strval($a);
     asrt($b, '[test] - ');
 }
Example #11
0
 /**
  * Use oci binding to execute the binding and execute the query
  *
  *
  * @param string $sql      SQL Code to execute
  * @param array  $aValues  Values to bind to SQL query
  *
  * @throws RedBean_Exception_SQL
  *
  * @return void
  */
 private function doBinding($sql, $aValues = array())
 {
     foreach ($aValues as $key => $value) {
         $sql = preg_replace('/\\?/', ' :SLOT' . $key . ' ', $sql, 1);
     }
     //if we insert we fetch the inserted id
     $isInsert = preg_match('/^insert/i', $sql);
     if ($isInsert) {
         $sql .= ' RETURN ID INTO :ID';
     }
     $this->statement = oci_parse($this->connection, $sql);
     foreach ($aValues as $key => $value) {
         if (!is_int($key)) {
             $keyv = str_replace(':', '', $key);
             ${'SLOT' . $keyv} = $value;
             oci_bind_by_name($this->statement, $key, ${'SLOT' . $keyv});
         } else {
             ${'SLOT' . $key} = $value;
             oci_bind_by_name($this->statement, ':SLOT' . $key, ${'SLOT' . $key});
         }
     }
     if ($isInsert) {
         oci_bind_by_name($this->statement, ':ID', $this->lastInsertedId, 20, SQLT_INT);
     }
     if ($this->debug) {
         if (!$this->autocommit) {
             $result = oci_execute($this->statement, OCI_NO_AUTO_COMMIT);
             // data not committed
         } else {
             $result = oci_execute($this->statement);
         }
     } else {
         // no supression of warning
         if (!$this->autocommit) {
             $result = @oci_execute($this->statement, OCI_NO_AUTO_COMMIT);
             // data not committed
         } else {
             $result = @oci_execute($this->statement);
         }
     }
     if (!$result) {
         $error = oci_error($this->statement);
         $x = new RedBean_Exception_SQL($error['message'] . ':' . $error['sqltext'], 0);
         $x->setSQLState($this->mergeErrors($error['code']));
         throw $x;
     }
 }