getPdo() public method

Get the current PDO connection.
public getPdo ( ) : PDO
return PDO
 /**
  *
  * @param string $query
  * @param array $bindings
  * @param float $time
  * @param \Illuminate\Database\Connection $connection
  */
 public function addQuery($query, $bindings, $time, $connection)
 {
     $time = $time / 1000;
     $endTime = microtime(true);
     $startTime = $endTime - $time;
     $pdo = $connection->getPdo();
     $bindings = $connection->prepareBindings($bindings);
     $bindings = $this->checkBindings($bindings);
     if (!empty($bindings) && $this->renderSqlWithParams) {
         foreach ($bindings as $binding) {
             $query = preg_replace('/\\?/', $pdo->quote($binding), $query, 1);
         }
     }
     $source = null;
     if ($this->findSource) {
         try {
             $source = $this->findSource();
         } catch (\Exception $e) {
         }
     }
     $this->queries[] = array('query' => $query, 'bindings' => $bindings, 'time' => $time, 'source' => $source);
     if ($this->timeCollector !== null) {
         $this->timeCollector->addMeasure($query, $startTime, $endTime);
     }
 }
Exemplo n.º 2
0
 /**
  *
  * @param string $query
  * @param array $bindings
  * @param float $time
  * @param \Illuminate\Database\Connection $connection
  */
 public function addQuery($query, $bindings, $time, $connection)
 {
     $explainResults = array();
     $time = $time / 1000;
     $endTime = microtime(true);
     $startTime = $endTime - $time;
     $hints = $this->performQueryAnalysis($query);
     $pdo = $connection->getPdo();
     $bindings = $connection->prepareBindings($bindings);
     // Run EXPLAIN on this query (if needed)
     if ($this->explainQuery && preg_match('/^(' . implode($this->explainTypes) . ') /i', $query)) {
         $statement = $pdo->prepare('EXPLAIN ' . $query);
         $statement->execute($bindings);
         $explainResults = $statement->fetchAll(\PDO::FETCH_CLASS);
     }
     $bindings = $this->checkBindings($bindings);
     if (!empty($bindings) && $this->renderSqlWithParams) {
         foreach ($bindings as $binding) {
             $query = preg_replace('/\\?/', $pdo->quote($binding), $query, 1);
         }
     }
     $source = null;
     if ($this->findSource) {
         try {
             $source = $this->findSource();
         } catch (\Exception $e) {
         }
     }
     $this->queries[] = array('query' => $query, 'bindings' => $this->escapeBindings($bindings), 'time' => $time, 'source' => $source, 'explain' => $explainResults, 'hints' => $hints);
     if ($this->timeCollector !== null) {
         $this->timeCollector->addMeasure($query, $startTime, $endTime);
     }
 }
Exemplo n.º 3
0
 /**
  * Enures the given closur is executed within a PDO transaction.
  *
  * @param  Closure  $callback
  * @return void
  */
 public function ensureTransaction(Closure $callback)
 {
     if (!$this->connection->getPdo()->inTransaction()) {
         $this->connection->transaction($callback);
     } else {
         $callback($this->connection);
     }
 }
Exemplo n.º 4
0
 /**
  *
  * @param string $query
  * @param array $bindings
  * @param float $time
  * @param \Illuminate\Database\Connection $connection
  */
 public function addQuery($query, $bindings, $time, $connection)
 {
     $explainResults = [];
     $time = $time / 1000;
     $endTime = microtime(true);
     $startTime = $endTime - $time;
     $hints = $this->performQueryAnalysis($query);
     $pdo = $connection->getPdo();
     $bindings = $connection->prepareBindings($bindings);
     // Run EXPLAIN on this query (if needed)
     if ($this->explainQuery && preg_match('/^(' . implode($this->explainTypes) . ') /i', $query)) {
         $statement = $pdo->prepare('EXPLAIN ' . $query);
         $statement->execute($bindings);
         $explainResults = $statement->fetchAll(\PDO::FETCH_CLASS);
     }
     $bindings = $this->checkBindings($bindings);
     if (!empty($bindings) && $this->renderSqlWithParams) {
         foreach ($bindings as $key => $binding) {
             // This regex matches placeholders only, not the question marks,
             // nested in quotes, while we iterate through the bindings
             // and substitute placeholders by suitable values.
             $regex = is_numeric($key) ? "/\\?(?=(?:[^'\\\\']*'[^'\\\\']*')*[^'\\\\']*\$)/" : "/:{$key}(?=(?:[^'\\\\']*'[^'\\\\']*')*[^'\\\\']*\$)/";
             $query = preg_replace($regex, $pdo->quote($binding), $query, 1);
         }
     }
     $source = null;
     if ($this->findSource) {
         try {
             $source = $this->findSource();
         } catch (\Exception $e) {
         }
     }
     $this->queries[] = ['query' => $query, 'bindings' => $this->escapeBindings($bindings), 'time' => $time, 'source' => $source, 'explain' => $explainResults, 'connection' => $connection->getDatabaseName(), 'hints' => $this->showHints ? $hints : null];
     if ($this->timeCollector !== null) {
         $this->timeCollector->addMeasure($query, $startTime, $endTime);
     }
 }
Exemplo n.º 5
0
 /**
  * Prepare the read write mode for database connection instance.
  *
  * @param  \Illuminate\Database\Connection  $connection
  * @param  string  $type
  * @return \Illuminate\Database\Connection
  */
 protected function setPdoForType(Connection $connection, $type = null)
 {
     if ($type == 'read') {
         $connection->setPdo($connection->getReadPdo());
     } elseif ($type == 'write') {
         $connection->setReadPdo($connection->getPdo());
     }
     return $connection;
 }
 /**
  * @param null|string $connectionName
  */
 public function __construct($connectionName = null)
 {
     $connection = is_null($connectionName) ? Config::get('database.default') : DB::connection($connectionName);
     $this->connection = DB::connection($connection);
     $this->connection->getPdo()->exec('use INFORMATION_SCHEMA');
 }
Exemplo n.º 7
0
 public function getLastId()
 {
     return $this->connection->getPdo()->lastInsertId();
 }
Exemplo n.º 8
0
 public function isConnected()
 {
     return !empty($this->connection) && $this->connection->getPdo() instanceof \PDO;
 }
 public function getTraceablePdo()
 {
     return new TraceablePDO($this->db->getPdo());
 }
 public function boot(Connection $db)
 {
     $this->publishes([__DIR__ . '/config/log.php' => config_path('log.php')], 'zedisdog/mysqlHandler');
     $mysqlHandler = new MySQLHandler($db->getPdo(), config('log.table', 'log'), [], Logger::DEBUG);
     \Log::getMonolog()->pushHandler($mysqlHandler);
 }
    /**
     * @param Connection $db
     * @param array      $creds
     *
     * @return bool
     */
    protected function createDatabase($db, array $creds)
    {
        try {
            $_dbName = $creds['database'];
            if (false === $db->statement(<<<MYSQL
CREATE DATABASE IF NOT EXISTS `{$_dbName}`
MYSQL
)) {
                throw new DatabaseException(json_encode($db->getPdo()->errorInfo()));
            }
            return true;
        } catch (\Exception $_ex) {
            $this->error('[provisioning:database] create database - failure: ' . $_ex->getMessage());
            return false;
        }
    }