123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php namespace System;
- class DB {
-
- public static $connections = array();
-
- public static function connection($connection = null)
- {
- if (is_null($connection))
- {
- $connection = Config::get('db.default');
- }
- if ( ! array_key_exists($connection, static::$connections))
- {
- static::$connections[$connection] = DB\Connector::connect($connection);
- }
- return static::$connections[$connection];
- }
-
- public static function first($sql, $bindings = array(), $connection = null)
- {
- return (count($results = static::query($sql, $bindings, $connection)) > 0) ? $results[0] : null;
- }
-
- public static function query($sql, $bindings = array(), $connection = null)
- {
- $query = static::connection($connection)->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 static function table($table, $connection = null)
- {
- return new DB\Query($table, $connection);
- }
-
- public static function driver($connection = null)
- {
- return static::connection($connection)->getAttribute(\PDO::ATTR_DRIVER_NAME);
- }
-
- public static function prefix($connection = null)
- {
- $connections = Config::get('db.connections');
- if (is_null($connection))
- {
- $connection = Config::get('db.default');
- }
- return (array_key_exists('prefix', $connections[$connection])) ? $connections[$connection]['prefix'] : '';
- }
- }
|