<?php namespace Laravel\Database; use PDO, PDOStatement, Laravel\Config, Laravel\Event; class Connection { /** * The raw PDO connection instance. * 原始 PDO 连接实例。 * @var PDO */ public $pdo; /** * The connection configuration array. * 连接配置数组。 * @var array */ public $config; /** * The query grammar instance for the connection. * 连接的查询语法实例。 * @var Grammars\Grammar */ protected $grammar; /** * All of the queries that have been executed on all connections. * 已在所有连接上执行的所有查询。 * @var array */ public static $queries = array(); /** * Create a new database connection instance. * 创建一个新的数据库连接实例。 * @param PDO $pdo * @param array $config * @return void */ public function __construct(PDO $pdo, $config) { $this->pdo = $pdo; $this->config = $config; } /** * Begin a fluent query against a table. * 开始对表进行流畅的查询。 * <code> * // Start a fluent query against the "users" table * $query = DB::connection()->table('users'); * * // Start a fluent query against the "users" table and get all the users * $users = DB::connection()->table('users')->get(); * </code> * * @param string $table * @return Query */ public function table($table) { return new Query($this, $this->grammar(), $table); } /** * Create a new query grammar for the connection. * 为连接创建新的查询语法。 * @return Query\Grammars\Grammar */ protected function grammar() { if (isset($this->grammar)) return $this->grammar; switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver()) { case 'mysql': return $this->grammar = new Query\Grammars\MySQL($this); case 'sqlsrv': return $this->grammar = new Query\Grammars\SQLServer($this); default: return $this->grammar = new Query\Grammars\Grammar($this); } } /** * Execute a SQL query against the connection and return a single column result. * 对连接执行 SQL 查询并返回单列结果。 * <code> * // Get the total number of rows on a table * $count = DB::connection()->only('select count(*) from users'); * * // Get the sum of payment amounts from a table * $sum = DB::connection()->only('select sum(amount) from payments') * </code> * * @param string $sql * @param array $bindings * @return mixed */ public function only($sql, $bindings = array()) { $results = (array) $this->first($sql, $bindings); return reset($results); } /** * Execute a SQL query against the connection and return the first result. * 对连接执行 SQL 查询并返回第一个结果。 * <code> * // Execute a query against the database connection * $user = DB::connection()->first('select * from users'); * * // Execute a query with bound parameters * $user = DB::connection()->first('select * from users where id = ?', array($id)); * </code> * * @param string $sql * @param array $bindings * @return object */ public function first($sql, $bindings = array()) { if (count($results = $this->query($sql, $bindings)) > 0) { return $results[0]; } } /** * Execute a SQL query and return an array of StdClass objects. * 执行 SQL 查询并返回 StdClass 对象数组。 * @param string $sql * @param array $bindings * @return array */ public function query($sql, $bindings = array()) { list($statement, $result) = $this->execute($sql, $bindings); // The result we return depends on the type of query executed against the // database. On SELECT clauses, we will return the result set, for update // and deletes we will return the affected row count. And for all other // queries we'll just return the boolean result. // 我们返回的结果取决于对数据库执行的查询类型。 // 在 SELECT 子句上,我们将返回结果集,对于更新和删除,我们将返回受影响的行数。 // 对于所有其他查询,我们将只返回布尔结果。 if (stripos($sql, 'select') === 0) { return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass'); } elseif (stripos($sql, 'update') === 0 or stripos($sql, 'delete') === 0) { return $statement->rowCount(); } else { return $result; } } /** * Execute a SQL query against the connection. * 对连接执行 SQL 查询。 * The PDO statement and boolean result will be return in an array. * PDO 语句和布尔结果将在数组中返回。 * @param string $sql * @param array $bindings * @return array */ protected function execute($sql, $bindings = array()) { $bindings = (array) $bindings; // Since expressions are injected into the query as strings, we need to // remove them from the array of bindings. After we have removed them, // we'll reset the array so there aren't gaps in the keys. // 由于表达式是作为字符串注入到查询中的,我们需要将它们从绑定数组中删除。 // 删除它们后,我们将重置数组,以便键中没有间隙。 $bindings = array_values(array_filter($bindings, function($binding) { return ! $binding instanceof Expression; })); $sql = $this->grammar()->shortcut($sql, $bindings); // Each database operation is wrapped in a try / catch so we can wrap // any database exceptions in our custom exception class, which will // set the message to include the SQL and query bindings. // 每个数据库操作都包装在 try / catch 中,因此我们可以将任何数据库异常包装在我们的自定义异常类中, // 这将设置消息以包含 SQL 和查询绑定。 try { $statement = $this->pdo->prepare($sql); $start = microtime(true); $result = $statement->execute($bindings); } // If an exception occurs, we'll pass it into our custom exception // and set the message to include the SQL and query bindings so // debugging is much easier on the developer. // 如果发生异常,我们会将其传递到我们的自定义异常中,并将消息设置为包含 SQL 和查询绑定,以便开发人员更轻松地进行调试。 catch (\Exception $exception) { $exception = new Exception($sql, $bindings, $exception); throw $exception; } // Once we have execute the query, we log the SQL, bindings, and // execution time in a static array that is accessed by all of // the connections actively being used by the application. // 执行查询后,我们将 SQL、绑定和执行时间记录在一个静态数组中,该数组由应用程序正在使用的所有连接访问。 if (Config::get('database.profile')) { $this->log($sql, $bindings, $start); } return array($statement, $result); } /** * Log the query and fire the core query event. * 记录查询并触发核心查询事件。 * @param string $sql * @param array $bindings * @param int $start * @return void */ protected function log($sql, $bindings, $start) { $time = number_format((microtime(true) - $start) * 1000, 2); Event::fire('laravel.query', array($sql, $bindings, $time)); static::$queries[] = compact('sql', 'bindings', 'time'); } /** * Get the driver name for the database connection. * 获取数据库连接的驱动程序名称。 * @return string */ public function driver() { return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME); } /** * Magic Method for dynamically beginning queries on database tables. * 动态开始数据库表查询的魔术方法。 */ public function __call($method, $parameters) { return $this->table($method); } }
github地址: https://github.com/liu-shilong/laravel3-scr