Ubiquity  2.0.2
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  if(isset($options["quote"]))
45  SqlUtils::$quote=$options["quote"];
46  $this->options=$options;
47  if ($cache !== false) {
48  if(\is_callable($cache)){
49  $this->cache=$cache();
50  }else{
51  if(\class_exists($cache)){
52  $this->cache=new $cache();
53  }else{
54  throw new CacheException($cache." is not a valid value for database cache");
55  }
56  }
57  }
58  }
59 
63  public function connect() {
64  $this->pdoObject=new \PDO($this->dbType.':host=' . $this->serverName . ';dbname=' . $this->dbName . ';charset=UTF8;port:' . $this->port, $this->user, $this->password,$this->options);
65  $this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
66  }
67 
73  public function query($sql) {
74  return $this->pdoObject->query($sql);
75  }
76 
84  public function prepareAndExecute($tableName, $condition,$fields,$useCache=NULL) {
85  $cache=(DbCache::$active && $useCache !== false) || (!DbCache::$active && $useCache === true);
86  $result=false;
87  if ($cache) {
88  $result=$this->cache->fetch($tableName, $condition);
89  }
90  if ($result === false) {
91  $fields=SqlUtils::getFieldList($fields,$tableName);
92  $statement=$this->getStatement("SELECT {$fields} FROM " . $tableName . $condition);
93  $statement->execute();
94  $result=$statement->fetchAll();
95  $statement->closeCursor();
96  if ($cache) {
97  $this->cache->store($tableName, $condition, $result);
98  }
99  }
100  return $result;
101  }
102 
107  private function getStatement($sql) {
108  if (!isset($this->statements[$sql])) {
109  $this->statements[$sql]=$this->pdoObject->prepare($sql);
110  $this->statements[$sql]->setFetchMode(\PDO::FETCH_ASSOC);
111  }
112  return $this->statements[$sql];
113  }
114 
120  public function execute($sql) {
121  return $this->pdoObject->exec($sql);
122  }
123 
124  public function getServerName() {
125  return $this->serverName;
126  }
127 
128  public function setServerName($serverName) {
129  $this->serverName=$serverName;
130  }
131 
137  public function prepareStatement($sql) {
138  return $this->pdoObject->prepare($sql);
139  }
140 
148  public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
149  return $statement->bindValue(":" . $parameter, $value);
150  }
151 
156  public function lastInserId() {
157  return $this->pdoObject->lastInsertId();
158  }
159 
160  public function getTablesName() {
161  $sql='SHOW TABLES';
162  $query=$this->pdoObject->query($sql);
163  return $query->fetchAll(\PDO::FETCH_COLUMN);
164  }
165 
171  public function count($tableName, $condition='') {
172  if ($condition != '')
173  $condition=" WHERE " . $condition;
174  return $this->query("SELECT COUNT(*) FROM " . $tableName . $condition)->fetchColumn();
175  }
176 
177  public function isConnected(){
178  return ($this->pdoObject!==null && $this->pdoObject instanceof \PDO);
179  }
180 
181  public function setDbType($dbType) {
182  $this->dbType=$dbType;
183  return $this;
184  }
185 
186  public function getPort() {
187  return $this->port;
188  }
189 
190  public function getDbName() {
191  return $this->dbName;
192  }
193 
194  public function getUser() {
195  return $this->user;
196  }
197 
198  public function getPdoObject() {
199  return $this->pdoObject;
200  }
201 
202 }
connect()
Creates the PDO instance.
Definition: Database.php:63
setServerName($serverName)
Definition: Database.php:128
prepareStatement($sql)
Prepares a statement for execution and returns a statement object.
Definition: Database.php:137
execute($sql)
Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) ...
Definition: Database.php:120
prepareAndExecute($tableName, $condition, $fields, $useCache=NULL)
Definition: Database.php:84
query($sql)
Executes an SQL statement, returning a result set as a PDOStatement object.
Definition: Database.php:73
static getFieldList($fields, $tableName=false)
Definition: SqlUtils.php:91
lastInserId()
Returns the last insert id.
Definition: Database.php:156
__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:171
bindValueFromStatement(\PDOStatement $statement, $parameter, $value)
Sets $value to $parameter.
Definition: Database.php:148