Ubiquity  2.0.0
php rapid development framework
Database.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\db;
4 
7 
15 class Database {
16  private $dbType;
17  private $serverName;
18  private $port;
19  private $dbName;
20  private $user;
21  private $password;
22  private $pdoObject;
23  private $statements=[ ];
24  private $cache;
25  private $options;
26 
37  public function __construct($dbType,$dbName, $serverName="localhost", $port="3306", $user="root", $password="", $options=[],$cache=false) {
38  $this->dbType=$dbType;
39  $this->dbName=$dbName;
40  $this->serverName=$serverName;
41  $this->port=$port;
42  $this->user=$user;
43  $this->password=$password;
44  $this->options=$options;
45  if ($cache !== false) {
46  if(\is_callable($cache)){
47  $this->cache=$cache();
48  }else{
49  if(\class_exists($cache)){
50  $this->cache=new $cache();
51  }else{
52  throw new CacheException($cache." is not a valid value for database cache");
53  }
54  }
55  }
56  }
57 
61  public function connect() {
62  $this->pdoObject=new \PDO($this->dbType.':host=' . $this->serverName . ';dbname=' . $this->dbName . ';charset=UTF8;port:' . $this->port, $this->user, $this->password,$this->options);
63  $this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
64  }
65 
71  public function query($sql) {
72  return $this->pdoObject->query($sql);
73  }
74 
82  public function prepareAndExecute($tableName, $condition,$fields,$useCache=NULL) {
83  $cache=(DbCache::$active && $useCache !== false) || (!DbCache::$active && $useCache === true);
84  $result=false;
85  if ($cache) {
86  $result=$this->cache->fetch($tableName, $condition);
87  }
88  if ($result === false) {
89  $fields=SqlUtils::getFieldList($fields);
90  $statement=$this->getStatement("SELECT {$fields} FROM " . $tableName . $condition);
91  $statement->execute();
92  $result=$statement->fetchAll();
93  $statement->closeCursor();
94  if ($cache) {
95  $this->cache->store($tableName, $condition, $result);
96  }
97  }
98  return $result;
99  }
100 
105  private function getStatement($sql) {
106  if (!isset($this->statements[$sql])) {
107  $this->statements[$sql]=$this->pdoObject->prepare($sql);
108  $this->statements[$sql]->setFetchMode(\PDO::FETCH_ASSOC);
109  }
110  return $this->statements[$sql];
111  }
112 
118  public function execute($sql) {
119  return $this->pdoObject->exec($sql);
120  }
121 
122  public function getServerName() {
123  return $this->serverName;
124  }
125 
126  public function setServerName($serverName) {
127  $this->serverName=$serverName;
128  }
129 
135  public function prepareStatement($sql) {
136  return $this->pdoObject->prepare($sql);
137  }
138 
146  public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
147  return $statement->bindValue(":" . $parameter, $value);
148  }
149 
154  public function lastInserId() {
155  return $this->pdoObject->lastInsertId();
156  }
157 
158  public function getTablesName() {
159  $sql='SHOW TABLES';
160  $query=$this->pdoObject->query($sql);
161  return $query->fetchAll(\PDO::FETCH_COLUMN);
162  }
163 
169  public function count($tableName, $condition='') {
170  if ($condition != '')
171  $condition=" WHERE " . $condition;
172  return $this->query("SELECT COUNT(*) FROM " . $tableName . $condition)->fetchColumn();
173  }
174 
175  public function isConnected(){
176  return ($this->pdoObject!==null && $this->pdoObject instanceof \PDO);
177  }
178 
179  public function setDbType($dbType) {
180  $this->dbType=$dbType;
181  return $this;
182  }
183 
184  public function getPort() {
185  return $this->port;
186  }
187 
188  public function getDbName() {
189  return $this->dbName;
190  }
191 
192  public function getUser() {
193  return $this->user;
194  }
195 
196  public function getPdoObject() {
197  return $this->pdoObject;
198  }
199 
200 }
connect()
Creates the PDO instance.
Definition: Database.php:61
static getFieldList($fields)
Definition: SqlUtils.php:89
setServerName($serverName)
Definition: Database.php:126
prepareStatement($sql)
Prepares a statement for execution and returns a statement object.
Definition: Database.php:135
execute($sql)
Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) ...
Definition: Database.php:118
prepareAndExecute($tableName, $condition, $fields, $useCache=NULL)
Definition: Database.php:82
query($sql)
Executes an SQL statement, returning a result set as a PDOStatement object.
Definition: Database.php:71
lastInserId()
Returns the last insert id.
Definition: Database.php:154
__construct($dbType, $dbName, $serverName="localhost", $port="3306", $user="root", $password="", $options=[], $cache=false)
Constructor.
Definition: Database.php:37
count($tableName, $condition='')
Returns the number of records in $tableName that respects the condition passed as a parameter...
Definition: Database.php:169
bindValueFromStatement(\PDOStatement $statement, $parameter, $value)
Sets $value to $parameter.
Definition: Database.php:146