Ubiquity  2.0.3
php rapid development framework
Database.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\db;
4 
7 
16 class Database {
17  private $dbType;
18  private $serverName;
19  private $port;
20  private $dbName;
21  private $user;
22  private $password;
23  private $pdoObject;
24  private $statements = [ ];
25  private $cache;
26  private $options;
27 
39  public function __construct($dbType, $dbName, $serverName = "localhost", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) {
40  $this->dbType = $dbType;
41  $this->dbName = $dbName;
42  $this->serverName = $serverName;
43  $this->port = $port;
44  $this->user = $user;
45  $this->password = $password;
46  if (isset ( $options ["quote"] ))
47  SqlUtils::$quote = $options ["quote"];
48  $this->options = $options;
49  if ($cache !== false) {
50  if (\is_callable ( $cache )) {
51  $this->cache = $cache ();
52  } else {
53  if (\class_exists ( $cache )) {
54  $this->cache = new $cache ();
55  } else {
56  throw new CacheException ( $cache . " is not a valid value for database cache" );
57  }
58  }
59  }
60  }
61 
66  public function connect() {
67  try{
68  $this->_connect();
69  return true;
70  }catch (\PDOException $e){
71  return false;
72  }
73  }
74 
75  public function _connect(){
76  $this->options[\PDO::ATTR_ERRMODE]=\PDO::ERRMODE_EXCEPTION;
77  $this->pdoObject = new \PDO ( $this->getDSN(), $this->user, $this->password, $this->options );
78  }
79 
80  public function getDSN(){
81  return $this->dbType . ':dbname=' . $this->dbName . ';host=' . $this->serverName . ';charset=UTF8;port=' . $this->port;
82  }
83 
90  public function query($sql) {
91  return $this->pdoObject->query ( $sql );
92  }
93 
102  public function prepareAndExecute($tableName, $condition, $fields, $useCache = NULL) {
103  $cache = (DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true);
104  $result = false;
105  if ($cache) {
106  $result = $this->cache->fetch ( $tableName, $condition );
107  }
108  if ($result === false) {
109  $fields = SqlUtils::getFieldList ( $fields, $tableName );
110  $statement = $this->getStatement ( "SELECT {$fields} FROM " . $tableName . $condition );
111  $statement->execute ();
112  $result = $statement->fetchAll ();
113  $statement->closeCursor ();
114  if ($cache) {
115  $this->cache->store ( $tableName, $condition, $result );
116  }
117  }
118  return $result;
119  }
120 
126  private function getStatement($sql) {
127  if (! isset ( $this->statements [$sql] )) {
128  $this->statements [$sql] = $this->pdoObject->prepare ( $sql );
129  $this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC );
130  }
131  return $this->statements [$sql];
132  }
133 
140  public function execute($sql) {
141  return $this->pdoObject->exec ( $sql );
142  }
143 
144  public function getServerName() {
145  return $this->serverName;
146  }
147 
148  public function setServerName($serverName) {
149  $this->serverName = $serverName;
150  }
151 
158  public function prepareStatement($sql) {
159  return $this->pdoObject->prepare ( $sql );
160  }
161 
170  public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
171  return $statement->bindValue ( ":" . $parameter, $value );
172  }
173 
179  public function lastInserId() {
180  return $this->pdoObject->lastInsertId ();
181  }
182 
183  public function getTablesName() {
184  $sql = 'SHOW TABLES';
185  $query = $this->pdoObject->query ( $sql );
186  return $query->fetchAll ( \PDO::FETCH_COLUMN );
187  }
188 
196  public function count($tableName, $condition = '') {
197  if ($condition != '')
198  $condition = " WHERE " . $condition;
199  return $this->query ( "SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn ();
200  }
201 
202  public function queryColumn($query){
203  return $this->query ( $query )->fetchColumn ();
204  }
205 
206  public function isConnected() {
207  return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO && $this->ping());
208  }
209 
210  public function setDbType($dbType) {
211  $this->dbType = $dbType;
212  return $this;
213  }
214 
215  public function ping() {
216  return (1 === intval($this->pdoObject->query('SELECT 1')->fetchColumn(0)));
217  }
218 
219  public function getPort() {
220  return $this->port;
221  }
222 
223  public function getDbName() {
224  return $this->dbName;
225  }
226 
227  public function getUser() {
228  return $this->user;
229  }
230 
231  public function getPdoObject() {
232  return $this->pdoObject;
233  }
234 
235  public static function getAvailableDrivers(){
236  return \PDO::getAvailableDrivers();
237  }
238 }
connect()
Creates the PDO instance and realize a safe connection.
Definition: Database.php:66
static getAvailableDrivers()
Definition: Database.php:235
setServerName($serverName)
Definition: Database.php:148
prepareStatement($sql)
Prepares a statement for execution and returns a statement object.
Definition: Database.php:158
execute($sql)
Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) ...
Definition: Database.php:140
prepareAndExecute($tableName, $condition, $fields, $useCache=NULL)
Definition: Database.php:102
query($sql)
Executes an SQL statement, returning a result set as a PDOStatement object.
Definition: Database.php:90
static getFieldList($fields, $tableName=false)
Definition: SqlUtils.php:99
lastInserId()
Returns the last insert id.
Definition: Database.php:179
__construct($dbType, $dbName, $serverName="localhost", $port="3306", $user="root", $password="", $options=[], $cache=false)
Constructor.
Definition: Database.php:39
count($tableName, $condition='')
Returns the number of records in $tableName that respects the condition passed as a parameter...
Definition: Database.php:196
bindValueFromStatement(\PDOStatement $statement, $parameter, $value)
Sets $value to $parameter.
Definition: Database.php:170