12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php namespace System;
- class DB {
-
- private 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))
- {
- $config = Config::get('db.connections');
- if ( ! array_key_exists($connection, $config))
- {
- throw new \Exception("Database connection [$connection] is not defined.");
- }
- static::$connections[$connection] = DB\Connector::connect((object) $config[$connection]);
- }
- return static::$connections[$connection];
- }
-
- public static function query($sql, $bindings = array(), $connection = null)
- {
- $query = static::connection($connection)->prepare($sql);
- $result = $query->execute($bindings);
-
-
-
-
-
-
-
-
-
- if (strpos(Str::upper($sql), 'SELECT') === 0)
- {
- return $query->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
- }
- elseif (strpos(Str::upper($sql), 'UPDATE') === 0 or strpos(Str::upper($sql), 'DELETE') === 0)
- {
- return $query->rowCount();
- }
- else
- {
- return $result;
- }
- }
-
- public static function table($table, $connection = null)
- {
- return new DB\Query($table, $connection);
- }
- }
|