db.php 2.56 KB
Newer Older
1 2 3 4 5
<?php namespace System;

class DB {

	/**
6
	 * The established database connections.
7 8 9
	 *
	 * @var array
	 */
10
	public static $connections = array();
11 12

	/**
13 14 15 16
	 * Get a database connection. If no database name is specified, the default
	 * connection will be returned as defined in the db configuration file.
	 *
	 * Note: Database connections are managed as singletons.
17 18 19 20 21 22 23 24 25 26 27
	 *
	 * @param  string  $connection
	 * @return PDO
	 */
	public static function connection($connection = null)
	{
		if (is_null($connection))
		{
			$connection = Config::get('db.default');
		}

Taylor Otwell committed
28 29 30 31 32 33
		if ( ! array_key_exists($connection, static::$connections))
		{
			static::$connections[$connection] = DB\Connector::connect($connection);
		}

		return static::$connections[$connection];
34 35 36 37 38
	}

	/**
	 * Execute a SQL query against the connection.
	 *
39 40 41 42 43 44 45
	 * The method returns the following based on query type:
	 *
	 *     SELECT -> Array of stdClasses
	 *     UPDATE -> Number of rows affected.
	 *     DELETE -> Number of Rows affected.
	 *     ELSE   -> Boolean true / false depending on success.
	 *
46 47 48 49 50 51 52 53 54
	 * @param  string  $sql
	 * @param  array   $bindings
	 * @param  string  $connection
	 * @return mixed
	 */
	public static function query($sql, $bindings = array(), $connection = null)
	{
		$query = static::connection($connection)->prepare($sql);

55
		$result = $query->execute($bindings);
56

57
		if (strpos(strtoupper($sql), 'SELECT') === 0)
58 59 60
		{
			return $query->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
		}
61
		elseif (strpos(strtoupper($sql), 'UPDATE') === 0 or strpos(strtoupper($sql), 'DELETE') === 0)
62 63 64 65 66 67 68 69 70 71 72 73
		{
			return $query->rowCount();
		}
		else
		{
			return $result;
		}
	}

	/**
	 * Begin a fluent query against a table.
	 *
74 75
	 * This method is simply a convenient shortcut into Query::table.
	 *
76 77 78 79 80 81 82 83 84
	 * @param  string  $table
	 * @param  string  $connection
	 * @return Query
	 */
	public static function table($table, $connection = null)
	{
		return new DB\Query($table, $connection);
	}

85 86 87 88 89 90 91 92 93 94 95
	/**
	 * Get the driver name for a database connection.
	 *
	 * @param  string  $connection
	 * @return string
	 */
	public static function driver($connection = null)
	{
		return static::connection($connection)->getAttribute(\PDO::ATTR_DRIVER_NAME);
	}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	/**
	 * Get the table prefix for a database connection.
	 *
	 * @param  string  $connection
	 * @return string
	 */
	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'] : '';
	}

114
}