Ejemplo n.º 1
0
 /**
  * @param \PDOStatement $pdoStatement
  * @param string $classname optional
  */
 public function __construct($pdoStatement, $classname = '\\StdClass')
 {
     $this->pdoStatement = $pdoStatement;
     $this->count = $this->pdoStatement->rowCount();
     $this->classname = $classname;
     $this->pdoStatement->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, $this->classname);
 }
Ejemplo n.º 2
0
 protected function _fetchAll()
 {
     switch ($this->_fetchMode) {
         case self::FETCH_DATAOBJECT:
             self::$_stmt->setFetchMode(PDO::FETCH_ASSOC);
             $rowset = new \SplFixedArray(self::$_stmt->rowCount());
             $rowClass = $this->_fetchArgument;
             foreach (self::$_stmt as $index => $data) {
                 $rowset[$index] = new $rowClass($data, true, $this->_ctorArgs);
             }
             return $rowset;
         case self::FETCH_CLASSFUNC:
             self::$_stmt->setFetchMode(PDO::FETCH_ASSOC);
             $rowset = new \SplFixedArray(self::$_stmt->rowCount());
             $classFunc = $this->_fetchArgument;
             foreach (self::$_stmt as $index => $data) {
                 $rowClass = $classFunc($data);
                 $rowset[$index] = new $rowClass($data, true, $this->_ctorArgs);
             }
             return $rowset;
         default:
             if (isset($this->_ctorArgs)) {
                 return self::$_stmt->fetchAll($this->_fetchMode, $this->_fetchArgument, $this->_ctorArgs);
             }
             if (isset($this->_fetchArgument)) {
                 return self::$_stmt->fetchAll($this->_fetchMode, $this->_fetchArgument);
             }
             if (isset($this->_fetchMode)) {
                 return self::$_stmt->fetchAll($this->_fetchMode);
             }
     }
 }
Ejemplo n.º 3
0
 /**
  * @param PDOStatement $query_result
  */
 private function treat_success($query_result)
 {
     if ($query_result->rowCount() > 1) {
         parent::set_error(1, "Mais de um item foi removido, assim, a remoção foi cancelada.");
         TTransaction::rollback();
     } elseif ($query_result->rowCount() == 0) {
         parent::set_error(1, "Nenhum item foi removido.");
     }
 }
 /**
  * @param $entityClass
  * @return array
  */
 public function getResultList($entityClass)
 {
     $this->sqlStatement->execute($this->params);
     if ($this->sqlStatement->rowCount() > 0) {
         $entities = array();
         $rows = $this->sqlStatement->fetchAll();
         foreach ($rows as $row) {
             /** @var Entity $entity */
             $entity = new $entityClass($this);
             $entity->init($row);
             array_push($entities, $entity);
         }
         return $entities;
     }
     return array();
 }
 public function affectedRows()
 {
     if (empty($this->lastStatement)) {
         return 0;
     }
     return $this->lastStatement->rowCount();
 }
Ejemplo n.º 6
0
 /**
  * @method execute
  * @param $query|string
  * @return DataBase
  */
 public function execute($query)
 {
     $this->error = false;
     if ($query) {
         if ($this->query = $this->pdo->prepare($this->escapeDoubleSpaces($query))) {
             if (count($this->paramArray)) {
                 foreach ($this->paramArray as $param => $val) {
                     if (is_null($val)) {
                         $var = PDO::PARAM_NULL;
                     } elseif (is_int($val)) {
                         $var = PDO::PARAM_INT;
                     } elseif (is_bool($val)) {
                         $var = PDO::PARAM_BOOL;
                     } else {
                         $var = PDO::PARAM_STR;
                         if ($this->queryType === self::QUERY_TYPE_UPDATE) {
                             $val = $this->wrapInSingleQuotes($val);
                         }
                     }
                     $this->query->bindValue($param, $val, $var);
                 }
                 #End Foreach
             }
             if ($this->query->execute()) {
                 $this->results = $this->query->fetchAll(PDO::FETCH_OBJ);
                 $this->count = $this->query->rowCount();
             } else {
                 $this->error = true;
             }
         }
     }
     $this->reset();
     return $this;
 }
Ejemplo n.º 7
0
 /**
  * Devolvemos la cantidad de filas afectadas por la consulta
  *
  * @todo Ver resultado en algunos tipos de base de datos que no retornan
  * el resultado esperado.
  *
  * @author Ignacio Daniel Rostagno <*****@*****.**>
  * @return int
  */
 public function num_rows()
 {
     if ($this->cant === NULL) {
         $this->cant = $this->query->rowCount();
     }
     return $this->cant;
 }
Ejemplo n.º 8
0
 /**
  * @return int
  */
 public function getLastRowCount()
 {
     if (!$this->lastPdoStatement) {
         return null;
     }
     return $this->lastPdoStatement->rowCount();
 }
Ejemplo n.º 9
0
 private function debugger()
 {
     if ($this->fpdo->debug) {
         if (!is_callable($this->fpdo->debug)) {
             $backtrace = '';
             $query = $this->getQuery();
             $parameters = $this->getParameters();
             $debug = '';
             if ($parameters) {
                 $debug = "# parameters: " . implode(", ", array_map(array($this, 'quote'), $parameters)) . "\n";
             }
             $debug .= $query;
             $pattern = '(^' . preg_quote(dirname(__FILE__)) . '(\\.php$|[/\\\\]))';
             // can be static
             foreach (debug_backtrace() as $backtrace) {
                 if (isset($backtrace["file"]) && !preg_match($pattern, $backtrace["file"])) {
                     // stop on first file outside FluentPDO source codes
                     break;
                 }
             }
             $time = sprintf('%0.3f', $this->time * 1000) . ' ms';
             $rows = $this->result ? $this->result->rowCount() : 0;
             fwrite(STDERR, "# {$backtrace['file']}:{$backtrace['line']} ({$time}; rows = {$rows})\n{$debug}\n\n");
         } else {
             call_user_func($this->fpdo->debug, $this);
         }
     }
 }
Ejemplo n.º 10
0
 /**
  * Echo/pass a debug string
  */
 private function debugger()
 {
     if ($this->fpdo->debug) {
         if (!is_callable($this->fpdo->debug)) {
             $backtrace = '';
             $query = $this->getQuery();
             $parameters = $this->getParameters();
             $debug = '';
             if ($parameters) {
                 $debug = '# parameters: ' . implode(', ', array_map(array($this, 'quote'), $parameters)) . "\n";
             }
             $debug .= $query;
             $pattern = '(^' . preg_quote(__DIR__) . '(\\.php$|[/\\\\]))';
             // can be static
             foreach (debug_backtrace() as $backtrace) {
                 if (isset($backtrace['file']) && !preg_match($pattern, $backtrace['file'])) {
                     // stop on first file outside FluentPDO source codes
                     break;
                 }
             }
             $time = sprintf('%0.3f', $this->time * 1000) . ' ms';
             $rows = $this->result ? $this->result->rowCount() : 0;
             $finalString = "# {$backtrace['file']}:{$backtrace['line']} ({$time}; rows = {$rows})\n{$debug}\n\n";
             if (is_resource(STDERR)) {
                 // if STDERR is set, send there, otherwise just output the string
                 fwrite(STDERR, $finalString);
             } else {
                 echo $finalString;
             }
         } else {
             call_user_func($this->fpdo->debug, $this);
         }
     }
 }
Ejemplo n.º 11
0
 public function rowCount()
 {
     if ($this->_statement instanceof \PDOStatement) {
         return $this->_statement->rowCount();
     }
     return null;
 }
Ejemplo n.º 12
0
 /**
  * Gets number of rows returned by a resulset
  *
  *<code>
  *  $result = $connection->query("SELECT * FROM robots ORDER BY name");
  *  echo 'There are ', $result->numRows(), ' rows in the resulset';
  *</code>
  *
  * @return int
  */
 public function numRows()
 {
     $rowCount = $this->_rowCount;
     if ($rowCount === false) {
         switch ($this->_connection->getType()) {
             case 'mysql':
                 $rowCount = $this->_pdoStatement->rowCount();
                 break;
             case 'pgsql':
                 $rowCount = $this->_pdoStatement->rowCount();
                 break;
         }
         if ($rowCount === false) {
             //SQLite/Oracle/SQLServer returns resultsets that to the client eyes (PDO) has an
             //arbitrary number of rows, so we need to perform an extra count to know that
             $sqlStatement = $this->_sqlStatement;
             //If the sql_statement starts with SELECT COUNT(*) we don't make the count
             if (strpos($sqlStatement, 'SELECT COUNT(*) ') !== 0) {
                 $bindParams = $this->_bindParams;
                 $bindTypes = $this->_bindTypes;
                 $matches = null;
                 if (preg_match("/^SELECT\\s+(.*)\$/i", $sqlStatement, $matches) == true) {
                     $rowCount = $this->_connection->query("SELECT COUNT(*) \"numrows\" FROM (SELECT " . $matches[0] . ')', $bindParams, $bindTypes)->fetch()->numRows();
                 }
             } else {
                 $rowCount = 1;
             }
         }
         //Update the value to avoid further calculations
         $this->_rowCount = $rowCount;
     }
     return $rowCount;
 }
 /**
  * Returns the number of rows affected by the last SQL statement
  *
  * @return int the number of rows.
  */
 public function getAffectedRows()
 {
     $affectedRows = 0;
     if (!is_null($this->resourceHandle)) {
         $affectedRows = $this->resourceHandle->rowCount();
     }
     return $affectedRows;
 }
Ejemplo n.º 14
0
 /**
  * Get affected rows from last SQL query
  * @return integer
  */
 function affected_rows()
 {
     if ($this->connected !== true || $this->res === null) {
         return false;
     } else {
         return $this->res->rowCount();
     }
 }
Ejemplo n.º 15
0
	/**
	 * Counts number of affected rows by the last sql statement (INSERT, UPDATE or DELETE).
	 * 
	 * @return	integer		number of affected rows
	 */
	public function getAffectedRows() {
		try {
			return $this->pdoStatement->rowCount();
		}
		catch (\PDOException $e) {
			throw new DatabaseException("Can not fetch affected rows: ".$e->getMessage(), $this);
		}
	}
 /** @param PDOStatement $query_result */
 private function treat_success($query_result)
 {
     if ($query_result->rowCount() > 0) {
         (new Delete_invitation(array('id' => parent::get_input('id_invitation'))))->run();
     } else {
         TTransaction::rollback();
     }
 }
Ejemplo n.º 17
0
 public function countAffected()
 {
     if ($this->statement) {
         return $this->statement->rowCount();
     } else {
         return 0;
     }
 }
Ejemplo n.º 18
0
 /**
  * Récupère un résultat exactement
  */
 protected function fetchOne(\PDOStatement $query)
 {
     if ($query->rowCount() != 1) {
         return false;
     } else {
         return $query->fetch();
     }
 }
Ejemplo n.º 19
0
 /** @param PDOStatement $query_result */
 private function treat_success($query_result)
 {
     if ($query_result->rowCount() > 0) {
         (new Insert_user_dummy(array('id_user' => $this->connected, 'id_dummy' => TTransaction::get_last_inserted_id())))->run();
     } else {
         TTransaction::rollback();
     }
 }
Ejemplo n.º 20
0
 public function formatOne(PDOStatement $stmt)
 {
     if ($stmt->rowCount() == 0) {
         return null;
     } else {
         return $stmt;
     }
 }
Ejemplo n.º 21
0
 /**
  * Returns the number of rows affected by the execution of the
  * last INSERT, DELETE, or UPDATE statement executed by this
  * statement object.
  *
  * @return int     The number of rows affected.
  * @throws Zend_Db_Statement_Exception
  */
 public function rowCount()
 {
     try {
         return $this->_stmt->rowCount();
     } catch (PDOException $e) {
         require_once 'Zend/Db/Statement/Exception.php';
         throw new Zend_Db_Statement_Exception($e->getMessage());
     }
 }
Ejemplo n.º 22
0
 /** @param PDOStatement $query_result */
 private function treat_success($query_result)
 {
     if ($query_result->rowCount() > 0 && TSession::try_get_value('id_user', $id_user)) {
         (new Insert_user_event(array('id_user' => $id_user, 'id_event' => TTransaction::get_last_inserted_id())))->run();
     } else {
         parent::set_error(102);
         TTransaction::rollback();
     }
 }
Ejemplo n.º 23
0
 /**
  * (non-PHPdoc)
  * @see Simplify\Db\QueryResult::numRows()
  */
 public function numRows()
 {
     if (is_int($this->stmt)) {
         $return = $this->stmt;
     } else {
         $return = $this->stmt->rowCount();
     }
     return $return;
 }
Ejemplo n.º 24
0
 /**
  * returns sets result set from query
  * @param string $sql
  * @param array $bindValues
  * @return \ErrorObject|boolean
  */
 public function query($sql, array $bindValues)
 {
     $this->stmt = $this->pdoConn->prepare($sql);
     try {
         $this->stmt->execute($bindValues);
     } catch (PDOException $e) {
         $errorObject = new ErrorObject($e->getCode(), $e->getMessage(), $e->getTrace(), $_SERVER['SCRIPT_FILENAME']);
         return $errorObject;
     }
     if ($this->stmt->columnCount() > 0) {
         $this->resultSet = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
     } else {
         $this->lastInserted = $this->pdoConn->lastInsertId();
         $this->numRows = $this->stmt->rowCount();
     }
     unset($this->stmt);
     unset($this->pdoConn);
     return true;
 }
Ejemplo n.º 25
0
 /**
  * Executes a sql call
  *
  * @abstract
  * @param string $sql The sql to execute
  *
  * @return \PDOStatement
  */
 function query($sql)
 {
     $this->setSql($sql);
     $this->lastPdoStatement = $this->pdoConnection->query($this->sql);
     if ($this->lastPdoStatement !== false) {
         $this->lastRowsEffected = $this->lastPdoStatement->rowCount();
     } else {
         $this->databaseError();
     }
     return $this->lastPdoStatement;
 }
Ejemplo n.º 26
0
 /**
  * Execute an insert, update and delete queries
  *
  * @param  array  $params   Query parameters
  * 
  * @return int              Rows affected
  * @throws RunTimeException
  */
 protected function execute(array $params = array())
 {
     try {
         // Execute the statement
         $this->pdoStatement->execute($params);
         // Return a number of rows affected
         return $this->pdoStatement->rowCount();
     } catch (\PDOException $e) {
         throw new \RunTimeException($e->getMessage());
     }
 }
Ejemplo n.º 27
0
 public function open()
 {
     if (!isset($this->dbh)) {
         $options = $this->getOptions();
         $dsn = $this->getDSN();
         try {
             $this->dbh = new \PDO($dsn, $options['username'], $options['password']);
         } catch (\PDOException $e) {
             throw new \Exception(sprintf('Can\'t connect to database %s (%s)', $dsn, $e->getMessage()));
         }
         $this->stmt = $this->dbh->query($options['query']);
         if (!$this->stmt) {
             $info = $this->dbh->errorInfo();
             throw new \PDOException($info[2], $info[1]);
         }
         if ($this->reporter) {
             $this->reporter->setTotalLines($this->stmt->rowCount());
         }
     }
 }
Ejemplo n.º 28
0
 /**
  * Eksekusi query
  *
  * @return $this
  */
 public function query($sql, $params = [])
 {
     $this->_error = false;
     if ($this->_query = $this->_pdo->prepare($sql)) {
         $pos = 1;
         if (count($params)) {
             foreach ($params as $param) {
                 $this->_query->bindValue($pos, $param);
                 $pos++;
             }
         }
         if ($this->_query->execute()) {
             $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
             $this->_count = $this->_query->rowCount();
         } else {
             $this->_error = true;
         }
     }
     return $this;
 }
Ejemplo n.º 29
0
 public function delete(\PDOStatement $stmnt, array $params)
 {
     try {
         $this->triggerListeners('beforeMutation', array($this, $stmnt, $params, DBListener::QUERY_TYPE_DELETE));
         $stmnt->execute($params);
         $this->triggerListeners('afterMutation', array($this, $stmnt, $params, DBListener::QUERY_TYPE_DELETE));
         return $stmnt->rowCount();
     } catch (\PDOException $e) {
         throw new DBException($e, $stmnt->queryString, $params);
     }
 }
Ejemplo n.º 30
0
 /**
  * Executes the statement.
  * 
  * @return boolean
  * @throws DBQueryException
  */
 public function execute()
 {
     $start = microtime(true);
     $this->query->execute();
     $end = microtime(true);
     $sql = (string) $this->query->queryString;
     $error_id = (int) $this->query->errorCode();
     if ($this->callback) {
         $time = ($end - $start) * 1000;
         call_user_func_array($this->callback, array($sql, $time));
     }
     if ($error_id) {
         $error = $this->query->errorInfo();
         $message = "Database query error: ({$error[0]}/{$error[1]}) {$error[2]} in query [{$sql}]";
         throw new DBQueryException($message, E_USER_WARNING);
         return false;
     }
     $this->key = 0;
     $this->count = $this->query->rowCount();
     $this->current = $this->query->fetch();
 }