123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php namespace System\DB;
- class Connection {
-
- public $name;
-
- public $config;
-
- public $pdo;
-
- public function __construct($name, $config, $connector)
- {
- $this->name = $name;
- $this->config = $config;
- $this->pdo = $connector->connect($this->config);
- }
-
- public function first($sql, $bindings = array())
- {
- return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null;
- }
-
- public function query($sql, $bindings = array())
- {
- $query = $this->pdo->prepare($sql);
- $result = $query->execute($bindings);
- if (strpos(strtoupper($sql), 'SELECT') === 0)
- {
- return $query->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
- }
- elseif (strpos(strtoupper($sql), 'UPDATE') === 0 or strpos(strtoupper($sql), 'DELETE') === 0)
- {
- return $query->rowCount();
- }
- else
- {
- return $result;
- }
- }
-
- public function table($table)
- {
- return new Query($table, $this);
- }
-
- public function wrapper()
- {
- if (array_key_exists('wrap', $this->config) and $this->config['wrap'] === false) return '';
- return ($this->driver() == 'mysql') ? '`' : '"';
- }
-
- public function driver()
- {
- return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
- }
-
- public function prefix()
- {
- return (array_key_exists('prefix', $this->config)) ? $this->config['prefix'] : '';
- }
- }
|