Completed
Push — master ( a48513...213d8d )
by Jean-Christophe
01:39
created

Database::setServerName()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 1
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 . ';port:' . $this->port, $this->user, $this->password,$this->options);
63
		$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
64
		$this->pdoObject->exec("SET CHARACTER SET utf8");
65
	}
66
67
	/**
68
	 * Exécute l'instruction SQL passée en paramètre et retourne un statement
69
	 * @param string $sql
70
	 * @return PDOStatement
71
	 */
72
	public function query($sql) {
73
		return $this->pdoObject->query($sql);
74
	}
75
76
	public function prepareAndExecute($tableName, $condition,$useCache=NULL) {
77
		$cache=(DbCache::$active && $useCache !== false) || (!DbCache::$active && $useCache === true);
78
		$result=false;
79
		if ($cache) {
80
			$result=$this->cache->fetch($tableName, $condition);
81
		}
82
		if ($result === false) {
83
			$statement=$this->getStatement("SELECT * 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
	protected function getFieldList($fields){
95
		if(!\is_array($fields)){
96
			return $fields;
97
		}
98
		$result=[];
99
		foreach ($fields as $field) {
100
			$result[]= "`{$field}`";
101
		}
102
		return \implode(",", $result);
103
	}
104
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
113
	/**
114
	 * Exécute l'instruction sql $sql de mise à jour (INSERT, UPDATE ou DELETE)
115
	 * @return le nombre d'enregistrements affectés
116
	 */
117
	public function execute($sql) {
118
		return $this->pdoObject->exec($sql);
119
	}
120
121
	public function getServerName() {
122
		return $this->serverName;
123
	}
124
125
	public function setServerName($serverName) {
126
		$this->serverName=$serverName;
127
	}
128
129
	/**
130
	 * Prépare l'instruction $sql pour son exécution
131
	 * @param String $sql
132
	 * @return PDOStatement
133
	 */
134
	public function prepareStatement($sql) {
135
		return $this->pdoObject->prepare($sql);
136
	}
137
138
	/**
139
	 * Sets $value to $parameter
140
	 * @param PDOStatement $statement
141
	 * @param String $parameter
142
	 * @param mixed $value
143
	 * @return boolean
144
	 */
145
	public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
146
		return $statement->bindValue(":" . $parameter, $value);
147
	}
148
149
	/**
150
	 * Returns the last insert id
151
	 * @return integer
152
	 */
153
	public function lastInserId() {
154
		return $this->pdoObject->lastInsertId();
155
	}
156
157
	public function getTablesName() {
158
		$sql='SHOW TABLES';
159
		$query=$this->pdoObject->query($sql);
160
		return $query->fetchAll(\PDO::FETCH_COLUMN);
161
	}
162
163
	public function isConnected(){
164
		return ($this->pdoObject!==null && $this->pdoObject instanceof \PDO);
165
	}
166
}
167