Completed
Push — master ( f97bb0...2ac44b )
by Jean-Christophe
01:48
created

Database::isConnected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Ubiquity\db;
4
5
use Ubiquity\cache\database\DbCache;
6
use Ubiquity\exceptions\CacheException;
7
8
/**
9
 * Classe d'accès aux Bases de données encapsulant un objet PDO
10
 * @author heron
11
 * @version 1.0.0.3
12
 * @package db
13
 *
14
 */
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
27
	/**
28
	 * Constructeur
29
	 * @param string $dbName
30
	 * @param string $serverName
31
	 * @param string $port
32
	 * @param string $user
33
	 * @param string $password
34
	 * @param array $options
35
	 * @param boolean|string $cache
36
	 */
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
58
	/**
59
	 * Réalise la connexion à la base de données
60
	 */
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
66
	/**
67
	 * Exécute l'instruction SQL passée en paramètre et retourne un statement
68
	 * @param string $sql
69
	 * @return PDOStatement
70
	 */
71
	public function query($sql) {
72
		return $this->pdoObject->query($sql);
73
	}
74
75
	public function prepareAndExecute($tableName, $condition,$fields,$useCache=NULL) {
76
		$cache=(DbCache::$active && $useCache !== false) || (!DbCache::$active && $useCache === true);
77
		$result=false;
78
		if ($cache) {
79
			$result=$this->cache->fetch($tableName, $condition);
80
		}
81
		if ($result === false) {
82
			$fields=SqlUtils::getFieldList($fields);
83
			$statement=$this->getStatement("SELECT {$fields} FROM " . $tableName . $condition);
84
			$statement->execute();
85
			$result=$statement->fetchAll();
86
			$statement->closeCursor();
87
			if ($cache) {
88
				$this->cache->store($tableName, $condition, $result);
89
			}
90
		}
91
		return $result;
92
	}
93
94
	private function getStatement($sql) {
95
		if (!isset($this->statements[$sql])) {
96
			$this->statements[$sql]=$this->pdoObject->prepare($sql);
97
			$this->statements[$sql]->setFetchMode(\PDO::FETCH_ASSOC);
98
		}
99
		return $this->statements[$sql];
100
	}
101
102
	/**
103
	 * Exécute l'instruction sql $sql de mise à jour (INSERT, UPDATE ou DELETE)
104
	 * @return le nombre d'enregistrements affectés
105
	 */
106
	public function execute($sql) {
107
		return $this->pdoObject->exec($sql);
108
	}
109
110
	public function getServerName() {
111
		return $this->serverName;
112
	}
113
114
	public function setServerName($serverName) {
115
		$this->serverName=$serverName;
116
	}
117
118
	/**
119
	 * Prépare l'instruction $sql pour son exécution
120
	 * @param String $sql
121
	 * @return PDOStatement
122
	 */
123
	public function prepareStatement($sql) {
124
		return $this->pdoObject->prepare($sql);
125
	}
126
127
	/**
128
	 * Sets $value to $parameter
129
	 * @param PDOStatement $statement
130
	 * @param String $parameter
131
	 * @param mixed $value
132
	 * @return boolean
133
	 */
134
	public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
135
		return $statement->bindValue(":" . $parameter, $value);
136
	}
137
138
	/**
139
	 * Returns the last insert id
140
	 * @return integer
141
	 */
142
	public function lastInserId() {
143
		return $this->pdoObject->lastInsertId();
144
	}
145
146
	public function getTablesName() {
147
		$sql='SHOW TABLES';
148
		$query=$this->pdoObject->query($sql);
149
		return $query->fetchAll(\PDO::FETCH_COLUMN);
150
	}
151
152
	/**
153
	 * Retourne le nombre d'enregistrements de $tableName respectant la condition éventuellement passée en paramètre
154
	 * @param string $tableName
155
	 * @param string $condition Partie suivant le WHERE d'une instruction SQL
156
	 */
157
	public function count($tableName, $condition='') {
158
		if ($condition != '')
159
			$condition=" WHERE " . $condition;
160
			return $this->query("SELECT COUNT(*) FROM " . $tableName . $condition)->fetchColumn();
161
	}
162
163
	public function isConnected(){
164
		return ($this->pdoObject!==null && $this->pdoObject instanceof \PDO);
165
	}
166
167
	public function setDbType($dbType) {
168
		$this->dbType=$dbType;
169
		return $this;
170
	}
171
172
	public function getPort() {
173
		return $this->port;
174
	}
175
176
	public function getDbName() {
177
		return $this->dbName;
178
	}
179
180
	public function getUser() {
181
		return $this->user;
182
	}
183
184
	public function getPdoObject() {
185
		return $this->pdoObject;
186
	}
187
188
}
189